Skip to content

Commit 36a7c4c

Browse files
committed
RequirementMachine: Move some code around
1 parent d1cda34 commit 36a7c4c

File tree

2 files changed

+113
-113
lines changed

2 files changed

+113
-113
lines changed

lib/AST/RequirementMachine/GenericSignatureQueries.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,85 @@ RequirementMachine::lookupNestedType(Type depType, Identifier name) const {
664664

665665
return nullptr;
666666
}
667+
668+
void RequirementMachine::verify(const MutableTerm &term) const {
669+
#ifndef NDEBUG
670+
// If the term is in the generic parameter domain, ensure we have a valid
671+
// generic parameter.
672+
if (term.begin()->getKind() == Symbol::Kind::GenericParam) {
673+
auto *genericParam = term.begin()->getGenericParam();
674+
TypeArrayView<GenericTypeParamType> genericParams = getGenericParams();
675+
auto found = std::find(genericParams.begin(),
676+
genericParams.end(),
677+
genericParam);
678+
if (found == genericParams.end()) {
679+
llvm::errs() << "Bad generic parameter in " << term << "\n";
680+
dump(llvm::errs());
681+
abort();
682+
}
683+
}
684+
685+
MutableTerm erased;
686+
687+
// First, "erase" resolved associated types from the term, and try
688+
// to simplify it again.
689+
for (auto symbol : term) {
690+
if (erased.empty()) {
691+
switch (symbol.getKind()) {
692+
case Symbol::Kind::Protocol:
693+
case Symbol::Kind::GenericParam:
694+
erased.add(symbol);
695+
continue;
696+
697+
case Symbol::Kind::AssociatedType:
698+
erased.add(Symbol::forProtocol(symbol.getProtocols()[0], Context));
699+
break;
700+
701+
case Symbol::Kind::Name:
702+
case Symbol::Kind::Layout:
703+
case Symbol::Kind::Superclass:
704+
case Symbol::Kind::ConcreteType:
705+
case Symbol::Kind::ConcreteConformance:
706+
llvm::errs() << "Bad initial symbol in " << term << "\n";
707+
abort();
708+
break;
709+
}
710+
}
711+
712+
switch (symbol.getKind()) {
713+
case Symbol::Kind::Name:
714+
assert(!erased.empty());
715+
erased.add(symbol);
716+
break;
717+
718+
case Symbol::Kind::AssociatedType:
719+
erased.add(Symbol::forName(symbol.getName(), Context));
720+
break;
721+
722+
case Symbol::Kind::Protocol:
723+
case Symbol::Kind::GenericParam:
724+
case Symbol::Kind::Layout:
725+
case Symbol::Kind::Superclass:
726+
case Symbol::Kind::ConcreteType:
727+
case Symbol::Kind::ConcreteConformance:
728+
llvm::errs() << "Bad interior symbol " << symbol << " in " << term << "\n";
729+
abort();
730+
break;
731+
}
732+
}
733+
734+
MutableTerm simplified = erased;
735+
System.simplify(simplified);
736+
737+
// We should end up with the same term.
738+
if (simplified != term) {
739+
llvm::errs() << "Term verification failed\n";
740+
llvm::errs() << "Initial term: " << term << "\n";
741+
llvm::errs() << "Erased term: " << erased << "\n";
742+
llvm::errs() << "Simplified term: " << simplified << "\n";
743+
llvm::errs() << "\n";
744+
dump(llvm::errs());
745+
abort();
746+
}
747+
#endif
748+
}

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 31 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -21,119 +21,6 @@
2121
using namespace swift;
2222
using namespace rewriting;
2323

