diff --git a/lib/astutils.cpp b/lib/astutils.cpp index bf2eb1817..cfd3c3947 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -40,53 +40,10 @@ #include #include #include -#include #include #include #include -template )> -void visitAstNodesGeneric(T *ast, std::function visitor) -{ - if (!ast) - return; - - std::stack> tokens; - T *tok = ast; - do { - ChildrenToVisit c = visitor(tok); - - if (c == ChildrenToVisit::done) - break; - if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) { - T *t2 = tok->astOperand2(); - if (t2) - tokens.push(t2); - } - if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) { - T *t1 = tok->astOperand1(); - if (t1) - tokens.push(t1); - } - - if (tokens.empty()) - break; - - tok = tokens.top(); - tokens.pop(); - } while (true); -} - -void visitAstNodes(const Token *ast, std::function visitor) -{ - visitAstNodesGeneric(ast, std::move(visitor)); -} - -void visitAstNodes(Token *ast, std::function visitor) -{ - visitAstNodesGeneric(ast, std::move(visitor)); -} - const Token* findAstNode(const Token* ast, const std::function& pred) { const Token* result = nullptr; diff --git a/lib/astutils.h b/lib/astutils.h index 8dcc85310..0c7515aa5 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -47,8 +48,37 @@ enum class ChildrenToVisit { /** * Visit AST nodes recursively. The order is not "well defined" */ -void visitAstNodes(const Token *ast, std::function visitor); -void visitAstNodes(Token *ast, std::function visitor); +template )> +void visitAstNodes(T *ast, const TFunc &visitor) +{ + if (!ast) + return; + + std::stack> tokens; + T *tok = ast; + do { + ChildrenToVisit c = visitor(tok); + + if (c == ChildrenToVisit::done) + break; + if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) { + T *t2 = tok->astOperand2(); + if (t2) + tokens.push(t2); + } + if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) { + T *t1 = tok->astOperand1(); + if (t1) + tokens.push(t1); + } + + if (tokens.empty()) + break; + + tok = tokens.top(); + tokens.pop(); + } while (true); +} const Token* findAstNode(const Token* ast, const std::function& pred); const Token* findExpression(const nonneg int exprid,