mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 19:55:11 +00:00
Fix Carbon:: name leakage across files. (#7973)
Split the `Carbon::` namespace into one semantic namespace tree, plus one separate lookup-only namespace tree per Carbon file. Use the semantic namespaces as the parents of entities, but use the lookup-only namespaces when C++ name lookup names a Carbon namespace, in order to use a different C++ name lookup table in each Carbon file. Assisted-by: Gemini via Antigravity
This commit is contained in:
1 parent
0d3b606a56
commit
d08805f1a6
10 files changed
+299
-79
No files matched your search
@@ -214,8 +214,8 @@ auto CarbonExternalASTSource::MapInstIdToClangDeclOrType(LookupResult lookup)
|
||||
CARBON_KIND_SWITCH(target_inst) {
|
||||
case CARBON_KIND(SemIR::Namespace namespace_info): {
|
||||
auto* decl_context =
|
||||
ExportNameScopeToCpp(*context_, SemIR::LocId(target_inst_id),
|
||||
namespace_info.name_scope_id);
|
||||
ExportLookupNamespaceToCpp(*context_, SemIR::LocId(target_inst_id),
|
||||
namespace_info.name_scope_id);
|
||||
if (!decl_context) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -258,39 +258,28 @@ auto CarbonExternalASTSource::BuildCarbonNamespace() -> void {
|
||||
auto& ast_context = context_->ast_context();
|
||||
auto* identifier = &ast_context.Idents.get(carbon_namespace_name);
|
||||
|
||||
auto* decl_context = ast_context.getTranslationUnitDecl();
|
||||
auto* tu_decl = ast_context.getTranslationUnitDecl();
|
||||
|
||||
// Check if it already exists.
|
||||
clang::NamespaceDecl* carbon_cpp_namespace = nullptr;
|
||||
auto lookup_result = decl_context->lookup(identifier);
|
||||
if (!lookup_result.empty()) {
|
||||
carbon_cpp_namespace = cast<clang::NamespaceDecl>(lookup_result.front());
|
||||
} else {
|
||||
// Create it if it doesn't exist.
|
||||
carbon_cpp_namespace = clang::NamespaceDecl::Create(
|
||||
ast_context, decl_context, /*Inline=*/false, clang::SourceLocation(),
|
||||
clang::SourceLocation(), identifier, /*PrevDecl=*/nullptr,
|
||||
/*Nested=*/false);
|
||||
decl_context->addDecl(carbon_cpp_namespace);
|
||||
// Create or find the semantic `::Carbon` namespace for this C++ domain, which
|
||||
// is used as the parent `DeclContext` for package sub-namespaces.
|
||||
auto& domain = context_->cpp_context()->domain();
|
||||
auto* semantic_carbon_namespace = domain.carbon_namespace();
|
||||
if (!semantic_carbon_namespace) {
|
||||
semantic_carbon_namespace =
|
||||
CreateSemanticNamespace(*context_, tu_decl, identifier);
|
||||
domain.set_carbon_namespace(semantic_carbon_namespace);
|
||||
}
|
||||
|
||||
// We provide custom lookup results within this namespace.
|
||||
carbon_cpp_namespace->getPrimaryContext()->setHasExternalVisibleStorage();
|
||||
|
||||
// Create or find the sub-namespace for this file's package, which is used as
|
||||
// the parent `DeclContext` for package-scope entities so that the package
|
||||
// name is included in C++ mangled names.
|
||||
// Create or find the semantic sub-namespace for this file's package, which is
|
||||
// used as the parent `DeclContext` for package-scope entities so that the
|
||||
// package name is included in C++ mangled names.
|
||||
auto& package_scope =
|
||||
context_->name_scopes().Get(SemIR::NameScopeId::Package);
|
||||
auto package_name = context_->names().GetIRBaseName(package_scope.name_id());
|
||||
auto* package_identifier = &ast_context.Idents.get(package_name);
|
||||
auto* package_cpp_namespace = ExportPackageNamespaceToCpp(
|
||||
*context_, carbon_cpp_namespace, package_identifier);
|
||||
auto* package_cpp_namespace = ExportNamespaceToCpp(
|
||||
*context_, semantic_carbon_namespace, package_identifier);
|
||||
|
||||
// Register this file's package scope as corresponding to the package
|
||||
// sub-namespace in C++, and also map the `Carbon` namespace itself to the
|
||||
// package scope so that C++ lookups in `Carbon` find package-scope entities
|
||||
// directly (and do not find the current package name).
|
||||
auto package_key =
|
||||
SemIR::ClangDeclKey::ForNonFunctionDecl(package_cpp_namespace);
|
||||
auto package_clang_decl_id = context_->clang_decls().Add(
|
||||
@@ -298,8 +287,22 @@ auto CarbonExternalASTSource::BuildCarbonNamespace() -> void {
|
||||
package_scope.set_clang_decl_context_id(package_clang_decl_id,
|
||||
/*is_cpp_scope=*/false);
|
||||
|
||||
// Create a per-file lookup-only `::Carbon` namespace, mapped to the package
|
||||
// scope so that C++ lookups in `Carbon` find package-scope entities directly
|
||||
// (and do not find the current package name), and are isolated to this file.
|
||||
auto* lookup_carbon_namespace = clang::NamespaceDecl::Create(
|
||||
ast_context, tu_decl, /*Inline=*/false, clang::SourceLocation(),
|
||||
clang::SourceLocation(), identifier, /*PrevDecl=*/nullptr,
|
||||
/*Nested=*/false);
|
||||
tu_decl->addDecl(lookup_carbon_namespace);
|
||||
lookup_carbon_namespace->setHasExternalVisibleStorage();
|
||||
|
||||
context_->cpp_context()->set_carbon_lookup_namespace(lookup_carbon_namespace);
|
||||
context_->cpp_context()->lookup_namespaces().Insert(
|
||||
SemIR::NameScopeId::Package, lookup_carbon_namespace);
|
||||
|
||||
auto carbon_key =
|
||||
SemIR::ClangDeclKey::ForNonFunctionDecl(carbon_cpp_namespace);
|
||||
SemIR::ClangDeclKey::ForNonFunctionDecl(lookup_carbon_namespace);
|
||||
context_->clang_decls().Add(
|
||||
{.key = carbon_key, .inst_id = SemIR::Namespace::PackageInstId});
|
||||
}
|
||||
@@ -394,14 +397,30 @@ auto CarbonExternalASTSource::FindExternalVisibleDeclsByName(
|
||||
}
|
||||
|
||||
case CARBON_KIND(clang::QualType type): {
|
||||
// Create a typedef declaration to model the type result.
|
||||
// Create a typedef declaration to model the type result. We create the
|
||||
// typedef declaration in whatever name context the lookup was performed
|
||||
// in, since we don't have a Carbon declaration to take a name scope from.
|
||||
// But if that context was a namespace, map from the lookup-only namespace
|
||||
// to the corresponding semantic namespace so that lookup-only
|
||||
// NamespaceDecls are never the parent DeclContext of exported entities.
|
||||
// TODO: If the type is a tag type that was declared with this name in
|
||||
// this context, use the tag decl directly.
|
||||
auto* semantic_decl_context = decl_context;
|
||||
if (auto namespace_info = context_->insts().TryGetAs<SemIR::Namespace>(
|
||||
clang_decl->inst_id)) {
|
||||
semantic_decl_context = ExportNameScopeToCpp(
|
||||
*context_, SemIR::LocId(result.scope_result.target_inst_id()),
|
||||
namespace_info->name_scope_id);
|
||||
if (!semantic_decl_context) {
|
||||
SetNoExternalVisibleDeclsForName(decl_context, decl_name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto& ast_context = context_->ast_context();
|
||||
auto loc = GetCppLocation(
|
||||
*context_, SemIR::LocId(result.scope_result.target_inst_id()));
|
||||
auto* typedef_decl = clang::TypedefDecl::Create(
|
||||
ast_context, decl_context, loc, loc, identifier,
|
||||
ast_context, semantic_decl_context, loc, loc, identifier,
|
||||
ast_context.getTrivialTypeSourceInfo(type, loc));
|
||||
if (isa<clang::CXXRecordDecl>(decl_context)) {
|
||||
typedef_decl->setAccess(
|
||||
@@ -1097,6 +1116,21 @@ auto FinishAst(Context& context) -> void {
|
||||
*context.cpp_context()->sema().getDiagnostics().getClient());
|
||||
context.emitter().Flush();
|
||||
|
||||
// Hide the per-file lookup-only `::Carbon` namespace from the further lookup
|
||||
// results and remove it from the translation unit's name lookup table if it's
|
||||
// already there, so that subsequent files in a shared C++ AST domain do not
|
||||
// see it.
|
||||
if (auto* lookup_ns = context.cpp_context()->carbon_lookup_namespace()) {
|
||||
lookup_ns->clearIdentifierNamespace();
|
||||
auto* tu_decl = context.ast_context().getTranslationUnitDecl();
|
||||
if (auto* lookup = tu_decl->getLookupPtr()) {
|
||||
if (auto it = lookup->find(lookup_ns->getDeclName());
|
||||
it != lookup->end()) {
|
||||
it->second.remove(lookup_ns);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the `CarbonExternalASTSource` installed in `GenerateAst` and
|
||||
// replace it with a `ReadOnlyASTSource`. This is necessary because
|
||||
// the source may be accessed later during lowering, but the
|
||||
|
||||
Reference in new issue
Block a user