Skip to content

[cxx-interop] Support generic types with isValueType trait #75396

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
Jul 24, 2024
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
10 changes: 5 additions & 5 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,14 @@ void ClangValueTypePrinter::printTypeGenericTraits(
os << "> = true;\n";
}

if (!isa<ClassDecl>(typeDecl) && !typeDecl->hasClangNode() &&
typeMetadataFuncRequirements.empty()) {
// FIXME: generic support.
os << "template<>\n";
if (!isa<ClassDecl>(typeDecl) && !typeDecl->hasClangNode()) {
assert(NTD && "not a nominal type?");
if (printer.printNominalTypeOutsideMemberDeclTemplateSpecifiers(NTD))
os << "template<>\n";
os << "static inline const constexpr bool isValueType<";
printer.printBaseName(typeDecl->getModuleContext());
os << "::";
printer.printBaseName(typeDecl);
printer.printNominalTypeReference(NTD, moduleContext);
os << "> = true;\n";
}
if (isOpaqueLayout) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -typecheck %t/generics.swift -typecheck -module-name Generics -enable-experimental-cxx-interop -emit-clang-header-path %t/Generics-Swift.h

// RUN: %target-interop-build-clangxx -fno-exceptions -std=gnu++20 -c %t/generics.cpp -I %t -o %t/generics.o
// RUN: %target-build-swift %t/generics.swift -o %t/generics -Xlinker %t/generics.o -module-name Generics -Xfrontend -entry-point-function-name -Xfrontend swiftMain

//--- generics.swift

public struct MyClass<T> {
public var a: T
public init(_ p: T) { self.a = p }
}

public func genericFunc<T>(_ p: T) -> T {
return p
}

//--- generics.cpp

#include "Generics-Swift.h"
using namespace Generics;

int main() {
auto c = MyClass<int>::init(10);
auto result = genericFunc<MyClass<int>>(c);
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ public func inoutConcretePair(_ x: UInt16, _ y: inout GenericPair<UInt16, UInt16
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
// CHECK-NEXT: #endif // __cpp_concepts
// CHECK-NEXT: static inline const constexpr bool isValueType<Generics::GenericPair<T_0_0, T_0_1>> = true;
// CHECK-NEXT: template<class T_0_0, class T_0_1>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
// CHECK-NEXT: #endif // __cpp_concepts
// CHECK-NEXT: static inline const constexpr bool isOpaqueLayout<Generics::GenericPair<T_0_0, T_0_1>> = true;
// CHECK-NEXT: template<class T_0_0, class T_0_1>
// CHECK-NEXT: #ifdef __cpp_concepts
Expand Down
1 change: 1 addition & 0 deletions test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>
// CHECK-NEXT: #endif
Expand Down