@@ -623,49 +623,6 @@ void irgen::setProtocolWitnessTableName(IRGenModule &IGM, llvm::Value *wtable,
623623 wtable->setName (name);
624624}
625625
626- // / Return the index of the given dependent type in the list of all
627- // / dependent types.
628- // /
629- // / This will be its index in the list of substitutions.
630- static unsigned getDependentTypeIndex (CanGenericSignature generics,
631- ModuleDecl &M, CanType type) {
632- assert (type->isTypeParameter ());
633-
634- // Make a pass over all the dependent types.
635- unsigned index = 0 ;
636- for (auto depTy : generics->getAllDependentTypes ()) {
637- if (depTy->isEqual (type))
638- return index;
639- index++;
640- }
641-
642- llvm_unreachable (" didn't find dependent type in all-dependent-types list" );
643- }
644-
645- // / Return the index of the given protocol conformance in the list of all
646- // / protocol conformances for the given dependent type in the given signature.
647- // /
648- // / This will be its index in the list of protocol conformances on the
649- // / dependent type's substitution.
650- static unsigned
651- getProtocolConformanceIndex (CanGenericSignature generics, ModuleDecl &M,
652- CanType type, ProtocolDecl *protocol) {
653- assert (type->isTypeParameter ());
654-
655- // Make a pass over all the dependent types.
656- unsigned index = 0 ;
657- for (auto reqt : generics->getRequirements ()) {
658- if (reqt.getKind () == RequirementKind::Conformance &&
659- reqt.getFirstType ()->isEqual (type)) {
660- if (reqt.getSecondType ()->getAnyNominal () == protocol)
661- return index;
662- index++;
663- }
664- }
665-
666- llvm_unreachable (" didn't find dependent type in all-dependent-types list" );
667- }
668-
669626namespace {
670627 // / A concrete witness table, together with its known layout.
671628 class WitnessTable {
@@ -2037,15 +1994,12 @@ llvm::Value *MetadataPath::followComponent(IRGenFunction &IGF,
20371994 auto &requirement = requirements.getRequirements ()[reqtIndex];
20381995
20391996 auto module = IGF.getSwiftModule ();
2040- auto generics = generic->getDecl ()->getGenericSignatureOfContext ()
2041- ->getCanonicalSignature ();
2042-
2043- auto argIndex =
2044- getDependentTypeIndex (generics, *module , requirement.TypeParameter );
2045- Substitution sub = generic->gatherAllSubstitutions (module , nullptr )[argIndex];
1997+ auto subs = generic->getContextSubstitutionMap (module ,
1998+ generic->getDecl ());
1999+ auto sub = requirement.TypeParameter .subst (subs)->getCanonicalType ();
20462000
20472001 // In either case, we need to change the type.
2048- sourceKey.Type = sub. getReplacement ()-> getCanonicalType () ;
2002+ sourceKey.Type = sub;
20492003
20502004 // If this is a type argument, we've fully updated sourceKey.
20512005 if (component.getKind () == Component::Kind::NominalTypeArgument) {
@@ -2060,10 +2014,8 @@ llvm::Value *MetadataPath::followComponent(IRGenFunction &IGF,
20602014 // conformance kind.
20612015 } else {
20622016 assert (requirement.Protocol && " index mismatch!" );
2063- auto confIndex = getProtocolConformanceIndex (generics, *module ,
2064- requirement.TypeParameter ,
2065- requirement.Protocol );
2066- auto conformance = sub.getConformances ()[confIndex];
2017+ auto conformance = *subs.lookupConformance (requirement.TypeParameter ,
2018+ requirement.Protocol );
20672019 assert (conformance.getRequirement () == requirement.Protocol );
20682020 sourceKey.Kind = LocalTypeDataKind::forProtocolWitnessTable (conformance);
20692021
@@ -2339,7 +2291,7 @@ void irgen::emitWitnessTableRefs(IRGenFunction &IGF,
23392291}
23402292
23412293static CanType getSubstSelfType (CanSILFunctionType origFnType,
2342- ArrayRef<Substitution> subs) {
2294+ const SubstitutionMap & subs) {
23432295 // Grab the apparent 'self' type. If there isn't a 'self' type,
23442296 // we're not going to try to access this anyway.
23452297 assert (!origFnType->getParameters ().empty ());
@@ -2366,8 +2318,7 @@ static CanType getSubstSelfType(CanSILFunctionType origFnType,
23662318 // SIL function type to make that work, which could be managed by having a
23672319 // "substituted generic signature" concept.
23682320 if (!subs.empty ()) {
2369- auto subMap = origFnType->getGenericSignature ()->getSubstitutionMap (subs);
2370- inputType = inputType.subst (subMap)->getCanonicalType ();
2321+ inputType = inputType.subst (subs)->getCanonicalType ();
23712322 }
23722323
23732324 return inputType;
@@ -2381,11 +2332,11 @@ namespace {
23812332 CanSILFunctionType polyFn)
23822333 : PolymorphicConvention(IGF.IGM, polyFn), IGF(IGF) {}
23832334
2384- void emit (CanSILFunctionType substFnType, ArrayRef<Substitution> subs,
2335+ void emit (CanSILFunctionType substFnType, const SubstitutionMap & subs,
23852336 WitnessMetadata *witnessMetadata, Explosion &out);
23862337
23872338 private:
2388- void emitEarlySources (ArrayRef<Substitution> subs, Explosion &out) {
2339+ void emitEarlySources (const SubstitutionMap & subs, Explosion &out) {
23892340 for (auto &source : getSources ()) {
23902341 switch (source.getKind ()) {
23912342 // Already accounted for in the parameters.
@@ -2415,15 +2366,15 @@ namespace {
24152366void irgen::emitPolymorphicArguments (IRGenFunction &IGF,
24162367 CanSILFunctionType origFnType,
24172368 CanSILFunctionType substFnType,
2418- ArrayRef<Substitution> subs,
2369+ const SubstitutionMap & subs,
24192370 WitnessMetadata *witnessMetadata,
24202371 Explosion &out) {
24212372 EmitPolymorphicArguments (IGF, origFnType).emit (substFnType, subs,
24222373 witnessMetadata, out);
24232374}
24242375
24252376void EmitPolymorphicArguments::emit (CanSILFunctionType substFnType,
2426- ArrayRef<Substitution> subs,
2377+ const SubstitutionMap & subs,
24272378 WitnessMetadata *witnessMetadata,
24282379 Explosion &out) {
24292380 // Add all the early sources.
@@ -2469,7 +2420,7 @@ NecessaryBindings
24692420NecessaryBindings::forFunctionInvocations (IRGenModule &IGM,
24702421 CanSILFunctionType origType,
24712422 CanSILFunctionType substType,
2472- ArrayRef<Substitution> subs) {
2423+ const SubstitutionMap & subs) {
24732424 NecessaryBindings bindings;
24742425
24752426 // Bail out early if we don't have polymorphic parameters.
@@ -2482,21 +2433,11 @@ NecessaryBindings::forFunctionInvocations(IRGenModule &IGM,
24822433 // - unfulfilled requirements
24832434 convention.enumerateUnfulfilledRequirements (
24842435 [&](GenericRequirement requirement) {
2485- auto depTyIndex =
2486- getDependentTypeIndex (origType->getGenericSignature (),
2487- *IGM.getSwiftModule (),
2488- requirement.TypeParameter );
2489-
2490- auto &sub = subs[depTyIndex];
2491- CanType type = sub.getReplacement ()->getCanonicalType ();
2436+ CanType type = requirement.TypeParameter .subst (subs)->getCanonicalType ();
24922437
24932438 if (requirement.Protocol ) {
2494- auto confIndex =
2495- getProtocolConformanceIndex (origType->getGenericSignature (),
2496- *IGM.getSwiftModule (),
2497- requirement.TypeParameter ,
2498- requirement.Protocol );
2499- auto conf = sub.getConformances ()[confIndex];
2439+ auto conf = *subs.lookupConformance (requirement.TypeParameter ,
2440+ requirement.Protocol );
25002441 bindings.addProtocolConformance (type, conf);
25012442 } else {
25022443 bindings.addTypeMetadata (type);
@@ -2583,23 +2524,16 @@ GenericTypeRequirements::GenericTypeRequirements(IRGenModule &IGM,
25832524
25842525void
25852526GenericTypeRequirements::enumerateFulfillments (IRGenModule &IGM,
2586- ArrayRef<Substitution> subs,
2527+ const SubstitutionMap & subs,
25872528 FulfillmentCallback callback) {
25882529 if (empty ()) return ;
25892530
2590- auto signature =
2591- TheDecl->getGenericSignatureOfContext ()->getCanonicalSignature ();
25922531 for (auto reqtIndex : indices (getRequirements ())) {
25932532 auto &reqt = getRequirements ()[reqtIndex];
2594- auto typeIndex = getDependentTypeIndex (signature, *IGM.getSwiftModule (),
2595- reqt.TypeParameter );
2596- auto &sub = subs[typeIndex];
2597- CanType type = sub.getReplacement ()->getCanonicalType ();
2533+ CanType type = reqt.TypeParameter .subst (subs)->getCanonicalType ();
25982534 if (reqt.Protocol ) {
2599- auto confIndex =
2600- getProtocolConformanceIndex (signature, *IGM.getSwiftModule (),
2601- reqt.TypeParameter , reqt.Protocol );
2602- auto conformance = sub.getConformances ()[confIndex];
2535+ auto conformance = *subs.lookupConformance (reqt.TypeParameter ,
2536+ reqt.Protocol );
26032537 callback (reqtIndex, type, conformance);
26042538 } else {
26052539 callback (reqtIndex, type, None);
@@ -2608,7 +2542,7 @@ GenericTypeRequirements::enumerateFulfillments(IRGenModule &IGM,
26082542}
26092543
26102544void GenericTypeRequirements::emitInitOfBuffer (IRGenFunction &IGF,
2611- ArrayRef<Substitution> subs,
2545+ const SubstitutionMap & subs,
26122546 Address buffer) {
26132547 if (Requirements.empty ()) return ;
26142548
@@ -2652,20 +2586,17 @@ irgen::emitGenericRequirementFromSubstitutions(IRGenFunction &IGF,
26522586 CanGenericSignature generics,
26532587 ModuleDecl &module ,
26542588 GenericRequirement requirement,
2655- ArrayRef<Substitution> subs) {
2589+ const SubstitutionMap & subs) {
26562590 CanType depTy = requirement.TypeParameter ;
2657- auto typeIndex = getDependentTypeIndex (generics, module , depTy);
2658- const Substitution &sub = subs[typeIndex];
2659- CanType argType = sub.getReplacement ()->getCanonicalType ();
2591+ CanType argType = depTy.subst (subs)->getCanonicalType ();
26602592
26612593 if (!requirement.Protocol ) {
26622594 auto argMetadata = IGF.emitTypeMetadataRef (argType);
26632595 return argMetadata;
26642596 }
26652597
26662598 auto proto = requirement.Protocol ;
2667- auto protoIndex = getProtocolConformanceIndex (generics, module , depTy, proto);
2668- auto conformance = sub.getConformances ()[protoIndex];
2599+ auto conformance = *subs.lookupConformance (depTy, proto);
26692600 assert (conformance.getRequirement () == proto);
26702601 llvm::Value *metadata = nullptr ;
26712602 auto wtable = emitWitnessTableRef (IGF, argType, &metadata, conformance);
0 commit comments