Skip to content

Commit ed6587b

Browse files
committed
[clang][bytecode] Add canClassify() helper
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.
1 parent 3ea76af commit ed6587b

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
666666
}
667667

668668
case CK_VectorSplat: {
669-
assert(!classify(CE->getType()));
670-
assert(classify(SubExpr->getType()));
669+
assert(!canClassify(CE->getType()));
670+
assert(canClassify(SubExpr->getType()));
671671
assert(CE->getType()->isVectorType());
672672

673673
if (!Initializing) {
@@ -2069,7 +2069,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
20692069

20702070
unsigned ArgIndex = 0;
20712071
for (const Expr *Arg : Args) {
2072-
if (OptPrimType T = classify(Arg)) {
2072+
if (canClassify(Arg)) {
20732073
if (!this->visit(Arg))
20742074
return false;
20752075
} else {
@@ -3154,7 +3154,7 @@ bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
31543154
template <class Emitter>
31553155
bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
31563156
QualType T = E->getType();
3157-
assert(!classify(T));
3157+
assert(!canClassify(T));
31583158

31593159
if (T->isRecordType()) {
31603160
const CXXConstructorDecl *Ctor = E->getConstructor();
@@ -4149,7 +4149,7 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
41494149

41504150
// Create local variable to hold the return value.
41514151
if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
4152-
!classify(E->getType())) {
4152+
!canClassify(E->getType())) {
41534153
std::optional<unsigned> LocalIndex = allocateLocal(E);
41544154
if (!LocalIndex)
41554155
return false;
@@ -4169,7 +4169,7 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
41694169

41704170
template <class Emitter>
41714171
bool Compiler<Emitter>::visitInitializer(const Expr *E) {
4172-
assert(!classify(E->getType()));
4172+
assert(!canClassify(E->getType()));
41734173

41744174
OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
41754175
/*NewInitializing=*/true);
@@ -4376,7 +4376,7 @@ bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
43764376
template <class Emitter>
43774377
bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
43784378
const Expr *E) {
4379-
if (!classify(E->getType()))
4379+
if (!canClassify(E->getType()))
43804380
return false;
43814381

43824382
if (!this->visit(RHS))

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
256256

257257
OptPrimType classify(const Expr *E) const { return Ctx.classify(E); }
258258
OptPrimType classify(QualType Ty) const { return Ctx.classify(Ty); }
259+
bool canClassify(const Expr *E) const { return Ctx.canClassify(E); }
260+
bool canClassify(QualType T) const { return Ctx.canClassify(T); }
259261

260262
/// Classifies a known primitive type.
261263
PrimType classifyPrim(QualType Ty) const {

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FuncDecl) {
501501
// elsewhere in the code.
502502
QualType Ty = FuncDecl->getReturnType();
503503
bool HasRVO = false;
504-
if (!Ty->isVoidType() && !classify(Ty)) {
504+
if (!Ty->isVoidType() && !canClassify(Ty)) {
505505
HasRVO = true;
506506
ParamTypes.push_back(PT_Ptr);
507507
ParamOffsets.push_back(ParamOffset);

clang/lib/AST/ByteCode/Context.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ class Context final {
9393
return classify(E->getType());
9494
}
9595

96+
bool canClassify(QualType T) {
97+
if (const auto *BT = dyn_cast<BuiltinType>(T)) {
98+
if (BT->isInteger() || BT->isFloatingPoint())
99+
return true;
100+
if (BT->getKind() == BuiltinType::Bool)
101+
return true;
102+
}
103+
104+
if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
105+
T->isVectorType())
106+
return false;
107+
return classify(T) != std::nullopt;
108+
}
109+
bool canClassify(const Expr *E) {
110+
if (E->isGLValue())
111+
return true;
112+
return canClassify(E->getType());
113+
}
114+
96115
const CXXMethodDecl *
97116
getOverridingFunction(const CXXRecordDecl *DynamicDecl,
98117
const CXXRecordDecl *StaticDecl,

0 commit comments

Comments
 (0)