Skip to content

Commit 8909f24

Browse files
committed
Add SWIFT_NONCOPYABLE_WITH_DESTROY to swift/bridging header
This packages up the ~Copyable and "destroy" attributes in a macro.
1 parent 63135e8 commit 8909f24

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

lib/ClangImporter/SwiftBridging/swift/bridging

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define _CXX_INTEROP_CONCAT(...) \
4646
_CXX_INTEROP_CONCAT_(__VA_ARGS__,,,,,,,,,,,,,,,,,)
4747

48-
/// Specifies that a C++ `class` or `struct` is reference-counted using
48+
/// Specifies that a C `class` or `struct` is reference-counted using
4949
/// the given `retain` and `release` functions. This annotation lets Swift import
5050
/// such a type as reference counted type in Swift, taking advantage of Swift's
5151
/// automatic reference counting.
@@ -76,7 +76,7 @@
7676
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:_retain)))) \
7777
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:_release))))
7878

79-
/// Specifies that a C++ `class` or `struct` is a reference type whose lifetime
79+
/// Specifies that a `class` or `struct` is a reference type whose lifetime
8080
/// is presumed to be immortal, i.e. the reference to such object is presumed to
8181
/// always be valid. This annotation lets Swift import such a type as a reference
8282
/// type in Swift.
@@ -160,11 +160,35 @@
160160
#define SWIFT_UNCHECKED_SENDABLE \
161161
__attribute__((swift_attr("@Sendable")))
162162

163-
/// Specifies that a specific c++ type such class or struct should be imported
164-
/// as a non-copyable Swift value type.
163+
/// Specifies that a class or struct should be imported as a non-copyable
164+
/// Swift value type.
165165
#define SWIFT_NONCOPYABLE \
166166
__attribute__((swift_attr("~Copyable")))
167167

168+
/// Specifies that a class or struct should be imported as a non-copyable
169+
/// Swift value type that calls the given _destroy function when a value is no
170+
/// longer used.
171+
///
172+
/// This example shows how to use this macro to let Swift know that
173+
/// a given C struct should have its members freed when it goes out of scope:
174+
/// ```c
175+
/// typedef struct SWIFT_NONCOPYABLE_WITH_DESTROY(mytypeFreeMembers) MyType {
176+
/// void *storage
177+
/// } MyType;
178+
///
179+
/// void mytypeFreeMembers(MyType toBeDestroyed);
180+
/// MyType mytypeCreate(void);
181+
/// ```
182+
///
183+
/// Usage in Swift:
184+
/// ```swift
185+
/// let mt = mytypeCreate()
186+
/// let mt2 = mt // consumes mt
187+
/// // once mt2 is unused, Swift will call mytypeFreeMembers(mt2)
188+
#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \
189+
__attribute__((swift_attr("~Copyable"))) \
190+
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(destroy:_destroy))))
191+
168192
/// Specifies that a specific c++ type such class or struct should be imported
169193
/// as a non-escapable Swift value type when the non-escapable language feature
170194
/// is enabled.
@@ -258,6 +282,7 @@
258282
#define SWIFT_MUTATING
259283
#define SWIFT_UNCHECKED_SENDABLE
260284
#define SWIFT_NONCOPYABLE
285+
#define SWIDT_NONCOPYABLE_WITH_DESTROY(_destroy)
261286
#define SWIFT_NONESCAPABLE
262287
#define SWIFT_ESCAPABLE
263288
#define SWIFT_ESCAPABLE_IF(...)

test/Interop/C/struct/Inputs/noncopyable-struct.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %target-typecheck-verify-swift -I %S/Inputs/
1+
#include "swift/bridging"
22

33
typedef struct __attribute__((swift_attr("~Copyable"))) NonCopyable {
44
float x, y;
55
} NonCopyable;
66

7-
typedef struct __attribute__((swift_attr("~Copyable"))) __attribute__((swift_attr("destroy:freeNonCopyableWithDeinit"))) NonCopyableWithDeinit {
7+
typedef struct SWIFT_NONCOPYABLE_WITH_DESTROY(freeNonCopyableWithDeinit) NonCopyableWithDeinit {
88
void *storage;
99
} NonCopyableWithDeinit;
1010

test/Interop/C/struct/noncopyable_structs.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// Check that we get the expected errors for incorrect uses of noncopyable
33
// imported types with both C and C++ interoperability.
44

5-
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -verify -DERRORS -verify-additional-prefix conly-
6-
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -verify -DERRORS -DCPLUSPLUS -verify-additional-prefix cplusplus- -cxx-interoperability-mode=default
5+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -verify -DERRORS -verify-additional-prefix conly-
6+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -verify -DERRORS -DCPLUSPLUS -verify-additional-prefix cplusplus- -cxx-interoperability-mode=default
77

88
// Check that we get the expected SIL
9-
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -o - | %FileCheck -check-prefix CHECK-SIL %s
10-
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -o - -cxx-interoperability-mode=default| %FileCheck -check-prefix CHECK-SIL %s
9+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -o - | %FileCheck -check-prefix CHECK-SIL %s
10+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -o - -cxx-interoperability-mode=default| %FileCheck -check-prefix CHECK-SIL %s
1111

1212
// Check that we get the expected IR
1313

14-
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ %s -o - | %FileCheck -check-prefix CHECK-IR %s
15-
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ %s -o - -cxx-interoperability-mode=default | %FileCheck -check-prefix CHECK-IR %s
14+
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -o - | %FileCheck -check-prefix CHECK-IR %s
15+
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ -I %swift_src_root/lib/ClangImporter/SwiftBridging %s -o - -cxx-interoperability-mode=default | %FileCheck -check-prefix CHECK-IR %s
1616

1717
import NoncopyableStructs
1818

0 commit comments

Comments
 (0)