39
39
#include " swift/Sema/SolutionResult.h"
40
40
#include " swift/Sema/SyntacticElementTarget.h"
41
41
#include " llvm/ADT/MapVector.h"
42
+ #include " llvm/ADT/PointerIntPair.h"
42
43
#include " llvm/ADT/PointerUnion.h"
43
44
#include " llvm/ADT/STLExtras.h"
44
45
#include " llvm/ADT/SetOperations.h"
@@ -339,21 +340,17 @@ class TypeVariableType::Implementation {
339
340
// / The corresponding node in the constraint graph.
340
341
constraints::ConstraintGraphNode *GraphNode = nullptr ;
341
342
342
- // / Index into the list of type variables, as used by the
343
- // / constraint graph .
344
- unsigned GraphIndex ;
343
+ // / Temporary state for ConstraintGraph::computeConnectedComponents(),
344
+ // / stored inline for performance .
345
+ llvm::PointerIntPair<TypeVariableType *, 1 , unsigned > Component ;
345
346
346
347
friend class constraints ::SolverTrail;
347
348
348
349
public:
349
350
// / Retrieve the type variable associated with this implementation.
350
- TypeVariableType *getTypeVariable () {
351
- return reinterpret_cast <TypeVariableType *>(this ) - 1 ;
352
- }
353
-
354
- // / Retrieve the type variable associated with this implementation.
355
- const TypeVariableType *getTypeVariable () const {
356
- return reinterpret_cast <const TypeVariableType *>(this ) - 1 ;
351
+ TypeVariableType *getTypeVariable () const {
352
+ return reinterpret_cast <TypeVariableType *>(
353
+ const_cast <Implementation *>(this )) - 1 ;
357
354
}
358
355
359
356
explicit Implementation (constraints::ConstraintLocator *locator,
@@ -406,17 +403,6 @@ class TypeVariableType::Implementation {
406
403
void setGraphNode (constraints::ConstraintGraphNode *newNode) {
407
404
GraphNode = newNode;
408
405
}
409
-
410
- // / Retrieve the index into the constraint graph's list of type variables.
411
- unsigned getGraphIndex () const {
412
- assert (GraphNode && " Graph node isn't set" );
413
- return GraphIndex;
414
- }
415
-
416
- // / Set the index into the constraint graph's list of type variables.
417
- void setGraphIndex (unsigned newIndex) {
418
- GraphIndex = newIndex;
419
- }
420
406
421
407
// / Check whether this type variable either has a representative that
422
408
// / is not itself or has a fixed type binding.
@@ -430,6 +416,12 @@ class TypeVariableType::Implementation {
430
416
return ParentOrFixed.get <TypeVariableType *>() != getTypeVariable ();
431
417
}
432
418
419
+ // / Low-level accessor; use getRepresentative() or getFixedType() instead.
420
+ llvm::PointerUnion<TypeVariableType *, TypeBase *>
421
+ getRepresentativeOrFixed () const {
422
+ return ParentOrFixed;
423
+ }
424
+
433
425
// / Record the current type-variable binding.
434
426
void recordBinding (constraints::SolverTrail &trail) {
435
427
trail.recordChange (constraints::SolverTrail::Change::UpdatedTypeVariable (
@@ -647,6 +639,37 @@ class TypeVariableType::Implementation {
647
639
impl.getTypeVariable ()->Bits .TypeVariableType .Options |= TVO_CanBindToHole;
648
640
}
649
641
642
+ void setComponent (TypeVariableType *parent) {
643
+ Component.setPointerAndInt (parent, /* valid=*/ false );
644
+ }
645
+
646
+ TypeVariableType *getComponent () const {
647
+ auto *rep = getTypeVariable ();
648
+ while (rep != rep->getImpl ().Component .getPointer ())
649
+ rep = rep->getImpl ().Component .getPointer ();
650
+
651
+ // Path compression
652
+ if (rep != getTypeVariable ()) {
653
+ const_cast <TypeVariableType::Implementation *>(this )
654
+ ->Component .setPointer (rep);
655
+ }
656
+
657
+ return rep;
658
+ }
659
+
660
+ bool isValidComponent () const {
661
+ ASSERT (Component.getPointer () == getTypeVariable ());
662
+ return Component.getInt ();
663
+ }
664
+
665
+ bool markValidComponent () {
666
+ if (Component.getInt ())
667
+ return false ;
668
+ ASSERT (Component.getPointer () == getTypeVariable ());
669
+ Component.setInt (1 );
670
+ return true ;
671
+ }
672
+
650
673
// / Print the type variable to the given output stream.
651
674
void print (llvm::raw_ostream &OS);
652
675
@@ -2339,7 +2362,7 @@ class ConstraintSystem {
2339
2362
ConstraintList InactiveConstraints;
2340
2363
2341
2364
// / The constraint graph.
2342
- ConstraintGraph & CG;
2365
+ ConstraintGraph CG;
2343
2366
2344
2367
// / A mapping from constraint locators to the set of opened types associated
2345
2368
// / with that locator.
@@ -2707,7 +2730,7 @@ class ConstraintSystem {
2707
2730
~ConstraintSystem ();
2708
2731
2709
2732
// / Retrieve the constraint graph associated with this constraint system.
2710
- ConstraintGraph &getConstraintGraph () const { return CG; }
2733
+ ConstraintGraph &getConstraintGraph () { return CG; }
2711
2734
2712
2735
// / Retrieve the AST context.
2713
2736
ASTContext &getASTContext () const { return Context; }
0 commit comments