Debian Codegraph @

clang/docs/APINotes.rst

raw
1 ================================================
2 API Notes: Annotations Without Modifying Headers
3 ================================================
4
5 **The Problem:** You have headers you want to use, but you also want to add
6 extra information to the API. You don't want to put that information in the
7 headers themselves --- perhaps because you want to keep them clean for other
8 clients, or perhaps because they're from some open source project and you don't
9 want to modify them at all.
10
11 **Incomplete solution:** Redeclare all the interesting parts of the API in your
12 own header and add the attributes you want. Unfortunately, this:
13
14 * doesn't work with attributes that must be present on a definition
15 * doesn't allow changing the definition in other ways
16 * requires your header to be included in any client code to take effect
17
18 **Better solution:** Provide a "sidecar" file with the information you want to
19 add, and have that automatically get picked up by the module-building logic in
20 the compiler.
21
22 That's API notes.
23
24 API notes use a YAML-based file format. YAML is a format best explained by
25 example, so here is a `small example
26 <https://github.com/llvm/llvm-project/blob/main/clang/test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.apinotes>`_
27 from the compiler test suite of API
28 notes for a hypothetical "SomeKit" framework.
29
30
31 Usage
32 =====
33
34 API notes files are found relative to the module map that defines a module,
35 under the name "SomeKit.apinotes" for a module named "SomeKit". Additionally, a
36 file named "SomeKit_private.apinotes" will also be picked up to go with a
37 private module map. For bare modules these two files will be in the same
38 directory as the corresponding module map; for framework modules, they should
39 be placed in the Headers and PrivateHeaders directories, respectively. The
40 module map for a private top-level framework module should be placed in the
41 PrivateHeaders directory as well, though it does not need an additional
42 "_private" suffix on its name.
43
44 Clang will search for API notes files next to module maps only when passed the
45 ``-fapi-notes-modules`` option.
46
47
48 Limitations
49 ===========
50
51 - Since they're identified by module name, API notes cannot be used to modify
52 arbitrary textual headers.
53
54
55 "Versioned" API Notes
56 =====================
57
58 Many API notes affect how a C API is imported into Swift. In order to change
59 that behavior while still remaining backwards-compatible, API notes can be
60 selectively applied based on the Swift compatibility version provided to the
61 compiler (e.g. ``-fapi-notes-swift-version=5``). The rule is that an
62 explicitly-versioned API note applies to that version *and all earlier
63 versions,* and any applicable explicitly-versioned API note takes precedence
64 over an unversioned API note.
65
66
67 Reference
68 =========
69
70 An API notes file contains a YAML dictionary with the following top-level
71 entries:
72
73 :Name:
74
75 The name of the module (the framework name, for frameworks). Note that this
76 is always the name of a top-level module, even within a private API notes
77 file.
78
79 ::
80
81 Name: MyFramework
82
83 :Classes, Protocols, Tags, Typedefs, Globals, Enumerators, Functions, Namespaces:
84
85 Arrays of top-level declarations. Each entry in the array must have a
86 'Name' key with its Objective-C or C++ name. "Tags" refers to structs,
87 C++ classes, enums, and unions; "Classes" refers to Objective-C classes;
88 "Enumerators" refers to enum cases.
89
90 ::
91
92 Classes:
93 - Name: MyController
94 …
95 - Name: MyView
96 …
97
98 :SwiftVersions:
99
100 Contains explicit information for backwards compatibility. Each entry in
101 the array contains a 'Version' key, which should be set to '4' for
102 annotations that only apply to Swift 4 mode and earlier. The other entries
103 in this dictionary are the same declaration entries as at the top level:
104 Classes, Protocols, Tags, Typedefs, Globals, Enumerators, and Functions.
105
106 ::
107
108 SwiftVersions:
109 - Version: 4
110 Classes: …
111 Protocols: …
112
113 Each entry under 'Classes' and 'Protocols' can contain "Methods" and
114 "Properties" arrays, in addition to the attributes described below:
115
116 :Methods:
117
118 Identified by 'Selector' and 'MethodKind'; the MethodKind is either
119 "Instance" or "Class".
120
121 ::
122
123 Classes:
124 - Name: UIViewController
125 Methods:
126 - Selector: "presentViewController:animated:"
127 MethodKind: Instance
128 …
129
130 :Properties:
131
132 Identified by 'Name' and 'PropertyKind'; the PropertyKind is also either
133 "Instance" or "Class".
134
135 ::
136
137 Classes:
138 - Name: UIView
139 Properties:
140 - Name: subviews
141 PropertyKind: Instance
142 …
143
144 Each declaration supports the following annotations (if relevant to that
145 declaration kind), all of which are optional:
146
147 :SwiftName:
148
149 Equivalent to ``NS_SWIFT_NAME``. For a method, must include the full Swift name
150 with all arguments. Use "_" to omit an argument label.
151
152 ::
153
154 - Selector: "presentViewController:animated:"
155 MethodKind: Instance
156 SwiftName: "present(_:animated:)"
157
158 - Class: NSBundle
159 SwiftName: Bundle
160
161 :SwiftImportAs:
162
163 For a class, possible values are ``owned`` (equivalent to
164 ``SWIFT_SELF_CONTAINED``) or ``reference`` (equivalent to
165 ``SWIFT_SHARED_REFERENCE``, also requires specifying ``SwiftReleaseOp`` and
166 ``SwiftRetainOp``).
167
168 For a method, possible values are ``unsafe`` (equivalent
169 to ``SWIFT_RETURNS_INDEPENDENT_VALUE``) or ``computed_property`` (equivalent to
170 ``SWIFT_COMPUTED_PROPERTY``).
171
172 ::
173
174 Tags:
175 - Name: RefCountedStorage
176 SwiftImportAs: reference
177 SwiftReleaseOp: RCRelease
178 SwiftRetainOp: RCRetain
179
180 :SwiftCopyable:
181
182 Allows annotating a C++ class as non-copyable in Swift. Equivalent to
183 ``SWIFT_NONCOPYABLE``, or to an explicit conformance ``: ~Copyable``.
184
185 ::
186
187 Tags:
188 - Name: tzdb
189 SwiftCopyable: false
190
191 :Availability, AvailabilityMsg:
192
193 A value of "nonswift" is equivalent to ``NS_SWIFT_UNAVAILABLE``. A value of
194 "available" can be used in the "SwiftVersions" section to undo the effect of
195 "nonswift".
196
197 ::
198
199 - Selector: "dealloc"
200 MethodKind: Instance
201 Availability: nonswift
202 AvailabilityMsg: "prefer 'deinit'"
203
204 :SwiftPrivate:
205
206 Equivalent to NS_REFINED_FOR_SWIFT.
207
208 ::
209
210 - Name: CGColorEqualToColor
211 SwiftPrivate: true
212
213 :Nullability:
214
215 Used for properties and globals. There are four options, identified by their
216 initials:
217
218 - ``Nonnull`` or ``N`` (corresponding to ``_Nonnull``)
219 - ``Optional`` or ``O`` (corresponding to ``_Nullable``)
220 - ``Unspecified`` or ``U`` (corresponding to ``_Null_unspecified``)
221 - ``Scalar`` or ``S`` (deprecated)
222
223 Note that 'Nullability' is overridden by 'Type', even in a "SwiftVersions"
224 section.
225
226 .. note::
227
228 'Nullability' can also be used to describe the argument types of methods
229 and functions, but this usage is deprecated in favor of 'Parameters' (see
230 below).
231
232 ::
233
234 - Name: dataSource
235 Nullability: O
236
237 :NullabilityOfRet:
238
239 Used for methods and functions. Describes the nullability of the return type.
240
241 Note that 'NullabilityOfRet' is overridden by 'ResultType', even in a
242 "SwiftVersions" section.
243
244 .. warning::
245
246 Due to a compiler bug, 'NullabilityOfRet' may change nullability of the
247 parameters as well (rdar://30544062). Avoid using it and instead use
248 'ResultType' and specify the return type along with a nullability
249 annotation (see documentation for 'ResultType').
250
251 ::
252
253 - Selector: superclass
254 MethodKind: Class
255 NullabilityOfRet: O
256
257 :Type:
258
259 Used for properties and globals. This completely overrides the type of the
260 declaration; it should ideally only be used for Swift backwards
261 compatibility, when existing type information has been made more precise in a
262 header. Prefer 'Nullability' and other annotations when possible.
263
264 We parse the specified type as if it appeared at the location of the
265 declaration whose type is being modified. Macros are not available and
266 nullability must be applied explicitly (even in an ``NS_ASSUME_NONNULL_BEGIN``
267 section).
268
269 ::
270
271 - Name: delegate
272 PropertyKind: Instance
273 Type: "id"
274
275 :ResultType:
276
277 Used for methods and functions. This completely overrides the return type; it
278 should ideally only be used for Swift backwards compatibility, when existing
279 type information has been made more precise in a header.
280
281 We parse the specified type as if it appeared at the location of the
282 declaration whose type is being modified. Macros are not available and
283 nullability must be applied explicitly (even in an ``NS_ASSUME_NONNULL_BEGIN``
284 section).
285
286 ::
287
288 - Selector: "subviews"
289 MethodKind: Instance
290 ResultType: "NSArray * _Nonnull"
291
292 :SwiftImportAsAccessors:
293
294 Used for properties. If true, the property will be exposed in Swift as its
295 accessor methods, rather than as a computed property using ``var``.
296
297 ::
298
299 - Name: currentContext
300 PropertyKind: Class
301 SwiftImportAsAccessors: true
302
303 :NSErrorDomain:
304
305 Used for ``NSError`` code enums. The value is the name of the associated
306 domain ``NSString`` constant; an empty string (``""``) means the enum is a
307 normal enum rather than an error code.
308
309 ::
310
311 - Name: MKErrorCode
312 NSErrorDomain: MKErrorDomain
313
314 :SwiftWrapper:
315
316 Controls ``NS_STRING_ENUM`` and ``NS_EXTENSIBLE_STRING_ENUM``. There are three
317 options:
318
319 - "struct" (extensible)
320 - "enum"
321 - "none"
322
323 Note that even an "enum" wrapper is still presented as a struct in Swift;
324 it's just a "more enum-like" struct.
325
326 ::
327
328 - Name: AVMediaType
329 SwiftWrapper: none
330
331 :EnumKind:
332
333 Has the same effect as ``NS_ENUM`` and ``NS_OPTIONS``. There are four options:
334
335 - "NSEnum" / "CFEnum"
336 - "NSClosedEnum" / "CFClosedEnum"
337 - "NSOptions" / "CFOptions"
338 - "none"
339
340 ::
341
342 - Name: GKPhotoSize
343 EnumKind: none
344
345 :Parameters:
346
347 Used for methods and functions. Parameters are identified by a 0-based
348 'Position' and support the 'Nullability', 'NoEscape', and 'Type' keys.
349
350 .. note::
351
352 Using 'Parameters' within a parameter entry to describe the parameters of a
353 block is not implemented. Use 'Type' on the entire parameter instead.
354
355 ::
356
357 - Selector: "isEqual:"
358 MethodKind: Instance
359 Parameters:
360 - Position: 0
361 Nullability: O
362
363 :NoEscape:
364
365 Used only for block parameters. Equivalent to ``NS_NOESCAPE``.
366
367 ::
368
369 - Name: dispatch_sync
370 Parameters:
371 - Position: 0
372 NoEscape: true
373
374 :SwiftBridge:
375
376 Used for Objective-C class types bridged to Swift value types. An empty
377 string ("") means a type is not bridged. Not supported outside of Apple
378 frameworks (the Swift side of it requires conforming to implementation-detail
379 protocols that are subject to change).
380
381 ::
382
383 - Name: NSIndexSet
384 SwiftBridge: IndexSet
385
386 :DesignatedInit:
387
388 Used for init methods. Equivalent to ``NS_DESIGNATED_INITIALIZER``.
389
390 ::
391
392 - Selector: "initWithFrame:"
393 MethodKind: Instance
394 DesignatedInit: true