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
+5 -6
View File
@@ -447,10 +447,9 @@ auto CarbonExternalASTSource::GetOrExportFunctionToCpp(
SemIR::InstId target_inst_id, SemIR::FunctionId function_id)
-> clang::FunctionDecl* {
SemIR::Function& function = context_->functions().Get(function_id);
auto clang_decl_id = context_->clang_decls().Lookup(function.first_decl_id());
if (clang_decl_id.has_value()) {
return cast<clang::FunctionDecl>(
context_->clang_decls().Get(clang_decl_id).key.decl);
if (const auto* clang_decl =
context_->clang_decls().Lookup(function.first_decl_id())) {
return cast<clang::FunctionDecl>(clang_decl->decl());
}
auto* clang_function_decl =
@@ -510,7 +509,7 @@ auto CarbonExternalASTSource::FindExternalVisibleDeclsByName(
auto* decl = cast<clang::Decl>(
const_cast<clang::DeclContext*>(decl_context->getPrimaryContext()));
auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(decl);
auto decl_id = context_->clang_decls().Lookup(key);
auto decl_id = context_->clang_decls().LookupId(key);
CARBON_CHECK(
decl_id.has_value(),
"The DeclContext should already be associated with a Carbon InstId.");
@@ -591,7 +590,7 @@ static auto GetAsCarbonOwnedClass(Context& context,
auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(
const_cast<clang::TagDecl*>(tag_decl->getFirstDecl()));
auto clang_decl_id = context.clang_decls().Lookup(key);
auto clang_decl_id = context.clang_decls().LookupId(key);
if (!clang_decl_id.has_value()) {
return std::nullopt;
}