Improve ClangDeclStore ergonomics (#7311)

Change `Lookup` by InstId to return a ClangDecl pointer. All callers
were immediately calling `Get` anyway, so this makes call sites a little
shorter. The other `Lookup` method, by ClangDeclKey, is sometimes called
without calling `Get`, so left that as-is, but renamed to `LookupId`.

Also add a `decl` method to ClangDecl so that the commonly repeated
`clang_decl->key.decl` can be written `clang_decl->decl()`.
This commit is contained in:
Nicholas Bishop
2026-06-05 20:49:34 +00:00
committed by GitHub
parent 44b17ff436
commit 5e62791ad2
16 changed files with 70 additions and 95 deletions
+7 -11
View File
@@ -351,15 +351,12 @@ auto FileContext::GetOrCreateLLVMFunction(
// decl_id so it doesn't fall out naturally from the handling below)
if (function_id != sem_ir().global_ctor_id()) {
const auto& function = sem_ir().functions().Get(function_id);
auto clang_decl_id =
sem_ir().clang_decls().Lookup(function.first_decl_id());
if (clang_decl_id.has_value()) {
const auto& clang_decl = sem_ir().clang_decls().Get(clang_decl_id);
if (clang_decl.is_imported) {
if (const auto* clang_decl =
sem_ir().clang_decls().Lookup(function.first_decl_id())) {
if (clang_decl->is_imported) {
CARBON_CHECK(!specific_id.has_value(),
"Specific functions cannot have C++ definitions");
return HandleReferencedCppFunction(
clang_decl.key.decl->getAsFunction());
return HandleReferencedCppFunction(clang_decl->decl()->getAsFunction());
}
}
}
@@ -720,11 +717,10 @@ auto FileContext::BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
// happens when a Carbon variable is exported and used from C++; code
// generation for the C++ code may have already created an
// llvm::GlobalVariable.
auto clang_decl_id = sem_ir().clang_decls().Lookup(var_storage.pattern_id);
if (clang_decl_id.has_value()) {
auto* decl = sem_ir().clang_decls().Get(clang_decl_id).key.decl;
if (const auto* clang_decl =
sem_ir().clang_decls().Lookup(var_storage.pattern_id)) {
auto* constant = cpp_code_generator_->GetAddrOfGlobal(
CreateGlobalDecl(cast<clang::NamedDecl>(decl)),
CreateGlobalDecl(cast<clang::NamedDecl>(clang_decl->decl())),
/*isForDefinition=*/false);
if (constant) {
return constant;