Skip to content

Commit 5ac83e2

Browse files
committed
RequirementMachine: Move buildPropertyMap() from RewriteSystem to PropertyMap
Now that the property map stores a reference to the RewriteSystem, defining the buildPropertyMap() method on PropertyMap feels cleaner, and allows more of the property map's internals to be private.
1 parent b294fe9 commit 5ac83e2

File tree

5 files changed

+46
-52
lines changed

5 files changed

+46
-52
lines changed

lib/AST/RequirementMachine/PropertyMap.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,6 @@ void PropertyMap::addProperty(
296296
props->addProperty(property, Context,
297297
inducedRules, Debug.contains(DebugFlags::ConcreteUnification));
298298
}
299-
void PropertyMap::dump(llvm::raw_ostream &out) const {
300-
out << "Property map: {\n";
301-
for (const auto &props : Entries) {
302-
out << " ";
303-
props->dump(out);
304-
out << "\n";
305-
}
306-
out << "}\n";
307-
}
308299

309300
/// Build the property map from all rules of the form T.[p] => T, where
310301
/// [p] is a property symbol.
@@ -318,19 +309,18 @@ void PropertyMap::dump(llvm::raw_ostream &out) const {
318309
/// left hand side has a length exceeding \p maxDepth.
319310
///
320311
/// Otherwise, the status is CompletionResult::Success.
321-
std::pair<RewriteSystem::CompletionResult, unsigned>
322-
RewriteSystem::buildPropertyMap(PropertyMap &map,
323-
unsigned maxIterations,
324-
unsigned maxDepth) {
325-
map.clear();
312+
std::pair<CompletionResult, unsigned>
313+
PropertyMap::buildPropertyMap(unsigned maxIterations,
314+
unsigned maxDepth) {
315+
clear();
326316

327317
// PropertyMap::addRule() requires that shorter rules are added
328318
// before longer rules, so that it can perform lookups on suffixes and call
329319
// PropertyBag::copyPropertiesFrom(). However, we don't have to perform a
330320
// full sort by term order here; a bucket sort by term length suffices.
331321
SmallVector<std::vector<std::pair<Term, Symbol>>, 4> properties;
332322

333-
for (const auto &rule : Rules) {
323+
for (const auto &rule : System.getRules()) {
334324
if (rule.isSimplified())
335325
continue;
336326

@@ -355,34 +345,44 @@ RewriteSystem::buildPropertyMap(PropertyMap &map,
355345

356346
for (const auto &bucket : properties) {
357347
for (auto pair : bucket) {
358-
map.addProperty(pair.first, pair.second, inducedRules);
348+
addProperty(pair.first, pair.second, inducedRules);
359349
}
360350
}
361351

362352
// We collect terms with fully concrete types so that we can re-use them
363353
// to tie off recursion in the next step.
364-
map.computeConcreteTypeInDomainMap();
354+
computeConcreteTypeInDomainMap();
365355

366356
// Now, we merge concrete type rules with conformance rules, by adding
367357
// relations between associated type members of type parameters with
368358
// the concrete type witnesses in the concrete type's conformance.
369-
map.concretizeNestedTypesFromConcreteParents(inducedRules);
359+
concretizeNestedTypesFromConcreteParents(inducedRules);
370360

371361
// Some of the induced rules might be trivial; only count the induced rules
372362
// where the left hand side is not already equivalent to the right hand side.
373363
unsigned addedNewRules = 0;
374364
for (auto pair : inducedRules) {
375-
if (addRule(pair.first, pair.second)) {
365+
if (System.addRule(pair.first, pair.second)) {
376366
++addedNewRules;
377367

378-
const auto &newRule = Rules.back();
379-
if (newRule.getLHS().size() > maxDepth)
368+
const auto &newRule = System.getRules().back();
369+
if (newRule.getDepth() > maxDepth)
380370
return std::make_pair(CompletionResult::MaxDepth, addedNewRules);
381371
}
382372
}
383373

384-
if (Rules.size() > maxIterations)
374+
if (System.getRules().size() > maxIterations)
385375
return std::make_pair(CompletionResult::MaxIterations, addedNewRules);
386376

387377
return std::make_pair(CompletionResult::Success, addedNewRules);
378+
}
379+
380+
void PropertyMap::dump(llvm::raw_ostream &out) const {
381+
out << "Property map: {\n";
382+
for (const auto &props : Entries) {
383+
out << " ";
384+
props->dump(out);
385+
out << "\n";
386+
}
387+
out << "}\n";
388388
}

lib/AST/RequirementMachine/PropertyMap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,13 @@ class PropertyMap {
163163

164164
PropertyBag *lookUpProperties(const MutableTerm &key) const;
165165

166+
std::pair<CompletionResult, unsigned>
167+
buildPropertyMap(unsigned maxIterations,
168+
unsigned maxDepth);
169+
166170
void dump(llvm::raw_ostream &out) const;
167171

172+
private:
168173
void clear();
169174
void addProperty(Term key, Symbol property,
170175
SmallVectorImpl<std::pair<MutableTerm, MutableTerm>> &inducedRules);
@@ -173,7 +178,6 @@ class PropertyMap {
173178
void concretizeNestedTypesFromConcreteParents(
174179
SmallVectorImpl<std::pair<MutableTerm, MutableTerm>> &inducedRules) const;
175180

176-
private:
177181
void concretizeNestedTypesFromConcreteParent(
178182
Term key, RequirementKind requirementKind,
179183
CanType concreteType, ArrayRef<Term> substitutions,

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,16 @@ void RequirementMachine::computeCompletion(RewriteSystem::ValidityPolicy policy)
528528
// Check for failure.
529529
auto checkCompletionResult = [&]() {
530530
switch (result.first) {
531-
case RewriteSystem::CompletionResult::Success:
531+
case CompletionResult::Success:
532532
break;
533533

534-
case RewriteSystem::CompletionResult::MaxIterations:
534+
case CompletionResult::MaxIterations:
535535
llvm::errs() << "Generic signature " << Sig
536536
<< " exceeds maximum completion step count\n";
537537
System.dump(llvm::errs());
538538
abort();
539539

540-
case RewriteSystem::CompletionResult::MaxDepth:
540+
case CompletionResult::MaxDepth:
541541
llvm::errs() << "Generic signature " << Sig
542542
<< " exceeds maximum completion depth\n";
543543
System.dump(llvm::errs());
@@ -553,8 +553,7 @@ void RequirementMachine::computeCompletion(RewriteSystem::ValidityPolicy policy)
553553
// Build the property map, which also performs concrete term
554554
// unification; if this added any new rules, run the completion
555555
// procedure again.
556-
result = System.buildPropertyMap(
557-
Map,
556+
result = Map.buildPropertyMap(
558557
RequirementMachineStepLimit,
559558
RequirementMachineDepthLimit);
560559

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ class Rule final {
132132
}
133133
};
134134

135+
/// Result type for RewriteSystem::computeConfluentCompletion() and
136+
/// PropertyMap::buildPropertyMap().
137+
enum class CompletionResult {
138+
/// Confluent completion was computed successfully.
139+
Success,
140+
141+
/// Maximum number of iterations reached.
142+
MaxIterations,
143+
144+
/// Completion produced a rewrite rule whose left hand side has a length
145+
/// exceeding the limit.
146+
MaxDepth
147+
};
148+
135149
/// A term rewrite system for working with types in a generic signature.
136150
///
137151
/// Out-of-line methods are documented in RewriteSystem.cpp.
@@ -249,18 +263,6 @@ class RewriteSystem final {
249263
///
250264
//////////////////////////////////////////////////////////////////////////////
251265

252-
enum class CompletionResult {
253-
/// Confluent completion was computed successfully.
254-
Success,
255-
256-
/// Maximum number of iterations reached.
257-
MaxIterations,
258-
259-
/// Completion produced a rewrite rule whose left hand side has a length
260-
/// exceeding the limit.
261-
MaxDepth
262-
};
263-
264266
std::pair<CompletionResult, unsigned>
265267
computeConfluentCompletion(unsigned maxIterations,
266268
unsigned maxDepth);
@@ -385,17 +387,6 @@ class RewriteSystem final {
385387
void computeGeneratingConformances(
386388
llvm::DenseSet<unsigned> &redundantConformances);
387389

388-
//////////////////////////////////////////////////////////////////////////////
389-
///
390-
/// Property map
391-
///
392-
//////////////////////////////////////////////////////////////////////////////
393-
394-
std::pair<CompletionResult, unsigned>
395-
buildPropertyMap(PropertyMap &map,
396-
unsigned maxIterations,
397-
unsigned maxDepth);
398-
399390
void dump(llvm::raw_ostream &out) const;
400391
};
401392

lib/AST/RequirementMachine/RewriteSystemCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ RewriteSystem::computeCriticalPair(ArrayRef<Symbol>::const_iterator from,
490490
/// left hand side has a length exceeding \p maxDepth.
491491
///
492492
/// Otherwise, the status is CompletionResult::Success.
493-
std::pair<RewriteSystem::CompletionResult, unsigned>
493+
std::pair<CompletionResult, unsigned>
494494
RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
495495
unsigned maxDepth) {
496496
assert(Initialized);

0 commit comments

Comments
 (0)