MicroModelicaCCompiler  4.5.3
element.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 "element.hpp"
21 
22 #include <list>
23 #include <sstream>
24 
25 #include "class.hpp"
26 #include "modification.hpp"
27 #include "../util/ast_util.hpp"
28 
29 using namespace std;
30 
31 /* Element Class */
32 
33 GET_AS_IMP(Element, ClassWrapper);
34 GET_AS_IMP(Element, Component);
35 GET_AS_IMP(Element, ExtendsClause);
36 GET_AS_IMP(Element, ImportClause);
37 
38 ostream &operator<<(ostream &os, const AST_Element &e)
39 {
40  os << *e;
41  return os;
42 }
43 
44 ostream &operator<<(ostream &os, const AST_Element_ &e)
45 {
46  os << e.print();
47  return os;
48 }
49 
51 
52 void AST_Element_::accept(AST_Visitor *visitor) { visitor->visit(this); }
53 
54 /* Element Class Wrapper Class */
55 
57 
59 {
60  stringstream ret(stringstream::out);
61  if (_c == nullptr)
62  ret << "CLASS[nullptr]";
63  else
64  ret << (*_c);
65  return ret.str();
66 }
67 
69 
70 AST_Class AST_Element_ClassWrapper_::getClass() { return _c; }
71 
72 /* Element Component Class */
73 
74 AST_Element_Component_::AST_Element_Component_(AST_DeclarationList decl_list, string type, AST_TypePrefix tp, AST_ExpressionList indexes)
75  : _decl_list(decl_list), _type(type), _indexes(indexes), _origin(nullptr), _tp(tp)
76 {
77 }
78 
80 {
81  stringstream ret(stringstream::out);
82  AST_DeclarationListIterator it, itt;
83  MAKE_SPACE;
84  if (isParameter()) {
85  ret << "parameter ";
86  }
87  if (isInput()) {
88  ret << "input ";
89  }
90  if (isOutput()) {
91  ret << "output ";
92  }
93  if (isDiscrete()) {
94  ret << "discrete ";
95  }
96  if (isFlow()) {
97  ret << "flow ";
98  }
99  if (isConstant()) {
100  ret << "constant ";
101  }
102  if (isStream()) {
103  ret << "stream ";
104  }
105  ret << _type;
106  AST_ExpressionListIterator exp_it;
107  if (indexes()->size()) {
108  ret << "[";
109  int size = indexes()->size(), i = 0;
110  foreach (exp_it, indexes()) {
111  i++;
112  ret << current_element(it);
113  if (i < size) ret << ",";
114  }
115  ret << "]";
116  }
117  ret << " ";
118  for (it = _decl_list->begin(); it != _decl_list->end(); it++) {
119  itt = it;
120  itt++;
121  ret << current_element(it) << (itt == _decl_list->end() ? "" : ",");
122  }
123  ret << ";";
124  return ret.str();
125 }
126 
127 ostream &operator<<(ostream &os, const AST_Element_Component &c)
128 {
129  os << *c;
130  return os;
131 }
132 
133 bool AST_Element_Component_::hasIndexes() { return !_indexes->empty(); }
134 
135 /* Extends Clause Class */
136 
138 
140 {
141  stringstream ret(stringstream::out);
142  ret << "EXTENDS " << _name;
143  return ret.str();
144 }
145 
147 
149 
150 /* Import Clause Class */
151 
153 
155 {
156  stringstream ret(stringstream::out);
157  ret << "IMPORT[" << _name << "]";
158  return ret.str();
159 }
160 
161 string AST_Element_ImportClause_::name() const { return _name; }
162 
164 
165 /* Comment Class */
166 
167 AST_Comment_::AST_Comment_(AST_String st, AST_ArgumentList al) : _st(st), _al(al) {}
168 
169 AST_ArgumentList AST_Comment_::arguments() const { return _al; }
170 
172 
173 void AST_Comment_::setAnnotation(AST_ArgumentList al) { _al = al; }
174 
175 ostream &operator<<(ostream &os, const AST_Comment_ &c)
176 {
177  if (c.string()) {
178  os << " \"" << c.string() << "\" ";
179  }
180  if (c.arguments()->size()) {
181  AST_ListPrint(c.arguments(), os, " annotation", ",", "(", ")");
182  }
183  return os;
184 }
185 
186 ostream &operator<<(ostream &os, const AST_Comment &c)
187 {
188  if (c != nullptr) {
189  os << *c;
190  }
191  return os;
192 }
193 
194 void AST_Comment_::accept(AST_Visitor *visitor) { visitor->visit(this); }
195 
196 /* Declaration Class */
197 
198 AST_Declaration_::AST_Declaration_(string name, AST_ExpressionList indexes, AST_Modification m)
199  : _name(name), _indexes(indexes), _mod(m), _comm(nullptr)
200 {
201 }
202 
204 {
205  stringstream ret(stringstream::out);
206  AST_ExpressionListIterator it;
207  ret << name();
208  if (indexes()->size()) {
209  ret << "[";
210  int size = indexes()->size(), i = 0;
211  foreach (it, indexes()) {
212  i++;
213  ret << current_element(it);
214  if (i < size) ret << ",";
215  }
216  ret << "]";
217  }
218  if (modification() != nullptr) {
219  /*
220  if (modification()->modificationType()==MODEQUAL) {
221  ret << " = " << modification()->getAsEqual()->exp();
222  } else if (modification()->modificationType()==MODCLASS) {
223  AST_Expression e = modification()->getAsClass()->exp();
224  if (e!=nullptr && e->expressionType()!=EXPNULL)
225  ret << " = " << e;
226  }
227  */
228  ret << modification();
229  }
230  if (comment() != nullptr) ret << comment();
231  return ret.str();
232 }
233 
234 string AST_Declaration_::name() const { return _name; }
235 
236 AST_ExpressionList AST_Declaration_::indexes() const { return _indexes; }
237 
239 {
240  if (_indexes != nullptr) {
241  if (AST_Length(_indexes) > 0) {
242  return true;
243  }
244  }
245  return false;
246 }
247 
248 AST_Modification AST_Declaration_::modification() const { return _mod; }
249 
250 bool AST_Declaration_::hasModification() { return _mod != nullptr; }
251 
252 void AST_Declaration_::setComment(AST_Comment c) { _comm = c; }
253 
254 AST_Comment AST_Declaration_::comment() const { return _comm; }
255 
256 /* Component */
257 
259 
260 bool AST_Element_Component_::isInput() const { return _tp & TP_INPUT; }
261 
263 
265 
266 bool AST_Element_Component_::isFlow() const { return _tp & TP_FLOW; }
267 
269 
271 
272 string AST_Element_Component_::type() { return _type; };
273 
275 
276 string AST_Element_Component_::name() { return _decl_list->front()->name(); }
277 
278 AST_DeclarationList AST_Element_Component_::nameList() { return _decl_list; }
279 
281 
283 {
284  if (_origin == nullptr) _origin = c;
285 }
286 
288 
289 AST_ExpressionList AST_Element_Component_::indexes() const { return _indexes; }
290 
291 bool AST_Element_Component_::isReal() { return _type == "Real"; }
292 
293 bool AST_Element_Component_::isInteger() { return _type == "Integer"; }
294 
295 bool AST_Element_Component_::isString() { return _type == "String"; }
AST_Element_Component_::_indexes
AST_ExpressionList _indexes
Definition: element.hpp:110
AST_Declaration_::modification
AST_Modification modification() const
Definition: element.cpp:248
MicroModelica::Generator::MODEL_INSTANCE::Component
Component
Definition: model_instance.hpp:89
AST_Element_::elementType
virtual ElementType elementType()
Definition: element.cpp:50
AST_Declaration_::hasModification
bool hasModification()
Definition: element.cpp:250
AST_Element_Component_::setOrigin
void setOrigin(AST_Class c)
Definition: element.cpp:282
AST_Element_Component_::origin
AST_Class origin()
Definition: element.cpp:280
AST_Element_ExtendsClause_::AST_Element_ExtendsClause_
AST_Element_ExtendsClause_(string name)
Definition: element.cpp:137
AST_Element_ClassWrapper_::print
string print() const
Definition: element.cpp:58
AST_Element_ImportClause_::print
string print() const
Definition: element.cpp:154
AST_Element_
Definition: element.hpp:30
AST_ListPrint
void AST_ListPrint(list< T1 > *l1, ostream &ret, string sec_name="", string separator=" ", string opener="", string closer="", bool block=false)
Definition: ast_types.hpp:319
AST_Declaration_::_mod
AST_Modification _mod
Definition: element.hpp:79
AST_Element_ImportClause_::_name
string _name
Definition: element.hpp:61
AST_Comment_::AST_Comment_
AST_Comment_(AST_String st, AST_ArgumentList al)
Definition: element.cpp:167
AST_Declaration_::_comm
AST_Comment _comm
Definition: element.hpp:80
AST_Comment_::_st
AST_String _st
Definition: element.hpp:136
AST_Element_Component_::isInput
bool isInput() const
Definition: element.cpp:260
TP_FLOW
@ TP_FLOW
Definition: ast_types.hpp:198
AST_Element_::print
virtual string print() const =0
AST_Element_Component_::_type
string _type
Definition: element.hpp:109
GET_AS_IMP
GET_AS_IMP(Element, ClassWrapper)
AST_Element_Component_::nameList
AST_DeclarationList nameList()
Definition: element.cpp:278
ELCLASS
@ ELCLASS
Definition: ast_types.hpp:159
AST_Element_ExtendsClause_::elementType
virtual ElementType elementType()
Definition: element.cpp:146
AST_Declaration_::hasIndexes
bool hasIndexes()
Definition: element.cpp:238
ELNONE
@ ELNONE
Definition: ast_types.hpp:159
AST_Comment_::setAnnotation
void setAnnotation(AST_ArgumentList al)
Definition: element.cpp:173
AST_Element_Component_::isOutput
bool isOutput() const
Definition: element.cpp:262
element.hpp
AST_Element_ExtendsClause_::_name
string _name
Definition: element.hpp:50
AST_Element_Component_::type
string type()
Definition: element.cpp:272
AST_Element_Component_::isParameter
bool isParameter() const
Definition: element.cpp:258
TP_CONSTANT
@ TP_CONSTANT
Definition: ast_types.hpp:200
operator<<
ostream & operator<<(ostream &os, const AST_Element &e)
Definition: element.cpp:38
AST_Element_Component_::isDiscrete
bool isDiscrete() const
Definition: element.cpp:264
AST_String
string * AST_String
Definition: ast_types.hpp:46
AST_Element_Component_::isConstant
bool isConstant() const
Definition: element.cpp:270
AST_Element_ClassWrapper_::elementType
virtual ElementType elementType()
Definition: element.cpp:68
AST_Class_
Definition: class.hpp:28
AST_Element_Component_::_origin
AST_Class _origin
Definition: element.hpp:111
EXTENDS
@ EXTENDS
Definition: ast_types.hpp:159
AST_Declaration_::name
string name() const
Definition: element.cpp:234
AST_Element_ImportClause_::elementType
virtual ElementType elementType()
Definition: element.cpp:163
AST_Declaration_::_name
string _name
Definition: element.hpp:77
AST_Element_Component_::_decl_list
AST_DeclarationList _decl_list
Definition: element.hpp:108
AST_Element_ClassWrapper_::AST_Element_ClassWrapper_
AST_Element_ClassWrapper_(AST_Class c)
Definition: element.cpp:56
MAKE_SPACE
#define MAKE_SPACE
Definition: ast_types.hpp:30
AST_Element_ClassWrapper_::_c
AST_Class _c
Definition: element.hpp:123
AST_Element_Component_::AST_Element_Component_
AST_Element_Component_(AST_DeclarationList decl_list, string type, AST_TypePrefix tp, AST_ExpressionList index)
Definition: element.cpp:74
TP_PARAMETER
@ TP_PARAMETER
Definition: ast_types.hpp:197
AST_Element_Component_::typePrefix
AST_TypePrefix typePrefix()
Definition: element.cpp:287
AST_Length
int AST_Length(list< T1 > *l1)
Definition: ast_types.hpp:292
AST_TypePrefix
int AST_TypePrefix
Definition: ast_types.hpp:50
AST_Element_Component_::isInteger
bool isInteger()
Definition: element.cpp:293
AST_Element_Component_::_tp
AST_TypePrefix _tp
Definition: element.hpp:112
AST_Element_Component_::isString
bool isString()
Definition: element.cpp:295
AST_Comment_
Definition: element.hpp:126
AST_Element_ImportClause_::name
string name() const
Definition: element.cpp:161
AST_Comment_::string
AST_String string() const
Definition: element.cpp:171
AST_Declaration_::comment
AST_Comment comment() const
Definition: element.cpp:254
AST_Declaration_::setComment
void setComment(AST_Comment c)
Definition: element.cpp:252
AST_Element_Component_::print
string print() const
Definition: element.cpp:79
AST_Element_Component_::elementType
virtual ElementType elementType()
Definition: element.cpp:274
TP_OUTPUT
@ TP_OUTPUT
Definition: ast_types.hpp:195
AST_Element_Component_::isStream
bool isStream() const
Definition: element.cpp:268
class.hpp
ElementType
ElementType
Definition: ast_types.hpp:159
AST_Element_Component_::name
string name()
Definition: element.cpp:276
modification.hpp
AST_Visitor::visit
virtual void visit(AST_Class x)=0
AST_Comment_::accept
void accept(AST_Visitor *visitor)
Definition: element.cpp:194
TP_STREAM
@ TP_STREAM
Definition: ast_types.hpp:199
AST_Declaration_::AST_Declaration_
AST_Declaration_(string name, AST_ExpressionList indexes, AST_Modification)
Definition: element.cpp:198
AST_Comment_::arguments
AST_ArgumentList arguments() const
Definition: element.cpp:169
TP_DISCRETE
@ TP_DISCRETE
Definition: ast_types.hpp:196
AST_Element_ExtendsClause_::print
string print() const
Definition: element.cpp:139
AST_Element_Component_::hasIndexes
bool hasIndexes()
Definition: element.cpp:133
AST_Element_Component_::indexes
AST_ExpressionList indexes() const
Definition: element.cpp:289
AST_Declaration_::_indexes
AST_ExpressionList _indexes
Definition: element.hpp:78
AST_Visitor
Definition: ast_util.hpp:224
AST_Declaration_::print
string print() const
Definition: element.cpp:203
AST_Element_::accept
void accept(AST_Visitor *visitor)
Definition: element.cpp:52
AST_Element_Component_::isReal
bool isReal()
Definition: element.cpp:291
AST_Element_ImportClause_::AST_Element_ImportClause_
AST_Element_ImportClause_(string name)
Definition: element.cpp:152
COMPONENT
@ COMPONENT
Definition: ast_types.hpp:159
IMPORT
@ IMPORT
Definition: ast_types.hpp:159
AST_Declaration_::indexes
AST_ExpressionList indexes() const
Definition: element.cpp:236
current_element
#define current_element(it)
Definition: ast_types.hpp:34
AST_Element_ClassWrapper_::getClass
AST_Class getClass()
Definition: element.cpp:70
AST_Element_Component_::isFlow
bool isFlow() const
Definition: element.cpp:266
AST_Comment_::_al
AST_ArgumentList _al
Definition: element.hpp:137
AST_Element_ExtendsClause_::name
AST_String name()
Definition: element.cpp:148
TP_INPUT
@ TP_INPUT
Definition: ast_types.hpp:194