Skip to content

Commit b9fef09

Browse files
authored
[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.
1 parent 2f8e4f8 commit b9fef09

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
@@ -667,8 +667,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
667667
}
668668

669669
case CK_VectorSplat: {
670-
assert(!classify(CE->getType()));
671-
assert(classify(SubExpr->getType()));
670+
assert(!canClassify(CE->getType()));
671+
assert(canClassify(SubExpr->getType()));
672672
assert(CE->getType()->isVectorType());
673673

674674
if (!Initializing) {
@@ -2070,7 +2070,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
20702070

20712071
unsigned ArgIndex = 0;
20722072
for (const Expr *Arg : Args) {
2073-
if (OptPrimType T = classify(Arg)) {
2073+
if (canClassify(Arg)) {
20742074
if (!this->visit(Arg))
20752075
return false;
20762076
} else {
@@ -3155,7 +3155,7 @@ bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
31553155
template <class Emitter>
31563156
bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
31573157
QualType T = E->getType();
3158-
assert(!classify(T));
3158+
assert(!canClassify(T));
31593159

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

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

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

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

43834383
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)