2023-08-18 13:12:20 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Dan Klishch <danilklishch@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Compiler/GenericASTPass.h"
|
|
|
|
|
#include "AST/AST.h"
|
|
|
|
|
#include "Function.h"
|
|
|
|
|
|
|
|
|
|
namespace JSSpecCompiler {
|
|
|
|
|
|
2023-08-18 16:23:29 -04:00
|
|
|
void RecursiveASTVisitor::run_in_const_subtree(NullableTree nullable_tree)
|
|
|
|
|
{
|
|
|
|
|
if (nullable_tree) {
|
|
|
|
|
auto tree = nullable_tree.release_nonnull();
|
|
|
|
|
auto tree_copy = tree;
|
|
|
|
|
NodeSubtreePointer pointer { &tree };
|
|
|
|
|
recurse(tree, pointer);
|
|
|
|
|
VERIFY(tree == tree_copy);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 13:12:20 -04:00
|
|
|
void RecursiveASTVisitor::run_in_subtree(Tree& tree)
|
|
|
|
|
{
|
|
|
|
|
NodeSubtreePointer pointer { &tree };
|
|
|
|
|
recurse(tree, pointer);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 16:23:29 -04:00
|
|
|
void RecursiveASTVisitor::replace_current_node_with(NullableTree tree)
|
2023-08-18 13:12:20 -04:00
|
|
|
{
|
2023-09-02 14:33:35 -04:00
|
|
|
m_current_subtree_pointer->replace_subtree({}, move(tree));
|
2023-08-18 13:12:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RecursionDecision RecursiveASTVisitor::recurse(Tree root, NodeSubtreePointer& pointer)
|
|
|
|
|
{
|
|
|
|
|
RecursionDecision decision;
|
|
|
|
|
|
|
|
|
|
m_current_subtree_pointer = &pointer;
|
|
|
|
|
decision = on_entry(root);
|
|
|
|
|
|
|
|
|
|
if (decision == RecursionDecision::Recurse) {
|
|
|
|
|
for (auto& child : root->subtrees()) {
|
2023-09-02 14:33:35 -04:00
|
|
|
if (recurse(child.get({}), child) == RecursionDecision::Break)
|
2023-08-18 13:12:20 -04:00
|
|
|
return RecursionDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_current_subtree_pointer = &pointer;
|
|
|
|
|
on_leave(root);
|
|
|
|
|
|
|
|
|
|
return RecursionDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 19:43:19 -04:00
|
|
|
void GenericASTPass::process_function()
|
2023-08-18 13:12:20 -04:00
|
|
|
{
|
|
|
|
|
run_in_subtree(m_function->m_ast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|