// 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_SEM_IR_CPP_DOMAIN_H_ #define CARBON_TOOLCHAIN_SEM_IR_CPP_DOMAIN_H_ #include #include #include "common/check.h" #include "common/map.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringMap.h" #include "toolchain/sem_ir/ids.h" namespace clang { class CodeGenerator; class CompilerInstance; class DeclContext; class IdentifierInfo; class Module; class NamespaceDecl; class Parser; } // namespace clang namespace llvm { class LLVMContext; } // namespace llvm namespace Carbon::SemIR { // An input Carbon file and its CheckIRId for C++ domain code generation. struct CppInputFile { // The ID used to identify this file within SemIR. CheckIRId check_ir_id; // The Carbon source filename for this input. llvm::StringRef filename; // Whether this input IR will be lowered. If not, we don't need to build a // Clang CodeGenerator for it. bool is_lowered; }; // 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: // A map from parent context and namespace name to semantic namespace, for // cross-file deduplication of exported packages and namespaces. We use the // same semantic C++ namespace for the same Carbon package and namespace path // in every file. using SemanticNamespacesMap = Map, clang::NamespaceDecl*>; explicit CppDomain(std::shared_ptr clang_instance, std::unique_ptr parser, llvm::ArrayRef inputs, llvm::ArrayRef code_generators, llvm::LLVMContext* llvm_context); ~CppDomain(); auto clang_instance() const -> clang::CompilerInstance& { return *clang_instance_; } auto clang_instance_ptr() const -> std::shared_ptr { return clang_instance_; } auto parser() const -> clang::Parser& { return *parser_; } auto llvm_context() const -> llvm::LLVMContext* { return llvm_context_; } auto GetCodeGenerator(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(); } auto file_modules() -> Map& { return modules_; } auto header_modules() -> llvm::StringMap& { return header_modules_; } auto carbon_namespace() const -> clang::NamespaceDecl* { return carbon_namespace_; } auto set_carbon_namespace(clang::NamespaceDecl* carbon_namespace) -> void { carbon_namespace_ = carbon_namespace; } auto semantic_namespaces() -> SemanticNamespacesMap& { return semantic_namespaces_; } private: std::shared_ptr clang_instance_; std::unique_ptr parser_; Map code_generators_; Map modules_; llvm::StringMap header_modules_; clang::NamespaceDecl* carbon_namespace_ = nullptr; SemanticNamespacesMap semantic_namespaces_; llvm::LLVMContext* llvm_context_ = nullptr; }; } // namespace Carbon::SemIR #endif // CARBON_TOOLCHAIN_SEM_IR_CPP_DOMAIN_H_