
RecursiveASTVisitor was recursing into the subtrees of an old root if it was changed in on_entry callback. Fix that by querying root pointer just after on_entry callback returns. While on it, also use `AK::TemporaryChange` instead of setting `m_current_subtree_pointer` manually. As it turns out, `FunctionCallCanonicalizationPass` was relying on being able to replace tree on entry, and the bug in RecursiveASTVisitor made the pass to not fully canonicalize nested function calls. The changes to GenericASTPass.cpp alone are enough to fix the problem but it is canonical (for some definition of canonicity) to only change trees in on_leave. Therefore, the commit also switches FunctionCallCanonicalizationPass to on_leave callback. A test for this fix and one from the previous commit is also included.
38 lines
1.2 KiB
C++
38 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "Compiler/Passes/FunctionCallCanonicalizationPass.h"
|
|
#include "AST/AST.h"
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
void FunctionCallCanonicalizationPass::on_leave(Tree tree)
|
|
{
|
|
if (auto binary_operation = as<BinaryOperation>(tree)) {
|
|
if (binary_operation->m_operation == BinaryOperator::FunctionCall) {
|
|
Vector<Tree> arguments;
|
|
|
|
auto current_tree = binary_operation->m_right;
|
|
while (true) {
|
|
auto argument_tree = as<BinaryOperation>(current_tree);
|
|
if (!argument_tree || argument_tree->m_operation != BinaryOperator::Comma)
|
|
break;
|
|
arguments.append(argument_tree->m_left);
|
|
current_tree = argument_tree->m_right;
|
|
}
|
|
arguments.append(current_tree);
|
|
|
|
if (arguments[0] == zero_argument_function_call) {
|
|
VERIFY(arguments.size() == 1);
|
|
arguments.clear();
|
|
}
|
|
|
|
replace_current_node_with(make_ref_counted<FunctionCall>(binary_operation->m_left, move(arguments)));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|