[AST] Introduce explicit actions for ASTWalker

Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
This commit is contained in:
Hamish Knight
2022-09-13 10:35:29 +01:00
parent 6804623a82
commit 4716f61fba
68 changed files with 1827 additions and 1375 deletions

View File

@@ -1541,15 +1541,15 @@ private:
: PlaceholderLoc(PlaceholderLoc), Found(Found) {
}
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (isa<EditorPlaceholderExpr>(E) && E->getStartLoc() == PlaceholderLoc) {
Found = cast<EditorPlaceholderExpr>(E);
return { false, nullptr };
return Action::Stop();
}
return { true, E };
return Action::Continue(E);
}
bool walkToDeclPre(Decl *D) override {
PreWalkAction walkToDeclPre(Decl *D) override {
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
// The base walker assumes the content of active IfConfigDecl clauses
// has been injected into the parent context and will be walked there.
@@ -1560,9 +1560,9 @@ private:
Elem.walk(*this);
}
}
return false;
return Action::SkipChildren();
}
return true;
return Action::Continue();
}
};
@@ -1574,7 +1574,7 @@ private:
explicit ClosureTypeWalker(SourceManager &SM, ClosureInfo &Info) : SM(SM),
Info(Info) { }
bool walkToTypeReprPre(TypeRepr *T) override {
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
if (auto *FTR = dyn_cast<FunctionTypeRepr>(T)) {
FoundFunctionTypeRepr = true;
for (auto &ArgElt : FTR->getArgsTypeRepr()->getElements()) {
@@ -1595,14 +1595,8 @@ private:
Info.ReturnTypeRange = CharSourceRange(SM, RTR->getStartLoc(), SRE);
}
}
return !FoundFunctionTypeRepr;
return Action::StopIf(FoundFunctionTypeRepr);
}
bool walkToTypeReprPost(TypeRepr *T) override {
// If we just visited the FunctionTypeRepr, end traversal.
return !FoundFunctionTypeRepr;
}
};
bool containClosure(Expr *E) {