Skip to content

Commit 05c9fd7

Browse files
authored
Add Check::Context::clang_decls() methods (#6094)
Use them instead of explicitly going through `sem_ir()`.
1 parent 8cbf289 commit 05c9fd7

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

toolchain/check/context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ class Context {
281281
auto ast_context() -> clang::ASTContext& {
282282
return sem_ir().clang_ast_unit()->getASTContext();
283283
}
284+
auto clang_decls() -> SemIR::ClangDeclStore& {
285+
return sem_ir().clang_decls();
286+
}
284287
auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); }
285288
auto name_scopes() -> SemIR::NameScopeStore& {
286289
return sem_ir().name_scopes();

toolchain/check/cpp/import.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ auto ImportCppFiles(Context& context,
479479

480480
SemIR::NameScope& name_scope = context.name_scopes().Get(name_scope_id);
481481
name_scope.set_is_closed_import(true);
482-
name_scope.set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
482+
name_scope.set_clang_decl_context_id(context.clang_decls().Add(
483483
{.decl = generated_ast->getASTContext().getTranslationUnitDecl(),
484484
.inst_id = name_scope.inst_id()}));
485485

@@ -500,7 +500,7 @@ static auto GetDeclContext(Context& context, SemIR::NameScopeId scope_id)
500500
auto scope_clang_decl_context_id =
501501
context.name_scopes().Get(scope_id).clang_decl_context_id();
502502
return dyn_cast<clang::DeclContext>(
503-
context.sem_ir().clang_decls().Get(scope_clang_decl_context_id).decl);
503+
context.clang_decls().Get(scope_clang_decl_context_id).decl);
504504
}
505505

506506
static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
@@ -563,20 +563,20 @@ static auto ClangLookupDeclarationName(Context& context, SemIR::LocId loc_id,
563563
}
564564

