@@ -38,11 +38,13 @@ using namespace constraints;
38
38
ConstraintGraph::ConstraintGraph (ConstraintSystem &cs) : CS(cs) { }
39
39
40
40
ConstraintGraph::~ConstraintGraph () {
41
- for ( unsigned i = 0 , n = TypeVariables. size (); i != n; ++i) {
42
- auto &impl = TypeVariables[i]-> getImpl ();
43
- delete impl. getGraphNode ();
44
- impl.setGraphNode ( nullptr );
41
+ # ifndef NDEBUG
42
+ for ( unsigned i = 0 , n = CS. TypeVariables . size (); i != n; ++i) {
43
+ auto & impl = CS. TypeVariables [i]-> getImpl ();
44
+ ASSERT ( impl.getGraphNode () == nullptr );
45
45
}
46
+ #endif
47
+
46
48
for (auto *node : FreeList) {
47
49
delete node;
48
50
}
@@ -70,30 +72,16 @@ void ConstraintGraph::addTypeVariable(TypeVariableType *typeVar) {
70
72
FreeList.pop_back ();
71
73
nodePtr->initTypeVariable (typeVar);
72
74
}
73
- unsigned index = TypeVariables.size ();
74
75
impl.setGraphNode (nodePtr);
75
- impl.setGraphIndex (index);
76
-
77
- // Record this type variable.
78
- TypeVariables.push_back (typeVar);
79
76
80
77
if (CS.solverState )
81
78
CS.recordChange (SolverTrail::Change::AddedTypeVariable (typeVar));
82
79
}
83
80
84
81
ConstraintGraphNode &
85
82
ConstraintGraph::operator [](TypeVariableType *typeVar) {
86
- // Check whether we've already created a node for this type variable.
87
- auto &impl = typeVar->getImpl ();
88
- auto *nodePtr = impl.getGraphNode ();
89
- if (!nodePtr) {
90
- llvm::errs () << " Type variable $T" << impl.getID () << " not in constraint graph\n " ;
91
- abort ();
92
- }
83
+ auto *nodePtr = typeVar->getImpl ().getGraphNode ();
93
84
ASSERT (nodePtr->TypeVar == typeVar && " Use-after-free" );
94
- DEBUG_ASSERT (impl.getGraphIndex () < TypeVariables.size () && " Out-of-bounds index" );
95
- DEBUG_ASSERT (TypeVariables[impl.getGraphIndex ()] == typeVar &&
96
- " Type variable mismatch" );
97
85
return *nodePtr;
98
86
}
99
87
@@ -357,17 +345,10 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
357
345
void ConstraintGraph::removeNode (TypeVariableType *typeVar) {
358
346
// Remove this node.
359
347
auto &impl = typeVar->getImpl ();
360
- unsigned index = impl.getGraphIndex ();
361
348
auto *node = impl.getGraphNode ();
362
349
node->reset ();
363
350
FreeList.push_back (node);
364
351
impl.setGraphNode (nullptr );
365
-
366
- // Remove this type variable from the list.
367
- unsigned lastIndex = TypeVariables.size ()-1 ;
368
- if (index < lastIndex)
369
- TypeVariables[index] = TypeVariables[lastIndex];
370
- TypeVariables.pop_back ();
371
352
}
372
353
373
354
void ConstraintGraph::addConstraint (Constraint *constraint) {
@@ -1633,7 +1614,7 @@ void ConstraintGraph::verify() {
1633
1614
// Verify that the type variables are either representatives or represented
1634
1615
// within their representative's equivalence class.
1635
1616
// FIXME: Also check to make sure the equivalence classes aren't too large?
1636
- for (auto typeVar : TypeVariables) {
1617
+ for (auto typeVar : CS. TypeVariables ) {
1637
1618
auto typeVarRep = CS.getRepresentative (typeVar);
1638
1619
auto &repNode = (*this )[typeVarRep];
1639
1620
if (typeVar != typeVarRep) {
@@ -1654,24 +1635,15 @@ void ConstraintGraph::verify() {
1654
1635
}
1655
1636
}
1656
1637
1657
- // Verify that our type variable map/vector are in sync.
1658
- for (unsigned i = 0 , n = TypeVariables.size (); i != n; ++i) {
1659
- auto typeVar = TypeVariables[i];
1660
- auto &impl = typeVar->getImpl ();
1661
- requireSameValue (impl.getGraphIndex (), i, " wrong graph node index" );
1662
- require (impl.getGraphNode (), " null graph node" );
1663
- }
1664
-
1665
1638
// Verify consistency of all of the nodes in the graph.
1666
- for (unsigned i = 0 , n = TypeVariables.size (); i != n; ++i) {
1667
- auto typeVar = TypeVariables[i];
1639
+ for (auto typeVar : CS.TypeVariables ) {
1668
1640
auto &impl = typeVar->getImpl ();
1669
1641
impl.getGraphNode ()->verify (*this );
1670
1642
}
1671
1643
1672
1644
// Collect all of the constraints known to the constraint graph.
1673
1645
llvm::SmallPtrSet<Constraint *, 4 > knownConstraints;
1674
- for (auto typeVar : getTypeVariables () ) {
1646
+ for (auto typeVar : CS. TypeVariables ) {
1675
1647
for (auto constraint : (*this )[typeVar].getConstraints ())
1676
1648
knownConstraints.insert (constraint);
1677
1649
}
0 commit comments