Skip to content

Commit 220d488

Browse files
committed
RequirementMachine: Move some code to a new PropertyRelations.cpp file
1 parent a7091d9 commit 220d488

File tree

3 files changed

+91
-71
lines changed

3 files changed

+91
-71
lines changed

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_swift_host_library(swiftAST STATIC
7979
RequirementMachine/KnuthBendix.cpp
8080
RequirementMachine/MinimalConformances.cpp
8181
RequirementMachine/PropertyMap.cpp
82+
RequirementMachine/PropertyRelations.cpp
8283
RequirementMachine/PropertyUnification.cpp
8384
RequirementMachine/RequirementLowering.cpp
8485
RequirementMachine/RequirementMachine.cpp
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===--- PropertyRelations.cpp - Relations between property rules ---------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/Type.h"
14+
#include "llvm/Support/raw_ostream.h"
15+
#include <algorithm>
16+
#include "RewriteSystem.h"
17+
18+
using namespace swift;
19+
using namespace rewriting;
20+
21+
unsigned RewriteSystem::recordRelation(Symbol lhs, Symbol rhs) {
22+
auto key = std::make_pair(lhs, rhs);
23+
auto found = RelationMap.find(key);
24+
if (found != RelationMap.end())
25+
return found->second;
26+
27+
unsigned index = Relations.size();
28+
Relations.push_back(key);
29+
auto inserted = RelationMap.insert(std::make_pair(key, index));
30+
assert(inserted.second);
31+
(void) inserted;
32+
33+
return index;
34+
}
35+
36+
RewriteSystem::Relation
37+
RewriteSystem::getRelation(unsigned index) const {
38+
return Relations[index];
39+
}
40+
41+
RewriteSystem::TypeWitness::TypeWitness(
42+
Term lhs, llvm::PointerUnion<Symbol, Term> rhs)
43+
: LHS(lhs), RHS(rhs) {
44+
assert(LHS.size() >= 2);
45+
assert(getConcreteConformance().getKind() ==
46+
Symbol::Kind::ConcreteConformance);
47+
assert(getAssocType().getKind() == Symbol::Kind::AssociatedType);
48+
if (RHS.is<Symbol>())
49+
assert(RHS.get<Symbol>().getKind() == Symbol::Kind::ConcreteType);
50+
assert(getAssocType().getProtocols().size() == 1);
51+
assert(getAssocType().getProtocols()[0] ==
52+
getConcreteConformance().getProtocol());
53+
}
54+
55+
namespace swift {
56+
namespace rewriting {
57+
bool operator==(const RewriteSystem::TypeWitness &lhs,
58+
const RewriteSystem::TypeWitness &rhs) {
59+
return lhs.LHS == rhs.LHS && lhs.RHS == rhs.RHS;
60+
}
61+
}
62+
}
63+
64+
void RewriteSystem::TypeWitness::dump(llvm::raw_ostream &out) const {
65+
out << "Subject type: " << LHS << "\n";
66+
if (RHS.is<Symbol>())
67+
out << "Concrete type witness: " << RHS.get<Symbol>() << "\n";
68+
else
69+
out << "Abstract type witness: " << RHS.get<Term>() << "\n";
70+
}
71+
72+
unsigned RewriteSystem::recordTypeWitness(
73+
RewriteSystem::TypeWitness witness) {
74+
unsigned index = TypeWitnesses.size();
75+
auto inserted = TypeWitnessMap.insert(std::make_pair(witness.LHS, index));
76+
77+
if (!inserted.second) {
78+
index = inserted.first->second;
79+
} else {
80+
TypeWitnesses.push_back(witness);
81+
}
82+
83+
assert(TypeWitnesses[index] == witness);
84+
return index;
85+
}
86+
87+
const RewriteSystem::TypeWitness &
88+
RewriteSystem::getTypeWitness(unsigned index) const {
89+
return TypeWitnesses[index];
90+
}

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@ bool PropertyMap::checkRulePairOnce(unsigned firstRuleID,
4949
std::make_pair(firstRuleID, secondRuleID)).second;
5050
}
5151

52-
unsigned RewriteSystem::recordRelation(Symbol lhs, Symbol rhs) {
53-
auto key = std::make_pair(lhs, rhs);
54-
auto found = RelationMap.find(key);
55-
if (found != RelationMap.end())
56-
return found->second;
57-
58-
unsigned index = Relations.size();
59-
Relations.push_back(key);
60-
auto inserted = RelationMap.insert(std::make_pair(key, index));
61-
assert(inserted.second);
62-
(void) inserted;
63-
64-
return index;
65-
}
66-
67-
RewriteSystem::Relation
68-
RewriteSystem::getRelation(unsigned index) const {
69-
return Relations[index];
70-
}
71-
7252
/// Given a key T, a rule (V.[p1] => V) where T == U.V, and a property [p2]
7353
/// where [p1] < [p2], record a rule (T.[p2] => T) that is induced by
7454
/// the original rule (V.[p1] => V).
@@ -814,57 +794,6 @@ void PropertyMap::concretizeTypeWitnessInConformance(
814794
}
815795
}
816796

817-
RewriteSystem::TypeWitness::TypeWitness(
818-
Term lhs, llvm::PointerUnion<Symbol, Term> rhs)
819-
: LHS(lhs), RHS(rhs) {
820-
assert(LHS.size() >= 2);
821-
assert(getConcreteConformance().getKind() ==
822-
Symbol::Kind::ConcreteConformance);
823-
assert(getAssocType().getKind() == Symbol::Kind::AssociatedType);
824-
if (RHS.is<Symbol>())
825-
assert(RHS.get<Symbol>().getKind() == Symbol::Kind::ConcreteType);
826-
assert(getAssocType().getProtocols().size() == 1);
827-
assert(getAssocType().getProtocols()[0] ==
828-
getConcreteConformance().getProtocol());
829-
}
830-
831-
namespace swift {
832-
namespace rewriting {
833-
bool operator==(const RewriteSystem::TypeWitness &lhs,
834-
const RewriteSystem::TypeWitness &rhs) {
835-
return lhs.LHS == rhs.LHS && lhs.RHS == rhs.RHS;
836-
}
837-
}
838-
}
839-
840-
void RewriteSystem::TypeWitness::dump(llvm::raw_ostream &out) const {
841-
out << "Subject type: " << LHS << "\n";
842-
if (RHS.is<Symbol>())
843-
out << "Concrete type witness: " << RHS.get<Symbol>() << "\n";
844-
else
845-
out << "Abstract type witness: " << RHS.get<Term>() << "\n";
846-
}
847-
848-
unsigned RewriteSystem::recordTypeWitness(
849-
RewriteSystem::TypeWitness witness) {
850-
unsigned index = TypeWitnesses.size();
851-
auto inserted = TypeWitnessMap.insert(std::make_pair(witness.LHS, index));
852-
853-
if (!inserted.second) {
854-
index = inserted.first->second;
855-
} else {
856-
TypeWitnesses.push_back(witness);
857-
}
858-
859-
assert(TypeWitnesses[index] == witness);
860-
return index;
861-
}
862-
863-
const RewriteSystem::TypeWitness &
864-
RewriteSystem::getTypeWitness(unsigned index) const {
865-
return TypeWitnesses[index];
866-
}
867-
868797
/// Given the key of a property bag known to have \p concreteType,
869798
/// together with a \p typeWitness from a conformance on that concrete
870799
/// type, return the right hand side of a rewrite rule to relate

0 commit comments

Comments
 (0)