Skip to content

Commit 0c53543

Browse files
committed
Sema: More general dumpActiveScopeChanges()
1 parent 105b6b3 commit 0c53543

File tree

2 files changed

+73
-116
lines changed

2 files changed

+73
-116
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ class SolverTrail {
120120
/// had prior to this change.
121121
///
122122
/// Changes must be undone in stack order.
123-
void undo(ConstraintSystem &cs);
123+
void undo(ConstraintSystem &cs) const;
124+
125+
void dump(llvm::raw_ostream &out, ConstraintSystem &cs,
126+
unsigned indent = 0) const;
124127
};
125128

126129
SolverTrail(ConstraintSystem &cs) : CS(cs) {}
@@ -134,7 +137,7 @@ class SolverTrail {
134137

135138
void dumpActiveScopeChanges(llvm::raw_ostream &out,
136139
unsigned fromIndex,
137-
unsigned indent = 0);
140+
unsigned indent = 0) const;
138141

139142
unsigned size() const {
140143
return Changes.size();

lib/Sema/CSTrail.cpp

Lines changed: 68 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ SolverTrail::Change::updatedTypeVariable(
9898
return result;
9999
}
100100

101-
void SolverTrail::Change::undo(ConstraintSystem &cs) {
101+
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
102102
auto &cg = cs.getConstraintGraph();
103103

104104
switch (Kind) {
@@ -131,6 +131,68 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) {
131131
}
132132
}
133133

134+
void SolverTrail::Change::dump(llvm::raw_ostream &out,
135+
ConstraintSystem &cs,
136+
unsigned indent) const {
137+
PrintOptions PO;
138+
PO.PrintTypesForDebugging = true;
139+
140+
out.indent(indent);
141+
142+
switch (Kind) {
143+
case ChangeKind::AddedTypeVariable:
144+
out << "(added type variable ";
145+
TypeVar->print(out, PO);
146+
out << ")\n";
147+
break;
148+
149+
case ChangeKind::AddedConstraint:
150+
out << "(added constraint ";
151+
TheConstraint->print(out, &cs.getASTContext().SourceMgr, indent + 2);
152+
out << ")\n";
153+
break;
154+
155+
case ChangeKind::RemovedConstraint:
156+
out << "(removed constraint ";
157+
TheConstraint->print(out, &cs.getASTContext().SourceMgr, indent + 2);
158+
out << ")\n";
159+
break;
160+
161+
case ChangeKind::ExtendedEquivalenceClass: {
162+
out << "(equivalence ";
163+
EquivClass.TypeVar->print(out, PO);
164+
out << " " << EquivClass.PrevSize << ")\n";
165+
break;
166+
}
167+
168+
case ChangeKind::BoundTypeVariable:
169+
out << "(bound type variable ";
170+
Binding.TypeVar->print(out, PO);
171+
out << " to fixed type ";
172+
Binding.FixedType->print(out, PO);
173+
out << ")\n";
174+
break;
175+
176+
case ChangeKind::UpdatedTypeVariable:
177+
out << "(updated type variable ";
178+
Update.TypeVar->print(out, PO);
179+
180+
auto parentOrFixed = Update.TypeVar->getImpl().ParentOrFixed;
181+
if (auto *parent = parentOrFixed.dyn_cast<TypeVariableType *>()) {
182+
out << " to parent ";
183+
parent->print(out, PO);
184+
}
185+
else {
186+
out << " to fixed type ";
187+
parentOrFixed.get<TypeBase *>()->print(out, PO);
188+
}
189+
out << " with options 0x";
190+
out.write_hex(Update.Options);
191+
out << ")\n";
192+
break;
193+
}
194+
}
195+
134196
void SolverTrail::recordChange(Change change) {
135197
if (UndoActive)
136198
return;
@@ -166,121 +228,13 @@ void SolverTrail::undo(unsigned toIndex) {
166228

167229
void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
168230
unsigned fromIndex,
169-
unsigned indent) {
170-
if (Changes.empty())
171-
return;
172-
173-
// Collect Changes for printing.
174-
std::map<TypeVariableType *, TypeBase *> tvWithboundTypes;
175-
std::vector<TypeVariableType *> addedTypeVars;
176-
std::vector<TypeVariableType *> equivTypeVars;
177-
std::set<Constraint *> addedConstraints;
178-
std::set<Constraint *> removedConstraints;
179-
for (unsigned int i = fromIndex; i < Changes.size(); i++) {
180-
auto change = Changes[i];
181-
switch (change.Kind) {
182-
case ChangeKind::BoundTypeVariable:
183-
tvWithboundTypes.insert(std::pair<TypeVariableType *, TypeBase *>(
184-
change.Binding.TypeVar, change.Binding.FixedType));
185-
break;
186-
case ChangeKind::AddedTypeVariable:
187-
addedTypeVars.push_back(change.TypeVar);
188-
break;
189-
case ChangeKind::ExtendedEquivalenceClass:
190-
equivTypeVars.push_back(change.EquivClass.TypeVar);
191-
break;
192-
case ChangeKind::AddedConstraint:
193-
addedConstraints.insert(change.TheConstraint);
194-
break;
195-
case ChangeKind::RemovedConstraint:
196-
removedConstraints.insert(change.TheConstraint);
197-
break;
198-
case ChangeKind::UpdatedTypeVariable:
199-
// Don't consider changes that don't affect the graph.
200-
break;
201-
}
202-
}
231+
unsigned indent) const {
232+
out.indent(indent);
233+
out << "(changes:\n";
203234

204-
// If there are any constraints that were both added and removed in this set
205-
// of Changes, remove them from both.
206-
std::set<Constraint *> intersects;
207-
set_intersection(addedConstraints.begin(), addedConstraints.end(),
208-
removedConstraints.begin(), removedConstraints.end(),
209-
std::inserter(intersects, intersects.begin()));
210-
llvm::set_subtract(addedConstraints, intersects);
211-
llvm::set_subtract(removedConstraints, intersects);
235+
for (unsigned i = fromIndex; i < Changes.size(); ++i)
236+
Changes[i].dump(out, CS, indent + 2);
212237

213-
// Print out Changes.
214-
PrintOptions PO;
215-
PO.PrintTypesForDebugging = true;
216-
out.indent(indent);
217-
out << "(Changes:\n";
218-
if (!tvWithboundTypes.empty()) {
219-
out.indent(indent + 2);
220-
out << "(Newly Bound: \n";
221-
for (const auto &tvWithType : tvWithboundTypes) {
222-
out.indent(indent + 4);
223-
out << "> $T" << tvWithType.first->getImpl().getID() << " := ";
224-
tvWithType.second->print(out, PO);
225-
out << '\n';
226-
}
227-
out.indent(indent + 2);
228-
out << ")\n";
229-
}
230-
if (!addedTypeVars.empty()) {
231-
out.indent(indent + 2);
232-
auto heading = (addedTypeVars.size() > 1) ? "(New Type Variables: \n"
233-
: "(New Type Variable: \n";
234-
out << heading;
235-
for (const auto &typeVar : addedTypeVars) {
236-
out.indent(indent + 4);
237-
out << "> $T" << typeVar->getImpl().getID();
238-
out << '\n';
239-
}
240-
out.indent(indent + 2);
241-
out << ")\n";
242-
}
243-
if (!equivTypeVars.empty()) {
244-
out.indent(indent + 2);
245-
auto heading = (equivTypeVars.size() > 1) ? "(New Equivalences: \n"
246-
: "(New Equivalence: \n";
247-
out << heading;
248-
for (const auto &typeVar : equivTypeVars) {
249-
out.indent(indent + 4);
250-
out << "> $T" << typeVar->getImpl().getID();
251-
out << '\n';
252-
}
253-
out.indent(indent + 2);
254-
out << ")\n";
255-
}
256-
if (!addedConstraints.empty()) {
257-
out.indent(indent + 2);
258-
auto heading = (addedConstraints.size() > 1) ? "(Added Constraints: \n"
259-
: "(Added Constraint: \n";
260-
out << heading;
261-
for (const auto &constraint : addedConstraints) {
262-
out.indent(indent + 4);
263-
out << "> ";
264-
constraint->print(out, &CS.getASTContext().SourceMgr, indent + 6);
265-
out << '\n';
266-
}
267-
out.indent(indent + 2);
268-
out << ")\n";
269-
}
270-
if (!removedConstraints.empty()) {
271-
out.indent(indent + 2);
272-
auto heading = (removedConstraints.size() > 1) ? "(Removed Constraints: \n"
273-
: "(Removed Constraint: \n";
274-
out << heading;
275-
for (const auto &constraint : removedConstraints) {
276-
out.indent(indent + 4);
277-
out << "> ";
278-
constraint->print(out, &CS.getASTContext().SourceMgr, indent + 6);
279-
out << '\n';
280-
}
281-
out.indent(indent + 2);
282-
out << ")\n";
283-
}
284238
out.indent(indent);
285239
out << ")\n";
286240
}

0 commit comments

Comments
 (0)