1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// 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 -disable-availability-checking
// RUN: %target-interop-build-clangxx -std=c++20 -c %t/use-swift-cxx-types.cpp -I %t -o %t/swift-cxx-execution.o
// 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 -Xfrontend -disable-availability-checking
// RUN: %target-codesign %t/swift-cxx-execution
// RUN: %target-run %t/swift-cxx-execution | %FileCheck %s
// REQUIRES: executable_test
//--- header.h
#include <stdio.h>
struct Trivial {
int x, y;
inline Trivial(int x, int y) : x(x), y(y) {}
};
template<class T>
struct NonTrivialTemplate {
T x;
inline NonTrivialTemplate(T x) : x(x) {
puts("create NonTrivialTemplate");
}
inline NonTrivialTemplate(const NonTrivialTemplate<T> &other) : x(other.x) {
puts("copy NonTrivialTemplate");
}
inline NonTrivialTemplate(NonTrivialTemplate<T> &&other) : x(static_cast<T &&>(other.x)) {
puts("move NonTrivialTemplate");
}
inline ~NonTrivialTemplate() {
puts("~NonTrivialTemplate");
}
inline void testPrint() const {
puts("testPrint");
}
};
using NonTrivialTemplateTrivial = NonTrivialTemplate<Trivial>;
class ImmortalFRT {
public:
int x;
} __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:immortal")))
__attribute__((swift_attr("release:immortal")));
class SharedFRT {
public:
SharedFRT() {}
SharedFRT(int x) : x(x) {}
int x;
} __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:retainShared")))
__attribute__((swift_attr("release:releaseShared")));
inline void retainShared(SharedFRT *r) { puts("retainShared"); }
inline void releaseShared(SharedFRT *r) { puts("releaseShared"); }
//--- module.modulemap
module CxxTest {
header "header.h"
requires cplusplus
}
//--- use-cxx-types.swift
import CxxTest
public func consumeNonTrivial(_ x: consuming NonTrivialTemplateTrivial) -> CInt {
print("x and y: \(x.x.x), \(x.x.y)")
return x.x.x
}
public struct TakesNonTrivial {
public init(_ x: NonTrivialTemplateTrivial) {
self.prop = x
}
public var prop: NonTrivialTemplateTrivial
}
public func consumeImmortalFRT(_ x: consuming ImmortalFRT) {
print("immortal frt x \(x.x)")
}
public
func consumeSharedFRT(_ x : consuming SharedFRT) {
print("consume shared frt x \(x.x)")
}
public
func takeSharedFRT(_ x : SharedFRT) { print("take shared frt x \(x.x)") }
public
func returnSharedFRT(_ x : SharedFRT) -> SharedFRT {
print("return shared frt x \(x.x)")
return x
}
//--- use-swift-cxx-types.cpp
#include "header.h"
#include "UseCxx.h"
#include <assert.h>
int main() {
{
auto x = NonTrivialTemplate(Trivial(1, 2));
UseCxx::consumeNonTrivial(x);
puts("DoneCall");
}
// CHECK: create NonTrivialTemplate
// CHECK-NEXT: copy NonTrivialTemplate
// CHECK-NEXT: x and y: 1, 2
// CHECK-NEXT: ~NonTrivialTemplate
// CHECK-NEXT: DoneCall
// CHECK-NEXT: ~NonTrivialTemplate
{
auto x = NonTrivialTemplate(Trivial(-4, 0));
puts("call");
auto swiftVal = UseCxx::TakesNonTrivial::init(x);
puts("DoneCall");
swiftVal.setProp(x);
}
// CHECK-NEXT: create NonTrivialTemplate
// CHECK-NEXT: call
// CHECK-NEXT: copy NonTrivialTemplate
// CHECK-NEXT: copy NonTrivialTemplate
// CHECK-NEXT: ~NonTrivialTemplate
// CHECK-NEXT: DoneCall
// CHECK-NEXT: copy NonTrivialTemplate
// CHECK-NEXT: ~NonTrivialTemplate
// CHECK-NEXT: copy NonTrivialTemplate
// CHECK-NEXT: ~NonTrivialTemplate
// CHECK-NEXT: ~NonTrivialTemplate
// CHECK-NEXT: ~NonTrivialTemplate
{
ImmortalFRT frt;
frt.x = 2;
UseCxx::consumeImmortalFRT(&frt);
}
// CHECK-NEXT: immortal frt x 2
{
SharedFRT sfrt;
sfrt.x = 2;
UseCxx::takeSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: releaseShared
// CHECK-NEXT: take shared frt x 2
UseCxx::consumeSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: releaseShared
// CHECK-NEXT: consume shared frt x 2
SharedFRT *sfrtptr = UseCxx::returnSharedFRT(&sfrt);
// CHECK-NEXT: retainShared
// CHECK-NEXT: return shared frt x 2
}
puts("EndOfTest");
// CHECK-NEXT: EndOfTest
return 0;
}
|