This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
NFDimension.mo
251 lines (222 loc) · 6.67 KB
/
NFDimension.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated uniontype NFDimension
protected
import Dimension = NFDimension;
public
import Absyn.{Exp, Path, Subscript};
import Dump;
import NFClass.Class;
import Expression = NFExpression;
import NFInstNode.InstNode;
import Type = NFType;
import ComponentRef = NFComponentRef;
import NFPrefixes.Variability;
record RAW_DIM
Absyn.Subscript dim;
end RAW_DIM;
record UNTYPED
Expression dimension;
Boolean isProcessing;
end UNTYPED;
record INTEGER
Integer size;
end INTEGER;
record BOOLEAN
end BOOLEAN;
record ENUM
Type enumType;
end ENUM;
record EXP
Expression exp;
Variability var;
end EXP;
record UNKNOWN
end UNKNOWN;
function fromExp
input Expression exp;
input Variability var;
output Dimension dim;
algorithm
dim := match exp
local
Class cls;
ComponentRef cref;
Type ty;
case Expression.INTEGER() then INTEGER(exp.value);
case Expression.TYPENAME(ty = Type.ARRAY(elementType = ty))
then
match ty
case Type.BOOLEAN() then BOOLEAN();
case Type.ENUMERATION() then ENUM(ty);
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid typename", sourceInfo());
then
fail();
end match;
else Dimension.EXP(exp, var);
end match;
end fromExp;
function fromExpList
input list<Expression> expl;
output Dimension dim = INTEGER(listLength(expl));
end fromExpList;
function toDAE
input Dimension dim;
output DAE.Dimension daeDim;
algorithm
daeDim := match dim
local
Type ty;
case INTEGER() then DAE.DIM_INTEGER(dim.size);
case BOOLEAN() then DAE.DIM_BOOLEAN();
case ENUM(enumType = ty as Type.ENUMERATION())
then DAE.DIM_ENUM(ty.typePath, ty.literals, listLength(ty.literals));
case EXP() then DAE.DIM_EXP(Expression.toDAE(dim.exp));
case UNKNOWN() then DAE.DIM_UNKNOWN();
end match;
end toDAE;
function size
input Dimension dim;
output Integer size;
algorithm
size := match dim
local
Type ty;
case INTEGER() then dim.size;
case BOOLEAN() then 2;
case ENUM(enumType = ty as Type.ENUMERATION()) then listLength(ty.literals);
end match;
end size;
function isEqual
input Dimension dim1;
input Dimension dim2;
output Boolean isEqual;
algorithm
isEqual := match (dim1, dim2)
case (UNKNOWN(), _) then true;
case (_, UNKNOWN()) then true;
case (EXP(), _) then true;
case (_, EXP()) then true;
else Dimension.size(dim1) == Dimension.size(dim2);
end match;
end isEqual;
public function allEqual
input list<Dimension> dims1;
input list<Dimension> dims2;
output Boolean allEqual;
algorithm
allEqual := match(dims1, dims2)
local
Dimension dim1, dim2;
list<Dimension> rest1, rest2;
case ({}, {}) then true;
case (dim1::rest1, dim2::rest2) guard isEqual(dim1, dim2)
then allEqual(rest1, rest2);
else false;
end match;
end allEqual;
function isEqualKnown
input Dimension dim1;
input Dimension dim2;
output Boolean isEqual;
algorithm
isEqual := match (dim1, dim2)
case (UNKNOWN(), _) then false;
case (_, UNKNOWN()) then false;
case (EXP(), EXP()) then Expression.isEqual(dim1.exp, dim2.exp);
case (EXP(), _) then false;
case (_, EXP()) then false;
else Dimension.size(dim1) == Dimension.size(dim2);
end match;
end isEqualKnown;
function isKnown
input Dimension dim;
output Boolean known;
algorithm
known := match dim
case INTEGER() then true;
case BOOLEAN() then true;
case ENUM() then true;
else false;
end match;
end isKnown;
function subscriptType
"Returns the expected type of a subscript for the given dimension."
input Dimension dim;
output Type ty;
algorithm
ty := match dim
case INTEGER() then Type.INTEGER();
case BOOLEAN() then Type.BOOLEAN();
case ENUM() then dim.enumType;
case EXP() then Expression.typeOf(dim.exp);
else Type.UNKNOWN();
end match;
end subscriptType;
function toString
input Dimension dim;
output String str;
algorithm
str := match dim
local
Type ty;
case INTEGER() then String(dim.size);
case BOOLEAN() then "Boolean";
case ENUM(enumType = ty as Type.ENUMERATION()) then Absyn.pathString(ty.typePath);
case EXP() then Expression.toString(dim.exp);
case UNKNOWN() then ":";
case UNTYPED() then Expression.toString(dim.dimension);
end match;
end toString;
function sizeExp
"Returns the size of a dimension as an expression."
input Dimension dim;
input ComponentRef cref;
input Integer index;
output Expression sizeExp;
algorithm
sizeExp := match dim
local
Type ty;
case INTEGER() then Expression.INTEGER(dim.size);
case BOOLEAN() then Expression.BOOLEAN(true);
case ENUM(enumType = ty as Type.ENUMERATION())
then Expression.makeEnumLiteral(ty, listLength(ty.literals));
case EXP() then dim.exp;
case UNKNOWN()
then Expression.SIZE(Expression.CREF(Type.INTEGER(), ComponentRef.stripSubscripts(cref)),
SOME(Expression.INTEGER(index)));
end match;
end sizeExp;
annotation(__OpenModelica_Interface="frontend");
end NFDimension;