Skip to content

Commit 01ba790

Browse files
committed
RequirementMachine: Move remapConcreteSubstitutionSchema() to RewriteContext
1 parent b27c156 commit 01ba790

File tree

3 files changed

+55
-53
lines changed

3 files changed

+55
-53
lines changed

lib/AST/RequirementMachine/InterfaceType.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,51 @@ Type PropertyMap::getTypeFromSubstitutionSchema(
494494
});
495495
}
496496

497+
/// This method takes a concrete type that was derived from a concrete type
498+
/// produced by RewriteContext::getSubstitutionSchemaFromType() either by
499+
/// extracting a structural sub-component or performing a (Swift AST)
500+
/// substitution using subst(). It returns a new concrete substitution schema
501+
/// and a new list of substitution terms.
502+
///
503+
/// For example, suppose we start with the concrete type
504+
///
505+
/// Dictionary<τ_0_0, Array<τ_0_1>> with substitutions {X.Y, Z}
506+
///
507+
/// We can extract out the structural sub-component Array<τ_0_1>. If we wish
508+
/// to build a new concrete substitution schema, we call this method with
509+
/// Array<τ_0_1> and the original substitutions {X.Y, Z}. This will produce
510+
/// the new schema Array<τ_0_0> with substitutions {Z}.
511+
///
512+
/// As another example, consider we start with the schema Bar<τ_0_0> with
513+
/// original substitutions {X.Y}, and perform a Swift AST subst() to get
514+
/// Foo<τ_0_0.A.B>. We can then call this method with Foo<τ_0_0.A.B> and
515+
/// the original substitutions {X.Y} to produce the new schema Foo<τ_0_0>
516+
/// with substitutions {X.Y.A.B}.
517+
CanType
518+
RewriteContext::remapConcreteSubstitutionSchema(
519+
CanType concreteType,
520+
ArrayRef<Term> substitutions,
521+
SmallVectorImpl<Term> &result) {
522+
assert(!concreteType->isTypeParameter() && "Must have a concrete type here");
523+
524+
if (!concreteType->hasTypeParameter())
525+
return concreteType;
526+
527+
return CanType(concreteType.transformRec([&](Type t) -> Optional<Type> {
528+
if (!t->isTypeParameter())
529+
return None;
530+
531+
auto term = getRelativeTermForType(CanType(t), substitutions);
532+
533+
unsigned newIndex = result.size();
534+
result.push_back(Term::get(term, *this));
535+
536+
return CanGenericTypeParamType::get(/*type sequence=*/ false,
537+
/*depth=*/ 0, newIndex,
538+
Context);
539+
}));
540+
}
541+
497542
/// Given a concrete type that may contain type parameters in structural positions,
498543
/// collect all the structural type parameter components, and replace them all with
499544
/// fresh generic parameters. The fresh generic parameters all have a depth of 0,

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -132,51 +132,6 @@ static void recordConflict(Term key,
132132
newRule.markConflicting();
133133
}
134134

