From 54728e47fe4b4526335803f4be809b1cbba00315 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Thu, 12 Nov 2026 23:12:38 +0000 Subject: [PATCH] Handle ParameterizedClassType in ExportNameScopeToCpp (#8123) This is a step towards being able to export methods of parameterized classes. When exporting such a method, the decl context should be a `ClassTemplateSpecializationDecl`. Thread the scope's specific ID down from `MapInstIdToClangDeclOrType` and call `ExportClassSpecificToCpp` to create this context. Also add a `context.TODO` to prevent too much code from being exercised with methods of parameterized classes after getting the parent scope, as that would lead to various test crashes. The user-visible effect of this change is that the somewhat mysterious "TODO: non-class non-namespace name scope" error is replaced with "TODO: support exporting member functions of parameterized classes". --------- Co-authored-by: Geoff Romer --- toolchain/check/cpp/export.cpp | 529 ++++++++++-------- toolchain/check/cpp/export.h | 10 +- toolchain/check/cpp/generate_ast.cpp | 3 +- .../interop/cpp/class/export/access.carbon | 13 +- .../interop/cpp/class/export/generic.carbon | 9 +- .../cpp/function/export/constructor.carbon | 7 - .../cpp/function/export/generic.carbon | 9 +- 7 files changed, 322 insertions(+), 258 deletions(-) diff --git a/toolchain/check/cpp/export.cpp b/toolchain/check/cpp/export.cpp index c099d611dfb8..c29526168c09 100644 --- a/toolchain/check/cpp/export.cpp +++ b/toolchain/check/cpp/export.cpp @@ -194,96 +194,6 @@ auto ExportLookupNamespaceToCpp(Context& context, SemIR::LocId loc_id, return decl_context; } -auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id, - SemIR::NameScopeId name_scope_id) - -> clang::DeclContext* { - llvm::SmallVector name_scope_ids_to_create; - - // Walk through the parent scopes, looking for one that's already mapped into - // C++. We already mapped the package scope to ::Carbon::, so we - // must find one. - clang::DeclContext* decl_context = nullptr; - while (true) { - // If this name scope was produced by importing a C++ declaration or has - // already been exported to C++, return the corresponding Clang declaration. - if (auto* existing_decl_context = - GetClangDeclContextForScope(context, name_scope_id)) { - decl_context = existing_decl_context; - break; - } - - // Otherwise, continue to the parent and create a scope for it first. - name_scope_ids_to_create.push_back(name_scope_id); - const auto& name_scope = context.name_scopes().Get(name_scope_id); - if (name_scope.is_imported_package()) { - // Imported packages are exported as sub-namespaces of `::Carbon`, not of - // the current package's sub-namespace. - decl_context = context.cpp_context()->domain().carbon_namespace(); - break; - } - name_scope_id = name_scope.parent_scope_id(); - - // TODO: What should happen if there's an intervening function scope? - CARBON_CHECK( - name_scope_id.has_value(), - "Reached the top level without finding a scope mapped into C++"); - } - - // Create the name scopes in order, starting from the outermost one. - while (!name_scope_ids_to_create.empty()) { - name_scope_id = name_scope_ids_to_create.pop_back_val(); - - auto& name_scope = context.name_scopes().Get(name_scope_id); - - auto const_inst_id = - context.constant_values().GetConstantInstId(name_scope.inst_id()); - if (context.insts().Is(const_inst_id)) { - auto* identifier_info = - GetClangIdentifierInfo(context, name_scope.name_id()); - if (!identifier_info) { - // TODO: Handle keyword package names like `Cpp` and `Core`. These can - // be named from C++ via an alias. - context.TODO(loc_id, "interop with non-identifier package name"); - return nullptr; - } - - decl_context = - ExportNamespaceToCpp(context, decl_context, identifier_info); - } else if (auto class_type = - context.insts().TryGetAs(const_inst_id)) { - const auto& class_info = context.classes().Get(class_type->class_id); - decl_context = ExportClassToCppInDeclContext( - context, decl_context, class_info, class_type->specific_id); - } else { - context.TODO(loc_id, "non-class non-namespace name scope"); - return nullptr; - } - - auto key = SemIR::ClangDeclKey::ForNonFunctionDecl( - cast(decl_context)); - auto clang_decl_id = context.clang_decls().Add( - {.key = key, .inst_id = name_scope.inst_id()}); - name_scope.set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false); - - // Complete the type here to avoid hitting a clang assert later when - // adding methods. - if (auto* record_decl = - llvm::dyn_cast(decl_context)) { - context.ast_context().getExternalSource()->CompleteType(record_decl); - } - } - - if (auto* record_decl = llvm::dyn_cast(decl_context)) { - // We can't use a class as a name scope until it's complete, so we need to - // ensure it's complete here. - context.clang_sema().RequireCompleteType( - GetCppLocation(context, loc_id), - context.ast_context().getCanonicalTagType(record_decl), - clang::diag::err_incomplete_member_access); - } - return decl_context; -} - static auto GetClassTypeInstId(Context& context, SemIR::ClassId class_id, SemIR::SpecificId specific_id) -> SemIR::TypeInstId { @@ -322,113 +232,6 @@ static auto CreateClassTemplateSpecializationDecl( return class_template_specialization_decl; } -// Exports a specific Carbon class into C++ as a template class specialization. -// -// If the specific class has already been exported, returns the existing C++ -// decl. Otherwise, creates a new C++ class template specialization and -// returns it. Returns nullptr if the class could not be exported and an error -// was diagnosed. -static auto ExportClassSpecificToCpp(Context& context, SemIR::LocId loc_id, - SemIR::ClassType class_type) - -> clang::ClassTemplateSpecializationDecl* { - CARBON_CHECK(class_type.specific_id.has_value()); - - // Use existing export if possible. - auto class_type_inst_id = - GetClassTypeInstId(context, class_type.class_id, class_type.specific_id); - if (const auto* clang_decl = - context.clang_decls().Lookup(class_type_inst_id)) { - return cast(clang_decl->decl()); - } - - // Ensure the parameterized class is exported, and get its - // `ClassTemplateDecl`. - auto param_class_type_id = GetParameterizedClassType( - context, class_type.class_id, SemIR::SpecificId::None); - auto param_class_type = - context.types().GetAs(param_class_type_id); - auto* class_template_decl = - ExportParameterizedClassToCpp(context, param_class_type); - if (!class_template_decl) { - return nullptr; - } - - llvm::SmallVector template_args; - const auto specific = context.specifics().Get(class_type.specific_id); - auto specific_args = context.inst_blocks().Get(specific.args_id); - for (auto specific_arg_inst_id : specific_args) { - // TODO: also handle non-type args. Such args can't happen here yet, since - // the `ExportGenericClassToCpp` call above already checks for them. - - // Extract the access type from constrained types. - SemIR::TypeId specific_arg_type_id = SemIR::TypeId::None; - if (context.types().IsConstrainedFacetType( - context.insts().GetAttachedType(specific_arg_inst_id))) { - specific_arg_type_id = GetFacetAccessType(context, specific_arg_inst_id); - } else { - specific_arg_type_id = - context.types().GetTypeIdForTypeInstId(specific_arg_inst_id); - } - - auto cpp_type = MapToCppType(context, specific_arg_type_id); - if (cpp_type.isNull()) { - context.TODO(loc_id, "failed to map specific type arg to C++"); - return nullptr; - } - - template_args.push_back(cpp_type); - } - - return CreateClassTemplateSpecializationDecl( - context, class_template_decl, template_args, class_type_inst_id); -} - -auto ExportClassToCpp(Context& context, SemIR::ClassType class_type) - -> clang::TagDecl* { - const auto& class_info = context.classes().Get(class_type.class_id); - SemIR::LocId loc_id(class_info.first_decl_id()); - - if (class_type.specific_id.has_value()) { - return ExportClassSpecificToCpp(context, loc_id, class_type); - } - - // 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 (const auto* clang_decl = - context.clang_decls().Lookup(class_info.first_decl_id())) { - return cast(clang_decl->decl()); - } - - auto* decl_context = - ExportNameScopeToCpp(context, loc_id, class_info.parent_scope_id); - auto* record_decl = ExportClassToCppInDeclContext( - context, decl_context, class_info, class_type.specific_id); - - auto key = - SemIR::ClangDeclKey::ForNonFunctionDecl(cast(record_decl)); - 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. - context.name_scopes() - .Get(class_info.scope_id) - .set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false); - } - return record_decl; -} - -auto ExportAndCompleteClassToCpp(Context& context, SemIR::ClassType class_type) - -> clang::TagDecl* { - auto* tag_decl = ExportClassToCpp(context, class_type); - if (tag_decl && context.cpp_context() && - context.ast_context().getExternalSource()) { - context.ast_context().getExternalSource()->CompleteType(tag_decl); - } - return tag_decl; -} - // Export the bindings in a generic as a `clang::TemplateParameterList`. static auto ExportGenericBindings(Context& context, SemIR::LocId loc_id, SemIR::GenericId generic_id, @@ -484,6 +287,280 @@ static auto ExportGenericBindings(Context& context, SemIR::LocId loc_id, /*RequiresClause=*/nullptr); } +static auto ExportParameterizedClassToCppInDeclContext( + Context& context, clang::DeclContext* decl_context, SemIR::ClassId class_id) + -> clang::ClassTemplateDecl* { + // Use existing export if possible. + const auto& class_info = context.classes().Get(class_id); + auto decl_id = class_info.first_decl_id(); + if (const auto* clang_decl = context.clang_decls().Lookup(decl_id)) { + return cast(clang_decl->decl()); + } + + SemIR::LocId loc_id(decl_id); + auto* template_param_list = ExportGenericBindings( + context, loc_id, class_info.generic_id, decl_context); + if (!template_param_list) { + return nullptr; + } + + auto clang_loc = GetCppLocation(context, loc_id); + auto* record_decl = ExportClassToCppInDeclContext( + context, decl_context, class_info, SemIR::SpecificId::None); + auto* class_template_decl = clang::ClassTemplateDecl::Create( + context.ast_context(), decl_context, + /*L=*/clang_loc, record_decl->getDeclName(), template_param_list, + record_decl); + + auto key = SemIR::ClangDeclKey::ForNonFunctionDecl( + cast(class_template_decl)); + context.clang_decls().Add({.key = key, .inst_id = decl_id}); + + return class_template_decl; +} + +// Look up the `ClassTemplateSpecializationDecl` corresponding to `class_type`, +// which must be a specific of a parameterized class. Returns nullptr if this +// class specific has not been exported. +static auto GetExistingExportedClassSpecific(Context& context, + SemIR::ClassType class_type) + -> clang::ClassTemplateSpecializationDecl* { + auto class_type_inst_id = + GetClassTypeInstId(context, class_type.class_id, class_type.specific_id); + if (const auto* clang_decl = + context.clang_decls().Lookup(class_type_inst_id)) { + return cast(clang_decl->decl()); + } + return nullptr; +} + +// Exports a specific Carbon class into C++ as a template class specialization. +// +// If the specific class has already been exported, returns the existing C++ +// decl. Otherwise, creates a new C++ class template specialization and +// returns it. Returns nullptr if the class could not be exported and an error +// was diagnosed. +static auto ExportClassSpecificToCpp( + Context& context, clang::ClassTemplateDecl* class_template_decl, + SemIR::LocId loc_id, SemIR::ClassType class_type) + -> clang::ClassTemplateSpecializationDecl* { + CARBON_CHECK(class_type.specific_id.has_value()); + + // Use existing export if possible. + if (auto* decl = GetExistingExportedClassSpecific(context, class_type)) { + return decl; + } + + llvm::SmallVector template_args; + const auto specific = context.specifics().Get(class_type.specific_id); + auto specific_args = context.inst_blocks().Get(specific.args_id); + for (auto specific_arg_inst_id : specific_args) { + // TODO: also handle non-type args. Such args can't happen here yet, since + // the `ExportGenericClassToCpp` call above already checks for them. + + // Extract the access type from constrained types. + SemIR::TypeId specific_arg_type_id = SemIR::TypeId::None; + if (context.types().IsConstrainedFacetType( + context.insts().GetAttachedType(specific_arg_inst_id))) { + specific_arg_type_id = GetFacetAccessType(context, specific_arg_inst_id); + } else { + specific_arg_type_id = + context.types().GetTypeIdForTypeInstId(specific_arg_inst_id); + } + + auto cpp_type = MapToCppType(context, specific_arg_type_id); + if (cpp_type.isNull()) { + context.TODO(loc_id, "failed to map specific type arg to C++"); + return nullptr; + } + + template_args.push_back(cpp_type); + } + + auto class_type_inst_id = + GetClassTypeInstId(context, class_type.class_id, class_type.specific_id); + return CreateClassTemplateSpecializationDecl( + context, class_template_decl, template_args, class_type_inst_id); +} + +auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id, + SemIR::NameScopeId name_scope_id, + SemIR::SpecificId scope_specific_id) + -> clang::DeclContext* { + llvm::SmallVector name_scope_ids_to_create; + + // Walk through the parent scopes, looking for one that's already mapped into + // C++. We already mapped the package scope to ::Carbon::, so we + // must find one. + clang::DeclContext* decl_context = nullptr; + while (true) { + // If this name scope was produced by importing a C++ declaration or has + // already been exported to C++, return the corresponding Clang declaration. + if (auto* existing_decl_context = + GetClangDeclContextForScope(context, name_scope_id)) { + decl_context = existing_decl_context; + break; + } + + // Otherwise, continue to the parent and create a scope for it first. + name_scope_ids_to_create.push_back(name_scope_id); + const auto& name_scope = context.name_scopes().Get(name_scope_id); + if (name_scope.is_imported_package()) { + // Imported packages are exported as sub-namespaces of `::Carbon`, not of + // the current package's sub-namespace. + decl_context = context.cpp_context()->domain().carbon_namespace(); + break; + } + name_scope_id = name_scope.parent_scope_id(); + + // TODO: What should happen if there's an intervening function scope? + CARBON_CHECK( + name_scope_id.has_value(), + "Reached the top level without finding a scope mapped into C++"); + } + + // Create the name scopes in order, starting from the outermost one. + while (!name_scope_ids_to_create.empty()) { + name_scope_id = name_scope_ids_to_create.pop_back_val(); + + auto& name_scope = context.name_scopes().Get(name_scope_id); + + auto inst_id = name_scope.inst_id(); + auto const_inst_id = context.constant_values().GetConstantInstId(inst_id); + auto type_inst_id = + context.types().GetTypeInstId(context.insts().Get(inst_id).type_id()); + if (context.insts().Is(const_inst_id)) { + auto* identifier_info = + GetClangIdentifierInfo(context, name_scope.name_id()); + if (!identifier_info) { + // TODO: Handle keyword package names like `Cpp` and `Core`. These can + // be named from C++ via an alias. + context.TODO(loc_id, "interop with non-identifier package name"); + return nullptr; + } + + decl_context = + ExportNamespaceToCpp(context, decl_context, identifier_info); + } else if (auto class_type = + context.insts().TryGetAs(const_inst_id)) { + const auto& class_info = context.classes().Get(class_type->class_id); + decl_context = ExportClassToCppInDeclContext( + context, decl_context, class_info, class_type->specific_id); + } else if (auto param_class_type = + context.insts().TryGetAs( + type_inst_id)) { + // Require a specific so that the specialized class can be used for the + // decl context. The unspecialized template class is only a declaration, + // not a definition, and can't be used as a decl context. + CARBON_CHECK(scope_specific_id.has_value()); + + SemIR::ClassType class_type{ + .type_id = SemIR::TypeType::TypeId, + .class_id = param_class_type->class_id, + .specific_id = scope_specific_id, + }; + decl_context = GetExistingExportedClassSpecific(context, class_type); + if (!decl_context) { + auto* class_template_decl = ExportParameterizedClassToCppInDeclContext( + context, decl_context, class_type.class_id); + if (!class_template_decl) { + return nullptr; + } + decl_context = ExportClassSpecificToCpp(context, class_template_decl, + loc_id, class_type); + } + if (!decl_context) { + return nullptr; + } + } else { + context.TODO(loc_id, "non-class non-namespace name scope"); + return nullptr; + } + + auto key = SemIR::ClangDeclKey::ForNonFunctionDecl( + cast(decl_context)); + auto clang_decl_id = context.clang_decls().Add( + {.key = key, .inst_id = name_scope.inst_id()}); + name_scope.set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false); + + // Complete the type here to avoid hitting a clang assert later when + // adding methods. + if (auto* record_decl = + llvm::dyn_cast(decl_context)) { + if (!record_decl->isCompleteDefinition()) { + context.ast_context().getExternalSource()->CompleteType(record_decl); + } + } + } + + if (auto* record_decl = llvm::dyn_cast(decl_context)) { + // We can't use a class as a name scope until it's complete, so we need to + // ensure it's complete here. + context.clang_sema().RequireCompleteType( + GetCppLocation(context, loc_id), + context.ast_context().getCanonicalTagType(record_decl), + clang::diag::err_incomplete_member_access); + } + return decl_context; +} + +auto ExportClassToCpp(Context& context, SemIR::ClassType class_type) + -> clang::TagDecl* { + const auto& class_info = context.classes().Get(class_type.class_id); + SemIR::LocId loc_id(class_info.first_decl_id()); + + if (class_type.specific_id.has_value()) { + SemIR::ParameterizedClassType param_class_type{ + .type_id = SemIR::TypeType::TypeId, + .class_id = class_type.class_id, + .enclosing_specific_id = SemIR::SpecificId::None, + }; + auto* class_template_decl = + ExportParameterizedClassToCpp(context, param_class_type); + if (!class_template_decl) { + return nullptr; + } + return ExportClassSpecificToCpp(context, class_template_decl, loc_id, + class_type); + } + + // 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 (const auto* clang_decl = + context.clang_decls().Lookup(class_info.first_decl_id())) { + return cast(clang_decl->decl()); + } + + auto* decl_context = + ExportNameScopeToCpp(context, loc_id, class_info.parent_scope_id); + auto* record_decl = ExportClassToCppInDeclContext( + context, decl_context, class_info, class_type.specific_id); + + auto key = + SemIR::ClangDeclKey::ForNonFunctionDecl(cast(record_decl)); + 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. + context.name_scopes() + .Get(class_info.scope_id) + .set_clang_decl_context_id(clang_decl_id, /*is_cpp_scope=*/false); + } + return record_decl; +} + +auto ExportAndCompleteClassToCpp(Context& context, SemIR::ClassType class_type) + -> clang::TagDecl* { + auto* tag_decl = ExportClassToCpp(context, class_type); + if (tag_decl && context.cpp_context() && + context.ast_context().getExternalSource()) { + context.ast_context().getExternalSource()->CompleteType(tag_decl); + } + return tag_decl; +} + /// Create a Specific for the given generic using the given template args. /// /// Returns `SemIR::SpecificId::None` if an error occurs. @@ -525,40 +602,17 @@ static auto MakeSpecificForTemplateArgs( auto ExportParameterizedClassToCpp( Context& context, SemIR::ParameterizedClassType param_class_type) -> clang::ClassTemplateDecl* { - // Use existing export if possible. - const auto& class_info = context.classes().Get(param_class_type.class_id); - auto decl_id = class_info.first_decl_id(); - if (const auto* clang_decl = context.clang_decls().Lookup(decl_id)) { - return cast(clang_decl->decl()); - } - // Map the parent scope into the C++ AST. - SemIR::LocId loc_id(decl_id); + const auto& class_info = context.classes().Get(param_class_type.class_id); + SemIR::LocId loc_id(class_info.first_decl_id()); auto* decl_context = ExportNameScopeToCpp(context, loc_id, class_info.parent_scope_id); if (!decl_context) { return nullptr; } - auto* template_param_list = ExportGenericBindings( - context, loc_id, class_info.generic_id, decl_context); - if (!template_param_list) { - return nullptr; - } - - auto clang_loc = GetCppLocation(context, loc_id); - auto* record_decl = ExportClassToCppInDeclContext( - context, decl_context, class_info, SemIR::SpecificId::None); - auto* class_template_decl = clang::ClassTemplateDecl::Create( - context.ast_context(), decl_context, - /*L=*/clang_loc, record_decl->getDeclName(), template_param_list, - record_decl); - - auto key = SemIR::ClangDeclKey::ForNonFunctionDecl( - cast(class_template_decl)); - context.clang_decls().Add({.key = key, .inst_id = decl_id}); - - return class_template_decl; + return ExportParameterizedClassToCppInDeclContext(context, decl_context, + param_class_type.class_id); } auto ExportClassSpecializationToCpp( @@ -908,13 +962,14 @@ static auto MapToCppThunkParamType(Context& context, SemIR::TypeId type_id) // name scope if necessary. Returns `nullopt` if an error was diagnosed. static auto BuildFunctionInfo(Context& context, SemIR::LocId loc_id, SemIR::FunctionId callee_function_id, - SemIR::SpecificId callee_specific_id) + SemIR::SpecificId callee_specific_id, + SemIR::SpecificId scope_specific_id) -> std::optional { const SemIR::Function& callee = context.functions().Get(callee_function_id); // Map the parent scope into the C++ AST. - auto* decl_context = - ExportNameScopeToCpp(context, loc_id, callee.parent_scope_id); + auto* decl_context = ExportNameScopeToCpp( + context, loc_id, callee.parent_scope_id, scope_specific_id); if (!decl_context) { return std::nullopt; } @@ -1869,7 +1924,8 @@ static auto ExportGenericFunctionToCpp(Context& context, SemIR::LocId loc_id, // Exports the given function to C++ and returns the exported `NamedDecl`, // or `nullptr` if an error was diagnosed. static auto ExportFunctionToCpp(Context& context, SemIR::LocId loc_id, - SemIR::FunctionId callee_function_id) + SemIR::FunctionId callee_function_id, + SemIR::SpecificId scope_specific_id) -> clang::NamedDecl* { if (context.functions() .Get(callee_function_id) @@ -1879,12 +1935,19 @@ static auto ExportFunctionToCpp(Context& context, SemIR::LocId loc_id, return nullptr; } - auto target = BuildFunctionInfo(context, loc_id, callee_function_id, - SemIR::SpecificId::None); + auto target = BuildFunctionInfo( + context, loc_id, callee_function_id, + /*callee_specific_id=*/SemIR::SpecificId::None, scope_specific_id); if (!target) { return nullptr; } + if (scope_specific_id.has_value()) { + context.TODO(loc_id, + "support exporting member functions of parameterized classes"); + return nullptr; + } + if (target->function.generic_id.has_value()) { if (target->export_as_constructor || target->self_param.has_value()) { context.TODO(loc_id, "support exporting generic member functions"); @@ -1897,7 +1960,8 @@ static auto ExportFunctionToCpp(Context& context, SemIR::LocId loc_id, } auto GetOrExportFunctionToCpp(Context& context, SemIR::LocId loc_id, - SemIR::FunctionId function_id) + SemIR::FunctionId function_id, + SemIR::SpecificId scope_specific_id) -> clang::NamedDecl* { SemIR::Function& function = context.functions().Get(function_id); if (auto clang_decl_id = @@ -1907,7 +1971,8 @@ auto GetOrExportFunctionToCpp(Context& context, SemIR::LocId loc_id, context.clang_decls().Get(clang_decl_id).decl()); } - auto* named_decl = ExportFunctionToCpp(context, loc_id, function_id); + auto* named_decl = + ExportFunctionToCpp(context, loc_id, function_id, scope_specific_id); if (!named_decl) { return nullptr; } diff --git a/toolchain/check/cpp/export.h b/toolchain/check/cpp/export.h index b1edbc54c9fb..8285ff3ac52b 100644 --- a/toolchain/check/cpp/export.h +++ b/toolchain/check/cpp/export.h @@ -50,8 +50,9 @@ auto ExportLookupNamespaceToCpp(Context& context, SemIR::LocId loc_id, // If the name scope has already been exported, returns the existing context. // Otherwise, creates a new C++ declaration context and returns it. Returns // nullptr if the name scope could not be exported and an error was diagnosed. -auto ExportNameScopeToCpp(Context& context, SemIR::LocId loc_id, - SemIR::NameScopeId name_scope_id) +auto ExportNameScopeToCpp( + Context& context, SemIR::LocId loc_id, SemIR::NameScopeId name_scope_id, + SemIR::SpecificId scope_specific_id = SemIR::SpecificId::None) -> clang::DeclContext*; // Exports a Carbon class into C++ as a class, or returns the C++ tag type that @@ -107,8 +108,9 @@ auto ExportFieldToCpp(Context& context, SemIR::InstId field_inst_id, // Returns the `ClangDeclId` of a `clang::FunctionDecl` or // `clang::FunctionTemplateDecl` that can be used to call the given function. // Returns null if an error was diagnosed. -auto GetOrExportFunctionToCpp(Context& context, SemIR::LocId loc_id, - SemIR::FunctionId function_id) +auto GetOrExportFunctionToCpp( + Context& context, SemIR::LocId loc_id, SemIR::FunctionId function_id, + SemIR::SpecificId scope_specific_id = SemIR::SpecificId::None) -> clang::NamedDecl*; // Exports the necessary declarations to permit conversion from the given diff --git a/toolchain/check/cpp/generate_ast.cpp b/toolchain/check/cpp/generate_ast.cpp index 4cbd02891250..1a9f46962bdd 100644 --- a/toolchain/check/cpp/generate_ast.cpp +++ b/toolchain/check/cpp/generate_ast.cpp @@ -234,7 +234,8 @@ auto CarbonExternalASTSource::MapInstIdToClangDeclOrType(LookupResult lookup) auto callee = GetCallee(context_->sem_ir(), target_inst_id); if (auto* callee_function = std::get_if(&callee)) { return GetOrExportFunctionToCpp(*context_, SemIR::LocId(target_inst_id), - callee_function->function_id); + callee_function->function_id, + lookup.specific_id); } else if (auto param_class_type = context_->insts().TryGetAs( type_inst_id)) { diff --git a/toolchain/check/testdata/interop/cpp/class/export/access.carbon b/toolchain/check/testdata/interop/cpp/class/export/access.carbon index caa10c7024fb..7493d4bc1585 100644 --- a/toolchain/check/testdata/interop/cpp/class/export/access.carbon +++ b/toolchain/check/testdata/interop/cpp/class/export/access.carbon @@ -120,14 +120,15 @@ class A {} class B(T: type) { // TODO: exporting of methods in generics is not yet supported. - // CHECK:STDERR: ╭─┤ fail_todo_access_exported_private_method_in_generic.carbon:[[@LINE+7]]:3 - // CHECK:STDERR: -> │ private fn F() {} - // CHECK:STDERR: · ━━━━━━━━┳━━━━━━━ - // CHECK:STDERR: · ┗━━┫ error: semantics TODO: `non-class non-namespace name scope` - // CHECK:STDERR: · ┃ [SemanticsTodo] + // CHECK:STDERR: ╭─┤ fail_todo_access_exported_private_method_in_generic.carbon:[[@LINE+8]]:3 + // CHECK:STDERR: -> │ private fn F(unused self) {} + // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━ + // CHECK:STDERR: · ┗━━┫ error: semantics TODO: + // CHECK:STDERR: · ┃ `support exporting member functions of parameterized classes` + // CHECK:STDERR: · ┃ [SemanticsTodo] // CHECK:STDERR: ╰──── // CHECK:STDERR: - private fn F() {} + private fn F(unused self) {} } fn MakeB() -> B(A) { diff --git a/toolchain/check/testdata/interop/cpp/class/export/generic.carbon b/toolchain/check/testdata/interop/cpp/class/export/generic.carbon index 313d3db568e1..327df09c2198 100644 --- a/toolchain/check/testdata/interop/cpp/class/export/generic.carbon +++ b/toolchain/check/testdata/interop/cpp/class/export/generic.carbon @@ -83,11 +83,12 @@ library "[[@TEST_NAME]]"; import Cpp; class ClassWithMethod(T: type) { - // CHECK:STDERR: ╭─┤ fail_todo_generic_class_method_call.carbon:[[@LINE+7]]:3 + // CHECK:STDERR: ╭─┤ fail_todo_generic_class_method_call.carbon:[[@LINE+8]]:3 // CHECK:STDERR: -> │ fn M(unused ref self) {} - // CHECK:STDERR: · ━━━━━━━━━━━┳━━━━━━━━━━━ - // CHECK:STDERR: · ┗━━┫ error: semantics TODO: `non-class non-namespace name scope` - // CHECK:STDERR: · ┃ [SemanticsTodo] + // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━ + // CHECK:STDERR: · ┗━━┫ error: semantics TODO: + // CHECK:STDERR: · ┃ `support exporting member functions of parameterized classes` + // CHECK:STDERR: · ┃ [SemanticsTodo] // CHECK:STDERR: ╰──── // CHECK:STDERR: fn M(unused ref self) {} diff --git a/toolchain/check/testdata/interop/cpp/function/export/constructor.carbon b/toolchain/check/testdata/interop/cpp/function/export/constructor.carbon index dbae6bd9f8c6..8efe4102a712 100644 --- a/toolchain/check/testdata/interop/cpp/function/export/constructor.carbon +++ b/toolchain/check/testdata/interop/cpp/function/export/constructor.carbon @@ -205,13 +205,6 @@ library "[[@TEST_NAME]]"; import Cpp; class GenericClass(T: type) { - // CHECK:STDERR: ╭─┤ fail_todo_call_generic_class_constructor.carbon:[[@LINE+7]]:3 - // CHECK:STDERR: -> │ fn GenericClass(_: T) -> Self { - // CHECK:STDERR: · ━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━ - // CHECK:STDERR: · ┗━━┫ error: semantics TODO: `non-class non-namespace name scope` - // CHECK:STDERR: · ┃ [SemanticsTodo] - // CHECK:STDERR: ╰──── - // CHECK:STDERR: fn GenericClass(_: T) -> Self { return {}; } diff --git a/toolchain/check/testdata/interop/cpp/function/export/generic.carbon b/toolchain/check/testdata/interop/cpp/function/export/generic.carbon index f2c99edbe286..64bafc2099e4 100644 --- a/toolchain/check/testdata/interop/cpp/function/export/generic.carbon +++ b/toolchain/check/testdata/interop/cpp/function/export/generic.carbon @@ -122,11 +122,12 @@ library "[[@TEST_NAME]]"; import Cpp; class EnclosingA(T: type) { - // CHECK:STDERR: ╭─┤ fail_todo_enclosing_generic.carbon:[[@LINE+7]]:3 + // CHECK:STDERR: ╭─┤ fail_todo_enclosing_generic.carbon:[[@LINE+8]]:3 // CHECK:STDERR: -> │ fn MemberF[U: type](x: T, y: U); - // CHECK:STDERR: · ━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━ - // CHECK:STDERR: · ┗━━┫ error: semantics TODO: `non-class non-namespace name scope` - // CHECK:STDERR: · ┃ [SemanticsTodo] + // CHECK:STDERR: · ┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + // CHECK:STDERR: · ┗━━┫ error: semantics TODO: + // CHECK:STDERR: · ┃ `support exporting member functions of parameterized classes` + // CHECK:STDERR: · ┃ [SemanticsTodo] // CHECK:STDERR: ╰──── // CHECK:STDERR: fn MemberF[U: type](x: T, y: U);