Skip to content

Reduce usage of expression depth maps #28221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions lib/IDE/Refactoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2939,30 +2939,36 @@ static NumberLiteralExpr *getTrailingNumberLiteral(ResolvedCursorInfo Tok) {
// This cursor must point to the start of an expression.
if (Tok.Kind != CursorInfoKind::ExprStart)
return nullptr;
Expr *Parent = Tok.TrailingExpr;
assert(Parent);

// Check if an expression is a number literal.
auto IsLiteralNumber = [&](Expr *E) -> NumberLiteralExpr* {
if (auto *NL = dyn_cast<NumberLiteralExpr>(E)) {

// The sub-expression must have the same start loc with the outermost
// expression, i.e. the cursor position.
if (Parent->getStartLoc().getOpaquePointerValue() ==
E->getStartLoc().getOpaquePointerValue()) {
return NL;
}
}
return nullptr;
};

// For every sub-expression, try to find the literal expression that matches
// our criteria.
for (auto Pair: Parent->getDepthMap()) {
if (auto Result = IsLiteralNumber(Pair.getFirst())) {
return Result;
class FindLiteralNumber : public ASTWalker {
Expr * const parent;

public:
NumberLiteralExpr *found = nullptr;

explicit FindLiteralNumber(Expr *parent) : parent(parent) { }

std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
if (auto *literal = dyn_cast<NumberLiteralExpr>(expr)) {
// The sub-expression must have the same start loc with the outermost
// expression, i.e. the cursor position.
if (!found &&
parent->getStartLoc().getOpaquePointerValue() ==
expr->getStartLoc().getOpaquePointerValue()) {
found = literal;
}
}

return { found == nullptr, expr };
}
}
return nullptr;
};

auto parent = Tok.TrailingExpr;
FindLiteralNumber finder(parent);
parent->walk(finder);
return finder.found;
}

static std::string insertUnderscore(StringRef Text) {
Expand Down
15 changes: 7 additions & 8 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,7 @@ static Type getUnlabeledType(Type type, ASTContext &ctx) {

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

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

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

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

switch (compareSolutions(*this, viable, diff, i, j, ExprWeights)) {
switch (compareSolutions(*this, viable, diff, i, j)) {
case SolutionCompareResult::Identical:
// FIXME: Dub one of these the loser arbitrarily?
break;
Expand Down
7 changes: 3 additions & 4 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,7 +2695,6 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
// Heuristically, all other things being equal, we should complain about the
// ambiguous expression that (1) has the most overloads, (2) is deepest, or
// (3) comes earliest in the expression.
auto depthMap = expr->getDepthMap();
auto indexMap = expr->getPreorderIndexMap();

for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
Expand All @@ -2711,10 +2710,10 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
continue;
unsigned index = it->second;

auto e = depthMap.find(anchor);
if (e == depthMap.end())
auto optDepth = getExprDepth(anchor);
if (!optDepth)
continue;
unsigned depth = e->second.first;
unsigned depth = *optDepth;

// If we don't have a name to hang on to, it'll be hard to diagnose this
// overload.
Expand Down
15 changes: 13 additions & 2 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,18 @@ class ConstraintSystem {
return nullptr;
}

/// Retrieve the depth of the given expression.
Optional<unsigned> getExprDepth(Expr *expr) const {
auto e = ExprWeights.find(expr);
if (e != ExprWeights.end())
return e->second.first;

if (baseCS && baseCS != this)
return baseCS->getExprDepth(expr);

return None;
}

/// Returns a locator describing the callee for the anchor of a given locator.
///
/// - For an unresolved dot/member anchor, this will be a locator describing
Expand Down Expand Up @@ -3659,8 +3671,7 @@ class ConstraintSystem {
/// \param idx2 The index of the second solution.
static SolutionCompareResult
compareSolutions(ConstraintSystem &cs, ArrayRef<Solution> solutions,
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights);
const SolutionDiff &diff, unsigned idx1, unsigned idx2);

public:
/// Increase the score of the given kind for the current (partial) solution
Expand Down