Skip to content

Commit 8bf1bc2

Browse files
committed
RequirementMachine: Rename A/B stack to primary/secondary stack
1 parent d1bb98b commit 8bf1bc2

File tree

2 files changed

+73
-69
lines changed

2 files changed

+73
-69
lines changed

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,35 @@ void RewriteLoop::dump(llvm::raw_ostream &out,
139139
}
140140

141141
void RewritePathEvaluator::dump(llvm::raw_ostream &out) const {
142-
out << "A stack:\n";
143-
for (const auto &term : A) {
142+
out << "Primary stack:\n";
143+
for (const auto &term : Primary) {
144144
out << term << "\n";
145145
}
146-
out << "\nB stack:\n";
147-
for (const auto &term : B) {
146+
out << "\nSecondary stack:\n";
147+
for (const auto &term : Secondary) {
148148
out << term << "\n";
149149
}
150150
}
151151

152-
void RewritePathEvaluator::checkA() const {
153-
if (A.empty()) {
154-
llvm::errs() << "Empty A stack\n";
152+
void RewritePathEvaluator::checkPrimary() const {
153+
if (Primary.empty()) {
154+
llvm::errs() << "Empty primary stack\n";
155155
dump(llvm::errs());
156156
abort();
157157
}
158158
}
159159

160-
void RewritePathEvaluator::checkB() const {
161-
if (B.empty()) {
162-
llvm::errs() << "Empty B stack\n";
160+
void RewritePathEvaluator::checkSecondary() const {
161+
if (Secondary.empty()) {
162+
llvm::errs() << "Empty secondary stack\n";
163163
dump(llvm::errs());
164164
abort();
165165
}
166166
}
167167

168168
MutableTerm &RewritePathEvaluator::getCurrentTerm() {
169-
checkA();
170-
return A.back();
169+
checkPrimary();
170+
return Primary.back();
171171
}
172172

173173
AppliedRewriteStep
@@ -268,15 +268,15 @@ void RewritePathEvaluator::applyShift(const RewriteStep &step,
268268
assert(step.RuleID == 0);
269269

270270
if (!step.Inverse) {
271-
// Move top of A stack to B stack.
272-
checkA();
273-
B.push_back(A.back());
274-
A.pop_back();
271+
// Move top of primary stack to secondary stack.
272+
checkPrimary();
273+
Secondary.push_back(Primary.back());
274+
Primary.pop_back();
275275
} else {
276-
// Move top of B stack to A stack.
277-
checkB();
278-
A.push_back(B.back());
279-
B.pop_back();
276+
// Move top of secondary stack to primary stack.
277+
checkSecondary();
278+
Primary.push_back(Secondary.back());
279+
Secondary.pop_back();
280280
}
281281
}
282282

@@ -294,7 +294,7 @@ void RewritePathEvaluator::applyDecompose(const RewriteStep &step,
294294
auto symbol = *(term.end() - step.EndOffset - 1);
295295
if (!symbol.hasSubstitutions()) {
296296
llvm::errs() << "Expected term with superclass or concrete type symbol"
297-
<< " on A stack\n";
297+
<< " on primary stack\n";
298298
dump(llvm::errs());
299299
abort();
300300
}
@@ -306,28 +306,28 @@ void RewritePathEvaluator::applyDecompose(const RewriteStep &step,
306306
abort();
307307
}
308308

309-
// Push each substitution on the A stack.
309+
// Push each substitution on the primary stack.
310310
for (auto substitution : symbol.getSubstitutions()) {
311-
A.push_back(MutableTerm(substitution));
311+
Primary.push_back(MutableTerm(substitution));
312312
}
313313
} else {
314-
// The A stack must store the number of substitutions, together with a
315-
// term that takes the form U.[concrete: C].V or U.[superclass: C].V,
314+
// The primary stack must store the number of substitutions, together with
315+
// a term that takes the form U.[concrete: C].V or U.[superclass: C].V,
316316
// where |V| == EndOffset.
317-
if (A.size() < numSubstitutions + 1) {
318-
llvm::errs() << "Not enough terms on A stack\n";
317+
if (Primary.size() < numSubstitutions + 1) {
318+
llvm::errs() << "Not enough terms on primary stack\n";
319319
dump(llvm::errs());
320320
abort();
321321
}
322322

323323
// The term immediately underneath the substitutions is the one we're
324324
// updating with new substitutions.
325-
auto &term = *(A.end() - numSubstitutions - 1);
325+
auto &term = *(Primary.end() - numSubstitutions - 1);
326326

327327
auto &symbol = *(term.end() - step.EndOffset - 1);
328328
if (!symbol.hasSubstitutions()) {
329329
llvm::errs() << "Expected term with superclass or concrete type symbol"
330-
<< " on A stack\n";
330+
<< " on primary stack\n";
331331
dump(llvm::errs());
332332
abort();
333333
}
@@ -340,27 +340,27 @@ void RewritePathEvaluator::applyDecompose(const RewriteStep &step,
340340
abort();
341341
}
342342

343-
// Collect the substitutions from the A stack.
343+
// Collect the substitutions from the primary stack.
344344
SmallVector<Term, 2> substitutions;
345345
substitutions.reserve(numSubstitutions);
346346
for (unsigned i = 0; i < numSubstitutions; ++i) {
347-
const auto &substitution = *(A.end() - numSubstitutions + i);
347+
const auto &substitution = *(Primary.end() - numSubstitutions + i);
348348
substitutions.push_back(Term::get(substitution, ctx));
349349
}
350350

351351
// Build the new symbol with the new substitutions.
352352
symbol = symbol.withConcreteSubstitutions(substitutions, ctx);
353353

354-
// Pop the substitutions from the A stack.
355-
A.resize(A.size() - numSubstitutions);
354+
// Pop the substitutions from the primary stack.
355+
Primary.resize(Primary.size() - numSubstitutions);
356356
}
357357
}
358358

359359
void
360360
RewritePathEvaluator::applyConcreteConformance(const RewriteStep &step,
361361
const RewriteSystem &system) {
362-
checkA();
363-
auto &term = A.back();
362+
checkPrimary();
363+
auto &term = Primary.back();
364364
Symbol *last = term.end() - step.EndOffset;
365365

366366
auto &ctx = system.getRewriteContext();
@@ -440,8 +440,8 @@ RewritePathEvaluator::applyConcreteConformance(const RewriteStep &step,
440440

441441
void RewritePathEvaluator::applyConcreteTypeWitness(const RewriteStep &step,
442442
const RewriteSystem &system) {
443-
checkA();
444-
auto &term = A.back();
443+
checkPrimary();
444+
auto &term = Primary.back();
445445

446446
const auto &witness = system.getTypeWitness(step.RuleID);
447447
auto fail = [&]() {
@@ -498,8 +498,8 @@ void RewritePathEvaluator::applyConcreteTypeWitness(const RewriteStep &step,
498498

499499
void RewritePathEvaluator::applySameTypeWitness(const RewriteStep &step,
500500
const RewriteSystem &system) {
501-
checkA();
502-
auto &term = A.back();
501+
checkPrimary();
502+
auto &term = Primary.back();
503503

504504
const auto &witness = system.getTypeWitness(step.RuleID);
505505
auto fail = [&]() {
@@ -571,8 +571,8 @@ void RewritePathEvaluator::applySameTypeWitness(const RewriteStep &step,
571571
void
572572
RewritePathEvaluator::applyAbstractTypeWitness(const RewriteStep &step,
573573
const RewriteSystem &system) {
574-
checkA();
575-
auto &term = A.back();
574+
checkPrimary();
575+
auto &term = Primary.back();
576576

577577
const auto &witness = system.getTypeWitness(step.RuleID);
578578
auto fail = [&]() {

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct RewriteStep {
3838
/// *** Rewrite step kinds introduced by Knuth-Bendix completion ***
3939
///
4040

41-
/// Apply a rewrite rule to the term at the top of the A stack.
41+
/// Apply a rewrite rule to the term at the top of the primary stack.
4242
///
4343
/// Formally, this is a whiskered, oriented rewrite rule. For example,
4444
/// given a rule (X => Y) and the term A.X.B, the application at
@@ -54,7 +54,7 @@ struct RewriteStep {
5454
/// The RuleID field encodes the rule to apply.
5555
ApplyRewriteRule,
5656

57-
/// The term at the top of the A stack must be a term ending with a
57+
/// The term at the top of the primary stack must be a term ending with a
5858
/// superclass or concrete type symbol.
5959
///
6060
/// If not inverted: prepend the prefix to each substitution.
@@ -68,17 +68,17 @@ struct RewriteStep {
6868
/// *** Rewrite step kinds introduced by simplifySubstitutions() ***
6969
///
7070

71-
/// Move a term from the A stack to the B stack (if not inverted) or
72-
/// B stack to A stack (if inverted).
71+
/// Move a term from the primary stack to the secondary stack (if not
72+
/// inverted) or the secondary stack to primary stack (if inverted).
7373
Shift,
7474

75-
/// If not inverted: the top of the A stack must be a term ending with a
76-
/// superclass or concrete type symbol. Each concrete substitution in the
77-
/// term is pushed onto the A stack.
75+
/// If not inverted: the top of the primary stack must be a term ending
76+
/// with a superclass or concrete type symbol. Each concrete substitution
77+
/// in the term is pushed onto the primary stack.
7878
///
79-
/// If inverted: pop concrete substitutions from the A stack, which must
80-
/// follow a term ending with a superclass or concrete type symbol. The
81-
/// new substitutions replace the substitutions in that symbol.
79+
/// If inverted: pop concrete substitutions from the primary stack, which
80+
/// must follow a term ending with a superclass or concrete type symbol.
81+
/// The new substitutions replace the substitutions in that symbol.
8282
///
8383
/// The RuleID field encodes the number of substitutions.
8484
Decompose,
@@ -87,29 +87,29 @@ struct RewriteStep {
8787
/// *** Rewrite step kinds introduced by the property map ***
8888
///
8989

90-
/// If not inverted: the top of the A stack must be a term ending in a
91-
/// concrete type symbol [concrete: C] followed by a protocol symbol [P].
90+
/// If not inverted: the top of the primary stack must be a term ending in
91+
/// a concrete type symbol [concrete: C] followed by a protocol symbol [P].
9292
/// These two symbols are combined into a single concrete conformance
9393
/// symbol [concrete: C : P].
9494
///
95-
/// If inverted: the top of the A stack must be a term ending in a
95+
/// If inverted: the top of the primary stack must be a term ending in a
9696
/// concrete conformance symbol [concrete: C : P]. This symbol is replaced
9797
/// with the concrete type symbol [concrete: C] followed by the protocol
9898
/// symbol [P].
9999
ConcreteConformance,
100100

101-
/// If not inverted: the top of the A stack must be a term ending in a
102-
/// superclass symbol [superclass: C] followed by a protocol symbol [P].
101+
/// If not inverted: the top of the primary stack must be a term ending in
102+
/// a superclass symbol [superclass: C] followed by a protocol symbol [P].
103103
/// These two symbols are combined into a single concrete conformance
104104
/// symbol [concrete: C : P].
105105
///
106-
/// If inverted: the top of the A stack must be a term ending in a
106+
/// If inverted: the top of the primary stack must be a term ending in a
107107
/// concrete conformance symbol [concrete: C : P]. This symbol is replaced
108108
/// with the superclass symbol [superclass: C] followed by the protocol
109109
/// symbol [P].
110110
SuperclassConformance,
111111

112-
/// If not inverted: the top of the A stack must be a term ending in a
112+
/// If not inverted: the top of the primary stack must be a term ending in a
113113
/// concrete conformance symbol [concrete: C : P] followed by an associated
114114
/// type symbol [P:X], and the concrete type symbol [concrete: C.X] for the
115115
/// type witness of 'X' in the conformance 'C : P'. The concrete type symbol
@@ -123,7 +123,7 @@ struct RewriteStep {
123123
/// the step.
124124
ConcreteTypeWitness,
125125

126-
/// If not inverted: the top of the A stack must be a term ending in a
126+
/// If not inverted: the top of the primary stack must be a term ending in a
127127
/// concrete conformance symbol [concrete: C : P] followed by an associated
128128
/// type symbol [P:X]. The associated type symbol is eliminated.
129129
///
@@ -361,32 +361,36 @@ struct AppliedRewriteStep {
361361
/// A rewrite path is a list of instructions for a two-stack interpreter.
362362
///
363363
/// - ApplyRewriteRule and AdjustConcreteType manipulate the term at the top of
364-
/// the A stack.
364+
/// the primary stack.
365365
///
366366
/// - Shift moves a term from A to B (if not inverted) or B to A (if inverted).
367367
///
368368
/// - Decompose splits off the substitutions from a superclass or concrete type
369-
/// symbol at the top of the A stack (if not inverted) or assembles a new
370-
/// superclass or concrete type symbol at the top of the A stack
369+
/// symbol at the top of the primary stack (if not inverted) or assembles a
370+
/// new superclass or concrete type symbol at the top of the primary stack
371371
/// (if inverted).
372372
struct RewritePathEvaluator {
373-
SmallVector<MutableTerm, 2> A;
374-
SmallVector<MutableTerm, 2> B;
373+
/// The primary stack. Most rewrite steps operate on the top of this stack.
374+
SmallVector<MutableTerm, 2> Primary;
375+
376+
/// The secondary stack. The 'Shift' rewrite step moves terms between the
377+
/// primary and secondary stacks.
378+
SmallVector<MutableTerm, 2> Secondary;
375379

376380
explicit RewritePathEvaluator(const MutableTerm &term) {
377-
A.push_back(term);
381+
Primary.push_back(term);
378382
}
379383

380-
void checkA() const;
381-
void checkB() const;
384+
void checkPrimary() const;
385+
void checkSecondary() const;
382386

383387
MutableTerm &getCurrentTerm();
384388

385389
/// We're "in context" if we're in the middle of rewriting concrete
386390
/// substitutions.
387391
bool isInContext() const {
388-
assert(A.size() > 0);
389-
return (A.size() > 1 || B.size() > 0);
392+
assert(Primary.size() > 0);
393+
return (Primary.size() > 1 || Secondary.size() > 0);
390394
}
391395

392396
void apply(const RewriteStep &step,

0 commit comments

Comments
 (0)