@@ -52,8 +52,9 @@ namespace constraints {
52
52
class ConstraintGraph ;
53
53
class ConstraintGraphNode ;
54
54
class ConstraintSystem ;
55
- class Disjunction ;
56
- class TypeVarBindingGenerator ;
55
+ class DisjunctionChoiceProducer ;
56
+ class TypeVarBindingProducer ;
57
+ class TypeVariableBinding ;
57
58
58
59
} // end namespace constraints
59
60
@@ -925,7 +926,8 @@ class ConstraintSystem {
925
926
friend class DisjunctionChoice ;
926
927
friend class Component ;
927
928
friend class FailureDiagnostic ;
928
- friend class TypeVarBindingGenerator ;
929
+ friend class TypeVarBindingProducer ;
930
+ friend class TypeVariableBinding ;
929
931
930
932
class SolverScope ;
931
933
@@ -2983,7 +2985,7 @@ class ConstraintSystem {
2983
2985
// / the best solution to the constraint system.
2984
2986
// /
2985
2987
// / \returns true if we failed to find any solutions, false otherwise.
2986
- bool solveForDisjunctionChoices (Disjunction &disjunction,
2988
+ bool solveForDisjunctionChoices (DisjunctionChoiceProducer &disjunction,
2987
2989
SmallVectorImpl<Solution> &solutions);
2988
2990
2989
2991
// / \brief Solve the system of constraints after it has already been
@@ -3366,7 +3368,15 @@ void simplifyLocator(Expr *&anchor,
3366
3368
// / null otherwise.
3367
3369
Expr *simplifyLocatorToAnchor (ConstraintSystem &cs, ConstraintLocator *locator);
3368
3370
3369
- class DisjunctionChoice {
3371
+ // / Common interface to encapsulate attempting choices of
3372
+ // / different entities, such as type variables (types)
3373
+ // / or disjunctions (their choices).
3374
+ struct TypeBinding {
3375
+ virtual ~TypeBinding () {}
3376
+ virtual void attempt (ConstraintSystem &cs) const = 0;
3377
+ };
3378
+
3379
+ class DisjunctionChoice : public TypeBinding {
3370
3380
ConstraintSystem *CS;
3371
3381
unsigned Index;
3372
3382
Constraint *Choice;
@@ -3398,8 +3408,10 @@ class DisjunctionChoice {
3398
3408
bool isGenericOperator () const ;
3399
3409
bool isSymmetricOperator () const ;
3400
3410
3411
+ void attempt (ConstraintSystem &cs) const override ;
3412
+
3401
3413
// / \brief Apply given choice to the system and try to solve it.
3402
- Optional<Score> solve (SmallVectorImpl<Solution> &solutions);
3414
+ Optional<Score> attempt (SmallVectorImpl<Solution> &solutions);
3403
3415
3404
3416
operator Constraint *() { return Choice; }
3405
3417
@@ -3428,7 +3440,25 @@ class DisjunctionChoice {
3428
3440
}
3429
3441
};
3430
3442
3431
- class TypeVarBindingGenerator {
3443
+ class TypeVariableBinding : public TypeBinding {
3444
+ TypeVariableType *TypeVar;
3445
+ ConstraintSystem::PotentialBinding Binding;
3446
+
3447
+ public:
3448
+ TypeVariableBinding (TypeVariableType *typeVar,
3449
+ ConstraintSystem::PotentialBinding &binding)
3450
+ : TypeVar(typeVar), Binding(binding) {}
3451
+
3452
+ Type getType () const { return Binding.BindingType ; }
3453
+
3454
+ void attempt (ConstraintSystem &cs) const override ;
3455
+
3456
+ bool isDefaultableBinding () const { return Binding.isDefaultableBinding (); }
3457
+
3458
+ bool hasDefaultedProtocol () const { return Binding.DefaultedProtocol ; }
3459
+ };
3460
+
3461
+ class TypeVarBindingProducer {
3432
3462
using BindingKind = ConstraintSystem::AllowedBindingKind;
3433
3463
using Binding = ConstraintSystem::PotentialBinding;
3434
3464
@@ -3446,19 +3476,19 @@ class TypeVarBindingGenerator {
3446
3476
llvm::SmallPtrSet<TypeBase *, 4 > BoundTypes;
3447
3477
3448
3478
public:
3449
- TypeVarBindingGenerator (ConstraintSystem &cs, TypeVariableType *typeVar,
3450
- ArrayRef<Binding> initialBindings)
3479
+ TypeVarBindingProducer (ConstraintSystem &cs, TypeVariableType *typeVar,
3480
+ ArrayRef<Binding> initialBindings)
3451
3481
: CS(cs), TypeVar(typeVar),
3452
3482
Bindings (initialBindings.begin(), initialBindings.end()) {}
3453
3483
3454
- Optional<Binding > operator ()() {
3484
+ Optional<TypeVariableBinding > operator ()() {
3455
3485
// Once we reach the end of the current bindings
3456
3486
// let's try to compute new ones, e.g. supertypes,
3457
3487
// literal defaults, if that fails, we are done.
3458
3488
if (needsToComputeNext () && !computeNext ())
3459
3489
return None;
3460
3490
3461
- return Bindings[Index++];
3491
+ return TypeVariableBinding (TypeVar, Bindings[Index++]) ;
3462
3492
}
3463
3493
3464
3494
// / Check whether generator would have to compute next
@@ -3481,7 +3511,7 @@ class TypeVarBindingGenerator {
3481
3511
// / Iterator over disjunction choices, makes it
3482
3512
// / easy to work with disjunction and encapsulates
3483
3513
// / some other important information such as locator.
3484
- class Disjunction {
3514
+ class DisjunctionChoiceProducer {
3485
3515
ConstraintSystem &CS;
3486
3516
ArrayRef<Constraint *> Choices;
3487
3517
ConstraintLocator *Locator;
@@ -3490,20 +3520,20 @@ class Disjunction {
3490
3520
unsigned Index = 0 ;
3491
3521
3492
3522
public:
3493
- Disjunction (ConstraintSystem &cs, ArrayRef<Constraint *> choices,
3494
- ConstraintLocator *locator, bool explicitConversion)
3523
+ DisjunctionChoiceProducer (ConstraintSystem &cs,
3524
+ ArrayRef<Constraint *> choices,
3525
+ ConstraintLocator *locator, bool explicitConversion)
3495
3526
: CS(cs), Choices(choices), Locator(locator),
3496
3527
IsExplicitConversion (explicitConversion) {}
3497
3528
3498
- const Disjunction &begin () const { return *this ; }
3499
- const Disjunction &end () const { return *this ; }
3500
-
3501
- bool operator !=(const Disjunction &) const { return Index < Choices.size (); }
3502
-
3503
- void operator ++() { ++Index; }
3529
+ Optional<DisjunctionChoice> operator ()() {
3530
+ unsigned currIndex = Index;
3531
+ if (currIndex >= Choices.size ())
3532
+ return None;
3504
3533
3505
- DisjunctionChoice operator *() const {
3506
- return {&CS, Index, Choices[Index], IsExplicitConversion};
3534
+ ++Index;
3535
+ return DisjunctionChoice (&CS, currIndex, Choices[currIndex],
3536
+ IsExplicitConversion);
3507
3537
}
3508
3538
3509
3539
ConstraintLocator *getLocator () const { return Locator; }
0 commit comments