Skip to content

Commit a4a229b

Browse files
authored
Initialize cpp_mangle_context_ in Mangler's constructor (#5095)
This is a followup of [a comment](https://github.com/carbon-language/carbon-lang/pull/5062/files/89e56d51858bcc18d4242d4e5c9ee0e7496d887e#r1979993815) in #5062. Add a mutable AST pointer to `FileContext`. This is necessary since we use [Clang with lack of const correctness](llvm/llvm-project#130096 (comment)). Alternatives in Clang: * Change `ASTUnit::getASTContext() const` to return a non-const `ASTContext`. [Tried and was rejected upstream due to weakening const correctness](llvm/llvm-project#130096). * Change `createMangleContext()` to be `const`. Tried that and it seems like it relies heavily on non const API. * Change `MangleContext::mangleName()` to `const`. Tried that but there are several lazy initialization and id creations happening that modify the context. See details in llvm/llvm-project#130613. Alternatives in Carbon: * Use `const_cast` on `ASTContext` when calling `createMangleContext()`. * Make `FileContext::sem_ir_` point to a mutable `SemIR::File`. * Change `File::cpp_ast()` to be const while keeping it return a mutable pointer. Part of #4666.
1 parent 2881814 commit a4a229b

File tree

8 files changed

+28
-17
lines changed

8 files changed

+28
-17
lines changed

toolchain/driver/compile_subcommand.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,10 @@ auto CompilationUnit::RunLower(
598598
// TODO: Consider disabling instruction naming by default if we're not
599599
// producing textual LLVM IR.
600600
SemIR::InstNamer inst_namer(&*sem_ir_);
601-
module_ = Lower::LowerToLLVM(
602-
*llvm_context_, tree_and_subtrees_getters_for_debug_info,
603-
input_filename_, *sem_ir_, &inst_namer, vlog_stream_);
601+
module_ = Lower::LowerToLLVM(*llvm_context_,
602+
tree_and_subtrees_getters_for_debug_info,
603+
input_filename_, *sem_ir_, sem_ir_->cpp_ast(),
604+
&inst_namer, vlog_stream_);
604605
});
605606
if (vlog_stream_) {
606607
CARBON_VLOG("*** llvm::Module ***\n");

toolchain/lower/file_context.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ FileContext::FileContext(
3131
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
3232
tree_and_subtrees_getters_for_debug_info,
3333
llvm::StringRef module_name, const SemIR::File& sem_ir,
34-
const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream)
34+
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
35+
llvm::raw_ostream* vlog_stream)
3536
: llvm_context_(&llvm_context),
3637
llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
3738
di_builder_(*llvm_module_),
@@ -42,6 +43,7 @@ FileContext::FileContext(
4243
tree_and_subtrees_getters_for_debug_info_(
4344
tree_and_subtrees_getters_for_debug_info),
4445
sem_ir_(&sem_ir),
46+
cpp_ast_(cpp_ast),
4547
inst_namer_(inst_namer),
4648
vlog_stream_(vlog_stream) {
4749
CARBON_CHECK(!sem_ir.has_errors(),

toolchain/lower/file_context.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class FileContext {
3333
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
3434
tree_and_subtrees_getters_for_debug_info,
3535
llvm::StringRef module_name, const SemIR::File& sem_ir,
36-
const SemIR::InstNamer* inst_namer, llvm::raw_ostream* vlog_stream);
36+
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
37+
llvm::raw_ostream* vlog_stream);
3738

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

167+
// A mutable Clang AST is necessary for lowering since using the AST in lower
168+
// modifies it.
169+
clang::ASTUnit* cpp_ast_;
170+
165171
// The instruction namer, if given.
166172
const SemIR::InstNamer* const inst_namer_;
167173

toolchain/lower/lower.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ auto LowerToLLVM(llvm::LLVMContext& llvm_context,
1212
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
1313
tree_and_subtrees_getters_for_debug_info,
1414
llvm::StringRef module_name, const SemIR::File& sem_ir,
15-
const SemIR::InstNamer* inst_namer,
15+
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
1616
llvm::raw_ostream* vlog_stream)
1717
-> std::unique_ptr<llvm::Module> {
1818
FileContext context(llvm_context, tree_and_subtrees_getters_for_debug_info,
19-
module_name, sem_ir, inst_namer, vlog_stream);
19+
module_name, sem_ir, cpp_ast, inst_namer, vlog_stream);
2020
return context.Run();
2121
}
2222

toolchain/lower/lower.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ auto LowerToLLVM(llvm::LLVMContext& llvm_context,
1919
std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
2020
tree_and_subtrees_getters_for_debug_info,
2121
llvm::StringRef module_name, const SemIR::File& sem_ir,
22-
const SemIR::InstNamer* inst_namer,
22+
clang::ASTUnit* cpp_ast, const SemIR::InstNamer* inst_namer,
2323
llvm::raw_ostream* vlog_stream)
2424
-> std::unique_ptr<llvm::Module>;
2525

toolchain/lower/mangler.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,9 @@ auto Mangler::Mangle(SemIR::FunctionId function_id,
162162
}
163163

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

173169
RawStringOstream cpp_mangled_name;
174170
cpp_mangle_context_->mangleName(decl, cpp_mangled_name);

toolchain/lower/mangler.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class Mangler {
2222
public:
2323
// Initialize a new Mangler instance for mangling entities within the
2424
// specified `FileContext`.
25-
explicit Mangler(FileContext& file_context) : file_context_(file_context) {}
25+
explicit Mangler(FileContext& file_context)
26+
: file_context_(file_context),
27+
cpp_mangle_context_(
28+
file_context.cpp_ast()
29+
? file_context.cpp_ast()->getASTContext().createMangleContext()
30+
: nullptr) {}
2631

2732
// Produce a deterministically unique mangled name for the function specified
2833
// by `function_id` and `specific_id`.

toolchain/sem_ir/file.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ class File : public Printable<File> {
189189
}
190190
auto cpp_ast() -> clang::ASTUnit* { return cpp_ast_; }
191191
// TODO: When the AST can be created before creating `File`, initialize the
192-
// pointer in the constructor and remove this function.
192+
// pointer in the constructor and remove this function. This is part of
193+
// https://github.com/carbon-language/carbon-lang/issues/4666
193194
auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void { cpp_ast_ = cpp_ast; }
194195
auto names() const -> NameStoreWrapper {
195196
return NameStoreWrapper(&identifiers());

0 commit comments

Comments
 (0)