[clang] [clang][bytecode][NFC] Add a FunctionKind enum (PR #125391)

2025-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr closed 
https://github.com/llvm/llvm-project/pull/125391
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][bytecode][NFC] Add a FunctionKind enum (PR #125391)

2025-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/125391

>From df8e464c9d99d65f4c171c9de7000febd438d441 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Sun, 2 Feb 2025 10:45:24 +0100
Subject: [PATCH] [clang][bytecode][NFC] Add a FunctionKind enum

Some function types are special to us, so add an enum and determinte the
function kind once when creating the function, instead of looking at the
Decl every time we need the information.
---
 clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 11 ++---
 clang/lib/AST/ByteCode/Function.cpp| 33 +--
 clang/lib/AST/ByteCode/Function.h  | 48 +++---
 3 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 19e2416c4c9422c..5bd1b73133d6545 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -135,11 +135,9 @@ Function *ByteCodeEmitter::compileFunc(const FunctionDecl 
*FuncDecl) {
   // Create a handle over the emitted code.
   Function *Func = P.getFunction(FuncDecl);
   if (!Func) {
-unsigned BuiltinID = FuncDecl->getBuiltinID();
-Func =
-P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
- std::move(ParamDescriptors), std::move(ParamOffsets),
- HasThisPointer, HasRVO, BuiltinID);
+Func = P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
+std::move(ParamDescriptors),
+std::move(ParamOffsets), HasThisPointer, HasRVO);
   }
 
   assert(Func);
@@ -212,8 +210,7 @@ Function *ByteCodeEmitter::compileObjCBlock(const BlockExpr 
*BE) {
   Function *Func =
   P.createFunction(BE, ParamOffset, std::move(ParamTypes),
std::move(ParamDescriptors), std::move(ParamOffsets),
-   /*HasThisPointer=*/false, /*HasRVO=*/false,
-   /*IsUnevaluatedBuiltin=*/false);
+   /*HasThisPointer=*/false, /*HasRVO=*/false);
 
   assert(Func);
   Func->setDefined(true);
diff --git a/clang/lib/AST/ByteCode/Function.cpp 
b/clang/lib/AST/ByteCode/Function.cpp
index 896a4fb3f9469ae..6b892dfd616c177 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -19,12 +19,28 @@ Function::Function(Program &P, FunctionDeclTy Source, 
unsigned ArgSize,
llvm::SmallVectorImpl &&ParamTypes,
llvm::DenseMap &&Params,
llvm::SmallVectorImpl &&ParamOffsets,
-   bool HasThisPointer, bool HasRVO, unsigned BuiltinID)
-: P(P), Source(Source), ArgSize(ArgSize), 
ParamTypes(std::move(ParamTypes)),
-  Params(std::move(Params)), ParamOffsets(std::move(ParamOffsets)),
-  HasThisPointer(HasThisPointer), HasRVO(HasRVO), BuiltinID(BuiltinID) {
-  if (const auto *F = Source.dyn_cast())
+   bool HasThisPointer, bool HasRVO)
+: P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
+  ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
+  ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
+  HasRVO(HasRVO) {
+  if (const auto *F = dyn_cast(Source)) {
 Variadic = F->isVariadic();
+BuiltinID = F->getBuiltinID();
+if (const auto *CD = dyn_cast(F)) {
+  Virtual = CD->isVirtual();
+  Kind = FunctionKind::Ctor;
+} else if (const auto *CD = dyn_cast(F)) {
+  Virtual = CD->isVirtual();
+  Kind = FunctionKind::Dtor;
+} else if (const auto *MD = dyn_cast(F)) {
+  Virtual = MD->isVirtual();
+  if (MD->isLambdaStaticInvoker())
+Kind = FunctionKind::LambdaStaticInvoker;
+  else if (clang::isLambdaCallOperator(F))
+Kind = FunctionKind::LambdaCallOperator;
+}
+  }
 }
 
 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
@@ -45,13 +61,6 @@ SourceInfo Function::getSource(CodePtr PC) const {
   return It->second;
 }
 
