MicroModelicaCCompiler  4.5.3
type.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 <sstream>
21 
22 #include "type.hpp"
23 
24 #include <ast/ast_builder.hpp>
25 
26 ostream &operator<<(ostream &os, const Type_ &e)
27 {
28  os << e.print();
29  return os;
30 }
31 
32 ostream &operator<<(ostream &os, const Type &e)
33 {
34  os << *e;
35  return os;
36 }
37 
38 string Type_Real_::print() const
39 {
40  stringstream ret(stringstream::out);
41  ret << "Real";
42  return ret.str();
43 }
44 
45 Type_Real newType_Real() { return new Type_Real_(); }
46 
47 void deleteType_Real(Type_Real m) { delete m; }
48 
49 string Type_Integer_::print() const
50 {
51  stringstream ret(stringstream::out);
52  ret << "Integer";
53  return ret.str();
54 }
55 
56 Type_Integer newType_Integer() { return new Type_Integer_(); }
57 
58 void deleteType_Integer(Type_Integer m) { delete m; }
59 
60 string Type_Boolean_::print() const
61 {
62  stringstream ret(stringstream::out);
63  ret << "Boolean";
64  return ret.str();
65 }
66 
67 string Type_String_::print() const
68 {
69  stringstream ret(stringstream::out);
70  ret << "String";
71  return ret.str();
72 }
73 
74 Type_String newType_String() { return new Type_String_(); }
75 
76 void deleteType_String(Type_String m) { delete m; }
77 
78 string Type_Array_::print() const
79 {
80  stringstream ret(stringstream::out);
81  AST_ExpressionList exls = newAST_ExpressionList();
82  Type tt = _t;
83  AST_ListPrepend(exls, _dim);
84  while (tt->getType() == SymbolType::TYARRAY) {
85  AST_ListPrepend(exls, tt->getAsArray()->dimension());
86  tt = tt->getAsArray()->arrayOf();
87  }
88 
89  ret << tt->print() << " [";
90  AST_ExpressionListIterator exit = exls->begin();
91  int s = exls->size();
92  for (int i = 0; i < s; i++) ret << current_element(exit++) << (i + 1 < s ? "," : "");
93 
94  ret << "]";
95  return ret.str();
96 }
97 
98 Type_Array_::Type_Array_(Type t, AST_Expression dim) : _t(t), _dim(dim){};
99 
101 
102 Type_Array Type_::getAsArray() { return dynamic_cast<Type_Array_ *>(this); }
103 
104 Type_Tupla Type_::getAsTupla() { return dynamic_cast<Type_Tupla_ *>(this); }
105 
106 Type_Function Type_::getAsFunction() { return dynamic_cast<Type_Function_ *>(this); }
107 
108 int operator==(Type_ &e1, Type_ &e2)
109 {
110  if (e1.getType() == e2.getType()) {
111  switch (e1.getType()) {
112  case SymbolType::TYARRAY:
113  return *(e1.getAsArray()->arrayOf()) == e2.getAsArray()->arrayOf();
114  case SymbolType::TYTUPLA: {
115  Type_Tupla t1 = e1.getAsTupla(), t2 = e2.getAsTupla();
116  if (t2->tupla()->size() != t1->tupla()->size()) return 0;
117  TypeListIterator it1 = t1->tupla()->begin(), it2 = t1->tupla()->begin();
118  foreach (it1, t1->tupla()) {
119  if (*current_element(it1) != current_element(it2)) return 0;
120  it2++;
121  }
122  return 1;
123  }
124  case SymbolType::TYFUNCTION: {
125  Type_Function f1 = e1.getAsFunction(), f2 = e2.getAsFunction();
126  return *(f1->output()) == f2->output();
127  }
128  default:
129  return 1;
130  }
131  } else
132  return 0;
133 }
134 
135 int operator==(Type_ &e1, Type e2) { return e1 == *e2; }
136 
137 int operator!=(Type_ &e1, Type_ &e2) { return !(e1 == e2); }
138 
139 int operator!=(Type_ &e1, Type e2) { return !(e1 == *e2); }
140 
141 Type_Tupla_::Type_Tupla_(TypeList tyl) : _tyl(tyl){};
142 
143 string Type_Tupla_::print() const
144 {
145  stringstream ret(stringstream::out);
146  TypeListIterator tyit;
147  unsigned long i = 0;
148  unsigned long s = _tyl->size();
149  ret << "< ";
150  foreach (tyit, _tyl) {
151  i++;
152  ret << current_element(tyit);
153  if (i < s) ret << " , ";
154  }
155  ret << " > ";
156  return ret.str();
157 }
158 
159 Type_Function_::Type_Function_(Type o, TypeList i) : _input(i), _output(o){};
160 
161 string Type_Function_::print() const
162 {
163  stringstream ret(stringstream::out);
164  TypeListIterator tyit;
165  unsigned long i = 0;
166  unsigned long s = _input->size();
167 
168  ret << _output << " function ";
169 
170  ret << "( ";
171  foreach (tyit, _input) {
172  i++;
173  ret << current_element(tyit);
174  if (i < s) ret << " , ";
175  }
176  ret << " ) ";
177  return ret.str();
178 }
ast_builder.hpp
SymbolType::TYTUPLA
@ TYTUPLA
Type_Array_::_dim
AST_Expression _dim
Definition: type.hpp:109
AST_ListPrepend
list< T1 > * AST_ListPrepend(list< T1 > *l, T1 e)
Definition: ast_types.hpp:247
SymbolType::TYFUNCTION
@ TYFUNCTION
Type_Array_::arrayOf
Type arrayOf()
Definition: type.cpp:100
Type_Boolean_::print
string print() const override
Definition: type.cpp:60
operator<<
ostream & operator<<(ostream &os, const Type_ &e)
Definition: type.cpp:26
Type_Real_::print
string print() const override
Definition: type.cpp:38
Type_Array_::Type_Array_
Type_Array_(Type t, AST_Expression dim)
Definition: type.cpp:98
Type_::print
virtual string print() const =0
Type_::getAsFunction
Type_Function getAsFunction()
Definition: type.cpp:106
MicroModelica::IR::EQUATION::Type
Type
Definition: equation.hpp:50
newType_Integer
Type_Integer newType_Integer()
Definition: type.cpp:56
deleteType_String
void deleteType_String(Type_String m)
Definition: type.cpp:76
Type_::getType
virtual SymbolType getType()=0
Type_Tupla_::print
string print() const override
Definition: type.cpp:143
newType_Real
Type_Real newType_Real()
Definition: type.cpp:45
deleteType_Real
void deleteType_Real(Type_Real m)
Definition: type.cpp:47
Type_::getAsArray
Type_Array getAsArray()
Definition: type.cpp:102
Type_Array_::_t
Type _t
Definition: type.hpp:105
Type_Function_::_output
Type _output
Definition: type.hpp:135
Type_Tupla_
Definition: type.hpp:112
Type_Real_
Definition: type.hpp:58
Type_Array_::print
string print() const override
Definition: type.cpp:78
type.hpp
Type_Tupla_::_tyl
TypeList _tyl
Definition: type.hpp:121
Type_String_::print
string print() const override
Definition: type.cpp:67
Type_Function_::_input
TypeList _input
Definition: type.hpp:131
operator==
int operator==(Type_ &e1, Type_ &e2)
Definition: type.cpp:108
Type_Array_
Definition: type.hpp:98
Type_Tupla_::Type_Tupla_
Type_Tupla_(TypeList tyl)
Definition: type.cpp:141
Type_
Definition: type.hpp:42
Type_Function_::Type_Function_
Type_Function_(Type output, TypeList input)
Definition: type.cpp:159
operator!=
int operator!=(Type_ &e1, Type_ &e2)
Definition: type.cpp:137
Type_String_
Definition: type.hpp:88
Type_Integer_::print
string print() const override
Definition: type.cpp:49
Type_::getAsTupla
Type_Tupla getAsTupla()
Definition: type.cpp:104
deleteType_Integer
void deleteType_Integer(Type_Integer m)
Definition: type.cpp:58
newAST_ExpressionList
AST_ExpressionList newAST_ExpressionList(AST_Expression e)
Definition: ast_builder.cpp:132
newType_String
Type_String newType_String()
Definition: type.cpp:74
SymbolType::TYARRAY
@ TYARRAY
current_element
#define current_element(it)
Definition: ast_types.hpp:34
Type_Function_
Definition: type.hpp:124
Type_Function_::print
string print() const override
Definition: type.cpp:161
Type_Integer_
Definition: type.hpp:69