@@ -175,27 +175,34 @@ class RecordTypeInfoBuilder {
175
175
unsigned Size, Alignment, Stride, NumExtraInhabitants;
176
176
RecordKind Kind;
177
177
std::vector<FieldInfo> Fields;
178
+ bool Invalid;
178
179
179
180
public:
180
181
RecordTypeInfoBuilder (TypeConverter &TC, RecordKind Kind)
181
182
: TC(TC), Size(0 ), Alignment(1 ), Stride(0 ), NumExtraInhabitants(0 ),
182
- Kind (Kind) {}
183
+ Kind (Kind), Invalid(false ) {}
184
+
185
+ void addField (const std::string &Name, const TypeRef *TR) {
186
+ const TypeInfo *TI = TC.getTypeInfo (TR);
187
+ if (TI == nullptr ) {
188
+ Invalid = true ;
189
+ return ;
190
+ }
183
191
184
- void addField (const std::string &Name, const TypeInfo &TI) {
185
192
// FIXME: I just made this up
186
193
if (Size == 0 )
187
- NumExtraInhabitants = TI. getNumExtraInhabitants ();
194
+ NumExtraInhabitants = TI-> getNumExtraInhabitants ();
188
195
else
189
196
NumExtraInhabitants = 0 ;
190
197
191
- unsigned fieldSize = TI. getSize ();
192
- unsigned fieldAlignment = TI. getAlignment ();
198
+ unsigned fieldSize = TI-> getSize ();
199
+ unsigned fieldAlignment = TI-> getAlignment ();
193
200
194
201
// Align the current size appropriately
195
202
Size = ((Size + fieldAlignment - 1 ) & ~(fieldAlignment - 1 ));
196
203
197
204
// Add the field at the aligned offset
198
- Fields.push_back ({Name, Size, TI});
205
+ Fields.push_back ({Name, Size, TR, * TI});
199
206
200
207
// Update the aggregate size
201
208
Size += fieldSize;
@@ -208,6 +215,9 @@ class RecordTypeInfoBuilder {
208
215
}
209
216
210
217
const RecordTypeInfo *build () {
218
+ if (Invalid)
219
+ return nullptr ;
220
+
211
221
return TC.makeTypeInfo <RecordTypeInfo>(
212
222
Size, Alignment, Stride,
213
223
NumExtraInhabitants, Kind, Fields);
@@ -227,10 +237,10 @@ TypeConverter::getReferenceTypeInfo(ReferenceKind Kind,
227
237
const TypeRef *TR;
228
238
switch (Refcounting) {
229
239
case ReferenceCounting::Native:
230
- TR = BuiltinTypeRef::create (Builder, " Bo " );
240
+ TR = getNativeObjectTypeRef ( );
231
241
break ;
232
242
case ReferenceCounting::Unknown:
233
- TR = BuiltinTypeRef::create (Builder, " BO " );
243
+ TR = getUnknownObjectTypeRef ( );
234
244
break ;
235
245
}
236
246
@@ -254,26 +264,35 @@ TypeConverter::getThickFunctionTypeInfo() {
254
264
return ThickFunctionTI;
255
265
256
266
RecordTypeInfoBuilder builder (*this , RecordKind::ThickFunction);
257
- auto *TI = getRawPointerTypeInfo ();
258
- if (TI == nullptr )
259
- return nullptr ;
260
- builder.addField (" function" , *TI);
261
- TI = getReferenceTypeInfo (ReferenceKind::Strong,
262
- ReferenceCounting::Native);
263
- if (TI == nullptr )
264
- return nullptr ;
265
- builder.addField (" context" , *TI);
267
+ builder.addField (" function" , getRawPointerTypeRef ());
268
+ builder.addField (" context" , getNativeObjectTypeRef ());
266
269
ThickFunctionTI = builder.build ();
267
270
268
271
return ThickFunctionTI;
269
272
}
270
273
271
- const TypeInfo *TypeConverter::getRawPointerTypeInfo () {
272
- if (RawPointerTI != nullptr )
273
- return RawPointerTI;
274
+ const TypeRef *TypeConverter::getRawPointerTypeRef () {
275
+ if (RawPointerTR != nullptr )
276
+ return RawPointerTR;
277
+
278
+ RawPointerTR = BuiltinTypeRef::create (Builder, " Bp" );
279
+ return RawPointerTR;
280
+ }
281
+
282
+ const TypeRef *TypeConverter::getNativeObjectTypeRef () {
283
+ if (NativeObjectTR != nullptr )
284
+ return NativeObjectTR;
285
+
286
+ NativeObjectTR = BuiltinTypeRef::create (Builder, " Bo" );
287
+ return NativeObjectTR;
288
+ }
289
+
290
+ const TypeRef *TypeConverter::getUnknownObjectTypeRef () {
291
+ if (UnknownObjectTR != nullptr )
292
+ return UnknownObjectTR;
274
293
275
- RawPointerTI = getTypeInfo ( BuiltinTypeRef::create (Builder, " Bp " ) );
276
- return RawPointerTI ;
294
+ UnknownObjectTR = BuiltinTypeRef::create (Builder, " BO " );
295
+ return UnknownObjectTR ;
277
296
}
278
297
279
298
class LowerType
@@ -286,6 +305,14 @@ class LowerType
286
305
LowerType (TypeConverter &TC) : TC(TC) {}
287
306
288
307
const TypeInfo *visitBuiltinTypeRef (const BuiltinTypeRef *B) {
308
+ if (B->getMangledName () == " Bo" ) {
309
+ return TC.getReferenceTypeInfo (ReferenceKind::Strong,
310
+ ReferenceCounting::Native);
311
+ } else if (B->getMangledName () == " BO" ) {
312
+ return TC.getReferenceTypeInfo (ReferenceKind::Strong,
313
+ ReferenceCounting::Unknown);
314
+ }
315
+
289
316
auto *descriptor = TC.getBuilder ().getBuiltinTypeInfo (B);
290
317
assert (descriptor != nullptr );
291
318
return TC.makeTypeInfo <BuiltinTypeInfo>(descriptor);
@@ -303,12 +330,8 @@ class LowerType
303
330
ReferenceCounting::Native);
304
331
case FieldDescriptorKind::Struct: {
305
332
RecordTypeInfoBuilder builder (TC, RecordKind::Struct);
306
- for (auto Field : TC.getBuilder ().getFieldTypeRefs (TR, FD)) {
307
- auto *FieldTI = TC.getTypeInfo (Field.second );
308
- if (FieldTI == nullptr )
309
- return nullptr ;
310
- builder.addField (Field.first , *FieldTI);
311
- }
333
+ for (auto Field : TC.getBuilder ().getFieldTypeRefs (TR, FD))
334
+ builder.addField (Field.first , Field.second );
312
335
return builder.build ();
313
336
}
314
337
case FieldDescriptorKind::Enum: {
@@ -352,35 +375,22 @@ class LowerType
352
375
353
376
const TypeInfo *visitTupleTypeRef (const TupleTypeRef *T) {
354
377
RecordTypeInfoBuilder builder (TC, RecordKind::Tuple);
355
- for (auto Element : T->getElements ()) {
356
- auto *FieldTI = TC.getTypeInfo (Element);
357
- if (FieldTI == nullptr )
358
- return nullptr ;
359
- builder.addField (" " , *FieldTI);
360
- }
378
+ for (auto Element : T->getElements ())
379
+ builder.addField (" " , Element);
361
380
return builder.build ();
362
381
}
363
382
364
383
const TypeInfo *visitFunctionTypeRef (const FunctionTypeRef *F) {
365
384
switch (F->getFlags ().getConvention ()) {
366
- case FunctionMetadataConvention::Swift: {
367
- RecordTypeInfoBuilder builder (TC, RecordKind::ThickFunction);
368
- auto *TI = TC.getRawPointerTypeInfo ();
369
- if (TI == nullptr )
370
- return nullptr ;
371
- builder.addField (" function" , *TI);
372
- TI = TC.getReferenceTypeInfo (ReferenceKind::Strong,
373
- ReferenceCounting::Native);
374
- builder.addField (" context" , *TI);
375
- return builder.build ();
376
- }
385
+ case FunctionMetadataConvention::Swift:
386
+ return TC.getThickFunctionTypeInfo ();
377
387
case FunctionMetadataConvention::Block:
378
388
// FIXME: Native convention if blocks are ever supported on Linux?
379
389
return TC.getReferenceTypeInfo (ReferenceKind::Strong,
380
390
ReferenceCounting::Unknown);
381
391
case FunctionMetadataConvention::Thin:
382
392
case FunctionMetadataConvention::CFunctionPointer:
383
- return TC.getRawPointerTypeInfo ( );
393
+ return TC.getTypeInfo (TC. getRawPointerTypeRef () );
384
394
}
385
395
}
386
396
0 commit comments