Skip to content

Commit 08cc2a6

Browse files
committed
RequirementMachine: Rename Term to MutableTerm
MutableTerm will be used for temporaries; I'm going to add a new Term type for uniqued permanently-allocated terms shortly.
1 parent b631480 commit 08cc2a6

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

include/swift/AST/RewriteSystem.h

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -269,29 +269,32 @@ class Atom final {
269269

270270
/// A term is a sequence of one or more atoms.
271271
///
272+
/// The MutableTerm type is a dynamically-allocated representation,
273+
/// used to represent temporary values in simplification and completion.
274+
///
272275
/// The first atom in the term must be a protocol, generic parameter, or
273276
/// associated type atom.
274277
///
275278
/// A layout constraint atom must only appear at the end of a term.
276279
///
277280
/// Out-of-line methods are documented in RewriteSystem.cpp.
278-
class Term final {
281+
class MutableTerm final {
279282
llvm::SmallVector<Atom, 3> Atoms;
280283

281284
public:
282-
Term() {}
285+
MutableTerm() {}
283286

284-
explicit Term(llvm::SmallVector<Atom, 3> &&atoms)
287+
explicit MutableTerm(llvm::SmallVector<Atom, 3> &&atoms)
285288
: Atoms(std::move(atoms)) {}
286289

287-
explicit Term(ArrayRef<Atom> atoms)
290+
explicit MutableTerm(ArrayRef<Atom> atoms)
288291
: Atoms(atoms.begin(), atoms.end()) {}
289292

290293
void add(Atom atom) {
291294
Atoms.push_back(atom);
292295
}
293296

294-
int compare(const Term &other, const ProtocolGraph &protos) const;
297+
int compare(const MutableTerm &other, const ProtocolGraph &protos) const;
295298

296299
size_t size() const { return Atoms.size(); }
297300

@@ -317,18 +320,20 @@ class Term final {
317320
return Atoms[index];
318321
}
319322

320-
decltype(Atoms)::const_iterator findSubTerm(const Term &other) const;
323+
decltype(Atoms)::const_iterator findSubTerm(
324+
const MutableTerm &other) const;
321325

322-
decltype(Atoms)::iterator findSubTerm(const Term &other);
326+
decltype(Atoms)::iterator findSubTerm(
327+
const MutableTerm &other);
323328

324329
/// Returns true if this term contains, or is equal to, \p other.
325-
bool containsSubTerm(const Term &other) const {
330+
bool containsSubTerm(const MutableTerm &other) const {
326331
return findSubTerm(other) != end();
327332
}
328333

329-
bool rewriteSubTerm(const Term &lhs, const Term &rhs);
334+
bool rewriteSubTerm(const MutableTerm &lhs, const MutableTerm &rhs);
330335

331-
bool checkForOverlap(const Term &other, Term &result) const;
336+
bool checkForOverlap(const MutableTerm &other, MutableTerm &result) const;
332337

333338
void dump(llvm::raw_ostream &out) const;
334339
};
@@ -358,8 +363,8 @@ class RewriteContext final {
358363

359364
RewriteContext(UnifiedStatsReporter *stats) : Stats(stats) {}
360365

361-
Term getTermForType(CanType paramType,
362-
const ProtocolDecl *proto);
366+
MutableTerm getTermForType(CanType paramType,
367+
const ProtocolDecl *proto);
363368
};
364369

365370
/// A rewrite rule that replaces occurrences of LHS with RHS.
@@ -368,22 +373,22 @@ class RewriteContext final {
368373
///
369374
/// Out-of-line methods are documented in RewriteSystem.cpp.
370375
class Rule final {
371-
Term LHS;
372-
Term RHS;
376+
MutableTerm LHS;
377+
MutableTerm RHS;
373378
bool deleted;
374379

375380
public:
376-
Rule(const Term &lhs, const Term &rhs)
381+
Rule(const MutableTerm &lhs, const MutableTerm &rhs)
377382
: LHS(lhs), RHS(rhs), deleted(false) {}
378383

379-
const Term &getLHS() const { return LHS; }
380-
const Term &getRHS() const { return RHS; }
384+
const MutableTerm &getLHS() const { return LHS; }
385+
const MutableTerm &getRHS() const { return RHS; }
381386

382-
bool apply(Term &term) const {
387+
bool apply(MutableTerm &term) const {
383388
return term.rewriteSubTerm(LHS, RHS);
384389
}
385390

386-
bool checkForOverlap(const Rule &other, Term &result) const {
391+
bool checkForOverlap(const Rule &other, MutableTerm &result) const {
387392
return LHS.checkForOverlap(other.LHS, result);
388393
}
389394

@@ -445,7 +450,7 @@ class RewriteSystem final {
445450
/// - the last atom in both lhs and rhs has the same name
446451
///
447452
/// See RewriteSystem::processMergedAssociatedTypes() for details.
448-
std::vector<std::pair<Term, Term>> MergedAssociatedTypes;
453+
std::vector<std::pair<MutableTerm, MutableTerm>> MergedAssociatedTypes;
449454

450455
/// A list of pending pairs for checking overlap in the completion
451456
/// procedure.
@@ -476,12 +481,12 @@ class RewriteSystem final {
476481
/// Return the object recording information about known protocols.
477482
const ProtocolGraph &getProtocols() const { return Protos; }
478483

479-
void initialize(std::vector<std::pair<Term, Term>> &&rules,
484+
void initialize(std::vector<std::pair<MutableTerm, MutableTerm>> &&rules,
480485
ProtocolGraph &&protos);
481486

482-
bool addRule(Term lhs, Term rhs);
487+
bool addRule(MutableTerm lhs, MutableTerm rhs);
483488

484-
bool simplify(Term &term) const;
489+
bool simplify(MutableTerm &term) const;
485490

486491
enum class CompletionResult {
487492
/// Confluent completion was computed successfully.

lib/AST/RequirementMachine.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct RewriteSystemBuilder {
3535
bool Debug;
3636

3737
ProtocolGraph Protocols;
38-
std::vector<std::pair<Term, Term>> Rules;
38+
std::vector<std::pair<MutableTerm, MutableTerm>> Rules;
3939

4040
RewriteSystemBuilder(RewriteContext &ctx, bool debug)
4141
: Context(ctx), Debug(debug) {}
@@ -88,11 +88,11 @@ void RewriteSystemBuilder::addGenericSignature(CanGenericSignature sig) {
8888
/// named T".
8989
void RewriteSystemBuilder::addAssociatedType(const AssociatedTypeDecl *type,
9090
const ProtocolDecl *proto) {
91-
Term lhs;
91+
MutableTerm lhs;
9292
lhs.add(Atom::forProtocol(proto, Context));
9393
lhs.add(Atom::forName(type->getName(), Context));
9494

95-
Term rhs;
95+
MutableTerm rhs;
9696
rhs.add(Atom::forAssociatedType(proto, type->getName(), Context));
9797

9898
Rules.emplace_back(lhs, rhs);

lib/AST/RewriteSystem.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ void Atom::dump(llvm::raw_ostream &out) const {
267267
///
268268
/// First we compare length, then perform a lexicographic comparison
269269
/// on atoms if the two terms have the same length.
270-
int Term::compare(const Term &other, const ProtocolGraph &graph) const {
270+
int MutableTerm::compare(const MutableTerm &other,
271+
const ProtocolGraph &graph) const {
271272
if (size() != other.size())
272273
return size() < other.size() ? -1 : 1;
273274

@@ -289,17 +290,17 @@ int Term::compare(const Term &other, const ProtocolGraph &graph) const {
289290

290291
/// Find the start of \p other in this term, returning end() if
291292
/// \p other does not occur as a subterm of this term.
292-
decltype(Term::Atoms)::const_iterator
293-
Term::findSubTerm(const Term &other) const {
293+
decltype(MutableTerm::Atoms)::const_iterator
294+
MutableTerm::findSubTerm(const MutableTerm &other) const {
294295
if (other.size() > size())
295296
return end();
296297

297298
return std::search(begin(), end(), other.begin(), other.end());
298299
}
299300

300301
/// Non-const variant of the above.
301-
decltype(Term::Atoms)::iterator
302-
Term::findSubTerm(const Term &other) {
302+
decltype(MutableTerm::Atoms)::iterator
303+
MutableTerm::findSubTerm(const MutableTerm &other) {
303304
if (other.size() > size())
304305
return end();
305306

@@ -311,7 +312,8 @@ Term::findSubTerm(const Term &other) {
311312
/// order on terms. Returns true if the term contained \p lhs;
312313
/// otherwise returns false, in which case the term remains
313314
/// unchanged.
314-
bool Term::rewriteSubTerm(const Term &lhs, const Term &rhs) {
315+
bool MutableTerm::rewriteSubTerm(const MutableTerm &lhs,
316+
const MutableTerm &rhs) {
315317
// Find the start of lhs in this term.
316318
auto found = findSubTerm(lhs);
317319

@@ -362,7 +364,8 @@ bool Term::rewriteSubTerm(const Term &lhs, const Term &rhs) {
362364
///
363365
/// Note that this relation is not commutative; we need to check
364366
/// for overlap between both (X and Y) and (Y and X).
365-
bool Term::checkForOverlap(const Term &other, Term &result) const {
367+
bool MutableTerm::checkForOverlap(const MutableTerm &other,
368+
MutableTerm &result) const {
366369
assert(result.size() == 0);
367370

368371
// If the other term is longer than this term, there's no way
@@ -420,7 +423,7 @@ bool Term::checkForOverlap(const Term &other, Term &result) const {
420423
return false;
421424
}
422425

423-
void Term::dump(llvm::raw_ostream &out) const {
426+
void MutableTerm::dump(llvm::raw_ostream &out) const {
424427
bool first = true;
425428

426429
for (auto atom : Atoms) {
@@ -445,8 +448,8 @@ void Term::dump(llvm::raw_ostream &out) const {
445448
/// The bound associated types in the interface type are ignored; the
446449
/// resulting term consists entirely of a root atom followed by zero
447450
/// or more name atoms.
448-
Term RewriteContext::getTermForType(CanType paramType,
449-
const ProtocolDecl *proto) {
451+
MutableTerm RewriteContext::getTermForType(CanType paramType,
452+
const ProtocolDecl *proto) {
450453
assert(paramType->isTypeParameter());
451454

452455
// Collect zero or more nested type names in reverse order.
@@ -467,7 +470,7 @@ Term RewriteContext::getTermForType(CanType paramType,
467470

468471
std::reverse(atoms.begin(), atoms.end());
469472

470-
return Term(atoms);
473+
return MutableTerm(atoms);
471474
}
472475

473476
void Rule::dump(llvm::raw_ostream &out) const {
@@ -478,21 +481,22 @@ void Rule::dump(llvm::raw_ostream &out) const {
478481
out << " [deleted]";
479482
}
480483

481-
void RewriteSystem::initialize(std::vector<std::pair<Term, Term>> &&rules,
482-
ProtocolGraph &&graph) {
484+
void RewriteSystem::initialize(
485+
std::vector<std::pair<MutableTerm, MutableTerm>> &&rules,
486+
ProtocolGraph &&graph) {
483487
Protos = graph;
484488

485489
// FIXME: Probably this sort is not necessary
486490
std::sort(rules.begin(), rules.end(),
487-
[&](std::pair<Term, Term> lhs,
488-
std::pair<Term, Term> rhs) -> int {
491+
[&](std::pair<MutableTerm, MutableTerm> lhs,
492+
std::pair<MutableTerm, MutableTerm> rhs) -> int {
489493
return lhs.first.compare(rhs.first, graph) < 0;
490494
});
491495
for (const auto &rule : rules)
492496
addRule(rule.first, rule.second);
493497
}
494498

495-
bool RewriteSystem::addRule(Term lhs, Term rhs) {
499+
bool RewriteSystem::addRule(MutableTerm lhs, MutableTerm rhs) {
496500
// Simplify the rule as much as possible with the rules we have so far.
497501
//
498502
// This avoids unnecessary work in the completion algorithm.
@@ -564,7 +568,7 @@ bool RewriteSystem::addRule(Term lhs, Term rhs) {
564568
}
565569

566570
/// Reduce a term by applying all rewrite rules until fixed point.
567-
bool RewriteSystem::simplify(Term &term) const {
571+
bool RewriteSystem::simplify(MutableTerm &term) const {
568572
bool changed = false;
569573

570574
if (DebugSimplify) {
@@ -754,7 +758,7 @@ void RewriteSystem::processMergedAssociatedTypes() {
754758
}
755759

756760
// Build the term X.[P1&P2:T].
757-
Term mergedTerm = lhs;
761+
MutableTerm mergedTerm = lhs;
758762
mergedTerm.back() = mergedAtom;
759763

760764
// Add the rule X.[P1:T] => X.[P1&P2:T].
@@ -789,11 +793,11 @@ void RewriteSystem::processMergedAssociatedTypes() {
789793
//
790794
// [P1&P2].[Q] => [P1&P2]
791795
//
792-
Term newLHS;
796+
MutableTerm newLHS;
793797
newLHS.add(mergedAtom);
794798
newLHS.add(otherLHS[1]);
795799

796-
Term newRHS;
800+
MutableTerm newRHS;
797801
newRHS.add(mergedAtom);
798802

799803
addRule(newLHS, newRHS);
@@ -832,7 +836,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
832836
auto pair = Worklist.front();
833837
Worklist.pop_front();
834838

835-
Term first;
839+
MutableTerm first;
836840

837841
const auto &lhs = Rules[pair.first];
838842
const auto &rhs = Rules[pair.second];
@@ -867,7 +871,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
867871
//
868872
// 1) lhs == TUV and rhs == U
869873
// 2) lhs == TU and rhs == UV
870-
Term second = first;
874+
MutableTerm second = first;
871875

872876
// In both cases, rewrite the term TUV using both rules to
873877
// produce two new terms X and Y.

0 commit comments

Comments
 (0)