@@ -2458,6 +2458,75 @@ class ConstraintSystem {
2458
2458
SynthesizedConformances;
2459
2459
2460
2460
private:
2461
+ // / Describe the candidate expression for partial solving.
2462
+ // / This class used by shrink & solve methods which apply
2463
+ // / variation of directional path consistency algorithm in attempt
2464
+ // / to reduce scopes of the overload sets (disjunctions) in the system.
2465
+ class Candidate {
2466
+ Expr *E;
2467
+ DeclContext *DC;
2468
+ llvm::BumpPtrAllocator &Allocator;
2469
+
2470
+ // Contextual Information.
2471
+ Type CT;
2472
+ ContextualTypePurpose CTP;
2473
+
2474
+ public:
2475
+ Candidate (ConstraintSystem &cs, Expr *expr, Type ct = Type(),
2476
+ ContextualTypePurpose ctp = ContextualTypePurpose::CTP_Unused)
2477
+ : E(expr), DC(cs.DC), Allocator(cs.Allocator), CT(ct), CTP(ctp) {}
2478
+
2479
+ // / Return underlying expression.
2480
+ Expr *getExpr () const { return E; }
2481
+
2482
+ // / Try to solve this candidate sub-expression
2483
+ // / and re-write it's OSR domains afterwards.
2484
+ // /
2485
+ // / \param shrunkExprs The set of expressions which
2486
+ // / domains have been successfully shrunk so far.
2487
+ // /
2488
+ // / \returns true on solver failure, false otherwise.
2489
+ bool solve (llvm::SmallSetVector<OverloadSetRefExpr *, 4 > &shrunkExprs);
2490
+
2491
+ // / Apply solutions found by solver as reduced OSR sets for
2492
+ // / for current and all of it's sub-expressions.
2493
+ // /
2494
+ // / \param solutions The solutions found by running solver on the
2495
+ // / this candidate expression.
2496
+ // /
2497
+ // / \param shrunkExprs The set of expressions which
2498
+ // / domains have been successfully shrunk so far.
2499
+ void applySolutions (
2500
+ llvm::SmallVectorImpl<Solution> &solutions,
2501
+ llvm::SmallSetVector<OverloadSetRefExpr *, 4 > &shrunkExprs) const ;
2502
+
2503
+ // / Check if attempt at solving of the candidate makes sense given
2504
+ // / the current conditions - number of shrunk domains which is related
2505
+ // / to the given candidate over the total number of disjunctions present.
2506
+ static bool
2507
+ isTooComplexGiven (ConstraintSystem *const cs,
2508
+ llvm::SmallSetVector<OverloadSetRefExpr *, 4 > &shrunkExprs) {
2509
+ SmallVector<Constraint *, 8 > disjunctions;
2510
+ cs->collectDisjunctions (disjunctions);
2511
+
2512
+ unsigned unsolvedDisjunctions = disjunctions.size ();
2513
+ for (auto *disjunction : disjunctions) {
2514
+ auto *locator = disjunction->getLocator ();
2515
+ if (!locator)
2516
+ continue ;
2517
+
2518
+ if (auto *OSR = getAsExpr<OverloadSetRefExpr>(locator->getAnchor ())) {
2519
+ if (shrunkExprs.count (OSR) > 0 )
2520
+ --unsolvedDisjunctions;
2521
+ }
2522
+ }
2523
+
2524
+ unsigned threshold =
2525
+ cs->getASTContext ().TypeCheckerOpts .SolverShrinkUnsolvedThreshold ;
2526
+ return unsolvedDisjunctions >= threshold;
2527
+ }
2528
+ };
2529
+
2461
2530
// / Describes the current solver state.
2462
2531
struct SolverState {
2463
2532
SolverState (ConstraintSystem &cs,
@@ -5244,6 +5313,15 @@ class ConstraintSystem {
5244
5313
// / \returns true if an error occurred, false otherwise.
5245
5314
bool solveSimplified (SmallVectorImpl<Solution> &solutions);
5246
5315
5316
+ // / Find reduced domains of disjunction constraints for given
5317
+ // / expression, this is achieved to solving individual sub-expressions
5318
+ // / and combining resolving types. Such algorithm is called directional
5319
+ // / path consistency because it goes from children to parents for all
5320
+ // / related sub-expressions taking union of their domains.
5321
+ // /
5322
+ // / \param expr The expression to find reductions for.
5323
+ void shrink (Expr *expr);
5324
+
5247
5325
// / Pick a disjunction from the InactiveConstraints list.
5248
5326
// /
5249
5327
// / \returns The selected disjunction.
0 commit comments