Skip to content

Commit 49c40d9

Browse files
committed
[AST] Augment getDepthMap with information about parent expressions
Which is very useful for the solver because otherwise it'd have to compute and store this information twice.
1 parent 8944665 commit 49c40d9

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

include/swift/AST/Expr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ class alignas(8) Expr {
524524
/// the parent map.
525525
llvm::DenseMap<Expr *, Expr *> getParentMap();
526526

527-
/// Produce a mapping from each subexpression to its depth in the root
528-
/// expression. The root expression has depth 0, its children have depth
529-
/// 1, etc.
530-
llvm::DenseMap<Expr *, unsigned> getDepthMap();
527+
/// Produce a mapping from each subexpression to its depth and parent,
528+
/// in the root expression. The root expression has depth 0, its children have
529+
/// depth 1, etc.
530+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> getDepthMap();
531531

532532
/// Produce a mapping from each expression to its index according to a
533533
/// preorder traversal of the expressions. The parent has index 0, its first

lib/AST/Expr.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,17 +698,18 @@ llvm::DenseMap<Expr *, Expr *> Expr::getParentMap() {
698698
return parentMap;
699699
}
700700

701-
llvm::DenseMap<Expr *, unsigned> Expr::getDepthMap() {
701+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> Expr::getDepthMap() {
702702
class RecordingTraversal : public ASTWalker {
703703
public:
704-
llvm::DenseMap<Expr *, unsigned> &DepthMap;
704+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
705705
unsigned Depth = 0;
706706

707-
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &depthMap)
708-
: DepthMap(depthMap) { }
707+
explicit RecordingTraversal(
708+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
709+
: DepthMap(depthMap) {}
709710

710711
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
711-
DepthMap[E] = Depth;
712+
DepthMap[E] = {Depth, Parent.getAsExpr()};
712713
Depth++;
713714
return { true, E };
714715
}
@@ -719,7 +720,7 @@ llvm::DenseMap<Expr *, unsigned> Expr::getDepthMap() {
719720
}
720721
};
721722

722-
llvm::DenseMap<Expr *, unsigned> depthMap;
723+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> depthMap;
723724
RecordingTraversal traversal(depthMap);
724725
walk(traversal);
725726
return depthMap;

lib/Sema/CSRanking.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ static Type getUnlabeledType(Type type, ASTContext &ctx) {
772772
SolutionCompareResult ConstraintSystem::compareSolutions(
773773
ConstraintSystem &cs, ArrayRef<Solution> solutions,
774774
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
775-
llvm::DenseMap<Expr *, unsigned> &weights) {
775+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights) {
776776
if (cs.TC.getLangOpts().DebugConstraintSolver) {
777777
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
778778
log.indent(cs.solverState->depth * 2)
@@ -806,7 +806,7 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
806806
if (auto *anchor = locator->getAnchor()) {
807807
auto weight = weights.find(anchor);
808808
if (weight != weights.end())
809-
return weight->getSecond() + 1;
809+
return weight->getSecond().first + 1;
810810
}
811811

812812
return 1;
@@ -1210,10 +1210,10 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
12101210
: SolutionCompareResult::Incomparable;
12111211
}
12121212

1213-
Optional<unsigned>
1214-
ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
1215-
llvm::DenseMap<Expr *, unsigned> &weights,
1216-
bool minimize) {
1213+
Optional<unsigned> ConstraintSystem::findBestSolution(
1214+
SmallVectorImpl<Solution> &viable,
1215+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights,
1216+
bool minimize) {
12171217
if (viable.empty())
12181218
return None;
12191219
if (viable.size() == 1)

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,10 +2289,11 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
22892289
if (it == indexMap.end())
22902290
continue;
22912291
unsigned index = it->second;
2292-
it = depthMap.find(anchor);
2293-
if (it == depthMap.end())
2292+
2293+
auto e = depthMap.find(anchor);
2294+
if (e == depthMap.end())
22942295
continue;
2295-
unsigned depth = it->second;
2296+
unsigned depth = e->second.first;
22962297

22972298
// If we don't have a name to hang on to, it'll be hard to diagnose this
22982299
// overload.

lib/Sema/ConstraintSystem.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ class ConstraintSystem {
11671167
FreeTypeVariableBinding allowFreeTypeVariables);
11681168
~SolverState();
11691169

1170-
llvm::DenseMap<Expr *, unsigned> ExprWeights;
1170+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> ExprWeights;
11711171

11721172
/// The constraint system.
11731173
ConstraintSystem &CS;
@@ -1315,6 +1315,11 @@ class ConstraintSystem {
13151315
return AllowFreeTypeVariables != FreeTypeVariableBinding::Disallow;
13161316
}
13171317

1318+
Expr *getParentExpr(Expr *expr) {
1319+
const auto &e = ExprWeights.find(expr);
1320+
return (e != ExprWeights.end()) ? e->second.second : nullptr;
1321+
}
1322+
13181323
private:
13191324
/// The list of constraints that have been retired along the
13201325
/// current path, this list is used in LIFO fashion when constraints
@@ -1563,9 +1568,10 @@ class ConstraintSystem {
15631568
/// set of solutions should be filtered even if there is
15641569
/// no single best solution, see `findBestSolution` for
15651570
/// more details.
1566-
void filterSolutions(SmallVectorImpl<Solution> &solutions,
1567-
llvm::DenseMap<Expr *, unsigned> &weights,
1568-
bool minimize = false) {
1571+
void
1572+
filterSolutions(SmallVectorImpl<Solution> &solutions,
1573+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights,
1574+
bool minimize = false) {
15691575
if (solutions.size() < 2)
15701576
return;
15711577

@@ -3148,7 +3154,7 @@ class ConstraintSystem {
31483154
static SolutionCompareResult
31493155
compareSolutions(ConstraintSystem &cs, ArrayRef<Solution> solutions,
31503156
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
3151-
llvm::DenseMap<Expr *, unsigned> &weights);
3157+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights);
31523158

31533159
public:
31543160
/// Increase the score of the given kind for the current (partial) solution
@@ -3172,9 +3178,10 @@ class ConstraintSystem {
31723178
///
31733179
/// \returns The index of the best solution, or nothing if there was no
31743180
/// best solution.
3175-
Optional<unsigned> findBestSolution(SmallVectorImpl<Solution> &solutions,
3176-
llvm::DenseMap<Expr *, unsigned> &weights,
3177-
bool minimize);
3181+
Optional<unsigned>
3182+
findBestSolution(SmallVectorImpl<Solution> &solutions,
3183+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights,
3184+
bool minimize);
31783185

31793186
/// Apply a given solution to the expression, producing a fully
31803187
/// type-checked expression.

0 commit comments

Comments
 (0)