mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 22:20:29 +00:00
Use the plain identifier for Carbon names in C++ (#7897)
C++ interop used `GetFormatted` to turn a Carbon name into a C++ name. For an identifier that is a Carbon keyword, such as `base`, `GetFormatted` adds an `r#` prefix, so the C++ name came out as `r#base` instead of `base`. This showed up in three places: - A failed call to the C++ function `base` was reported as "no matching function for call to 'r#base'". - An exported Carbon function was mangled as `_ZN6CarbonL6r#baseEv` instead of `_ZN6CarbonL4baseEv`. - Its thunk was named `r#base__carbon_thunk` instead of `base__carbon_thunk`. All three now use `GetClangIdentifierInfo`, which `export.cpp` already uses for other C++ names. The name is always an identifier in these places: an overload set is only imported by looking up an identifier in C++, and only functions declared in Carbon are exported, which are always named by an identifier. So they check for an identifier, as the export of a class name already does, rather than falling back to `GetFormatted`. This removes a TODO about naming a `NameId::CppOperator` overload set, which can't happen: C++ operators are resolved in `operators.cpp` without an overload set. Assisted-by: Claude Code
This commit is contained in:
1 parent
8f258eaaa6
commit
c1e83b0b78
4 files changed
+124
-11
No files matched your search
@@ -25,14 +25,14 @@
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
// Map a Carbon name into a C++ name.
|
||||
// Map a Carbon name into a C++ name. An overload set is only imported by
|
||||
// looking up an identifier in C++, so its name is always an identifier.
|
||||
static auto GetCppName(Context& context, SemIR::NameId name_id)
|
||||
-> clang::DeclarationName {
|
||||
// TODO: Some special names should probably use different formatting. In
|
||||
// particular, NameId::CppOperator should probably map back to a
|
||||
// CXXOperatorName.
|
||||
auto name_str = context.names().GetFormatted(name_id);
|
||||
return clang::DeclarationName(&context.ast_context().Idents.get(name_str));
|
||||
auto* identifier_info = GetClangIdentifierInfo(context, name_id);
|
||||
CARBON_CHECK(identifier_info, "non-identifier overload set name {0}",
|
||||
name_id);
|
||||
return clang::DeclarationName(identifier_info);
|
||||
}
|
||||
|
||||
// Adds the given overload candidates to the candidate set.
|
||||
|
||||
Reference in new issue
Block a user