Skip to content

[clang][bytecode] Classify variable initializer, not the decl #146338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4574,22 +4574,23 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
template <class Emitter>
bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD,
bool ConstantContext) {
std::optional<PrimType> VarT = classify(VD->getType());

// We only create variables if we're evaluating in a constant context.
// Otherwise, just evaluate the initializer and return it.
if (!ConstantContext) {
DeclScope<Emitter> LS(this, VD);
if (!this->visit(VD->getAnyInitializer()))
const Expr *Init = VD->getInit();
if (!this->visit(Init))
return false;
return this->emitRet(VarT.value_or(PT_Ptr), VD) && LS.destroyLocals() &&
this->emitCheckAllocations(VD);
return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
LS.destroyLocals() && this->emitCheckAllocations(VD);
}

LocalScope<Emitter> VDScope(this, VD);
if (!this->visitVarDecl(VD, /*Toplevel=*/true))
return false;

std::optional<PrimType> VarT = classify(VD->getType());
if (Context::shouldBeGloballyIndexed(VD)) {
auto GlobalIndex = P.getGlobal(VD);
assert(GlobalIndex); // visitVarDecl() didn't return false.
Expand Down