Skip to content

Commit fdb9f22

Browse files
committed
[NFC] Sema: Reimplement ReturnStmtFinder as a function with a callback
A callback enables the caller to control whether to abort the walk. This is handy for `BraceHasExplicitReturnStmtRequest`.
1 parent 6d1ffa1 commit fdb9f22

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,45 +1222,61 @@ ConstraintSystem::matchResultBuilder(AnyFunctionRef fn, Type builderType,
12221222
return getTypeMatchSuccess();
12231223
}
12241224

1225-
namespace {
1226-
class ReturnStmtFinder : public ASTWalker {
1227-
std::vector<ReturnStmt *> ReturnStmts;
1225+
/// Walks the given brace statement and calls the given function reference on
1226+
/// every occurrence of an explicit `return` statement.
1227+
///
1228+
/// \param callback A function reference that takes a `return` statement and
1229+
/// returns a boolean value indicating whether to abort the walk.
1230+
///
1231+
/// \returns `true` if the walk was aborted, `false` otherwise.
1232+
static bool walkExplicitReturnStmts(const BraceStmt *BS,
1233+
function_ref<bool(ReturnStmt *)> callback) {
1234+
class Walker : public ASTWalker {
1235+
function_ref<bool(ReturnStmt *)> callback;
1236+
1237+
public:
1238+
Walker(decltype(Walker::callback) callback) : callback(callback) {}
1239+
1240+
MacroWalking getMacroWalkingBehavior() const override {
1241+
return MacroWalking::Arguments;
1242+
}
12281243

1229-
public:
1230-
static std::vector<ReturnStmt *> find(const BraceStmt *BS) {
1231-
ReturnStmtFinder finder;
1232-
const_cast<BraceStmt *>(BS)->walk(finder);
1233-
return std::move(finder.ReturnStmts);
1234-
}
1244+
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
1245+
return Action::SkipNode(E);
1246+
}
12351247

1236-
MacroWalking getMacroWalkingBehavior() const override {
1237-
return MacroWalking::Arguments;
1238-
}
1248+
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
1249+
if (S->isImplicit()) {
1250+
return Action::SkipNode(S);
1251+
}
12391252

1240-
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
1241-
return Action::SkipNode(E);
1242-
}
1253+
auto *returnStmt = dyn_cast<ReturnStmt>(S);
1254+
if (!returnStmt) {
1255+
return Action::Continue(S);
1256+
}
12431257

1244-
PreWalkResult<Stmt *> walkToStmtPre(Stmt *S) override {
1245-
// If we see a return statement, note it..
1246-
auto *returnStmt = dyn_cast<ReturnStmt>(S);
1247-
if (!returnStmt || returnStmt->isImplicit())
1248-
return Action::Continue(S);
1258+
if (callback(returnStmt)) {
1259+
return Action::Stop();
1260+
}
12491261

1250-
ReturnStmts.push_back(returnStmt);
1251-
return Action::SkipNode(S);
1252-
}
1262+
// Skip children & post walk and continue.
1263+
return Action::SkipNode(S);
1264+
}
12531265

1254-
/// Ignore patterns.
1255-
PreWalkResult<Pattern *> walkToPatternPre(Pattern *pat) override {
1256-
return Action::SkipNode(pat);
1257-
}
1258-
};
1259-
} // end anonymous namespace
1266+
/// Ignore patterns.
1267+
PreWalkResult<Pattern *> walkToPatternPre(Pattern *pat) override {
1268+
return Action::SkipNode(pat);
1269+
}
1270+
};
1271+
1272+
Walker walker(callback);
1273+
1274+
return const_cast<BraceStmt *>(BS)->walk(walker) == nullptr;
1275+
}
12601276

12611277
bool BraceHasExplicitReturnStmtRequest::evaluate(Evaluator &evaluator,
12621278
const BraceStmt *BS) const {
1263-
return !ReturnStmtFinder::find(BS).empty();
1279+
return walkExplicitReturnStmts(BS, [](ReturnStmt *) { return true; });
12641280
}
12651281

12661282
std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
@@ -1269,7 +1285,14 @@ std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
12691285
return std::vector<ReturnStmt *>();
12701286
}
12711287

1272-
return ReturnStmtFinder::find(fn.getBody());
1288+
std::vector<ReturnStmt *> results;
1289+
1290+
walkExplicitReturnStmts(fn.getBody(), [&results](ReturnStmt *RS) {
1291+
results.push_back(RS);
1292+
return false;
1293+
});
1294+
1295+
return results;
12731296
}
12741297

12751298
ResultBuilderOpSupport TypeChecker::checkBuilderOpSupport(

0 commit comments

Comments
 (0)