@@ -123,13 +123,14 @@ Optional<Type> ConstraintSystem::checkTypeOfBinding(TypeVariableType *typeVar,
123
123
return type;
124
124
}
125
125
126
- Solution ConstraintSystem::finalize (
127
- FreeTypeVariableBinding allowFreeTypeVariables) {
126
+ Solution ConstraintSystem::finalize () {
127
+ assert (solverState);
128
+
128
129
// Create the solution.
129
130
Solution solution (*this , CurrentScore);
130
131
131
132
// Update the best score we've seen so far.
132
- if (solverState && !retainAllSolutions ()) {
133
+ if (!retainAllSolutions ()) {
133
134
assert (TC.getLangOpts ().DisableConstraintSolverPerformanceHacks ||
134
135
!solverState->BestScore || CurrentScore <= *solverState->BestScore );
135
136
@@ -142,7 +143,7 @@ Solution ConstraintSystem::finalize(
142
143
if (getFixedType (tv))
143
144
continue ;
144
145
145
- switch (allowFreeTypeVariables ) {
146
+ switch (solverState-> AllowFreeTypeVariables ) {
146
147
case FreeTypeVariableBinding::Disallow:
147
148
llvm_unreachable (" Solver left free type variables" );
148
149
@@ -386,9 +387,10 @@ void truncate(SmallVectorImpl<T> &vec, unsigned newSize) {
386
387
387
388
} // end anonymous namespace
388
389
389
- ConstraintSystem::SolverState::SolverState (Expr *const expr,
390
- ConstraintSystem &cs)
391
- : CS(cs) {
390
+ ConstraintSystem::SolverState::SolverState (
391
+ Expr *const expr, ConstraintSystem &cs,
392
+ FreeTypeVariableBinding allowFreeTypeVariables)
393
+ : CS(cs), AllowFreeTypeVariables(allowFreeTypeVariables) {
392
394
assert (!CS.solverState &&
393
395
" Constraint system should not already have solver state!" );
394
396
CS.solverState = this ;
@@ -520,8 +522,7 @@ ConstraintSystem::SolverScope::~SolverScope() {
520
522
bool ConstraintSystem::tryTypeVariableBindings (
521
523
TypeVariableType *typeVar,
522
524
ArrayRef<ConstraintSystem::PotentialBinding> initialBindings,
523
- SmallVectorImpl<Solution> &solutions,
524
- FreeTypeVariableBinding allowFreeTypeVariables) {
525
+ SmallVectorImpl<Solution> &solutions) {
525
526
auto &TC = getTypeChecker ();
526
527
bool anySolved = false ;
527
528
bool sawFirstLiteralConstraint = false ;
@@ -558,7 +559,7 @@ bool ConstraintSystem::tryTypeVariableBindings(
558
559
if (binding.isDefaultableBinding ())
559
560
DefaultedConstraints.push_back (binding.DefaultableBinding );
560
561
561
- return !solveRec (solutions, allowFreeTypeVariables );
562
+ return !solveRec (solutions);
562
563
};
563
564
564
565
if (TC.getLangOpts ().DebugConstraintSolver ) {
@@ -713,12 +714,12 @@ bool ConstraintSystem::Candidate::solve(
713
714
// Try to solve the system and record all available solutions.
714
715
llvm::SmallVector<Solution, 2 > solutions;
715
716
{
716
- SolverState state (E, cs);
717
+ SolverState state (E, cs, FreeTypeVariableBinding::Allow );
717
718
718
719
// Use solveRec() instead of solve() in here, because solve()
719
720
// would try to deduce the best solution, which we don't
720
721
// really want. Instead, we want the reduced set of domain choices.
721
- cs.solveRec (solutions, FreeTypeVariableBinding::Allow );
722
+ cs.solveRec (solutions);
722
723
}
723
724
724
725
if (TC.getLangOpts ().DebugConstraintSolver ) {
@@ -1302,10 +1303,10 @@ bool ConstraintSystem::solve(Expr *const expr,
1302
1303
SmallVectorImpl<Solution> &solutions,
1303
1304
FreeTypeVariableBinding allowFreeTypeVariables) {
1304
1305
// Set up solver state.
1305
- SolverState state (expr, *this );
1306
+ SolverState state (expr, *this , allowFreeTypeVariables );
1306
1307
1307
1308
// Solve the system.
1308
- solveRec (solutions, allowFreeTypeVariables );
1309
+ solveRec (solutions);
1309
1310
1310
1311
if (TC.getLangOpts ().DebugConstraintSolver ) {
1311
1312
auto &log = getASTContext ().TypeCheckerDebug ->getStream ();
@@ -1330,8 +1331,7 @@ bool ConstraintSystem::solve(Expr *const expr,
1330
1331
return solutions.empty () || getExpressionTooComplex (solutions);
1331
1332
}
1332
1333
1333
- bool ConstraintSystem::solveRec (SmallVectorImpl<Solution> &solutions,
1334
- FreeTypeVariableBinding allowFreeTypeVariables){
1334
+ bool ConstraintSystem::solveRec (SmallVectorImpl<Solution> &solutions) {
1335
1335
// If we already failed, or simplification fails, we're done.
1336
1336
if (failedConstraint || simplify ())
1337
1337
return true ;
@@ -1347,11 +1347,10 @@ bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
1347
1347
1348
1348
// If any free type variables remain and we're not allowed to have them,
1349
1349
// fail.
1350
- if (allowFreeTypeVariables == FreeTypeVariableBinding::Disallow &&
1351
- hasFreeTypeVariables ())
1350
+ if (!solverState->allowsFreeTypeVariables () && hasFreeTypeVariables ())
1352
1351
return true ;
1353
1352
1354
- auto solution = finalize (allowFreeTypeVariables );
1353
+ auto solution = finalize ();
1355
1354
if (TC.getLangOpts ().DebugConstraintSolver ) {
1356
1355
auto &log = getASTContext ().TypeCheckerDebug ->getStream ();
1357
1356
log.indent (solverState->depth * 2 )
@@ -1376,7 +1375,7 @@ bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
1376
1375
// If we don't have more than one component, just solve the whole
1377
1376
// system.
1378
1377
if (numComponents < 2 )
1379
- return solveSimplified (solutions, allowFreeTypeVariables );
1378
+ return solveSimplified (solutions);
1380
1379
1381
1380
if (TC.Context .LangOpts .DebugConstraintSolver ) {
1382
1381
auto &log = getASTContext ().TypeCheckerDebug ->getStream ();
@@ -1487,8 +1486,7 @@ bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
1487
1486
}
1488
1487
1489
1488
// Solve for this component. If it fails, we're done.
1490
- bool failed = bucket.solve (*this , partialSolutions[component],
1491
- allowFreeTypeVariables);
1489
+ bool failed = bucket.solve (*this , partialSolutions[component]);
1492
1490
1493
1491
if (failed) {
1494
1492
if (TC.getLangOpts ().DebugConstraintSolver ) {
@@ -1553,7 +1551,7 @@ bool ConstraintSystem::solveRec(SmallVectorImpl<Solution> &solutions,
1553
1551
// skip it.
1554
1552
if (!worseThanBestSolution ()) {
1555
1553
// Finalize this solution.
1556
- auto solution = finalize (allowFreeTypeVariables );
1554
+ auto solution = finalize ();
1557
1555
if (TC.getLangOpts ().DebugConstraintSolver ) {
1558
1556
auto &log = getASTContext ().TypeCheckerDebug ->getStream ();
1559
1557
log.indent (solverState->depth * 2 )
@@ -1777,8 +1775,7 @@ Constraint *ConstraintSystem::selectDisjunction() {
1777
1775
}
1778
1776
1779
1777
bool ConstraintSystem::solveForDisjunctionChoices (
1780
- Disjunction &disjunction, SmallVectorImpl<Solution> &solutions,
1781
- FreeTypeVariableBinding allowFreeTypeVariables) {
1778
+ Disjunction &disjunction, SmallVectorImpl<Solution> &solutions) {
1782
1779
Optional<Score> bestNonGenericScore;
1783
1780
Optional<std::pair<DisjunctionChoice, Score>> lastSolvedChoice;
1784
1781
@@ -1835,7 +1832,7 @@ bool ConstraintSystem::solveForDisjunctionChoices(
1835
1832
}
1836
1833
}
1837
1834
1838
- if (auto score = currentChoice.solve (solutions, allowFreeTypeVariables )) {
1835
+ if (auto score = currentChoice.solve (solutions)) {
1839
1836
if (!currentChoice.isGenericOperator () &&
1840
1837
currentChoice.isSymmetricOperator ()) {
1841
1838
if (!bestNonGenericScore || score < bestNonGenericScore)
@@ -1855,8 +1852,7 @@ bool ConstraintSystem::solveForDisjunctionChoices(
1855
1852
}
1856
1853
1857
1854
bool ConstraintSystem::solveForDisjunction (
1858
- Constraint *disjunction, SmallVectorImpl<Solution> &solutions,
1859
- FreeTypeVariableBinding allowFreeTypeVariables) {
1855
+ Constraint *disjunction, SmallVectorImpl<Solution> &solutions) {
1860
1856
assert (disjunction->getKind () == ConstraintKind::Disjunction);
1861
1857
1862
1858
// Remove this disjunction constraint from the list.
@@ -1915,8 +1911,7 @@ bool ConstraintSystem::solveForDisjunction(
1915
1911
auto choices = Disjunction (*this , disjunction->getNestedConstraints (),
1916
1912
locator, disjunction->isExplicitConversion ());
1917
1913
1918
- auto noSolutions =
1919
- solveForDisjunctionChoices (choices, solutions, allowFreeTypeVariables);
1914
+ auto noSolutions = solveForDisjunctionChoices (choices, solutions);
1920
1915
1921
1916
if (hasDisabledChoices) {
1922
1917
// Re-enable previously disabled overload choices.
@@ -1933,12 +1928,8 @@ bool ConstraintSystem::solveForDisjunction(
1933
1928
return noSolutions;
1934
1929
}
1935
1930
1936
- bool ConstraintSystem::solveSimplified (
1937
- SmallVectorImpl<Solution> &solutions,
1938
- FreeTypeVariableBinding allowFreeTypeVariables) {
1939
-
1931
+ bool ConstraintSystem::solveSimplified (SmallVectorImpl<Solution> &solutions) {
1940
1932
auto *disjunction = selectDisjunction ();
1941
-
1942
1933
auto bestBindings = determineBestBindings ();
1943
1934
1944
1935
// If we've already explored a lot of potential solutions, bail.
@@ -1951,17 +1942,15 @@ bool ConstraintSystem::solveSimplified(
1951
1942
if (bestBindings && (!disjunction || (!bestBindings->InvolvesTypeVariables &&
1952
1943
!bestBindings->FullyBound ))) {
1953
1944
return tryTypeVariableBindings (bestBindings->TypeVar ,
1954
- bestBindings->Bindings , solutions,
1955
- allowFreeTypeVariables);
1945
+ bestBindings->Bindings , solutions);
1956
1946
}
1957
1947
1958
1948
if (disjunction)
1959
- return solveForDisjunction (disjunction, solutions, allowFreeTypeVariables );
1949
+ return solveForDisjunction (disjunction, solutions);
1960
1950
1961
1951
// If there are no disjunctions we can't solve this system unless we have
1962
1952
// free type variables and are allowing them in the solution.
1963
- if (allowFreeTypeVariables == FreeTypeVariableBinding::Disallow ||
1964
- !hasFreeTypeVariables ())
1953
+ if (!solverState->allowsFreeTypeVariables () || !hasFreeTypeVariables ())
1965
1954
return true ;
1966
1955
1967
1956
// If this solution is worse than the best solution we've seen so far,
@@ -1981,7 +1970,7 @@ bool ConstraintSystem::solveSimplified(
1981
1970
}
1982
1971
}
1983
1972
1984
- auto solution = finalize (allowFreeTypeVariables );
1973
+ auto solution = finalize ();
1985
1974
if (TC.getLangOpts ().DebugConstraintSolver ) {
1986
1975
auto &log = getASTContext ().TypeCheckerDebug ->getStream ();
1987
1976
log.indent (solverState->depth * 2 ) << " (found solution)\n " ;
@@ -1991,15 +1980,13 @@ bool ConstraintSystem::solveSimplified(
1991
1980
return false ;
1992
1981
}
1993
1982
1994
- Optional<Score>
1995
- DisjunctionChoice::solve (SmallVectorImpl<Solution> &solutions,
1996
- FreeTypeVariableBinding allowFreeTypeVariables) {
1983
+ Optional<Score> DisjunctionChoice::solve (SmallVectorImpl<Solution> &solutions) {
1997
1984
CS->simplifyDisjunctionChoice (Choice);
1998
1985
1999
1986
if (ExplicitConversion)
2000
1987
propagateConversionInfo ();
2001
1988
2002
- if (CS->solveRec (solutions, allowFreeTypeVariables ))
1989
+ if (CS->solveRec (solutions))
2003
1990
return None;
2004
1991
2005
1992
assert (!solutions.empty ());
0 commit comments