2929#include " GenHeap.h"
3030#include " GenProto.h"
3131#include " IRGenModule.h"
32+ #include " Linking.h"
3233#include " LoadableTypeInfo.h"
3334
3435using namespace swift ;
@@ -276,13 +277,13 @@ class AssociatedTypeMetadataBuilder : public ReflectionMetadataBuilder {
276277 if (!init)
277278 return nullptr ;
278279
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);
284286 var->setSection (IGM.getAssociatedTypeMetadataSectionName ());
285- var->setAlignment (4 );
286287
287288 auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
288289 tempBase->replaceAllUsesWith (replacer);
@@ -444,13 +445,14 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
444445 if (!init)
445446 return nullptr ;
446447
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);
452455 var->setSection (IGM.getFieldTypeMetadataSectionName ());
453- var->setAlignment (4 );
454456
455457 auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
456458 tempBase->replaceAllUsesWith (replacer);
@@ -460,61 +462,57 @@ class FieldTypeMetadataBuilder : public ReflectionMetadataBuilder {
460462};
461463
462464class 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 ;
466468
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 (
480484 nominalDecl->getDeclaredTypeInContext ()->getCanonicalType ()));
481-
482- addFixedType (nominalDecl->getParentModule (),
483- nominalDecl->getDeclaredType ()->getCanonicalType (), ti);
484485 }
485486
486487 void layout () {
487- for (auto builtinType : IGM.BuiltinTypes )
488- addBuiltinType (builtinType);
488+ addTypeRef (module , type);
489489
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));
492494 }
493495
494- public:
495- FixedTypeMetadataBuilder (IRGenModule &IGM)
496- : ReflectionMetadataBuilder(IGM) {}
497-
498496 llvm::GlobalVariable *emit () {
499-
500497 auto tempBase = std::unique_ptr<llvm::GlobalVariable>(
501498 new llvm::GlobalVariable (IGM.Int8Ty , /* isConstant*/ true ,
502499 llvm::GlobalValue::PrivateLinkage));
503500 setRelativeAddressBase (tempBase.get ());
504501
505502 layout ();
503+
506504 auto init = getInit ();
507505
508506 if (!init)
509507 return nullptr ;
510508
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);
516515 var->setSection (IGM.getBuiltinTypeMetadataSectionName ());
517- var->setAlignment (4 );
518516
519517 auto replacer = llvm::ConstantExpr::getBitCast (var, IGM.Int8PtrTy );
520518 tempBase->replaceAllUsesWith (replacer);
@@ -523,6 +521,20 @@ class FixedTypeMetadataBuilder : public ReflectionMetadataBuilder {
523521 }
524522};
525523
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+
526538// / Builds a constant LLVM struct describing the layout of a fixed-size
527539// / SIL @box. These look like closure contexts, but without any necessary
528540// / bindings or metadata sources, and only a single captured value.
@@ -915,10 +927,17 @@ void IRGenModule::emitBuiltinReflectionMetadata() {
915927 for (auto PD : ImportedProtocols)
916928 emitFieldMetadataRecord (PD);
917929
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+ }
922941}
923942
924943void IRGenModule::emitFieldMetadataRecord (const NominalTypeDecl *Decl) {
@@ -941,3 +960,9 @@ void IRGenModule::emitReflectionMetadataVersion() {
941960 Version->setVisibility (llvm::GlobalValue::HiddenVisibility);
942961 addUsedGlobal (Version);
943962}
963+
964+ void IRGenerator::emitReflectionMetadataVersion () {
965+ for (auto &m : *this ) {
966+ m.second ->emitReflectionMetadataVersion ();
967+ }
968+ }
0 commit comments