@@ -41,19 +41,28 @@ All metadata records share a common header, with the following fields:
41
41
42
42
The current kind values are as follows:
43
43
44
+ * `Class metadata `_ has of kind of **0 ** unless the class is required to
45
+ interoperate with Objective-C. If the class is required to interoperate
46
+ with Objective-C, the kind field is instead an *isa pointer * to an
47
+ Objective-C metaclass. Such a pointer can be distinguished from an
48
+ enumerated metadata kind because it is guaranteed to have a value larger
49
+ than **4096 **. Note that this is a more basic sense of interoperation
50
+ than is meant by the ``@objc `` attribute: it is what is required to
51
+ support Objective-C message sends and retain/release. All classes are
52
+ required to interoperate with Objective-C on this level when building
53
+ for an Apple platform.
44
54
* `Struct metadata `_ has a kind of **1 **.
45
55
* `Enum metadata `_ has a kind of **2 **.
56
+ * `Optional metadata `_ has a kind of **3 **.
46
57
* **Opaque metadata ** has a kind of **8 **. This is used for compiler
47
58
``Builtin `` primitives that have no additional runtime information.
48
59
* `Tuple metadata `_ has a kind of **9 **.
49
60
* `Function metadata `_ has a kind of **10 **.
50
61
* `Protocol metadata `_ has a kind of **12 **. This is used for
51
62
protocol types, for protocol compositions, and for the ``Any `` type.
52
63
* `Metatype metadata `_ has a kind of **13 **.
53
- * `Class metadata `_, instead of a kind, has an *isa pointer * in its kind slot,
54
- pointing to the class's metaclass record. This isa pointer is guaranteed
55
- to have an integer value larger than **4096 ** and so can be discriminated
56
- from non-class kind values.
64
+ * `Objective C class wrapper metadata `_ has a kind of **14 **.
65
+ * `Existential metatype metadata `_ has a kind of **15 **.
57
66
58
67
Struct Metadata
59
68
~~~~~~~~~~~~~~~
@@ -63,19 +72,13 @@ contain the following fields:
63
72
64
73
- The `nominal type descriptor `_ is referenced at **offset 1 **.
65
74
66
- - A reference to the **parent ** metadata record is stored at **offset 2 **. For
67
- structs that are members of an enclosing nominal type, this is a reference
68
- to the enclosing type's metadata. For top-level structs, this is null.
69
-
70
- TODO: The parent pointer is currently always null.
71
-
72
- - A vector of **field offsets ** begins at **offset 3 **. For each field of the
73
- struct, in ``var `` declaration order, the field's offset in bytes from the
74
- beginning of the struct is stored as a pointer-sized integer.
75
-
76
75
- If the struct is generic, then the
77
- `generic parameter vector `_ begins at **offset 3+n **, where **n ** is the
78
- number of fields in the struct.
76
+ `generic argument vector `_ begins at **offset 2 **.
77
+
78
+ - A vector of **field offsets ** begins immediately after the generic
79
+ argument vector. For each field of the struct, in ``var `` declaration
80
+ order, the field's offset in bytes from the beginning of the struct is
81
+ stored as a pointer-sized integer.
79
82
80
83
Enum Metadata
81
84
~~~~~~~~~~~~~
@@ -85,14 +88,18 @@ contain the following fields:
85
88
86
89
- The `nominal type descriptor `_ is referenced at **offset 1 **.
87
90
88
- - A reference to the **parent ** metadata record is stored at **offset 2 **. For
89
- enums that are members of an enclosing nominal type, this is a reference to
90
- the enclosing type's metadata. For top-level enums, this is null.
91
+ - If the enum is generic, then the
92
+ `generic argument vector `_ begins at **offset 2 **.
91
93
92
- TODO: The parent pointer is currently always null.
94
+ - In certain circumstances, there is a `payload size `_ immediately following
95
+ the `generic argument vector `.
93
96
94
- - If the enum is generic, then the
95
- `generic parameter vector `_ begins at **offset 3 **.
97
+ Optional Metadata
98
+ ~~~~~~~~~~~~~~~~~
99
+
100
+ Optional metadata share the same basic layout as enum metadata. They are
101
+ distinguished from enum metadata because of the importance of the
102
+ ``Optional `` type for various reflection and dynamic-casting purposes.
96
103
97
104
Tuple Metadata
98
105
~~~~~~~~~~~~~~
@@ -102,15 +109,16 @@ contain the following fields:
102
109
103
110
- The **number of elements ** in the tuple is a pointer-sized integer at
104
111
**offset 1 **.
105
- - The **labels string ** is a pointer to a list of consecutive null-terminated
106
- label names for the tuple at **offset 2 **. Each label name is given as a
107
- null-terminated, UTF-8-encoded string in sequence. If the tuple has no
108
- labels, this is a null pointer.
109
-
110
- TODO: The labels string pointer is currently always null, and labels are
111
- not factored into tuple metadata uniquing.
112
-
113
- - The **element vector ** begins at **offset 3 ** and consists of a vector of
112
+ - The **labels string ** is a pointer to the labels for the tuple elements
113
+ at **offset 2 **. Labels are encoded in UTF-8 and separated by spaces, and
114
+ the entire string is terminated with a null character. For example, the
115
+ labels in the tuple type ``(x: Int, Int, z: Int) `` would be encoded as the
116
+ character array ``"x z \0" ``. A label (possibly zero-length) is provided
117
+ for each element of the tuple, meaning that the label string for a tuple
118
+ of **n ** elements always contains exactly **n ** spaces. If the tuple has
119
+ no labels at all, the label string is a null pointer.
120
+
121
+ - The **element vector ** begins at **offset 3 ** and consists of an array of
114
122
type-offset pairs. The metadata for the *n *\ th element's type is a pointer
115
123
at **offset 3+2*n **. The offset in bytes from the beginning of the tuple to
116
124
the beginning of the *n *\ th element is at **offset 3+2*n+1 **.
@@ -121,22 +129,20 @@ Function Metadata
121
129
In addition to the `common metadata layout `_ fields, function metadata records
122
130
contain the following fields:
123
131
124
- - The function flags are stored at **offset 1 **, information contained by function
125
- flags includes flags (8 bits) which _currently_ consists of 'throws' bit and
126
- 'parameter flags' bit, function convention (8 bits), and number of parameters (16 bits) .
127
- - A reference to the **result type ** metadata record is stored after function
128
- flags. If the function has multiple returns, this references a `tuple metadata `_
132
+ - The function flags are stored at **offset 1 **. This includes information
133
+ such as the semantic convention of the function, whether the function
134
+ throws, and how many parameters the function has .
135
+ - A reference to the **result type ** metadata record is stored at * offset 2* *.
136
+ If the function has multiple returns, this references a `tuple metadata `_
129
137
record.
130
138
- The **parameter type vector ** follows the result type and consists of
131
- NumParameters type metadata pointers corresponding to the types of the parameters.
132
- - The optional **parameter flags vector ** begins after the end of **parameter type vector **
133
- and consists of NumParameters unsigned 32-bit integer values representing flags
134
- for each parameter such as inout, __shared, variadic and possibly others. This
135
- vector is present only if the hasParameterFlags() function flag is set; otherwise
136
- all of the parameter flags are assumed to be zero.
137
-
138
- If the function takes no arguments, **parameter type vector ** as well as
139
- **parameter flags vector ** are going to be empty.
139
+ **NumParameters ** type metadata pointers corresponding to the types of the parameters.
140
+ - The optional **parameter flags vector ** begins after the end of
141
+ **parameter type vector ** and consists of **NumParameters ** 32-bit flags.
142
+ This includes information such as whether the parameter is ``inout `` or
143
+ whether it is variadic. The presence of this vector is signalled by a flag
144
+ in the function flags; if no parameter has any non-default flags, the flag
145
+ is not set.
140
146
141
147
Currently we have specialized ABI endpoints to retrieve metadata for functions
142
148
with 0/1/2/3 parameters - `swift_getFunctionTypeMetadata{0|1|2|3} ` and the general
@@ -187,6 +193,18 @@ contain the following fields:
187
193
- A reference to the metadata record for the **instance type ** that the metatype
188
194
represents is stored at **offset 1 **.
189
195
196
+ Existential Metatype Metadata
197
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198
+
199
+ In addition to the `common metadata layout `_ fields, existential metatype
200
+ metadata records contain the following fields:
201
+
202
+ - A reference to the metadata record for the **instance type ** of the metatype
203
+ is stored at **offset 1 **. This is always either an existential type
204
+ metadata or another existential metatype.
205
+
206
+ - A word of flags summarizing the existential type are stored at **offset 2 **.
207
+
190
208
Class Metadata
191
209
~~~~~~~~~~~~~~
192
210
@@ -240,7 +258,7 @@ classes.
240
258
241
259
TODO: The parent pointer is currently always null.
242
260
243
- * If the class is generic, its `generic parameter vector `_ is stored inline.
261
+ * If the class is generic, its `generic argument vector `_ is stored inline.
244
262
* The **vtable ** is stored inline and contains a function pointer to the
245
263
implementation of every method of the class in declaration order.
246
264
* If the layout of a class instance is dependent on its generic parameters,
@@ -252,11 +270,23 @@ classes.
252
270
Note that none of these fields are present for Objective-C base classes in
253
271
the inheritance hierarchy.
254
272
255
- Generic Parameter Vector
256
- ~~~~~~~~~~~~~~~~~~~~~~~~
273
+ Objective C class wrapper metadata
274
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275
+
276
+ Objective-C class wrapper metadata are used when an Objective-C ``Class ``
277
+ object is not a valid Swift type metadata.
278
+
279
+ In addition to the `common metadata layout `_ fields, Objective-C class
280
+ wrapper metadata records have the following fields:
281
+
282
+ - A ``Class `` value at **offset 1 ** which is known to not be a Swift type
283
+ metadata.
284
+
285
+ Generic Argument Vector
286
+ ~~~~~~~~~~~~~~~~~~~~~~~
257
287
258
288
Metadata records for instances of generic types contain information about their
259
- generic parameters . For each parameter of the type, a reference to the metadata
289
+ generic arguments . For each parameter of the type, a reference to the metadata
260
290
record for the type argument is stored. After all of the type argument
261
291
metadata references, for each type parameter, if there are protocol
262
292
requirements on that type parameter, a reference to the witness table for each
@@ -297,6 +327,8 @@ reference counting but are otherwise opaque to the Swift runtime.
297
327
Nominal Type Descriptor
298
328
~~~~~~~~~~~~~~~~~~~~~~~
299
329
330
+ **Warning: this is all out of date! **
331
+
300
332
The metadata records for class, struct, and enum types contain a pointer to a
301
333
**nominal type descriptor **, which contains basic information about the nominal
302
334
type such as its name, members, and metadata layout. For a generic type, one
0 commit comments