User Guide
local_to_assigned_visitor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <iostream>
9 #include <memory>
10 #include <unordered_set>
11 
12 #include "ast/assigned_block.hpp"
14 #include "ast/program.hpp"
17 
18 namespace nmodl {
19 namespace visitor {
20 
22  ast::AssignedDefinitionVector assigned_variables;
23  std::unordered_set<ast::LocalVar*> local_variables_to_remove;
24  std::unordered_set<ast::Node*> local_nodes_to_remove;
25  std::shared_ptr<ast::AssignedBlock> assigned_block;
26 
27  const auto& top_level_nodes = node.get_blocks();
28  const auto& symbol_table = node.get_symbol_table();
29 
30  for (auto& top_level_node: top_level_nodes) {
31  /// save pointer to assigned block to add new variables (only one block)
32  if (top_level_node->is_assigned_block()) {
33  assigned_block = std::static_pointer_cast<ast::AssignedBlock>(top_level_node);
34  }
35 
36  /// only process local_list statements otherwise continue
37  if (!top_level_node->is_local_list_statement()) {
38  continue;
39  }
40 
41  const auto& local_list_statement = std::static_pointer_cast<ast::LocalListStatement>(
42  top_level_node);
43 
44  for (const auto& local_variable: local_list_statement->get_variables()) {
45  const auto& variable_name = local_variable->get_node_name();
46  /// check if local variable is being updated in the mod file
47  if (symbol_table->lookup(variable_name)->get_write_count() > 0) {
48  assigned_variables.emplace_back(
49  std::make_shared<ast::AssignedDefinition>(local_variable->get_name(),
50  nullptr,
51  nullptr,
52  nullptr,
53  nullptr,
54  nullptr,
55  nullptr));
56  local_variables_to_remove.insert(local_variable.get());
57  }
58  }
59 
60  /// remove local variables being converted to assigned
61  local_list_statement->erase_local_var(local_variables_to_remove);
62 
63  /// if all local variables are converted to assigned, keep track
64  /// for removal
65  if (local_list_statement->get_variables().empty()) {
66  local_nodes_to_remove.insert(top_level_node.get());
67  }
68  }
69 
70  /// remove empty local statements if empty
71  node.erase_node(local_nodes_to_remove);
72 
73  /// if no assigned block found add one to the node otherwise emplace back new assigned variables
74  if (assigned_block == nullptr) {
75  assigned_block = std::make_shared<ast::AssignedBlock>(assigned_variables);
76  node.emplace_back_node(std::static_pointer_cast<ast::Node>(assigned_block));
77  } else {
78  for (const auto& assigned_variable: assigned_variables) {
79  assigned_block->emplace_back_assigned_definition(assigned_variable);
80  }
81  }
82 }
83 
84 } // namespace visitor
85 } // namespace nmodl
local_list_statement.hpp
Auto generated AST classes declaration.
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ast::Program::erase_node
NodeVector::const_iterator erase_node(NodeVector::const_iterator first)
Erase member to blocks.
Definition: ast.cpp:12836
local_to_assigned_visitor.hpp
Visitor to convert top level LOCAL variables to ASSIGNED variables.
nmodl::visitor::LocalToAssignedVisitor::visit_program
void visit_program(ast::Program &node) override
Visit ast::Program node to transform top level LOCAL variables to ASSIGNED if they are written in the...
Definition: local_to_assigned_visitor.cpp:21
assigned_block.hpp
Auto generated AST classes declaration.
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
nmodl::ast::AssignedDefinitionVector
std::vector< std::shared_ptr< AssignedDefinition > > AssignedDefinitionVector
Definition: ast_decl.hpp:377
nmodl::ast::Program::get_blocks
const NodeVector & get_blocks() const noexcept
Getter for member variable Program::blocks.
Definition: program.hpp:216
nmodl::ast::Program::get_symbol_table
symtab::SymbolTable * get_symbol_table() const override
Return associated symbol table for the current ast node.
Definition: program.hpp:153
nmodl::ast::Program
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
nmodl::ast::Program::emplace_back_node
void emplace_back_node(Node *n)
Add member to blocks by raw pointer.
Definition: ast.cpp:12817