Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions toolchain/driver/compile_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,10 @@ auto CompilationUnit::RunLower(
// TODO: Consider disabling instruction naming by default if we're not
// producing textual LLVM IR.
SemIR::InstNamer inst_namer(&*sem_ir_);
module_ = Lower::LowerToLLVM(
*llvm_context_, tree_and_subtrees_getters_for_debug_info,
input_filename_, *sem_ir_, &inst_namer, vlog_stream_);
module_ = Lower::LowerToLLVM(*llvm_context_,
tree_and_subtrees_getters_for_debug_info,
input_filename_, *sem_ir_, sem_ir_->cpp_ast(),
&inst_namer, vlog_stream_);
});
if (vlog_stream_) {
CARBON_VLOG("*** llvm::Module ***\n");
Expand Down
4 changes: 3 additions & 1 deletion toolchain/lower/file_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ FileContext::FileContext(
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
tree_and_subtrees_getters_for_debug_info,
llvm::StringRef module_name, const SemIR::File& sem_ir,
const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream)
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
llvm::raw_ostream* vlog_stream)
: llvm_context_(&llvm_context),
llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
di_builder_(*llvm_module_),
Expand All @@ -42,6 +43,7 @@ FileContext::FileContext(
tree_and_subtrees_getters_for_debug_info_(
tree_and_subtrees_getters_for_debug_info),
sem_ir_(&sem_ir),
cpp_ast_(cpp_ast),
inst_namer_(inst_namer),
vlog_stream_(vlog_stream) {
CARBON_CHECK(!sem_ir.has_errors(),
Expand Down
8 changes: 7 additions & 1 deletion toolchain/lower/file_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class FileContext {
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
tree_and_subtrees_getters_for_debug_info,
llvm::StringRef module_name, const SemIR::File& sem_ir,
const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream);
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
llvm::raw_ostream* vlog_stream);

// Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
// the main execution loop.
Expand Down Expand Up @@ -91,6 +92,7 @@ class FileContext {
auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
auto llvm_module() -> llvm::Module& { return *llvm_module_; }
auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
return global_variables_;
Expand Down Expand Up @@ -162,6 +164,10 @@ class FileContext {
// The input SemIR.
const SemIR::File* const sem_ir_;

// A mutable Clang AST is necessary for lowering since using the AST in lower
// modifies it.
clang::ASTUnit* cpp_ast_;

// The instruction namer, if given.
const SemIR::InstNamer* const inst_namer_;

Expand Down
4 changes: 2 additions & 2 deletions toolchain/lower/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ auto LowerToLLVM(llvm::LLVMContext& llvm_context,
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
tree_and_subtrees_getters_for_debug_info,
llvm::StringRef module_name, const SemIR::File& sem_ir,
const SemIR::InstNamer* inst_namer,
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
llvm::raw_ostream* vlog_stream)
-> std::unique_ptr<llvm::Module> {
FileContext context(llvm_context, tree_and_subtrees_getters_for_debug_info,
module_name, sem_ir, inst_namer, vlog_stream);
module_name, sem_ir, cpp_ast, inst_namer, vlog_stream);
return context.Run();
}

Expand Down
2 changes: 1 addition & 1 deletion toolchain/lower/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ auto LowerToLLVM(llvm::LLVMContext& llvm_context,
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
tree_and_subtrees_getters_for_debug_info,
llvm::StringRef module_name, const SemIR::File& sem_ir,
const SemIR::InstNamer* inst_namer,
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
llvm::raw_ostream* vlog_stream)
-> std::unique_ptr<llvm::Module>;

Expand Down
10 changes: 3 additions & 7 deletions toolchain/lower/mangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,9 @@ auto Mangler::Mangle(SemIR::FunctionId function_id,
}

auto Mangler::MangleCppClang(const clang::NamedDecl* decl) -> std::string {
if (!cpp_mangle_context_) {
// We assume all declarations are from the same AST Context.
// TODO: Consider initializing this in the constructor. This is related to:
// https://github.com/carbon-language/carbon-lang/issues/4666. See
// https://github.com/carbon-language/carbon-lang/pull/5062/files/89e56d51858bcc18d4242d4e5c9ee0e7496d887e#r1979993815
cpp_mangle_context_.reset(decl->getASTContext().createMangleContext());
}
CARBON_CHECK(
cpp_mangle_context_,
"Mangling of a C++ imported declaration without a Clang `MangleContext`");

RawStringOstream cpp_mangled_name;
cpp_mangle_context_->mangleName(decl, cpp_mangled_name);
Expand Down
7 changes: 6 additions & 1 deletion toolchain/lower/mangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class Mangler {
public:
// Initialize a new Mangler instance for mangling entities within the
// specified `FileContext`.
explicit Mangler(FileContext& file_context) : file_context_(file_context) {}
explicit Mangler(FileContext& file_context)
: file_context_(file_context),
cpp_mangle_context_(
file_context.cpp_ast()
? file_context.cpp_ast()->getASTContext().createMangleContext()
: nullptr) {}

// Produce a deterministically unique mangled name for the function specified
// by `function_id` and `specific_id`.
Expand Down
3 changes: 2 additions & 1 deletion toolchain/sem_ir/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ class File : public Printable<File> {
}
auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
// TODO: When the AST can be created before creating `File`, initialize the
// pointer in the constructor and remove this function.
// pointer in the constructor and remove this function. This is part of
// https://github.com/carbon-language/carbon-lang/issues/4666
auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void { cpp_ast_ = cpp_ast; }
auto names() const -> NameStoreWrapper {
return NameStoreWrapper(&identifiers());
Expand Down