NEURON
initial_block_visitor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <unordered_set>
9 
12 
13 #include "ast/all.hpp"
14 
15 namespace nmodl {
16 namespace visitor {
17 
19  // check if there is > 1 INITIAL block
21  return;
22  }
23 
24  // collect all blocks in the program
25  const auto& blocks = node.get_blocks();
26 
27  // collect all statements from top-level INITIAL blocks, and the blocks themselves
28  ast::StatementVector statements;
29  std::unordered_set<ast::Node*> blocks_to_delete;
30  for (auto& block: blocks) {
31  if (block->is_initial_block()) {
32  auto statement_block =
33  std::static_pointer_cast<ast::InitialBlock>(block)->get_statement_block();
34  // if block is not empty, copy statements into vector
35  if (statement_block) {
36  for (const auto& statement: statement_block->get_statements()) {
37  statements.push_back(statement);
38  }
39  }
40  blocks_to_delete.insert(block.get());
41  }
42  }
43 
44  // insert new top-level INITIAL block which has the above statements
45  auto new_block = ast::StatementBlock(statements);
46  auto new_initial_block = ast::InitialBlock(new_block.clone());
47  node.emplace_back_node(new_initial_block.clone());
48 
49  // delete all of the previously-found top-level INITIAL blocks
50  node.erase_node(blocks_to_delete);
51 }
52 } // namespace visitor
53 } // namespace nmodl
Auto generated AST classes declaration.
Represents a INITIAL block in the NMODL.
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
Represents block encapsulating list of statements.
void visit_program(ast::Program &node) override
visit node of type ast::Program
@ INITIAL_BLOCK
type of ast::InitialBlock
std::vector< std::shared_ptr< Statement > > StatementVector
Definition: ast_decl.hpp:302
Visitor which merges all INITIAL blocks into one.
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
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 *)
Definition: netcvode.cpp:291
Utility functions for visitors implementation.