Author: Timm Baeder Date: 2025-08-09T15:08:14+02:00 New Revision: b9fef09e8e5144c9d7e43a64df6df374efc91c90
URL: https://github.com/llvm/llvm-project/commit/b9fef09e8e5144c9d7e43a64df6df374efc91c90 DIFF: https://github.com/llvm/llvm-project/commit/b9fef09e8e5144c9d7e43a64df6df374efc91c90.diff LOG: [clang][bytecode] Add canClassify() helper (#152755) Sometimes we don't need the return value of a classsify() call, we only need to know if we can map the given Expr/Type to a primitive type. Add canClassify() for that. Added: Modified: clang/lib/AST/ByteCode/Compiler.cpp clang/lib/AST/ByteCode/Compiler.h clang/lib/AST/ByteCode/Context.cpp clang/lib/AST/ByteCode/Context.h Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 839e39f6c0c9a..9db2011417e71 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -667,8 +667,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) { } case CK_VectorSplat: { - assert(!classify(CE->getType())); - assert(classify(SubExpr->getType())); + assert(!canClassify(CE->getType())); + assert(canClassify(SubExpr->getType())); assert(CE->getType()->isVectorType()); if (!Initializing) { @@ -2070,7 +2070,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args, unsigned ArgIndex = 0; for (const Expr *Arg : Args) { - if (OptPrimType T = classify(Arg)) { + if (canClassify(Arg)) { if (!this->visit(Arg)) return false; } else { @@ -3155,7 +3155,7 @@ bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { template <class Emitter> bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) { QualType T = E->getType(); - assert(!classify(T)); + assert(!canClassify(T)); if (T->isRecordType()) { const CXXConstructorDecl *Ctor = E->getConstructor(); @@ -4150,7 +4150,7 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) { // Create local variable to hold the return value. if (!E->isGLValue() && !E->getType()->isAnyComplexType() && - !classify(E->getType())) { + !canClassify(E->getType())) { std::optional<unsigned> LocalIndex = allocateLocal(E); if (!LocalIndex) return false; @@ -4170,7 +4170,7 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) { template <class Emitter> bool Compiler<Emitter>::visitInitializer(const Expr *E) { - assert(!classify(E->getType())); + assert(!canClassify(E->getType())); OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false, /*NewInitializing=*/true); @@ -4377,7 +4377,7 @@ bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) { template <class Emitter> bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS, const Expr *E) { - if (!classify(E->getType())) + if (!canClassify(E->getType())) return false; if (!this->visit(RHS)) diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h index ee8327d8fee1e..d72ffa13f6b9d 100644 --- a/clang/lib/AST/ByteCode/Compiler.h +++ b/clang/lib/AST/ByteCode/Compiler.h @@ -256,6 +256,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>, OptPrimType classify(const Expr *E) const { return Ctx.classify(E); } OptPrimType classify(QualType Ty) const { return Ctx.classify(Ty); } + bool canClassify(const Expr *E) const { return Ctx.canClassify(E); } + bool canClassify(QualType T) const { return Ctx.canClassify(T); } /// Classifies a known primitive type. PrimType classifyPrim(QualType Ty) const { diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp index 43d30a1b5651d..6343b2af313f1 100644 --- a/clang/lib/AST/ByteCode/Context.cpp +++ b/clang/lib/AST/ByteCode/Context.cpp @@ -501,7 +501,7 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FuncDecl) { // elsewhere in the code. QualType Ty = FuncDecl->getReturnType(); bool HasRVO = false; - if (!Ty->isVoidType() && !classify(Ty)) { + if (!Ty->isVoidType() && !canClassify(Ty)) { HasRVO = true; ParamTypes.push_back(PT_Ptr); ParamOffsets.push_back(ParamOffset); diff --git a/clang/lib/AST/ByteCode/Context.h b/clang/lib/AST/ByteCode/Context.h index 1c084acbe916b..a6d90bb385067 100644 --- a/clang/lib/AST/ByteCode/Context.h +++ b/clang/lib/AST/ByteCode/Context.h @@ -93,6 +93,25 @@ class Context final { return classify(E->getType()); } + bool canClassify(QualType T) { + if (const auto *BT = dyn_cast<BuiltinType>(T)) { + if (BT->isInteger() || BT->isFloatingPoint()) + return true; + if (BT->getKind() == BuiltinType::Bool) + return true; + } + + if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() || + T->isVectorType()) + return false; + return classify(T) != std::nullopt; + } + bool canClassify(const Expr *E) { + if (E->isGLValue()) + return true; + return canClassify(E->getType()); + } + const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits