NEURON
index_remover.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 #include "ast/all.hpp"
11 #include "visitors/ast_visitor.hpp"
12 
13 namespace nmodl {
14 namespace visitor {
15 
16 /**
17  * \class IndexRemover
18  * \brief Helper visitor to replace index of array variable with integer
19  *
20  * When loop is unrolled, the index variable like `i` :
21  *
22  * ca[i] <-> ca[i+1]
23  *
24  * has type `Name` in the AST. This needs to be replaced with `Integer`
25  * for optimizations like constant folding. This pass look at name and
26  * binary expressions under index variables.
27  */
28 class IndexRemover: public AstVisitor {
29  private:
30  /// index variable name
31  std::string index;
32 
33  /// integer value of index variable
34  int value;
35 
36  /// true if we are visiting index variable
37  bool under_indexed_name = false;
38 
39  public:
40  IndexRemover(std::string index, int value);
41 
42  /// if expression we are visiting is `Name` then return new `Integer` node
43  std::shared_ptr<ast::Expression> replace_for_name(
44  const std::shared_ptr<ast::Expression>& node) const;
45 
48 };
49 
50 } // namespace visitor
51 } // namespace nmodl
Auto generated AST classes declaration.
Concrete visitor for all AST classes.
Represents binary expression in the NMODL.
Represents specific element of an array variable.
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
Helper visitor to replace index of array variable with integer.
void visit_indexed_name(ast::IndexedName &node) override
visit node of type ast::IndexedName
std::string index
index variable name
IndexRemover(std::string index, int value)
int value
integer value of index variable
std::shared_ptr< ast::Expression > replace_for_name(const std::shared_ptr< ast::Expression > &node) const
if expression we are visiting is Name then return new Integer node
bool under_indexed_name
true if we are visiting index variable
void visit_binary_expression(ast::BinaryExpression &node) override
visit node of type ast::BinaryExpression
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
static Node * node(Object *)
Definition: netcvode.cpp:291