Selaa lähdekoodia

Use `context.x()` instead of `context.sema_ir().x()` in `check/cpp/` when possible (#6109)

Avoid using `const Context&`. We always work with a cmutable `Context&`
(https://github.com/carbon-language/carbon-lang/pull/6094#discussion_r2359199247).
Boaz Brickner 7 kuukautta sitten
vanhempi
sitoutus
412d911578

+ 11 - 18
toolchain/check/cpp/import.cpp

@@ -531,7 +531,7 @@ static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
 static auto ClangConstructorLookup(Context& context,
                                    SemIR::NameScopeId scope_id)
     -> clang::DeclContextLookupResult {
-  const SemIR::NameScope& scope = context.sem_ir().name_scopes().Get(scope_id);
+  const SemIR::NameScope& scope = context.name_scopes().Get(scope_id);
 
   clang::Sema& sema = context.sem_ir().clang_ast_unit()->getSema();
   clang::Decl* decl =
@@ -556,17 +556,15 @@ static auto IsDeclInjectedClassName(Context& context,
   }
 
   const SemIR::ClangDecl& clang_decl = context.clang_decls().Get(
-      context.sem_ir().name_scopes().Get(scope_id).clang_decl_context_id());
+      context.name_scopes().Get(scope_id).clang_decl_context_id());
   const auto* scope_record_decl = cast<clang::CXXRecordDecl>(clang_decl.decl);
 
-  const clang::ASTContext& ast_context =
-      context.sem_ir().clang_ast_unit()->getASTContext();
+  const clang::ASTContext& ast_context = context.ast_context();
   CARBON_CHECK(ast_context.getCanonicalTagType(scope_record_decl) ==
                ast_context.getCanonicalTagType(record_decl));
 
   auto class_decl = context.insts().GetAs<SemIR::ClassDecl>(clang_decl.inst_id);
-  CARBON_CHECK(name_id ==
-               context.sem_ir().classes().Get(class_decl.class_id).name_id);
+  CARBON_CHECK(name_id == context.classes().Get(class_decl.class_id).name_id);
   return true;
 }
 
@@ -602,12 +600,8 @@ static auto ClangLookupName(Context& context, SemIR::NameScopeId scope_id,
 }
 
 // Returns whether `decl` already mapped to an instruction.
-static auto IsClangDeclImported(const Context& context, clang::Decl* decl)
-    -> bool {
-  return context.sem_ir()
-      .clang_decls()
-      .Lookup(decl->getCanonicalDecl())
-      .has_value();
+static auto IsClangDeclImported(Context& context, clang::Decl* decl) -> bool {
+  return context.clang_decls().Lookup(decl->getCanonicalDecl()).has_value();
 }
 
 // If `decl` already mapped to an instruction, returns that instruction.
@@ -1448,8 +1442,7 @@ static auto GetReturnTypeExpr(Context& context, SemIR::LocId loc_id,
 
   // TODO: Make this a `PartialType`.
   SemIR::TypeInstId record_type_inst_id = context.types().GetAsTypeInstId(
-      context.sem_ir()
-          .clang_decls()
+      context.clang_decls()
           .Get(context.clang_decls().Lookup(
               cast<clang::Decl>(clang_decl->getParent())))
           .inst_id);
@@ -1703,7 +1696,7 @@ using ImportWorklist = llvm::SmallVector<ImportItem>;
 }  // namespace
 
 // Adds the given declaration to our list of declarations to import.
-static auto AddDependentDecl(const Context& context, clang::Decl* decl,
+static auto AddDependentDecl(Context& context, clang::Decl* decl,
                              ImportWorklist& worklist) -> void {
   if (!IsClangDeclImported(context, decl)) {
     worklist.push_back({.decl = decl, .added_dependencies = false});
@@ -1712,7 +1705,7 @@ static auto AddDependentDecl(const Context& context, clang::Decl* decl,
 
 // Finds all decls that need to be imported before importing the given type and
 // adds them to the given set.
-static auto AddDependentUnimportedTypeDecls(const Context& context,
+static auto AddDependentUnimportedTypeDecls(Context& context,
                                             clang::QualType type,
                                             ImportWorklist& worklist) -> void {
   while (true) {
@@ -1734,7 +1727,7 @@ static auto AddDependentUnimportedTypeDecls(const Context& context,
 // Finds all decls that need to be imported before importing the given function
 // and adds them to the given set.
 static auto AddDependentUnimportedFunctionDecls(
-    const Context& context, const clang::FunctionDecl& clang_decl,
+    Context& context, const clang::FunctionDecl& clang_decl,
     ImportWorklist& worklist) -> void {
   for (const auto* param : clang_decl.parameters()) {
     AddDependentUnimportedTypeDecls(context, param->getType(), worklist);
@@ -1745,7 +1738,7 @@ static auto AddDependentUnimportedFunctionDecls(
 
 // Finds all decls that need to be imported before importing the given
 // declaration and adds them to the given set.
-static auto AddDependentUnimportedDecls(const Context& context,
+static auto AddDependentUnimportedDecls(Context& context,
                                         clang::Decl* clang_decl,
                                         ImportWorklist& worklist) -> void {
   if (auto* clang_function_decl = clang_decl->getAsFunction()) {

+ 1 - 2
toolchain/check/cpp/thunk.cpp

@@ -517,8 +517,7 @@ static auto BuildThunkBody(clang::Sema& sema,
 auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
     -> clang::FunctionDecl* {
   clang::FunctionDecl* callee_function_decl =
-      context.sem_ir()
-          .clang_decls()
+      context.clang_decls()
           .Get(callee_function.clang_decl_id)
           .decl->getAsFunction();
   CARBON_CHECK(callee_function_decl);

+ 10 - 14
toolchain/check/cpp/type_mapping.cpp

@@ -94,7 +94,7 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
   // If the class was imported from C++, return the original C++ type.
   auto clang_decl_id =
       context.name_scopes()
-          .Get(context.sem_ir().classes().Get(class_type.class_id).scope_id)
+          .Get(context.classes().Get(class_type.class_id).scope_id)
           .clang_decl_context_id();
   if (clang_decl_id.has_value()) {
     clang::Decl* clang_decl = context.clang_decls().Get(clang_decl_id).decl;
@@ -149,7 +149,7 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type)
 // to keep them in sync.
 static auto MapNonWrapperType(Context& context, SemIR::InstId inst_id,
                               SemIR::TypeId type_id) -> clang::QualType {
-  auto type_inst = context.sem_ir().types().GetAsInst(type_id);
+  auto type_inst = context.types().GetAsInst(type_id);
 
   CARBON_KIND_SWITCH(type_inst) {
     case SemIR::BoolType::Kind: {
@@ -189,17 +189,14 @@ static auto MapToCppType(Context& context, SemIR::InstId inst_id)
   llvm::SmallVector<SemIR::TypeId> wrapper_types;
   while (true) {
     SemIR::TypeId orig_type_id = type_id;
-    if (auto const_type =
-            context.sem_ir().types().TryGetAs<SemIR::ConstType>(type_id);
+    if (auto const_type = context.types().TryGetAs<SemIR::ConstType>(type_id);
         const_type) {
-      type_id =
-          context.sem_ir().types().GetTypeIdForTypeInstId(const_type->inner_id);
+      type_id = context.types().GetTypeIdForTypeInstId(const_type->inner_id);
     } else if (auto pointer_type =
-                   context.sem_ir().types().TryGetAs<SemIR::PointerType>(
-                       type_id);
+                   context.types().TryGetAs<SemIR::PointerType>(type_id);
                pointer_type) {
-      type_id = context.sem_ir().types().GetTypeIdForTypeInstId(
-          pointer_type->pointee_id);
+      type_id =
+          context.types().GetTypeIdForTypeInstId(pointer_type->pointee_id);
     } else {
       break;
     }
@@ -212,12 +209,11 @@ static auto MapToCppType(Context& context, SemIR::InstId inst_id)
   }
 
   for (auto wrapper_type_id : llvm::reverse(wrapper_types)) {
-    if (auto const_type = context.sem_ir().types().TryGetAs<SemIR::ConstType>(
-            wrapper_type_id);
+    if (auto const_type =
+            context.types().TryGetAs<SemIR::ConstType>(wrapper_type_id);
         const_type) {
       mapped_type.addConst();
-    } else if (context.sem_ir().types().TryGetAs<SemIR::PointerType>(
-                   wrapper_type_id)) {
+    } else if (context.types().TryGetAs<SemIR::PointerType>(wrapper_type_id)) {
       auto pointer_type = context.ast_context().getPointerType(mapped_type);
       mapped_type = context.ast_context().getAttributedType(
           clang::attr::TypeNonNull, pointer_type, pointer_type);