User Guide
function_callpath_visitor.cpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2024 Blue Brain Project, EPFL.
4  * See the top-level LICENSE file for details.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
10 
11 namespace nmodl {
12 namespace visitor {
13 
14 using symtab::Symbol;
16 
18  if (visited_functions_or_procedures.empty()) {
19  return;
20  }
21  /// If node is either a RANGE var, a POINTER or a BBCOREPOINTER then
22  /// the FUNCTION or PROCEDURE it's used in should have the `use_range_ptr_var`
23  /// property
24  auto sym = psymtab->lookup(node.get_node_name());
25  const auto properties = NmodlType::range_var | NmodlType::pointer_var |
26  NmodlType::bbcore_pointer_var;
27  if (sym && sym->has_any_property(properties)) {
28  const auto top = visited_functions_or_procedures.back();
29  const auto caller_func_name =
30  top->is_function_block()
31  ? dynamic_cast<const ast::FunctionBlock*>(top)->get_node_name()
32  : dynamic_cast<const ast::ProcedureBlock*>(top)->get_node_name();
33  auto caller_func_proc_sym = psymtab->lookup(caller_func_name);
34  caller_func_proc_sym->add_properties(NmodlType::use_range_ptr_var);
35  }
36 }
37 
39  if (visited_functions_or_procedures.empty()) {
40  return;
41  }
42  const auto name = node.get_node_name();
43  const auto func_symbol = psymtab->lookup(name);
44  if (!func_symbol ||
45  !func_symbol->has_any_property(NmodlType::function_block | NmodlType::procedure_block) ||
46  func_symbol->get_nodes().empty()) {
47  return;
48  }
49  /// Visit the called FUNCTION/PROCEDURE AST node to check whether
50  /// it has `use_range_ptr_var` property. If it does the currently called
51  /// function needs to have it too.
52  const auto func_block = func_symbol->get_nodes()[0];
53  func_block->accept(*this);
54  if (func_symbol->has_any_property(NmodlType::use_range_ptr_var)) {
55  const auto top = visited_functions_or_procedures.back();
56  auto caller_func_name =
57  top->is_function_block()
58  ? dynamic_cast<const ast::FunctionBlock*>(top)->get_node_name()
59  : dynamic_cast<const ast::ProcedureBlock*>(top)->get_node_name();
60  auto caller_func_proc_sym = psymtab->lookup(caller_func_name);
61  caller_func_proc_sym->add_properties(NmodlType::use_range_ptr_var);
62  }
63 }
64 
66  /// Avoid recursive calls
67  if (std::find(visited_functions_or_procedures.begin(),
69  &node) != visited_functions_or_procedures.end()) {
70  return;
71  }
72  visited_functions_or_procedures.push_back(&node);
73  node.visit_children(*this);
75 }
76 
78  // Avoid recursive calls
79  if (std::find(visited_functions_or_procedures.begin(),
81  &node) != visited_functions_or_procedures.end()) {
82  return;
83  }
84  visited_functions_or_procedures.push_back(&node);
85  node.visit_children(*this);
87 }
88 
90  psymtab = node.get_symbol_table();
91  node.visit_children(*this);
92 }
93 
94 } // namespace visitor
95 } // namespace nmodl
nmodl::ast::FunctionCall::get_node_name
std::string get_node_name() const override
Return name of the node.
Definition: ast.cpp:7065
nmodl::ast::FunctionBlock
TODO.
Definition: function_block.hpp:39
nmodl::visitor::FunctionCallpathVisitor::visit_function_block
void visit_function_block(const ast::FunctionBlock &node) override
visit node of type ast::FunctionBlock
Definition: function_callpath_visitor.cpp:77
nmodl::visitor::FunctionCallpathVisitor::visit_program
void visit_program(const ast::Program &node) override
visit node of type ast::Program
Definition: function_callpath_visitor.cpp:89
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::ast::VarName
Represents a variable.
Definition: var_name.hpp:43
nmodl::visitor::FunctionCallpathVisitor::visited_functions_or_procedures
std::vector< const ast::Block * > visited_functions_or_procedures
Vector of currently visited functions or procedures (used as a searchable stack)
Definition: function_callpath_visitor.hpp:45
nmodl::ast::FunctionBlock::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:3980
nmodl::ast::ProcedureBlock::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:4177
nmodl::ast::FunctionCall
TODO.
Definition: function_call.hpp:38
nmodl::ast::Program::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:12906
nmodl::visitor::FunctionCallpathVisitor::psymtab
symtab::SymbolTable * psymtab
symbol table for the program
Definition: function_callpath_visitor.hpp:48
nmodl::symtab::syminfo::NmodlType
NmodlType
NMODL variable properties.
Definition: symbol_properties.hpp:116
function_callpath_visitor.hpp
Visitor for traversing FunctionBlock s and ProcedureBlocks through their FunctionCall s
nmodl::ast::ProcedureBlock
TODO.
Definition: procedure_block.hpp:39
nmodl::visitor::FunctionCallpathVisitor::visit_var_name
void visit_var_name(const ast::VarName &node) override
visit node of type ast::VarName
Definition: function_callpath_visitor.cpp:17
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::FunctionCallpathVisitor::visit_function_call
void visit_function_call(const ast::FunctionCall &node) override
visit node of type ast::FunctionCall
Definition: function_callpath_visitor.cpp:38
nmodl::ast::Program
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
nmodl::symtab::SymbolTable::lookup
std::shared_ptr< Symbol > lookup(const std::string &name) const
check if symbol with given name exist in the current table (but not in parents)
Definition: symbol_table.hpp:199
nmodl::visitor::FunctionCallpathVisitor::visit_procedure_block
void visit_procedure_block(const ast::ProcedureBlock &node) override
visit node of type ast::ProcedureBlock
Definition: function_callpath_visitor.cpp:65
nmodl::ast::VarName::get_node_name
std::string get_node_name() const override
Return name of the node.
Definition: ast.cpp:1124