MicroModelicaCCompiler  4.5.3
index.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <string>
23 #include <map>
24 
25 #include <ir/expression.hpp>
26 #include <deps/sbg_graph/deps_graph.hpp>
27 #include <util/symbol_table.hpp>
28 #include <util/table.hpp>
29 
30 namespace MicroModelica {
31 namespace IR {
32 
33 namespace RANGE {
34 typedef enum { For, If } Type;
35 }
36 
37 namespace INDEX {
38 typedef enum { Iterator, Dimension } Type;
39 }
40 
41 class Range;
42 
43 class IndexDefinition {
44  public:
46  IndexDefinition(std::string variable, int constant, int factor);
47  ~IndexDefinition() = default;
48  int constant();
49  int factor();
50  std::string variable();
51 
52  private:
53  string _variable;
54  int _constant;
55  int _factor;
56 };
57 
58 class Index {
59  public:
60  Index() : _indexes(), _exp(){};
61  Index(Expression exp);
62  ~Index() = default;
63  void setMap(IR::Expression exp);
64  bool hasMap() const;
65  bool operator==(const Index& other) const;
66  bool isConstant() const;
67  int dimension();
68  void setExp(Expression exp);
69  std::string print() const;
70  std::string identifier(bool recursive_use = false) const;
71  Range range() const;
72  Index revert() const;
73  Index replace(bool range_idx = false) const;
74  Index replace(Option<Range> range, bool range_idx = false) const;
75  std::string usageExp() const;
76  std::string modelicaExp() const;
77  friend std::ostream& operator<<(std::ostream& out, const Index& i);
78  Util::Variable variable() const;
79  bool hasVariable(int dim);
80  std::string variable(int dim);
81  bool hasFactor(int dim);
82  int factor(int dim);
83  bool hasConstant(int dim);
84  int constant(int dim);
86  bool isEmpty() const;
87  std::vector<std::string> variables();
88  std::vector<int> initValues(Option<Range> range);
89 
90  protected:
91  void parseIndexes();
92 
93  private:
94  map<int, IndexDefinition> _indexes;
96 };
97 
99 
100 typedef vector<int> Usage;
101 
103  public:
104  RangeDefinition(){};
105  RangeDefinition(int begin, int end, int step = 1);
106  RangeDefinition(Expression begin_exp, Expression end_exp, int begin, int end, int step = 1);
107  ~RangeDefinition() = default;
108  inline int begin() const { return _begin; }
109  inline int cBegin() const { return begin() - 1; }
110  inline int end() const { return _end; };
111  inline int cEnd() const { return end() - 1; };
112  inline int step() const { return _step; };
113  inline int size() const { return (_begin == _end) ? 1 : (_end - _begin + 1) / _step; };
114  void setBegin(int begin);
115  void setEnd(int end);
116  std::string beginExp(bool convert_params, bool c_index) const;
117  std::string endExp(bool convert_params, bool c_index) const;
118 
119  protected:
120  std::string generateExp(Expression exp, int limit, bool convert_params) const;
121  int _begin;
122  int _end;
123  int _step;
126 };
127 
129 
130 class Range {
131  public:
132  Range();
133  Range(AST_Equation_For eqf, RANGE::Type type = RANGE::For);
134  Range(AST_Statement_For stf, RANGE::Type type = RANGE::For, bool from_event = false);
136  Range(AST_Expression exp);
137  Range(SB::Set set, int offset, std::vector<std::string> vars = std::vector<std::string>(), Option<Range> orig_range = Option<Range>());
138 
139  ~Range() = default;
140 
141  int size() const;
142  bool isEmpty() const;
143  inline RangeDefinitionTable definition() const { return _ranges; };
144  int dim() const;
145  std::string print(bool range = false, bool c_index = false, bool convert_params = false) const;
146  std::string end() const;
147  std::string indexes() const;
148  std::vector<std::string> getIndexes() const;
149  std::vector<std::string> getInitValues() const;
150  void addLocalVariables() const;
151  void addRangeLocalVariables() const;
152  int rowSize(int dim) const;
153  std::string block(int dim = -1) const;
154  int pos(std::string var);
155  std::string iterator(int dim, bool range_idx = false);
156  std::string iterator(std::string var, int dim, bool range_idx = false);
157  std::string getPrintDimensionVarsString() const;
158  std::string getDimensionVarsString(bool range = false) const;
159  std::vector<std::string> getDimensionVars(bool range = false) const;
160  std::string getDimensionVar(int i, bool range = false) const;
161  bool intersect(Range other);
163  bool checkUsage(Index usage, Index def);
164  std::string in(ExpressionList exps);
165  std::string in(std::vector<std::string> exps);
166  map<std::string, AST_Expression> initExps();
167  void replace(Index ife_usage, Index ifr_usage = Index());
168  friend std::ostream& operator<<(std::ostream& out, const Range& r);
169  void generate(SB::Set set, int offset, std::vector<std::string> vars, std::vector<Expression> begin_exps = std::vector<Expression>(),
170  std::vector<Expression> end_exps = std::vector<Expression>());
171 
172  bool fixed() const;
173 
174  void merge(Range other);
175 
176  bool hasMergedDims() const;
177 
178  bool isDimensionVar(std::string var);
179  void update(int offset);
180  void update(Range other);
181 
182  protected:
183  void generate(Util::Variable var);
184  void generate(AST_Expression exp);
185  void generate(SB::Set set, int offset, std::vector<std::string> vars, Option<Range> orig_range);
186  void updateRangeDefinition(std::string index_def, RangeDefinition def, int pos);
187  void addRangeVariables(int i, string index) const;
188  bool isVariable(std::string var);
189  bool testExpression(AST_Expression exp);
190  Expression getExp(std::vector<Expression> exps, size_t pos);
191  bool checkRangeVariable(string var, set<string>& added_vars, vector<string>& old_keys, int& pos);
192  bool checkRangeVariables(string ife_idx, string ifr_idx, set<string>& added_vars, vector<string>& old_keys, int& pos);
193 
194  private:
195  void setRangeDefinition(AST_ForIndexList fil, bool from_event = false);
198  int _size;
200  std::vector<int> _row_size;
201  bool _fixed;
202  bool _merged_dims;
203 };
204 
205 } // namespace IR
206 } // namespace MicroModelica
MicroModelica::IR::Range::end
std::string end() const
Definition: index.cpp:559
MicroModelica::IR::RangeDefinition::_begin_exp
Expression _begin_exp
Definition: index.hpp:158
MicroModelica::IR::Range
Definition: index.hpp:164
MicroModelica::IR::Index::hasFactor
bool hasFactor(int dim)
Definition: index.cpp:230
MicroModelica::IR::Index::initValues
std::vector< int > initValues(Option< Range > range)
Definition: index.cpp:94
MicroModelica::IR::IndexDefinition::variable
std::string variable()
Definition: index.cpp:70
MicroModelica::IR::Range::addLocalVariables
void addLocalVariables() const
Definition: index.cpp:634
ModelTable
Definition: table.hpp:27
MicroModelica::IR::Index::setExp
void setExp(Expression exp)
Definition: index.cpp:143
usage
void usage()
Definition: main.cpp:48
MicroModelica::Util::Variable
Definition: symbol_table.hpp:75
MicroModelica::IR::Range::_fixed
bool _fixed
Definition: index.hpp:235
MicroModelica::IR::RangeDefinition::RangeDefinition
RangeDefinition()
Definition: index.hpp:138
MicroModelica::IR::Range::initExps
map< std::string, AST_Expression > initExps()
Definition: index.cpp:799
MicroModelica::IR::IndexTable
ModelTable< std::string, Index > IndexTable
Definition: index.hpp:132
MicroModelica::IR::RangeDefinition::begin
int begin() const
Definition: index.hpp:142
expression.hpp
MicroModelica::IR::Range::getPrintDimensionVarsString
std::string getPrintDimensionVarsString() const
Definition: index.cpp:516
MicroModelica::IR::Index::print
std::string print() const
Definition: index.cpp:201
MicroModelica::IR::Range::~Range
~Range()=default
MicroModelica::IR::Index::range
Range range() const
Definition: index.cpp:167
MicroModelica::IR::Range::_ranges
RangeDefinitionTable _ranges
Definition: index.hpp:230
MicroModelica::IR::Range::applyUsage
void applyUsage(Index usage)
Definition: index.cpp:694
MicroModelica::IR::Index::~Index
~Index()=default
MicroModelica::IR::Range::addRangeLocalVariables
void addRangeLocalVariables() const
Definition: index.cpp:646
MicroModelica::IR::Range::_type
RANGE::Type _type
Definition: index.hpp:233
MicroModelica::IR::Index::constant
int constant(int dim)
Definition: index.cpp:236
symbol_table.hpp
MicroModelica::IR::IndexDefinition::IndexDefinition
IndexDefinition()
Definition: index.cpp:62
MicroModelica::IR::Index::operator==
bool operator==(const Index &other) const
Definition: index.cpp:132
MicroModelica::IR::RangeDefinition::endExp
std::string endExp(bool convert_params, bool c_index) const
Definition: index.cpp:277
MicroModelica::IR::Index::isEmpty
bool isEmpty() const
Definition: index.cpp:238
MicroModelica::IR::RangeDefinition::_end_exp
Expression _end_exp
Definition: index.hpp:159
MicroModelica::IR::Range::print
std::string print(bool range=false, bool c_index=false, bool convert_params=false) const
Definition: index.cpp:569
Option
Definition: util_types.hpp:32
MicroModelica::IR::Range::generate
void generate(SB::Set set, int offset, std::vector< std::string > vars, std::vector< Expression > begin_exps=std::vector< Expression >(), std::vector< Expression > end_exps=std::vector< Expression >())
MicroModelica::IR::Range::pos
int pos(std::string var)
Definition: index.cpp:685
MicroModelica::IR::IndexDefinition::~IndexDefinition
~IndexDefinition()=default
MicroModelica::IR::IndexDefinition::constant
int constant()
Definition: index.cpp:66
MicroModelica::IR::Range::fixed
bool fixed() const
Definition: index.cpp:894
MicroModelica::IR::Index::variables
std::vector< std::string > variables()
Definition: index.cpp:211
MicroModelica::IR::RangeDefinition::~RangeDefinition
~RangeDefinition()=default
MicroModelica::IR::Index::revert
Index revert() const
Definition: index.cpp:169
MicroModelica::IR::RangeDefinition::cEnd
int cEnd() const
Definition: index.hpp:145
MicroModelica::IR::Range::merge
void merge(Range other)
Definition: index.cpp:896
MicroModelica::IR::Range::rowSize
int rowSize(int dim) const
Definition: index.cpp:663
MicroModelica::IR::Index::hasMap
bool hasMap() const
Definition: index.cpp:130
MicroModelica::IR::Range::definition
RangeDefinitionTable definition() const
Definition: index.hpp:177
MicroModelica::IR::Range::getExp
Expression getExp(std::vector< Expression > exps, size_t pos)
Definition: index.cpp:425
MicroModelica::IR::Range::testExpression
bool testExpression(AST_Expression exp)
Definition: index.cpp:325
MicroModelica::IR::IndexDefinition::_variable
string _variable
Definition: index.hpp:87
MicroModelica::IR::Expression
Definition: expression.hpp:64
MicroModelica::IR::Range::Range
Range()
Definition: index.cpp:282
MicroModelica::IR::IndexDefinition::_factor
int _factor
Definition: index.hpp:89
MicroModelica::IR::ExpressionList
list< Expression > ExpressionList
Definition: expression.hpp:95
MicroModelica::IR::Index::factor
int factor(int dim)
Definition: index.cpp:232
MicroModelica::IR::Index::_exp
Expression _exp
Definition: index.hpp:129
MicroModelica::IR::IndexDefinition::factor
int factor()
Definition: index.cpp:68
MicroModelica::IR::Range::isDimensionVar
bool isDimensionVar(std::string var)
Definition: index.cpp:514
MicroModelica::IR::Range::_size
int _size
Definition: index.hpp:232
MicroModelica::IR::Index::dimension
int dimension()
Definition: index.cpp:124
MicroModelica::IR::INDEX::Dimension
@ Dimension
Definition: index.hpp:72
MicroModelica::IR::RangeDefinition::size
int size() const
Definition: index.hpp:147
MicroModelica::IR::Index::isConstant
bool isConstant() const
Definition: index.cpp:134
MicroModelica::IR::Range::addRangeVariables
void addRangeVariables(int i, string index) const
Definition: index.cpp:657
MicroModelica::IR::Range::getDimensionVars
std::vector< std::string > getDimensionVars(bool range=false) const
Definition: index.cpp:536
MicroModelica::IR::Range::block
std::string block(int dim=-1) const
Definition: index.cpp:672
MicroModelica::IR::RangeDefinition::beginExp
std::string beginExp(bool convert_params, bool c_index) const
Definition: index.cpp:272
MicroModelica::IR::Range::getDimensionVarsString
std::string getDimensionVarsString(bool range=false) const
Definition: index.cpp:526
MicroModelica::IR::Range::operator<<
friend std::ostream & operator<<(std::ostream &out, const Range &r)
Definition: index.cpp:910
MicroModelica::IR::Index::parseIndexes
void parseIndexes()
Definition: index.cpp:74
MicroModelica::IR::Range::intersect
bool intersect(Range other)
MicroModelica::IR::Range::iterator
std::string iterator(int dim, bool range_idx=false)
Definition: index.cpp:488
MicroModelica::IR::Range::checkRangeVariables
bool checkRangeVariables(string ife_idx, string ifr_idx, set< string > &added_vars, vector< string > &old_keys, int &pos)
Definition: index.cpp:828
MicroModelica::IR::Index::Index
Index()
Definition: index.hpp:94
MicroModelica::IR::IndexDefinition::_constant
int _constant
Definition: index.hpp:88
MicroModelica::IR::RangeDefinition::_end
int _end
Definition: index.hpp:156
MicroModelica::IR::RangeDefinition::generateExp
std::string generateExp(Expression exp, int limit, bool convert_params) const
Definition: index.cpp:257
MicroModelica::IR::Index::usageExp
std::string usageExp() const
Definition: index.cpp:188
MicroModelica::IR::Range::isEmpty
bool isEmpty() const
Definition: index.cpp:797
MicroModelica::IR::Range::in
std::string in(ExpressionList exps)
Definition: index.cpp:765
MicroModelica::IR::INDEX::Type
Type
Definition: index.hpp:72
MicroModelica::IR::RANGE::Type
Type
Definition: index.hpp:85
MicroModelica::IR::Range::setRangeDefinition
void setRangeDefinition(AST_ForIndexList fil, bool from_event=false)
Definition: index.cpp:342
MicroModelica::IR::Usage
vector< int > Usage
Definition: index.hpp:134
MicroModelica::IR::Index
Definition: index.hpp:92
MicroModelica::IR::RangeDefinition::cBegin
int cBegin() const
Definition: index.hpp:143
MicroModelica::IR::RangeDefinition::setBegin
void setBegin(int begin)
Definition: index.cpp:253
MicroModelica::IR::Index::replace
Index replace(bool range_idx=false) const
Definition: index.cpp:175
MicroModelica::IR::Range::indexes
std::string indexes() const
Definition: index.cpp:598
MicroModelica::IR::RangeDefinitionTable
ModelTable< std::string, RangeDefinition > RangeDefinitionTable
Definition: index.hpp:162
MicroModelica::IR::Index::setMap
void setMap(IR::Expression exp)
Definition: index.cpp:128
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::RANGE::For
@ For
Definition: index.hpp:102
MicroModelica::IR::Range::checkUsage
bool checkUsage(Index usage, Index def)
Definition: index.cpp:745
MicroModelica::IR::Index::modelicaExp
std::string modelicaExp() const
Definition: index.cpp:190
MicroModelica::IR::RangeDefinition::end
int end() const
Definition: index.hpp:144
MicroModelica::IR::Range::_index_pos
ModelTable< std::string, int > _index_pos
Definition: index.hpp:231
MicroModelica::IR::Range::checkRangeVariable
bool checkRangeVariable(string var, set< string > &added_vars, vector< string > &old_keys, int &pos)
Definition: index.cpp:810
table.hpp
MicroModelica::IR::Range::size
int size() const
Definition: index.cpp:795
MicroModelica::IR::Range::_row_size
std::vector< int > _row_size
Definition: index.hpp:234
MicroModelica::IR::RangeDefinition::_begin
int _begin
Definition: index.hpp:155
MicroModelica::IR::RangeDefinition::setEnd
void setEnd(int end)
Definition: index.cpp:255
MicroModelica::IR::Index::identifier
std::string identifier(bool recursive_use=false) const
Definition: index.cpp:149
MicroModelica::IR::Index::variable
Util::Variable variable() const
Definition: index.cpp:160
MicroModelica::IR::Range::update
void update(int offset)
Definition: index.cpp:710
MicroModelica::IR::Index::hasConstant
bool hasConstant(int dim)
Definition: index.cpp:234
MicroModelica::IR::Range::getDimensionVar
std::string getDimensionVar(int i, bool range=false) const
Definition: index.cpp:546
MicroModelica::IR::Index::hasVariable
bool hasVariable(int dim)
Definition: index.cpp:226
MicroModelica::IR::Range::updateRangeDefinition
void updateRangeDefinition(std::string index_def, RangeDefinition def, int pos)
Definition: index.cpp:313
MicroModelica::IR::Range::replace
void replace(Index ife_usage, Index ifr_usage=Index())
Definition: index.cpp:843
MicroModelica::IR::Range::isVariable
bool isVariable(std::string var)
Definition: index.cpp:414
MicroModelica::IR::RANGE::If
@ If
Definition: index.hpp:102
MicroModelica::IR::RangeDefinition::step
int step() const
Definition: index.hpp:146
MicroModelica::IR::Index::expression
Expression expression() const
Definition: index.cpp:126
MicroModelica::IR::INDEX::Iterator
@ Iterator
Definition: index.hpp:72
MicroModelica::IR::Range::dim
int dim() const
Definition: index.cpp:906
MicroModelica::IR::Index::_indexes
map< int, IndexDefinition > _indexes
Definition: index.hpp:128
MicroModelica::IR::RangeDefinition
Definition: index.hpp:136
MicroModelica::IR::Range::hasMergedDims
bool hasMergedDims() const
Definition: index.cpp:908
MicroModelica::IR::Range::_merged_dims
bool _merged_dims
Definition: index.hpp:236
MicroModelica::IR::Range::getInitValues
std::vector< std::string > getInitValues() const
Definition: index.cpp:609
MicroModelica::IR::RangeDefinition::_step
int _step
Definition: index.hpp:157
MicroModelica::IR::Range::getIndexes
std::vector< std::string > getIndexes() const
Definition: index.cpp:622
MicroModelica::IR::Index::operator<<
friend std::ostream & operator<<(std::ostream &out, const Index &i)
Definition: index.cpp:240