21
21
using namespace swift ;
22
22
using namespace constraints ;
23
23
24
- void ConstraintSystem::inferTransitiveSupertypeBindings (
25
- const llvm::SmallDenseMap<TypeVariableType *, PotentialBindings>
26
- &inferredBindings,
27
- PotentialBindings &bindings) {
28
- auto *typeVar = bindings.TypeVar ;
24
+ void ConstraintSystem::PotentialBindings::inferTransitiveBindings (
25
+ ConstraintSystem &cs,
26
+ const llvm::SmallDenseMap<TypeVariableType *,
27
+ ConstraintSystem::PotentialBindings>
28
+ &inferredBindings) {
29
+ using BindingKind = ConstraintSystem::AllowedBindingKind;
29
30
30
31
llvm::SmallVector<Constraint *, 4 > subtypeOf;
31
32
// First, let's collect all of the `subtype` constraints associated
32
33
// with this type variable.
33
- llvm::copy_if (bindings. Sources , std::back_inserter (subtypeOf),
34
+ llvm::copy_if (Sources, std::back_inserter (subtypeOf),
34
35
[&](const Constraint *constraint) -> bool {
35
36
if (constraint->getKind () != ConstraintKind::Subtype)
36
37
return false ;
37
38
38
- auto rhs = simplifyType (constraint->getSecondType ());
39
- return rhs->getAs <TypeVariableType>() == typeVar ;
39
+ auto rhs = cs. simplifyType (constraint->getSecondType ());
40
+ return rhs->getAs <TypeVariableType>() == TypeVar ;
40
41
});
41
42
42
43
if (subtypeOf.empty ())
43
44
return ;
44
45
45
46
// We need to make sure that there are no duplicate bindings in the
46
- // set, other we'll produce multiple identical solutions.
47
+ // set, otherwise solver would produce multiple identical solutions.
47
48
llvm::SmallPtrSet<CanType, 4 > existingTypes;
48
- for (const auto &binding : bindings. Bindings )
49
+ for (const auto &binding : Bindings)
49
50
existingTypes.insert (binding.BindingType ->getCanonicalType ());
50
51
51
52
for (auto *constraint : subtypeOf) {
52
53
auto *tv =
53
- simplifyType (constraint->getFirstType ())->getAs <TypeVariableType>();
54
+ cs. simplifyType (constraint->getFirstType ())->getAs <TypeVariableType>();
54
55
if (!tv)
55
56
continue ;
56
57
@@ -63,8 +64,8 @@ void ConstraintSystem::inferTransitiveSupertypeBindings(
63
64
// either be Exact or Supertypes in order for it to make sense
64
65
// to add Supertype bindings based on the relationship between
65
66
// our type variables.
66
- if (binding.Kind != AllowedBindingKind ::Exact &&
67
- binding.Kind != AllowedBindingKind ::Supertypes)
67
+ if (binding.Kind != BindingKind ::Exact &&
68
+ binding.Kind != BindingKind ::Supertypes)
68
69
continue ;
69
70
70
71
auto type = binding.BindingType ;
@@ -75,16 +76,16 @@ void ConstraintSystem::inferTransitiveSupertypeBindings(
75
76
if (!existingTypes.insert (type->getCanonicalType ()).second )
76
77
continue ;
77
78
78
- if (ConstraintSystem::typeVarOccursInType (typeVar , type))
79
+ if (ConstraintSystem::typeVarOccursInType (TypeVar , type))
79
80
continue ;
80
81
81
- bindings. addPotentialBinding (
82
- binding.withSameSource (type, AllowedBindingKind ::Supertypes));
82
+ addPotentialBinding (
83
+ binding.withSameSource (type, BindingKind ::Supertypes));
83
84
}
84
85
85
86
// Infer transitive protocol requirements.
86
87
for (auto *protocol : relatedBindings->getSecond ().Protocols ) {
87
- bindings. Protocols .push_back (protocol);
88
+ Protocols.push_back (protocol);
88
89
}
89
90
}
90
91
}
@@ -113,7 +114,7 @@ ConstraintSystem::determineBestBindings() {
113
114
114
115
auto &bindings = cachedBindings->getSecond ();
115
116
116
- inferTransitiveSupertypeBindings (cache, bindings );
117
+ bindings. inferTransitiveBindings (* this , cache );
117
118
118
119
if (isDebugMode ()) {
119
120
bindings.dump (typeVar, llvm::errs (), solverState->depth * 2 );
0 commit comments