@@ -774,35 +774,6 @@ ProtocolDependenciesRequest::evaluate(Evaluator &evaluator,
774
774
// Building rewrite rules from desugared requirements.
775
775
//
776
776
777
- // / Given a concrete type that may contain type parameters in structural positions,
778
- // / collect all the structural type parameter components, and replace them all with
779
- // / fresh generic parameters. The fresh generic parameters all have a depth of 0,
780
- // / and the index is an index into the 'result' array.
781
- // /
782
- // / For example, given the concrete type Foo<X.Y, Array<Z>>, this produces the
783
- // / result type Foo<τ_0_0, Array<τ_0_1>>, with result array {X.Y, Z}.
784
- CanType
785
- RuleBuilder::getConcreteSubstitutionSchema (CanType concreteType,
786
- const ProtocolDecl *proto,
787
- SmallVectorImpl<Term> &result) {
788
- assert (!concreteType->isTypeParameter () && " Must have a concrete type here" );
789
-
790
- if (!concreteType->hasTypeParameter ())
791
- return concreteType;
792
-
793
- return CanType (concreteType.transformRec ([&](Type t) -> Optional<Type> {
794
- if (!t->isTypeParameter ())
795
- return None;
796
-
797
- unsigned index = result.size ();
798
- result.push_back (Context.getTermForType (CanType (t), proto));
799
-
800
- return CanGenericTypeParamType::get (/* type sequence=*/ false ,
801
- /* depth=*/ 0 , index,
802
- Context.getASTContext ());
803
- }));
804
- }
805
-
806
777
void RuleBuilder::addRequirements (ArrayRef<Requirement> requirements) {
807
778
// Collect all protocols transitively referenced from these requirements.
808
779
for (auto req : requirements) {
@@ -861,7 +832,7 @@ void RuleBuilder::addAssociatedType(const AssociatedTypeDecl *type,
861
832
PermanentRules.emplace_back (lhs, rhs);
862
833
}
863
834
864
- // / Lowers a generic requirement to a rewrite rule.
835
+ // / Lowers a desugared generic requirement to a rewrite rule.
865
836
// /
866
837
// / If \p proto is null, this is a generic requirement from the top-level
867
838
// / generic signature. The added rewrite rule will be rooted in a generic
@@ -870,17 +841,13 @@ void RuleBuilder::addAssociatedType(const AssociatedTypeDecl *type,
870
841
// / If \p proto is non-null, this is a generic requirement in the protocol's
871
842
// / requirement signature. The added rewrite rule will be rooted in a
872
843
// / protocol symbol.
873
- void RuleBuilder::addRequirement (const Requirement &req,
874
- const ProtocolDecl *proto) {
875
- if (Dump) {
876
- llvm::dbgs () << " + " ;
877
- req.dump (llvm::dbgs ());
878
- llvm::dbgs () << " \n " ;
879
- }
880
-
844
+ std::pair<MutableTerm, MutableTerm>
845
+ swift::rewriting::getRuleForRequirement (const Requirement &req,
846
+ const ProtocolDecl *proto,
847
+ RewriteContext &ctx) {
881
848
// Compute the left hand side.
882
849
auto subjectType = CanType (req.getFirstType ());
883
- auto subjectTerm = Context .getMutableTermForType (subjectType, proto);
850
+ auto subjectTerm = ctx .getMutableTermForType (subjectType, proto);
884
851
885
852
// Compute the right hand side.
886
853
MutableTerm constraintTerm;
@@ -895,7 +862,7 @@ void RuleBuilder::addRequirement(const Requirement &req,
895
862
auto *proto = req.getProtocolDecl ();
896
863
897
864
constraintTerm = subjectTerm;
898
- constraintTerm.add (Symbol::forProtocol (proto, Context ));
865
+ constraintTerm.add (Symbol::forProtocol (proto, ctx ));
899
866
break ;
900
867
}
901
868
@@ -907,10 +874,10 @@ void RuleBuilder::addRequirement(const Requirement &req,
907
874
908
875
// Build the symbol [superclass: C<X, Y>].
909
876
SmallVector<Term, 1 > substitutions;
910
- otherType = getConcreteSubstitutionSchema (otherType, proto,
911
- substitutions);
877
+ otherType = ctx. getSubstitutionSchemaFromType (otherType, proto,
878
+ substitutions);
912
879
auto superclassSymbol = Symbol::forSuperclass (otherType, substitutions,
913
- Context );
880
+ ctx );
914
881
915
882
// Build the term T.[superclass: C<X, Y>].
916
883
constraintTerm = subjectTerm;
@@ -924,7 +891,7 @@ void RuleBuilder::addRequirement(const Requirement &req,
924
891
// T.[layout: L] == T
925
892
constraintTerm = subjectTerm;
926
893
constraintTerm.add (Symbol::forLayout (req.getLayoutConstraint (),
927
- Context ));
894
+ ctx ));
928
895
break ;
929
896
}
930
897
@@ -937,21 +904,32 @@ void RuleBuilder::addRequirement(const Requirement &req,
937
904
//
938
905
// T.[concrete: C<X, Y>] => T
939
906
SmallVector<Term, 1 > substitutions;
940
- otherType = getConcreteSubstitutionSchema (otherType, proto,
941
- substitutions);
907
+ otherType = ctx. getSubstitutionSchemaFromType (otherType, proto,
908
+ substitutions);
942
909
943
910
constraintTerm = subjectTerm;
944
911
constraintTerm.add (Symbol::forConcreteType (otherType, substitutions,
945
- Context ));
912
+ ctx ));
946
913
break ;
947
914
}
948
915
949
- constraintTerm = Context .getMutableTermForType (otherType, proto);
916
+ constraintTerm = ctx .getMutableTermForType (otherType, proto);
950
917
break ;
951
918
}
952
919
}
953
920
954
- RequirementRules.emplace_back (subjectTerm, constraintTerm);
921
+ return std::make_pair (subjectTerm, constraintTerm);
922
+ }
923
+
924
+ void RuleBuilder::addRequirement (const Requirement &req,
925
+ const ProtocolDecl *proto) {
926
+ if (Dump) {
927
+ llvm::dbgs () << " + " ;
928
+ req.dump (llvm::dbgs ());
929
+ llvm::dbgs () << " \n " ;
930
+ }
931
+
932
+ RequirementRules.push_back (getRuleForRequirement (req, proto, Context));
955
933
}
956
934
957
935
void RuleBuilder::addRequirement (const StructuralRequirement &req,
0 commit comments