Skip to content

Commit 9e7209c

Browse files
authored
[clang][bytecode][NFC] Clean up variable creation (#164415)
1 parent abf80b5 commit 9e7209c

File tree

1 file changed

+48
-54
lines changed

1 file changed

+48
-54
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,46 +4841,39 @@ Compiler<Emitter>::visitVarDecl(const VarDecl *VD, const Expr *Init,
48414841
return !NeedsOp || this->emitCheckDecl(VD, VD);
48424842
};
48434843

4844-
auto initGlobal = [&](unsigned GlobalIndex) -> bool {
4845-
assert(Init);
4846-
4847-
if (VarT) {
4848-
if (!this->visit(Init))
4849-
return checkDecl() && false;
4850-
4851-
return checkDecl() && this->emitInitGlobal(*VarT, GlobalIndex, VD);
4852-
}
4853-
4854-
if (!checkDecl())
4855-
return false;
4856-
4857-
if (!this->emitGetPtrGlobal(GlobalIndex, Init))
4858-
return false;
4859-
4860-
if (!visitInitializer(Init))
4861-
return false;
4862-
4863-
return this->emitFinishInitGlobal(Init);
4864-
};
4865-
48664844
DeclScope<Emitter> LocalScope(this, VD);
48674845

4868-
// We've already seen and initialized this global.
4869-
if (UnsignedOrNone GlobalIndex = P.getGlobal(VD)) {
4846+
UnsignedOrNone GlobalIndex = P.getGlobal(VD);
4847+
if (GlobalIndex) {
4848+
// We've already seen and initialized this global.
48704849
if (P.getPtrGlobal(*GlobalIndex).isInitialized())
48714850
return checkDecl();
4872-
48734851
// The previous attempt at initialization might've been unsuccessful,
48744852
// so let's try this one.
4875-
return !Init || (checkDecl() && initGlobal(*GlobalIndex));
4853+
} else if ((GlobalIndex = P.createGlobal(VD, Init))) {
4854+
} else {
4855+
return false;
48764856
}
4857+
if (!Init)
4858+
return true;
48774859

4878-
UnsignedOrNone GlobalIndex = P.createGlobal(VD, Init);
4860+
if (!checkDecl())
4861+
return false;
48794862

4880-
if (!GlobalIndex)
4863+
if (VarT) {
4864+
if (!this->visit(Init))
4865+
return false;
4866+
4867+
return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
4868+
}
4869+
4870+
if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
4871+
return false;
4872+
4873+
if (!visitInitializer(Init))
48814874
return false;
48824875

4883-
return !Init || (checkDecl() && initGlobal(*GlobalIndex));
4876+
return this->emitFinishInitGlobal(Init);
48844877
}
48854878
// Local variables.
48864879
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
@@ -4890,36 +4883,37 @@ Compiler<Emitter>::visitVarDecl(const VarDecl *VD, const Expr *Init,
48904883
VD, *VarT, VD->getType().isConstQualified(),
48914884
VD->getType().isVolatileQualified(), nullptr, ScopeKind::Block,
48924885
IsConstexprUnknown);
4893-
if (Init) {
4894-
// If this is a toplevel declaration, create a scope for the
4895-
// initializer.
4896-
if (Toplevel) {
4897-
LocalScope<Emitter> Scope(this);
4898-
if (!this->visit(Init))
4899-
return false;
4900-
return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
4901-
}
4902-
if (!this->visit(Init))
4903-
return false;
4904-
return this->emitSetLocal(*VarT, Offset, VD);
4905-
}
4906-
} else {
4907-
if (UnsignedOrNone Offset = this->allocateLocal(
4908-
VD, VD->getType(), nullptr, ScopeKind::Block, IsConstexprUnknown)) {
4909-
if (!Init)
4910-
return true;
49114886

4912-
if (!this->emitGetPtrLocal(*Offset, Init))
4913-
return false;
4887+
if (!Init)
4888+
return true;
49144889

4915-
if (!visitInitializer(Init))
4890+
// If this is a toplevel declaration, create a scope for the
4891+
// initializer.
4892+
if (Toplevel) {
4893+
LocalScope<Emitter> Scope(this);
4894+
if (!this->visit(Init))
49164895
return false;
4917-
4918-
return this->emitFinishInitPop(Init);
4896+
return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
49194897
}
4920-
return false;
4898+
if (!this->visit(Init))
4899+
return false;
4900+
return this->emitSetLocal(*VarT, Offset, VD);
49214901
}
4922-
return true;
4902+
// Local composite variables.
4903+
if (UnsignedOrNone Offset = this->allocateLocal(
4904+
VD, VD->getType(), nullptr, ScopeKind::Block, IsConstexprUnknown)) {
4905+
if (!Init)
4906+
return true;
4907+
4908+
if (!this->emitGetPtrLocal(*Offset, Init))
4909+
return false;
4910+
4911+
if (!visitInitializer(Init))
4912+
return false;
4913+
4914+
return this->emitFinishInitPop(Init);
4915+
}
4916+
return false;
49234917
}
49244918

49254919
template <class Emitter>

0 commit comments

Comments
 (0)