Create modules for header imports. (#7613)

Make multiple imports of the same header only parse it once per C++
domain. Reuse of the same header in `--share-cpp-ast` mode now reuses
the representation.

Importing a Carbon file with C++ dependencies now makes those transitive
C++ dependencies in the same C++ domain visible too.

Assisted-by: Gemini via Antigravity
This commit is contained in:
Richard Smith
2026-08-06 13:32:37 +00:00
committed by GitHub
parent b9692a0e77
commit a1e843718e
19 changed files with 324 additions and 164 deletions
+18 -2
View File
@@ -39,7 +39,6 @@
#include "toolchain/check/core_identifier.h"
#include "toolchain/check/cpp/access.h"
#include "toolchain/check/cpp/custom_type_mapping.h"
#include "toolchain/check/cpp/domain.h"
#include "toolchain/check/cpp/generate_ast.h"
#include "toolchain/check/cpp/location.h"
#include "toolchain/check/cpp/macros.h"
@@ -62,6 +61,7 @@
#include "toolchain/parse/node_ids.h"
#include "toolchain/sem_ir/clang_decl.h"
#include "toolchain/sem_ir/class.h"
#include "toolchain/sem_ir/cpp_domain.h"
#include "toolchain/sem_ir/cpp_file.h"
#include "toolchain/sem_ir/cpp_overload_set.h"
#include "toolchain/sem_ir/function.h"
@@ -119,7 +119,7 @@ static auto AddNamespace(Context& context, PackageNameId cpp_package_id,
auto ImportCpp(Context& context,
llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
CppDomain* domain) -> void {
SemIR::CppDomain* domain) -> void {
if (imports.empty()) {
// TODO: Consider always having a (non-null) AST even if there are no Cpp
// imports.
@@ -260,6 +260,22 @@ auto FindCorrespondingClangDeclKey(Context& context, SemIR::LocId loc_id,
}
CARBON_CHECK(clang_decl_id.has_value());
auto key = file.clang_decls().Get(clang_decl_id).key;
// Easy case: files are from the same domain. We can just reuse the decl
// pointer.
if (context.sem_ir().cpp_file() && file.cpp_file() &&
context.sem_ir().cpp_file()->cpp_domain() ==
file.cpp_file()->cpp_domain()) {
if (key.signature_id.has_value()) {
key.signature_id = context.sem_ir().clang_decl_signatures().Add(
file.clang_decl_signatures().Get(key.signature_id));
}
if (ImportCppDecl(context, loc_id, key) != SemIR::ErrorInst::InstId) {
return key;
}
return std::nullopt;
}
const auto* decl = key.decl;
auto* corresponding = FindCorrespondingDecl(context, loc_id, decl);
if (!corresponding) {