@@ -712,8 +712,7 @@ DevirtualizationResult swift::tryDevirtualizeClassMethod(FullApplySite AI,
712
712
// ===----------------------------------------------------------------------===//
713
713
714
714
static SubstitutionMap
715
- getSubstitutionsForProtocolConformance (ModuleDecl *M,
716
- ProtocolConformanceRef CRef) {
715
+ getSubstitutionsForProtocolConformance (ProtocolConformanceRef CRef) {
717
716
auto C = CRef.getConcrete ();
718
717
719
718
// Walk down to the base NormalProtocolConformance.
@@ -747,7 +746,8 @@ getSubstitutionsForProtocolConformance(ModuleDecl *M,
747
746
748
747
if (Subs.empty ()) {
749
748
auto *DC = NormalC->getDeclContext ();
750
- return NormalC->getType ()->getContextSubstitutionMap (M, DC);
749
+ return NormalC->getType ()
750
+ ->getContextSubstitutionMap (DC->getParentModule (), DC);
751
751
}
752
752
753
753
return NormalC->getGenericSignature ()->getSubstitutionMap (Subs);
@@ -764,50 +764,17 @@ getSubstitutionsForProtocolConformance(ModuleDecl *M,
764
764
// / are written in terms of the requirement's generic signature need
765
765
// / to be remapped to substitutions suitable for the witness signature.
766
766
// /
767
- // / Supported remappings are:
768
- // /
769
- // / - (Concrete witness thunk) Original substitutions:
770
- // / [Self := ConcreteType, R0 := X0, R1 := X1, ...]
771
- // / - Requirement generic signature:
772
- // / <Self : P, R0, R1, ...>
773
- // / - Witness thunk generic signature:
774
- // / <W0, W1, ...>
775
- // / - Remapped substitutions:
776
- // / [W0 := X0, W1 := X1, ...]
777
- // /
778
- // / - (Class witness thunk) Original substitutions:
779
- // / [Self := C<A0, A1>, T0 := X0, T1 := X1, ...]
780
- // / - Requirement generic signature:
781
- // / <Self : P, R0, R1, ...>
782
- // / - Witness thunk generic signature:
783
- // / <Self : C<B0, B1>, B0, B1, W0, W1, ...>
784
- // / - Remapped substitutions:
785
- // / [Self := C<B0, B1>, B0 := A0, B1 := A1, W0 := X0, W1 := X1]
786
- // /
787
- // / - (Default witness thunk) Original substitutions:
788
- // / [Self := ConcreteType, R0 := X0, R1 := X1, ...]
789
- // / - Requirement generic signature:
790
- // / <Self : P, R0, R1, ...>
791
- // / - Witness thunk generic signature:
792
- // / <Self : P, W0, W1, ...>
793
- // / - Remapped substitutions:
794
- // / [Self := ConcreteType, W0 := X0, W1 := X1, ...]
795
- // /
796
767
// / \param conformanceRef The (possibly-specialized) conformance
797
768
// / \param requirementSig The generic signature of the requirement
798
769
// / \param witnessThunkSig The generic signature of the witness method
799
770
// / \param origSubs The substitutions from the call instruction
800
- // / \param isDefaultWitness True if this is a default witness method
801
- // / \param classWitness The ClassDecl if this is a class witness method
802
771
static SubstitutionMap
803
772
getWitnessMethodSubstitutions (
804
- ModuleDecl *mod,
805
773
ProtocolConformanceRef conformanceRef,
806
774
GenericSignature *requirementSig,
807
775
GenericSignature *witnessThunkSig,
808
776
SubstitutionList origSubs,
809
- bool isDefaultWitness,
810
- ClassDecl *classWitness) {
777
+ bool isDefaultWitness) {
811
778
812
779
if (witnessThunkSig == nullptr )
813
780
return SubstitutionMap ();
@@ -822,7 +789,7 @@ getWitnessMethodSubstitutions(
822
789
823
790
// If `Self` maps to a bound generic type, this gives us the
824
791
// substitutions for the concrete type's generic parameters.
825
- auto baseSubMap = getSubstitutionsForProtocolConformance (mod, conformanceRef);
792
+ auto baseSubMap = getSubstitutionsForProtocolConformance (conformanceRef);
826
793
827
794
unsigned baseDepth = 0 ;
828
795
auto *rootConformance = conformance->getRootNormalConformance ();
@@ -831,39 +798,6 @@ getWitnessMethodSubstitutions(
831
798
832
799
auto origDepth = 1 ;
833
800
834
- // If the witness has a class-constrained 'Self' generic parameter,
835
- // we have to build a new substitution map that shifts all generic
836
- // parameters down by one.
837
- if (classWitness != nullptr ) {
838
- baseDepth += 1 ;
839
-
840
- auto &ctx = mod->getASTContext ();
841
- auto *proto = conformance->getProtocol ();
842
- auto selfType = proto->getSelfInterfaceType ();
843
-
844
- auto witnessThunkToWitnessMap = witnessThunkSig->getSubstitutionMap (
845
- [&](SubstitutableType *type) -> Type {
846
- if (type->isEqual (selfType))
847
- return classWitness->getSelfInterfaceType ();
848
-
849
- auto *origParamTy = cast<GenericTypeParamType>(type);
850
- auto *substParamTy = GenericTypeParamType::get (
851
- origParamTy->getDepth () - 1 ,
852
- origParamTy->getIndex (),
853
- ctx);
854
-
855
- return substParamTy;
856
- },
857
- [&](CanType origType, Type replacementType, ProtocolType *protoType)
858
- -> Optional<ProtocolConformanceRef> {
859
- assert (!origType->isEqual (selfType));
860
-
861
- return ProtocolConformanceRef (protoType->getDecl ());
862
- });
863
-
864
- baseSubMap = witnessThunkToWitnessMap.subst (baseSubMap);
865
- }
866
-
867
801
return SubstitutionMap::combineSubstitutionMaps (
868
802
baseSubMap,
869
803
origSubMap,
@@ -873,40 +807,24 @@ getWitnessMethodSubstitutions(
873
807
witnessThunkSig);
874
808
}
875
809
876
- static ClassDecl *
877
- getWitnessMethodClass (SILFunctionType *witnessFnTy, ModuleDecl &M) {
878
- auto selfTy = witnessFnTy->getSelfInstanceType ();
879
- auto genericSig = witnessFnTy->getGenericSignature ();
880
- if (auto paramTy = dyn_cast<GenericTypeParamType>(selfTy)) {
881
- auto superclass = genericSig->getSuperclassBound (paramTy, M);
882
- if (superclass)
883
- return superclass->getClassOrBoundGenericClass ();
884
- }
885
-
886
- return nullptr ;
887
- }
888
-
889
810
static SubstitutionMap
890
811
getWitnessMethodSubstitutions (SILModule &Module, ApplySite AI, SILFunction *F,
891
812
ProtocolConformanceRef CRef) {
892
- auto witnessFnTy = F->getLoweredFunctionType ();
893
- assert (witnessFnTy->getRepresentation () ==
894
- SILFunctionTypeRepresentation::WitnessMethod);
895
-
896
813
auto requirementSig = AI.getOrigCalleeType ()->getGenericSignature ();
897
- auto witnessThunkSig = witnessFnTy ->getGenericSignature ();
814
+ auto witnessThunkSig = F-> getLoweredFunctionType () ->getGenericSignature ();
898
815
899
816
SubstitutionList origSubs = AI.getSubstitutions ();
900
817
901
- auto *mod = Module.getSwiftModule ();
902
818
bool isDefaultWitness =
903
- (witnessFnTy->getDefaultWitnessMethodProtocol (*mod)
904
- == CRef.getRequirement ());
905
- auto *classWitness = getWitnessMethodClass (witnessFnTy, *mod);
819
+ F->getLoweredFunctionType ()->getRepresentation ()
820
+ == SILFunctionTypeRepresentation::WitnessMethod &&
821
+ F->getLoweredFunctionType ()->getDefaultWitnessMethodProtocol (
822
+ *Module.getSwiftModule ())
823
+ == CRef.getRequirement ();
906
824
907
825
return getWitnessMethodSubstitutions (
908
- mod, CRef, requirementSig, witnessThunkSig,
909
- origSubs, isDefaultWitness, classWitness );
826
+ CRef, requirementSig, witnessThunkSig,
827
+ origSubs, isDefaultWitness);
910
828
}
911
829
912
830
// / Generate a new apply of a function_ref to replace an apply of a
0 commit comments