Repository: carbon-language/carbon-lang
File: toolchain/check/cpp/import.cpp (C++)

Code before:

  if (!lookup->isSingleResult()) {
    // Clang will diagnose ambiguous lookup results for us.
    if (!lookup->isAmbiguous()) {
      context.TODO(loc_id,
                   llvm::formatv("Unsupported: Lookup succeeded but couldn't "
                                 "find a single result; LookupResultKind: {0}",
                                 static_cast<int>(lookup->getResultKind())));
    }

    return result;
  }

  result = {lookup->getFoundDecl(), lookup->begin().getAccess()};
  return result;
}

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

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

// Returns true if the given Clang declaration is the implicit injected class
// name within the class.
static auto IsDeclInjectedClassName(const Context& context,
                                    SemIR::NameScopeId scope_id,
                                    SemIR::NameId name_id,
                                    const clang::NamedDecl* named_decl)
    -> bool {
  if (!named_decl->isImplicit()) {
    return false;
  }

  const auto* record_decl = dyn_cast<clang::CXXRecordDecl>(named_decl);
  if (!record_decl) {
    return false;
  }

  const SemIR::ClangDecl& clang_decl = context.clang_decls().Get(
      context.sem_ir().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();

Review comment (suggestion):
This method can be changed to take `Context& context` instead of a const one. We don't try to scope Context to be const anywhere else really.

Code after:

  if (!lookup->isSingleResult()) {
    // Clang will diagnose ambiguous lookup results for us.
    if (!lookup->isAmbiguous()) {
      context.TODO(loc_id,
                   llvm::formatv("Unsupported: Lookup succeeded but couldn't "
                                 "find a single result; LookupResultKind: {0}",
                                 static_cast<int>(lookup->getResultKind())));
    }

    return result;
  }

  result = {lookup->getFoundDecl(), lookup->begin().getAccess()};
  return result;
}

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

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

// Returns true if the given Clang declaration is the implicit injected class
// name within the class.
static auto IsDeclInjectedClassName(Context& context,
                                    SemIR::NameScopeId scope_id,
                                    SemIR::NameId name_id,
                                    const clang::NamedDecl* named_decl)
    -> bool {
  if (!named_decl->isImplicit()) {
    return false;
  }

  const auto* record_decl = dyn_cast<clang::CXXRecordDecl>(named_decl);
  if (!record_decl) {
    return false;
  }

  const SemIR::ClangDecl& clang_decl = context.clang_decls().Get(
      context.sem_ir().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();