@@ -38,11 +38,13 @@ using namespace constraints;
3838ConstraintGraph::ConstraintGraph (ConstraintSystem &cs) : CS(cs) { }
3939
4040ConstraintGraph::~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 );
4545 }
46+ #endif
47+
4648 for (auto *node : FreeList) {
4749 delete node;
4850 }
@@ -70,30 +72,16 @@ void ConstraintGraph::addTypeVariable(TypeVariableType *typeVar) {
7072 FreeList.pop_back ();
7173 nodePtr->initTypeVariable (typeVar);
7274 }
73- unsigned index = TypeVariables.size ();
7475 impl.setGraphNode (nodePtr);
75- impl.setGraphIndex (index);
76-
77- // Record this type variable.
78- TypeVariables.push_back (typeVar);
7976
8077 if (CS.solverState )
8178 CS.recordChange (SolverTrail::Change::AddedTypeVariable (typeVar));
8279}
8380
8481ConstraintGraphNode &
8582ConstraintGraph::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 ();
9384 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" );
9785 return *nodePtr;
9886}
9987
@@ -372,17 +360,10 @@ void ConstraintGraphNode::introduceToInference(Type fixedType) {
372360void ConstraintGraph::removeNode (TypeVariableType *typeVar) {
373361 // Remove this node.
374362 auto &impl = typeVar->getImpl ();
375- unsigned index = impl.getGraphIndex ();
376363 auto *node = impl.getGraphNode ();
377364 node->reset ();
378365 FreeList.push_back (node);
379366 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 ();
386367}
387368
388369void ConstraintGraph::addConstraint (Constraint *constraint) {
@@ -1648,7 +1629,7 @@ void ConstraintGraph::verify() {
16481629 // Verify that the type variables are either representatives or represented
16491630 // within their representative's equivalence class.
16501631 // FIXME: Also check to make sure the equivalence classes aren't too large?
1651- for (auto typeVar : TypeVariables) {
1632+ for (auto typeVar : CS. TypeVariables ) {
16521633 auto typeVarRep = CS.getRepresentative (typeVar);
16531634 auto &repNode = (*this )[typeVarRep];
16541635 if (typeVar != typeVarRep) {
@@ -1669,24 +1650,15 @@ void ConstraintGraph::verify() {
16691650 }
16701651 }
16711652
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-
16801653 // 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 ) {
16831655 auto &impl = typeVar->getImpl ();
16841656 impl.getGraphNode ()->verify (*this );
16851657 }
16861658
16871659 // Collect all of the constraints known to the constraint graph.
16881660 llvm::SmallPtrSet<Constraint *, 4 > knownConstraints;
1689- for (auto typeVar : getTypeVariables () ) {
1661+ for (auto typeVar : CS. TypeVariables ) {
16901662 for (auto constraint : (*this )[typeVar].getConstraints ())
16911663 knownConstraints.insert (constraint);
16921664 }
0 commit comments