@@ -134,8 +134,10 @@ class PolymorphicConvention {
134134
135135private:
136136 void initGenerics ();
137- void considerNewTypeSource (MetadataSource::Kind kind, unsigned paramIndex,
138- CanType type, IsExact_t isExact);
137+
138+ template <typename ...Args>
139+ void considerNewTypeSource (IsExact_t isExact, MetadataSource::Kind kind,
140+ CanType type, Args... args);
139141 bool considerType (CanType type, IsExact_t isExact,
140142 unsigned sourceIndex, MetadataPath &&path);
141143
@@ -234,12 +236,17 @@ PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,
234236
235237void PolymorphicConvention::addPseudogenericFulfillments () {
236238 enumerateRequirements ([&](GenericRequirement reqt) {
237- MetadataPath path;
238- path.addImpossibleComponent ();
239+ auto archetype = Generics->getGenericEnvironment ()
240+ ->mapTypeIntoContext (reqt.TypeParameter )
241+ ->getAs <ArchetypeType>();
242+ assert (archetype && " did not get an archetype by mapping param?" );
243+ auto erasedTypeParam = archetype->getExistentialType ()->getCanonicalType ();
244+ Sources.emplace_back (MetadataSource::Kind::ErasedTypeMetadata,
245+ reqt.TypeParameter , erasedTypeParam);
239246
240- unsigned sourceIndex = 0 ; // unimportant, since impossible
247+ MetadataPath path;
241248 Fulfillments.addFulfillment ({reqt.TypeParameter , reqt.Protocol },
242- sourceIndex , std::move (path),
249+ Sources. size () - 1 , std::move (path),
243250 MetadataState::Complete);
244251 });
245252}
@@ -302,14 +309,15 @@ void PolymorphicConvention::initGenerics() {
302309 Generics = FnType->getInvocationGenericSignature ();
303310}
304311
305- void PolymorphicConvention::considerNewTypeSource (MetadataSource::Kind kind,
306- unsigned paramIndex,
312+ template <typename ...Args>
313+ void PolymorphicConvention::considerNewTypeSource (IsExact_t isExact,
314+ MetadataSource::Kind kind,
307315 CanType type,
308- IsExact_t isExact ) {
316+ Args... args ) {
309317 if (!Fulfillments.isInterestingTypeForFulfillments (type)) return ;
310318
311319 // Prospectively add a source.
312- Sources.emplace_back (kind, paramIndex, type );
320+ Sources.emplace_back (kind, type, std::forward<Args>(args)... );
313321
314322 // Consider the source.
315323 if (!considerType (type, isExact, Sources.size () - 1 , MetadataPath ())) {
@@ -333,9 +341,7 @@ void PolymorphicConvention::considerWitnessSelf(CanSILFunctionType fnType) {
333341 auto conformance = fnType->getWitnessMethodConformanceOrInvalid ();
334342
335343 // First, bind type metadata for Self.
336- Sources.emplace_back (MetadataSource::Kind::SelfMetadata,
337- MetadataSource::InvalidSourceIndex,
338- selfTy);
344+ Sources.emplace_back (MetadataSource::Kind::SelfMetadata, selfTy);
339345
340346 if (selfTy->is <GenericTypeParamType>()) {
341347 // The Self type is abstract, so we can fulfill its metadata from
@@ -347,8 +353,7 @@ void PolymorphicConvention::considerWitnessSelf(CanSILFunctionType fnType) {
347353
348354 // The witness table for the Self : P conformance can be
349355 // fulfilled from the Self witness table parameter.
350- Sources.emplace_back (MetadataSource::Kind::SelfWitnessTable,
351- MetadataSource::InvalidSourceIndex, selfTy);
356+ Sources.emplace_back (MetadataSource::Kind::SelfWitnessTable, selfTy);
352357 addSelfWitnessTableFulfillment (selfTy, conformance);
353358}
354359
@@ -359,8 +364,7 @@ void PolymorphicConvention::considerObjCGenericSelf(CanSILFunctionType fnType) {
359364 unsigned paramIndex = fnType->getParameters ().size () - 1 ;
360365
361366 // Bind type metadata for Self.
362- Sources.emplace_back (MetadataSource::Kind::ClassPointer, paramIndex,
363- selfTy);
367+ Sources.emplace_back (MetadataSource::Kind::ClassPointer, selfTy, paramIndex);
364368
365369 if (isa<GenericTypeParamType>(selfTy))
366370 addSelfMetadataFulfillment (selfTy);
@@ -385,8 +389,9 @@ void PolymorphicConvention::considerParameter(SILParameterInfo param,
385389 case ParameterConvention::Indirect_InoutAliasable:
386390 if (!isSelfParameter) return ;
387391 if (type->getNominalOrBoundGenericNominal ()) {
388- considerNewTypeSource (MetadataSource::Kind::GenericLValueMetadata,
389- paramIndex, type, IsExact);
392+ considerNewTypeSource (IsExact,
393+ MetadataSource::Kind::GenericLValueMetadata,
394+ type, paramIndex);
390395 }
391396 return ;
392397
@@ -395,15 +400,15 @@ void PolymorphicConvention::considerParameter(SILParameterInfo param,
395400 case ParameterConvention::Direct_Guaranteed:
396401 // Classes are sources of metadata.
397402 if (type->getClassOrBoundGenericClass ()) {
398- considerNewTypeSource (MetadataSource::Kind::ClassPointer,
399- paramIndex, type, IsInexact );
403+ considerNewTypeSource (IsInexact, MetadataSource::Kind::ClassPointer,
404+ type, paramIndex );
400405 return ;
401406 }
402407
403408 if (isa<GenericTypeParamType>(type)) {
404409 if (auto superclassTy = getSuperclassBound (type)) {
405- considerNewTypeSource (MetadataSource::Kind::ClassPointer,
406- paramIndex, superclassTy, IsInexact );
410+ considerNewTypeSource (IsInexact, MetadataSource::Kind::ClassPointer,
411+ superclassTy, paramIndex );
407412 return ;
408413
409414 }
@@ -418,11 +423,11 @@ void PolymorphicConvention::considerParameter(SILParameterInfo param,
418423 // sources of metadata.
419424 CanType objTy = metatypeTy.getInstanceType ();
420425 if (auto classDecl = objTy->getClassOrBoundGenericClass ())
421- if (classDecl->usesObjCGenericsModel ())
426+ if (classDecl->isTypeErasedGenericClass ())
422427 return ;
423428
424- considerNewTypeSource (MetadataSource::Kind::Metadata,
425- paramIndex, objTy, IsInexact );
429+ considerNewTypeSource (IsInexact, MetadataSource::Kind::Metadata, objTy ,
430+ paramIndex);
426431 return ;
427432 }
428433
@@ -599,6 +604,17 @@ void EmitPolymorphicParameters::bindExtraSource(
599604 }
600605 return ;
601606 }
607+
608+ case MetadataSource::Kind::ErasedTypeMetadata: {
609+ ArtificialLocation Loc (IGF.getDebugScope (), IGF.IGM .DebugInfo .get (),
610+ IGF.Builder );
611+ CanType argTy = getTypeInContext (source.Type );
612+ llvm::Value *metadata = IGF.emitTypeMetadataRef (source.getFixedType ());
613+ setTypeMetadataName (IGF.IGM , metadata, argTy);
614+ IGF.bindLocalTypeDataFromTypeMetadata (argTy, IsExact, metadata,
615+ MetadataState::Complete);
616+ return ;
617+ }
602618 }
603619 llvm_unreachable (" bad source kind!" );
604620}
@@ -2986,6 +3002,10 @@ namespace {
29863002 case MetadataSource::Kind::SelfMetadata:
29873003 case MetadataSource::Kind::SelfWitnessTable:
29883004 continue ;
3005+
3006+ // No influence on the arguments.
3007+ case MetadataSource::Kind::ErasedTypeMetadata:
3008+ continue ;
29893009 }
29903010 llvm_unreachable (" bad source kind!" );
29913011 }
@@ -3040,6 +3060,10 @@ void EmitPolymorphicArguments::emit(SubstitutionMap subs,
30403060 // Added later.
30413061 continue ;
30423062 }
3063+
3064+ case MetadataSource::Kind::ErasedTypeMetadata:
3065+ // No influence on the arguments.
3066+ continue ;
30433067 }
30443068 llvm_unreachable (" bad source kind" );
30453069 }
@@ -3098,6 +3122,10 @@ NecessaryBindings NecessaryBindings::computeBindings(
30983122 case MetadataSource::Kind::SelfWitnessTable:
30993123 // We'll just pass undef in cases like this.
31003124 continue ;
3125+
3126+ case MetadataSource::Kind::ErasedTypeMetadata:
3127+ // Fixed in the body.
3128+ continue ;
31013129 }
31023130 llvm_unreachable (" bad source kind" );
31033131 }
@@ -3320,6 +3348,8 @@ namespace {
33203348 case MetadataSource::Kind::SelfMetadata:
33213349 case MetadataSource::Kind::SelfWitnessTable:
33223350 return ; // handled as a special case in expand()
3351+ case MetadataSource::Kind::ErasedTypeMetadata:
3352+ return ; // fixed in the body
33233353 }
33243354 llvm_unreachable (" bad source kind" );
33253355 }
0 commit comments