astutils.cpp: optimized visitAstNodesGeneric() a bit (#3716)

This commit is contained in:
Oliver Stöneberg 2022-01-17 20:34:35 +01:00 committed by GitHub
parent 605fd7cf98
commit 2148b8b165
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 5 deletions

View File

@ -42,7 +42,7 @@
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> 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) void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
{ {
std::stack<T *> tokens; std::stack<T *, std::vector<T *>> tokens;
tokens.push(ast); tokens.push(ast);
while (!tokens.empty()) { while (!tokens.empty()) {
T *tok = tokens.top(); T *tok = tokens.top();
@ -54,10 +54,16 @@ void visitAstNodesGeneric(T *ast, std::function<ChildrenToVisit(T *)> visitor)
if (c == ChildrenToVisit::done) if (c == ChildrenToVisit::done)
break; break;
if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2) {
tokens.push(tok->astOperand2()); T *t2 = tok->astOperand2();
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) if (t2)
tokens.push(tok->astOperand1()); tokens.push(t2);
}
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2) {
T *t1 = tok->astOperand1();
if (t1)
tokens.push(t1);
}
} }
} }