User Guide
local_var_rename_visitor.hpp
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 #pragma once
9 
10 /**
11  * \file
12  * \brief \copybrief nmodl::visitor::LocalVarRenameVisitor
13  */
14 
15 #include <map>
16 #include <stack>
17 
18 #include "symtab/decl.hpp"
19 #include "visitors/ast_visitor.hpp"
20 
21 namespace nmodl {
22 namespace visitor {
23 
24 /**
25  * \addtogroup visitor_classes
26  * \{
27  */
28 
29 /**
30  * \class LocalVarRenameVisitor
31  * \brief %Visitor to rename local variables conflicting with global scope
32  *
33  * Motivation: During inlining we have to do data-flow-analysis. Consider
34  * below example:
35  *
36  * \code{.mod}
37  * NEURON {
38  * RANGE tau, beta
39  * }
40  *
41  * DERIVATIVE states() {
42  * LOCAL tau
43  * ...
44  * rates()
45  * }
46  *
47  * PROCEDURE rates() {
48  * tau = beta * 0.12 * some_var
49  * }
50  * \endcode
51  *
52  * When rates() will be inlined into states(), local definition of tau will
53  * conflict with range variable tau. Hence we can't just copy the statements.
54  * Dataflow analysis could be done at the time of inlining. Other way is to run
55  * this pass before inlining and pre-rename any local-global variable conflicts.
56  * As we are renaming local variables only, it's safe and there are no side effects.
57  *
58  * \todo
59  * - Currently we are renaming variables even if there is no inlining candidates.
60  * In this case ideally we should not rename.
61  */
63  private:
64  /// non-null symbol table in the scope hierarchy
65  const symtab::SymbolTable* symtab = nullptr;
66 
67  /// symbol tables in case of nested blocks
68  std::stack<const symtab::SymbolTable*> symtab_stack;
69 
70  /// variables currently being renamed and their count
71  std::map<std::string, int> renamed_variables;
72 
73  public:
74  LocalVarRenameVisitor() = default;
75  void visit_statement_block(ast::StatementBlock& node) override;
76 };
77 
78 /** \} */ // end of visitor_classes
79 
80 } // namespace visitor
81 } // namespace nmodl
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::visitor::LocalVarRenameVisitor::symtab
const symtab::SymbolTable * symtab
non-null symbol table in the scope hierarchy
Definition: local_var_rename_visitor.hpp:65
nmodl::visitor::LocalVarRenameVisitor::LocalVarRenameVisitor
LocalVarRenameVisitor()=default
nmodl::visitor::AstVisitor
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
nmodl::symtab::SymbolTable
Represent symbol table for a NMODL block.
Definition: symbol_table.hpp:57
nmodl::visitor::LocalVarRenameVisitor::renamed_variables
std::map< std::string, int > renamed_variables
variables currently being renamed and their count
Definition: local_var_rename_visitor.hpp:71
nmodl::visitor::LocalVarRenameVisitor::symtab_stack
std::stack< const symtab::SymbolTable * > symtab_stack
symbol tables in case of nested blocks
Definition: local_var_rename_visitor.hpp:68
nmodl::ast::StatementBlock
Represents block encapsulating list of statements.
Definition: statement_block.hpp:53
nmodl::visitor::LocalVarRenameVisitor::visit_statement_block
void visit_statement_block(ast::StatementBlock &node) override
rename name conflicting variables in the statement block and it's all children
Definition: local_var_rename_visitor.cpp:23
nmodl::visitor::LocalVarRenameVisitor
Visitor to rename local variables conflicting with global scope
Definition: local_var_rename_visitor.hpp:62
decl.hpp
Forward declarations of symbols in namespace nmodl::symtab.
ast_visitor.hpp
Concrete visitor for all AST classes.