Skip to content

Commit

Permalink
Merge pull request #75396 from swiftlang/gaborh/value-type-trait
Browse files Browse the repository at this point in the history
[cxx-interop] Support generic types with isValueType trait
  • Loading branch information
Xazax-hun authored Jul 24, 2024
2 parents b191390 + 48ebed6 commit 536a889
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
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);
}
5 changes: 5 additions & 0 deletions test/Interop/SwiftToCxx/generics/generic-struct-in-cxx.swift
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

0 comments on commit 536a889

Please sign in to comment.