Skip to content

Commit abb85fb

Browse files
committed
RequirementMachine: Replace RewriteStep::{Superclass,Concrete}Conformance with relations
1 parent 3d2492f commit abb85fb

File tree

7 files changed

+42
-164
lines changed

7 files changed

+42
-164
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ RewriteLoop::findRulesAppearingOnceInEmptyContext(
9898
case RewriteStep::Shift:
9999
case RewriteStep::Decompose:
100100
case RewriteStep::Relation:
101-
case RewriteStep::ConcreteConformance:
102-
case RewriteStep::SuperclassConformance:
103101
case RewriteStep::ConcreteTypeWitness:
104102
case RewriteStep::SameTypeWitness:
105103
case RewriteStep::AbstractTypeWitness:
@@ -222,8 +220,6 @@ RewritePath RewritePath::splitCycleAtRule(unsigned ruleID) const {
222220
case RewriteStep::Shift:
223221
case RewriteStep::Decompose:
224222
case RewriteStep::Relation:
225-
case RewriteStep::ConcreteConformance:
226-
case RewriteStep::SuperclassConformance:
227223
case RewriteStep::ConcreteTypeWitness:
228224
case RewriteStep::SameTypeWitness:
229225
case RewriteStep::AbstractTypeWitness:
@@ -324,8 +320,6 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
324320
case RewriteStep::Shift:
325321
case RewriteStep::Decompose:
326322
case RewriteStep::Relation:
327-
case RewriteStep::ConcreteConformance:
328-
case RewriteStep::SuperclassConformance:
329323
case RewriteStep::ConcreteTypeWitness:
330324
case RewriteStep::SameTypeWitness:
331325
case RewriteStep::AbstractTypeWitness:

lib/AST/RequirementMachine/MinimalConformances.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ void RewriteLoop::findProtocolConformanceRules(
138138
case RewriteStep::Shift:
139139
case RewriteStep::Decompose:
140140
case RewriteStep::Relation:
141-
case RewriteStep::ConcreteConformance:
142-
case RewriteStep::SuperclassConformance:
143141
case RewriteStep::ConcreteTypeWitness:
144142
case RewriteStep::SameTypeWitness:
145143
case RewriteStep::AbstractTypeWitness:

lib/AST/RequirementMachine/PropertyRelations.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ unsigned RewriteSystem::recordRelation(Symbol lhsProperty,
6565
Term::get(rhsTerm, Context));
6666
}
6767

68+
/// Record a relation ([concrete: C].[P] => [concrete: C : P])
69+
/// or ([superclass: C].[P] => [concrete: C : P]) which combines a
70+
/// concrete type symbol (or a superclass symbol) with a protocol
71+
/// symbol to form a single a concrete conformance symbol.
72+
unsigned RewriteSystem::recordConcreteConformanceRelation(
73+
Symbol concreteSymbol, Symbol protocolSymbol,
74+
Symbol concreteConformanceSymbol) {
75+
assert(protocolSymbol.getKind() == Symbol::Kind::Protocol);
76+
assert(protocolSymbol.getProtocol() ==
77+
concreteConformanceSymbol.getProtocol());
78+
assert(concreteSymbol.getKind() == Symbol::Kind::Superclass ||
79+
concreteSymbol.getKind() == Symbol::Kind::ConcreteType);
80+
81+
MutableTerm lhsTerm;
82+
lhsTerm.add(concreteSymbol);
83+
lhsTerm.add(protocolSymbol);
84+
85+
MutableTerm rhsTerm;
86+
rhsTerm.add(concreteConformanceSymbol);
87+
88+
return recordRelation(
89+
Term::get(lhsTerm, Context),
90+
Term::get(rhsTerm, Context));
91+
}
92+
6893
RewriteSystem::TypeWitness::TypeWitness(
6994
Term lhs, llvm::PointerUnion<Symbol, Term> rhs)
7095
: LHS(lhs), RHS(rhs) {

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -957,23 +957,6 @@ void PropertyMap::recordConcreteConformanceRule(
957957
const auto &concreteRule = System.getRule(concreteRuleID);
958958
const auto &conformanceRule = System.getRule(conformanceRuleID);
959959

960-
#ifndef NDEBUG
961-
{
962-
auto conformanceSymbol = *conformanceRule.isPropertyRule();
963-
assert(conformanceSymbol.getKind() == Symbol::Kind::Protocol);
964-
assert(conformanceSymbol.getProtocol() ==
965-
concreteConformanceSymbol.getProtocol());
966-
967-
auto concreteSymbol = *concreteRule.isPropertyRule();
968-
if (concreteSymbol.getKind() == Symbol::Kind::Superclass)
969-
assert(requirementKind == RequirementKind::Superclass);
970-
else {
971-
assert(concreteSymbol.getKind() == Symbol::Kind::ConcreteType);
972-
assert(requirementKind == RequirementKind::SameType);
973-
}
974-
}
975-
#endif
976-
977960
RewritePath path;
978961

979962
// We have a pair of rules T.[P] and T'.[concrete: C].
@@ -1000,20 +983,28 @@ void PropertyMap::recordConcreteConformanceRule(
1000983

1001984
// Apply a concrete type adjustment to the concrete symbol if T' is shorter
1002985
// than T.
986+
auto concreteSymbol = *concreteRule.isPropertyRule();
1003987
unsigned adjustment = rhs.size() - concreteRule.getRHS().size();
988+
1004989
if (adjustment > 0 &&
1005990
!concreteConformanceSymbol.getSubstitutions().empty()) {
1006991
path.add(RewriteStep::forAdjustment(adjustment, /*endOffset=*/1,
1007992
/*inverse=*/false));
993+
994+
MutableTerm prefix(rhs.begin(), rhs.begin() + adjustment);
995+
concreteSymbol = concreteSymbol.prependPrefixToConcreteSubstitutions(
996+
prefix, Context);
1008997
}
1009998

999+
auto protocolSymbol = *conformanceRule.isPropertyRule();
1000+
10101001
// Now, transform T''.[concrete: C].[P] into T''.[concrete: C : P].
1011-
if (requirementKind == RequirementKind::Superclass) {
1012-
path.add(RewriteStep::forSuperclassConformance(/*inverse=*/false));
1013-
} else {
1014-
assert(requirementKind == RequirementKind::SameType);
1015-
path.add(RewriteStep::forConcreteConformance(/*inverse=*/false));
1016-
}
1002+
unsigned relationID = System.recordConcreteConformanceRelation(
1003+
concreteSymbol, protocolSymbol, concreteConformanceSymbol);
1004+
1005+
path.add(RewriteStep::forRelation(
1006+
/*startOffset=*/rhs.size(), relationID,
1007+
/*inverse=*/false));
10171008

10181009
MutableTerm lhs(rhs);
10191010
lhs.add(concreteConformanceSymbol);

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,6 @@ void RewriteStep::dump(llvm::raw_ostream &out,
7979

8080
break;
8181
}
82-
case ConcreteConformance: {
83-
evaluator.applyConcreteConformance(*this, system);
84-
85-
out << (Inverse ? "ConcreteConformance⁻¹"
86-
: "ConcreteConformance");
87-
break;
88-
}
89-
case SuperclassConformance: {
90-
evaluator.applyConcreteConformance(*this, system);
91-
92-
out << (Inverse ? "SuperclassConformance⁻¹"
93-
: "SuperclassConformance");
94-
break;
95-
}
9682
case ConcreteTypeWitness: {
9783
evaluator.applyConcreteTypeWitness(*this, system);
9884

@@ -411,88 +397,6 @@ RewritePathEvaluator::applyRelation(const RewriteStep &step,
411397
return {lhs, rhs, prefix, suffix};
412398
}
413399

414-
void
415-
RewritePathEvaluator::applyConcreteConformance(const RewriteStep &step,
416-
const RewriteSystem &system) {
417-
checkPrimary();
418-
auto &term = Primary.back();
419-
Symbol *last = term.end() - step.EndOffset;
420-
421-
auto &ctx = system.getRewriteContext();
422-
423-
if (!step.Inverse) {
424-
// The input term takes one of the following forms, where |V| == EndOffset:
425-
// - U.[concrete: C].[P].V
426-
// - U.[superclass: C].[P].V
427-
assert(term.size() > step.EndOffset + 2);
428-
auto concreteType = *(last - 2);
429-
auto proto = *(last - 1);
430-
assert(proto.getKind() == Symbol::Kind::Protocol);
431-
432-
// Get the prefix U.
433-
MutableTerm newTerm(term.begin(), last - 2);
434-
435-
// Build the term U.[concrete: C : P].
436-
if (step.Kind == RewriteStep::ConcreteConformance) {
437-
assert(concreteType.getKind() == Symbol::Kind::ConcreteType);
438-
439-
newTerm.add(Symbol::forConcreteConformance(
440-
concreteType.getConcreteType(),
441-
concreteType.getSubstitutions(),
442-
proto.getProtocol(),
443-
ctx));
444-
} else {
445-
assert(step.Kind == RewriteStep::SuperclassConformance);
446-
assert(concreteType.getKind() == Symbol::Kind::Superclass);
447-
448-
newTerm.add(Symbol::forConcreteConformance(
449-
concreteType.getSuperclass(),
450-
concreteType.getSubstitutions(),
451-
proto.getProtocol(),
452-
ctx));
453-
}
454-
455-
// Add the suffix V to get the final term U.[concrete: C : P].V.
456-
newTerm.append(last, term.end());
457-
term = newTerm;
458-
} else {
459-
// The input term takes the form U.[concrete: C : P].V, where
460-
// |V| == EndOffset.
461-
assert(term.size() > step.EndOffset + 1);
462-
auto concreteConformance = *(last - 1);
463-
assert(concreteConformance.getKind() == Symbol::Kind::ConcreteConformance);
464-
465-
// Build the term U.
466-
MutableTerm newTerm(term.begin(), last - 1);
467-
468-
// Add the symbol [concrete: C] or [superclass: C] to get the term
469-
// U.[concrete: C] or U.[superclass: C].
470-
if (step.Kind == RewriteStep::ConcreteConformance) {
471-
newTerm.add(Symbol::forConcreteType(
472-
concreteConformance.getConcreteType(),
473-
concreteConformance.getSubstitutions(),
474-
ctx));
475-
} else {
476-
assert(step.Kind == RewriteStep::SuperclassConformance);
477-
newTerm.add(Symbol::forSuperclass(
478-
concreteConformance.getConcreteType(),
479-
concreteConformance.getSubstitutions(),
480-
ctx));
481-
}
482-
483-
// Add the symbol [P] to get the term U.[concrete: C].[P] or
484-
// U.[superclass: C].[P].
485-
newTerm.add(Symbol::forProtocol(
486-
concreteConformance.getProtocol(),
487-
ctx));
488-
489-
// Add the suffix V to get the final term U.[concrete: C].[P].V or
490-
// U.[superclass: C].[P].V.
491-
newTerm.append(last, term.end());
492-
term = newTerm;
493-
}
494-
}
495-
496400
void RewritePathEvaluator::applyConcreteTypeWitness(const RewriteStep &step,
497401
const RewriteSystem &system) {
498402
checkPrimary();
@@ -682,11 +586,6 @@ void RewritePathEvaluator::apply(const RewriteStep &step,
682586
applyRelation(step, system);
683587
break;
684588

685-
case RewriteStep::ConcreteConformance:
686-
case RewriteStep::SuperclassConformance:
687-
applyConcreteConformance(step, system);
688-
break;
689-
690589
case RewriteStep::ConcreteTypeWitness:
691590
applyConcreteTypeWitness(step, system);
692591
break;

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,6 @@ struct RewriteStep {
100100
/// RewriteSystem::recordRelation().
101101
Relation,
102102

103-
/// If not inverted: the top of the primary stack must be a term ending in
104-
/// a concrete type symbol [concrete: C] followed by a protocol symbol [P].
105-
/// These two symbols are combined into a single concrete conformance
106-
/// symbol [concrete: C : P].
107-
///
108-
/// If inverted: the top of the primary stack must be a term ending in a
109-
/// concrete conformance symbol [concrete: C : P]. This symbol is replaced
110-
/// with the concrete type symbol [concrete: C] followed by the protocol
111-
/// symbol [P].
112-
ConcreteConformance,
113-
114-
/// If not inverted: the top of the primary stack must be a term ending in
115-
/// a superclass symbol [superclass: C] followed by a protocol symbol [P].
116-
/// These two symbols are combined into a single concrete conformance
117-
/// symbol [concrete: C : P].
118-
///
119-
/// If inverted: the top of the primary stack must be a term ending in a
120-
/// concrete conformance symbol [concrete: C : P]. This symbol is replaced
121-
/// with the superclass symbol [superclass: C] followed by the protocol
122-
/// symbol [P].
123-
SuperclassConformance,
124-
125103
/// If not inverted: the top of the primary stack must be a term ending in a
126104
/// concrete conformance symbol [concrete: C : P] followed by an associated
127105
/// type symbol [P:X], and the concrete type symbol [concrete: C.X] for the
@@ -215,16 +193,6 @@ struct RewriteStep {
215193
/*arg=*/relationID, inverse);
216194
}
217195

218-
static RewriteStep forConcreteConformance(bool inverse) {
219-
return RewriteStep(ConcreteConformance, /*startOffset=*/0, /*endOffset=*/0,
220-
/*arg=*/0, inverse);
221-
}
222-
223-
static RewriteStep forSuperclassConformance(bool inverse) {
224-
return RewriteStep(SuperclassConformance, /*startOffset=*/0, /*endOffset=*/0,
225-
/*arg=*/0, inverse);
226-
}
227-
228196
static RewriteStep forConcreteTypeWitness(unsigned witnessID, bool inverse) {
229197
return RewriteStep(ConcreteTypeWitness, /*startOffset=*/0, /*endOffset=*/0,
230198
/*arg=*/witnessID, inverse);

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ class RewriteSystem final {
346346
Relation getRelation(unsigned index) const;
347347

348348
unsigned recordRelation(Symbol lhs, Symbol rhs);
349+
unsigned recordConcreteConformanceRelation(
350+
Symbol concreteSymbol, Symbol protocolSymbol,
351+
Symbol concreteConformanceSymbol);
349352

350353
/// A type witness has a subject type, stored in LHS, which takes the form:
351354
///

0 commit comments

Comments
 (0)