Skip to content

Commit e9ba6c7

Browse files
committed
Allow SemaAnnotator to handle patterns
Add the necessary walking hooks, and fix ReferenceCollector to use it.
1 parent 2cfd2bf commit e9ba6c7

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

include/swift/IDE/SourceEntityWalker.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class SourceEntityWalker {
7373
/// Walks the provided Expr.
7474
/// \returns true if traversal was aborted, false otherwise.
7575
bool walk(Expr *E);
76+
/// Walks the provided Pattern.
77+
/// \returns true if traversal was aborted, false otherwise.
78+
bool walk(Pattern *P);
7679
/// Walks the provided ASTNode.
7780
/// \returns true if traversal was aborted, false otherwise.
7881
bool walk(ASTNode N);
@@ -101,6 +104,14 @@ class SourceEntityWalker {
101104
/// returns false, the remaining traversal is terminated and returns failure.
102105
virtual bool walkToExprPost(Expr *E) { return true; }
103106

107+
/// This method is called when first visiting a pattern, before walking
108+
/// into its children. If it returns false, the subtree is skipped.
109+
virtual bool walkToPatternPre(Pattern *P) { return true; }
110+
111+
/// This method is called after visiting the children of a pattern. If it
112+
/// returns false, the remaining traversal is terminated and returns failure.
113+
virtual bool walkToPatternPost(Pattern *P) { return true; }
114+
104115
/// This method is called when a ValueDecl is referenced in source. If it
105116
/// returns false, the remaining traversal is terminated and returns failure.
106117
///

lib/IDE/Refactoring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5474,10 +5474,10 @@ class ReferenceCollector : private SourceEntityWalker {
54745474
return shouldWalkInto(S->getSourceRange());
54755475
}
54765476

5477-
std::pair<bool, Pattern *> walkToPatternPre(Pattern *P) {
5477+
bool walkToPatternPre(Pattern *P) override {
54785478
if (P == Target.dyn_cast<Pattern *>())
54795479
AfterTarget = true;
5480-
return { shouldWalkInto(P->getSourceRange()), P };
5480+
return shouldWalkInto(P->getSourceRange());
54815481
}
54825482

54835483
bool shouldWalkInto(SourceRange Range) {

lib/IDE/SourceEntityWalker.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class SemaAnnotator : public ASTWalker {
6666
Stmt *walkToStmtPost(Stmt *S) override;
6767

6868
std::pair<bool, Pattern *> walkToPatternPre(Pattern *P) override;
69+
Pattern *walkToPatternPost(Pattern *P) override;
6970

7071
bool handleImports(ImportDecl *Import);
7172
bool handleCustomAttributes(Decl *D);
@@ -587,6 +588,9 @@ std::pair<bool, Pattern *> SemaAnnotator::walkToPatternPre(Pattern *P) {
587588
return { false, nullptr };
588589
}
589590

591+
if (!SEWalker.walkToPatternPre(P))
592+
return { false, P };
593+
590594
if (P->isImplicit())
591595
return { true, P };
592596

@@ -609,6 +613,16 @@ std::pair<bool, Pattern *> SemaAnnotator::walkToPatternPre(Pattern *P) {
609613
return { false, P };
610614
}
611615

616+
Pattern *SemaAnnotator::walkToPatternPost(Pattern *P) {
617+
if (isDone())
618+
return nullptr;
619+
620+
bool Continue = SEWalker.walkToPatternPost(P);
621+
if (!Continue)
622+
Cancelled = true;
623+
return Continue ? P : nullptr;
624+
}
625+
612626
bool SemaAnnotator::handleCustomAttributes(Decl *D) {
613627
// CustomAttrs of non-param VarDecls are handled when this method is called
614628
// on their containing PatternBindingDecls (see below).
@@ -824,6 +838,11 @@ bool SourceEntityWalker::walk(Expr *E) {
824838
return performWalk(Annotator, [&]() { return E->walk(Annotator); });
825839
}
826840

841+
bool SourceEntityWalker::walk(Pattern *P) {
842+
SemaAnnotator Annotator(*this);
843+
return performWalk(Annotator, [&]() { return P->walk(Annotator); });
844+
}
845+
827846
bool SourceEntityWalker::walk(Decl *D) {
828847
SemaAnnotator Annotator(*this);
829848
return performWalk(Annotator, [&]() { return D->walk(Annotator); });

0 commit comments

Comments
 (0)