From b493a93c7c0f2ab4e763e0f4a761cb63e925814c Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 13 Feb 2022 19:57:44 +0100 Subject: [PATCH 1/2] astutils.cpp: adjusted visitAstNodesGeneric() signature --- lib/astutils.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index bf2eb1817d4..aabee0c468b 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -45,8 +45,8 @@ #include #include -template )> -void visitAstNodesGeneric(T *ast, std::function visitor) +template )> +void visitAstNodesGeneric(T *ast, const TFunc &visitor) { if (!ast) return; @@ -79,12 +79,12 @@ void visitAstNodesGeneric(T *ast, std::function visitor) void visitAstNodes(const Token *ast, std::function visitor) { - visitAstNodesGeneric(ast, std::move(visitor)); + visitAstNodesGeneric(ast, visitor); } void visitAstNodes(Token *ast, std::function visitor) { - visitAstNodesGeneric(ast, std::move(visitor)); + visitAstNodesGeneric(ast, visitor); } const Token* findAstNode(const Token* ast, const std::function& pred) From a5dcfbcaef28b6c1bd4a00289ba1091976628d1e Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 13 Feb 2022 19:59:13 +0100 Subject: [PATCH 2/2] moved visitAstNodesGeneric() to header and got rid of trampoline functions --- lib/astutils.cpp | 43 ------------------------------------------- lib/astutils.h | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 45 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index aabee0c468b..cfd3c3947e2 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, 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); -} - -void visitAstNodes(const Token *ast, std::function visitor) -{ - visitAstNodesGeneric(ast, visitor); -} - -void visitAstNodes(Token *ast, std::function visitor) -{ - visitAstNodesGeneric(ast, 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 8dcc8531087..0c7515aa5b6 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,