User Guide
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 
47  void visit_indexed_name(ast::IndexedName& node) override;
48 };
49 
50 } // namespace visitor
51 } // namespace nmodl
nmodl::visitor::IndexRemover::index
std::string index
index variable name
Definition: index_remover.hpp:31
nmodl::visitor::IndexRemover::under_indexed_name
bool under_indexed_name
true if we are visiting index variable
Definition: index_remover.hpp:37
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::visitor::IndexRemover
Helper visitor to replace index of array variable with integer.
Definition: index_remover.hpp:28
nmodl::ast::IndexedName
Represents specific element of an array variable.
Definition: indexed_name.hpp:48
nmodl::visitor::AstVisitor
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
nmodl::visitor::IndexRemover::visit_binary_expression
void visit_binary_expression(ast::BinaryExpression &node) override
visit node of type ast::BinaryExpression
Definition: index_remover.cpp:25
nmodl::visitor::IndexRemover::value
int value
integer value of index variable
Definition: index_remover.hpp:34
nmodl::visitor::IndexRemover::visit_indexed_name
void visit_indexed_name(ast::IndexedName &node) override
visit node of type ast::IndexedName
Definition: index_remover.cpp:37
nmodl::visitor::IndexRemover::replace_for_name
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
Definition: index_remover.cpp:14
nmodl::ast::BinaryExpression
Represents binary expression in the NMODL.
Definition: binary_expression.hpp:52
all.hpp
Auto generated AST classes declaration.
nmodl::visitor::IndexRemover::IndexRemover
IndexRemover(std::string index, int value)
Definition: index_remover.cpp:9
ast_visitor.hpp
Concrete visitor for all AST classes.