@@ -174,6 +174,35 @@ IRGenModule::getTypeRef(Type type, GenericSignature *genericSig,
174
174
return getTypeRef (type->getCanonicalType (genericSig), role);
175
175
}
176
176
177
+ std::pair<llvm::Constant *, unsigned >
178
+ IRGenModule::getLoweredTypeRef (SILType loweredType,
179
+ CanGenericSignature genericSig,
180
+ MangledTypeRefRole role) {
181
+ auto type =
182
+ substOpaqueTypesWithUnderlyingTypes (loweredType, genericSig).getASTType ();
183
+
184
+ switch (role) {
185
+ case MangledTypeRefRole::DefaultAssociatedTypeWitness:
186
+ case MangledTypeRefRole::Metadata:
187
+ // Note that we're using all of the nominal types referenced by this type,
188
+ // ensuring that we can always reconstruct type metadata from a mangled name
189
+ // in-process.
190
+ IRGen.noteUseOfTypeMetadata (type);
191
+ break ;
192
+
193
+ case MangledTypeRefRole::Reflection:
194
+ // For reflection records only used for out-of-process reflection, we do not
195
+ // need to force emission of runtime type metadata.
196
+ IRGen.noteUseOfFieldDescriptors (type);
197
+ break ;
198
+ }
199
+
200
+ IRGenMangler Mangler;
201
+ auto SymbolicName = Mangler.mangleTypeForReflection (*this , type);
202
+ return {getAddrOfStringForTypeRef (SymbolicName, role),
203
+ SymbolicName.runtimeSizeInBytes ()};
204
+ }
205
+
177
206
std::pair<llvm::Constant *, unsigned >
178
207
IRGenModule::getTypeRef (CanType type, MangledTypeRefRole role) {
179
208
type = substOpaqueTypesWithUnderlyingTypes (type);
@@ -399,6 +428,15 @@ class ReflectionMetadataBuilder {
399
428
addBuiltinTypeRefs (type);
400
429
}
401
430
431
+ void
432
+ addLoweredTypeRef (SILType loweredType,
433
+ CanGenericSignature genericSig,
434
+ MangledTypeRefRole role = MangledTypeRefRole::Reflection) {
435
+ B.addRelativeAddress (
436
+ IGM.getLoweredTypeRef (loweredType, genericSig, role).first );
437
+ addBuiltinTypeRefs (loweredType.getASTType ());
438
+ }
439
+
402
440
// / Add a 32-bit relative offset to a mangled nominal type string
403
441
// / in the typeref reflection section.
404
442
// /
@@ -725,17 +763,20 @@ void IRGenModule::emitBuiltinTypeMetadataRecord(CanType builtinType) {
725
763
// / SIL @box. These look like closure contexts, but without any necessary
726
764
// / bindings or metadata sources, and only a single captured value.
727
765
class BoxDescriptorBuilder : public ReflectionMetadataBuilder {
728
- CanType BoxedType;
766
+ SILType BoxedType;
767
+ CanGenericSignature genericSig;
729
768
public:
730
- BoxDescriptorBuilder (IRGenModule &IGM, CanType BoxedType)
731
- : ReflectionMetadataBuilder(IGM), BoxedType(BoxedType) {}
732
-
769
+ BoxDescriptorBuilder (IRGenModule &IGM, SILType BoxedType,
770
+ CanGenericSignature genericSig)
771
+ : ReflectionMetadataBuilder(IGM), BoxedType(BoxedType),
772
+ genericSig (genericSig) {}
773
+
733
774
void layout () override {
734
775
B.addInt32 (1 );
735
776
B.addInt32 (0 ); // Number of sources
736
777
B.addInt32 (0 ); // Number of generic bindings
737
778
738
- addTypeRef (BoxedType);
779
+ addLoweredTypeRef (BoxedType, genericSig );
739
780
}
740
781
741
782
llvm::GlobalVariable *emit () {
@@ -896,8 +937,8 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
896
937
897
938
// / Get the interface types of all of the captured values, mapped out of the
898
939
// / context of the callee we're partially applying.
899
- std::vector<CanType > getCaptureTypes () {
900
- std::vector<CanType > CaptureTypes;
940
+ std::vector<SILType > getCaptureTypes () {
941
+ std::vector<SILType > CaptureTypes;
901
942
902
943
for (auto ElementType : getElementTypes ()) {
903
944
auto SwiftType = ElementType.getASTType ();
@@ -914,7 +955,8 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
914
955
}
915
956
916
957
auto InterfaceType = SwiftType->mapTypeOutOfContext ();
917
- CaptureTypes.push_back (InterfaceType->getCanonicalType ());
958
+ CaptureTypes.push_back (
959
+ SILType::getPrimitiveObjectType (InterfaceType->getCanonicalType ()));
918
960
}
919
961
920
962
return CaptureTypes;
@@ -930,8 +972,11 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
930
972
931
973
// Now add typerefs of all of the captures.
932
974
for (auto CaptureType : CaptureTypes) {
933
- addTypeRef (CaptureType);
934
- addBuiltinTypeRefs (CaptureType);
975
+ addLoweredTypeRef (
976
+ CaptureType,
977
+ OrigCalleeType->getGenericSignature ()
978
+ ? OrigCalleeType->getGenericSignature ()->getCanonicalSignature ()
979
+ : CanGenericSignature ());
935
980
}
936
981
937
982
// Add the pairs that make up the generic param -> metadata source map
@@ -1026,11 +1071,12 @@ llvm::Constant *IRGenModule::getAddrOfFieldName(StringRef Name) {
1026
1071
}
1027
1072
1028
1073
llvm::Constant *
1029
- IRGenModule::getAddrOfBoxDescriptor (CanType BoxedType) {
1074
+ IRGenModule::getAddrOfBoxDescriptor (SILType BoxedType,
1075
+ CanGenericSignature genericSig) {
1030
1076
if (!IRGen.Opts .EnableReflectionMetadata )
1031
1077
return llvm::Constant::getNullValue (CaptureDescriptorPtrTy);
1032
1078
1033
- BoxDescriptorBuilder builder (*this , BoxedType);
1079
+ BoxDescriptorBuilder builder (*this , BoxedType, genericSig );
1034
1080
auto var = builder.emit ();
1035
1081
1036
1082
return llvm::ConstantExpr::getBitCast (var, CaptureDescriptorPtrTy);
0 commit comments