@@ -523,62 +523,76 @@ SubstitutionMap::getOverrideSubstitutions(
523523 return getOverrideSubstitutions (baseClass, derivedClass, baseSig, derivedParams);
524524}
525525
526- SubstitutionMap
527- SubstitutionMap::getOverrideSubstitutions (const ClassDecl *baseClass,
528- const ClassDecl *derivedClass,
529- GenericSignature baseSig,
530- GenericParamList *derivedParams) {
531- if (baseSig.isNull ())
532- return SubstitutionMap ();
526+ OverrideSubsInfo::OverrideSubsInfo (const NominalTypeDecl *baseNominal,
527+ const NominalTypeDecl *derivedNominal,
528+ GenericSignature baseSig,
529+ const GenericParamList *derivedParams)
530+ : Ctx(baseSig->getASTContext ()),
531+ BaseDepth(0 ),
532+ OrigDepth(0 ),
533+ DerivedParams(derivedParams) {
534+
535+ if (auto baseNominalSig = baseNominal->getGenericSignature ()) {
536+ BaseDepth = baseNominalSig.getGenericParams ().back ()->getDepth () + 1 ;
537+
538+ auto derivedNominalTy = derivedNominal->getDeclaredInterfaceType ();
539+ BaseSubMap = derivedNominalTy->getContextSubstitutionMap (
540+ baseNominal->getParentModule (), baseNominal);
541+ assert (!BaseSubMap.hasArchetypes ());
542+ }
543+
544+ if (auto derivedNominalSig = derivedNominal->getGenericSignature ())
545+ OrigDepth = derivedNominalSig.getGenericParams ().back ()->getDepth () + 1 ;
546+ }
533547
534- unsigned baseDepth = 0 ;
535- SubstitutionMap baseSubMap;
536- if (auto baseClassSig = baseClass->getGenericSignature ()) {
537- baseDepth = baseClassSig.getGenericParams ().back ()->getDepth () + 1 ;
548+ Type QueryOverrideSubs::operator ()(SubstitutableType *type) const {
549+ if (auto gp = type->getAs <GenericTypeParamType>()) {
550+ if (gp->getDepth () >= info.BaseDepth ) {
551+ assert (gp->getDepth () == info.BaseDepth );
552+ if (info.DerivedParams != nullptr ) {
553+ return info.DerivedParams ->getParams ()[gp->getIndex ()]
554+ ->getDeclaredInterfaceType ();
555+ }
538556
539- auto derivedClassTy = derivedClass->getDeclaredInterfaceType ();
540- baseSubMap = derivedClassTy->getContextSubstitutionMap (
541- baseClass->getParentModule (), baseClass);
542- assert (!baseSubMap.hasArchetypes ());
557+ return GenericTypeParamType::get (
558+ gp->isTypeSequence (),
559+ gp->getDepth () + info.OrigDepth - info.BaseDepth ,
560+ gp->getIndex (), info.Ctx );
561+ }
543562 }
544563
545- unsigned origDepth = 0 ;
546- if (auto derivedClassSig = derivedClass->getGenericSignature ())
547- origDepth = derivedClassSig.getGenericParams ().back ()->getDepth () + 1 ;
564+ return Type (type).subst (info.BaseSubMap );
565+ }
548566
549- auto &ctx = baseSig->getASTContext ();
567+ ProtocolConformanceRef
568+ LookUpConformanceInOverrideSubs::operator ()(CanType type,
569+ Type substType,
570+ ProtocolDecl *proto) const {
571+ if (type->getRootGenericParam ()->getDepth () >= info.BaseDepth )
572+ return ProtocolConformanceRef (proto);
550573
551- return get (
552- baseSig,
553- [&](SubstitutableType *type) -> Type {
554- if (auto gp = type->getAs <GenericTypeParamType>()) {
555- if (gp->getDepth () >= baseDepth) {
556- assert (gp->getDepth () == baseDepth);
557- if (derivedParams != nullptr ) {
558- return derivedParams->getParams ()[gp->getIndex ()]
559- ->getDeclaredInterfaceType ();
560- }
561-
562- return GenericTypeParamType::get (gp->isTypeSequence (),
563- gp->getDepth () + origDepth - baseDepth,
564- gp->getIndex (), ctx);
565- }
566- }
574+ if (auto conformance = info.BaseSubMap .lookupConformance (type, proto))
575+ return conformance;
567576
568- return Type (type).subst (baseSubMap);
569- },
570- [&](CanType type, Type substType, ProtocolDecl *proto) {
571- if (type->getRootGenericParam ()->getDepth () >= baseDepth)
572- return ProtocolConformanceRef (proto);
577+ if (substType->isTypeParameter ())
578+ return ProtocolConformanceRef (proto);
573579
574- if ( auto conformance = baseSubMap. lookupConformance (type , proto))
575- return conformance;
580+ return proto-> getParentModule ()-> lookupConformance (substType , proto);
581+ }
576582
577- if (substType->isTypeParameter ())
578- return ProtocolConformanceRef (proto);
583+ SubstitutionMap
584+ SubstitutionMap::getOverrideSubstitutions (const NominalTypeDecl *baseNominal,
585+ const NominalTypeDecl *derivedNominal,
586+ GenericSignature baseSig,
587+ const GenericParamList *derivedParams) {
588+ if (baseSig.isNull ())
589+ return SubstitutionMap ();
579590
580- return proto->getParentModule ()->lookupConformance (substType, proto);
581- });
591+ OverrideSubsInfo info (baseNominal, derivedNominal, baseSig, derivedParams);
592+
593+ return get (baseSig,
594+ QueryOverrideSubs (info),
595+ LookUpConformanceInOverrideSubs (info));
582596}
583597
584598SubstitutionMap
0 commit comments