24-
void RequirementMachine::verify(const MutableTerm &term) const {
25-
#ifndef NDEBUG
26-
// If the term is in the generic parameter domain, ensure we have a valid
27-
// generic parameter.
28-
if (term.begin()->getKind() == Symbol::Kind::GenericParam) {
29-
auto *genericParam = term.begin()->getGenericParam();
30-
TypeArrayView<GenericTypeParamType> genericParams = getGenericParams();
31-
auto found = std::find(genericParams.begin(),
32-
genericParams.end(),
33-
genericParam);
34-
if (found == genericParams.end()) {
35-
llvm::errs() << "Bad generic parameter in " << term << "\n";
36-
dump(llvm::errs());
37-
abort();
38-
}
39-
}
40-
41-
MutableTerm erased;
42-
43-
// First, "erase" resolved associated types from the term, and try
44-
// to simplify it again.
45-
for (auto symbol : term) {
46-
if (erased.empty()) {
47-
switch (symbol.getKind()) {
48-
case Symbol::Kind::Protocol:
49-
case Symbol::Kind::GenericParam:
50-
erased.add(symbol);
51-
continue;
52-
53-
case Symbol::Kind::AssociatedType:
54-
erased.add(Symbol::forProtocol(symbol.getProtocols()[0], Context));
55-
break;
56-
57-
case Symbol::Kind::Name:
58-
case Symbol::Kind::Layout:
59-
case Symbol::Kind::Superclass:
60-
case Symbol::Kind::ConcreteType:
61-
case Symbol::Kind::ConcreteConformance:
62-
llvm::errs() << "Bad initial symbol in " << term << "\n";
63-
abort();
64-
break;
65-
}
66-
}
67-
68-
switch (symbol.getKind()) {
69-
case Symbol::Kind::Name:
70-
assert(!erased.empty());
71-
erased.add(symbol);
72-
break;
73-
74-
case Symbol::Kind::AssociatedType:
75-
erased.add(Symbol::forName(symbol.getName(), Context));
76-
break;
77-
78-
case Symbol::Kind::Protocol:
79-
case Symbol::Kind::GenericParam:
80-
case Symbol::Kind::Layout:
81-
case Symbol::Kind::Superclass:
82-
case Symbol::Kind::ConcreteType:
83-
case Symbol::Kind::ConcreteConformance:
84-
llvm::errs() << "Bad interior symbol " << symbol << " in " << term << "\n";
85-
abort();
86-
break;
87-
}
88-
}
89-
90-
MutableTerm simplified = erased;
91-
System.simplify(simplified);
92-
93-
// We should end up with the same term.
94-
if (simplified != term) {
95-
llvm::errs() << "Term verification failed\n";
96-
llvm::errs() << "Initial term: " << term << "\n";
97-
llvm::errs() << "Erased term: " << erased << "\n";
98-
llvm::errs() << "Simplified term: " << simplified << "\n";
99-
llvm::errs() << "\n";
100-
dump(llvm::errs());
101-
abort();
102-
}
103-
#endif
104-
}
105-
106-
void RequirementMachine::dump(llvm::raw_ostream &out) const {
107-
out << "Requirement machine for ";
108-
if (Sig)
109-
out << Sig;
110-
else if (!Params.empty()) {
111-
out << "fresh signature ";
112-
for (auto paramTy : Params)
113-
out << " " << Type(paramTy);
114-
} else {
115-
assert(!Protos.empty());
116-
out << "protocols [";
117-
for (auto *proto : Protos) {
118-
out << " " << proto->getName();
119-
}
120-
out << " ]";
121-
}
122-
out << "\n";
123-
124-
System.dump(out);
125-
Map.dump(out);
126-
127-
out << "Conformance access paths: {\n";
128-
for (auto pair : ConformanceAccessPaths) {
129-
out << "- " << pair.first.first << " : ";
130-
out << pair.first.second->getName() << " => ";
131-
pair.second.print(out);
132-
out << "\n";
133-
}
134-
out << "}\n";
135-
}
136-
13724
RequirementMachine::RequirementMachine(RewriteContext &ctx)
13825
: Context(ctx), System(ctx), Map(System) {
13926
auto &langOpts = ctx.getASTContext().LangOpts;
@@ -368,3 +255,34 @@ void RequirementMachine::computeCompletion(RewriteSystem::ValidityPolicy policy)
368255
bool RequirementMachine::isComplete() const {
369256
return Complete;
370257
}
258+
259+
void RequirementMachine::dump(llvm::raw_ostream &out) const {
260+
out << "Requirement machine for ";
261+
if (Sig)
262+
out << Sig;
263+
else if (!Params.empty()) {
264+
out << "fresh signature ";
265+
for (auto paramTy : Params)
266+
out << " " << Type(paramTy);
267+
} else {
268+
assert(!Protos.empty());
269+
out << "protocols [";
270+
for (auto *proto : Protos) {
271+
out << " " << proto->getName();
272+
}
273+
out << " ]";
274+
}
275+
out << "\n";
276+
277+
System.dump(out);
278+
Map.dump(out);
279+
280+
out << "Conformance access paths: {\n";
281+
for (auto pair : ConformanceAccessPaths) {
282+
out << "- " << pair.first.first << " : ";
283+
out << pair.first.second->getName() << " => ";
284+
pair.second.print(out);
285+
out << "\n";
286+
}
287+
out << "}\n";
288+
}

0 commit comments

Comments
 (0)