[Constraint system] Sink Expr::getDepthMap() into its one client.

NFC. It'll be easier to refactor this when it's not an API on Expr.
This commit is contained in:
Doug Gregor
2019-11-14 13:21:13 -08:00
parent 4c2a7bf1ac
commit 01f203a54d
3 changed files with 33 additions and 35 deletions

View File

@@ -73,6 +73,36 @@ ExpressionTimer::~ExpressionTimer() {
.highlight(E->getSourceRange());
}
/// Extend the given depth map by adding depths for all of the subexpressions
/// of the given expression.
static void extendDepthMap(
Expr *expr,
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
class RecordingTraversal : public ASTWalker {
public:
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
unsigned Depth = 0;
explicit RecordingTraversal(
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
: DepthMap(depthMap) {}
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
DepthMap[E] = {Depth, Parent.getAsExpr()};
Depth++;
return { true, E };
}
Expr *walkToExprPost(Expr *E) override {
Depth--;
return E;
}
};
RecordingTraversal traversal(depthMap);
expr->walk(traversal);
}
ConstraintSystem::ConstraintSystem(DeclContext *dc,
ConstraintSystemOptions options,
Expr *expr)
@@ -80,8 +110,9 @@ ConstraintSystem::ConstraintSystem(DeclContext *dc,
Arena(dc->getASTContext(), Allocator),
CG(*new ConstraintGraph(*this))
{
if (expr)
ExprWeights = expr->getDepthMap();
if (expr) {
extendDepthMap(expr, ExprWeights);
}
assert(DC && "context required");
}