[clang] [CIR] Support for static variables (PR #143980)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 12 15:20:39 PDT 2025
================
@@ -219,6 +236,252 @@ void CIRGenFunction::emitVarDecl(const VarDecl &d) {
return emitAutoVarDecl(d);
}
+static std::string getStaticDeclName(CIRGenModule &cgm, const VarDecl &d) {
+ if (cgm.getLangOpts().CPlusPlus)
+ return cgm.getMangledName(&d).str();
+
+ // If this isn't C++, we don't need a mangled name, just a pretty one.
+ assert(!d.isExternallyVisible() && "name shouldn't matter");
+ std::string contextName;
+ const DeclContext *dc = d.getDeclContext();
+ if (auto *cd = dyn_cast<CapturedDecl>(dc))
+ dc = cast<DeclContext>(cd->getNonClosureContext());
+ if (const auto *fd = dyn_cast<FunctionDecl>(dc))
+ contextName = std::string(cgm.getMangledName(fd));
+ else if (isa<BlockDecl>(dc))
+ cgm.errorNYI(d.getSourceRange(), "block decl context for static var");
+ else if (isa<ObjCMethodDecl>(dc))
+ cgm.errorNYI(d.getSourceRange(), "ObjC decl context for static var");
+ else
+ cgm.errorNYI(d.getSourceRange(), "Unknown context for static var decl");
+
+ contextName += "." + d.getNameAsString();
+ return contextName;
+}
+
+// TODO(cir): LLVM uses a Constant base class. Maybe CIR could leverage an
+// interface for all constants?
+cir::GlobalOp
+CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &d,
+ cir::GlobalLinkageKind linkage) {
+ // In general, we don't always emit static var decls once before we reference
+ // them. It is possible to reference them before emitting the function that
+ // contains them, and it is possible to emit the containing function multiple
+ // times.
+ if (cir::GlobalOp existingGV = getStaticLocalDeclAddress(&d))
+ return existingGV;
+
+ QualType ty = d.getType();
+ assert(ty->isConstantSizeType() && "VLAs can't be static");
+
+ // Use the label if the variable is renamed with the asm-label extension.
+ std::string name;
+ if (d.hasAttr<AsmLabelAttr>())
+ errorNYI(d.getSourceRange(), "getOrCreateStaticVarDecl: asm label");
+ else
+ name = getStaticDeclName(*this, d);
----------------
erichkeane wrote:
I'm on the fence about a suggestion here... just always init 'name' above and let AsmLabelAttr errorNYI for now, and whoever comes to fix this can figure out if the branch is reasonable. WDYT?
https://github.com/llvm/llvm-project/pull/143980
More information about the cfe-commits
mailing list