565565
// Looks up for constructors in the class scope and returns the lookup result.
566-
static auto ClangConstructorLookup(const Context& context,
566+
static auto ClangConstructorLookup(Context& context,
567567
SemIR::NameScopeId scope_id)
568568
-> clang::DeclContextLookupResult {
569569
const SemIR::NameScope& scope = context.sem_ir().name_scopes().Get(scope_id);
570570

571571
clang::Sema& sema = context.sem_ir().clang_ast_unit()->getSema();
572572
clang::Decl* decl =
573-
context.sem_ir().clang_decls().Get(scope.clang_decl_context_id()).decl;
573+
context.clang_decls().Get(scope.clang_decl_context_id()).decl;
574574
return sema.LookupConstructors(cast<clang::CXXRecordDecl>(decl));
575575
}
576576

577577
// Returns true if the given Clang declaration is the implicit injected class
578578
// name within the class.
579-
static auto IsDeclInjectedClassName(const Context& context,
579+
static auto IsDeclInjectedClassName(Context& context,
580580
SemIR::NameScopeId scope_id,
581581
SemIR::NameId name_id,
582582
const clang::NamedDecl* named_decl)
@@ -590,7 +590,7 @@ static auto IsDeclInjectedClassName(const Context& context,
590590
return false;
591591
}
592592

593-
const SemIR::ClangDecl& clang_decl = context.sem_ir().clang_decls().Get(
593+
const SemIR::ClangDecl& clang_decl = context.clang_decls().Get(
594594
context.sem_ir().name_scopes().Get(scope_id).clang_decl_context_id());
595595
const auto* scope_record_decl = cast<clang::CXXRecordDecl>(clang_decl.decl);
596596

@@ -647,9 +647,9 @@ static auto IsClangDeclImported(const Context& context, clang::Decl* decl)
647647

648648
// If `decl` already mapped to an instruction, returns that instruction.
649649
// Otherwise returns `None`.
650-
static auto LookupClangDeclInstId(const Context& context, clang::Decl* decl)
650+
static auto LookupClangDeclInstId(Context& context, clang::Decl* decl)
651651
-> SemIR::InstId {
652-
const auto& clang_decls = context.sem_ir().clang_decls();
652+
const auto& clang_decls = context.clang_decls();
653653
if (auto context_clang_decl_id = clang_decls.Lookup(decl->getCanonicalDecl());
654654
context_clang_decl_id.has_value()) {
655655
return clang_decls.Get(context_clang_decl_id).inst_id;
@@ -710,7 +710,7 @@ static auto ImportNamespaceDecl(Context& context,
710710
/*import_id=*/SemIR::InstId::None);
711711
context.name_scopes()
712712
.Get(result.name_scope_id)
713-
.set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
713+
.set_clang_decl_context_id(context.clang_decls().Add(
714714
{.decl = clang_decl->getCanonicalDecl(), .inst_id = result.inst_id}));
715715
return result.inst_id;
716716
}
@@ -776,7 +776,7 @@ static auto ImportTagDecl(Context& context, clang::TagDecl* clang_decl)
776776
AddIdentifierName(context, clang_decl->getName()));
777777

778778
// TODO: The caller does the same lookup. Avoid doing it twice.
779-
auto clang_decl_id = context.sem_ir().clang_decls().Add(
779+
auto clang_decl_id = context.clang_decls().Add(
780780
{.decl = clang_decl->getCanonicalDecl(), .inst_id = class_inst_id});
781781

782782
// Name lookup into the Carbon class looks in the C++ class definition.
@@ -954,7 +954,7 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
954954
context, class_type_inst_id, field_type_inst_id),
955955
.name_id = field_name_id,
956956
.index = SemIR::ElementIndex(fields.size())}));
957-
context.sem_ir().clang_decls().Add(
957+
context.clang_decls().Add(
958958
{.decl = decl->getCanonicalDecl(), .inst_id = field_decl_id});
959959

960960
// Compute the offset to the field that appears directly in the class.
@@ -1085,15 +1085,15 @@ static auto ImportEnumConstantDecl(Context& context,
10851085
auto inst_id = AddInstInNoBlock<SemIR::IntValue>(
10861086
context, loc_id, {.type_id = type_id, .int_id = int_id});
10871087
context.imports().push_back(inst_id);
1088-
context.sem_ir().clang_decls().Add(
1088+
context.clang_decls().Add(
10891089
{.decl = enumerator_decl->getCanonicalDecl(), .inst_id = inst_id});
10901090
return inst_id;
10911091
}
10921092

10931093
// Mark the given `Decl` as failed in `clang_decls`.
10941094
static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
1095-
context.sem_ir().clang_decls().Add({.decl = clang_decl->getCanonicalDecl(),
1096-
.inst_id = SemIR::ErrorInst::InstId});
1095+
context.clang_decls().Add({.decl = clang_decl->getCanonicalDecl(),
1096+
.inst_id = SemIR::ErrorInst::InstId});
10971097
}
10981098

10991099
// Creates an integer type of the given size.
@@ -1193,7 +1193,7 @@ static auto MapTagType(Context& context, const clang::TagType& type)
11931193
if (auto* record_decl = dyn_cast<clang::CXXRecordDecl>(tag_decl)) {
11941194
auto custom_type = LookupCustomRecordType(context, record_decl);
11951195
if (custom_type.inst_id.has_value()) {
1196-
context.sem_ir().clang_decls().Add(
1196+
context.clang_decls().Add(
11971197
{.decl = record_decl, .inst_id = custom_type.inst_id});
11981198
return custom_type;
11991199
}
@@ -1485,7 +1485,7 @@ static auto GetReturnTypeExpr(Context& context, SemIR::LocId loc_id,
14851485
SemIR::TypeInstId record_type_inst_id = context.types().GetAsTypeInstId(
14861486
context.sem_ir()
14871487
.clang_decls()
1488-
.Get(context.sem_ir().clang_decls().Lookup(
1488+
.Get(context.clang_decls().Lookup(
14891489
cast<clang::Decl>(clang_decl->getParent())))
14901490
.inst_id);
14911491
return {
@@ -1659,7 +1659,7 @@ static auto ImportFunction(Context& context, SemIR::LocId loc_id,
16591659
.virtual_index = virtual_index,
16601660
.self_param_id = FindSelfPattern(
16611661
context, function_params_insts->implicit_param_patterns_id),
1662-
.clang_decl_id = context.sem_ir().clang_decls().Add(
1662+
.clang_decl_id = context.clang_decls().Add(
16631663
{.decl = clang_decl, .inst_id = decl_id})}};
16641664

16651665
function_decl.function_id = context.functions().Add(function_info);
@@ -1815,7 +1815,7 @@ static auto ImportVarDecl(Context& context, SemIR::LocId loc_id,
18151815
SemIR::InstId var_storage_inst_id =
18161816
AddPlaceholderInstInNoBlock(context, {loc_id, var_storage});
18171817

1818-
auto clang_decl_id = context.sem_ir().clang_decls().Add(
1818+
auto clang_decl_id = context.clang_decls().Add(
18191819
{.decl = var_decl, .inst_id = var_storage_inst_id});
18201820

18211821
// Entity name referring to a Clang decl for mangling.
@@ -1865,8 +1865,7 @@ static auto ImportDeclAfterDependencies(Context& context, SemIR::LocId loc_id,
18651865
type.getAsString()));
18661866
return SemIR::ErrorInst::InstId;
18671867
}
1868-
context.sem_ir().clang_decls().Add(
1869-
{.decl = clang_decl, .inst_id = type_inst_id});
1868+
context.clang_decls().Add({.decl = clang_decl, .inst_id = type_inst_id});
18701869
return type_inst_id;
18711870
}
18721871
if (isa<clang::FieldDecl, clang::IndirectFieldDecl>(clang_decl)) {
@@ -2402,8 +2401,8 @@ auto ImportClassDefinitionForClangDecl(Context& context, SemIR::LocId loc_id,
24022401
clang::ASTUnit* ast = context.sem_ir().clang_ast_unit();
24032402
CARBON_CHECK(ast);
24042403

2405-
auto* clang_decl = cast<clang::TagDecl>(
2406-
context.sem_ir().clang_decls().Get(clang_decl_id).decl);
2404+
auto* clang_decl =
2405+
cast<clang::TagDecl>(context.clang_decls().Get(clang_decl_id).decl);
24072406
auto class_inst_id = context.types().GetAsTypeInstId(
24082407
context.classes().Get(class_id).first_owning_decl_id);
24092408

toolchain/check/cpp/thunk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ auto IsCppThunkRequired(Context& context, const SemIR::Function& function)
9393
// corresponding SemIR function has an acceptable parameter type.
9494
// TODO: We should be able to avoid thunks for reference parameters.
9595
const auto* decl = cast<clang::FunctionDecl>(
96-
context.sem_ir().clang_decls().Get(function.clang_decl_id).decl);
96+
context.clang_decls().Get(function.clang_decl_id).decl);
9797
for (auto* param : decl->parameters()) {
9898
if (param->getType()->isReferenceType()) {
9999
thunk_required = true;

toolchain/check/cpp/type_mapping.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
9595
.Get(context.sem_ir().classes().Get(class_type.class_id).scope_id)
9696
.clang_decl_context_id();
9797
if (clang_decl_id.has_value()) {
98-
clang::Decl* clang_decl =
99-
context.sem_ir().clang_decls().Get(clang_decl_id).decl;
98+
clang::Decl* clang_decl = context.clang_decls().Get(clang_decl_id).decl;
10099
auto* tag_type_decl = clang::cast<clang::TagDecl>(clang_decl);
101100
return context.ast_context().getCanonicalTagType(tag_type_decl);
102101
}

0 commit comments

Comments
 (0)