Skip to content

Commit

Permalink
[clang][bytecode][NFC] Simplify visitDeclRef (#123380)
Browse files Browse the repository at this point in the history
Try to reduce indentation here.
  • Loading branch information
tbaederr authored Jan 18, 2025
1 parent e240261 commit 90696d1
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6194,60 +6194,67 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
return revisit(VD);
}

if (D != InitializingDecl) {
// Try to lazily visit (or emit dummy pointers for) declarations
// we haven't seen yet.
if (Ctx.getLangOpts().CPlusPlus) {
if (const auto *VD = dyn_cast<VarDecl>(D)) {
const auto typeShouldBeVisited = [&](QualType T) -> bool {
if (T.isConstant(Ctx.getASTContext()))
return true;
return T->isReferenceType();
};
// Avoid infinite recursion.
if (D == InitializingDecl)
return this->emitDummyPtr(D, E);

// Try to lazily visit (or emit dummy pointers for) declarations
// we haven't seen yet.
// For C.
if (!Ctx.getLangOpts().CPlusPlus) {
if (const auto *VD = dyn_cast<VarDecl>(D);
VD && VD->getAnyInitializer() &&
VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak())
return revisit(VD);
return this->emitDummyPtr(D, E);
}

// DecompositionDecls are just proxies for us.
if (isa<DecompositionDecl>(VD))
return revisit(VD);

if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
typeShouldBeVisited(VD->getType())) {
if (const Expr *Init = VD->getAnyInitializer();
Init && !Init->isValueDependent()) {
// Whether or not the evaluation is successul doesn't really matter
// here -- we will create a global variable in any case, and that
// will have the state of initializer evaluation attached.
APValue V;
SmallVector<PartialDiagnosticAt> Notes;
(void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
true);
return this->visitDeclRef(D, E);
}
return revisit(VD);
}
// ... and C++.
const auto *VD = dyn_cast<VarDecl>(D);
if (!VD)
return this->emitDummyPtr(D, E);

// FIXME: The evaluateValue() check here is a little ridiculous, since
// it will ultimately call into Context::evaluateAsInitializer(). In
// other words, we're evaluating the initializer, just to know if we can
// evaluate the initializer.
if (VD->isLocalVarDecl() && typeShouldBeVisited(VD->getType()) &&
VD->getInit() && !VD->getInit()->isValueDependent()) {
const auto typeShouldBeVisited = [&](QualType T) -> bool {
if (T.isConstant(Ctx.getASTContext()))
return true;
return T->isReferenceType();
};

if (VD->evaluateValue())
return revisit(VD);
// DecompositionDecls are just proxies for us.
if (isa<DecompositionDecl>(VD))
return revisit(VD);

if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
typeShouldBeVisited(VD->getType())) {
if (const Expr *Init = VD->getAnyInitializer();
Init && !Init->isValueDependent()) {
// Whether or not the evaluation is successul doesn't really matter
// here -- we will create a global variable in any case, and that
// will have the state of initializer evaluation attached.
APValue V;
SmallVector<PartialDiagnosticAt> Notes;
(void)Init->EvaluateAsInitializer(V, Ctx.getASTContext(), VD, Notes,
true);
return this->visitDeclRef(D, E);
}
return revisit(VD);
}

// FIXME: The evaluateValue() check here is a little ridiculous, since
// it will ultimately call into Context::evaluateAsInitializer(). In
// other words, we're evaluating the initializer, just to know if we can
// evaluate the initializer.
if (VD->isLocalVarDecl() && typeShouldBeVisited(VD->getType()) &&
VD->getInit() && !VD->getInit()->isValueDependent()) {

if (VD->evaluateValue())
return revisit(VD);

if (!D->getType()->isReferenceType())
return this->emitDummyPtr(D, E);
if (!D->getType()->isReferenceType())
return this->emitDummyPtr(D, E);

return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
/*InitializerFailed=*/true, E);
}
}
} else {
if (const auto *VD = dyn_cast<VarDecl>(D);
VD && VD->getAnyInitializer() &&
VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak())
return revisit(VD);
}
return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
/*InitializerFailed=*/true, E);
}

return this->emitDummyPtr(D, E);
Expand Down

0 comments on commit 90696d1

Please sign in to comment.