User Guide
program.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 ///
9 /// THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
10 ///
11 
12 #pragma once
13 
14 /**
15  * \dir
16  * \brief Auto generated AST Implementations
17  *
18  * \file
19  * \brief Auto generated AST classes declaration
20  */
21 
22 #include "ast/ast_decl.hpp"
23 #include "ast/ast.hpp"
24 #include "ast/node.hpp"
25 
26 namespace nmodl::ast {
27 
28 /**
29  * \addtogroup ast_class
30  * \ingroup ast
31  * \{
32  */
33 
34 /**
35  * \brief Represents top level AST node for whole NMODL input
36  *
37  *
38 */
39 class Program : public Ast {
40  private:
41  /// Vector of top level blocks in the mod file
43  /// token with location information
44  std::shared_ptr<ModToken> token;
45  /// symbol table for a block
47  /// global symbol table for model
49 
50  public:
51  /// \name Ctor & dtor
52  /// \{
53  explicit Program(const NodeVector& blocks);
54  Program(const Program& obj);
55  Program() = default;
56  virtual ~Program() = default;
57  /// \}
58 
59  /**
60  * \brief Check if the ast node is an instance of ast::Program
61  * \return true as object is of type ast::Program
62  */
63  bool is_program () const noexcept override {
64  return true;
65  }
66 
67  /**
68  * \brief Return a copy of the current node
69  *
70  * Recursively make a new copy/clone of the current node including
71  * all members and return a pointer to the node. This is used for
72  * passes like nmodl::visitor::InlineVisitor where nodes are cloned in the
73  * ast.
74  *
75  * \return pointer to the clone/copy of the current node
76  */
77  // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
78  Program* clone() const override {
79  return new Program(*this);
80  }
81  // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
82 
83  /// \name Getters
84  /// \{
85 
86  /**
87  * \brief Return type (ast::AstNodeType) of ast node
88  *
89  * Every node in the ast has a type defined in ast::AstNodeType and this
90  * function is used to retrieve the same.
91  *
92  * \return ast node type i.e. ast::AstNodeType::PROGRAM
93  *
94  * \sa Ast::get_node_type_name
95  */
96  AstNodeType get_node_type() const noexcept override {
97  return AstNodeType::PROGRAM;
98  }
99 
100  /**
101  * \brief Return type (ast::AstNodeType) of ast node as std::string
102  *
103  * Every node in the ast has a type defined in ast::AstNodeType.
104  * This type name can be returned as a std::string for printing
105  * node to text/json form.
106  *
107  * \return name of the node type as a string i.e. "Program"
108  *
109  * \sa Ast::get_node_name
110  */
111  std::string get_node_type_name() const noexcept override {
112  return "Program";
113  }
114 
115 
116  /**
117  * \brief Get std::shared_ptr from `this` pointer of the current ast node
118  */
119  std::shared_ptr<Ast> get_shared_ptr() override {
120  return std::static_pointer_cast<Program>(shared_from_this());
121  }
122 
123  /**
124  * \brief Get std::shared_ptr from `this` pointer of the current ast node
125  */
126  std::shared_ptr<const Ast> get_shared_ptr() const override {
127  return std::static_pointer_cast<const Program>(shared_from_this());
128  }
129 
130  /**
131  * \brief Return associated token for the current ast node
132  *
133  * Not all ast nodes have token information. For example, nmodl::visitor::NeuronSolveVisitor
134  * can insert new nodes in the ast as a solution of ODEs. In this case, we return
135  * nullptr to store in the nmodl::symtab::SymbolTable.
136  *
137  * \return pointer to token if exist otherwise nullptr
138  */
139  const ModToken* get_token() const noexcept override {
140  return token.get();
141  }
142  /**
143  * \brief Return associated symbol table for the current ast node
144  *
145  * Only certain ast nodes (e.g. inherited from ast::Block) have associated
146  * symbol table. These nodes have nmodl::symtab::SymbolTable as member
147  * and it can be accessed using this method.
148  *
149  * \return pointer to the symbol table
150  *
151  * \sa nmodl::symtab::SymbolTable nmodl::visitor::SymtabVisitor
152  */
154  return symtab;
155  }
156  /**
157  * \brief Return global symbol table for the mod file
158  */
160  return &model_symtab;
161  }
162 
163  /**
164  * \brief Add member to blocks by raw pointer
165  */
166  void emplace_back_node(Node *n);
167 
168  /**
169  * \brief Add member to blocks by shared_ptr
170  */
171  void emplace_back_node(std::shared_ptr<Node> n);
172 
173  /**
174  * \brief Erase member to blocks
175  */
176  NodeVector::const_iterator erase_node(NodeVector::const_iterator first);
177 
178  /**
179  * \brief Erase members to blocks
180  */
181  NodeVector::const_iterator erase_node(NodeVector::const_iterator first, NodeVector::const_iterator last);
182 
183  /**
184  * \brief Erase non-consecutive members to blocks
185  *
186  * loosely following the cpp reference of remove_if
187  */
188  size_t erase_node(std::unordered_set<Node*>& to_be_erased);
189 
190  /**
191  * \brief Insert member to blocks
192  */
193  NodeVector::const_iterator insert_node(NodeVector::const_iterator position, const std::shared_ptr<Node>& n);
194 
195  /**
196  * \brief Insert members to blocks
197  */
198  template <class NodeType, class InputIterator>
199  void insert_node(NodeVector::const_iterator position, NodeType& to, InputIterator first, InputIterator last);
200 
201  /**
202  * \brief Reset member to blocks
203  */
204  void reset_node(NodeVector::const_iterator position, Node* n);
205 
206  /**
207  * \brief Reset member to blocks
208  */
209  void reset_node(NodeVector::const_iterator position, std::shared_ptr<Node> n);
210 
211 
212 
213  /**
214  * \brief Getter for member variable \ref Program.blocks
215  */
216  const NodeVector& get_blocks() const noexcept {
217  return blocks;
218  }
219  /// \}
220 
221  /// \name Setters
222  /// \{
223  /**
224  * \brief Set token for the current ast node
225  */
226  void set_token(const ModToken& tok) { token = std::make_shared<ModToken>(tok); }
227  /**
228  * \brief Set symbol table for the current ast node
229  *
230  * Top level, block scoped nodes store symbol table in the ast node.
231  * nmodl::visitor::SymtabVisitor then used this method to setup symbol table
232  * for every node in the ast.
233  *
234  * \sa nmodl::visitor::SymtabVisitor
235  */
236  void set_symbol_table(symtab::SymbolTable* newsymtab) override {
237  symtab = newsymtab;
238  }
239 
240  /**
241  * \brief Setter for member variable \ref Program.blocks (rvalue reference)
242  */
243  void set_blocks(NodeVector&& blocks);
244 
245  /**
246  * \brief Setter for member variable \ref Program.blocks
247  */
248  void set_blocks(const NodeVector& blocks);
249 
250  /// \}
251 
252  /// \name Visitor
253  /// \{
254  /**
255  * \brief visit children i.e. member variables of current node using provided visitor
256  *
257  * Different nodes in the AST have different members (i.e. children). This method
258  * recursively visits children using provided visitor.
259  *
260  * \param v Concrete visitor that will be used to recursively visit children
261  *
262  * \sa Ast::visit_children for example.
263  */
264  void visit_children(visitor::Visitor& v) override;
265 
266  /**
267  * \brief visit children i.e. member variables of current node using provided visitor
268  *
269  * Different nodes in the AST have different members (i.e. children). This method
270  * recursively visits children using provided visitor.
271  *
272  * \param v Concrete constant visitor that will be used to recursively visit children
273  *
274  * \sa Ast::visit_children for example.
275  */
276  void visit_children(visitor::ConstVisitor& v) const override;
277 
278  /**
279  * \brief accept (or visit) the current AST node using provided visitor
280  *
281  * Instead of visiting children of AST node, like Ast::visit_children,
282  * accept allows to visit the current node itself using provided concrete
283  * visitor.
284  *
285  * \param v Concrete visitor that will be used to recursively visit node
286  *
287  * \sa Ast::accept for example.
288  */
289  void accept(visitor::Visitor& v) override;
290 
291  /**
292  * \copydoc accept(visitor::Visitor&)
293  */
294  void accept(visitor::ConstVisitor& v) const override;
295  /// \}
296 
297  private:
298  /**
299  * \brief Set this object as parent for all the children
300  *
301  * This should be called in every object (with children) constructor
302  * to set parents. Since it is called only in the constructors it
303  * should not be virtual to avoid ambiguities (issue #295).
304  */
305  void set_parent_in_children();
306 };
307 
308 /** \} */ // end of ast_class
309 
310 
311 /**
312  * \brief Insert members to blocks
313  */
314 template <class NodeType, class InputIterator>
315 void Program::insert_node(NodeVector::const_iterator position, NodeType& to, InputIterator first, InputIterator last) {
316 
317  for (auto it = first; it != last; ++it) {
318  auto& n = *it;
319  //set parents
320  n->set_parent(this);
321  }
322  blocks.insert(position, first, last);
323 }
324 
325 
326 } // namespace nmodl::ast
nmodl::ast::Program::get_model_symbol_table
symtab::ModelSymbolTable * get_model_symbol_table()
Return global symbol table for the mod file.
Definition: program.hpp:159
nmodl::visitor::ConstVisitor
Abstract base class for all constant visitors implementation.
Definition: visitor.hpp:298
nmodl::ast::Node
Base class for all AST node.
Definition: node.hpp:40
nmodl::ast::Program::is_program
bool is_program() const noexcept override
Check if the ast node is an instance of ast::Program.
Definition: program.hpp:63
nmodl::ast::Program::get_shared_ptr
std::shared_ptr< Ast > get_shared_ptr() override
Get std::shared_ptr from this pointer of the current ast node.
Definition: program.hpp:119
nmodl::ast::Ast
Base class for all Abstract Syntax Tree node types.
Definition: ast.hpp:69
nmodl::ast::Program::set_token
void set_token(const ModToken &tok)
Set token for the current ast node.
Definition: program.hpp:226
nmodl::ast::Program::set_symbol_table
void set_symbol_table(symtab::SymbolTable *newsymtab) override
Set symbol table for the current ast node.
Definition: program.hpp:236
node.hpp
Auto generated AST classes declaration.
ast_decl.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::ast::NodeVector
std::vector< std::shared_ptr< Node > > NodeVector
Definition: ast_decl.hpp:297
nmodl::ast::Program::erase_node
NodeVector::const_iterator erase_node(NodeVector::const_iterator first)
Erase member to blocks.
Definition: ast.cpp:12836
nmodl::ast::Program::get_node_type_name
std::string get_node_type_name() const noexcept override
Return type (ast::AstNodeType) of ast node as std::string.
Definition: program.hpp:111
nmodl::ast
Abstract Syntax Tree (AST) related implementations.
Definition: ast_common.hpp:29
nmodl::ast::Program::accept
void accept(visitor::Visitor &v) override
accept (or visit) the current AST node using provided visitor
Definition: ast.cpp:12918
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:164
nmodl::ast::Program::clone
Program * clone() const override
Return a copy of the current node.
Definition: program.hpp:78
nmodl::visitor::Visitor
Abstract base class for all visitors implementation.
Definition: visitor.hpp:39
nmodl::ast::Program::symtab
symtab::SymbolTable * symtab
symbol table for a block
Definition: program.hpp:46
nmodl::ast::Program::model_symtab
symtab::ModelSymbolTable model_symtab
global symbol table for model
Definition: program.hpp:48
nmodl::ast::Program::get_shared_ptr
std::shared_ptr< const Ast > get_shared_ptr() const override
Get std::shared_ptr from this pointer of the current ast node.
Definition: program.hpp:126
nmodl::ast::Program::set_parent_in_children
void set_parent_in_children()
Set this object as parent for all the children.
Definition: ast.cpp:12950
nmodl::ast::Program::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:12902
nmodl::ast::Program::token
std::shared_ptr< ModToken > token
token with location information
Definition: program.hpp:44
nmodl::symtab::SymbolTable
Represent symbol table for a NMODL block.
Definition: symbol_table.hpp:57
ast.hpp
Auto generated AST classes declaration.
nmodl::ast::Program::~Program
virtual ~Program()=default
nmodl::ast::Program::get_blocks
const NodeVector & get_blocks() const noexcept
Getter for member variable Program::blocks.
Definition: program.hpp:216
nmodl::ast::Program::reset_node
void reset_node(NodeVector::const_iterator position, Node *n)
Reset member to blocks.
Definition: ast.cpp:12882
nmodl::symtab::ModelSymbolTable
Hold top level (i.e.
Definition: symbol_table.hpp:239
nmodl::ast::Program::get_token
const ModToken * get_token() const noexcept override
Return associated token for the current ast node.
Definition: program.hpp:139
nmodl::ast::Program::insert_node
NodeVector::const_iterator insert_node(NodeVector::const_iterator position, const std::shared_ptr< Node > &n)
Insert member to blocks.
Definition: ast.cpp:12874
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::Program
Program()=default
nmodl::ast::Program::get_node_type
AstNodeType get_node_type() const noexcept override
Return type (ast::AstNodeType) of ast node.
Definition: program.hpp:96
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
nmodl::ast::Program::set_blocks
void set_blocks(NodeVector &&blocks)
Setter for member variable Program::blocks (rvalue reference)
Definition: ast.cpp:12962
nmodl::ast::Program::blocks
NodeVector blocks
Vector of top level blocks in the mod file.
Definition: program.hpp:42
nmodl::ModToken
Represent token returned by scanner.
Definition: modtoken.hpp:50
nmodl::ast::AstNodeType::PROGRAM
@ PROGRAM
type of ast::Program