User Guide
integer.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/number.hpp"
24 
25 namespace nmodl::ast {
26 
27 /**
28  * \addtogroup ast_class
29  * \ingroup ast
30  * \{
31  */
32 
33 /**
34  * \brief Represents an integer variable
35  *
36  * Non floating value in the mod file is parsed as an integer. For example,
37  * in the below code, integer literals like `0` and `11` are stored as ast::Integer
38  * in the AST :
39  *
40  * \code{.mod}
41  * FROM i=0 TO N {
42  * tau = X[i] + 11
43  * }
44  * \endcode
45  *
46  * \sa ast::Float ast::Double
47  *
48 */
49 class Integer : public Number {
50  private:
51  /// Value of integer
52  int value;
53  /// if integer is a macro then it's name
54  std::shared_ptr<Name> macro;
55  /// token with location information
56  std::shared_ptr<ModToken> token;
57 
58  public:
59  /// \name Ctor & dtor
60  /// \{
61  explicit Integer(int value, Name* macro);
62  explicit Integer(int value, std::shared_ptr<Name> macro);
63  Integer(const Integer& obj);
64  Integer() = default;
65  virtual ~Integer() = default;
66  /// \}
67 
68  /**
69  * \brief Check if the ast node is an instance of ast::Integer
70  * \return true as object is of type ast::Integer
71  */
72  bool is_integer () const noexcept override {
73  return true;
74  }
75 
76  /**
77  * \brief Return a copy of the current node
78  *
79  * Recursively make a new copy/clone of the current node including
80  * all members and return a pointer to the node. This is used for
81  * passes like nmodl::visitor::InlineVisitor where nodes are cloned in the
82  * ast.
83  *
84  * \return pointer to the clone/copy of the current node
85  */
86  // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
87  Integer* clone() const override {
88  return new Integer(*this);
89  }
90  // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
91 
92  /// \name Getters
93  /// \{
94 
95  /**
96  * \brief Return type (ast::AstNodeType) of ast node
97  *
98  * Every node in the ast has a type defined in ast::AstNodeType and this
99  * function is used to retrieve the same.
100  *
101  * \return ast node type i.e. ast::AstNodeType::INTEGER
102  *
103  * \sa Ast::get_node_type_name
104  */
105  AstNodeType get_node_type() const noexcept override {
106  return AstNodeType::INTEGER;
107  }
108 
109  /**
110  * \brief Return type (ast::AstNodeType) of ast node as std::string
111  *
112  * Every node in the ast has a type defined in ast::AstNodeType.
113  * This type name can be returned as a std::string for printing
114  * node to text/json form.
115  *
116  * \return name of the node type as a string i.e. "Integer"
117  *
118  * \sa Ast::get_node_name
119  */
120  std::string get_node_type_name() const noexcept override {
121  return "Integer";
122  }
123 
124 
125  /**
126  * \brief Get std::shared_ptr from `this` pointer of the current ast node
127  */
128  std::shared_ptr<Ast> get_shared_ptr() override {
129  return std::static_pointer_cast<Integer>(shared_from_this());
130  }
131 
132  /**
133  * \brief Get std::shared_ptr from `this` pointer of the current ast node
134  */
135  std::shared_ptr<const Ast> get_shared_ptr() const override {
136  return std::static_pointer_cast<const Integer>(shared_from_this());
137  }
138 
139  /**
140  * \brief Return associated token for the current ast node
141  *
142  * Not all ast nodes have token information. For example, nmodl::visitor::NeuronSolveVisitor
143  * can insert new nodes in the ast as a solution of ODEs. In this case, we return
144  * nullptr to store in the nmodl::symtab::SymbolTable.
145  *
146  * \return pointer to token if exist otherwise nullptr
147  */
148  const ModToken* get_token() const noexcept override {
149  return token.get();
150  }
151 
152 
153 
154  /**
155  * \brief Getter for member variable \ref Integer.value
156  */
157  int get_value() const noexcept {
158  return value;
159  }
160 
161 
162 
163  /**
164  * \brief Getter for member variable \ref Integer.macro
165  */
166  std::shared_ptr<Name> get_macro() const noexcept {
167  return macro;
168  }
169  /// \}
170 
171  /// \name Setters
172  /// \{
173  /**
174  * \brief Set token for the current ast node
175  */
176  void set_token(const ModToken& tok) { token = std::make_shared<ModToken>(tok); }
177  /**
178  * \brief Set new value to the current ast node
179  * \sa Integer::eval
180  */
181  void set(int _value) {
182  value = _value;
183  }
184 
185  /**
186  * \brief Setter for member variable \ref Integer.value
187  */
188  void set_value(int value);
189 
190 
191  /**
192  * \brief Setter for member variable \ref Integer.macro (rvalue reference)
193  */
194  void set_macro(std::shared_ptr<Name>&& macro);
195 
196  /**
197  * \brief Setter for member variable \ref Integer.macro
198  */
199  void set_macro(const std::shared_ptr<Name>& macro);
200 
201  /// \}
202 
203  /// \name Visitor
204  /// \{
205  /**
206  * \brief visit children i.e. member variables of current node using provided visitor
207  *
208  * Different nodes in the AST have different members (i.e. children). This method
209  * recursively visits children using provided visitor.
210  *
211  * \param v Concrete visitor that will be used to recursively visit children
212  *
213  * \sa Ast::visit_children for example.
214  */
215  void visit_children(visitor::Visitor& v) override;
216 
217  /**
218  * \brief visit children i.e. member variables of current node using provided visitor
219  *
220  * Different nodes in the AST have different members (i.e. children). This method
221  * recursively visits children using provided visitor.
222  *
223  * \param v Concrete constant visitor that will be used to recursively visit children
224  *
225  * \sa Ast::visit_children for example.
226  */
227  void visit_children(visitor::ConstVisitor& v) const override;
228 
229  /**
230  * \brief accept (or visit) the current AST node using provided visitor
231  *
232  * Instead of visiting children of AST node, like Ast::visit_children,
233  * accept allows to visit the current node itself using provided concrete
234  * visitor.
235  *
236  * \param v Concrete visitor that will be used to recursively visit node
237  *
238  * \sa Ast::accept for example.
239  */
240  void accept(visitor::Visitor& v) override;
241 
242  /**
243  * \copydoc accept(visitor::Visitor&)
244  */
245  void accept(visitor::ConstVisitor& v) const override;
246  /// \}
247 
248  /**
249  * \brief Negate the value of current ast node
250  *
251  * Parser parse `-x` in two parts : `x` and then `-`. Once second token
252  * `-` is encountered, the corresponding value of ast node needs to be
253  * multiplied by `-1` for ast::Number node types.
254  */
255  void negate() override {
256  value = -value;
257  }
258 
259  /**
260  * \brief Return value of the current ast node as double
261  */
262  double to_double() override {
263  return value;
264  }
265  /**
266  * \brief Return value of the ast node
267  *
268  * Base data type nodes like ast::Inetegr, ast::Double can be evaluated
269  * to their literal values. This method is used to access underlying
270  * literal value.
271  *
272  * \sa Integer::set
273  */
274  int eval() const {
275  return value;
276  }
277  private:
278  /**
279  * \brief Set this object as parent for all the children
280  *
281  * This should be called in every object (with children) constructor
282  * to set parents. Since it is called only in the constructors it
283  * should not be virtual to avoid ambiguities (issue #295).
284  */
285  void set_parent_in_children();
286 };
287 
288 /** \} */ // end of ast_class
289 
290 
291 
292 
293 } // namespace nmodl::ast
nmodl::visitor::ConstVisitor
Abstract base class for all constant visitors implementation.
Definition: visitor.hpp:298
nmodl::ast::Integer::set
void set(int _value)
Set new value to the current ast node.
Definition: integer.hpp:181
nmodl::ast::Integer::get_node_type
AstNodeType get_node_type() const noexcept override
Return type (ast::AstNodeType) of ast node.
Definition: integer.hpp:105
nmodl::ast::Integer::set_token
void set_token(const ModToken &tok)
Set token for the current ast node.
Definition: integer.hpp:176
nmodl::ast::Integer::value
int value
Value of integer.
Definition: integer.hpp:52
nmodl::ast::Integer::Integer
Integer()=default
nmodl::ast::Integer::accept
void accept(visitor::Visitor &v) override
accept (or visit) the current AST node using provided visitor
Definition: ast.cpp:535
nmodl::ast::Integer::set_parent_in_children
void set_parent_in_children()
Set this object as parent for all the children.
Definition: ast.cpp:572
nmodl::ast::Integer::to_double
double to_double() override
Return value of the current ast node as double.
Definition: integer.hpp:262
ast_decl.hpp
THIS FILE IS GENERATED AT BUILD TIME AND SHALL NOT BE EDITED.
nmodl::ast::Integer::get_value
int get_value() const noexcept
Getter for member variable Integer::value.
Definition: integer.hpp:157
nmodl::ast::Integer::get_macro
std::shared_ptr< Name > get_macro() const noexcept
Getter for member variable Integer::macro.
Definition: integer.hpp:166
number.hpp
Auto generated AST classes declaration.
nmodl::ast::Integer
Represents an integer variable.
Definition: integer.hpp:49
nmodl::ast
Abstract Syntax Tree (AST) related implementations.
Definition: ast_common.hpp:29
nmodl::ast::Integer::get_shared_ptr
std::shared_ptr< Ast > get_shared_ptr() override
Get std::shared_ptr from this pointer of the current ast node.
Definition: integer.hpp:128
nmodl::ast::Integer::is_integer
bool is_integer() const noexcept override
Check if the ast node is an instance of ast::Integer.
Definition: integer.hpp:72
nmodl::ast::AstNodeType
AstNodeType
Enum type for every AST node type.
Definition: ast_decl.hpp:164
nmodl::ast::Integer::get_node_type_name
std::string get_node_type_name() const noexcept override
Return type (ast::AstNodeType) of ast node as std::string.
Definition: integer.hpp:120
nmodl::ast::Integer::token
std::shared_ptr< ModToken > token
token with location information
Definition: integer.hpp:56
nmodl::visitor::Visitor
Abstract base class for all visitors implementation.
Definition: visitor.hpp:39
nmodl::ast::Integer::get_token
const ModToken * get_token() const noexcept override
Return associated token for the current ast node.
Definition: integer.hpp:148
nmodl::ast::Integer::visit_children
void visit_children(visitor::Visitor &v) override
visit children i.e.
Definition: ast.cpp:519
nmodl::ast::Integer::clone
Integer * clone() const override
Return a copy of the current node.
Definition: integer.hpp:87
nmodl::ast::Integer::eval
int eval() const
Return value of the ast node.
Definition: integer.hpp:274
nmodl::ast::Integer::~Integer
virtual ~Integer()=default
nmodl::ast::Integer::set_macro
void set_macro(std::shared_ptr< Name > &&macro)
Setter for member variable Integer::macro (rvalue reference)
Definition: ast.cpp:590
nmodl::ast::AstNodeType::INTEGER
@ INTEGER
type of ast::Integer
nmodl::ast::Name
Represents a name.
Definition: name.hpp:44
nmodl::ast::Number
Base class for all numbers.
Definition: number.hpp:39
nmodl::ast::Integer::macro
std::shared_ptr< Name > macro
if integer is a macro then it's name
Definition: integer.hpp:54
nmodl::ast::Integer::negate
void negate() override
Negate the value of current ast node.
Definition: integer.hpp:255
nmodl::ModToken
Represent token returned by scanner.
Definition: modtoken.hpp:50
nmodl::ast::Integer::set_value
void set_value(int value)
Setter for member variable Integer::value.
Definition: ast.cpp:583
nmodl::ast::Integer::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: integer.hpp:135