Skip to content

Commit ba9edf8

Browse files
committed
[clang][bytecode] Fix 'if consteval' in non-constant contexts
The previous code made this a compile-time decision but it's not.
1 parent 7180170 commit ba9edf8

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,11 +4355,6 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
43554355
}
43564356

43574357
template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
4358-
if (IS->isNonNegatedConsteval())
4359-
return visitStmt(IS->getThen());
4360-
if (IS->isNegatedConsteval())
4361-
return IS->getElse() ? visitStmt(IS->getElse()) : true;
4362-
43634358
if (auto *CondInit = IS->getInit())
43644359
if (!visitStmt(CondInit))
43654360
return false;
@@ -4368,8 +4363,19 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
43684363
if (!visitDeclStmt(CondDecl))
43694364
return false;
43704365

4371-
if (!this->visitBool(IS->getCond()))
4372-
return false;
4366+
// Compile condition.
4367+
if (IS->isNonNegatedConsteval()) {
4368+
if (!this->emitIsConstantContext(IS))
4369+
return false;
4370+
} else if (IS->isNegatedConsteval()) {
4371+
if (!this->emitIsConstantContext(IS))
4372+
return false;
4373+
if (!this->emitInvBool(IS))
4374+
return false;
4375+
} else {
4376+
if (!this->visitBool(IS->getCond()))
4377+
return false;
4378+
}
43734379

43744380
if (const Stmt *Else = IS->getElse()) {
43754381
LabelTy LabelElse = this->getLabel();

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,6 +3054,11 @@ static inline bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm) {
30543054
BlockDesc, Source);
30553055
}
30563056

3057+
static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3058+
S.Stk.push<Boolean>(Boolean::from(S.inConstantContext()));
3059+
return true;
3060+
}
3061+
30573062
inline bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
30583063
assert(T);
30593064
assert(!S.getLangOpts().CPlusPlus23);

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,5 @@ def AllocCN : Opcode {
787787
def Free : Opcode {
788788
let Args = [ArgBool];
789789
}
790+
791+
def IsConstantContext: Opcode;

clang/test/CodeGenCXX/cxx2b-consteval-if.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 -std=c++23 %s -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s
23

34
void should_be_used_1();
45
void should_be_used_2();

0 commit comments

Comments
 (0)