|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: split-file %s %t |
| 3 | + |
| 4 | +// RUN: %target-swift-frontend -typecheck %t/use-cxx-types.swift -typecheck -module-name UseCxx -emit-clang-header-path %t/UseCxx.h -I %t -enable-experimental-cxx-interop -clang-header-expose-decls=all-public |
| 5 | + |
| 6 | +// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o |
| 7 | +// RUN: %target-interop-build-swift %t/use-cxx-types.swift -o %t/swift-cxx-execution -Xlinker %t/swift-cxx-execution.o -module-name UseCxx -Xfrontend -entry-point-function-name -Xfrontend swiftMain -I %t -O |
| 8 | + |
| 9 | +// RUN: %target-codesign %t/swift-cxx-execution |
| 10 | +// RUN: %target-run %t/swift-cxx-execution | %FileCheck %s |
| 11 | + |
| 12 | +// REQUIRES: executable_test |
| 13 | + |
| 14 | +//--- header.h |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | + |
| 18 | +struct Trivial { |
| 19 | + int x, y; |
| 20 | + |
| 21 | + inline Trivial(int x, int y) : x(x), y(y) {} |
| 22 | +}; |
| 23 | + |
| 24 | +template<class T> |
| 25 | +struct NonTrivialTemplate { |
| 26 | + T x; |
| 27 | + |
| 28 | + inline NonTrivialTemplate(T x) : x(x) { |
| 29 | + puts("create NonTrivialTemplate"); |
| 30 | + } |
| 31 | + inline NonTrivialTemplate(const NonTrivialTemplate<T> &other) : x(other.x) { |
| 32 | + puts("copy NonTrivialTemplate"); |
| 33 | + } |
| 34 | + inline NonTrivialTemplate(NonTrivialTemplate<T> &&other) : x(static_cast<T &&>(other.x)) { |
| 35 | + puts("move NonTrivialTemplate"); |
| 36 | + } |
| 37 | + inline ~NonTrivialTemplate() { |
| 38 | + puts("~NonTrivialTemplate"); |
| 39 | + } |
| 40 | + inline void testPrint() const { |
| 41 | + puts("testPrint"); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +using NonTrivialTemplateTrivial = NonTrivialTemplate<Trivial>; |
| 46 | + |
| 47 | +//--- module.modulemap |
| 48 | +module CxxTest { |
| 49 | + header "header.h" |
| 50 | + requires cplusplus |
| 51 | +} |
| 52 | + |
| 53 | +//--- use-cxx-types.swift |
| 54 | +import CxxTest |
| 55 | + |
| 56 | +public func consumeNonTrivial(_ x: consuming NonTrivialTemplateTrivial) -> CInt { |
| 57 | + print("x and y: \(x.x.x), \(x.x.y)") |
| 58 | + return x.x.x |
| 59 | +} |
| 60 | + |
| 61 | +public struct TakesNonTrivial { |
| 62 | + public init(_ x: NonTrivialTemplateTrivial) { |
| 63 | + self.prop = x |
| 64 | + } |
| 65 | + |
| 66 | + public var prop: NonTrivialTemplateTrivial |
| 67 | +} |
| 68 | + |
| 69 | +//--- use-swift-cxx-types.cpp |
| 70 | + |
| 71 | +#include "header.h" |
| 72 | +#include "UseCxx.h" |
| 73 | +#include <assert.h> |
| 74 | + |
| 75 | +int main() { |
| 76 | + { |
| 77 | + auto x = NonTrivialTemplate(Trivial(1, 2)); |
| 78 | + UseCxx::consumeNonTrivial(x); |
| 79 | + puts("DoneCall"); |
| 80 | + } |
| 81 | +// CHECK: create NonTrivialTemplate |
| 82 | +// CHECK-NEXT: copy NonTrivialTemplate |
| 83 | +// CHECK-NEXT: x and y: 1, 2 |
| 84 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 85 | +// CHECK-NEXT: DoneCall |
| 86 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 87 | + { |
| 88 | + auto x = NonTrivialTemplate(Trivial(-4, 0)); |
| 89 | + puts("call"); |
| 90 | + auto swiftVal = UseCxx::TakesNonTrivial::init(x); |
| 91 | + puts("DoneCall"); |
| 92 | + swiftVal.setProp(x); |
| 93 | + } |
| 94 | + puts("EndOfTest"); |
| 95 | +// CHECK-NEXT: create NonTrivialTemplate |
| 96 | +// CHECK-NEXT: call |
| 97 | +// CHECK-NEXT: copy NonTrivialTemplate |
| 98 | +// CHECK-NEXT: copy NonTrivialTemplate |
| 99 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 100 | +// CHECK-NEXT: DoneCall |
| 101 | +// CHECK-NEXT: copy NonTrivialTemplate |
| 102 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 103 | +// CHECK-NEXT: copy NonTrivialTemplate |
| 104 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 105 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 106 | +// CHECK-NEXT: ~NonTrivialTemplate |
| 107 | +// CHECK-NEXT: EndOfTest |
| 108 | + return 0; |
| 109 | +} |
0 commit comments