Skip to content

Commit 7148aea

Browse files
committed
RequirementMachine: Rename RewriteStep::RuleID to RewriteStep::Arg
It's used to store more than just the rule ID. However, add a getRuleID() method that also asserts that Kind is ApplyRewriteRule for readability.
1 parent 8bf1bc2 commit 7148aea

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ RewriteLoop::findRulesAppearingOnceInEmptyContext(
8888
switch (step.Kind) {
8989
case RewriteStep::ApplyRewriteRule: {
9090
if (!step.isInContext() && !evaluator.isInContext())
91-
rulesInEmptyContext.insert(step.RuleID);
91+
rulesInEmptyContext.insert(step.getRuleID());
9292

93-
++ruleMultiplicity[step.RuleID];
93+
++ruleMultiplicity[step.getRuleID()];
9494
break;
9595
}
9696

@@ -207,7 +207,7 @@ RewritePath RewritePath::splitCycleAtRule(unsigned ruleID) const {
207207
for (auto step : Steps) {
208208
switch (step.Kind) {
209209
case RewriteStep::ApplyRewriteRule: {
210-
if (step.RuleID != ruleID)
210+
if (step.getRuleID() != ruleID)
211211
break;
212212

213213
assert(!sawRule && "Rule appears more than once?");
@@ -262,7 +262,7 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
262262

263263
for (const auto &step : Steps) {
264264
if (step.Kind == RewriteStep::ApplyRewriteRule &&
265-
step.RuleID == ruleID) {
265+
step.getRuleID() == ruleID) {
266266
foundAny = true;
267267
break;
268268
}
@@ -282,7 +282,7 @@ bool RewritePath::replaceRuleWithPath(unsigned ruleID,
282282
for (const auto &step : Steps) {
283283
switch (step.Kind) {
284284
case RewriteStep::ApplyRewriteRule: {
285-
if (step.RuleID != ruleID) {
285+
if (step.getRuleID() != ruleID) {
286286
newSteps.push_back(step);
287287
break;
288288
}

lib/AST/RequirementMachine/MinimalConformances.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void RewriteLoop::findProtocolConformanceRules(
113113
if (!evaluator.isInContext()) {
114114
switch (step.Kind) {
115115
case RewriteStep::ApplyRewriteRule: {
116-
const auto &rule = system.getRule(step.RuleID);
116+
const auto &rule = system.getRule(step.getRuleID());
117117

118118
if (rule.isIdentityConformanceRule())
119119
break;
@@ -127,7 +127,7 @@ void RewriteLoop::findProtocolConformanceRules(
127127
// the prefix term is 'X'.
128128
const auto &term = evaluator.getCurrentTerm();
129129
MutableTerm prefix(term.begin(), term.begin() + step.StartOffset);
130-
result[proto].RulesInContext.emplace_back(prefix, step.RuleID);
130+
result[proto].RulesInContext.emplace_back(prefix, step.getRuleID());
131131
}
132132
}
133133

@@ -262,7 +262,7 @@ MinimalConformances::decomposeTermIntoConformanceRuleLeftHandSides(
262262
const auto &step = *steps.begin();
263263

264264
#ifndef NDEBUG
265-
const auto &rule = System.getRule(step.RuleID);
265+
const auto &rule = System.getRule(step.getRuleID());
266266
assert(rule.isAnyConformanceRule());
267267
assert(!rule.isIdentityConformanceRule());
268268
#endif
@@ -277,9 +277,10 @@ MinimalConformances::decomposeTermIntoConformanceRuleLeftHandSides(
277277
// Build the term U.
278278
MutableTerm prefix(term.begin(), term.begin() + step.StartOffset);
279279

280-
decomposeTermIntoConformanceRuleLeftHandSides(prefix, step.RuleID, result);
280+
decomposeTermIntoConformanceRuleLeftHandSides(
281+
prefix, step.getRuleID(), result);
281282
} else {
282-
result.push_back(step.RuleID);
283+
result.push_back(step.getRuleID());
283284
}
284285
}
285286

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void RewriteStep::dump(llvm::raw_ostream &out,
6161
evaluator.applyDecompose(*this, system);
6262

6363
out << (Inverse ? "Compose(" : "Decompose(");
64-
out << RuleID << ")";
64+
out << Arg << ")";
6565
break;
6666
}
6767
case ConcreteConformance: {
@@ -177,7 +177,7 @@ RewritePathEvaluator::applyRewriteRule(const RewriteStep &step,
177177

178178
assert(step.Kind == RewriteStep::ApplyRewriteRule);
179179

180-
const auto &rule = system.getRule(step.RuleID);
180+
const auto &rule = system.getRule(step.getRuleID());
181181

182182
auto lhs = (step.Inverse ? rule.getRHS() : rule.getLHS());
183183
auto rhs = (step.Inverse ? rule.getLHS() : rule.getRHS());
@@ -220,7 +220,7 @@ RewritePathEvaluator::applyAdjustment(const RewriteStep &step,
220220

221221
auto &ctx = system.getRewriteContext();
222222
MutableTerm prefix(term.begin() + step.StartOffset,
223-
term.begin() + step.StartOffset + step.RuleID);
223+
term.begin() + step.StartOffset + step.Arg);
224224
MutableTerm suffix(term.end() - step.EndOffset - 1, term.end());
225225

226226
// We're either adding or removing the prefix to each concrete substitution.
@@ -237,7 +237,7 @@ RewritePathEvaluator::applyAdjustment(const RewriteStep &step,
237237
[&](Term t) -> Term {
238238
if (step.Inverse) {
239239
if (!std::equal(t.begin(),
240-
t.begin() + step.RuleID,
240+
t.begin() + step.Arg,
241241
prefix.begin())) {
242242
llvm::errs() << "Invalid rewrite path\n";
243243
llvm::errs() << "- Term: " << term << "\n";
@@ -248,7 +248,7 @@ RewritePathEvaluator::applyAdjustment(const RewriteStep &step,
248248
abort();
249249
}
250250

251-
MutableTerm mutTerm(t.begin() + step.RuleID, t.end());
251+
MutableTerm mutTerm(t.begin() + step.Arg, t.end());
252252
return Term::get(mutTerm, ctx);
253253
} else {
254254
MutableTerm mutTerm(prefix);
@@ -265,7 +265,7 @@ void RewritePathEvaluator::applyShift(const RewriteStep &step,
265265
assert(step.Kind == RewriteStep::Shift);
266266
assert(step.StartOffset == 0);
267267
assert(step.EndOffset == 0);
268-
assert(step.RuleID == 0);
268+
assert(step.Arg == 0);
269269

270270
if (!step.Inverse) {
271271
// Move top of primary stack to secondary stack.
@@ -285,7 +285,7 @@ void RewritePathEvaluator::applyDecompose(const RewriteStep &step,
285285
assert(step.Kind == RewriteStep::Decompose);
286286

287287
auto &ctx = system.getRewriteContext();
288-
unsigned numSubstitutions = step.RuleID;
288+
unsigned numSubstitutions = step.Arg;
289289

290290
if (!step.Inverse) {
291291
// The input term takes the form U.[concrete: C].V or U.[superclass: C].V,
@@ -443,7 +443,7 @@ void RewritePathEvaluator::applyConcreteTypeWitness(const RewriteStep &step,
443443
checkPrimary();
444444
auto &term = Primary.back();
445445

446-
const auto &witness = system.getTypeWitness(step.RuleID);
446+
const auto &witness = system.getTypeWitness(step.Arg);
447447
auto fail = [&]() {
448448
llvm::errs() << "Bad concrete type witness term:\n";
449449
llvm::errs() << term << "\n\n";
@@ -501,7 +501,7 @@ void RewritePathEvaluator::applySameTypeWitness(const RewriteStep &step,
501501
checkPrimary();
502502
auto &term = Primary.back();
503503

504-
const auto &witness = system.getTypeWitness(step.RuleID);
504+
const auto &witness = system.getTypeWitness(step.Arg);
505505
auto fail = [&]() {
506506
llvm::errs() << "Bad same-type witness term:\n";
507507
llvm::errs() << term << "\n\n";
@@ -574,7 +574,7 @@ RewritePathEvaluator::applyAbstractTypeWitness(const RewriteStep &step,
574574
checkPrimary();
575575
auto &term = Primary.back();
576576

577-
const auto &witness = system.getTypeWitness(step.RuleID);
577+
const auto &witness = system.getTypeWitness(step.Arg);
578578
auto fail = [&]() {
579579
llvm::errs() << "Bad abstract type witness term:\n";
580580
llvm::errs() << term << "\n\n";

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct RewriteStep {
5151
///
5252
/// The StartOffset field encodes the offset where to apply the rule.
5353
///
54-
/// The RuleID field encodes the rule to apply.
54+
/// The Arg field encodes the rule to apply.
5555
ApplyRewriteRule,
5656

5757
/// The term at the top of the primary stack must be a term ending with a
@@ -80,7 +80,7 @@ struct RewriteStep {
8080
/// must follow a term ending with a superclass or concrete type symbol.
8181
/// The new substitutions replace the substitutions in that symbol.
8282
///
83-
/// The RuleID field encodes the number of substitutions.
83+
/// The Arg field encodes the number of substitutions.
8484
Decompose,
8585

8686
///
@@ -117,10 +117,7 @@ struct RewriteStep {
117117
///
118118
/// If inverted: the concrete type symbol [concrete: C.X] is introduced.
119119
///
120-
/// The RuleID field is repurposed to store the result of calling
121-
/// RewriteSystem::recordTypeWitness(). This index is then passed in
122-
/// to RewriteSystem::getTypeWitness() when applying
123-
/// the step.
120+
/// The Arg field stores the result of RewriteSystem::recordTypeWitness().
124121
ConcreteTypeWitness,
125122

126123
/// If not inverted: the top of the primary stack must be a term ending in a
@@ -129,8 +126,7 @@ struct RewriteStep {
129126
///
130127
/// If inverted: the associated type symbol [P:X] is introduced.
131128
///
132-
/// The RuleID field is a TypeWitness ID as above.
133-
/// the step.
129+
/// The Arg field stores the result of RewriteSystem::recordTypeWitness().
134130
SameTypeWitness,
135131

136132
/// If not inverted: replaces the abstract type witness term with the
@@ -139,7 +135,7 @@ struct RewriteStep {
139135
/// If inverted: replaces the subject type term with the abstract type
140136
/// term.
141137
///
142-
/// The RuleID field is a TypeWitness ID as above.
138+
/// The Arg field stores the result of RewriteSystem::recordTypeWitness().
143139
AbstractTypeWitness,
144140
};
145141

@@ -164,18 +160,18 @@ struct RewriteStep {
164160
/// at the beginning of each concrete substitution.
165161
///
166162
/// If Kind is Concrete, the number of substitutions to push or pop.
167-
unsigned RuleID : 16;
163+
unsigned Arg : 16;
168164

169165
RewriteStep(StepKind kind, unsigned startOffset, unsigned endOffset,
170-
unsigned ruleID, bool inverse) {
166+
unsigned arg, bool inverse) {
171167
Kind = kind;
172168

173169
StartOffset = startOffset;
174170
assert(StartOffset == startOffset && "Overflow");
175171
EndOffset = endOffset;
176172
assert(EndOffset == endOffset && "Overflow");
177-
RuleID = ruleID;
178-
assert(RuleID == ruleID && "Overflow");
173+
Arg = arg;
174+
assert(Arg == arg && "Overflow");
179175
Inverse = inverse;
180176
}
181177

@@ -187,42 +183,42 @@ struct RewriteStep {
187183
static RewriteStep forAdjustment(unsigned offset, unsigned endOffset,
188184
bool inverse) {
189185
return RewriteStep(AdjustConcreteType, /*startOffset=*/0, endOffset,
190-
/*ruleID=*/offset, inverse);
186+
/*arg=*/offset, inverse);
191187
}
192188

193189
static RewriteStep forShift(bool inverse) {
194190
return RewriteStep(Shift, /*startOffset=*/0, /*endOffset=*/0,
195-
/*ruleID=*/0, inverse);
191+
/*arg=*/0, inverse);
196192
}
197193

198194
static RewriteStep forDecompose(unsigned numSubstitutions, bool inverse) {
199195
return RewriteStep(Decompose, /*startOffset=*/0, /*endOffset=*/0,
200-
/*ruleID=*/numSubstitutions, inverse);
196+
/*arg=*/numSubstitutions, inverse);
201197
}
202198

203199
static RewriteStep forConcreteConformance(bool inverse) {
204200
return RewriteStep(ConcreteConformance, /*startOffset=*/0, /*endOffset=*/0,
205-
/*ruleID=*/0, inverse);
201+
/*arg=*/0, inverse);
206202
}
207203

208204
static RewriteStep forSuperclassConformance(bool inverse) {
209205
return RewriteStep(SuperclassConformance, /*startOffset=*/0, /*endOffset=*/0,
210-
/*ruleID=*/0, inverse);
206+
/*arg=*/0, inverse);
211207
}
212208

213209
static RewriteStep forConcreteTypeWitness(unsigned witnessID, bool inverse) {
214210
return RewriteStep(ConcreteTypeWitness, /*startOffset=*/0, /*endOffset=*/0,
215-
/*ruleID=*/witnessID, inverse);
211+
/*arg=*/witnessID, inverse);
216212
}
217213

218214
static RewriteStep forSameTypeWitness(unsigned witnessID, bool inverse) {
219215
return RewriteStep(SameTypeWitness, /*startOffset=*/0, /*endOffset=*/0,
220-
/*ruleID=*/witnessID, inverse);
216+
/*arg=*/witnessID, inverse);
221217
}
222218

223219
static RewriteStep forAbstractTypeWitness(unsigned witnessID, bool inverse) {
224220
return RewriteStep(AbstractTypeWitness, /*startOffset=*/0, /*endOffset=*/0,
225-
/*ruleID=*/witnessID, inverse);
221+
/*arg=*/witnessID, inverse);
226222
}
227223

228224
bool isInContext() const {
@@ -233,10 +229,10 @@ struct RewriteStep {
233229
Inverse = !Inverse;
234230
}
235231

236-
bool isInverseOf(const RewriteStep &other) const;
237-
238-
bool maybeSwapRewriteSteps(RewriteStep &other,
239-
const RewriteSystem &system);
232+
unsigned getRuleID() const {
233+
assert(Kind == RewriteStep::ApplyRewriteRule);
234+
return Arg;
235+
}
240236

241237
void dump(llvm::raw_ostream &out,
242238
RewritePathEvaluator &evaluator,

0 commit comments

Comments
 (0)