@@ -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
@@ -372,17 +360,10 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
372
360
void ConstraintGraph::removeNode (TypeVariableType *typeVar) {
373
361
// Remove this node.
374
362
auto &impl = typeVar->getImpl ();
375
- unsigned index = impl.getGraphIndex ();
376
363
auto *node = impl.getGraphNode ();
377
364
node->reset ();
378
365
FreeList.push_back (node);
379
366
impl.setGraphNode (nullptr );
380
-
381
- // Remove this type variable from the list.
382
- unsigned lastIndex = TypeVariables.size ()-1 ;
383
- if (index < lastIndex)
384
- TypeVariables[index ] = TypeVariables[lastIndex];
385
- TypeVariables.pop_back ();
386
367
}
387
368
388
369
void ConstraintGraph::addConstraint (Constraint *constraint) {
@@ -1648,7 +1629,7 @@ void ConstraintGraph::verify() {
1648
1629
// Verify that the type variables are either representatives or represented
1649
1630
// within their representative's equivalence class.
1650
1631
// FIXME: Also check to make sure the equivalence classes aren't too large?
1651
- for (auto typeVar : TypeVariables) {
1632
+ for (auto typeVar : CS. TypeVariables ) {
1652
1633
auto typeVarRep = CS.getRepresentative (typeVar);
1653
1634
auto &repNode = (*this )[typeVarRep];
1654
1635
if (typeVar != typeVarRep) {
@@ -1669,24 +1650,15 @@ void ConstraintGraph::verify() {
1669
1650
}
1670
1651
}
1671
1652
1672
- // Verify that our type variable map/vector are in sync.
1673
- for (unsigned i = 0 , n = TypeVariables.size (); i != n; ++i) {
1674
- auto typeVar = TypeVariables[i];
1675
- auto &impl = typeVar->getImpl ();
1676
- requireSameValue (impl.getGraphIndex (), i, " wrong graph node index" );
1677
- require (impl.getGraphNode (), " null graph node" );
1678
- }
1679
-
1680
1653
// Verify consistency of all of the nodes in the graph.
1681
- for (unsigned i = 0 , n = TypeVariables.size (); i != n; ++i) {
1682
- auto typeVar = TypeVariables[i];
1654
+ for (auto typeVar : CS.TypeVariables ) {
1683
1655
auto &impl = typeVar->getImpl ();
1684
1656
impl.getGraphNode ()->verify (*this );
1685
1657
}
1686
1658
1687
1659
// Collect all of the constraints known to the constraint graph.
1688
1660
llvm::SmallPtrSet<Constraint *, 4 > knownConstraints;
1689
- for (auto typeVar : getTypeVariables () ) {
1661
+ for (auto typeVar : CS. TypeVariables ) {
1690
1662
for (auto constraint : (*this )[typeVar].getConstraints ())
1691
1663
knownConstraints.insert (constraint);
1692
1664
}
0 commit comments