Skip to content

Commit 1fa36c7

Browse files
committed
RequirementMachine: Factor out Rule::isPropertyRule() from the property map
1 parent 67fad89 commit 1fa36c7

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

lib/AST/RequirementMachine/PropertyMap.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,23 +1043,16 @@ RewriteSystem::buildPropertyMap(PropertyMap &map,
10431043
if (rule.isDeleted())
10441044
continue;
10451045

1046-
auto lhs = rule.getLHS();
1047-
auto rhs = rule.getRHS();
1048-
10491046
// Collect all rules of the form T.[p] => T where T is canonical.
1050-
auto property = lhs.back();
1051-
if (!property.isProperty())
1052-
continue;
1053-
1054-
if (lhs.size() - 1 != rhs.size())
1047+
auto property = rule.isPropertyRule();
1048+
if (!property)
10551049
continue;
10561050

1057-
if (!std::equal(rhs.begin(), rhs.end(), lhs.begin()))
1058-
continue;
1059-
1060-
if (rhs.size() >= properties.size())
1061-
properties.resize(rhs.size() + 1);
1062-
properties[rhs.size()].emplace_back(rhs, property);
1051+
auto rhs = rule.getRHS();
1052+
unsigned length = rhs.size();
1053+
if (length >= properties.size())
1054+
properties.resize(length + 1);
1055+
properties[length].emplace_back(rhs, *property);
10631056
}
10641057

10651058
// Merging multiple superclass or concrete type rules can induce new rules

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
2323
using namespace swift;
2424
using namespace rewriting;
2525

26+
Optional<Symbol> Rule::isPropertyRule() const {
27+
auto property = LHS.back();
28+
29+
if (!property.isProperty())
30+
return None;
31+
32+
if (LHS.size() - 1 != RHS.size())
33+
return None;
34+
35+
if (!std::equal(RHS.begin(), RHS.end(), LHS.begin()))
36+
return None;
37+
38+
return property;
39+
}
40+
41+
bool Rule::isProtocolConformanceRule() const {
42+
if (auto property = isPropertyRule())
43+
return property->getKind() == Symbol::Kind::Protocol;
44+
45+
return false;
46+
}
47+
2648
void Rule::dump(llvm::raw_ostream &out) const {
2749
out << LHS << " => " << RHS;
2850
if (deleted)

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class Rule final {
5050
const Term &getLHS() const { return LHS; }
5151
const Term &getRHS() const { return RHS; }
5252

53+
Optional<Symbol> isPropertyRule() const;
54+
55+
bool isProtocolConformanceRule() const;
56+
5357
/// Returns if the rule was deleted.
5458
bool isDeleted() const {
5559
return deleted;

0 commit comments

Comments
 (0)