Files
carbon-lang/toolchain/check/cpp/context.h
T
Richard Smith 7d89ac98c7 Add a flag to build a single ASTContext shared across all compilations (#7567)
Instead of building one Clang `ASTContext` per compilation, the
`--share-cpp-ast` flag causes us to build a single `ASTContext` and
share it across all contexts. One new abstraction is added: `CppDomain`
represents the Carbon-side view of a Clang AST that might be shared
across multiple `SemIR::File`s. This object owns the Clang instance and
the AST.

For now, we have no isolation between the C++ state exposed to different
Carbon compilations, and we have no multiplexing of generated LLVM IR
from C++ into different Carbon compilations, so the mode is not usable
yet. The plan is to keep it behind a flag until it's ready.

Assisted-by: Gemini via Antigravity
2026-07-28 00:19:40 +00:00

83 lines
2.5 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 "llvm/ADT/SmallVector.h"
#include "toolchain/check/cpp/diagnostic_listener.h"
namespace clang {
class ASTContext;
class CompilerInstance;
class FunctionDecl;
class MangleContext;
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(clang::CompilerInstance& instance,
std::shared_ptr<clang::Parser> parser,
std::unique_ptr<CppDiagnosticListener> listener);
~CppContext();
auto ast_context() -> clang::ASTContext& { return *ast_context_; }
auto sema() -> clang::Sema& { return *sema_; }
auto parser() -> clang::Parser& { return *parser_; }
auto parser_ptr() const -> std::shared_ptr<clang::Parser> { return parser_; }
auto clang_mangle_context() -> clang::MangleContext&;
auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
return carbon_file_locations_;
}
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 Clang AST context.
clang::ASTContext* ast_context_;
// The Clang semantic analysis engine.
clang::Sema* sema_;
// The Clang parser.
std::shared_ptr<clang::Parser> parser_;
// 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_