29
29
#include " GenHeap.h"
30
30
#include " GenProto.h"
31
31
#include " IRGenModule.h"
32
+ #include " Linking.h"
32
33
#include " LoadableTypeInfo.h"
33
34
34
35
using namespace swift ;
@@ -276,13 +277,13 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
276
277
if (!init)
277
278
return nullptr ;
278
279
279
- auto var = new llvm::GlobalVariable (*IGM.getModule (), init->getType (),
280
- /* isConstant*/ true ,
281
- llvm::GlobalValue::PrivateLinkage,
282
- init,
283
- " \x01 l__swift3_assocty_metadata" );
280
+ auto entity = LinkEntity::forReflectionAssociatedTypeDescriptor (Conformance);
281
+ auto info = LinkInfo::get (IGM, entity, ForDefinition);
282
+
283
+ auto var = info.createVariable (IGM, init->getType (), Alignment (4 ));
284
+ var->setConstant (true );
285
+ var->setInitializer (init);
284
286
var->setSection (IGM.getAssociatedTypeMetadataSectionName ());
285
- var->setAlignment (4 );
286
287
287
288
auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
288
289
tempBase->replaceAllUsesWith (replacer);
@@ -444,13 +445,14 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
444
445
if (!init)
445
446
return nullptr ;
446
447
447
- auto var = new llvm::GlobalVariable (*IGM.getModule (), init->getType (),
448
- /* isConstant*/ true ,
449
- llvm::GlobalValue::PrivateLinkage,
450
- init,
451
- " \x01 l__swift3_reflection_metadata" );
448
+ auto entity = LinkEntity::forReflectionFieldDescriptor (
449
+ NTD->getDeclaredType ()->getCanonicalType ());
450
+ auto info = LinkInfo::get (IGM, entity, ForDefinition);
451
+
452
+ auto var = info.createVariable (IGM, init->getType (), Alignment (4 ));
453
+ var->setConstant (true );
454
+ var->setInitializer (init);
452
455
var->setSection (IGM.getFieldTypeMetadataSectionName ());
453
- var->setAlignment (4 );
454
456
455
457
auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
456
458
tempBase->replaceAllUsesWith (replacer);
@@ -460,61 +462,57 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
460
462
};
461
463
462
464
class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
463
- void addFixedType (Module *module , CanType type,
464
- const FixedTypeInfo &ti) {
465
- addTypeRef ( module , type) ;
465
+ ModuleDecl *module ;
466
+ CanType type;
467
+ const FixedTypeInfo *ti ;
466
468
467
- addConstantInt32 (ti.getFixedSize ().getValue ());
468
- addConstantInt32 (ti.getFixedAlignment ().getValue ());
469
- addConstantInt32 (ti.getFixedStride ().getValue ());
470
- addConstantInt32 (ti.getFixedExtraInhabitantCount (IGM));
471
- }
472
-
473
- void addBuiltinType (CanType builtinType) {
474
- auto &ti = cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered (builtinType));
475
- addFixedType (builtinType->getASTContext ().TheBuiltinModule , builtinType, ti);
476
- }
477
-
478
- void addOpaqueType (const NominalTypeDecl *nominalDecl) {
479
- auto &ti = cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered (
469
+ public:
470
+ FixedTypeMetadataBuilder (IRGenModule &IGM,
471
+ CanType builtinType)
472
+ : ReflectionMetadataBuilder(IGM) {
473
+ module = builtinType->getASTContext ().TheBuiltinModule ;
474
+ type = builtinType;
475
+ ti = &cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered (builtinType));
476
+ }
477
+
478
+ FixedTypeMetadataBuilder (IRGenModule &IGM,
479
+ const NominalTypeDecl *nominalDecl)
480
+ : ReflectionMetadataBuilder(IGM) {
481
+ module = nominalDecl->getParentModule ();
482
+ type = nominalDecl->getDeclaredType ()->getCanonicalType ();
483
+ ti = &cast<FixedTypeInfo>(IGM.getTypeInfoForUnlowered (
480
484
nominalDecl->getDeclaredTypeInContext ()->getCanonicalType ()));
481
-
482
- addFixedType (nominalDecl->getParentModule (),
483
- nominalDecl->getDeclaredType ()->getCanonicalType (), ti);
484
485
}
485
486
486
487
void layout () {
487
- for (auto builtinType : IGM.BuiltinTypes )
488
- addBuiltinType (builtinType);
488
+ addTypeRef (module , type);
489
489
490
- for (auto nominalDecl : IGM.OpaqueTypes )
491
- addOpaqueType (nominalDecl);
490
+ addConstantInt32 (ti->getFixedSize ().getValue ());
491
+ addConstantInt32 (ti->getFixedAlignment ().getValue ());
492
+ addConstantInt32 (ti->getFixedStride ().getValue ());
493
+ addConstantInt32 (ti->getFixedExtraInhabitantCount (IGM));
492
494
}
493
495
494
- public:
495
- FixedTypeMetadataBuilder (IRGenModule &IGM)
496
- : ReflectionMetadataBuilder(IGM) {}
497
-
498
496
llvm::GlobalVariable *emit () {
499
-
500
497
auto tempBase = std::unique_ptr<llvm::GlobalVariable>(
501
498
new llvm::GlobalVariable (IGM.Int8Ty , /* isConstant*/ true ,
502
499
llvm::GlobalValue::PrivateLinkage));
503
500
setRelativeAddressBase (tempBase.get ());
504
501
505
502
layout ();
503
+
506
504
auto init = getInit ();
507
505
508
506
if (!init)
509
507
return nullptr ;
510
508
511
- auto var = new llvm::GlobalVariable (*IGM.getModule (), init->getType (),
512
- /* isConstant*/ true ,
513
- llvm::GlobalValue::PrivateLinkage,
514
- init,
515
- " \x01 l__swift3_builtin_metadata" );
509
+ auto entity = LinkEntity::forReflectionBuiltinDescriptor (type);
510
+ auto info = LinkInfo::get (IGM, entity, ForDefinition);
511
+
512
+ auto var = info.createVariable (IGM, init->getType (), Alignment (4 ));
513
+ var->setConstant (true );
514
+ var->setInitializer (init);
516
515
var->setSection (IGM.getBuiltinTypeMetadataSectionName ());
517
- var->setAlignment (4 );
518
516
519
517
auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
520
518
tempBase->replaceAllUsesWith (replacer);
@@ -523,6 +521,20 @@ class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
523
521
}
524
522
};
525
523
524
+ void IRGenModule::emitBuiltinTypeMetadataRecord (CanType builtinType) {
525
+ FixedTypeMetadataBuilder builder (*this , builtinType);
526
+ auto var = builder.emit ();
527
+ if (var)
528
+ addUsedGlobal (var);
529
+ }
530
+
531
+ void IRGenModule::emitOpaqueTypeMetadataRecord (const NominalTypeDecl *nominalDecl) {
532
+ FixedTypeMetadataBuilder builder (*this , nominalDecl);
533
+ auto var = builder.emit ();
534
+ if (var)
535
+ addUsedGlobal (var);
536
+ }
537
+
526
538
// / Builds a constant LLVM struct describing the layout of a fixed-size
527
539
// / SIL @box. These look like closure contexts, but without any necessary
528
540
// / bindings or metadata sources, and only a single captured value.
@@ -915,10 +927,17 @@ void IRGenModule::emitBuiltinReflectionMetadata() {
915
927
for (auto PD : ImportedProtocols)
916
928
emitFieldMetadataRecord (PD);
917
929
918
- FixedTypeMetadataBuilder builder (*this );
919
- auto var = builder.emit ();
920
- if (var)
921
- addUsedGlobal (var);
930
+ for (auto builtinType : BuiltinTypes)
931
+ emitBuiltinTypeMetadataRecord (builtinType);
932
+
933
+ for (auto nominalDecl : OpaqueTypes)
934
+ emitOpaqueTypeMetadataRecord (nominalDecl);
935
+ }
936
+
937
+ void IRGenerator::emitBuiltinReflectionMetadata () {
938
+ for (auto &m : *this ) {
939
+ m.second ->emitBuiltinReflectionMetadata ();
940
+ }
922
941
}
923
942
924
943
void IRGenModule::emitFieldMetadataRecord (const NominalTypeDecl *Decl) {
@@ -941,3 +960,9 @@ void IRGenModule::emitReflectionMetadataVersion() {
941
960
Version->setVisibility (llvm::GlobalValue::HiddenVisibility);
942
961
addUsedGlobal (Version);
943
962
}
963
+
964
+ void IRGenerator::emitReflectionMetadataVersion () {
965
+ for (auto &m : *this ) {
966
+ m.second ->emitReflectionMetadataVersion ();
967
+ }
968
+ }
0 commit comments