MicroModelicaCCompiler  4.5.3
symbol_table.cpp
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 #include "symbol_table.hpp"
21 
22 #include <math.h>
23 #include <sstream>
24 
25 #include <ast/ast_builder.hpp>
26 #include <ast/expression.hpp>
27 #include <ir/equation.hpp>
28 #include <ir/expression.hpp>
29 #include <ir/helpers.hpp>
30 #include <util/util.hpp>
31 
32 namespace MicroModelica {
33 using namespace IR;
34 namespace Util {
35 
36 /* Variable class. */
37 
39  : _unknown(false),
40  _t(nullptr),
41  _tp(TP_CONSTANT),
42  _m(nullptr),
43  _comm(nullptr),
44  _builtin(false),
45  _size(),
46  _value(0),
47  _exp(nullptr),
48  _hasStart(false),
49  _hasEach(false),
50  _hasAssigment(false),
51  _name(),
52  _isArray(false),
53  _hasOffset(false),
54  _offset(0),
55  _realType(NotAssigned)
56 {
57 }
58 
59 Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c)
60  : _unknown(false),
61  _t(t),
62  _tp(tp),
63  _m(m),
64  _comm(c),
65  _builtin(false),
66  _size(),
67  _value(0),
68  _exp(nullptr),
69  _hasStart(false),
70  _hasEach(false),
71  _hasAssigment(false),
72  _name(),
73  _isArray(false),
74  _hasOffset(false),
75  _offset(0),
76  _realType(NotAssigned)
77 {
78  processModification();
79 }
80 
81 Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector<int> &s, bool array)
82  : _unknown(false),
83  _t(t),
84  _tp(tp),
85  _m(m),
86  _comm(c),
87  _builtin(false),
88  _size(s),
89  _value(0),
90  _exp(nullptr),
91  _hasStart(false),
92  _hasEach(false),
93  _hasAssigment(false),
94  _name(),
95  _isArray(array),
96  _hasOffset(false),
97  _offset(0),
98  _realType(NotAssigned)
99 {
100  processModification();
101 }
102 
104 {
105  _t = other._t;
106  _tp = other._tp;
107  _m = other._m;
108  _comm = other._comm;
109  _builtin = other._builtin;
110  _size = other._size;
111  _value = other._value;
112  _exp = other._exp;
113  _hasStart = other._hasStart;
114  _hasEach = other._hasEach;
115  _hasAssigment = other._hasAssigment;
116  _name = other._name;
117  _isArray = other._isArray;
118  _hasOffset = other._hasOffset;
119  _offset = other._offset;
120  _realType = other._realType;
121  return *this;
122 }
123 
124 bool Variable::operator==(const Variable &other) { return print() == other.print(); }
125 
126 bool Variable::operator!=(const Variable &other) { return !((*this) == other); }
127 
135 {
136  _hasAssigment = false;
137  _hasEach = false;
138  _hasStart = false;
139  if (_m != nullptr) {
140  ModificationType t = _m->modificationType();
141  if (t == MODEQUAL) {
142  _hasAssigment = true;
143  _exp = _m->getAsEqual()->exp();
144  } else if (t == MODCLASS) {
145  AST_ArgumentList al = _m->getAsClass()->arguments();
146  AST_ArgumentListIterator it;
147  AST_Expression asgExp = _m->getAsClass()->exp();
148  if (asgExp->expressionType() != EXPNULL) {
149  _hasAssigment = true;
150  _exp = asgExp;
151  } else {
152  if (!isParameter()) {
153  foreach (it, al) {
154  if (current_element(it)->argumentType() == AR_MODIFICATION) {
155  AST_Argument_Modification am = current_element(it)->getAsModification();
156  if (am->modification()->modificationType() == MODEQUAL) {
157  _exp = am->modification()->getAsEqual()->exp();
158  if (current_element(it)->hasEach()) {
159  _hasEach = true;
160  } else if (am->name()->compare("start") == 0) {
161  _hasStart = true;
162  break;
163  }
164  }
165  }
166  }
167  }
168  }
169  }
170  }
171 }
172 
173 void Variable::setName(string name) { _name = name; }
174 
175 unsigned int Variable::size()
176 {
177  unsigned int total = 1;
178  for (vector<int>::const_iterator it = _size.begin(); it != _size.end(); it++) {
179  total *= *it;
180  }
181  return total;
182 }
183 
184 unsigned int Variable::rowSize(unsigned int dim) const
185 {
186  unsigned int total = 1;
187  for (unsigned int it = dim + 1; it < _size.size(); it++) {
188  total *= _size.at(it);
189  }
190  return total;
191 }
192 
193 string Variable::print() const
194 {
195  stringstream buffer;
196  if (isForType() || isInput() || isOutput() || isEqType() || isLocal()) {
197  buffer << _name;
198  } else if (isConstant()) {
199  buffer << _value;
200  } else {
201  buffer << "_" << _name;
202  }
203  return buffer.str();
204 }
205 
206 bool Variable::isDiscreteInteger() const { return (_t->getType() == SymbolType::TYINTEGER) && isDiscrete(); }
207 
208 std::string Variable::castOperator() const
209 {
210  string cast_operator;
211  if (isDiscreteInteger()) {
212  cast_operator = "(int)";
213  }
214  return cast_operator;
215 }
216 
217 ostream &operator<<(ostream &out, const Variable &v)
218 {
219  out << v.print();
220  return out;
221 }
222 
223 string Variable::declaration(string prefix)
224 {
225  stringstream buffer;
226  if (type()->print() == "Integer") {
227  buffer << "int ";
228  } else {
229  buffer << "double ";
230  }
231  buffer << prefix << name();
232  if (isArray()) {
233  buffer << "[" << size() << "]";
234  }
235  buffer << ";";
236  return buffer.str();
237 }
238 
240 {
241  stringstream buffer;
242  if (hasAssignment() || hasStartModifier() || hasEachModifier()) {
243  auto range = Range(*this);
244  Expression ex(exp());
245  Expression var = Utils::instance().variableExpression(name(), range);
246  if (hasEachModifier()) {
247  buffer << range;
248  buffer << TAB << TAB << var << " = " << ex << ";" << endl;
249  buffer << range.end();
250  } else if (hasAssignment() || hasStartModifier()) {
251  buffer << var << " = " << ex << ";";
252  }
253  }
254  return buffer.str();
255 }
256 
257 /* TypeSymbolTable_ class.*/
258 
260 {
261  insert("String", new Type_String_());
262  insert("Real", new Type_Real_());
263  insert("Integer", new Type_Integer_());
264  insert("Boolean", new Type_Boolean_());
265 }
266 
267 /* VarSymbolTable class */
268 
269 VarSymbolTable::VarSymbolTable() : _parameters(false), _max_dims(1) {}
270 
272 {
273  Variable t(ty["Real"].get(), 0, nullptr, nullptr, vector<int>(1, 0), false);
274  t.setBuiltIn();
275  t.setName("time");
276  insert("time", t);
277  Variable reinit(ty["Real"].get(), 0, nullptr, nullptr, vector<int>(1, 0), false);
278  reinit.setBuiltIn();
279  reinit.setName("reinit");
280  insert("reinit", reinit);
281  Variable terminate(ty["Real"].get(), 0, nullptr, nullptr, vector<int>(1, 0), false);
282  terminate.setBuiltIn();
283  terminate.setName("terminate");
284  insert("terminate", terminate);
285  Variable chain_rule(ty["Real"].get(), TP_LOCAL, nullptr, nullptr, vector<int>(1, 0), false);
286  chain_rule.setBuiltIn();
287  chain_rule.setName("aux");
288  insert("aux", chain_rule);
289  Variable row(ty["Integer"].get(), TP_LOCAL, nullptr, nullptr, vector<int>(1, 0), false);
290  row.setBuiltIn();
291  row.setName("row");
292  insert("row", row);
293 }
294 
295 void VarSymbolTable::insert(VarName name, Variable variable)
296 {
297  assert(name != "");
298  variable.setName(name);
300  if (variable.isParameter()) {
301  _parameters = true;
302  }
303  if (variable.dimensions() > _max_dims) {
304  _max_dims = variable.dimensions();
305  }
306 }
307 
308 Option<Variable> VarSymbolTable::lookup(const string &name) const
309 {
310  std::map<string, Variable> table = map();
311  Option<Variable> var = table[name];
312  if (var) {
313  return var;
314  }
315  return Option<Variable>();
316 }
317 
318 unsigned long VarSymbolTable::maxDim() const { return _max_dims; }
319 
320 } // namespace Util
321 } // namespace MicroModelica
MicroModelica::IR::Range
Definition: index.hpp:164
ModificationType
ModificationType
Definition: ast_types.hpp:189
ast_builder.hpp
MicroModelica::IR::operator<<
std::ostream & operator<<(std::ostream &out, const Equation &e)
Definition: equation.cpp:185
MicroModelica::Util::Variable::initialization
std::string initialization()
Definition: symbol_table.cpp:256
MicroModelica::Util::Variable
Definition: symbol_table.hpp:75
MicroModelica::Util::Variable::_size
vector< int > _size
Definition: symbol_table.hpp:166
expression.hpp
MicroModelica::Util::Variable::operator=
Variable & operator=(const Variable &other)
Definition: symbol_table.cpp:120
MicroModelica::Util::Variable::size
unsigned int size()
Definition: symbol_table.cpp:192
MicroModelica::Util::Variable::operator==
bool operator==(const Variable &other)
Definition: symbol_table.cpp:141
MicroModelica::Util::Variable::_exp
AST_Expression _exp
Definition: symbol_table.hpp:168
MicroModelica::Util::Variable::_hasAssigment
bool _hasAssigment
Definition: symbol_table.hpp:171
TP_LOCAL
@ TP_LOCAL
Definition: ast_types.hpp:203
MicroModelica::Util::Variable::_comm
AST_Comment _comm
Definition: symbol_table.hpp:164
MicroModelica::Util::Variable::_builtin
bool _builtin
Definition: symbol_table.hpp:165
MicroModelica::Util::VarSymbolTable::lookup
Option< Variable > lookup(const std::string &name) const
Definition: symbol_table.cpp:325
symbol_table.hpp
MicroModelica::Util::Variable::isDiscreteInteger
bool isDiscreteInteger() const
Definition: symbol_table.cpp:223
MicroModelica::Util::Variable::_m
AST_Modification _m
Definition: symbol_table.hpp:163
MicroModelica::Util::Variable::castOperator
std::string castOperator() const
Definition: symbol_table.cpp:225
TP_CONSTANT
@ TP_CONSTANT
Definition: ast_types.hpp:200
helpers.hpp
Option
Definition: util_types.hpp:32
MicroModelica::Util::Variable::print
std::string print() const
Definition: symbol_table.cpp:210
MicroModelica::IR::EQUATION::Type
Type
Definition: equation.hpp:50
MicroModelica::Util::VarSymbolTable::initialize
void initialize(TypeSymbolTable tst)
Definition: symbol_table.cpp:288
MicroModelica::Util::Variable::_t
Type _t
Definition: symbol_table.hpp:161
MicroModelica::Util::Variable::isParameter
bool isParameter() const
Definition: symbol_table.hpp:103
MicroModelica::Util::Variable::_offset
int _offset
Definition: symbol_table.hpp:175
MicroModelica::IR::Expression
Definition: expression.hpp:64
MicroModelica::Util::Variable::_name
string _name
Definition: symbol_table.hpp:172
MicroModelica::Util::Variable::Variable
Variable()
Definition: symbol_table.cpp:55
ModelTable< VarName, Variable >::map
std::map< VarName, Variable > map() const
Definition: table.hpp:122
Type_Real_
Definition: type.hpp:58
MicroModelica::Util::Variable::declaration
std::string declaration(std::string prefix="")
Definition: symbol_table.cpp:240
MicroModelica::Util::Variable::_hasStart
bool _hasStart
Definition: symbol_table.hpp:169
AST_TypePrefix
int AST_TypePrefix
Definition: ast_types.hpp:50
VarName
std::string VarName
Definition: util_types.hpp:43
equation.hpp
MicroModelica::Util::Variable::operator!=
bool operator!=(const Variable &other)
Definition: symbol_table.cpp:143
SymbolType::TYINTEGER
@ TYINTEGER
MicroModelica::Util::Variable::_isArray
bool _isArray
Definition: symbol_table.hpp:173
TAB
#define TAB
Definition: util.hpp:79
MicroModelica::Util::Variable::setName
void setName(string name)
Definition: symbol_table.cpp:190
EXPNULL
@ EXPNULL
Definition: ast_types.hpp:168
MODEQUAL
@ MODEQUAL
Definition: ast_types.hpp:189
MicroModelica::Util::TypeSymbolTable
Definition: symbol_table.hpp:179
MicroModelica::Util::VarSymbolTable::_max_dims
unsigned long _max_dims
Definition: symbol_table.hpp:196
MicroModelica::Util::Variable::rowSize
unsigned int rowSize(unsigned int dim) const
Definition: symbol_table.cpp:201
AR_MODIFICATION
@ AR_MODIFICATION
Definition: ast_types.hpp:206
MicroModelica::Util::VarSymbolTable::insert
void insert(VarName name, Variable variable)
Definition: symbol_table.cpp:312
MicroModelica::Util::Variable::_hasOffset
bool _hasOffset
Definition: symbol_table.hpp:174
MicroModelica::Util::Utils::variableExpression
IR::Expression variableExpression(string name, Option< IR::Range > range)
Definition: util.cpp:384
MicroModelica::Util::Variable::_realType
RealType _realType
Definition: symbol_table.hpp:176
Type_String_
Definition: type.hpp:88
MicroModelica
Definition: files.cpp:45
MicroModelica::Util::Variable::_value
int _value
Definition: symbol_table.hpp:167
MODCLASS
@ MODCLASS
Definition: ast_types.hpp:189
MicroModelica::Util::Utils::instance
static Utils & instance()
Definition: util.hpp:83
MicroModelica::Util::Variable::dimensions
unsigned long dimensions() const
Definition: symbol_table.hpp:135
MicroModelica::Util::Variable::_hasEach
bool _hasEach
Definition: symbol_table.hpp:170
ModelTable::insert
void insert(Key k, Value v)
Definition: table.hpp:48
MicroModelica::Util::Variable::_tp
AST_TypePrefix _tp
Definition: symbol_table.hpp:162
MicroModelica::Util::VarSymbolTable::_parameters
bool _parameters
Definition: symbol_table.hpp:195
MicroModelica::Util::VarSymbolTable::VarSymbolTable
VarSymbolTable()
Definition: symbol_table.cpp:286
MicroModelica::Util::TypeSymbolTable::TypeSymbolTable
TypeSymbolTable()
Definition: symbol_table.cpp:276
MicroModelica::Util::Variable::processModification
void processModification()
Process the argument modification to determine the variable modifiers if any.
Definition: symbol_table.cpp:151
current_element
#define current_element(it)
Definition: ast_types.hpp:34
util.hpp
Type_Integer_
Definition: type.hpp:69
MicroModelica::Util::VarSymbolTable::maxDim
unsigned long maxDim() const
Definition: symbol_table.cpp:335
expression.hpp
Type_Boolean_
Definition: type.hpp:80