Skip to content

Commit

Permalink
[clang][Interp] Control InitStack activity state in visitInitList
Browse files Browse the repository at this point in the history
This doesn't change anything about the current tests, but helps
once those tests change because of llvm#97308
  • Loading branch information
tbaederr committed Jul 19, 2024
1 parent 8a79dc7 commit d31603e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions clang/lib/AST/Interp/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,

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

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

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

bool Old = InitStackActive;
InitStackActive =
!(E->getUsedContext()->getDeclKind() == Decl::CXXConstructor);
bool Result = this->delegate(E->getExpr());
InitStackActive = Old;
return Result;
return this->delegate(E->getExpr());
}

template <class Emitter>
Expand Down Expand Up @@ -4151,6 +4148,9 @@ bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
// instance pointer of the current function frame, but e.g. to the declaration
// currently being initialized. Here we emit the necessary instruction(s) for
// this scenario.
if (!InitStackActive || !E->isImplicit())
return this->emitThis(E);

if (InitStackActive && !InitStack.empty()) {
unsigned StartIndex = 0;
for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/AST/Interp/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ template <class Emitter> class DestructorScope;
template <class Emitter> class VariableScope;
template <class Emitter> class DeclScope;
template <class Emitter> class InitLinkScope;
template <class Emitter> class InitStackScope;
template <class Emitter> class OptionScope;
template <class Emitter> class ArrayIndexScope;
template <class Emitter> class SourceLocScope;
Expand Down Expand Up @@ -298,6 +299,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
friend class DestructorScope<Emitter>;
friend class DeclScope<Emitter>;
friend class InitLinkScope<Emitter>;
friend class InitStackScope<Emitter>;
friend class OptionScope<Emitter>;
friend class ArrayIndexScope<Emitter>;
friend class SourceLocScope<Emitter>;
Expand Down Expand Up @@ -612,6 +614,20 @@ template <class Emitter> class InitLinkScope final {
Compiler<Emitter> *Ctx;
};

template <class Emitter> class InitStackScope final {
public:
InitStackScope(Compiler<Emitter> *Ctx, bool Active)
: Ctx(Ctx), OldValue(Ctx->InitStackActive) {
Ctx->InitStackActive = Active;
}

~InitStackScope() { this->Ctx->InitStackActive = OldValue; }

private:
Compiler<Emitter> *Ctx;
bool OldValue;
};

} // namespace interp
} // namespace clang

Expand Down

0 comments on commit d31603e

Please sign in to comment.