User Guide
global_var_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/global.hpp"
13 #include "ast/neuron_block.hpp"
14 #include "ast/program.hpp"
15 #include "ast/range.hpp"
16 #include "ast/statement_block.hpp"
19 
20 namespace nmodl {
21 namespace visitor {
22 
24  ast::RangeVarVector range_variables;
25  std::unordered_set<ast::GlobalVar*> global_variables_to_remove;
26  std::unordered_set<ast::Statement*> global_statements_to_remove;
27 
28  auto const& statement_block = node.get_statement_block();
29  auto& statements = (*statement_block).get_statements();
30  const auto& symbol_table = ast.get_symbol_table();
31 
32  for (auto& statement: statements) {
33  /// only process global statements
34  if (statement->is_global()) {
35  const auto& global_variables =
36  std::static_pointer_cast<ast::Global>(statement)->get_variables();
37  global_variables_to_remove.clear();
38  for (auto& global_variable: global_variables) {
39  auto variable_name = global_variable->get_node_name();
40  /// check if global variable is being updated in the mod file
41  if (symbol_table->lookup(variable_name)->get_write_count() > 0) {
42  range_variables.emplace_back(new ast::RangeVar(global_variable->get_name()));
43  global_variables_to_remove.emplace(global_variable.get());
44  }
45  }
46 
47  /// remove offending global variables
48  std::static_pointer_cast<ast::Global>(statement)->erase_global_var(
49  global_variables_to_remove);
50 
51  /// add empty global statements to global_statements_to_remove
52  if (global_variables.empty()) {
53  global_statements_to_remove.emplace(statement.get());
54  }
55  }
56  }
57 
58  /// remove offending global statements if empty
59  statement_block->erase_statement(global_statements_to_remove);
60 
61  /// insert new range variables replacing global ones
62  if (!range_variables.empty()) {
63  auto range_statement = new ast::Range(range_variables);
64  statement_block->emplace_back_statement(range_statement);
65  }
66 }
67 
68 } // namespace visitor
69 } // namespace nmodl
global.hpp
Auto generated AST classes declaration.
nmodl::ast::Range
Represents RANGE variables statement in NMODL.
Definition: range.hpp:39
nmodl::ast::RangeVarVector
std::vector< std::shared_ptr< RangeVar > > RangeVarVector
Definition: ast_decl.hpp:318
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
global_var_visitor.hpp
Visitor to convert GLOBAL variables to RANGE variables.
nmodl::ast::RangeVar
TODO.
Definition: range_var.hpp:38
nmodl::visitor::GlobalToRangeVisitor::visit_neuron_block
void visit_neuron_block(ast::NeuronBlock &node) override
Visit ast::NeuronBlock nodes to check if there is any GLOBAL variables defined in them that are writt...
Definition: global_var_visitor.cpp:23
nmodl::symtab::SymbolTable::get_variables
std::vector< std::shared_ptr< Symbol > > get_variables(syminfo::NmodlType with=syminfo::NmodlType::empty, syminfo::NmodlType without=syminfo::NmodlType::empty) const
get variables
Definition: symbol_table.cpp:107
visitor_utils.hpp
Utility functions for visitors implementation.
program.hpp
Auto generated AST classes declaration.
statement_block.hpp
Auto generated AST classes declaration.
range.hpp
Auto generated AST classes declaration.
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::visitor::GlobalToRangeVisitor::ast
const ast::Program & ast
ast::Ast* node
Definition: global_var_visitor.hpp:64
nmodl::ast::NeuronBlock
Represent NEURON block in the mod file.
Definition: neuron_block.hpp:53
nmodl::ast::NeuronBlock::get_statement_block
std::shared_ptr< StatementBlock > get_statement_block() const noexcept override
Getter for member variable NeuronBlock::statement_block.
Definition: neuron_block.hpp:188
neuron_block.hpp
Auto generated AST classes declaration.