ladybird/Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/Compiler/Passes/FunctionCallCanonicalizationPass.h
Dan Klishch b4a9fde756 JSSpecCompiler: Recurse into the correct subtrees in RecursiveASTVisitor
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.
2024-01-21 14:57:10 -07:00

28 lines
771 B
C++

/*
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include "Compiler/GenericASTPass.h"
namespace JSSpecCompiler {
// FunctionCallCanonicalizationPass simplifies ladders of BinaryOperators nodes in the function call
// arguments into nice and neat FunctionCall nodes.
//
// Ladders initially appear since I do not want to complicate expression parser, so it interprets
// `f(a, b, c, d)` as `f "function_call_operator" (a, (b, (c, d))))`.
class FunctionCallCanonicalizationPass : public GenericASTPass {
public:
inline static constexpr StringView name = "function-call-canonicalization"sv;
using GenericASTPass::GenericASTPass;
protected:
void on_leave(Tree tree) override;
};
}