135-
/// This method takes a concrete type that was derived from a concrete type
136-
/// produced by RewriteSystemBuilder::getConcreteSubstitutionSchema(),
137-
/// either by extracting a structural sub-component or performing a (Swift AST)
138-
/// substitution using subst(). It returns a new concrete substitution schema
139-
/// and a new list of substitution terms.
140-
///
141-
/// For example, suppose we start with the concrete type
142-
///
143-
/// Dictionary<τ_0_0, Array<τ_0_1>> with substitutions {X.Y, Z}
144-
///
145-
/// We can extract out the structural sub-component Array<τ_0_1>. If we wish
146-
/// to build a new concrete substitution schema, we call this method with
147-
/// Array<τ_0_1> and the original substitutions {X.Y, Z}. This will produce
148-
/// the new schema Array<τ_0_0> with substitutions {Z}.
149-
///
150-
/// As another example, consider we start with the schema Bar<τ_0_0> with
151-
/// original substitutions {X.Y}, and perform a Swift AST subst() to get
152-
/// Foo<τ_0_0.A.B>. We can then call this method with Foo<τ_0_0.A.B> and
153-
/// the original substitutions {X.Y} to produce the new schema Foo<τ_0_0>
154-
/// with substitutions {X.Y.A.B}.
155-
static CanType
156-
remapConcreteSubstitutionSchema(CanType concreteType,
157-
ArrayRef<Term> substitutions,
158-
RewriteContext &ctx,
159-
SmallVectorImpl<Term> &result) {
160-
assert(!concreteType->isTypeParameter() && "Must have a concrete type here");
161-
162-
if (!concreteType->hasTypeParameter())
163-
return concreteType;
164-
165-
return CanType(concreteType.transformRec([&](Type t) -> Optional<Type> {
166-
if (!t->isTypeParameter())
167-
return None;
168-
169-
auto term = ctx.getRelativeTermForType(CanType(t), substitutions);
170-
171-
unsigned newIndex = result.size();
172-
result.push_back(Term::get(term, ctx));
173-
174-
return CanGenericTypeParamType::get(/*type sequence=*/ false,
175-
/*depth=*/ 0, newIndex,
176-
ctx.getASTContext());
177-
}));
178-
}
179-
180135
namespace {
181136
/// Utility class used by unifyConcreteTypes() and unifySuperclasses()
182137
/// to walk two concrete types in parallel. Any time there is a mismatch,
@@ -228,9 +183,8 @@ namespace {
228183
lhsSubstitutions);
229184

230185
SmallVector<Term, 3> result;
231-
auto concreteType = remapConcreteSubstitutionSchema(CanType(secondType),
232-
rhsSubstitutions,
233-
ctx, result);
186+
auto concreteType = ctx.remapConcreteSubstitutionSchema(
187+
CanType(secondType), rhsSubstitutions, result);
234188

235189
MutableTerm constraintTerm(subjectTerm);
236190
constraintTerm.add(Symbol::forConcreteType(concreteType, result, ctx));
@@ -250,9 +204,8 @@ namespace {
250204
rhsSubstitutions);
251205

252206
SmallVector<Term, 3> result;
253-
auto concreteType = remapConcreteSubstitutionSchema(CanType(firstType),
254-
lhsSubstitutions,
255-
ctx, result);
207+
auto concreteType = ctx.remapConcreteSubstitutionSchema(
208+
CanType(firstType), lhsSubstitutions, result);
256209

257210
MutableTerm constraintTerm(subjectTerm);
258211
constraintTerm.add(Symbol::forConcreteType(concreteType, result, ctx));
@@ -889,8 +842,8 @@ MutableTerm PropertyMap::computeConstraintTermForTypeWitness(
889842
// Compute the concrete type symbol [concrete: C.X].
890843
SmallVector<Term, 3> result;
891844
auto typeWitnessSchema =
892-
remapConcreteSubstitutionSchema(typeWitness, substitutions,
893-
Context, result);
845+
Context.remapConcreteSubstitutionSchema(typeWitness, substitutions,
846+
result);
894847
auto typeWitnessSymbol =
895848
Symbol::forConcreteType(typeWitnessSchema, result, Context);
896849

lib/AST/RequirementMachine/RewriteContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ class RewriteContext final {
174174
const ProtocolDecl *proto,
175175
SmallVectorImpl<Term> &result);
176176

177+
CanType remapConcreteSubstitutionSchema(CanType concreteType,
178+
ArrayRef<Term> substitutions,
179+
SmallVectorImpl<Term> &result);
180+
177181
AssociatedTypeDecl *getAssociatedTypeForSymbol(Symbol symbol);
178182

179183
Symbol mergeAssociatedTypes(Symbol lhs, Symbol rhs);

0 commit comments

Comments
 (0)