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