Skip to content

[cxx-interop] Emit IR for templated static members with an out-of-line definition #63058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,27 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {

ClangDeclFinder refFinder(callback);

auto &clangSema = Context.getClangModuleLoader()->getClangSema();

while (!stack.empty()) {
auto *next = const_cast<clang::Decl *>(stack.pop_back_val());

// If this is a static member of a class, it might be defined out of line.
// If the class is templated, the definition of its static member might be
// templated as well. If it is, instantiate it here.
if (auto var = dyn_cast<clang::VarDecl>(next)) {
if (var->isStaticDataMember() &&
var->getTemplateSpecializationKind() ==
clang::TemplateSpecializationKind::TSK_ImplicitInstantiation)
clangSema.InstantiateVariableDefinition(var->getLocation(), var);
}

// If a function calls another method in a class template specialization, we
// need to instantiate that other function. Do that here.
if (auto *fn = dyn_cast<clang::FunctionDecl>(next)) {
// Make sure that this method is part of a class template specialization.
if (fn->getTemplateInstantiationPattern())
Context.getClangModuleLoader()
->getClangSema()
.InstantiateFunctionDefinition(fn->getLocation(), fn);
clangSema.InstantiateFunctionDefinition(fn->getLocation(), fn);
}

if (clang::Decl *executableDecl = getDeclWithExecutableCode(next)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_WITH_STATIC_OUT_OF_LINE_MEMBER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_WITH_STATIC_OUT_OF_LINE_MEMBER_H

template <class T>
struct HasStaticOutOfLineMember {
static int values[];

static int getFirstValue() {
return values[0];
}
};

template <class T>
int HasStaticOutOfLineMember<T>::values[123];

typedef HasStaticOutOfLineMember<int> HasStaticOutOfLineMemberInt;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_WITH_STATIC_OUT_OF_LINE_MEMBER_H
5 changes: 5 additions & 0 deletions test/Interop/Cxx/templates/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module ClassTemplateWithPrimitiveArgument {
requires cplusplus
}

module ClassTemplateWithOutOfLineMember {
header "class-template-with-static-out-of-line-member.h"
requires cplusplus
}

module NotPreDefinedClassTemplate {
header "not-pre-defined-class-template.h"
requires cplusplus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop -disable-availability-checking | %FileCheck %s

import ClassTemplateWithOutOfLineMember

public func usesStaticMember() {
HasStaticOutOfLineMemberInt.getFirstValue()
}

// CHECK: @{{_ZN24HasStaticOutOfLineMemberIiE6valuesE|"\?values@\?\$HasStaticOutOfLineMember@H@@2PAHA"}} = linkonce_odr{{.*}} global {{.*}} zeroinitializer

// CHECK: define {{.*}}i32 @{{_ZN24HasStaticOutOfLineMemberIiE13getFirstValueEv|"\?getFirstValue@\?\$HasStaticOutOfLineMember@H@@SAHXZ"}}()
// CHECK: %0 = {{.*}} @{{_ZN24HasStaticOutOfLineMemberIiE6valuesE|"\?values@\?\$HasStaticOutOfLineMember@H@@2PAHA"}}