Files
carbon-lang/toolchain/check/cpp/context.h
Richard Smith d08805f1a6 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
2026-10-16 20:59:16 +00:00

102 lines
3.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_
#include <memory>
#include "clang/Basic/SourceLocation.h"
#include "common/check.h"
#include "common/map.h"
#include "llvm/ADT/SmallVector.h"
#include "toolchain/check/cpp/diagnostic_listener.h"
#include "toolchain/sem_ir/cpp_domain.h"
#include "toolchain/sem_ir/ids.h"
namespace clang {
class ASTContext;
class CompilerInstance;
class FunctionDecl;
class MangleContext;
class NamespaceDecl;
class Parser;
class Sema;
} // namespace clang
namespace Carbon::Check {
// Context for C++ code during check.
//
// This stores state for a Clang AST and Sema, as well as any additional
// information needed to perform mapping between Carbon and C++ types,
// declarations, and similar values.
class CppContext {
public:
explicit CppContext(SemIR::CppDomain& domain,
std::unique_ptr<CppDiagnosticListener> listener);
~CppContext();
auto ast_context() -> clang::ASTContext&;
auto sema() -> clang::Sema&;
auto parser() -> clang::Parser& { return domain_->parser(); }
auto domain() -> SemIR::CppDomain& { return *domain_; }
auto domain() const -> const SemIR::CppDomain& { return *domain_; }
auto clang_mangle_context() -> clang::MangleContext&;
auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
return carbon_file_locations_;
}
auto carbon_lookup_namespace() const -> clang::NamespaceDecl* {
return carbon_lookup_namespace_;
}
void set_carbon_lookup_namespace(clang::NamespaceDecl* decl) {
carbon_lookup_namespace_ = decl;
}
auto lookup_namespaces() -> Map<SemIR::NameScopeId, clang::NamespaceDecl*>& {
return lookup_namespaces_;
}
auto placement_new_decl() const -> clang::FunctionDecl* {
return placement_new_decl_;
}
void set_placement_new_decl(clang::FunctionDecl* decl) {
placement_new_decl_ = decl;
}
private:
// The C++ compilation domain.
SemIR::CppDomain* domain_;
// The per-file lookup-only `::Carbon` namespace and child namespaces.
// These are only used for C++ name lookup within this Carbon file, and are
// never used as the parent `DeclContext` for exported Carbon entities.
clang::NamespaceDecl* carbon_lookup_namespace_ = nullptr;
Map<SemIR::NameScopeId, clang::NamespaceDecl*> lookup_namespaces_;
// TODO: All of the below state that is not specific to a particular
// Check::Context or SemIR::File should be moved into CppDomain.
// Per-Carbon-file start locations for corresponding Clang source buffers.
// Owned and managed by code in location.cpp.
llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
// The Clang mangle context for the target in the ASTContext.
std::unique_ptr<clang::MangleContext> clang_mangle_context_;
// The cached placement new function declaration.
clang::FunctionDecl* placement_new_decl_ = nullptr;
// Listener for Clang diagnostics while checking this Carbon context.
std::unique_ptr<CppDiagnosticListener> diagnostic_listener_;
};
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_