Skip to content

Commit 01ea11b

Browse files
author
Amritpan Kaur
committed
[ConstraintGraph] Collect and print changes in current Active Scope.
1 parent bd2968e commit 01ea11b

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

include/swift/Sema/ConstraintGraph.h

+1
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 dumpActiveScopeChanges(llvm::raw_ostream &out, unsigned indent = 0);
390391

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

lib/Sema/CSStep.h

+7
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ template <typename P> class BindingStep : public SolverStep {
545545
auto scope = std::make_unique<Scope>(CS);
546546
if (attempt(*choice)) {
547547
ActiveChoice.emplace(std::move(scope), *choice);
548+
549+
if (CS.isDebugMode()) {
550+
auto &log = llvm::errs();
551+
auto &CG = CS.getConstraintGraph();
552+
CG.dumpActiveScopeChanges(log, CS.solverState->getCurrentIndent());
553+
}
554+
548555
return suspend(std::make_unique<SplitterStep>(CS, Solutions));
549556
}
550557
}

lib/Sema/ConstraintGraph.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,123 @@ void ConstraintGraph::dump(llvm::raw_ostream &out) {
15581558
print(CS.getTypeVariables(), out);
15591559
}
15601560

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

0 commit comments

Comments
 (0)