Skip to content

Commit 23edd22

Browse files
authored
Merge pull request #28221 from DougGregor/reduce-depth-maps
Reduce usage of expression depth maps
2 parents 63c1f84 + f06b568 commit 23edd22

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,30 +2939,36 @@ static NumberLiteralExpr *getTrailingNumberLiteral(ResolvedCursorInfo Tok) {
29392939
// This cursor must point to the start of an expression.
29402940
if (Tok.Kind != CursorInfoKind::ExprStart)
29412941
return nullptr;
2942-
Expr *Parent = Tok.TrailingExpr;
2943-
assert(Parent);
2944-
2945-
// Check if an expression is a number literal.
2946-
auto IsLiteralNumber = [&](Expr *E) -> NumberLiteralExpr* {
2947-
if (auto *NL = dyn_cast<NumberLiteralExpr>(E)) {
2948-
2949-
// The sub-expression must have the same start loc with the outermost
2950-
// expression, i.e. the cursor position.
2951-
if (Parent->getStartLoc().getOpaquePointerValue() ==
2952-
E->getStartLoc().getOpaquePointerValue()) {
2953-
return NL;
2954-
}
2955-
}
2956-
return nullptr;
2957-
};
2942+
29582943
// For every sub-expression, try to find the literal expression that matches
29592944
// our criteria.
2960-
for (auto Pair: Parent->getDepthMap()) {
2961-
if (auto Result = IsLiteralNumber(Pair.getFirst())) {
2962-
return Result;
2945+
class FindLiteralNumber : public ASTWalker {
2946+
Expr * const parent;
2947+
2948+
public:
2949+
NumberLiteralExpr *found = nullptr;
2950+
2951+
explicit FindLiteralNumber(Expr *parent) : parent(parent) { }
2952+
2953+
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
2954+
if (auto *literal = dyn_cast<NumberLiteralExpr>(expr)) {
2955+
// The sub-expression must have the same start loc with the outermost
2956+
// expression, i.e. the cursor position.
2957+
if (!found &&
2958+
parent->getStartLoc().getOpaquePointerValue() ==
2959+
expr->getStartLoc().getOpaquePointerValue()) {
2960+
found = literal;
2961+
}
2962+
}
2963+
2964+
return { found == nullptr, expr };
29632965
}
2964-
}
2965-
return nullptr;
2966+
};
2967+
2968+
auto parent = Tok.TrailingExpr;
2969+
FindLiteralNumber finder(parent);
2970+
parent->walk(finder);
2971+
return finder.found;
29662972
}
29672973

29682974
static std::string insertUnderscore(StringRef Text) {

lib/Sema/CSRanking.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,7 @@ static Type getUnlabeledType(Type type, ASTContext &ctx) {
710710

711711
SolutionCompareResult ConstraintSystem::compareSolutions(
712712
ConstraintSystem &cs, ArrayRef<Solution> solutions,
713-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
714-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights) {
713+
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
715714
if (cs.getASTContext().LangOpts.DebugConstraintSolver) {
716715
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
717716
log.indent(cs.solverState->depth * 2)
@@ -743,9 +742,9 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
743742

744743
auto getWeight = [&](ConstraintLocator *locator) -> unsigned {
745744
if (auto *anchor = locator->getAnchor()) {
746-
auto weight = weights.find(anchor);
747-
if (weight != weights.end())
748-
return weight->getSecond().first + 1;
745+
auto weight = cs.getExprDepth(anchor);
746+
if (weight)
747+
return *weight + 1;
749748
}
750749

751750
return 1;
@@ -1197,7 +1196,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
11971196
SmallVector<bool, 16> losers(viable.size(), false);
11981197
unsigned bestIdx = 0;
11991198
for (unsigned i = 1, n = viable.size(); i != n; ++i) {
1200-
switch (compareSolutions(*this, viable, diff, i, bestIdx, ExprWeights)) {
1199+
switch (compareSolutions(*this, viable, diff, i, bestIdx)) {
12011200
case SolutionCompareResult::Identical:
12021201
// FIXME: Might want to warn about this in debug builds, so we can
12031202
// find a way to eliminate the redundancy in the search space.
@@ -1221,7 +1220,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
12211220
if (i == bestIdx)
12221221
continue;
12231222

1224-
switch (compareSolutions(*this, viable, diff, bestIdx, i, ExprWeights)) {
1223+
switch (compareSolutions(*this, viable, diff, bestIdx, i)) {
12251224
case SolutionCompareResult::Identical:
12261225
// FIXME: Might want to warn about this in debug builds, so we can
12271226
// find a way to eliminate the redundancy in the search space.
@@ -1273,7 +1272,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
12731272
if (losers[j])
12741273
continue;
12751274

1276-
switch (compareSolutions(*this, viable, diff, i, j, ExprWeights)) {
1275+
switch (compareSolutions(*this, viable, diff, i, j)) {
12771276
case SolutionCompareResult::Identical:
12781277
// FIXME: Dub one of these the loser arbitrarily?
12791278
break;

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,6 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27002700
// Heuristically, all other things being equal, we should complain about the
27012701
// ambiguous expression that (1) has the most overloads, (2) is deepest, or
27022702
// (3) comes earliest in the expression.
2703-
auto depthMap = expr->getDepthMap();
27042703
auto indexMap = expr->getPreorderIndexMap();
27052704

27062705
for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
@@ -2716,10 +2715,10 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27162715
continue;
27172716
unsigned index = it->second;
27182717

2719-
auto e = depthMap.find(anchor);
2720-
if (e == depthMap.end())
2718+
auto optDepth = getExprDepth(anchor);
2719+
if (!optDepth)
27212720
continue;
2722-
unsigned depth = e->second.first;
2721+
unsigned depth = *optDepth;
27232722

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

lib/Sema/ConstraintSystem.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,18 @@ class ConstraintSystem {
19931993
return nullptr;
19941994
}
19951995

1996+
/// Retrieve the depth of the given expression.
1997+
Optional<unsigned> getExprDepth(Expr *expr) const {
1998+
auto e = ExprWeights.find(expr);
1999+
if (e != ExprWeights.end())
2000+
return e->second.first;
2001+
2002+
if (baseCS && baseCS != this)
2003+
return baseCS->getExprDepth(expr);
2004+
2005+
return None;
2006+
}
2007+
19962008
/// Returns a locator describing the callee for the anchor of a given locator.
19972009
///
19982010
/// - For an unresolved dot/member anchor, this will be a locator describing
@@ -3661,8 +3673,7 @@ class ConstraintSystem {
36613673
/// \param idx2 The index of the second solution.
36623674
static SolutionCompareResult
36633675
compareSolutions(ConstraintSystem &cs, ArrayRef<Solution> solutions,
3664-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
3665-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights);
3676+
const SolutionDiff &diff, unsigned idx1, unsigned idx2);
36663677

36673678
public:
36683679
/// Increase the score of the given kind for the current (partial) solution

0 commit comments

Comments
 (0)