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 -10
View File
@@ -245,20 +245,19 @@ struct CalleeFunctionInfo {
auto IsCppThunkRequired(Context& context, const SemIR::Function& function)
-> bool {
auto clang_decl_id = context.clang_decls().Lookup(function.first_decl_id());
if (!clang_decl_id.has_value()) {
const auto* clang_decl =
context.clang_decls().Lookup(function.first_decl_id());
if (!clang_decl) {
return false;
}
const auto& decl_info = context.clang_decls().Get(clang_decl_id);
if (!decl_info.is_imported) {
if (!clang_decl->is_imported) {
return false;
}
const auto& signature =
context.clang_decl_signatures().Get(decl_info.key.signature_id);
auto* decl = cast<clang::FunctionDecl>(decl_info.key.decl);
context.clang_decl_signatures().Get(clang_decl->key.signature_id);
auto* decl = cast<clang::FunctionDecl>(clang_decl->decl());
if (signature.kind != SemIR::ClangDeclSignature::Normal ||
signature.num_params != static_cast<int>(decl->getNumNonObjectParams())) {
// We require a thunk if the number of parameters we want isn't all of them.
@@ -652,9 +651,7 @@ static auto BuildThunkBody(CppContext& cpp_context, clang::Sema& sema,
auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
-> clang::FunctionDecl* {
auto clang_decl_key =
context.clang_decls()
.Get(context.clang_decls().Lookup(callee_function.first_decl_id()))
.key;
context.clang_decls().Lookup(callee_function.first_decl_id())->key;
clang::FunctionDecl* callee_function_decl =
clang_decl_key.decl->getAsFunction();
CARBON_CHECK(callee_function_decl);