Skip to content

Commit b771145

Browse files
committed
RequirementMachine: Rename RewriteSystemBuilder::AssociatedTypeRules => PermanentRules
1 parent 76361d9 commit b771145

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ struct RewriteSystemBuilder {
4949
llvm::DenseMap<const ProtocolDecl *, bool> ProtocolMap;
5050
std::vector<const ProtocolDecl *> Protocols;
5151

52-
std::vector<std::pair<MutableTerm, MutableTerm>> AssociatedTypeRules;
52+
/// New rules to add which will be marked 'permanent'. These are rules for
53+
/// introducing associated types, and relationships between layout,
54+
/// superclass and concrete type symbols. They are not eliminated by
55+
/// homotopy reduction, since they are always added when the rewrite system
56+
/// is built.
57+
std::vector<std::pair<MutableTerm, MutableTerm>> PermanentRules;
58+
59+
/// New rules derived from requirements written by the user, which can be
60+
/// eliminated by homotopy reduction.
5361
std::vector<std::pair<MutableTerm, MutableTerm>> RequirementRules;
5462

5563
CanType getConcreteSubstitutionSchema(CanType concreteType,
@@ -141,7 +149,7 @@ void RewriteSystemBuilder::addAssociatedType(const AssociatedTypeDecl *type,
141149
MutableTerm rhs;
142150
rhs.add(Symbol::forAssociatedType(proto, type->getName(), Context));
143151

144-
AssociatedTypeRules.emplace_back(lhs, rhs);
152+
PermanentRules.emplace_back(lhs, rhs);
145153
}
146154

147155
/// Lowers a generic requirement to a rewrite rule.
@@ -283,7 +291,7 @@ void RewriteSystemBuilder::processProtocolDependencies() {
283291
MutableTerm rhs;
284292
rhs.add(Symbol::forProtocol(proto, Context));
285293

286-
AssociatedTypeRules.emplace_back(lhs, rhs);
294+
PermanentRules.emplace_back(lhs, rhs);
287295

288296
for (auto *assocType : proto->getAssociatedTypeMembers())
289297
addAssociatedType(assocType, proto);
@@ -457,10 +465,9 @@ void RequirementMachine::initWithGenericSignature(CanGenericSignature sig) {
457465
RewriteSystemBuilder builder(Context, Dump);
458466
builder.addGenericSignature(sig);
459467

460-
// Add the initial set of rewrite rules to the rewrite system, also
461-
// providing the protocol graph to use for the linear order on terms.
468+
// Add the initial set of rewrite rules to the rewrite system.
462469
System.initialize(/*recordLoops=*/false,
463-
std::move(builder.AssociatedTypeRules),
470+
std::move(builder.PermanentRules),
464471
std::move(builder.RequirementRules));
465472

466473
computeCompletion(RewriteSystem::DisallowInvalidRequirements);
@@ -498,10 +505,9 @@ void RequirementMachine::initWithProtocols(ArrayRef<const ProtocolDecl *> protos
498505
RewriteSystemBuilder builder(Context, Dump);
499506
builder.addProtocols(protos);
500507

501-
// Add the initial set of rewrite rules to the rewrite system, also
502-
// providing the protocol graph to use for the linear order on terms.
508+
// Add the initial set of rewrite rules to the rewrite system.
503509
System.initialize(/*recordLoops=*/true,
504-
std::move(builder.AssociatedTypeRules),
510+
std::move(builder.PermanentRules),
505511
std::move(builder.RequirementRules));
506512

507513
// FIXME: Only if the protocols were written in source, though.

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,15 @@ RewriteSystem::~RewriteSystem() {
122122

123123
void RewriteSystem::initialize(
124124
bool recordLoops,
125-
std::vector<std::pair<MutableTerm, MutableTerm>> &&associatedTypeRules,
125+
std::vector<std::pair<MutableTerm, MutableTerm>> &&permanentRules,
126126
std::vector<std::pair<MutableTerm, MutableTerm>> &&requirementRules) {
127127
assert(!Initialized);
128128
Initialized = 1;
129129

130130
RecordLoops = recordLoops;
131131

132-
for (const auto &rule : associatedTypeRules) {
133-
bool added = addRule(rule.first, rule.second);
134-
if (added)
135-
Rules.back().markPermanent();
136-
}
132+
for (const auto &rule : permanentRules)
133+
addPermanentRule(rule.first, rule.second);
137134

138135
for (const auto &rule : requirementRules)
139136
addRule(rule.first, rule.second);
@@ -406,6 +403,15 @@ bool RewriteSystem::addRule(MutableTerm lhs, MutableTerm rhs,
406403
return true;
407404
}
408405

406+
/// Add a new rule, marking it permanent.
407+
bool RewriteSystem::addPermanentRule(MutableTerm lhs, MutableTerm rhs) {
408+
bool added = addRule(std::move(lhs), std::move(rhs));
409+
if (added)
410+
Rules.back().markPermanent();
411+
412+
return added;
413+
}
414+
409415
/// Delete any rules whose left hand sides can be reduced by other rules,
410416
/// and reduce the right hand sides of all remaining rules as much as
411417
/// possible.

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class RewriteSystem final {
230230
RewriteContext &getRewriteContext() const { return Context; }
231231

232232
void initialize(bool recordLoops,
233-
std::vector<std::pair<MutableTerm, MutableTerm>> &&assocaitedTypeRules,
233+
std::vector<std::pair<MutableTerm, MutableTerm>> &&permanentRules,
234234
std::vector<std::pair<MutableTerm, MutableTerm>> &&requirementRules);
235235

236236
unsigned getRuleID(const Rule &rule) const {
@@ -253,6 +253,8 @@ class RewriteSystem final {
253253
bool addRule(MutableTerm lhs, MutableTerm rhs,
254254
const RewritePath *path=nullptr);
255255

256+
bool addPermanentRule(MutableTerm lhs, MutableTerm rhs);
257+
256258
bool simplify(MutableTerm &term, RewritePath *path=nullptr) const;
257259

258260
void simplifySubstitutions(MutableTerm &term, RewritePath &path) const;

0 commit comments

Comments
 (0)