MicroModelicaCCompiler  4.5.3
symbol_table.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 <functional>
23 #include <iostream>
24 #include <list>
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 #include <ast/ast_types.hpp>
30 #include <ast/element.hpp>
31 #include <ast/modification.hpp>
32 #include <util/type.hpp>
33 #include <util/table.hpp>
34 
35 namespace MicroModelica {
36 
37 namespace Util {
38 
39 class VarSymbolTable;
40 
41 class Variable {
42  public:
43  Variable();
44  Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c);
45  Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector<int>& s, bool array);
46  Variable& operator=(const Variable& other);
47  bool operator==(const Variable& other);
48  bool operator!=(const Variable& other);
49 
50  typedef enum { State, Algebraic, NotAssigned } RealType;
51 
52  inline void setRealType(RealType type) { _realType = type; };
53  inline AST_TypePrefix typePrefix() const { return _tp; };
54  inline AST_Comment comment() { return _comm; };
55  inline void setComment(AST_Comment c) { _comm = c; };
56  inline AST_Modification modification() { return _m; };
57  inline void setModification(AST_Modification m)
58  {
59  _m = m;
61  };
62  inline Type type() { return _t; };
63  inline void setType(Type t) { _t = t; };
64  inline void setParameter()
65  {
66  _tp = TP_PARAMETER;
68  };
69  inline bool isParameter() const { return _tp & TP_PARAMETER; };
70  inline bool isDiscrete() const { return (_tp & TP_DISCRETE); };
71  inline bool builtIn() const { return _builtin; };
72  inline void setBuiltIn() { _builtin = true; };
73  inline bool isConstant() const { return _tp & TP_CONSTANT; };
74  inline bool isInput() const { return _tp & TP_INPUT; };
75  inline bool isOutput() const { return _tp & TP_OUTPUT; };
76  inline bool isForType() const { return _tp & TP_FOR; };
77  inline bool isEqType() const { return _tp & TP_EQ; };
78  inline bool isLocal() const { return _tp & TP_LOCAL; };
79  inline bool isState() const { return _realType == State; };
80  inline bool isString() const { return _t->getType() == SymbolType::TYSTRING; };
81  inline void setState() { unsetAssignment(); };
82  inline bool isUnknown() const { return _unknown; };
83  inline void setUnknown() { _unknown = true; };
84  inline bool isTime() const { return _name.compare("time") == 0; };
85  inline bool isAlgebraic() const { return _realType == Algebraic; };
86  inline void setValue(int val) { _value = val; };
87  inline int value() const { return _value; };
88  unsigned int size();
89  inline bool hasAssignment() const { return _hasAssigment; };
90  inline bool hasStartModifier() const { return _hasStart; };
91  inline bool hasEachModifier() const { return _hasEach; };
92  inline void setEachModifier(bool each) { _hasEach = each; };
93  inline string name() const { return _name; };
94  void setName(string name);
95  inline AST_Expression exp() { return _exp; };
96  inline bool isArray() const { return _isArray; };
97  inline bool isScalar() const { return !isArray(); };
98  friend ostream& operator<<(ostream& os, const Variable& e);
99  inline unsigned int size(int dim) const { return _size[dim]; };
100  unsigned int rowSize(unsigned int dim) const;
101  inline unsigned long dimensions() const { return _size.size(); };
102  std::string declaration(std::string prefix = "");
103  std::string initialization();
104  inline bool hasOffset() const { return _hasOffset; };
105  inline void setOffset(int offset)
106  {
108  _hasOffset = true;
109  };
110  inline int offset() const { return _offset; };
111  inline bool isModelVar() const { return isState() || isDiscrete() || isAlgebraic() || isParameter() || isEqType() || isOutput(); };
112  std::string print() const;
113  std::string castOperator() const;
114  friend std::ostream& operator<<(std::ostream& out, const Variable& v);
115 
116  protected:
118  void unsetAssignment() { _hasAssigment = false; };
119  inline void unsetStartEach()
120  {
121  _hasEach = false;
122  _hasStart = false;
123  };
124  bool isDiscreteInteger() const;
125 
126  bool _unknown;
129  AST_Modification _m;
130  AST_Comment _comm;
131  bool _builtin;
132  vector<int> _size;
133  int _value;
134  AST_Expression _exp;
135  bool _hasStart;
136  bool _hasEach;
137  bool _hasAssigment;
138  string _name;
139  bool _isArray;
140  bool _hasOffset;
141  int _offset;
143 };
144 
145 class TypeSymbolTable : public ModelTable<TypeName, Type> {
146  public:
147  TypeSymbolTable();
148 };
149 
150 class VarSymbolTable : public ModelTable<VarName, Variable> {
151  public:
153  ~VarSymbolTable() = default;
154  void initialize(TypeSymbolTable tst);
155  void insert(VarName name, Variable variable);
156  inline bool parameters() const { return _parameters; };
157  Option<Variable> lookup(const std::string& name) const;
158  unsigned long maxDim() const;
159 
160  private:
162  unsigned long _max_dims;
163 };
164 
165 using VariableList = std::list<Variable>;
166 
167 } // namespace Util
168 } // namespace MicroModelica
MicroModelica::Util::Variable::operator<<
friend ostream & operator<<(ostream &os, const Variable &e)
Definition: symbol_table.cpp:234
ModelTable
Definition: table.hpp:27
MicroModelica::Util::Variable::hasStartModifier
bool hasStartModifier() const
Definition: symbol_table.hpp:124
MicroModelica::Util::Variable::setRealType
void setRealType(RealType type)
Definition: symbol_table.hpp:86
MicroModelica::Util::Variable::initialization
std::string initialization()
Definition: symbol_table.cpp:256
MicroModelica::Util::VarSymbolTable
Definition: symbol_table.hpp:184
MicroModelica::Util::Variable
Definition: symbol_table.hpp:75
MicroModelica::Util::Variable::setValue
void setValue(int val)
Definition: symbol_table.hpp:120
MicroModelica::Util::Variable::setModification
void setModification(AST_Modification m)
Definition: symbol_table.hpp:91
MicroModelica::Util::Variable::unsetAssignment
void unsetAssignment()
Definition: symbol_table.hpp:152
MicroModelica::Util::Variable::_size
vector< int > _size
Definition: symbol_table.hpp:166
TP_EQ
@ TP_EQ
Definition: ast_types.hpp:202
MicroModelica::Util::Variable::isForType
bool isForType() const
Definition: symbol_table.hpp:110
MicroModelica::Util::Variable::Algebraic
@ Algebraic
Definition: symbol_table.hpp:84
MicroModelica::Util::Variable::isUnknown
bool isUnknown() const
Definition: symbol_table.hpp:116
MicroModelica::Util::Variable::_unknown
bool _unknown
Definition: symbol_table.hpp:160
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::isString
bool isString() const
Definition: symbol_table.hpp:114
MicroModelica::Util::Variable::isTime
bool isTime() const
Definition: symbol_table.hpp:118
MicroModelica::Util::Variable::hasOffset
bool hasOffset() const
Definition: symbol_table.hpp:138
MicroModelica::Util::Variable::_exp
AST_Expression _exp
Definition: symbol_table.hpp:168
MicroModelica::Util::Variable::isOutput
bool isOutput() const
Definition: symbol_table.hpp:109
MicroModelica::Util::Variable::_hasAssigment
bool _hasAssigment
Definition: symbol_table.hpp:171
SymbolType::TYSTRING
@ TYSTRING
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
MicroModelica::Util::Variable::State
@ State
Definition: symbol_table.hpp:84
MicroModelica::Util::Variable::comment
AST_Comment comment()
Definition: symbol_table.hpp:88
MicroModelica::Util::Variable::setEachModifier
void setEachModifier(bool each)
Definition: symbol_table.hpp:126
MicroModelica::Util::Variable::isDiscreteInteger
bool isDiscreteInteger() const
Definition: symbol_table.cpp:223
element.hpp
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
MicroModelica::Util::Variable::setComment
void setComment(AST_Comment c)
Definition: symbol_table.hpp:89
TP_CONSTANT
@ TP_CONSTANT
Definition: ast_types.hpp:200
Option
Definition: util_types.hpp:32
MicroModelica::Util::Variable::print
std::string print() const
Definition: symbol_table.cpp:210
MicroModelica::Util::Variable::name
string name() const
Definition: symbol_table.hpp:127
MicroModelica::Util::Variable::isArray
bool isArray() const
Definition: symbol_table.hpp:130
MicroModelica::IR::EQUATION::Type
Type
Definition: equation.hpp:50
MicroModelica::Util::Variable::offset
int offset() const
Definition: symbol_table.hpp:144
MicroModelica::Util::Variable::isEqType
bool isEqType() const
Definition: symbol_table.hpp:111
MicroModelica::Util::Variable::RealType
RealType
Definition: symbol_table.hpp:84
MicroModelica::Util::VarSymbolTable::initialize
void initialize(TypeSymbolTable tst)
Definition: symbol_table.cpp:288
MicroModelica::Util::Variable::setOffset
void setOffset(int offset)
Definition: symbol_table.hpp:139
MicroModelica::Util::Variable::_t
Type _t
Definition: symbol_table.hpp:161
MicroModelica::Util::Variable::setUnknown
void setUnknown()
Definition: symbol_table.hpp:117
MicroModelica::Util::Variable::isParameter
bool isParameter() const
Definition: symbol_table.hpp:103
MicroModelica::Util::Variable::isModelVar
bool isModelVar() const
Definition: symbol_table.hpp:145
MicroModelica::Util::Variable::isScalar
bool isScalar() const
Definition: symbol_table.hpp:131
MicroModelica::Util::Variable::setState
void setState()
Definition: symbol_table.hpp:115
MicroModelica::Util::Variable::_offset
int _offset
Definition: symbol_table.hpp:175
TP_PARAMETER
@ TP_PARAMETER
Definition: ast_types.hpp:197
MicroModelica::Util::Variable::_name
string _name
Definition: symbol_table.hpp:172
MicroModelica::Util::Variable::Variable
Variable()
Definition: symbol_table.cpp:55
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
TP_FOR
@ TP_FOR
Definition: ast_types.hpp:201
type.hpp
MicroModelica::Util::Variable::isAlgebraic
bool isAlgebraic() const
Definition: symbol_table.hpp:119
MicroModelica::Util::Variable::operator!=
bool operator!=(const Variable &other)
Definition: symbol_table.cpp:143
MicroModelica::Util::Variable::_isArray
bool _isArray
Definition: symbol_table.hpp:173
MicroModelica::Util::Variable::setName
void setName(string name)
Definition: symbol_table.cpp:190
MicroModelica::Util::Variable::isInput
bool isInput() const
Definition: symbol_table.hpp:108
MicroModelica::Util::Variable::hasAssignment
bool hasAssignment() const
Definition: symbol_table.hpp:123
MicroModelica::Util::Variable::setType
void setType(Type t)
Definition: symbol_table.hpp:97
MicroModelica::Util::TypeSymbolTable
Definition: symbol_table.hpp:179
MicroModelica::Util::Variable::isLocal
bool isLocal() const
Definition: symbol_table.hpp:112
MicroModelica::Util::VarSymbolTable::_max_dims
unsigned long _max_dims
Definition: symbol_table.hpp:196
TP_OUTPUT
@ TP_OUTPUT
Definition: ast_types.hpp:195
MicroModelica::Util::Variable::rowSize
unsigned int rowSize(unsigned int dim) const
Definition: symbol_table.cpp:201
MicroModelica::Util::Variable::exp
AST_Expression exp()
Definition: symbol_table.hpp:129
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::Variable::_realType
RealType _realType
Definition: symbol_table.hpp:176
MicroModelica::Util::Variable::isState
bool isState() const
Definition: symbol_table.hpp:113
modification.hpp
MicroModelica
Definition: files.cpp:45
MicroModelica::Util::Variable::type
Type type()
Definition: symbol_table.hpp:96
MicroModelica::Util::Variable::_value
int _value
Definition: symbol_table.hpp:167
MicroModelica::Util::Variable::isDiscrete
bool isDiscrete() const
Definition: symbol_table.hpp:104
table.hpp
ast_types.hpp
MicroModelica::Util::VarSymbolTable::~VarSymbolTable
~VarSymbolTable()=default
MicroModelica::Util::Variable::dimensions
unsigned long dimensions() const
Definition: symbol_table.hpp:135
MicroModelica::Util::Variable::_hasEach
bool _hasEach
Definition: symbol_table.hpp:170
MicroModelica::Util::Variable::modification
AST_Modification modification()
Definition: symbol_table.hpp:90
TP_DISCRETE
@ TP_DISCRETE
Definition: ast_types.hpp:196
MicroModelica::Util::VariableList
std::list< Variable > VariableList
Definition: symbol_table.hpp:199
MicroModelica::Util::Variable::setParameter
void setParameter()
Definition: symbol_table.hpp:98
MicroModelica::Util::Variable::value
int value() const
Definition: symbol_table.hpp:121
MicroModelica::Util::Variable::_tp
AST_TypePrefix _tp
Definition: symbol_table.hpp:162
MicroModelica::Util::VarSymbolTable::parameters
bool parameters() const
Definition: symbol_table.hpp:190
MicroModelica::Util::VarSymbolTable::_parameters
bool _parameters
Definition: symbol_table.hpp:195
MicroModelica::Util::Variable::typePrefix
AST_TypePrefix typePrefix() const
Definition: symbol_table.hpp:87
MicroModelica::Util::Variable::isConstant
bool isConstant() const
Definition: symbol_table.hpp:107
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
MicroModelica::Util::Variable::NotAssigned
@ NotAssigned
Definition: symbol_table.hpp:84
MicroModelica::Util::Variable::setBuiltIn
void setBuiltIn()
Definition: symbol_table.hpp:106
MicroModelica::Util::Variable::unsetStartEach
void unsetStartEach()
Definition: symbol_table.hpp:153
MicroModelica::Util::Variable::hasEachModifier
bool hasEachModifier() const
Definition: symbol_table.hpp:125
MicroModelica::Util::Variable::builtIn
bool builtIn() const
Definition: symbol_table.hpp:105
MicroModelica::Util::VarSymbolTable::maxDim
unsigned long maxDim() const
Definition: symbol_table.cpp:335
TP_INPUT
@ TP_INPUT
Definition: ast_types.hpp:194