mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 22:02:37 +01:00
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
73 lines
2.6 KiB
C++
73 lines
2.6 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_GENERATE_AST_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_CPP_GENERATE_AST_H_
|
|
|
|
#include <memory>
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/parse/tree.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace clang {
|
|
class CompilerInvocation;
|
|
class CompilerInstance;
|
|
class Parser;
|
|
class ExternalSemaSource;
|
|
class CodeGenerator;
|
|
} // namespace clang
|
|
|
|
namespace Carbon::Diagnostics {
|
|
class Consumer;
|
|
} // namespace Carbon::Diagnostics
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// A C++ compilation domain, including a live Clang instance that can be used to
|
|
// parse more code into that domain. May be shared across multiple Carbon files.
|
|
struct CppDomain {
|
|
std::shared_ptr<clang::CompilerInstance> clang_instance;
|
|
std::shared_ptr<clang::Parser> parser;
|
|
clang::CodeGenerator* code_generator = nullptr;
|
|
llvm::LLVMContext* llvm_context = nullptr;
|
|
};
|
|
|
|
// Initializes a Clang compilation instance, which can be used to parse C++ code
|
|
// within one or more Carbon files. Returns the initialized state, or null on
|
|
// failure.
|
|
auto InitializeCppDomain(
|
|
Diagnostics::Consumer& consumer, llvm::StringRef filename,
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
|
|
llvm::LLVMContext* llvm_context,
|
|
std::shared_ptr<clang::CompilerInvocation> base_invocation)
|
|
-> std::shared_ptr<CppDomain>;
|
|
|
|
// Generates a Clang AST for the given C++ imports and sets it as the context's
|
|
// `cpp_context` and the SemIR's `cpp_file`. Returns a bool that represents
|
|
// whether compilation was successful.
|
|
auto GenerateAst(Context& context,
|
|
llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
|
|
CppDomain& domain) -> bool;
|
|
|
|
// Injects C++ code from `inline Cpp` into the active Clang AST context.
|
|
// Returns a bool representing whether parsing was successful.
|
|
auto InjectAstFromInlineCode(Context& context, SemIR::LocId loc_id,
|
|
llvm::StringRef source_code) -> void;
|
|
|
|
// Finishes AST generation for the given checking context. Performs end of file
|
|
// steps such as template instantiation and warning on unused declarations.
|
|
auto FinishAst(Context& context) -> void;
|
|
|
|
// Finalizes a C++ domain at the end of checking all files.
|
|
auto FinalizeCppDomain(CppDomain& domain) -> void;
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_CPP_GENERATE_AST_H_
|