-bool Function::isVirtual() const {
-  if (const auto *M = dyn_cast_if_present(
-  Source.dyn_cast()))
-return M->isVirtual();
-  return false;
-}
-
 /// Unevaluated builtins don't get their arguments put on the stack
 /// automatically. They instead operate on the AST of their Call
 /// Expression.
diff --git a/clang/lib/AST/ByteCode/Function.h 
b/clang/lib/AST/ByteCode/Function.h
index 409a80f59f1e942..2d3421e5e612950 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -80,6 +80,13 @@ using FunctionDeclTy =
 ///
 class Function final {
 public:
+  enum class FunctionKind {
+Normal,
+Ctor,
+Dtor,
+LambdaStaticInvoker,
+LambdaCallOperator,
+  };
   using ParamDescriptor = std::pair;
 
   /// Returns the size of the function's local stack.
@@ -141,43 +148,31 @@ class Function final {
   bool isConstexpr() const 

[clang] [clang][bytecode][NFC] Add a FunctionKind enum (PR #125391)

2025-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr updated 
https://github.com/llvm/llvm-project/pull/125391

>From 5fea55e25705386d1415478c1ed2b21909b3e708 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Sun, 2 Feb 2025 10:45:24 +0100
Subject: [PATCH] [clang][bytecode][NFC] Add a FunctionKind enum

Some function types are special to us, so add an enum and determinte the
function kind once when creating the function, instead of looking at the
Decl every time we need the information.
---
 clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 11 ++
 clang/lib/AST/ByteCode/Function.cpp| 24 +---
 clang/lib/AST/ByteCode/Function.h  | 45 ++
 3 files changed, 43 insertions(+), 37 deletions(-)

diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 19e2416c4c9422c..5bd1b73133d6545 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -135,11 +135,9 @@ Function *ByteCodeEmitter::compileFunc(const FunctionDecl 
*FuncDecl) {
   // Create a handle over the emitted code.
   Function *Func = P.getFunction(FuncDecl);
   if (!Func) {
-unsigned BuiltinID = FuncDecl->getBuiltinID();
-Func =
-P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
- std::move(ParamDescriptors), std::move(ParamOffsets),
- HasThisPointer, HasRVO, BuiltinID);
+Func = P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
+std::move(ParamDescriptors),
+std::move(ParamOffsets), HasThisPointer, HasRVO);
   }
 
   assert(Func);
@@ -212,8 +210,7 @@ Function *ByteCodeEmitter::compileObjCBlock(const BlockExpr 
*BE) {
   Function *Func =
   P.createFunction(BE, ParamOffset, std::move(ParamTypes),
std::move(ParamDescriptors), std::move(ParamOffsets),
-   /*HasThisPointer=*/false, /*HasRVO=*/false,
-   /*IsUnevaluatedBuiltin=*/false);
+   /*HasThisPointer=*/false, /*HasRVO=*/false);
 
   assert(Func);
   Func->setDefined(true);
diff --git a/clang/lib/AST/ByteCode/Function.cpp 
b/clang/lib/AST/ByteCode/Function.cpp
index 896a4fb3f9469ae..22270e1312a8768 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -19,12 +19,24 @@ Function::Function(Program &P, FunctionDeclTy Source, 
unsigned ArgSize,
llvm::SmallVectorImpl &&ParamTypes,
llvm::DenseMap &&Params,
llvm::SmallVectorImpl &&ParamOffsets,
-   bool HasThisPointer, bool HasRVO, unsigned BuiltinID)
-: P(P), Source(Source), ArgSize(ArgSize), 
ParamTypes(std::move(ParamTypes)),
-  Params(std::move(Params)), ParamOffsets(std::move(ParamOffsets)),
-  HasThisPointer(HasThisPointer), HasRVO(HasRVO), BuiltinID(BuiltinID) {
-  if (const auto *F = Source.dyn_cast())
+   bool HasThisPointer, bool HasRVO)
+: P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
+  ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
+  ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
+  HasRVO(HasRVO) {
+  if (const auto *F = dyn_cast(Source)) {
 Variadic = F->isVariadic();
+BuiltinID = F->getBuiltinID();
+if (isa(F))
+  Kind = FunctionKind::Ctor;
+else if (isa(F))
+  Kind = FunctionKind::Dtor;
+else if (const auto *MD = dyn_cast(F);
+ MD && MD->isLambdaStaticInvoker())
+  Kind = FunctionKind::LambdaStaticInvoker;
+else if (clang::isLambdaCallOperator(F))
+  Kind = FunctionKind::LambdaCallOperator;
+  }
 }
 
 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
@@ -47,7 +59,7 @@ SourceInfo Function::getSource(CodePtr PC) const {
 
 bool Function::isVirtual() const {
   if (const auto *M = dyn_cast_if_present(
-  Source.dyn_cast()))
+  dyn_cast(Source)))
 return M->isVirtual();
   return false;
 }
diff --git a/clang/lib/AST/ByteCode/Function.h 
b/clang/lib/AST/ByteCode/Function.h
index 409a80f59f1e942..8de6039d8a4f399 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -80,6 +80,13 @@ using FunctionDeclTy =
 ///
 class Function final {
 public:
+  enum class FunctionKind {
+Normal,
+Ctor,
+Dtor,
+LambdaStaticInvoker,
+LambdaCallOperator,
+  };
   using ParamDescriptor = std::pair;
 
   /// Returns the size of the function's local stack.
@@ -144,40 +151,28 @@ class Function final {
   bool isVirtual() const;
 
   /// Checks if the function is a constructor.
-  bool isConstructor() const {
-return isa_and_nonnull(
-dyn_cast(Source));
-  }
+  bool isConstructor() const { return Kind == FunctionKind::Ctor; }
   /// Checks if the function is a destructor.
-  bool isDestructor() const {
-return isa_and_no

[clang] [clang][bytecode][NFC] Add a FunctionKind enum (PR #125391)

2025-02-02 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)


Changes

Some function types are special to us, so add an enum and determinte the 
function kind once when creating the function, instead of looking at the Decl 
every time we need the information.

---
Full diff: https://github.com/llvm/llvm-project/pull/125391.diff


3 Files Affected:

- (modified) clang/lib/AST/ByteCode/ByteCodeEmitter.cpp (+4-7) 
- (modified) clang/lib/AST/ByteCode/Function.cpp (+17-5) 
- (modified) clang/lib/AST/ByteCode/Function.h (+20-23) 


``diff
diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 19e2416c4c9422..5bd1b73133d654 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -135,11 +135,9 @@ Function *ByteCodeEmitter::compileFunc(const FunctionDecl 
*FuncDecl) {
   // Create a handle over the emitted code.
   Function *Func = P.getFunction(FuncDecl);
   if (!Func) {
-unsigned BuiltinID = FuncDecl->getBuiltinID();
-Func =
-P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
- std::move(ParamDescriptors), std::move(ParamOffsets),
- HasThisPointer, HasRVO, BuiltinID);
+Func = P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
+std::move(ParamDescriptors),
+std::move(ParamOffsets), HasThisPointer, HasRVO);
   }
 
   assert(Func);
@@ -212,8 +210,7 @@ Function *ByteCodeEmitter::compileObjCBlock(const BlockExpr 
*BE) {
   Function *Func =
   P.createFunction(BE, ParamOffset, std::move(ParamTypes),
std::move(ParamDescriptors), std::move(ParamOffsets),
-   /*HasThisPointer=*/false, /*HasRVO=*/false,
-   /*IsUnevaluatedBuiltin=*/false);
+   /*HasThisPointer=*/false, /*HasRVO=*/false);
 
   assert(Func);
   Func->setDefined(true);
diff --git a/clang/lib/AST/ByteCode/Function.cpp 
b/clang/lib/AST/ByteCode/Function.cpp
index 896a4fb3f9469a..5207894c266ac8 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -19,12 +19,24 @@ Function::Function(Program &P, FunctionDeclTy Source, 
unsigned ArgSize,
llvm::SmallVectorImpl &&ParamTypes,
llvm::DenseMap &&Params,
llvm::SmallVectorImpl &&ParamOffsets,
-   bool HasThisPointer, bool HasRVO, unsigned BuiltinID)
-: P(P), Source(Source), ArgSize(ArgSize), 
ParamTypes(std::move(ParamTypes)),
-  Params(std::move(Params)), ParamOffsets(std::move(ParamOffsets)),
-  HasThisPointer(HasThisPointer), HasRVO(HasRVO), BuiltinID(BuiltinID) {
-  if (const auto *F = Source.dyn_cast())
+   bool HasThisPointer, bool HasRVO)
+: P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
+  ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
+  ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
+  HasRVO(HasRVO) {
+  if (const auto *F = Source.dyn_cast()) {
 Variadic = F->isVariadic();
+BuiltinID = F->getBuiltinID();
+if (isa(F))
+  Kind = FunctionKind::Ctor;
+else if (isa(F))
+  Kind = FunctionKind::Dtor;
+else if (const auto *MD = dyn_cast(F);
+ MD && MD->isLambdaStaticInvoker())
+  Kind = FunctionKind::LambdaStaticInvoker;
+else if (clang::isLambdaCallOperator(F))
+  Kind = FunctionKind::LambdaCallOperator;
+  }
 }
 
 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
diff --git a/clang/lib/AST/ByteCode/Function.h 
b/clang/lib/AST/ByteCode/Function.h
index 409a80f59f1e94..696185d4a71225 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -80,6 +80,13 @@ using FunctionDeclTy =
 ///
 class Function final {
 public:
+  enum class FunctionKind {
+Normal,
+Ctor,
+Dtor,
+LambdaStaticInvoker,
+LambdaCallOperator,
+  };
   using ParamDescriptor = std::pair;
 
   /// Returns the size of the function's local stack.
@@ -144,40 +151,28 @@ class Function final {
   bool isVirtual() const;
 
   /// Checks if the function is a constructor.
-  bool isConstructor() const {
-return isa_and_nonnull(
-dyn_cast(Source));
-  }
+  bool isConstructor() const { return Kind == FunctionKind::Ctor; }
   /// Checks if the function is a destructor.
-  bool isDestructor() const {
-return isa_and_nonnull(
-dyn_cast(Source));
-  }
-
-  /// Returns the parent record decl, if any.
-  const CXXRecordDecl *getParentDecl() const {
-if (const auto *MD = dyn_cast_if_present(
-dyn_cast(Source)))
-  return MD->getParent();
-return nullptr;
-  }
+  bool isDestructor() const { return Kind == FunctionKind::Dtor; }
 
   /// Returns whether this function is a lambda static invoker,
   /// which we generate

[clang] [clang][bytecode][NFC] Add a FunctionKind enum (PR #125391)

2025-02-02 Thread Timm Baeder via cfe-commits

https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/125391

Some function types are special to us, so add an enum and determinte the 
function kind once when creating the function, instead of looking at the Decl 
every time we need the information.

>From d0925d241a8c7da8ae3dd65042f8b33e37268eec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= 
Date: Sun, 2 Feb 2025 10:45:24 +0100
Subject: [PATCH] [clang][bytecode][NFC] Add a FunctionKind enum

Some function types are special to us, so add an enum and determinte the
function kind once when creating the function, instead of looking at the
Decl every time we need the information.
---
 clang/lib/AST/ByteCode/ByteCodeEmitter.cpp | 11 ++
 clang/lib/AST/ByteCode/Function.cpp| 22 ---
 clang/lib/AST/ByteCode/Function.h  | 43 ++
 3 files changed, 41 insertions(+), 35 deletions(-)

diff --git a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp 
b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
index 19e2416c4c9422..5bd1b73133d654 100644
--- a/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
@@ -135,11 +135,9 @@ Function *ByteCodeEmitter::compileFunc(const FunctionDecl 
*FuncDecl) {
   // Create a handle over the emitted code.
   Function *Func = P.getFunction(FuncDecl);
   if (!Func) {
-unsigned BuiltinID = FuncDecl->getBuiltinID();
-Func =
-P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
- std::move(ParamDescriptors), std::move(ParamOffsets),
- HasThisPointer, HasRVO, BuiltinID);
+Func = P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
+std::move(ParamDescriptors),
+std::move(ParamOffsets), HasThisPointer, HasRVO);
   }
 
   assert(Func);
@@ -212,8 +210,7 @@ Function *ByteCodeEmitter::compileObjCBlock(const BlockExpr 
*BE) {
   Function *Func =
   P.createFunction(BE, ParamOffset, std::move(ParamTypes),
std::move(ParamDescriptors), std::move(ParamOffsets),
-   /*HasThisPointer=*/false, /*HasRVO=*/false,
-   /*IsUnevaluatedBuiltin=*/false);
+   /*HasThisPointer=*/false, /*HasRVO=*/false);
 
   assert(Func);
   Func->setDefined(true);
diff --git a/clang/lib/AST/ByteCode/Function.cpp 
b/clang/lib/AST/ByteCode/Function.cpp
index 896a4fb3f9469a..5207894c266ac8 100644
--- a/clang/lib/AST/ByteCode/Function.cpp
+++ b/clang/lib/AST/ByteCode/Function.cpp
@@ -19,12 +19,24 @@ Function::Function(Program &P, FunctionDeclTy Source, 
unsigned ArgSize,
llvm::SmallVectorImpl &&ParamTypes,
llvm::DenseMap &&Params,
llvm::SmallVectorImpl &&ParamOffsets,
-   bool HasThisPointer, bool HasRVO, unsigned BuiltinID)
-: P(P), Source(Source), ArgSize(ArgSize), 
ParamTypes(std::move(ParamTypes)),
-  Params(std::move(Params)), ParamOffsets(std::move(ParamOffsets)),
-  HasThisPointer(HasThisPointer), HasRVO(HasRVO), BuiltinID(BuiltinID) {
-  if (const auto *F = Source.dyn_cast())
+   bool HasThisPointer, bool HasRVO)
+: P(P), Kind(FunctionKind::Normal), Source(Source), ArgSize(ArgSize),
+  ParamTypes(std::move(ParamTypes)), Params(std::move(Params)),
+  ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer),
+  HasRVO(HasRVO) {
+  if (const auto *F = Source.dyn_cast()) {
 Variadic = F->isVariadic();
+BuiltinID = F->getBuiltinID();
+if (isa(F))
+  Kind = FunctionKind::Ctor;
+else if (isa(F))
+  Kind = FunctionKind::Dtor;
+else if (const auto *MD = dyn_cast(F);
+ MD && MD->isLambdaStaticInvoker())
+  Kind = FunctionKind::LambdaStaticInvoker;
+else if (clang::isLambdaCallOperator(F))
+  Kind = FunctionKind::LambdaCallOperator;
+  }
 }
 
 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const {
diff --git a/clang/lib/AST/ByteCode/Function.h 
b/clang/lib/AST/ByteCode/Function.h
index 409a80f59f1e94..696185d4a71225 100644
--- a/clang/lib/AST/ByteCode/Function.h
+++ b/clang/lib/AST/ByteCode/Function.h
@@ -80,6 +80,13 @@ using FunctionDeclTy =
 ///
 class Function final {
 public:
+  enum class FunctionKind {
+Normal,
+Ctor,
+Dtor,
+LambdaStaticInvoker,
+LambdaCallOperator,
+  };
   using ParamDescriptor = std::pair;
 
   /// Returns the size of the function's local stack.
@@ -144,40 +151,28 @@ class Function final {
   bool isVirtual() const;
 
   /// Checks if the function is a constructor.
-  bool isConstructor() const {
-return isa_and_nonnull(
-dyn_cast(Source));
-  }
+  bool isConstructor() const { return Kind == FunctionKind::Ctor; }
   /// Checks if the function is a destructor.
-  bool isDestructor() const {
-return isa_and_nonnull(
-dyn_cast(Source));
-  }
-
-  /// Returns the parent record d