Skip to content

Commit 0b6ef0d

Browse files
authored
Merge pull request #28276 from DougGregor/constraint-solver-expr-ctor
[Constraint solver] Remove expression from the constructor.
2 parents 0540535 + abf41e7 commit 0b6ef0d

File tree

7 files changed

+100
-87
lines changed

7 files changed

+100
-87
lines changed

include/swift/AST/Expr.h

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

533-
/// Produce a mapping from each subexpression to its depth and parent,
534-
/// in the root expression. The root expression has depth 0, its children have
535-
/// depth 1, etc.
536-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> getDepthMap();
537-
538-
/// Produce a mapping from each expression to its index according to a
539-
/// preorder traversal of the expressions. The parent has index 0, its first
540-
/// child has index 1, its second child has index 2 if the first child is a
541-
/// leaf node, etc.
542-
llvm::DenseMap<Expr *, unsigned> getPreorderIndexMap();
543-
544533
SWIFT_DEBUG_DUMP;
545534
void dump(raw_ostream &OS, unsigned Indent = 0) const;
546535
void dump(raw_ostream &OS, llvm::function_ref<Type(const Expr *)> getType,

lib/AST/Expr.cpp

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

724-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> Expr::getDepthMap() {
725-
class RecordingTraversal : public ASTWalker {
726-
public:
727-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
728-
unsigned Depth = 0;
729-
730-
explicit RecordingTraversal(
731-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
732-
: DepthMap(depthMap) {}
733-
734-
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
735-
DepthMap[E] = {Depth, Parent.getAsExpr()};
736-
Depth++;
737-
return { true, E };
738-
}
739-
740-
Expr *walkToExprPost(Expr *E) override {
741-
Depth--;
742-
return E;
743-
}
744-
};
745-
746-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> depthMap;
747-
RecordingTraversal traversal(depthMap);
748-
walk(traversal);
749-
return depthMap;
750-
}
751-
752-
llvm::DenseMap<Expr *, unsigned> Expr::getPreorderIndexMap() {
753-
class RecordingTraversal : public ASTWalker {
754-
public:
755-
llvm::DenseMap<Expr *, unsigned> &IndexMap;
756-
unsigned Index = 0;
757-
758-
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &indexMap)
759-
: IndexMap(indexMap) { }
760-
761-
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
762-
IndexMap[E] = Index;
763-
Index++;
764-
return { true, E };
765-
}
766-
};
767-
768-
llvm::DenseMap<Expr *, unsigned> indexMap;
769-
RecordingTraversal traversal(indexMap);
770-
walk(traversal);
771-
return indexMap;
772-
}
773-
774724
//===----------------------------------------------------------------------===//
775725
// Support methods for Exprs.
776726
//===----------------------------------------------------------------------===//

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,8 @@ namespace {
37543754
} // end anonymous namespace
37553755

37563756
Expr *ConstraintSystem::generateConstraints(Expr *expr, DeclContext *dc) {
3757+
InputExprs.insert(expr);
3758+
37573759
// Remove implicit conversions from the expression.
37583760
expr = expr->walk(SanitizeExpr(*this));
37593761

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ bool ConstraintSystem::Candidate::solve(
570570
};
571571

572572
// Allocate new constraint system for sub-expression.
573-
ConstraintSystem cs(DC, None, E);
573+
ConstraintSystem cs(DC, None);
574574
cs.baseCS = &BaseCS;
575575

576576
// Set up expression type checker timer for the candidate.

lib/Sema/ConstraintSystem.cpp

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,13 @@ ExpressionTimer::~ExpressionTimer() {
7373
.highlight(E->getSourceRange());
7474
}
7575

76+
7677
ConstraintSystem::ConstraintSystem(DeclContext *dc,
77-
ConstraintSystemOptions options,
78-
Expr *expr)
78+
ConstraintSystemOptions options)
7979
: Context(dc->getASTContext()), DC(dc), Options(options),
8080
Arena(dc->getASTContext(), Allocator),
8181
CG(*new ConstraintGraph(*this))
8282
{
83-
if (expr)
84-
ExprWeights = expr->getDepthMap();
85-
8683
assert(DC && "context required");
8784
}
8885

@@ -499,6 +496,57 @@ ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
499496
return getConstraintLocator(anchor);
500497
}
501498

