-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathClassMetadataVisitor.h
340 lines (293 loc) · 11.4 KB
/
ClassMetadataVisitor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//===--- ClassMetadataVisitor.h - CRTP for class metadata -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A CRTP helper class for visiting all of the known fields in a class
// metadata object.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_CLASSMETADATAVISITOR_H
#define SWIFT_IRGEN_CLASSMETADATAVISITOR_H
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILModule.h"
#include "swift/SIL/SILVTable.h"
#include "swift/SIL/SILVTableVisitor.h"
#include "IRGen.h"
#include "Field.h"
#include "NominalMetadataVisitor.h"
namespace swift {
namespace irgen {
class IRGenModule;
/// Returns true if the given SILVTable entry needs to be reified as a runtime
/// vtable entry.
///
/// Methods that have no overrides, and no ABI constraints that require a
/// vtable to be present, can be left out of the runtime vtable for classes.
bool methodRequiresReifiedVTableEntry(IRGenModule &IGM,
const SILVTable *vtable,
SILDeclRef method);
/// A CRTP class for laying out class metadata. Note that this does
/// *not* handle the metadata template stuff.
template <class Impl> class ClassMetadataVisitor
: public NominalMetadataVisitor<Impl>,
public SILVTableVisitor<Impl> {
using super = NominalMetadataVisitor<Impl>;
protected:
using super::IGM;
using super::asImpl;
/// The most-derived class.
ClassDecl *const Target;
/// SILVTable entry for the class.
const SILVTable *VTable;
ClassMetadataVisitor(IRGenModule &IGM, ClassDecl *target)
: super(IGM), Target(target),
VTable(IGM.getSILModule().lookUpVTable(target, /*deserialize*/ false)) {}
ClassMetadataVisitor(IRGenModule &IGM, ClassDecl *target, SILVTable *vtable)
: super(IGM), Target(target), VTable(vtable) {}
public:
bool isPureObjC() const {
return Target->getObjCImplementationDecl();
}
// Layout in embedded mode while considering the class type.
// This is important for adding the right superclass pointer.
// The regular `layout` method can be used for layout tasks for which the
// actual superclass pointer is not relevant.
void layoutEmbedded(CanType classTy) {
asImpl().noteAddressPoint();
asImpl().addEmbeddedSuperclass(classTy);
asImpl().addDestructorFunction();
asImpl().addIVarDestroyer();
addEmbeddedClassMembers(Target);
}
void layout() {
static_assert(MetadataAdjustmentIndex::Class == 3,
"Adjustment index must be synchronized with this layout");
if (IGM.Context.LangOpts.hasFeature(Feature::Embedded)) {
asImpl().noteAddressPoint();
asImpl().addSuperclass();
asImpl().addDestructorFunction();
asImpl().addIVarDestroyer();
addEmbeddedClassMembers(Target);
return;
}
if (isPureObjC()) {
assert(IGM.ObjCInterop);
asImpl().noteAddressPoint();
asImpl().addMetadataFlags();
asImpl().addSuperclass();
asImpl().addClassCacheData();
asImpl().addClassDataPointer();
return;
}
// Pointer to layout string
asImpl().addLayoutStringPointer();
// HeapMetadata header.
asImpl().addDestructorFunction();
// Metadata header.
super::layout();
// ClassMetadata header. This must be layout-compatible with Objective-C
// classes when interoperability is enabled.
asImpl().addSuperclass();
if (IGM.ObjCInterop) {
asImpl().addClassCacheData();
asImpl().addClassDataPointer();
}
asImpl().addClassFlags();
asImpl().addInstanceAddressPoint();
asImpl().addInstanceSize();
asImpl().addInstanceAlignMask();
asImpl().addRuntimeReservedBits();
asImpl().addClassSize();
asImpl().addClassAddressPoint();
asImpl().addNominalTypeDescriptor();
asImpl().addIVarDestroyer();
// Class members.
addClassMembers(Target, Target);
}
/// Notes the beginning of the field offset vector for a particular ancestor
/// of a generic-layout class.
void noteStartOfFieldOffsets(ClassDecl *whichClass) {}
/// Notes the end of the field offset vector for a particular ancestor
/// of a generic-layout class.
void noteEndOfFieldOffsets(ClassDecl *whichClass) {}
/// Notes the existence of a formally virtual method that has been elided from the
/// reified vtable because it has no overrides.
void noteNonoverriddenMethod(SILDeclRef method) {}
private:
/// Add fields associated with the given class and its bases.
void addClassMembers(ClassDecl *theClass,
ClassDecl *rootClass) {
// Visit the superclass first.
if (auto *superclassDecl = theClass->getSuperclassDecl()) {
if (superclassDecl->hasClangNode()) {
// Nothing to do; Objective-C classes do not add new members to
// Swift class metadata.
// Super class metadata is resilient if
// the superclass is resilient when viewed from the current module.
} else if (IGM.hasResilientMetadata(superclassDecl,
ResilienceExpansion::Maximal,
rootClass)) {
// Runtime metadata instantiation will initialize our field offset
// vector and vtable entries.
//
// Metadata access needs to access our fields relative to a
// global variable.
asImpl().noteResilientSuperclass();
} else {
// NB: We don't apply superclass substitutions to members because we want
// consistent metadata layout between generic superclasses and concrete
// subclasses.
addClassMembers(superclassDecl,
rootClass);
}
}
// Note that we have to emit a global variable storing the metadata
// start offset, or access remaining fields relative to one.
asImpl().noteStartOfImmediateMembers(theClass);
// Add space for the generic parameters, if applicable.
// This must always be the first item in the immediate members.
asImpl().addGenericFields(theClass, theClass);
// If the class has resilient storage, we cannot make any assumptions about
// its storage layout, so skip the rest of this method.
if (IGM.isResilient(theClass, ResilienceExpansion::Maximal,
rootClass))
return;
// A class only really *needs* a field-offset vector in the
// metadata if:
// - it's in a generic context and
// - there might exist a context which
// - can access the class's field storage directly and
// - sees the class as having a possibly dependent layout.
//
// A context which knows that the class does not have a dependent
// layout should be able to just use a direct field offset
// (possibly a constant one).
//
// But we currently always give classes field-offset vectors,
// whether they need them or not.
asImpl().noteStartOfFieldOffsets(theClass);
forEachField(IGM, theClass, [&](Field field) {
asImpl().addFieldEntries(field);
});
asImpl().noteEndOfFieldOffsets(theClass);
// If the class has resilient metadata, we cannot make any assumptions
// about its metadata layout, so skip the rest of this method.
if (IGM.hasResilientMetadata(theClass, ResilienceExpansion::Maximal,
rootClass))
return;
// Add vtable entries.
asImpl().addVTableEntries(theClass);
}
/// Add fields associated with the given class and its bases.
void addEmbeddedClassMembers(ClassDecl *theClass) {
// Visit the superclass first.
if (auto *superclassDecl = theClass->getSuperclassDecl()) {
addEmbeddedClassMembers(superclassDecl);
}
// Note that we have to emit a global variable storing the metadata
// start offset, or access remaining fields relative to one.
asImpl().noteStartOfImmediateMembers(theClass);
// Add vtable entries.
asImpl().addVTableEntries(theClass);
}
friend SILVTableVisitor<Impl>;
void addMethod(SILDeclRef declRef) {
// Does this method require a reified runtime vtable entry?
if (!VTable || methodRequiresReifiedVTableEntry(IGM, VTable, declRef)) {
asImpl().addReifiedVTableEntry(declRef);
} else {
asImpl().noteNonoverriddenMethod(declRef);
}
}
void addFieldEntries(Field field) {
switch (field.getKind()) {
case Field::Var:
asImpl().addFieldOffset(field.getVarDecl());
return;
case Field::MissingMember:
asImpl().addFieldOffsetPlaceholders(field.getMissingMemberDecl());
return;
case Field::DefaultActorStorage:
asImpl().addDefaultActorStorageFieldOffset();
return;
case Field::NonDefaultDistributedActorStorage:
asImpl().addNonDefaultDistributedActorStorageFieldOffset();
return;
}
}
};
/// An "implementation" of ClassMetadataVisitor that just scans through
/// the metadata layout, maintaining the offset of the next field.
template <class Impl>
class ClassMetadataScanner : public ClassMetadataVisitor<Impl> {
using super = ClassMetadataVisitor<Impl>;
protected:
Size NextOffset = Size(0);
ClassMetadataScanner(IRGenModule &IGM, ClassDecl *target)
: super(IGM, target) {}
public:
void addMetadataFlags() { addPointer(); }
void addNominalTypeDescriptor() { addPointer(); }
void addIVarDestroyer() { addPointer(); }
void addValueWitnessTable() { addPointer(); }
void addLayoutStringPointer() { addPointer(); }
void addDestructorFunction() { addPointer(); }
void addSuperclass() { addPointer(); }
void addClassFlags() { addInt32(); }
void addInstanceAddressPoint() { addInt32(); }
void addInstanceSize() { addInt32(); }
void addInstanceAlignMask() { addInt16(); }
void addRuntimeReservedBits() { addInt16(); }
void addClassSize() { addInt32(); }
void addClassAddressPoint() { addInt32(); }
void addClassCacheData() { addPointer(); addPointer(); }
void addClassDataPointer() { addPointer(); }
void addReifiedVTableEntry(SILDeclRef declRef) {
addPointer();
}
void addMethodOverride(SILDeclRef baseRef, SILDeclRef declRef) {}
void addDefaultActorStorageFieldOffset() { addPointer(); }
void addNonDefaultDistributedActorStorageFieldOffset() { addPointer(); }
void addFieldOffset(VarDecl *var) { addPointer(); }
void addFieldOffsetPlaceholders(MissingMemberDecl *mmd) {
for (unsigned i = 0, e = mmd->getNumberOfFieldOffsetVectorEntries();
i < e; ++i) {
addPointer();
}
}
void addGenericRequirement(GenericRequirement requirement, ClassDecl *forClass) {
addPointer();
}
void addPlaceholder(MissingMemberDecl *MMD) {
for (auto i : range(MMD->getNumberOfVTableEntries())) {
(void)i;
addPointer();
}
}
private:
// Our layout here assumes that there will never be unclaimed space
// in the metadata.
void addPointer() {
NextOffset += super::IGM.getPointerSize();
}
void addInt32() {
NextOffset += Size(4);
}
void addInt16() {
NextOffset += Size(2);
}
};
} // end namespace irgen
} // end namespace swift
#endif