Skip to content

Commit 75b6e8d

Browse files
authored
inline visitAstNodes() calls (#3828)
1 parent d5ef25e commit 75b6e8d

2 files changed

Lines changed: 32 additions & 45 deletions

File tree

lib/astutils.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,10 @@
4040
#include <map>
4141
#include <memory>
4242
#include <set>
43-
#include <stack>
4443
#include <type_traits>
4544
#include <unordered_map>
4645
#include <utility>
4746

48-
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
49-
void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
50-
{
51-
if (!ast)
52-
return;
53-
54-
std::stack<T *, std::vector<T *>> tokens;
55-
T *tok = ast;
56-
do {
57-
ChildrenToVisit c = visitor(tok);
58-
59-
if (c == ChildrenToVisit::done)
60-
break;
61-
if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) {
62-
T *t2 = tok->astOperand2();
63-
if (t2)
64-
tokens.push(t2);
65-
}
66-
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) {
67-
T *t1 = tok->astOperand1();
68-
if (t1)
69-
tokens.push(t1);
70-
}
71-
72-
if (tokens.empty())
73-
break;
74-
75-
tok = tokens.top();
76-
tokens.pop();
77-
} while (true);
78-
}
79-
80-
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor)
81-
{
82-
visitAstNodesGeneric(ast, std::move(visitor));
83-
}
84-
85-
void visitAstNodes(Token *ast, std::function<ChildrenToVisit(Token *)> visitor)
86-
{
87-
visitAstNodesGeneric(ast, std::move(visitor));
88-
}
89-
9047
const Token* findAstNode(const Token* ast, const std::function<bool(const Token*)>& pred)
9148
{
9249
const Token* result = nullptr;

lib/astutils.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <functional>
2626
#include <set>
27+
#include <stack>
2728
#include <string>
2829
#include <vector>
2930

@@ -47,8 +48,37 @@ enum class ChildrenToVisit {
4748
/**
4849
* Visit AST nodes recursively. The order is not "well defined"
4950
*/
50-
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor);
51-
void visitAstNodes(Token *ast, std::function<ChildrenToVisit(Token *)> visitor);
51+
template<class T, class TFunc, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
52+
void visitAstNodes(T *ast, const TFunc &visitor)
53+
{
54+
if (!ast)
55+
return;
56+
57+
std::stack<T *, std::vector<T *>> tokens;
58+
T *tok = ast;
59+
do {
60+
ChildrenToVisit c = visitor(tok);
61+
62+
if (c == ChildrenToVisit::done)
63+
break;
64+
if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) {
65+
T *t2 = tok->astOperand2();
66+
if (t2)
67+
tokens.push(t2);
68+
}
69+
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) {
70+
T *t1 = tok->astOperand1();
71+
if (t1)
72+
tokens.push(t1);
73+
}
74+
75+
if (tokens.empty())
76+
break;
77+
78+
tok = tokens.top();
79+
tokens.pop();
80+
} while (true);
81+
}
5282

5383
const Token* findAstNode(const Token* ast, const std::function<bool(const Token*)>& pred);
5484
const Token* findExpression(const nonneg int exprid,

0 commit comments

Comments
 (0)