Skip to content

Commit 30ea12f

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 d082f1f commit 30ea12f

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->emitInv(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
@@ -3048,6 +3048,11 @@ static inline bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm) {
30483048
BlockDesc, Source);
30493049
}
30503050

3051+
static inline bool IsConstantContext(InterpState &S, CodePtr OpPC) {
3052+
S.Stk.push<Boolean>(Boolean::from(S.inConstantContext()));
3053+
return true;
3054+
}
3055+
30513056
inline bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T) {
30523057
assert(T);
30533058
assert(!S.getLangOpts().CPlusPlus23);

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,5 @@ def AllocCN : Opcode {
780780
def Free : Opcode {
781781
let Args = [ArgBool];
782782
}
783+
784+
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)