Skip to content

Commit 82d07ad

Browse files
committed
[ConstraintSystem] NFC: TypeBinding abstraction is no longer necessary
1 parent de34d9f commit 82d07ad

File tree

2 files changed

+28
-114
lines changed

2 files changed

+28
-114
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,17 +1406,17 @@ bool DisjunctionChoice::attempt(ConstraintSystem &cs) const {
14061406
return !cs.failedConstraint && !cs.simplify();
14071407
}
14081408

1409-
bool DisjunctionChoice::isGenericOp(Constraint *choice) {
1410-
auto *decl = getOperatorDecl(choice);
1409+
bool DisjunctionChoice::isGenericOperator() const {
1410+
auto *decl = getOperatorDecl(Choice);
14111411
if (!decl)
14121412
return false;
14131413

14141414
auto interfaceType = decl->getInterfaceType();
14151415
return interfaceType->is<GenericFunctionType>();
14161416
}
14171417

1418-
bool DisjunctionChoice::isSymmetricOp(Constraint *choice) {
1419-
auto *decl = getOperatorDecl(choice);
1418+
bool DisjunctionChoice::isSymmetricOperator() const {
1419+
auto *decl = getOperatorDecl(Choice);
14201420
if (!decl)
14211421
return false;
14221422

lib/Sema/ConstraintSystem.h

Lines changed: 24 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -3345,89 +3345,34 @@ void simplifyLocator(Expr *&anchor,
33453345
/// null otherwise.
33463346
Expr *simplifyLocatorToAnchor(ConstraintSystem &cs, ConstraintLocator *locator);
33473347

3348-
enum class TypeBindingFlag {
3349-
/// The constraint represented by this choice has been
3350-
/// marked as disabled by the solver.
3351-
Disabled = 0x01,
3352-
/// The declaration behind this binding is marked as `@unavailable`
3353-
Unavailable = 0x02,
3354-
/// This binding comes from the "defaults to" constraint.
3355-
Defaultable = 0x04,
3356-
/// This binding comes from the "defaults to" constraint
3357-
/// which has associated protocol.
3358-
HasDefaultedProtocol = 0x08,
3359-
/// Type of the binding comes from generic operator declaration.
3360-
GenericOperator = 0x10,
3361-
/// Type of the binding comes from symmetric operator declaration.
3362-
SymmetricOperator = 0x20,
3363-
};
3364-
3365-
using TypeBindingFlags = OptionSet<TypeBindingFlag>;
3366-
3367-
/// Common interface to encapsulate attempting choices of
3368-
/// different entities, such as type variables (types)
3369-
/// or disjunctions (their choices).
3370-
class TypeBinding {
3348+
class DisjunctionChoice {
33713349
unsigned Index;
3372-
TypeBindingFlags Flags;
3350+
Constraint *Choice;
3351+
bool ExplicitConversion;
33733352

33743353
public:
3375-
TypeBinding(unsigned index, TypeBindingFlags flags)
3376-
: Index(index), Flags(flags) {}
3377-
3378-
virtual ~TypeBinding() {}
3379-
3380-
/// Attempt given binding in the constraint system, this is going to
3381-
/// introduce some new constraints (potentially "bind" constraints)
3382-
/// to try and associate some type variables with new types.
3383-
///
3384-
/// \returns true if attempt has been accepted by the constraint system,
3385-
/// which means that new constraints have been successfully solved.
3386-
virtual bool attempt(ConstraintSystem &cs) const = 0;
3354+
DisjunctionChoice(unsigned index, Constraint *choice, bool explicitConversion)
3355+
: Index(index), Choice(choice), ExplicitConversion(explicitConversion) {}
33873356

3388-
/// Position of this binding in the chain.
33893357
unsigned getIndex() const { return Index; }
33903358

3391-
bool isDisabled() const { return Flags.contains(TypeBindingFlag::Disabled); }
3392-
3393-
bool isUnavailable() const {
3394-
return Flags.contains(TypeBindingFlag::Unavailable);
3395-
}
3359+
bool attempt(ConstraintSystem &cs) const;
33963360

3397-
bool isDefaultable() const {
3398-
return Flags.contains(TypeBindingFlag::Defaultable);
3399-
}
3361+
bool isDisabled() const { return Choice->isDisabled(); }
34003362

3401-
bool hasDefaultedProtocol() const {
3402-
return Flags.contains(TypeBindingFlag::HasDefaultedProtocol);
3363+
bool isUnavailable() const {
3364+
if (auto *decl = getDecl(Choice))
3365+
return decl->getAttrs().isUnavailable(decl->getASTContext());
3366+
return false;
34033367
}
34043368

34053369
// FIXME: Both of the accessors below are required to support
34063370
// performance optimization hacks in constraint solver.
34073371

3408-
bool isGenericOperator() const {
3409-
return Flags.contains(TypeBindingFlag::GenericOperator);
3410-
}
3411-
3412-
bool isSymmetricOperator() const {
3413-
return Flags.contains(TypeBindingFlag::SymmetricOperator);
3414-
}
3415-
3416-
virtual void print(llvm::raw_ostream &Out, SourceManager *SM) const = 0;
3417-
};
3418-
3419-
class DisjunctionChoice : public TypeBinding {
3420-
Constraint *Choice;
3421-
bool ExplicitConversion;
3422-
3423-
public:
3424-
DisjunctionChoice(unsigned index, Constraint *choice, bool explicitConversion)
3425-
: TypeBinding(index, computeFlags(choice)), Choice(choice),
3426-
ExplicitConversion(explicitConversion) {}
3427-
3428-
bool attempt(ConstraintSystem &cs) const override;
3372+
bool isGenericOperator() const;
3373+
bool isSymmetricOperator() const;
34293374

3430-
void print(llvm::raw_ostream &Out, SourceManager *SM) const override {
3375+
void print(llvm::raw_ostream &Out, SourceManager *SM) const {
34313376
Choice->print(Out, SM);
34323377
}
34333378

@@ -3439,15 +3384,6 @@ class DisjunctionChoice : public TypeBinding {
34393384
/// let's try to propagate its type early to prune search space.
34403385
void propagateConversionInfo(ConstraintSystem &cs) const;
34413386

3442-
static bool unavailable(Constraint *choice) {
3443-
if (auto *decl = getDecl(choice))
3444-
return decl->getAttrs().isUnavailable(decl->getASTContext());
3445-
return false;
3446-
}
3447-
3448-
static bool isGenericOp(Constraint *choice);
3449-
static bool isSymmetricOp(Constraint *choice);
3450-
34513387
static ValueDecl *getOperatorDecl(Constraint *choice) {
34523388
auto *decl = getDecl(choice);
34533389
if (!decl)
@@ -3466,46 +3402,25 @@ class DisjunctionChoice : public TypeBinding {
34663402

34673403
return choice.getDecl();
34683404
}
3469-
3470-
static TypeBindingFlags computeFlags(Constraint *choice) {
3471-
TypeBindingFlags flags;
3472-
if (choice->isDisabled())
3473-
flags |= TypeBindingFlag::Disabled;
3474-
if (unavailable(choice))
3475-
flags |= TypeBindingFlag::Unavailable;
3476-
if (isGenericOp(choice))
3477-
flags |= TypeBindingFlag::GenericOperator;
3478-
if (isSymmetricOp(choice))
3479-
flags |= TypeBindingFlag::SymmetricOperator;
3480-
return flags;
3481-
}
34823405
};
34833406

3484-
class TypeVariableBinding : public TypeBinding {
3407+
class TypeVariableBinding {
34853408
TypeVariableType *TypeVar;
34863409
ConstraintSystem::PotentialBinding Binding;
34873410

34883411
public:
3489-
TypeVariableBinding(unsigned index, TypeVariableType *typeVar,
3412+
TypeVariableBinding(TypeVariableType *typeVar,
34903413
ConstraintSystem::PotentialBinding &binding)
3491-
: TypeBinding(index, computeFlags(binding)), TypeVar(typeVar),
3492-
Binding(binding) {}
3414+
: TypeVar(typeVar), Binding(binding) {}
34933415

3494-
bool attempt(ConstraintSystem &cs) const override;
3416+
bool isDefaultable() const { return Binding.isDefaultableBinding(); }
34953417

3496-
void print(llvm::raw_ostream &Out, SourceManager *) const override {
3497-
Out << TypeVar->getString() << " := " << Binding.BindingType->getString();
3498-
}
3418+
bool hasDefaultedProtocol() const { return Binding.DefaultedProtocol; }
34993419

3500-
private:
3501-
static TypeBindingFlags
3502-
computeFlags(ConstraintSystem::PotentialBinding &binding) {
3503-
TypeBindingFlags flags;
3504-
if (binding.isDefaultableBinding())
3505-
flags |= TypeBindingFlag::Defaultable;
3506-
if (binding.DefaultedProtocol)
3507-
flags |= TypeBindingFlag::HasDefaultedProtocol;
3508-
return flags;
3420+
bool attempt(ConstraintSystem &cs) const;
3421+
3422+
void print(llvm::raw_ostream &Out, SourceManager *) const {
3423+
Out << TypeVar->getString() << " := " << Binding.BindingType->getString();
35093424
}
35103425
};
35113426

@@ -3564,8 +3479,7 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
35643479
if (needsToComputeNext() && !computeNext())
35653480
return None;
35663481

3567-
unsigned currIndex = Index++;
3568-
return TypeVariableBinding(currIndex, TypeVar, Bindings[currIndex]);
3482+
return TypeVariableBinding(TypeVar, Bindings[Index++]);
35693483
}
35703484

35713485
bool needsToComputeNext() const override { return Index >= Bindings.size(); }

0 commit comments

Comments
 (0)