Skip to content

Commit 8e3f350

Browse files
committed
RequirementMachine: Make the rewrite path optional with RewriteSystem::simplifySubstitution()
1 parent 17664c0 commit 8e3f350

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ bool RewriteSystem::simplify(MutableTerm &term, RewritePath *path) const {
254254
/// Simplify terms appearing in the substitutions of the last symbol of \p term,
255255
/// which must be a superclass or concrete type symbol.
256256
bool RewriteSystem::simplifySubstitutions(Symbol &symbol,
257-
RewritePath &path) const {
257+
RewritePath *path) const {
258258
assert(symbol.hasSubstitutions());
259259

260260
auto substitutions = symbol.getSubstitutions();
@@ -263,14 +263,16 @@ bool RewriteSystem::simplifySubstitutions(Symbol &symbol,
263263

264264
// Save the original rewrite path length so that we can reset if if we don't
265265
// find anything to simplify.
266-
unsigned oldSize = path.size();
266+
unsigned oldSize = (path ? path->size() : 0);
267267

268-
// The term is on the A stack. Push all substitutions onto the A stack.
269-
path.add(RewriteStep::forDecompose(substitutions.size(), /*inverse=*/false));
268+
if (path) {
269+
// The term is on the A stack. Push all substitutions onto the A stack.
270+
path->add(RewriteStep::forDecompose(substitutions.size(), /*inverse=*/false));
270271

271-
// Move all substitutions but the first one to the B stack.
272-
for (unsigned i = 1; i < substitutions.size(); ++i)
273-
path.add(RewriteStep::forShift(/*inverse=*/false));
272+
// Move all substitutions but the first one to the B stack.
273+
for (unsigned i = 1; i < substitutions.size(); ++i)
274+
path->add(RewriteStep::forShift(/*inverse=*/false));
275+
}
274276

275277
// Simplify and collect substitutions.
276278
SmallVector<Term, 2> newSubstitutions;
@@ -280,34 +282,37 @@ bool RewriteSystem::simplifySubstitutions(Symbol &symbol,
280282
bool anyChanged = false;
281283
for (auto substitution : substitutions) {
282284
// Move the next substitution from the B stack to the A stack.
283-
if (!first)
284-
path.add(RewriteStep::forShift(/*inverse=*/true));
285+
if (!first && path)
286+
path->add(RewriteStep::forShift(/*inverse=*/true));
285287
first = false;
286288

287289
// The current substitution is at the top of the A stack; simplify it.
288290
MutableTerm mutTerm(substitution);
289-
anyChanged |= simplify(mutTerm, &path);
291+
anyChanged |= simplify(mutTerm, path);
290292

291293
// Record the new substitution.
292294
newSubstitutions.push_back(Term::get(mutTerm, Context));
293295
}
294296

295297
// All simplified substitutions are now on the A stack. Collect them to
296298
// produce the new term.
297-
path.add(RewriteStep::forDecompose(substitutions.size(), /*inverse=*/true));
299+
if (path)
300+
path->add(RewriteStep::forDecompose(substitutions.size(), /*inverse=*/true));
298301

299302
// If nothing changed, we don't have to rebuild the symbol.
300303
if (!anyChanged) {
301-
// The rewrite path should consist of a Decompose, followed by a number
302-
// of Shifts, followed by a Compose.
303-
#ifndef NDEBUG
304-
for (auto iter = path.begin() + oldSize; iter < path.end(); ++iter) {
305-
assert(iter->Kind == RewriteStep::Shift ||
306-
iter->Kind == RewriteStep::Decompose);
307-
}
308-
#endif
304+
if (path) {
305+
// The rewrite path should consist of a Decompose, followed by a number
306+
// of Shifts, followed by a Compose.
307+
#ifndef NDEBUG
308+
for (auto iter = path->begin() + oldSize; iter < path->end(); ++iter) {
309+
assert(iter->Kind == RewriteStep::Shift ||
310+
iter->Kind == RewriteStep::Decompose);
311+
}
312+
#endif
309313

310-
path.resize(oldSize);
314+
path->resize(oldSize);
315+
}
311316
return false;
312317
}
313318

@@ -571,7 +576,7 @@ void RewriteSystem::simplifyRightHandSidesAndSubstitutions() {
571576
ruleID, /*inverse=*/true));
572577

573578
// (2) Now, simplify the substitutions to get the new lhs.
574-
if (!simplifySubstitutions(symbol, path))
579+
if (!simplifySubstitutions(symbol, &path))
575580
continue;
576581

577582
// We're either going to add a new rule or record an identity, so

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class RewriteSystem final {
259259

260260
bool simplify(MutableTerm &term, RewritePath *path=nullptr) const;
261261

262-
bool simplifySubstitutions(Symbol &symbol, RewritePath &path) const;
262+
bool simplifySubstitutions(Symbol &symbol, RewritePath *path=nullptr) const;
263263

264264
//////////////////////////////////////////////////////////////////////////////
265265
///

0 commit comments

Comments
 (0)