|
1 | | -// -*- C++ -*- |
2 | | -//===------------------ bridging - C++ and Swift Interop --------*- C++ -*-===// |
| 1 | +// -*- C -*- |
| 2 | +//===------------------ bridging - C and Swift Interop ----------*- C++ -*-===// |
3 | 3 | // |
4 | 4 | // This source file is part of the Swift.org open source project |
5 | 5 | // |
|
32 | 32 | /// that return a `class` or `struct` type that is annotated with this macro. |
33 | 33 | #define SWIFT_SELF_CONTAINED __attribute__((swift_attr("import_owned"))) |
34 | 34 |
|
35 | | -/// Specifies that a C++ method returns a value that is presumed to contain |
| 35 | +/// Specifies that a method returns a value that is presumed to contain |
36 | 36 | /// objects whose lifetime is not dependent on `this` or other parameters passed |
37 | 37 | /// to the method. |
38 | 38 | #define SWIFT_RETURNS_INDEPENDENT_VALUE __attribute__((swift_attr("import_unsafe"))) |
|
45 | 45 | #define _CXX_INTEROP_CONCAT(...) \ |
46 | 46 | _CXX_INTEROP_CONCAT_(__VA_ARGS__,,,,,,,,,,,,,,,,,) |
47 | 47 |
|
48 | | -/// Specifies that a C++ `class` or `struct` is reference-counted using |
| 48 | +/// Specifies that a `class` or `struct` is reference-counted using |
49 | 49 | /// the given `retain` and `release` functions. This annotation lets Swift import |
50 | 50 | /// such a type as reference counted type in Swift, taking advantage of Swift's |
51 | 51 | /// automatic reference counting. |
|
76 | 76 | __attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:_retain)))) \ |
77 | 77 | __attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:_release)))) |
78 | 78 |
|
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 |
80 | 80 | /// is presumed to be immortal, i.e. the reference to such object is presumed to |
81 | 81 | /// always be valid. This annotation lets Swift import such a type as a reference |
82 | 82 | /// type in Swift. |
|
103 | 103 | __attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:immortal)))) \ |
104 | 104 | __attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:immortal)))) |
105 | 105 |
|
106 | | -/// Specifies that a C++ `class` or `struct` is a reference type whose lifetime |
| 106 | +/// Specifies that a `class` or `struct` is a reference type whose lifetime |
107 | 107 | /// is not managed automatically. The programmer must validate that any reference |
108 | 108 | /// to such object is valid themselves. This annotation lets Swift import such a type as a reference type in Swift. |
109 | 109 | #define SWIFT_UNSAFE_REFERENCE \ |
|
115 | 115 | /// Specifies a name that will be used in Swift for this declaration instead of its original name. |
116 | 116 | #define SWIFT_NAME(_name) __attribute__((swift_name(#_name))) |
117 | 117 |
|
118 | | -/// Specifies that a specific C++ `class` or `struct` conforms to a |
| 118 | +/// Specifies that a specific `class` or `struct` conforms to a |
119 | 119 | /// a specific Swift protocol. |
120 | 120 | /// |
121 | 121 | /// This example shows how to use this macro to conform a class template to a Swift protocol: |
|
147 | 147 | #define SWIFT_MUTATING \ |
148 | 148 | __attribute__((swift_attr("mutating"))) |
149 | 149 |
|
150 | | -/// Specifies that a specific c++ type such class or struct should be imported as type marked |
| 150 | +/// Specifies that a specific class or struct should be imported as type marked |
151 | 151 | /// as `@unchecked Sendable` type in swift. If this annotation is used, the type is therefore allowed to |
152 | 152 | /// use safely across async contexts. |
153 | 153 | /// |
|
160 | 160 | #define SWIFT_UNCHECKED_SENDABLE \ |
161 | 161 | __attribute__((swift_attr("@Sendable"))) |
162 | 162 |
|
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. |
165 | 165 | #define SWIFT_NONCOPYABLE \ |
166 | 166 | __attribute__((swift_attr("~Copyable"))) |
167 | 167 |
|
168 | | -/// Specifies that a specific c++ type such class or struct should be imported |
169 | | -/// as a non-escapable Swift value type when the non-escapable language feature |
170 | | -/// is enabled. |
| 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 | +/// ``` |
| 189 | +#define SWIFT_NONCOPYABLE_WITH_DESTROY(_destroy) \ |
| 190 | + __attribute__((swift_attr("~Copyable"))) \ |
| 191 | + __attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(destroy:_destroy)))) |
| 192 | + |
| 193 | +/// Specifies that a specific class or struct should be imported |
| 194 | +/// as a non-escapable Swift value type. |
171 | 195 | #define SWIFT_NONESCAPABLE \ |
172 | 196 | __attribute__((swift_attr("~Escapable"))) |
173 | 197 |
|
174 | | -/// Specifies that a specific c++ type such class or struct should be imported |
175 | | -/// as a escapable Swift value. While this matches the default behavior, |
| 198 | +/// Specifies that a specific class or struct should be imported |
| 199 | +/// as an escapable Swift value. While this matches the default behavior, |
176 | 200 | /// in safe mode interop mode it ensures that the type is not marked as |
177 | 201 | /// unsafe. |
178 | 202 | #define SWIFT_ESCAPABLE \ |
|
183 | 207 | #define SWIFT_ESCAPABLE_IF(...) \ |
184 | 208 | __attribute__((swift_attr("escapable_if:" _CXX_INTEROP_CONCAT(__VA_ARGS__)))) |
185 | 209 |
|
186 | | -/// Specifies that the return value is passed as owned for C++ functions and |
| 210 | +/// Specifies that the return value is passed as owned for functions and |
187 | 211 | /// methods returning types annotated as `SWIFT_SHARED_REFERENCE` |
188 | 212 | #define SWIFT_RETURNS_RETAINED __attribute__((swift_attr("returns_retained"))) |
189 | | -/// Specifies that the return value is passed as unowned for C++ functions and |
| 213 | +/// Specifies that the return value is passed as unowned for functions and |
190 | 214 | /// methods returning types annotated as `SWIFT_SHARED_REFERENCE` |
191 | 215 | #define SWIFT_RETURNS_UNRETAINED \ |
192 | 216 | __attribute__((swift_attr("returns_unretained"))) |
193 | 217 |
|
194 | | -/// Applied to a C++ foreign reference type annotated with |
195 | | -/// SWIFT_SHARED_REFERENCE. Indicates that C++ APIs returning this type are |
| 218 | +/// Applied to a foreign reference type annotated with |
| 219 | +/// SWIFT_SHARED_REFERENCE. Indicates that APIs returning this type are |
196 | 220 | /// assumed to return an unowned (+0) value by default, unless explicitly annotated |
197 | 221 | /// with SWIFT_RETURNS_RETAINED. |
198 | 222 | /// |
|
203 | 227 | /// Bar { ... }; |
204 | 228 | /// ``` |
205 | 229 | /// |
206 | | -/// In Swift, C++ APIs returning `Bar*` will be assumed to return an unowned |
| 230 | +/// In Swift, APIs returning `Bar*` will be assumed to return an unowned |
207 | 231 | /// value. |
208 | 232 | #define SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT \ |
209 | 233 | __attribute__((swift_attr("returned_as_unretained_by_default"))) |
|
258 | 282 | #define SWIFT_MUTATING |
259 | 283 | #define SWIFT_UNCHECKED_SENDABLE |
260 | 284 | #define SWIFT_NONCOPYABLE |
| 285 | +#define SWIDT_NONCOPYABLE_WITH_DESTROY(_destroy) |
261 | 286 | #define SWIFT_NONESCAPABLE |
262 | 287 | #define SWIFT_ESCAPABLE |
263 | 288 | #define SWIFT_ESCAPABLE_IF(...) |
|
0 commit comments