inline visitAstNodes() calls (#3828)

This commit is contained in:
Oliver Stöneberg 2022-02-13 20:46:01 +01:00 committed by GitHub
parent d5ef25e477
commit 75b6e8d258
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 45 deletions

View File

@ -40,53 +40,10 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <set> #include <set>
#include <stack>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
#include <utility> #include <utility>
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
{
if (!ast)
return;
std::stack<T *, std::vector<T *>> 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<ChildrenToVisit(const Token *)> visitor)
{
visitAstNodesGeneric(ast, std::move(visitor));
}
void visitAstNodes(Token *ast, std::function<ChildrenToVisit(Token *)> visitor)
{
visitAstNodesGeneric(ast, std::move(visitor));
}
const Token* findAstNode(const Token* ast, const std::function<bool(const Token*)>& pred) const Token* findAstNode(const Token* ast, const std::function<bool(const Token*)>& pred)
{ {
const Token* result = nullptr; const Token* result = nullptr;

View File

@ -24,6 +24,7 @@
#include <functional> #include <functional>
#include <set> #include <set>
#include <stack>
#include <string> #include <string>
#include <vector> #include <vector>
@ -47,8 +48,37 @@ enum class ChildrenToVisit {
/** /**
* Visit AST nodes recursively. The order is not "well defined" * Visit AST nodes recursively. The order is not "well defined"
*/ */
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor); template<class T, class TFunc, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
void visitAstNodes(Token *ast, std::function<ChildrenToVisit(Token *)> visitor); void visitAstNodes(T *ast, const TFunc &visitor)
{
if (!ast)
return;
std::stack<T *, std::vector<T *>> 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<bool(const Token*)>& pred); const Token* findAstNode(const Token* ast, const std::function<bool(const Token*)>& pred);
const Token* findExpression(const nonneg int exprid, const Token* findExpression(const nonneg int exprid,