@@ -335,6 +335,46 @@ void ConstraintGraphNode::reintroduceToInference(Constraint *constraint,
335
335
introduceToInference (constraint, notifyReferencedVars);
336
336
}
337
337
338
+ void ConstraintGraphNode::introduceToInference (
339
+ Type fixedType, SmallPtrSetImpl<TypeVariableType *> &referencedVars) {
340
+ // Notify all of the type variables that reference this one.
341
+ //
342
+ // Since this type variable has been replaced with a fixed type
343
+ // all of the concrete types that reference it are going to change,
344
+ // which means that all of the not-yet-attempted bindings should
345
+ // change as well.
346
+ notifyReferencingVars ();
347
+
348
+ for (auto *referencedVar : referencedVars) {
349
+ auto &node = CG[referencedVar];
350
+
351
+ // Newly referred vars need to re-introduce all constraints associated
352
+ // with this type variable since they are now going to be used in
353
+ // all of the constraints that reference bound type variable.
354
+ for (auto *constraint : getConstraints ()) {
355
+ if (isUsefulForReferencedVars (constraint))
356
+ node.reintroduceToInference (constraint,
357
+ /* notifyReferencedVars=*/ false );
358
+ }
359
+ }
360
+ }
361
+
362
+ void ConstraintGraphNode::retractFromInference (
363
+ Type fixedType, SmallPtrSetImpl<TypeVariableType *> &referencedVars) {
364
+ // Notify referencing variables (just like in bound case) that this
365
+ // type variable has been modified.
366
+ notifyReferencingVars ();
367
+
368
+ // TODO: This might be an overkill but it's (currently)
369
+ // the simpliest way to reliably ensure that all of the
370
+ // no longer related constraints have been retracted.
371
+ for (auto *referencedVar : referencedVars) {
372
+ auto &node = CG[referencedVar];
373
+ if (node.forRepresentativeVar ())
374
+ node.resetBindingSet ();
375
+ }
376
+ }
377
+
338
378
void ConstraintGraphNode::resetBindingSet () {
339
379
assert (forRepresentativeVar ());
340
380
@@ -541,65 +581,36 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
541
581
542
582
auto &node = (*this )[typeVar];
543
583
544
- // Notify all of the type variables that reference this one.
545
- //
546
- // Since this type variable has been replaced with a fixed type
547
- // all of the concrete types that reference it are going to change,
548
- // which means that all of the not-yet-attempted bindings should
549
- // change as well.
550
- node.notifyReferencingVars ();
551
-
552
- if (!fixed->hasTypeVariable ())
553
- return ;
554
-
555
- llvm::SmallPtrSet<TypeVariableType *, 4 > typeVars;
556
- fixed->getTypeVariables (typeVars);
584
+ llvm::SmallPtrSet<TypeVariableType *, 4 > referencedVars;
585
+ fixed->getTypeVariables (referencedVars);
557
586
558
- for (auto otherTypeVar : typeVars ) {
587
+ for (auto otherTypeVar : referencedVars ) {
559
588
if (typeVar == otherTypeVar)
560
589
continue ;
561
590
562
591
auto &otherNode = (*this )[otherTypeVar];
563
592
564
593
otherNode.addReferencedBy (typeVar);
565
594
node.addReferencedVar (otherTypeVar);
566
-
567
- // Newly referred vars need to re-introduce all constraints associated
568
- // with this type variable since they are now going to be used in
569
- // all of the constraints that reference bound type variable.
570
- for (auto *constraint : (*this )[typeVar].getConstraints ()) {
571
- if (isUsefulForReferencedVars (constraint))
572
- otherNode.reintroduceToInference (constraint,
573
- /* notifyReferencedVars=*/ false );
574
- }
575
595
}
596
+
597
+ node.introduceToInference (fixed, referencedVars);
576
598
}
577
599
578
600
void ConstraintGraph::unbindTypeVariable (TypeVariableType *typeVar, Type fixed) {
579
601
auto &node = (*this )[typeVar];
580
602
581
- // Notify referencing variables (just like in bound case) that this
582
- // type variable has been modified.
583
- node.notifyReferencingVars ();
603
+ llvm::SmallPtrSet<TypeVariableType *, 4 > referencedVars;
604
+ fixed->getTypeVariables (referencedVars);
584
605
585
- if (!fixed->hasTypeVariable ())
586
- return ;
587
-
588
- llvm::SmallPtrSet<TypeVariableType *, 4 > typeVars;
589
- fixed->getTypeVariables (typeVars);
590
-
591
- for (auto otherTypeVar : typeVars) {
606
+ for (auto otherTypeVar : referencedVars) {
592
607
auto &otherNode = (*this )[otherTypeVar];
593
608
594
609
otherNode.removeReferencedBy (typeVar);
595
610
node.removeReference (otherTypeVar);
596
-
597
- // TODO: This might be an overkill but it's (currently)
598
- // the simpliest way to reliably ensure that all of the
599
- // no longer related constraints have been retracted.
600
- if (otherNode.forRepresentativeVar ())
601
- otherNode.resetBindingSet ();
602
611
}
612
+
613
+ node.retractFromInference (fixed, referencedVars);
603
614
}
604
615
605
616
#pragma mark Algorithms
0 commit comments