@@ -677,6 +677,96 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
677
677
return ErrorType::get (this );
678
678
}
679
679
680
+ SubstitutionMap TypeBase::getContextSubstitutionMap () {
681
+ // Fast path.
682
+ auto *nominalTy = castTo<NominalOrBoundGenericNominalType>();
683
+ if (nominalTy->ContextSubMap )
684
+ return nominalTy->ContextSubMap ;
685
+
686
+ auto nominal = nominalTy->getDecl ();
687
+ auto genericSig = nominal->getGenericSignature ();
688
+ if (!genericSig)
689
+ return SubstitutionMap ();
690
+
691
+ Type baseTy (this );
692
+
693
+ assert (!baseTy->hasLValueType () &&
694
+ !baseTy->is <AnyMetatypeType>() &&
695
+ !baseTy->is <ErrorType>());
696
+
697
+ // The resulting set of substitutions. Always use this to ensure we
698
+ // don't miss out on NRVO anywhere.
699
+ SmallVector<Type, 4 > replacementTypes;
700
+
701
+ // Gather all of the substitutions for all levels of generic arguments.
702
+ auto params = genericSig.getGenericParams ();
703
+ bool first = true ;
704
+
705
+ while (baseTy) {
706
+ // For a bound generic type, gather the generic parameter -> generic
707
+ // argument substitutions.
708
+ if (auto boundGeneric = baseTy->getAs <BoundGenericType>()) {
709
+ auto args = boundGeneric->getGenericArgs ();
710
+ for (auto arg : llvm::reverse (args)) {
711
+ replacementTypes.push_back (arg);
712
+ }
713
+
714
+ // Continue looking into the parent.
715
+ baseTy = boundGeneric->getParent ();
716
+ first = false ;
717
+ continue ;
718
+ }
719
+
720
+ // For an unbound generic type, fill in error types.
721
+ if (auto unboundGeneric = baseTy->getAs <UnboundGenericType>()) {
722
+ auto &ctx = getASTContext ();
723
+ auto decl = unboundGeneric->getDecl ();
724
+ for (auto *paramDecl : decl->getGenericParams ()->getParams ()) {
725
+ replacementTypes.push_back (ErrorType::get (ctx));
726
+ (void ) paramDecl;
727
+ }
728
+ baseTy = unboundGeneric->getParent ();
729
+ first = false ;
730
+ continue ;
731
+ }
732
+
733
+ // This case indicates we have invalid nesting of types.
734
+ if (auto protocolTy = baseTy->getAs <ProtocolType>()) {
735
+ if (!first)
736
+ break ;
737
+
738
+ replacementTypes.push_back (getASTContext ().TheErrorType );
739
+ break ;
740
+ }
741
+
742
+ // Continue looking into the parent.
743
+ if (auto nominalTy = baseTy->getAs <NominalType>()) {
744
+ baseTy = nominalTy->getParent ();
745
+ first = false ;
746
+ continue ;
747
+ }
748
+
749
+ abort ();
750
+ }
751
+
752
+ ASSERT (replacementTypes.size () <= params.size ());
753
+
754
+ // Add any outer generic parameters from the local context.
755
+ while (replacementTypes.size () < params.size ()) {
756
+ replacementTypes.push_back (getASTContext ().TheErrorType );
757
+ }
758
+
759
+ std::reverse (replacementTypes.begin (), replacementTypes.end ());
760
+
761
+ auto subMap = SubstitutionMap::get (
762
+ genericSig,
763
+ QueryReplacementTypeArray{genericSig, replacementTypes},
764
+ LookUpConformanceInModule ());
765
+
766
+ nominalTy->ContextSubMap = subMap;
767
+ return subMap;
768
+ }
769
+
680
770
TypeSubstitutionMap
681
771
TypeBase::getContextSubstitutions (const DeclContext *dc,
682
772
GenericEnvironment *genericEnv) {
@@ -805,13 +895,12 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
805
895
return substitutions;
806
896
}
807
897
808
- SubstitutionMap TypeBase::getContextSubstitutionMap () {
809
- return getContextSubstitutionMap (getAnyNominal (), nullptr );
810
- }
811
-
812
898
SubstitutionMap TypeBase::getContextSubstitutionMap (
813
899
const DeclContext *dc,
814
900
GenericEnvironment *genericEnv) {
901
+ if (dc == getAnyNominal () && genericEnv == nullptr )
902
+ return getContextSubstitutionMap ();
903
+
815
904
auto genericSig = dc->getGenericSignatureOfContext ();
816
905
if (genericSig.isNull ())
817
906
return SubstitutionMap ();
0 commit comments