diff --git a/toolchain/check/cpp/export.cpp b/toolchain/check/cpp/export.cpp index 87dc23085f66..ae7298982999 100644 --- a/toolchain/check/cpp/export.cpp +++ b/toolchain/check/cpp/export.cpp @@ -116,8 +116,7 @@ auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id, } auto ExportClassToCpp(Context& context, SemIR::LocId loc_id, - SemIR::InstId class_inst_id, SemIR::ClassType class_type) - -> clang::TagDecl* { + SemIR::ClassType class_type) -> clang::TagDecl* { // TODO: A lot of logic in this function is shared with ExportNameScopeToCpp. // This should be refactored. @@ -131,9 +130,11 @@ auto ExportClassToCpp(Context& context, SemIR::LocId loc_id, // If this class was produced by importing a C++ declaration or has // already been exported to C++, return the corresponding Clang declaration. // That could either be a CXXRecordDecl or an EnumDecl. - if (auto* decl_context = - GetClangDeclContextForScope(context, class_info.scope_id)) { - return cast(decl_context); + if (auto clang_decl_id = + context.clang_decls().Lookup(class_info.first_decl_id()); + clang_decl_id.has_value()) { + return cast( + context.clang_decls().Get(clang_decl_id).key.decl); } auto* identifier_info = GetClangIdentifierInfo(context, class_info.name_id); @@ -157,8 +158,8 @@ auto ExportClassToCpp(Context& context, SemIR::LocId loc_id, auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(cast(record_decl)); - auto clang_decl_id = - context.clang_decls().Add({.key = key, .inst_id = class_inst_id}); + auto clang_decl_id = context.clang_decls().Add( + {.key = key, .inst_id = class_info.first_decl_id()}); if (class_info.scope_id.has_value()) { // TODO: Record the Carbon class -> clang declaration mapping for incomplete // classes too. diff --git a/toolchain/check/cpp/export.h b/toolchain/check/cpp/export.h index cb29a4745960..7de607c6ec65 100644 --- a/toolchain/check/cpp/export.h +++ b/toolchain/check/cpp/export.h @@ -28,8 +28,7 @@ auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id, // Otherwise, creates a new C++ class and returns it. Returns nullptr if the // class could not be exported and an error was diagnosed. auto ExportClassToCpp(Context& context, SemIR::LocId loc_id, - SemIR::InstId class_inst_id, SemIR::ClassType class_type) - -> clang::TagDecl*; + SemIR::ClassType class_type) -> clang::TagDecl*; // Export all `SemIR::FieldDecl`s in the class body as `clang::FieldDecl`s. auto ExportAllFieldsToCpp(Context& context, SemIR::Class& class_info) -> void; diff --git a/toolchain/check/cpp/type_mapping.cpp b/toolchain/check/cpp/type_mapping.cpp index 2e6db1a6390b..0ddfd9c84ef7 100644 --- a/toolchain/check/cpp/type_mapping.cpp +++ b/toolchain/check/cpp/type_mapping.cpp @@ -226,8 +226,8 @@ static auto TryMapClassType(Context& context, SemIR::TypeInstId class_inst_id, } // Otherwise, find the existing C++ declaration or create a new one. - auto* tag_decl = ExportClassToCpp(context, SemIR::LocId(class_inst_id), - class_inst_id, class_type); + auto* tag_decl = + ExportClassToCpp(context, SemIR::LocId(class_inst_id), class_type); if (!tag_decl) { return clang::QualType(); } diff --git a/toolchain/check/testdata/interop/cpp/class/roundtrip.carbon b/toolchain/check/testdata/interop/cpp/class/roundtrip.carbon index db9166c3b794..8e9a54326fdc 100644 --- a/toolchain/check/testdata/interop/cpp/class/roundtrip.carbon +++ b/toolchain/check/testdata/interop/cpp/class/roundtrip.carbon @@ -41,3 +41,19 @@ auto F(C *p) -> Carbon::CAlias* { return p; } '''; + +// --- import.carbon + +library "[[@TEST_NAME]]"; + +import Cpp; + +class Class1; + +inline Cpp ''' +void func(Carbon::Class1* _Nonnull); +'''; + +fn Caller(p: Class1*) { + Cpp.func(p); +}