41 typename = std::enable_if_t<std::is_base_of_v<ast::Block, ast_class> &&
42 !std::is_abstract_v<ast_class>>>
55 std::unordered_set<ast::Node*> blocks_to_delete;
60 std::unordered_map<std::shared_ptr<ast::Include>,
ast::NodeVector> include_blocks_to_keep;
62 const auto& blocks =
node.get_blocks();
66 include_blocks_to_keep);
70 auto toplevel_block =
ast_class(statement_block.clone());
71 node.emplace_back_node(toplevel_block.clone());
74 node.erase_node(blocks_to_delete);
77 for (
const auto& [include_block, blocks_to_keep]: include_blocks_to_keep) {
78 include_block->set_blocks(blocks_to_keep);
86 for (
const auto& block:
node.get_blocks()) {
88 if (std::dynamic_pointer_cast<ast_class>(block) ==
nullptr) {
99 std::unordered_set<ast::Node*>& blocks_to_delete,
101 include_blocks_to_keep) {
102 for (
auto& block: blocks) {
103 auto include_block = std::dynamic_pointer_cast<ast::Include>(block);
106 const auto& included_blocks = include_block->get_blocks();
111 include_blocks_to_keep);
113 auto temp_block = std::dynamic_pointer_cast<ast_class>(block);
116 auto statement_block = temp_block->get_statement_block();
118 if (statement_block && !statement_block->get_statements().empty()) {
119 statements.push_back(
120 std::make_shared<ast::ExpressionStatement>(statement_block));
122 blocks_to_delete.insert(block.get());
Auto generated AST classes declaration.
Concrete visitor for all AST classes.
Represents an INCLUDE statement in NMODL.
Represents top level AST node for whole NMODL input.
Represents block encapsulating list of statements.
Concrete visitor for all AST classes.
Visitor which merges given top-level blocks into one.
ast::NodeVector collect_include_except(const ast::Include &node) const
MergeTopLevelBlocksVisitor()=default
void collect_statements_from_vector(const ast::NodeVector &blocks, ast::StatementVector &statements, std::unordered_set< ast::Node * > &blocks_to_delete, std::unordered_map< std::shared_ptr< ast::Include >, ast::NodeVector > &include_blocks_to_keep)
void visit_program(ast::Program &node) override
visit node of type ast::Program
AstNodeType
Enum type for every AST node type.
std::vector< std::shared_ptr< Statement > > StatementVector
std::vector< std::shared_ptr< Node > > NodeVector
constexpr const char * ast_class()
encapsulates code generation backend implementations
std::vector< std::shared_ptr< const ast::Ast > > collect_nodes(const ast::Ast &node, const std::vector< ast::AstNodeType > &types)
traverse node recursively and collect nodes of given types
static Node * node(Object *)
Utility functions for visitors implementation.