499+
/// Extend the given depth map by adding depths for all of the subexpressions
500+
/// of the given expression.
501+
static void extendDepthMap(
502+
Expr *expr,
503+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
504+
class RecordingTraversal : public ASTWalker {
505+
public:
506+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
507+
unsigned Depth = 0;
508+
509+
explicit RecordingTraversal(
510+
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap)
511+
: DepthMap(depthMap) {}
512+
513+
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
514+
DepthMap[E] = {Depth, Parent.getAsExpr()};
515+
Depth++;
516+
return { true, E };
517+
}
518+
519+
Expr *walkToExprPost(Expr *E) override {
520+
Depth--;
521+
return E;
522+
}
523+
};
524+
525+
RecordingTraversal traversal(depthMap);
526+
expr->walk(traversal);
527+
}
528+
529+
Optional<std::pair<unsigned, Expr *>> ConstraintSystem::getExprDepthAndParent(
530+
Expr *expr) {
531+
// Check whether the parent has this information.
532+
if (baseCS && baseCS != this) {
533+
if (auto known = baseCS->getExprDepthAndParent(expr))
534+
return *known;
535+
}
536+
537+
// Bring the set of expression weights up to date.
538+
while (NumInputExprsInWeights < InputExprs.size()) {
539+
extendDepthMap(InputExprs[NumInputExprsInWeights], ExprWeights);
540+
++NumInputExprsInWeights;
541+
}
542+
543+
auto e = ExprWeights.find(expr);
544+
if (e != ExprWeights.end())
545+
return e->second;
546+
547+
return None;
548+
}
549+
502550
Type ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
503551
ConstraintLocatorBuilder locator,
504552
OpenedTypeMap &replacements) {
@@ -2683,6 +2731,29 @@ static DeclName getOverloadChoiceName(ArrayRef<OverloadChoice> choices) {
26832731
return name;
26842732
}
26852733

2734+
/// Extend the given index map with all of the subexpressions in the given
2735+
/// expression.
2736+
static void extendPreorderIndexMap(
2737+
Expr *expr, llvm::DenseMap<Expr *, unsigned> &indexMap) {
2738+
class RecordingTraversal : public ASTWalker {
2739+
public:
2740+
llvm::DenseMap<Expr *, unsigned> &IndexMap;
2741+
unsigned Index = 0;
2742+
2743+
explicit RecordingTraversal(llvm::DenseMap<Expr *, unsigned> &indexMap)
2744+
: IndexMap(indexMap) { }
2745+
2746+
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
2747+
IndexMap[E] = Index;
2748+
Index++;
2749+
return { true, E };
2750+
}
2751+
};
2752+
2753+
RecordingTraversal traversal(indexMap);
2754+
expr->walk(traversal);
2755+
}
2756+
26862757
bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
26872758
ArrayRef<Solution> solutions) {
26882759
// Produce a diff of the solutions.
@@ -2703,7 +2774,10 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27032774
// Heuristically, all other things being equal, we should complain about the
27042775
// ambiguous expression that (1) has the most overloads, (2) is deepest, or
27052776
// (3) comes earliest in the expression.
2706-
auto indexMap = expr->getPreorderIndexMap();
2777+
llvm::DenseMap<Expr *, unsigned> indexMap;
2778+
for (auto expr : InputExprs) {
2779+
extendPreorderIndexMap(expr, indexMap);
2780+
}
27072781

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

lib/Sema/ConstraintSystem.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,12 @@ class ConstraintSystem {
10381038
unsigned CountDisjunctions = 0;
10391039

10401040
private:
1041+
/// The set of expressions for which we have generated constraints.
1042+
llvm::SetVector<Expr *> InputExprs;
1043+
1044+
/// The number of input expressions whose parents and depths have
1045+
/// been entered into \c ExprWeights.
1046+
unsigned NumInputExprsInWeights = 0;
10411047

10421048
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> ExprWeights;
10431049

@@ -1662,8 +1668,7 @@ class ConstraintSystem {
16621668
};
16631669

16641670
ConstraintSystem(DeclContext *dc,
1665-
ConstraintSystemOptions options,
1666-
Expr *expr = nullptr);
1671+
ConstraintSystemOptions options);
16671672
~ConstraintSystem();
16681673

16691674
/// Retrieve the type checker associated with this constraint system.
@@ -1986,29 +1991,22 @@ class ConstraintSystem {
19861991
getConstraintLocator(const ConstraintLocatorBuilder &builder);
19871992

19881993
/// Lookup and return parent associated with given expression.
1989-
Expr *getParentExpr(Expr *expr) const {
1990-
auto e = ExprWeights.find(expr);
1991-
if (e != ExprWeights.end())
1992-
return e->second.second;
1993-
1994-
if (baseCS && baseCS != this)
1995-
return baseCS->getParentExpr(expr);
1996-
1994+
Expr *getParentExpr(Expr *expr) {
1995+
if (auto result = getExprDepthAndParent(expr))
1996+
return result->second;
19971997
return nullptr;
19981998
}
19991999

20002000
/// Retrieve the depth of the given expression.
2001-
Optional<unsigned> getExprDepth(Expr *expr) const {
2002-
auto e = ExprWeights.find(expr);
2003-
if (e != ExprWeights.end())
2004-
return e->second.first;
2005-
2006-
if (baseCS && baseCS != this)
2007-
return baseCS->getExprDepth(expr);
2008-
2001+
Optional<unsigned> getExprDepth(Expr *expr) {
2002+
if (auto result = getExprDepthAndParent(expr))
2003+
return result->first;
20092004
return None;
20102005
}
20112006

2007+
/// Retrieve the depth and parent expression of the given expression.
2008+
Optional<std::pair<unsigned, Expr *>> getExprDepthAndParent(Expr *expr);
2009+
20122010
/// Returns a locator describing the callee for the anchor of a given locator.
20132011
///
20142012
/// - For an unresolved dot/member anchor, this will be a locator describing

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
21972197
if (options.contains(TypeCheckExprFlags::SubExpressionDiagnostics))
21982198
csOptions |= ConstraintSystemFlags::SubExpressionDiagnostics;
21992199

2200-
ConstraintSystem cs(dc, csOptions, expr);
2200+
ConstraintSystem cs(dc, csOptions);
22012201
cs.baseCS = baseCS;
22022202

22032203
// Verify that a purpose was specified if a convertType was. Note that it is

0 commit comments

Comments
 (0)