Only export each class or namespace to C++ once. (#7042)

Instead of exporting a class or namespace each time a new C++ name
lookup discovers it, track that we have exported the entity on its name
scope, and if a new name lookup finds the same entity, produce the same
clang declaration.
This commit is contained in:
Richard Smith
2026-04-13 19:43:44 +00:00
committed by GitHub
parent 3cdb159067
commit f31e1685fd
11 changed files with 325 additions and 161 deletions
+163 -4
View File
@@ -14,6 +14,159 @@
namespace Carbon::Check {
// If the given name scope was produced by importing a C++ declaration or has
// already been exported to C++, return the corresponding Clang decl context.
static auto GetClangDeclContextForScope(Context& context,
SemIR::NameScopeId scope_id)
-> clang::DeclContext* {
if (!scope_id.has_value()) {
return nullptr;
}
auto& scope = context.name_scopes().Get(scope_id);
auto clang_decl_context_id = scope.clang_decl_context_id();
if (!clang_decl_context_id.has_value()) {
return nullptr;
}
auto* decl = context.clang_decls().Get(clang_decl_context_id).key.decl;
return cast<clang::DeclContext>(decl);
}
auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id,
SemIR::NameScopeId name_scope_id)
-> clang::DeclContext* {
llvm::SmallVector<SemIR::NameScopeId> name_scope_ids_to_create;
// Walk through the parent scopes, looking for one that's already mapped into
// C++. We already mapped the package scope to ::Carbon, so we must find one.
clang::DeclContext* decl_context = nullptr;
while (true) {
// If this name scope was produced by importing a C++ declaration or has
// already been exported to C++, return the corresponding Clang declaration.
if (auto* existing_decl_context =
GetClangDeclContextForScope(context, name_scope_id)) {
decl_context = existing_decl_context;
break;
}
// Otherwise, continue to the parent and create a scope for it first.
name_scope_ids_to_create.push_back(name_scope_id);
name_scope_id = context.name_scopes().Get(name_scope_id).parent_scope_id();
// TODO: What should happen if there's an intervening function scope?
CARBON_CHECK(
name_scope_id.has_value(),
"Reached the top level without finding a scope mapped into C++");
}
// Create the name scopes in order, starting from the outermost one.
while (!name_scope_ids_to_create.empty()) {
name_scope_id = name_scope_ids_to_create.pop_back_val();
auto& name_scope = context.name_scopes().Get(name_scope_id);
auto* identifier_info =
GetClangIdentifierInfo(context, name_scope.name_id());
if (!identifier_info) {
// TODO: Handle keyword package names like `Cpp` and `Core`. These can
// be named from C++ via an alias.
context.TODO(loc_id, "interop with non-identifier package name");
return nullptr;
}
auto inst = context.insts().Get(name_scope.inst_id());
if (inst.Is<SemIR::Namespace>()) {
// TODO: Provide a source location.
auto* namespace_decl = clang::NamespaceDecl::Create(
context.ast_context(), decl_context, false, clang::SourceLocation(),
clang::SourceLocation(), identifier_info, nullptr, false);
decl_context = namespace_decl;
} else if (inst.Is<SemIR::ClassDecl>()) {
// TODO: Provide a source location.
auto* record_decl = clang::CXXRecordDecl::Create(
context.ast_context(), clang::TagTypeKind::Class, decl_context,
clang::SourceLocation(), clang::SourceLocation(), identifier_info);
// If this is a member class, set its access.
if (isa<clang::CXXRecordDecl>(decl_context)) {
// TODO: Map Carbon access to C++ access.
record_decl->setAccess(clang::AS_public);
}
decl_context = record_decl;
decl_context->setHasExternalLexicalStorage();
} else {
context.TODO(loc_id, "non-class non-namespace name scope");
return nullptr;
}
decl_context->setHasExternalVisibleStorage();
auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(
cast<clang::Decl>(decl_context));
auto clang_decl_id = context.clang_decls().Add(
{.key = key, .inst_id = name_scope.inst_id()});
name_scope.set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false);
}
return decl_context;
}
auto ExportClassToCpp(Context& context, SemIR::LocId loc_id,
SemIR::InstId class_inst_id, SemIR::ClassType class_type)
-> clang::TagDecl* {
// TODO: A lot of logic in this function is shared with ExportNameScopeToCpp.
// This should be refactored.
if (class_type.specific_id.has_value()) {
context.TODO(loc_id, "interop with specific class");
return nullptr;
}
const auto& class_info = context.classes().Get(class_type.class_id);
// If this class was produced by importing a C++ declaration or has
// already been exported to C++, return the corresponding Clang declaration.
// That could either be a CXXRecordDecl or an EnumDecl.
if (auto* decl_context =
GetClangDeclContextForScope(context, class_info.scope_id)) {
return cast<clang::TagDecl>(decl_context);
}
auto* identifier_info = GetClangIdentifierInfo(context, class_info.name_id);
if (!identifier_info) {
// TODO: Handle keyword package names like `Cpp` and `Core`. These can
// be named from C++ via an alias.
context.TODO(loc_id, "interop with non-identifier package name");
return nullptr;
}
auto* decl_context =
ExportNameScopeToCpp(context, loc_id, class_info.parent_scope_id);
// TODO: Provide a source location.
auto* record_decl = clang::CXXRecordDecl::Create(
context.ast_context(), clang::TagTypeKind::Class, decl_context,
clang::SourceLocation(), clang::SourceLocation(), identifier_info);
// If this is a member class, set its access.
if (isa<clang::CXXRecordDecl>(decl_context)) {
// TODO: Map Carbon access to C++ access.
record_decl->setAccess(clang::AS_public);
}
record_decl->setHasExternalLexicalStorage();
record_decl->setHasExternalVisibleStorage();
auto key =
SemIR::ClangDeclKey::ForNonFunctionDecl(cast<clang::Decl>(record_decl));
auto clang_decl_id =
context.clang_decls().Add({.key = key, .inst_id = class_inst_id});
if (class_info.scope_id.has_value()) {
// TODO: Record the Carbon class -> clang declaration mapping for incomplete
// classes too.
context.name_scopes()
.Get(class_info.scope_id)
.set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false);
}
return record_decl;
}
// Create a `clang::FunctionDecl` for the given Carbon function. This
// can be used to call the Carbon function from C++. The Carbon
// function's ABI must be compatible with C++.
@@ -233,9 +386,8 @@ static auto BuildCarbonToCarbonThunk(
return carbon_thunk_function_id;
}
auto GetReverseInteropFunctionDecl(Context& context, SemIR::LocId loc_id,
clang::DeclContext& decl_context,
SemIR::FunctionId callee_function_id)
auto ExportFunctionToCpp(Context& context, SemIR::LocId loc_id,
SemIR::FunctionId callee_function_id)
-> clang::FunctionDecl* {
const SemIR::Function& callee = context.functions().Get(callee_function_id);
@@ -260,6 +412,13 @@ auto GetReverseInteropFunctionDecl(Context& context, SemIR::LocId loc_id,
return nullptr;
}
// Map the parent scope into the C++ AST.
auto* decl_context =
ExportNameScopeToCpp(context, loc_id, callee.parent_scope_id);
if (!decl_context) {
return nullptr;
}
// Get the parameter types of the Carbon function being called.
auto callee_function_params =
context.inst_blocks().Get(callee.call_param_patterns_id);
@@ -283,7 +442,7 @@ auto GetReverseInteropFunctionDecl(Context& context, SemIR::LocId loc_id,
}
// Create a C++ thunk that calls the Carbon thunk.
return BuildCppToCarbonThunk(context, loc_id, &decl_context,
return BuildCppToCarbonThunk(context, loc_id, decl_context,
context.names().GetFormatted(callee.name_id),
carbon_function_decl, callee_param_type_ids);
}