Files
carbon-lang/toolchain/check/cpp/domain.h
T
Richard Smith 34a2e270f4 Fix C++ code generation in --share-cpp-ast mode (#7605)
Instead of creating a CodeGenerator per CppDomain, and then crashing in
lowering when we try to consume the same llvm Module multiple times,
create a CodeGenerator for each CppFile within the domain.

For now, we mulitplex all of Clang's ASTConsumer output to all code
generators, which means that any strong external definitions within a
Carbon file (for example, in an inline `Cpp` fragment) will be emitted
to all output files in the same `CppDomain`, resulting in link errors
due to symbol redefinitions. This will be addressed later. But this
should be sufficient for Carbon compilations in which such symbols are
not defined.

We also don't yet attempt to classify which compilations will need C++
code generation, and instead create a clang `CodeGenerator` for every
Carbon file that has C++ imports. For `carbom compile`, only one Carbon
file will need code generation, and yet we still build multiple
`CodeGenerator` objects in general. Fixing this requires more plumbing
from the driver, and this will also be handled in a follow-up.

Assisted-by: Gemini via Antigravity
2026-08-04 17:59:06 +00:00

70 lines
2.1 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_DOMAIN_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_DOMAIN_H_
#include <memory>
#include "common/check.h"
#include "common/map.h"
#include "llvm/ADT/ArrayRef.h"
#include "toolchain/sem_ir/ids.h"
namespace clang {
class CodeGenerator;
class CompilerInstance;
class Parser;
} // namespace clang
namespace llvm {
class LLVMContext;
} // namespace llvm
namespace Carbon::Check {
// An input Carbon file and its CheckIRId for C++ domain code generation.
struct CppInputFile {
SemIR::CheckIRId check_ir_id;
llvm::StringRef filename;
};
// 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.
class CppDomain {
public:
explicit CppDomain(std::shared_ptr<clang::CompilerInstance> clang_instance,
std::unique_ptr<clang::Parser> parser,
llvm::ArrayRef<CppInputFile> inputs,
llvm::ArrayRef<clang::CodeGenerator*> code_generators,
llvm::LLVMContext* llvm_context);
~CppDomain();
auto clang_instance() const -> clang::CompilerInstance& {
return *clang_instance_;
}
auto clang_instance_ptr() const -> std::shared_ptr<clang::CompilerInstance> {
return clang_instance_;
}
auto parser() const -> clang::Parser& { return *parser_; }
auto llvm_context() const -> llvm::LLVMContext* { return llvm_context_; }
auto GetCodeGenerator(SemIR::CheckIRId check_ir_id) const
-> clang::CodeGenerator* {
auto res = code_generators_.Lookup(check_ir_id);
CARBON_CHECK(res, "No CodeGenerator found for CheckIRId {0}", check_ir_id);
return res.value();
}
private:
std::shared_ptr<clang::CompilerInstance> clang_instance_;
std::unique_ptr<clang::Parser> parser_;
Map<SemIR::CheckIRId, clang::CodeGenerator*> code_generators_;
llvm::LLVMContext* llvm_context_ = nullptr;
};
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_DOMAIN_H_