Skip to content

Commit d31603e

Browse files
committed
[clang][Interp] Control InitStack activity state in visitInitList
This doesn't change anything about the current tests, but helps once those tests change because of #97308
1 parent 8a79dc7 commit d31603e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

clang/lib/AST/Interp/Compiler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
13341334

13351335
auto initPrimitiveField = [=](const Record::Field *FieldToInit,
13361336
const Expr *Init, PrimType T) -> bool {
1337+
InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
13371338
if (!this->visit(Init))
13381339
return false;
13391340

@@ -1344,6 +1345,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
13441345

13451346
auto initCompositeField = [=](const Record::Field *FieldToInit,
13461347
const Expr *Init) -> bool {
1348+
InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
13471349
InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
13481350
// Non-primitive case. Get a pointer to the field-to-initialize
13491351
// on the stack and recurse into visitInitializer().
@@ -4088,12 +4090,7 @@ template <class Emitter>
40884090
bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
40894091
SourceLocScope<Emitter> SLS(this, E);
40904092

4091-
bool Old = InitStackActive;
4092-
InitStackActive =
4093-
!(E->getUsedContext()->getDeclKind() == Decl::CXXConstructor);
4094-
bool Result = this->delegate(E->getExpr());
4095-
InitStackActive = Old;
4096-
return Result;
4093+
return this->delegate(E->getExpr());
40974094
}
40984095

40994096
template <class Emitter>
@@ -4151,6 +4148,9 @@ bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
41514148
// instance pointer of the current function frame, but e.g. to the declaration
41524149
// currently being initialized. Here we emit the necessary instruction(s) for
41534150
// this scenario.
4151+
if (!InitStackActive || !E->isImplicit())
4152+
return this->emitThis(E);
4153+
41544154
if (InitStackActive && !InitStack.empty()) {
41554155
unsigned StartIndex = 0;
41564156
for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {

clang/lib/AST/Interp/Compiler.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ template <class Emitter> class DestructorScope;
3333
template <class Emitter> class VariableScope;
3434
template <class Emitter> class DeclScope;
3535
template <class Emitter> class InitLinkScope;
36+
template <class Emitter> class InitStackScope;
3637
template <class Emitter> class OptionScope;
3738
template <class Emitter> class ArrayIndexScope;
3839
template <class Emitter> class SourceLocScope;
@@ -298,6 +299,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
298299
friend class DestructorScope<Emitter>;
299300
friend class DeclScope<Emitter>;
300301
friend class InitLinkScope<Emitter>;
302+
friend class InitStackScope<Emitter>;
301303
friend class OptionScope<Emitter>;
302304
friend class ArrayIndexScope<Emitter>;
303305
friend class SourceLocScope<Emitter>;
@@ -612,6 +614,20 @@ template <class Emitter> class InitLinkScope final {
612614
Compiler<Emitter> *Ctx;
613615
};
614616

617+
template <class Emitter> class InitStackScope final {
618+
public:
619+
InitStackScope(Compiler<Emitter> *Ctx, bool Active)
620+
: Ctx(Ctx), OldValue(Ctx->InitStackActive) {
621+
Ctx->InitStackActive = Active;
622+
}
623+
624+
~InitStackScope() { this->Ctx->InitStackActive = OldValue; }
625+
626+
private:
627+
Compiler<Emitter> *Ctx;
628+
bool OldValue;
629+
};
630+
615631
} // namespace interp
616632
} // namespace clang
617633

0 commit comments

Comments
 (0)