Skip to content

[clang][bytecode][NFC] Add a FunctionKind enum #125391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions clang/lib/AST/ByteCode/ByteCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 21 additions & 12 deletions clang/lib/AST/ByteCode/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,28 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
llvm::SmallVectorImpl<PrimType> &&ParamTypes,
llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
llvm::SmallVectorImpl<unsigned> &&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<const FunctionDecl *>())
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<const FunctionDecl *>(Source)) {
Variadic = F->isVariadic();
BuiltinID = F->getBuiltinID();
if (const auto *CD = dyn_cast<CXXConstructorDecl>(F)) {
Virtual = CD->isVirtual();
Kind = FunctionKind::Ctor;
} else if (const auto *CD = dyn_cast<CXXDestructorDecl>(F)) {
Virtual = CD->isVirtual();
Kind = FunctionKind::Dtor;
} else if (const auto *MD = dyn_cast<CXXMethodDecl>(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 {
Expand All @@ -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<CXXMethodDecl>(
Source.dyn_cast<const FunctionDecl *>()))
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.
Expand Down
48 changes: 23 additions & 25 deletions clang/lib/AST/ByteCode/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ using FunctionDeclTy =
///
class Function final {
public:
enum class FunctionKind {
Normal,
Ctor,
Dtor,
LambdaStaticInvoker,
LambdaCallOperator,
};
using ParamDescriptor = std::pair<PrimType, Descriptor *>;

/// Returns the size of the function's local stack.
Expand Down Expand Up @@ -141,43 +148,31 @@ class Function final {
bool isConstexpr() const { return IsValid || isLambdaStaticInvoker(); }

/// Checks if the function is virtual.
bool isVirtual() const;
bool isVirtual() const { return Virtual; };

/// Checks if the function is a constructor.
bool isConstructor() const {
return isa_and_nonnull<CXXConstructorDecl>(
dyn_cast<const FunctionDecl *>(Source));
}
bool isConstructor() const { return Kind == FunctionKind::Ctor; }
/// Checks if the function is a destructor.
bool isDestructor() const {
return isa_and_nonnull<CXXDestructorDecl>(
dyn_cast<const FunctionDecl *>(Source));
}

/// Returns the parent record decl, if any.
const CXXRecordDecl *getParentDecl() const {
if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
dyn_cast<const FunctionDecl *>(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 custom byte code for.
bool isLambdaStaticInvoker() const {
if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
dyn_cast<const FunctionDecl *>(Source)))
return MD->isLambdaStaticInvoker();
return false;
return Kind == FunctionKind::LambdaStaticInvoker;
}

/// Returns whether this function is the call operator
/// of a lambda record decl.
bool isLambdaCallOperator() const {
return Kind == FunctionKind::LambdaCallOperator;
}

/// Returns the parent record decl, if any.
const CXXRecordDecl *getParentDecl() const {
if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
dyn_cast<const FunctionDecl *>(Source)))
return clang::isLambdaCallOperator(MD);
return false;
return MD->getParent();
return nullptr;
}

/// Checks if the function is fully done compiling.
Expand Down Expand Up @@ -213,7 +208,7 @@ class Function final {

bool isThisPointerExplicit() const {
if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(
Source.dyn_cast<const FunctionDecl *>()))
dyn_cast<const FunctionDecl *>(Source)))
return MD->isExplicitObjectMemberFunction();
return false;
}
Expand All @@ -232,7 +227,7 @@ class Function final {
llvm::SmallVectorImpl<PrimType> &&ParamTypes,
llvm::DenseMap<unsigned, ParamDescriptor> &&Params,
llvm::SmallVectorImpl<unsigned> &&ParamOffsets, bool HasThisPointer,
bool HasRVO, unsigned BuiltinID);
bool HasRVO);

/// Sets the code of a function.
void setCode(unsigned NewFrameSize, std::vector<std::byte> &&NewCode,
Expand All @@ -255,6 +250,8 @@ class Function final {

/// Program reference.
Program &P;
/// Function Kind.
FunctionKind Kind;
/// Declaration this function was compiled from.
FunctionDeclTy Source;
/// Local area size: storage + metadata.
Expand Down Expand Up @@ -289,6 +286,7 @@ class Function final {
bool HasBody = false;
bool Defined = false;
bool Variadic = false;
bool Virtual = false;
unsigned BuiltinID = 0;

public:
Expand Down