|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-swift-frontend %s -typecheck -module-name Init -clang-header-expose-public-decls -emit-clang-header-path %t/inits.h |
| 3 | +// RUN: %FileCheck %s < %t/inits.h |
| 4 | + |
| 5 | +// RUN: %check-interop-cxx-header-in-clang(%t/inits.h -Wno-unused-function) |
| 6 | + |
| 7 | + |
| 8 | +// CHECK: SWIFT_EXTERN struct swift_interop_stub_Init_FirstSmallStruct $s4Init16FirstSmallStructVACycfC(void) SWIFT_NOEXCEPT SWIFT_CALL; // init() |
| 9 | +// CHECK-NEXT: SWIFT_EXTERN struct swift_interop_stub_Init_FirstSmallStruct $s4Init16FirstSmallStructVyACSicfC(ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_CALL; // init(_:) |
| 10 | + |
| 11 | +// CHECK: SWIFT_EXTERN void $s4Init11LargeStructVACycfC(SWIFT_INDIRECT_RESULT void * _Nonnull) SWIFT_NOEXCEPT SWIFT_CALL; // init() |
| 12 | +// CHECK-NEXT: SWIFT_EXTERN void $s4Init11LargeStructV1x1yACSi_AA010FirstSmallC0VtcfC(SWIFT_INDIRECT_RESULT void * _Nonnull, ptrdiff_t x, struct swift_interop_stub_Init_FirstSmallStruct y) SWIFT_NOEXCEPT SWIFT_CALL; // init(x:y:) |
| 13 | + |
| 14 | +// CHECK: SWIFT_EXTERN struct swift_interop_stub_Init_StructWithRefCountStoredProp $s4Init28StructWithRefCountStoredPropVACycfC(void) SWIFT_NOEXCEPT SWIFT_CALL; // init() |
| 15 | +// CHECK-NEXT: SWIFT_EXTERN struct swift_interop_stub_Init_StructWithRefCountStoredProp $s4Init28StructWithRefCountStoredPropV1xACSi_tcfC(ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_CALL; // init(x:) |
| 16 | + |
| 17 | +public struct FirstSmallStruct { |
| 18 | + public let x: UInt32 |
| 19 | + |
| 20 | + public init() { |
| 21 | + x = 42 |
| 22 | + } |
| 23 | + |
| 24 | + public init(_ x: Int) { |
| 25 | + self.x = UInt32(x) |
| 26 | + } |
| 27 | + |
| 28 | + private init(_ y: Float) { |
| 29 | + fatalError("nope") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// CHECK: class FirstSmallStruct final { |
| 34 | +// CHECK-NEXT: public: |
| 35 | +// CHECK: inline FirstSmallStruct(FirstSmallStruct &&) = default; |
| 36 | +// CHECK-NEXT: inline uint32_t getX() const; |
| 37 | +// CHECK-NEXT: static inline FirstSmallStruct init(); |
| 38 | +// CHECK-NEXT: static inline FirstSmallStruct init(swift::Int x); |
| 39 | +// CHECK-NEXT: private: |
| 40 | + |
| 41 | +// CHECK: inline uint32_t FirstSmallStruct::getX() const { |
| 42 | +// CHECK-NEXT: return _impl::$s4Init16FirstSmallStructV1xs6UInt32Vvg(_impl::swift_interop_passDirect_Init_FirstSmallStruct(_getOpaquePointer())); |
| 43 | +// CHECK-NEXT: } |
| 44 | +// CHECK-NEXT: inline FirstSmallStruct FirstSmallStruct::init() { |
| 45 | +// CHECK-NEXT: return _impl::_impl_FirstSmallStruct::returnNewValue([&](char * _Nonnull result) { |
| 46 | +// CHECK-NEXT: _impl::swift_interop_returnDirect_Init_FirstSmallStruct(result, _impl::$s4Init16FirstSmallStructVACycfC()); |
| 47 | +// CHECK-NEXT: }); |
| 48 | +// CHECK-NEXT: } |
| 49 | +// CHECK-NEXT: inline FirstSmallStruct FirstSmallStruct::init(swift::Int x) { |
| 50 | +// CHECK-NEXT: return _impl::_impl_FirstSmallStruct::returnNewValue([&](char * _Nonnull result) { |
| 51 | +// CHECK-NEXT: _impl::swift_interop_returnDirect_Init_FirstSmallStruct(result, _impl::$s4Init16FirstSmallStructVyACSicfC(x)); |
| 52 | +// CHECK-NEXT: }); |
| 53 | +// CHECK-NEXT: } |
| 54 | + |
| 55 | +public struct LargeStruct { |
| 56 | + public let x1, x2, x3, x4, x5, x6: Int |
| 57 | + |
| 58 | + public init () { |
| 59 | + x1 = 0 |
| 60 | + x2 = 0 |
| 61 | + x3 = 0 |
| 62 | + x4 = 0 |
| 63 | + x5 = 0 |
| 64 | + x6 = 0 |
| 65 | + } |
| 66 | + |
| 67 | + public init(x: Int, y: FirstSmallStruct) { |
| 68 | + x1 = x |
| 69 | + x2 = Int(y.x) |
| 70 | + x3 = -x |
| 71 | + x4 = 0 |
| 72 | + x5 = 11 |
| 73 | + x6 = x * Int(y.x) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// CHECK: class LargeStruct final { |
| 78 | +// CHECK: inline swift::Int getX6() const; |
| 79 | +// CHECK-NEXT: static inline LargeStruct init(); |
| 80 | +// CHECK-NEXT: static inline LargeStruct init(swift::Int x, const FirstSmallStruct& y); |
| 81 | +// CHECK-NEXT: private: |
| 82 | + |
| 83 | +// CHECK: inline LargeStruct LargeStruct::init() { |
| 84 | +// CHECK-NEXT: return _impl::_impl_LargeStruct::returnNewValue([&](void * _Nonnull result) { |
| 85 | +// CHECK-NEXT: _impl::$s4Init11LargeStructVACycfC(result); |
| 86 | +// CHECK-NEXT: }); |
| 87 | +// CHECK-NEXT: } |
| 88 | +// CHECK-NEXT: inline LargeStruct LargeStruct::init(swift::Int x, const FirstSmallStruct& y) { |
| 89 | +// CHECK-NEXT: return _impl::_impl_LargeStruct::returnNewValue([&](void * _Nonnull result) { |
| 90 | +// CHECK-NEXT: _impl::$s4Init11LargeStructV1x1yACSi_AA010FirstSmallC0VtcfC(result, x, _impl::swift_interop_passDirect_Init_FirstSmallStruct(_impl::_impl_FirstSmallStruct::getOpaquePointer(y))); |
| 91 | +// CHECK-NEXT: }); |
| 92 | +// CHECK-NEXT: } |
| 93 | + |
| 94 | +private class RefCountedClass { |
| 95 | + let x: Int |
| 96 | + |
| 97 | + init(x: Int) { |
| 98 | + self.x = x |
| 99 | + print("create RefCountedClass \(x)") |
| 100 | + } |
| 101 | + deinit { |
| 102 | + print("destroy RefCountedClass \(x)") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +public struct StructWithRefCountStoredProp { |
| 107 | + private let storedRef: RefCountedClass |
| 108 | + |
| 109 | + public init() { |
| 110 | + storedRef = RefCountedClass(x: -1) |
| 111 | + } |
| 112 | + |
| 113 | + public init(x: Int) { |
| 114 | + storedRef = RefCountedClass(x: x) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// CHECK: static inline StructWithRefCountStoredProp init(); |
| 119 | +// CHECK-NEXT: static inline StructWithRefCountStoredProp init(swift::Int x); |
| 120 | + |
| 121 | +// CHECK: inline StructWithRefCountStoredProp StructWithRefCountStoredProp::init() { |
| 122 | +// CHECK-NEXT: return _impl::_impl_StructWithRefCountStoredProp::returnNewValue([&](char * _Nonnull result) { |
| 123 | +// CHECK-NEXT: _impl::swift_interop_returnDirect_Init_StructWithRefCountStoredProp(result, _impl::$s4Init28StructWithRefCountStoredPropVACycfC()); |
| 124 | +// CHECK-NEXT: }); |
| 125 | +// CHECK-NEXT: } |
| 126 | +// CHECK-NEXT: inline StructWithRefCountStoredProp StructWithRefCountStoredProp::init(swift::Int x) { |
| 127 | +// CHECK-NEXT: return _impl::_impl_StructWithRefCountStoredProp::returnNewValue([&](char * _Nonnull result) { |
| 128 | +// CHECK-NEXT: _impl::swift_interop_returnDirect_Init_StructWithRefCountStoredProp(result, _impl::$s4Init28StructWithRefCountStoredPropV1xACSi_tcfC(x)); |
| 129 | +// CHECK-NEXT: }); |
| 130 | +// CHECK-NEXT: } |
0 commit comments