Skip to content

Commit 24b47b5

Browse files
committed
[Constraint solver] Sink Expr::getPreorderIndexMap() into its client.
1 parent 2d915f6 commit 24b47b5

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

include/swift/AST/Expr.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,6 @@ class alignas(8) Expr {
530530
/// the parent map.
531531
llvm::DenseMap<Expr *, Expr *> getParentMap();
532532

533-
/// Produce a mapping from each expression to its index according to a
534-
/// preorder traversal of the expressions. The parent has index 0, its first
535-
/// child has index 1, its second child has index 2 if the first child is a
536-
/// leaf node, etc.
537-
llvm::DenseMap<Expr *, unsigned> getPreorderIndexMap();
538-
539533
SWIFT_DEBUG_DUMP;
540534
void dump(raw_ostream &OS, unsigned Indent = 0) const;
541535
void dump(raw_ostream &OS, llvm::function_ref<Type(const Expr *)> getType,

lib/AST/Expr.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -721,28 +721,6 @@ llvm::DenseMap<Expr *, Expr *> Expr::getParentMap() {
721721
return parentMap;
722722
}
723723

724-
llvm::DenseMap<Expr *, unsigned> Expr::getPreorderIndexMap() {
725-
class RecordingTraversal : public ASTWalker {
726-
public:
727-
llvm::DenseMap<Expr *, unsigned> &IndexMap;
728-
unsigned Index = 0;
729-
730-
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &indexMap)
731-
: IndexMap(indexMap) { }
732-
733-
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
734-
IndexMap[E] = Index;
735-
Index++;
736-
return { true, E };
737-
}
738-
};
739-
740-
llvm::DenseMap<Expr *, unsigned> indexMap;
741-
RecordingTraversal traversal(indexMap);
742-
walk(traversal);
743-
return indexMap;
744-
}
745-
746724
//===----------------------------------------------------------------------===//
747725
// Support methods for Exprs.
748726
//===----------------------------------------------------------------------===//

lib/Sema/ConstraintSystem.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,29 @@ static DeclName getOverloadChoiceName(ArrayRef<OverloadChoice> choices) {
27272727
return name;
27282728
}
27292729

2730+
/// Extend the given index map with all of the subexpressions in the given
2731+
/// expression.
2732+
static void extendPreorderIndexMap(
2733+
Expr *expr, llvm::DenseMap<Expr *, unsigned> &indexMap) {
2734+
class RecordingTraversal : public ASTWalker {
2735+
public:
2736+
llvm::DenseMap<Expr *, unsigned> &IndexMap;
2737+
unsigned Index = 0;
2738+
2739+
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &indexMap)
2740+
: IndexMap(indexMap) { }
2741+
2742+
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
2743+
IndexMap[E] = Index;
2744+
Index++;
2745+
return { true, E };
2746+
}
2747+
};
2748+
2749+
RecordingTraversal traversal(indexMap);
2750+
expr->walk(traversal);
2751+
}
2752+
27302753
bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27312754
ArrayRef<Solution> solutions) {
27322755
// Produce a diff of the solutions.
@@ -2747,7 +2770,10 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27472770
// Heuristically, all other things being equal, we should complain about the
27482771
// ambiguous expression that (1) has the most overloads, (2) is deepest, or
27492772
// (3) comes earliest in the expression.
2750-
auto indexMap = expr->getPreorderIndexMap();
2773+
llvm::DenseMap<Expr *, unsigned> indexMap;
2774+
for (auto expr : InputExprs) {
2775+
extendPreorderIndexMap(expr, indexMap);
2776+
}
27512777

27522778
for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
27532779
auto &overload = diff.overloads[i];

0 commit comments

Comments
 (0)