Skip to content

Commit 66a738f

Browse files
author
Amritpan Kaur
committed
[ConstraintGraph] Collect and print changes in current Active Scope.
1 parent 52407c1 commit 66a738f

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

include/swift/Sema/ConstraintGraph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ class ConstraintGraph {
387387
/// Print the graph.
388388
void print(ArrayRef<TypeVariableType *> typeVars, llvm::raw_ostream &out);
389389
void dump(llvm::raw_ostream &out);
390+
void print(llvm::raw_ostream &out, unsigned indent = 0);
390391

391392
// FIXME: Potentially side-effectful.
392393
SWIFT_DEBUG_HELPER(void dump());

lib/Sema/CSStep.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,13 @@ template <typename P> class BindingStep : public SolverStep {
548548
auto scope = std::make_unique<Scope>(CS);
549549
if (attempt(*choice)) {
550550
ActiveChoice.emplace(std::move(scope), *choice);
551+
552+
if (CS.isDebugMode()) {
553+
auto &log = llvm::errs();
554+
auto &CG = CS.getConstraintGraph();
555+
CG.print(log, CS.solverState->getCurrentIndent());
556+
}
557+
551558
return suspend(std::make_unique<SplitterStep>(CS, Solutions));
552559
}
553560
}

lib/Sema/ConstraintGraph.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,119 @@ void ConstraintGraph::dump(llvm::raw_ostream &out) {
15581558
print(CS.getTypeVariables(), out);
15591559
}
15601560

1561+
void ConstraintGraph::print(llvm::raw_ostream &out, unsigned indent) {
1562+
if (Changes.empty() && (ActiveScope->getStartIdx() == Changes.size()))
1563+
return;
1564+
1565+
// Collect Changes for printing.
1566+
std::map<TypeVariableType *, TypeBase *> tvWithboundTypes;
1567+
std::vector<TypeVariableType *> addedTypeVars;
1568+
std::vector<TypeVariableType *> equivTypeVars;
1569+
std::vector<Constraint *> addedConstraints;
1570+
std::vector<Constraint *> removedConstraints;
1571+
for (unsigned int i = ActiveScope->getStartIdx(); i < Changes.size(); i++) {
1572+
auto change = Changes[i];
1573+
switch (change.Kind) {
1574+
case ChangeKind::BoundTypeVariable:
1575+
tvWithboundTypes.insert(std::pair<TypeVariableType *, TypeBase *>(
1576+
change.Binding.TypeVar, change.Binding.FixedType));
1577+
break;
1578+
case ChangeKind::AddedTypeVariable:
1579+
addedTypeVars.push_back(change.TypeVar);
1580+
break;
1581+
case ChangeKind::ExtendedEquivalenceClass:
1582+
equivTypeVars.push_back(change.EquivClass.TypeVar);
1583+
break;
1584+
case ChangeKind::AddedConstraint:
1585+
addedConstraints.push_back(change.TheConstraint);
1586+
break;
1587+
case ChangeKind::RemovedConstraint:
1588+
removedConstraints.push_back(change.TheConstraint);
1589+
break;
1590+
}
1591+
}
1592+
1593+
// If there are any constraints that were both added and removed in this set
1594+
// of Changes, remove them from both.
1595+
for (std::vector<Constraint *>::const_iterator itr = addedConstraints.begin();
1596+
itr != addedConstraints.end();) {
1597+
auto repeatedConstraintItr =
1598+
std::find(removedConstraints.begin(), removedConstraints.end(), *itr);
1599+
if (repeatedConstraintItr != removedConstraints.end()) {
1600+
removedConstraints.erase(repeatedConstraintItr);
1601+
itr = addedConstraints.erase(itr);
1602+
} else {
1603+
itr++;
1604+
}
1605+
}
1606+
1607+
// Print out Changes.
1608+
PrintOptions PO;
1609+
PO.PrintTypesForDebugging = true;
1610+
out.indent(indent);
1611+
out << "(Changes:\n";
1612+
if (!tvWithboundTypes.empty()) {
1613+
out.indent(indent + 2);
1614+
out << "(New Bound Type: \n";
1615+
for (const auto &tvWithType : tvWithboundTypes) {
1616+
out.indent(indent + 4);
1617+
out << "> $T" << tvWithType.first->getImpl().getID() << " := ";
1618+
tvWithType.second->print(out, PO);
1619+
out << '\n';
1620+
}
1621+
out.indent(indent + 2);
1622+
out << ")\n";
1623+
}
1624+
if (!addedTypeVars.empty()) {
1625+
out.indent(indent + 2);
1626+
out << "(New Type Variable: \n";
1627+
for (const auto &typeVar : addedTypeVars) {
1628+
out.indent(indent + 4);
1629+
out << "> $T" << typeVar->getImpl().getID();
1630+
out << '\n';
1631+
}
1632+
out.indent(indent + 2);
1633+
out << ")\n";
1634+
}
1635+
if (!equivTypeVars.empty()) {
1636+
out.indent(indent + 2);
1637+
out << "(New Equivalence Class: \n";
1638+
for (const auto &typeVar : equivTypeVars) {
1639+
out.indent(indent + 4);
1640+
out << "> $T" << typeVar->getImpl().getID();
1641+
out << '\n';
1642+
}
1643+
out.indent(indent + 2);
1644+
out << ")\n";
1645+
}
1646+
if (!addedConstraints.empty()) {
1647+
out.indent(indent + 2);
1648+
out << "(Added Constraint: \n";
1649+
for (const auto &constraint : addedConstraints) {
1650+
out.indent(indent + 4);
1651+
out << "> ";
1652+
constraint->print(out, &CS.getASTContext().SourceMgr);
1653+
out << '\n';
1654+
}
1655+
out.indent(indent + 2);
1656+
out << ")\n";
1657+
}
1658+
if (!removedConstraints.empty()) {
1659+
out.indent(indent + 2);
1660+
out << "(Removed Constraint: \n";
1661+
for (const auto &constraint : removedConstraints) {
1662+
out.indent(indent + 4);
1663+
out << "> ";
1664+
constraint->print(out, &CS.getASTContext().SourceMgr);
1665+
out << '\n';
1666+
}
1667+
out.indent(indent + 2);
1668+
out << ")\n";
1669+
}
1670+
out.indent(indent);
1671+
out << ")\n";
1672+
}
1673+
15611674
void ConstraintGraph::printConnectedComponents(
15621675
ArrayRef<TypeVariableType *> typeVars,
15631676
llvm::raw_ostream &out) {

0 commit comments

Comments
 (0)