Skip to content

Commit d8f2040

Browse files
committed
[AST] Add Argument visitors to ASTWalker
Allow ASTWalkers to override the walking of individual arguments in an argument list.
1 parent a9a31c1 commit d8f2040

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace swift {
2222

23+
class Argument;
2324
class ArgumentList;
2425
class Decl;
2526
class Expr;
@@ -643,6 +644,27 @@ class ASTWalker {
643644
return Action::Continue(ArgList);
644645
}
645646

647+
/// This method is called when first visiting an argument in an argument list,
648+
/// before walking into its expression.
649+
///
650+
/// \param Arg The argument to walk.
651+
///
652+
/// \returns The walking action to perform.
653+
///
654+
/// The default implementation returns \c Action::Continue().
655+
virtual PreWalkAction walkToArgumentPre(const Argument &Arg) {
656+
return Action::Continue();
657+
}
658+
659+
/// This method is called after visiting an argument in an argument list.
660+
///
661+
/// \returns The walking action to perform.
662+
///
663+
/// The default implementation returns \c Action::Continue().
664+
virtual PostWalkAction walkToArgumentPost(const Argument &Arg) {
665+
return Action::Continue();
666+
}
667+
646668
protected:
647669
ASTWalker() = default;
648670
ASTWalker(const ASTWalker &) = default;

lib/AST/ASTWalker.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,12 +1635,26 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
16351635
return false;
16361636
}
16371637

1638+
[[nodiscard]]
1639+
bool doIt(ArgumentList *ArgList, unsigned Idx) {
1640+
auto Arg = ArgList->get(Idx);
1641+
return traverse(
1642+
Walker.walkToArgumentPre(Arg),
1643+
[&]() {
1644+
auto *E = doIt(Arg.getExpr());
1645+
if (!E)
1646+
return true;
1647+
ArgList->setExpr(Idx, E);
1648+
return false;
1649+
},
1650+
[&]() { return Walker.walkToArgumentPost(Arg); });
1651+
}
1652+
16381653
[[nodiscard]]
16391654
ArgumentList *visit(ArgumentList *ArgList) {
16401655
for (auto Idx : indices(*ArgList)) {
1641-
auto *E = doIt(ArgList->getExpr(Idx));
1642-
if (!E) return nullptr;
1643-
ArgList->setExpr(Idx, E);
1656+
if (doIt(ArgList, Idx))
1657+
return nullptr;
16441658
}
16451659
return ArgList;
16461660
}

0 commit comments

Comments
 (0)