diff --git a/toolchain/check/cpp/export.cpp b/toolchain/check/cpp/export.cpp index 81a021c6449e..ddae3f769bc1 100644 --- a/toolchain/check/cpp/export.cpp +++ b/toolchain/check/cpp/export.cpp @@ -702,16 +702,24 @@ struct FunctionInfo { return SemIR::TypeId::None; } + // Get the identifier that names this function. Only functions declared in + // Carbon are exported, and a Carbon declaration always names its function + // with an identifier. + auto GetIdentifier(Context& context) const -> clang::IdentifierInfo* { + auto* identifier_info = GetClangIdentifierInfo(context, function.name_id); + CARBON_CHECK(identifier_info, "non-identifier function name {0}", + function.name_id); + return identifier_info; + } + // Get the clang::DeclarationName of this function's C++ counterpart. auto GetCppName(Context& context) const -> clang::DeclarationName { if (export_as_constructor) { auto* record_decl = cast(decl_context); return context.ast_context().DeclarationNames.getCXXConstructorName( context.ast_context().getCanonicalTagType(record_decl)); - } else { - return &context.ast_context().Idents.get( - context.names().GetFormatted(function.name_id)); } + return GetIdentifier(context); } SemIR::FunctionId function_id; @@ -1300,8 +1308,7 @@ static auto BuildCarbonToCarbonThunk(Context& context, SemIR::LocId loc_id, std::string_view extra_name = "") -> FunctionInfo { // Create the thunk's name. - llvm::SmallString<64> thunk_name = - context.names().GetFormatted(target.function.name_id); + llvm::SmallString<64> thunk_name = target.GetIdentifier(context)->getName(); thunk_name += "__carbon_thunk"; thunk_name += extra_name; auto& ident = context.ast_context().Idents.get(thunk_name); diff --git a/toolchain/check/cpp/overload_resolution.cpp b/toolchain/check/cpp/overload_resolution.cpp index ebf85082e8ab..a06d6b376717 100644 --- a/toolchain/check/cpp/overload_resolution.cpp +++ b/toolchain/check/cpp/overload_resolution.cpp @@ -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. diff --git a/toolchain/check/testdata/interop/cpp/function/import/function.carbon b/toolchain/check/testdata/interop/cpp/function/import/function.carbon index 247885c1353e..baed05f4f6a9 100644 --- a/toolchain/check/testdata/interop/cpp/function/import/function.carbon +++ b/toolchain/check/testdata/interop/cpp/function/import/function.carbon @@ -78,6 +78,24 @@ fn MyF() { //@dump-sem-ir-end } +// --- fail_import_special_name_call_escaped_no_match.carbon + +library "[[@TEST_NAME]]"; + +import Cpp library "special_name.h"; + +fn MyF() { + // CHECK:STDERR: fail_import_special_name_call_escaped_no_match.carbon:[[@LINE+8]]:15: error: no matching function for call to 'base' [CppInteropParseError] + // CHECK:STDERR: 15 | Cpp.r#base(1); + // CHECK:STDERR: | ^ + // CHECK:STDERR: fail_import_special_name_call_escaped_no_match.carbon:[[@LINE-6]]:10: in file included here [InCppInclude] + // CHECK:STDERR: ./special_name.h:2:6: note: candidate function not viable: requires 0 arguments, but 1 was provided [CppInteropParseNote] + // CHECK:STDERR: 2 | auto base() -> void; + // CHECK:STDERR: | ^ + // CHECK:STDERR: + Cpp.r#base(1); +} + // ============================================================================ // Overloaded // ============================================================================ diff --git a/toolchain/lower/testdata/interop/cpp/function/export/function.carbon b/toolchain/lower/testdata/interop/cpp/function/export/function.carbon index d4467231636e..f1717ee39864 100644 --- a/toolchain/lower/testdata/interop/cpp/function/export/function.carbon +++ b/toolchain/lower/testdata/interop/cpp/function/export/function.carbon @@ -62,6 +62,22 @@ inline void G2() { fn H() { Cpp.G2(); } +// --- keyword_name.carbon + +library "[[@TEST_NAME]]"; + +import Cpp; + +fn r#base() {} + +inline Cpp ''' +inline void CallBase() { + Carbon::base(); +} +'''; + +fn CallCallBase() { Cpp.CallBase(); } + // CHECK:STDOUT: ; --- // CHECK:STDOUT: ; ModuleID = 'other.carbon' // CHECK:STDOUT: source_filename = "other.carbon" @@ -377,3 +393,75 @@ fn H() { Cpp.G2(); } // CHECK:STDOUT: !80 = !DILocation(line: 20, column: 10, scope: !79) // CHECK:STDOUT: !81 = !DILocation(line: 20, column: 1, scope: !79) // CHECK:STDOUT: +// CHECK:STDOUT: ; --- +// CHECK:STDOUT: ; ModuleID = 'keyword_name.carbon' +// CHECK:STDOUT: source_filename = "keyword_name.carbon" +// CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" +// CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu" +// CHECK:STDOUT: +// CHECK:STDOUT: $_Z8CallBasev = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_Cbase.Main() #0 !dbg !84 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: ret void, !dbg !85 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_Cbase__carbon_thunk.Main() #0 !dbg !86 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @_Cbase.Main(), !dbg !87 +// CHECK:STDOUT: ret void, !dbg !87 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define void @_CCallCallBase.Main() #0 !dbg !88 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @_Z8CallBasev(), !dbg !89 +// CHECK:STDOUT: ret void, !dbg !90 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: inlinehint mustprogress uwtable +// CHECK:STDOUT: define linkonce_odr dso_local void @_Z8CallBasev() #1 comdat { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @_ZN6CarbonL4baseEv() +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress nounwind uwtable +// CHECK:STDOUT: define internal void @_ZN6CarbonL4baseEv() #2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @_Cbase__carbon_thunk.Main() +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: attributes #0 = { nounwind } +// CHECK:STDOUT: attributes #1 = { inlinehint mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #2 = { alwaysinline mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: +// CHECK:STDOUT: !llvm.dbg.cu = !{!83} +// CHECK:STDOUT: !llvm.module.flags = !{!68, !69, !70, !2, !3} +// CHECK:STDOUT: !llvm.errno.tbaa = !{!72} +// CHECK:STDOUT: +// CHECK:STDOUT: !2 = !{i32 7, !"Dwarf Version", i32 5} +// CHECK:STDOUT: !3 = !{i32 2, !"Debug Info Version", i32 3} +// CHECK:STDOUT: !7 = !{null} +// CHECK:STDOUT: !8 = !DISubroutineType(types: !7) +// CHECK:STDOUT: !60 = !{!"Simple C++ TBAA"} +// CHECK:STDOUT: !61 = !{!"omnipotent char", !60, i64 0} +// CHECK:STDOUT: !64 = !{!"int", !61, i64 0} +// CHECK:STDOUT: !68 = !{i32 8, !"PIC Level", i32 2} +// CHECK:STDOUT: !69 = !{i32 7, !"PIE Level", i32 2} +// CHECK:STDOUT: !70 = !{i32 7, !"uwtable", i32 2} +// CHECK:STDOUT: !71 = !{!"__libc_errno", !64, i64 0} +// CHECK:STDOUT: !72 = !{!71, !64, i64 0} +// CHECK:STDOUT: !82 = !DIFile(filename: "keyword_name.carbon", directory: "") +// CHECK:STDOUT: !83 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !82, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) +// CHECK:STDOUT: !84 = distinct !DISubprogram(name: "base", linkageName: "_Cbase.Main", scope: null, file: !82, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !83) +// CHECK:STDOUT: !85 = !DILocation(line: 6, column: 1, scope: !84) +// CHECK:STDOUT: !86 = distinct !DISubprogram(name: "base__carbon_thunk", linkageName: "_Cbase__carbon_thunk.Main", scope: null, file: !82, line: 6, type: !8, spFlags: DISPFlagDefinition, unit: !83) +// CHECK:STDOUT: !87 = !DILocation(line: 6, column: 1, scope: !86) +// CHECK:STDOUT: !88 = distinct !DISubprogram(name: "CallCallBase", linkageName: "_CCallCallBase.Main", scope: null, file: !82, line: 14, type: !8, spFlags: DISPFlagDefinition, unit: !83) +// CHECK:STDOUT: !89 = !DILocation(line: 14, column: 21, scope: !88) +// CHECK:STDOUT: !90 = !DILocation(line: 14, column: 1, scope: !88) +// CHECK:STDOUT: