@@ -25,10 +25,12 @@ using namespace rewriting;
25
25
26
26
namespace {
27
27
28
- // / A utility class for bulding a rewrite system from the top-level requirements
29
- // / of a generic signature, and all protocol requirement signatures from all
30
- // / transitively-referenced protocols.
31
- struct RewriteSystemBuilder {
28
+ // / A utility class for bulding rewrite rules from the top-level requirements
29
+ // / of a generic signature.
30
+ // /
31
+ // / This also collects requirements from the transitive closure of all protocols
32
+ // / appearing on the right hand side of conformance requirements.
33
+ struct RuleBuilder {
32
34
RewriteContext &Context;
33
35
bool Dump;
34
36
@@ -64,8 +66,7 @@ struct RewriteSystemBuilder {
64
66
const ProtocolDecl *proto,
65
67
SmallVectorImpl<Term> &result);
66
68
67
- RewriteSystemBuilder (RewriteContext &ctx, bool dump)
68
- : Context(ctx), Dump(dump) {}
69
+ RuleBuilder (RewriteContext &ctx, bool dump) : Context(ctx), Dump(dump) {}
69
70
void addRequirements (ArrayRef<Requirement> requirements);
70
71
void addProtocols (ArrayRef<const ProtocolDecl *> proto);
71
72
void addProtocol (const ProtocolDecl *proto,
@@ -74,7 +75,7 @@ struct RewriteSystemBuilder {
74
75
const ProtocolDecl *proto);
75
76
void addRequirement (const Requirement &req,
76
77
const ProtocolDecl *proto);
77
- void processProtocolDependencies ();
78
+ void collectRulesFromReferencedProtocols ();
78
79
};
79
80
80
81
} // end namespace
@@ -87,9 +88,9 @@ struct RewriteSystemBuilder {
87
88
// / For example, given the concrete type Foo<X.Y, Array<Z>>, this produces the
88
89
// / result type Foo<τ_0_0, Array<τ_0_1>>, with result array {X.Y, Z}.
89
90
CanType
90
- RewriteSystemBuilder ::getConcreteSubstitutionSchema (CanType concreteType,
91
- const ProtocolDecl *proto,
92
- SmallVectorImpl<Term> &result) {
91
+ RuleBuilder ::getConcreteSubstitutionSchema (CanType concreteType,
92
+ const ProtocolDecl *proto,
93
+ SmallVectorImpl<Term> &result) {
93
94
assert (!concreteType->isTypeParameter () && " Must have a concrete type here" );
94
95
95
96
if (!concreteType->hasTypeParameter ())
@@ -108,29 +109,29 @@ RewriteSystemBuilder::getConcreteSubstitutionSchema(CanType concreteType,
108
109
}));
109
110
}
110
111
111
- void RewriteSystemBuilder ::addRequirements (ArrayRef<Requirement> requirements) {
112
+ void RuleBuilder ::addRequirements (ArrayRef<Requirement> requirements) {
112
113
// Collect all protocols transitively referenced from these requirements.
113
114
for (auto req : requirements) {
114
115
if (req.getKind () == RequirementKind::Conformance) {
115
116
addProtocol (req.getProtocolDecl (), /* initialComponent=*/ false );
116
117
}
117
118
}
118
119
119
- processProtocolDependencies ();
120
+ collectRulesFromReferencedProtocols ();
120
121
121
122
// Add rewrite rules for all top-level requirements.
122
123
for (const auto &req : requirements)
123
124
addRequirement (req, /* proto=*/ nullptr );
124
125
}
125
126
126
- void RewriteSystemBuilder ::addProtocols (ArrayRef<const ProtocolDecl *> protos) {
127
+ void RuleBuilder ::addProtocols (ArrayRef<const ProtocolDecl *> protos) {
127
128
// Collect all protocols transitively referenced from this connected component
128
129
// of the protocol dependency graph.
129
130
for (auto proto : protos) {
130
131
addProtocol (proto, /* initialComponent=*/ true );
131
132
}
132
133
133
- processProtocolDependencies ();
134
+ collectRulesFromReferencedProtocols ();
134
135
}
135
136
136
137
// / For an associated type T in a protocol P, we add a rewrite rule:
@@ -139,8 +140,8 @@ void RewriteSystemBuilder::addProtocols(ArrayRef<const ProtocolDecl *> protos) {
139
140
// /
140
141
// / Intuitively, this means "if a type conforms to P, it has a nested type
141
142
// / named T".
142
- void RewriteSystemBuilder ::addAssociatedType (const AssociatedTypeDecl *type,
143
- const ProtocolDecl *proto) {
143
+ void RuleBuilder ::addAssociatedType (const AssociatedTypeDecl *type,
144
+ const ProtocolDecl *proto) {
144
145
MutableTerm lhs;
145
146
lhs.add (Symbol::forProtocol (proto, Context));
146
147
lhs.add (Symbol::forName (type->getName (), Context));
@@ -160,8 +161,8 @@ void RewriteSystemBuilder::addAssociatedType(const AssociatedTypeDecl *type,
160
161
// / If \p proto is non-null, this is a generic requirement in the protocol's
161
162
// / requirement signature. The added rewrite rule will be rooted in a
162
163
// / protocol symbol.
163
- void RewriteSystemBuilder ::addRequirement (const Requirement &req,
164
- const ProtocolDecl *proto) {
164
+ void RuleBuilder ::addRequirement (const Requirement &req,
165
+ const ProtocolDecl *proto) {
165
166
if (Dump) {
166
167
llvm::dbgs () << " + " ;
167
168
req.dump (llvm::dbgs ());
@@ -281,16 +282,16 @@ void RewriteSystemBuilder::addRequirement(const Requirement &req,
281
282
}
282
283
283
284
// / Record information about a protocol if we have no seen it yet.
284
- void RewriteSystemBuilder ::addProtocol (const ProtocolDecl *proto,
285
- bool initialComponent) {
285
+ void RuleBuilder ::addProtocol (const ProtocolDecl *proto,
286
+ bool initialComponent) {
286
287
if (ProtocolMap.count (proto) > 0 )
287
288
return ;
288
289
289
290
ProtocolMap[proto] = initialComponent;
290
291
Protocols.push_back (proto);
291
292
}
292
293
293
- void RewriteSystemBuilder::processProtocolDependencies () {
294
+ void RuleBuilder::collectRulesFromReferencedProtocols () {
294
295
unsigned i = 0 ;
295
296
while (i < Protocols.size ()) {
296
297
auto *proto = Protocols[i++];
@@ -490,7 +491,7 @@ void RequirementMachine::initWithGenericSignature(CanGenericSignature sig) {
490
491
491
492
// Collect the top-level requirements, and all transtively-referenced
492
493
// protocol requirement signatures.
493
- RewriteSystemBuilder builder (Context, Dump);
494
+ RuleBuilder builder (Context, Dump);
494
495
builder.addRequirements (sig.getRequirements ());
495
496
496
497
// Add the initial set of rewrite rules to the rewrite system.
@@ -530,7 +531,7 @@ void RequirementMachine::initWithProtocols(ArrayRef<const ProtocolDecl *> protos
530
531
llvm::dbgs () << " {\n " ;
531
532
}
532
533
533
- RewriteSystemBuilder builder (Context, Dump);
534
+ RuleBuilder builder (Context, Dump);
534
535
builder.addProtocols (protos);
535
536
536
537
// Add the initial set of rewrite rules to the rewrite system.
@@ -570,7 +571,7 @@ void RequirementMachine::initWithAbstractRequirements(
570
571
571
572
// Collect the top-level requirements, and all transtively-referenced
572
573
// protocol requirement signatures.
573
- RewriteSystemBuilder builder (Context, Dump);
574
+ RuleBuilder builder (Context, Dump);
574
575
builder.addRequirements (requirements);
575
576
576
577
// Add the initial set of rewrite rules to the rewrite system.
0 commit comments