1 ================================================
2 API Notes: Annotations Without Modifying Headers
3 ================================================
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.
11 **Incomplete solution:** Redeclare all the interesting parts of the API in your
12 own header and add the attributes you want. Unfortunately, this:
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
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
24 API notes use a YAML-based file format. YAML is a format best explained by
25 example, so here is a `small example`__ from the compiler test suite of API
26 notes for a hypothetical "SomeKit" framework.
28 __ test/APINotes/Inputs/Frameworks/SomeKit.framework/Headers/SomeKit.apinotes
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.
44 Clang will search for API notes files next to module maps only when passed the
45 ``-fapi-notes-modules`` option.
51 - Since they're identified by module name, API notes cannot be used to modify
52 arbitrary textual headers.
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.
70 An API notes file contains a YAML dictionary with the following top-level
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
83 :Classes, Protocols, Tags, Typedefs, Globals, Enumerators, Functions:
85 Arrays of top-level declarations. Each entry in the array must have a
86 'Name' key with its Objective-C name. "Tags" refers to structs, enums, and
87 unions; "Enumerators" refers to enum cases.
99 Contains explicit information for backwards compatibility. Each entry in
100 the array contains a 'Version' key, which should be set to '4' for
101 annotations that only apply to Swift 4 mode and earlier. The other entries
102 in this dictionary are the same declaration entries as at the top level:
103 Classes, Protocols, Tags, Typedefs, Globals, Enumerators, and Functions.
112 Each entry under 'Classes' and 'Protocols' can contain "Methods" and
113 "Properties" arrays, in addition to the attributes described below:
117 Identified by 'Selector' and 'MethodKind'; the MethodKind is either
118 "Instance" or "Class".
123 - Name: UIViewController
125 - Selector: "presentViewController:animated:"
131 Identified by 'Name' and 'PropertyKind'; the PropertyKind is also either
132 "Instance" or "Class".
140 PropertyKind: Instance
143 Each declaration supports the following annotations (if relevant to that
144 declaration kind), all of which are optional:
148 Equivalent to ``NS_SWIFT_NAME``. For a method, must include the full Swift name
149 with all arguments. Use "_" to omit an argument label.
153 - Selector: "presentViewController:animated:"
155 SwiftName: "present(_:animated:)"
160 :Availability, AvailabilityMsg:
162 A value of "nonswift" is equivalent to ``NS_SWIFT_UNAVAILABLE``. A value of
163 "available" can be used in the "SwiftVersions" section to undo the effect of
168 - Selector: "dealloc"
170 Availability: nonswift
171 AvailabilityMsg: "prefer 'deinit'"
175 Equivalent to NS_REFINED_FOR_SWIFT.
179 - Name: CGColorEqualToColor
184 Used for properties and globals. There are four options, identified by their
187 - ``Nonnull`` or ``N`` (corresponding to ``_Nonnull``)
188 - ``Optional`` or ``O`` (corresponding to ``_Nullable``)
189 - ``Unspecified`` or ``U`` (corresponding to ``_Null_unspecified``)
190 - ``Scalar`` or ``S`` (deprecated)
192 Note that 'Nullability' is overridden by 'Type', even in a "SwiftVersions"
197 'Nullability' can also be used to describe the argument types of methods
198 and functions, but this usage is deprecated in favor of 'Parameters' (see
208 Used for methods and functions. Describes the nullability of the return type.
210 Note that 'NullabilityOfRet' is overridden by 'ResultType', even in a
211 "SwiftVersions" section.
215 Due to a compiler bug, 'NullabilityOfRet' may change nullability of the
216 parameters as well (rdar://30544062). Avoid using it and instead use
217 'ResultType' and specify the return type along with a nullability
218 annotation (see documentation for 'ResultType').
222 - Selector: superclass
228 Used for properties and globals. This completely overrides the type of the
229 declaration; it should ideally only be used for Swift backwards
230 compatibility, when existing type information has been made more precise in a
231 header. Prefer 'Nullability' and other annotations when possible.
233 We parse the specified type as if it appeared at the location of the
234 declaration whose type is being modified. Macros are not available and
235 nullability must be applied explicitly (even in an ``NS_ASSUME_NONNULL_BEGIN``
241 PropertyKind: Instance
246 Used for methods and functions. This completely overrides the return type; it
247 should ideally only be used for Swift backwards compatibility, when existing
248 type information has been made more precise in a header.
250 We parse the specified type as if it appeared at the location of the
251 declaration whose type is being modified. Macros are not available and
252 nullability must be applied explicitly (even in an ``NS_ASSUME_NONNULL_BEGIN``
257 - Selector: "subviews"
259 ResultType: "NSArray * _Nonnull"
261 :SwiftImportAsAccessors:
263 Used for properties. If true, the property will be exposed in Swift as its
264 accessor methods, rather than as a computed property using ``var``.
268 - Name: currentContext
270 SwiftImportAsAccessors: true
274 Used for ``NSError`` code enums. The value is the name of the associated
275 domain ``NSString`` constant; an empty string (``""``) means the enum is a
276 normal enum rather than an error code.
281 NSErrorDomain: MKErrorDomain
285 Controls ``NS_STRING_ENUM`` and ``NS_EXTENSIBLE_STRING_ENUM``. There are three
288 - "struct" (extensible)
292 Note that even an "enum" wrapper is still presented as a struct in Swift;
293 it's just a "more enum-like" struct.
302 Has the same effect as ``NS_ENUM`` and ``NS_OPTIONS``. There are four options:
304 - "NSEnum" / "CFEnum"
305 - "NSClosedEnum" / "CFClosedEnum"
306 - "NSOptions" / "CFOptions"
316 Used for methods and functions. Parameters are identified by a 0-based
317 'Position' and support the 'Nullability', 'NoEscape', and 'Type' keys.
321 Using 'Parameters' within a parameter entry to describe the parameters of a
322 block is not implemented. Use 'Type' on the entire parameter instead.
326 - Selector: "isEqual:"
334 Used only for block parameters. Equivalent to ``NS_NOESCAPE``.
338 - Name: dispatch_sync
345 Used for Objective-C class types bridged to Swift value types. An empty
346 string ("") means a type is not bridged. Not supported outside of Apple
347 frameworks (the Swift side of it requires conforming to implementation-detail
348 protocols that are subject to change).
353 SwiftBridge: IndexSet
357 Used for init methods. Equivalent to ``NS_DESIGNATED_INITIALIZER``.
361 - Selector: "initWithFrame:"