User Guide
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::RenameVisitor
13  */
14 
15 #include <regex>
16 #include <string>
17 #include <unordered_map>
18 
19 #include "visitors/ast_visitor.hpp"
20 
21 
22 namespace nmodl {
23 namespace visitor {
24 
25 /**
26  * \addtogroup visitor_classes
27  * \{
28  */
29 
30 /**
31  * \class RenameVisitor
32  * \brief `Blindly` rename given variable to new name
33  *
34  * During inlining related passes we have to rename variables
35  * to avoid name conflicts. This pass "blindly" rename any given
36  * variable to new name. The error handling / legality checks are
37  * supposed to be done by other higher level passes. For example,
38  * local renaming pass should be done from inner-most block to top
39  * level block;
40  *
41  * \todo Add log/warning messages.
42  */
44  private:
45  /// ast::Ast* node
46  std::shared_ptr<ast::Program> ast;
47 
48  /// regex for searching which variables to replace
49  std::regex var_name_regex;
50 
51  /// new name
52  std::string new_var_name;
53 
54  /// variable prefix
55  std::string new_var_name_prefix;
56 
57  /// Map that keeps the renamed variables to keep the same random suffix when a variable is
58  /// renamed across the whole file
59  std::unordered_map<std::string, std::string> renamed_variables;
60 
61  /// add prefix to variable name
62  bool add_prefix = false;
63 
64  /// add random suffix
65  bool add_random_suffix = false;
66 
67  /// rename verbatim blocks as well
68  bool rename_verbatim = true;
69 
70  public:
71  RenameVisitor() = default;
72 
73  RenameVisitor(const std::string& old_name, std::string new_name)
74  : var_name_regex(old_name)
75  , new_var_name(std::move(new_name)) {}
76 
77  RenameVisitor(std::shared_ptr<ast::Program> ast,
78  const std::string& old_name,
79  std::string new_var_name_or_prefix,
80  bool add_prefix,
81  bool add_random_suffix)
82  : ast(std::move(ast))
83  , var_name_regex(old_name)
86  if (add_prefix) {
87  new_var_name_prefix = std::move(new_var_name_or_prefix);
88  } else {
89  new_var_name = std::move(new_var_name_or_prefix);
90  }
91  }
92 
93  /// Check if variable is already renamed and use the same naming otherwise add the new_name
94  /// to the renamed_variables map
95  std::string new_name_generator(const std::string& old_name);
96 
97  void set(const std::string& old_name, std::string new_name) {
98  var_name_regex = old_name;
99  new_var_name = std::move(new_name);
100  }
101 
102  void enable_verbatim(bool state) noexcept {
103  rename_verbatim = state;
104  }
105 
106  void visit_name(const ast::Name& node) override;
107  void visit_prime_name(const ast::PrimeName& node) override;
108  void visit_verbatim(const ast::Verbatim& node) override;
109 };
110 
111 /** \} */ // end of visitor_classes
112 
113 } // namespace visitor
114 } // namespace nmodl
nmodl::visitor::RenameVisitor::add_prefix
bool add_prefix
add prefix to variable name
Definition: rename_visitor.hpp:62
nmodl::ast::Verbatim
Represents a C code block.
Definition: verbatim.hpp:38
nmodl::visitor::ConstAstVisitor
Concrete constant visitor for all AST classes.
Definition: ast_visitor.hpp:166
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::visitor::RenameVisitor::visit_name
void visit_name(const ast::Name &node) override
rename matching variable
Definition: rename_visitor.cpp:48
nmodl::visitor::RenameVisitor::RenameVisitor
RenameVisitor(const std::string &old_name, std::string new_name)
Definition: rename_visitor.hpp:73
nmodl::visitor::RenameVisitor::visit_prime_name
void visit_prime_name(const ast::PrimeName &node) override
Prime name has member order which is an integer.
Definition: rename_visitor.cpp:68
nmodl::visitor::RenameVisitor::RenameVisitor
RenameVisitor()=default
nmodl::visitor::RenameVisitor::enable_verbatim
void enable_verbatim(bool state) noexcept
Definition: rename_visitor.hpp:102
nmodl::visitor::RenameVisitor::RenameVisitor
RenameVisitor(std::shared_ptr< ast::Program > ast, const std::string &old_name, std::string new_var_name_or_prefix, bool add_prefix, bool add_random_suffix)
Definition: rename_visitor.hpp:77
nmodl::visitor::RenameVisitor::rename_verbatim
bool rename_verbatim
rename verbatim blocks as well
Definition: rename_visitor.hpp:68
nmodl::visitor::RenameVisitor::new_var_name
std::string new_var_name
new name
Definition: rename_visitor.hpp:52
nmodl::visitor::RenameVisitor::add_random_suffix
bool add_random_suffix
add random suffix
Definition: rename_visitor.hpp:65
nmodl::visitor::RenameVisitor::renamed_variables
std::unordered_map< std::string, std::string > renamed_variables
Map that keeps the renamed variables to keep the same random suffix when a variable is renamed across...
Definition: rename_visitor.hpp:59
nmodl::ast::PrimeName
Represents a prime variable (for ODE)
Definition: prime_name.hpp:48
nmodl::visitor::RenameVisitor
Blindly rename given variable to new name
Definition: rename_visitor.hpp:43
nmodl::visitor::RenameVisitor::set
void set(const std::string &old_name, std::string new_name)
Definition: rename_visitor.hpp:97
nmodl::visitor::RenameVisitor::new_var_name_prefix
std::string new_var_name_prefix
variable prefix
Definition: rename_visitor.hpp:55
nmodl::ast::Name
Represents a name.
Definition: name.hpp:44
nmodl::visitor::RenameVisitor::new_name_generator
std::string new_name_generator(const std::string &old_name)
Check if variable is already renamed and use the same naming otherwise add the new_name to the rename...
Definition: rename_visitor.cpp:20
nmodl::visitor::RenameVisitor::var_name_regex
std::regex var_name_regex
regex for searching which variables to replace
Definition: rename_visitor.hpp:49
nmodl::visitor::RenameVisitor::visit_verbatim
void visit_verbatim(const ast::Verbatim &node) override
Parse verbatim blocks and rename variable if it is used.
Definition: rename_visitor.cpp:75
nmodl::visitor::RenameVisitor::ast
std::shared_ptr< ast::Program > ast
ast::Ast* node
Definition: rename_visitor.hpp:46
ast_visitor.hpp
Concrete visitor for all AST classes.