Skip to content

Commit 8b2bfcb

Browse files
committed
Refactor a couple functions out of solveSimplified().
These are both logical bits of code that deserve first-class function status.
1 parent 783012f commit 8b2bfcb

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,32 +2253,32 @@ static bool shortCircuitDisjunctionAt(Constraint *constraint,
22532253
return false;
22542254
}
22552255

2256-
bool ConstraintSystem::solveSimplified(
2257-
SmallVectorImpl<Solution> &solutions,
2258-
FreeTypeVariableBinding allowFreeTypeVariables) {
2259-
// Collect disjunctions.
2260-
SmallVector<Constraint *, 4> disjunctions;
2256+
void ConstraintSystem::collectDisjunctions(
2257+
SmallVectorImpl<Constraint *> &disjunctions) {
22612258
for (auto &constraint : InactiveConstraints) {
22622259
if (constraint.getKind() == ConstraintKind::Disjunction)
22632260
disjunctions.push_back(&constraint);
22642261
}
2262+
}
22652263

2264+
std::pair<PotentialBindings, TypeVariableType *>
2265+
determineBestBindings(ConstraintSystem &CS) {
22662266
// Look for potential type variable bindings.
22672267
TypeVariableType *bestTypeVar = nullptr;
22682268
PotentialBindings bestBindings;
2269-
for (auto typeVar : TypeVariables) {
2269+
for (auto typeVar : CS.getTypeVariables()) {
22702270
// Skip any type variables that are bound.
22712271
if (typeVar->getImpl().hasRepresentativeOrFixed())
22722272
continue;
22732273

22742274
// Get potential bindings.
2275-
auto bindings = getPotentialBindings(*this, typeVar);
2275+
auto bindings = getPotentialBindings(CS, typeVar);
22762276
if (!bindings)
22772277
continue;
22782278

2279-
if (TC.getLangOpts().DebugConstraintSolver) {
2280-
auto &log = getASTContext().TypeCheckerDebug->getStream();
2281-
bindings.dump(typeVar, log, solverState->depth * 2);
2279+
if (CS.TC.getLangOpts().DebugConstraintSolver) {
2280+
auto &log = CS.getASTContext().TypeCheckerDebug->getStream();
2281+
bindings.dump(typeVar, log, CS.solverState->depth * 2);
22822282
}
22832283

22842284
// If these are the first bindings, or they are better than what
@@ -2289,6 +2289,20 @@ bool ConstraintSystem::solveSimplified(
22892289
}
22902290
}
22912291

2292+
return std::make_pair(bestBindings, bestTypeVar);
2293+
}
2294+
2295+
bool ConstraintSystem::solveSimplified(
2296+
SmallVectorImpl<Solution> &solutions,
2297+
FreeTypeVariableBinding allowFreeTypeVariables) {
2298+
2299+
SmallVector<Constraint *, 4> disjunctions;
2300+
collectDisjunctions(disjunctions);
2301+
2302+
TypeVariableType *bestTypeVar = nullptr;
2303+
PotentialBindings bestBindings;
2304+
std::tie(bestBindings, bestTypeVar) = determineBestBindings(*this);
2305+
22922306
// If we have a binding that does not involve type variables, or we have
22932307
// no other option, go ahead and try the bindings for this type variable.
22942308
if (bestBindings &&

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,9 @@ class ConstraintSystem {
20312031
ConstraintLocatorBuilder locator,
20322032
bool isFavored);
20332033

2034+
/// \brief Collect the current inactive disjunciton constraints.
2035+
void collectDisjunctions(SmallVectorImpl<Constraint *> &disjunctions);
2036+
20342037
/// \brief Solve the system of constraints after it has already been
20352038
/// simplified.
20362039
///

0 commit comments

Comments
 (0)