/
Algorithm.mo
710 lines (663 loc) · 25.5 KB
/
Algorithm.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 package Algorithm
"
file: Algorithm.mo
package: Algorithm
description: Algorithm datatypes
RCS: $Id$
This file contains data types and functions for managing
algorithm sections. The algorithms in the AST are analyzed by the `Inst\'
module (Inst.mo) which uses this module to represent the algorithms. No
processing of any kind, except for building the datastructure is
done in this module.
It is used primarily by Inst.mo which both provides its input data
and uses its \"output\" data.
"
public import Absyn;
public import DAE;
public import SCode;
public
type Ident = String;
public type Algorithm = DAE.Algorithm;
public type Statement = DAE.Statement;
public type Else = DAE.Else;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Print;
protected import RTOpts;
protected import Types;
protected import Util;
public function algorithmEmpty "Returns true if algorithm is empty, i.e. no statements"
input Algorithm alg;
output Boolean empty;
algorithm
empty := matchcontinue(alg)
case(DAE.ALGORITHM_STMTS({})) then true;
case(_) then false;
end matchcontinue;
end algorithmEmpty;
public function isReinitStatement "returns true if statement is a reinit"
input Statement stmt;
output Boolean res;
algorithm
res := matchcontinue(stmt)
case(DAE.STMT_REINIT(var = _)) then true;
case(_) then false;
end matchcontinue;
end isReinitStatement;
public function isNotAssertStatement "returns true if statement is NOT an assert"
input Statement stmt;
output Boolean res;
algorithm
res := matchcontinue(stmt)
case(DAE.STMT_ASSERT(cond = _)) then false;
case(_) then true;
end matchcontinue;
end isNotAssertStatement;
public function splitReinits ""
input list<Algorithm> inAlgs;
output list<Algorithm> reinits;
output list<Statement> rest;
algorithm (reinits,rest) := matchcontinue(inAlgs)
local
Statement a;
list<Statement> al;
case({}) then ({},{});
case(DAE.ALGORITHM_STMTS(al as {a as DAE.STMT_REINIT(var = _)})::inAlgs)
equation
(reinits,rest) = splitReinits(inAlgs);
then
(DAE.ALGORITHM_STMTS({a})::reinits,rest);
case(DAE.ALGORITHM_STMTS(al as {a})::inAlgs)
equation
(reinits,rest) = splitReinits(inAlgs);
then
(reinits,a::rest);
case( DAE.ALGORITHM_STMTS((a::al)):: inAlgs )
equation
inAlgs = listAppend({DAE.ALGORITHM_STMTS({a}),DAE.ALGORITHM_STMTS(al)},inAlgs);
(reinits,rest) = splitReinits(inAlgs);
then
(reinits,rest);
end matchcontinue;
end splitReinits;
public function makeAssignment
"function: makeAssignment
This function creates an `DAE.STMT_ASSIGN\' construct, and checks that the
assignment is semantically valid, which means that the component
being assigned is not constant, and that the types match.
LS: Added call to getPropType and isPropAnyConst instead of
having PROP in the rules. Otherwise rules must be repeated because of
combinations with PROP_TUPLE"
input DAE.Exp inExp1;
input DAE.Properties inProperties2;
input DAE.Exp inExp3;
input DAE.Properties inProperties4;
input SCode.Accessibility inAccessibility5;
input SCode.Initial initial_;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inExp1,inProperties2,inExp3,inProperties4,inAccessibility5,initial_,source)
local
Ident lhs_str,rhs_str,lt_str,rt_str;
DAE.Exp lhs,rhs;
DAE.Properties lprop,rprop,lhprop,rhprop;
DAE.ComponentRef cr;
tuple<DAE.TType, Option<Absyn.Path>> lt,rt;
/* It is not allowed to assign to a constant */
case (lhs,lprop,rhs,rprop,_,initial_,source)
equation
DAE.C_CONST() = Types.propAnyConst(lprop);
lhs_str = ExpressionDump.printExpStr(lhs);
rhs_str = ExpressionDump.printExpStr(rhs);
Error.addSourceMessage(Error.ASSIGN_CONSTANT_ERROR, {lhs_str,rhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
/* assign to parameter in algorithm okay if record */
case ((lhs as DAE.CREF(componentRef=cr)),lhprop,rhs,rhprop,_,SCode.NON_INITIAL(),source)
equation
DAE.C_PARAM() = Types.propAnyConst(lhprop);
true = ComponentReference.isRecord(cr);
outStatement = makeAssignment2(lhs,lhprop,rhs,rhprop,source);
then outStatement;
/* assign to parameter in algorithm produce error */
case (lhs,lprop,rhs,rprop,_,SCode.NON_INITIAL(),source)
equation
DAE.C_PARAM() = Types.propAnyConst(lprop);
lhs_str = ExpressionDump.printExpStr(lhs);
rhs_str = ExpressionDump.printExpStr(rhs);
Error.addSourceMessage(Error.ASSIGN_PARAM_ERROR, {lhs_str,rhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
/* assignment to a constant, report error */
case (lhs,_,rhs,_,SCode.RO(),_,source)
equation
lhs_str = ExpressionDump.printExpStr(lhs);
rhs_str = ExpressionDump.printExpStr(rhs);
Error.addSourceMessage(Error.ASSIGN_READONLY_ERROR, {lhs_str,rhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
/* assignment to parameter ok in initial algorithm */
case (lhs,lhprop,rhs,rhprop,_,SCode.INITIAL(),source)
equation
DAE.C_PARAM() = Types.propAnyConst(lhprop);
outStatement = makeAssignment2(lhs,lhprop,rhs,rhprop,source);
then outStatement;
case (lhs,lhprop,rhs,rhprop,_,_,source)
equation
DAE.C_VAR() = Types.propAnyConst(lhprop);
outStatement = makeAssignment2(lhs,lhprop,rhs,rhprop,source);
then outStatement;
/* report an error */
case (lhs,lprop,rhs,rprop,_,_,source)
equation
lt = Types.getPropType(lprop);
rt = Types.getPropType(rprop);
false = Types.equivtypes(lt, rt);
lhs_str = ExpressionDump.printExpStr(lhs);
rhs_str = ExpressionDump.printExpStr(rhs);
lt_str = Types.unparseType(lt);
rt_str = Types.unparseType(rt);
Error.addSourceMessage(Error.ASSIGN_TYPE_MISMATCH_ERROR,
{lhs_str,rhs_str,lt_str,rt_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
/* failing */
case (lhs,lprop,rhs,rprop,_,_,source)
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- Algorithm.makeAssignment failed");
Debug.trace(" ");
Debug.trace(ExpressionDump.printExpStr(lhs));
Debug.trace(" := ");
Debug.traceln(ExpressionDump.printExpStr(rhs));
then
fail();
end matchcontinue;
end makeAssignment;
protected function makeAssignment2
"Help function to makeAssignment"
input DAE.Exp lhs;
input DAE.Properties lhprop;
input DAE.Exp rhs;
input DAE.Properties rhprop;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement := matchcontinue(lhs,lhprop,rhs,rhprop,source)
local
DAE.ComponentRef c;
DAE.ExpType crt,t;
DAE.Exp rhs_1,e3,e1;
DAE.Type ty;
list<DAE.Exp> ea2;
case (lhs as DAE.CREF(componentRef = c,ty = crt),lhprop,rhs,rhprop,source)
equation
(rhs_1,_) = Types.matchProp(rhs, rhprop, lhprop, true);
false = Types.isPropArray(lhprop);
t = getPropExpType(lhprop);
then
DAE.STMT_ASSIGN(t,lhs,rhs_1,source);
/* TODO: Use this when we have fixed states in BackendDAE .lower(...)
case (e1 as DAE.CALL(Absyn.IDENT("der"),{DAE.CREF(_,_)},_,_,_),lhprop,rhs,rhprop)
equation
(rhs_1,_) = Types.matchProp(rhs, rhprop, lhprop);
false = Types.isPropArray(lhprop);
t = getPropExpType(lhprop);
then
DAE.STMT_ASSIGN(t,e1,rhs_1);
*/
case (DAE.CREF(componentRef = c,ty = crt),lhprop,rhs,rhprop,source)
equation
(rhs_1,_) = Types.matchProp(rhs, rhprop, lhprop, false /* Don't duplicate errors */);
true = Types.isPropArray(lhprop);
ty = Types.getPropType(lhprop);
t = Types.elabType(ty);
then
DAE.STMT_ASSIGN_ARR(t,c,rhs_1,source);
case(e3 as DAE.ASUB(e1,ea2),lhprop,rhs,rhprop,source)
equation
(rhs_1,_) = Types.matchProp(rhs, rhprop, lhprop, true);
//false = Types.isPropArray(lhprop);
t = getPropExpType(lhprop);
then
DAE.STMT_ASSIGN(t,e3,rhs_1,source);
end matchcontinue;
end makeAssignment2;
public function makeAssignmentsList
input list<DAE.Exp> lhsExps;
input list<DAE.Properties> lhsProps;
input list<DAE.Exp> rhsExps;
input list<DAE.Properties> rhsProps;
input SCode.Accessibility accessibility;
input SCode.Initial initial_;
input DAE.ElementSource source;
output list<Statement> assignments;
algorithm
assignments := match(lhsExps, lhsProps, rhsExps, rhsProps, accessibility, initial_, source)
local
DAE.Exp lhs, rhs;
list<DAE.Exp> rest_lhs, rest_rhs;
DAE.Properties lhs_prop, rhs_prop;
list<DAE.Properties> rest_lhs_prop, rest_rhs_prop;
DAE.Statement ass;
list<DAE.Statement> rest_ass;
case ({}, {}, {}, {}, _, _, _) then {};
case (lhs :: rest_lhs, lhs_prop :: rest_lhs_prop,
rhs :: rest_rhs, rhs_prop :: rest_rhs_prop, _, _, _)
equation
ass = makeAssignment(lhs, lhs_prop, rhs, rhs_prop, accessibility, initial_, source);
rest_ass = makeAssignmentsList(rest_lhs, rest_lhs_prop, rest_rhs, rest_rhs_prop, accessibility, initial_, source);
then
ass :: rest_ass;
end match;
end makeAssignmentsList;
public function makeTupleAssignment "function: makeTupleAssignment
This function creates an `DAE.STMT_TUPLE_ASSIGN\' construct, and checks that the
assignment is semantically valid, which means that the component
being assigned is not constant, and that the types match."
input list<DAE.Exp> inExpExpLst;
input list<DAE.Properties> inTypesPropertiesLst;
input DAE.Exp inExp;
input DAE.Properties inProperties;
input SCode.Initial initial_;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement := matchcontinue (inExpExpLst,inTypesPropertiesLst,inExp,inProperties,initial_,source)
local
list<DAE.Const> bvals;
list<Ident> sl;
Ident s,lhs_str,rhs_str,str1,str2,strInitial;
list<DAE.Exp> lhs,expl;
list<DAE.Properties> lprop,lhprops;
DAE.Exp rhs;
DAE.Properties rprop;
list<tuple<DAE.TType, Option<Absyn.Path>>> lhrtypes,tpl;
list<DAE.TupleConst> clist;
case (lhs,lprop,rhs,rprop,initial_,source)
equation
bvals = Util.listMap(lprop, Types.propAnyConst);
DAE.C_CONST() = Util.listReduce(bvals, Types.constOr);
sl = Util.listMap(lhs, ExpressionDump.printExpStr);
s = Util.stringDelimitList(sl, ", ");
lhs_str = stringAppendList({"(",s,")"});
rhs_str = ExpressionDump.printExpStr(rhs);
Error.addSourceMessage(Error.ASSIGN_CONSTANT_ERROR, {lhs_str,rhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
case (lhs,lprop,rhs,rprop,SCode.NON_INITIAL(),source)
equation
bvals = Util.listMap(lprop, Types.propAnyConst);
DAE.C_PARAM() = Util.listReduce(bvals, Types.constOr);
sl = Util.listMap(lhs, ExpressionDump.printExpStr);
s = Util.stringDelimitList(sl, ", ");
lhs_str = stringAppendList({"(",s,")"});
rhs_str = ExpressionDump.printExpStr(rhs);
Error.addSourceMessage(Error.ASSIGN_PARAM_ERROR, {lhs_str,rhs_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
// a normal prop in rhs that contains a T_TUPLE!
case (expl,lhprops,rhs,DAE.PROP(type_ = (DAE.T_TUPLE(tupleType = tpl),_)),_,source)
equation
bvals = Util.listMap(lhprops, Types.propAnyConst);
DAE.C_VAR() = Util.listReduce(bvals, Types.constOr);
lhrtypes = Util.listMap(lhprops, Types.getPropType);
Types.matchTypeTupleCall(rhs, tpl, lhrtypes);
/* Don\'t use new rhs\', since type conversions of
several output args are not clearly defined. */
then
DAE.STMT_TUPLE_ASSIGN(DAE.ET_OTHER(),expl,rhs,source);
// a tuple in rhs
case (expl,lhprops,rhs,DAE.PROP_TUPLE(type_ = (DAE.T_TUPLE(tupleType = tpl),_),tupleConst = DAE.TUPLE_CONST(tupleConstLst = clist)),_,source)
equation
bvals = Util.listMap(lhprops, Types.propAnyConst);
DAE.C_VAR() = Util.listReduce(bvals, Types.constOr);
lhrtypes = Util.listMap(lhprops, Types.getPropType);
Types.matchTypeTupleCall(rhs, tpl, lhrtypes);
/* Don\'t use new rhs\', since type conversions of several output args are not clearly defined. */
then
DAE.STMT_TUPLE_ASSIGN(DAE.ET_OTHER(),expl,rhs,source);
case (lhs,lprop,rhs,rprop,initial_,source)
equation
true = RTOpts.debugFlag("failtrace");
sl = Util.listMap(lhs, ExpressionDump.printExpStr);
s = Util.stringDelimitList(sl, ", ");
lhs_str = stringAppendList({"(",s,")"});
rhs_str = ExpressionDump.printExpStr(rhs);
str1 = Util.stringDelimitList(Util.listMap(lprop, Types.printPropStr), ", ");
str2 = Types.printPropStr(rprop);
strInitial = SCode.printInitialStr(initial_);
Debug.traceln("- Algorithm.makeTupleAssignment failed on: \n\t" +&
lhs_str +& " = " +& rhs_str +&
"\n\tprops lhs: (" +& str1 +& ") = props rhs: " +& str2 +&
"\n\tin " +& strInitial +& " section");
then
fail();
end matchcontinue;
end makeTupleAssignment;
protected function getPropExpType "function: getPropExpType
Returns the expression type for a given Properties by calling
getTypeExpType. Used by makeAssignment."
input DAE.Properties p;
output DAE.ExpType t;
protected
tuple<DAE.TType, Option<Absyn.Path>> ty;
algorithm
ty := Types.getPropType(p);
t := getTypeExpType(ty);
end getPropExpType;
protected function getTypeExpType "function: getTypeExpType
Returns the expression type for a given Type module type. Used only by
getPropExpType."
input DAE.Type inType;
output DAE.ExpType outType;
algorithm
outType:=
matchcontinue (inType)
local tuple<DAE.TType, Option<Absyn.Path>> t;
case ((DAE.T_INTEGER(varLstInt = _),_)) then DAE.ET_INT();
case ((DAE.T_REAL(varLstReal = _),_)) then DAE.ET_REAL();
case ((DAE.T_STRING(varLstString = _),_)) then DAE.ET_STRING();
case ((DAE.T_BOOL(varLstBool = _),_)) then DAE.ET_BOOL();
case ((DAE.T_ARRAY(arrayType = t),_)) then getTypeExpType(t);
case ((DAE.T_COMPLEX(_,{},SOME(t),_),_))
then getTypeExpType(t);
case ((DAE.T_COMPLEX(_,_::_,_,_),_))
equation
// Commenting out this line because it prints a lot of warnings for
// record assignments (which actually work just fine). // sjoelund // 2009-05-07
//print("Warning complex_varList not implemented for Array_assign\n");
then fail();
case ((_,_)) then DAE.ET_OTHER(); /* was fail but records must be handled somehow */
end matchcontinue;
end getTypeExpType;
public function makeIf "function: makeIf
This function creates an `DAE.STMT_IF\' construct, checking that the types
of the parts are correct. Else part is generated using the makeElse
function."
input DAE.Exp inExp1;
input DAE.Properties inProperties2;
input list<Statement> inStatementLst3;
input list<tuple<DAE.Exp, DAE.Properties, list<Statement>>> inTplExpExpTypesPropertiesStatementLstLst4;
input list<Statement> inStatementLst5;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inExp1,inProperties2,inStatementLst3,inTplExpExpTypesPropertiesStatementLstLst4,inStatementLst5,source)
local
Else else_;
DAE.Exp e;
list<Statement> tb,fb;
list<tuple<DAE.Exp, DAE.Properties, list<Statement>>> eib;
Ident e_str,t_str;
tuple<DAE.TType, Option<Absyn.Path>> t;
case (e,DAE.PROP(type_ = t),tb,eib,fb,source)
equation
(e,_) = Types.matchType(e,t,DAE.T_BOOL_DEFAULT,true);
else_ = makeElse(eib, fb);
then
DAE.STMT_IF(e,tb,else_,source);
case (e,DAE.PROP(type_ = t),_,_,_,source)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addSourceMessage(Error.IF_CONDITION_TYPE_ERROR, {e_str,t_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
end matchcontinue;
end makeIf;
protected function makeElse "function: makeElse
This function creates the ELSE part of the DAE.STMT_IF and checks if is correct."
input list<tuple<DAE.Exp, DAE.Properties, list<Statement>>> inTplExpExpTypesPropertiesStatementLstLst;
input list<Statement> inStatementLst;
output Else outElse;
algorithm
outElse:=
matchcontinue (inTplExpExpTypesPropertiesStatementLstLst,inStatementLst)
local
list<Statement> fb,b;
Else else_;
DAE.Exp e;
list<tuple<DAE.Exp, DAE.Properties, list<Statement>>> xs;
Ident e_str,t_str;
tuple<DAE.TType, Option<Absyn.Path>> t;
case ({},{}) then DAE.NOELSE(); /* This removes empty else branches */
case ({},fb) then DAE.ELSE(fb);
case (((e,DAE.PROP(type_ = t),b) :: xs),fb)
equation
(e,_) = Types.matchType(e,t,DAE.T_BOOL_DEFAULT,true);
else_ = makeElse(xs, fb);
then
DAE.ELSEIF(e,b,else_);
case (((e,DAE.PROP(type_ = t),_) :: _),_)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addMessage(Error.IF_CONDITION_TYPE_ERROR, {e_str,t_str});
then
fail();
end matchcontinue;
end makeElse;
public function makeFor "function: makeFor
This function creates a DAE.STMT_FOR construct, checking
that the types of the parts are correct."
input Ident inIdent;
input DAE.Exp inExp;
input DAE.Properties inProperties;
input list<Statement> inStatementLst;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inIdent,inExp,inProperties,inStatementLst,source)
local
Boolean array;
DAE.ExpType et;
Ident i,e_str,t_str;
DAE.Exp e;
tuple<DAE.TType, Option<Absyn.Path>> t;
list<Statement> stmts;
case (i,e,DAE.PROP(type_ = (DAE.T_ARRAY(arrayType = t),_)),stmts,source)
equation
array = Types.isArray(t);
et = Types.elabType(t);
then
DAE.STMT_FOR(et,array,i,e,stmts,source);
case (_,e,DAE.PROP(type_ = t),_,source)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addSourceMessage(Error.FOR_EXPRESSION_TYPE_ERROR, {e_str,t_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
end matchcontinue;
end makeFor;
public function makeWhile "function: makeWhile
This function creates a DAE.STMT_WHILE construct, checking that the types
of the parts are correct."
input DAE.Exp inExp;
input DAE.Properties inProperties;
input list<Statement> inStatementLst;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inExp,inProperties,inStatementLst,source)
local
DAE.Exp e;
list<Statement> stmts;
Ident e_str,t_str;
tuple<DAE.TType, Option<Absyn.Path>> t;
case (e,DAE.PROP(type_ = (DAE.T_BOOL(varLstBool = _),_)),stmts,source) then DAE.STMT_WHILE(e,stmts,source);
case (e,DAE.PROP(type_ = t),_,source)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addSourceMessage(Error.WHILE_CONDITION_TYPE_ERROR, {e_str,t_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
end matchcontinue;
end makeWhile;
public function makeWhenA "function: makeWhenA
This function creates a DAE.STMT_WHEN algorithm construct,
checking that the types of the parts are correct."
input DAE.Exp inExp;
input DAE.Properties inProperties;
input list<Statement> inStatementLst;
input Option<Statement> elseWhenStmt;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inExp,inProperties,inStatementLst,elseWhenStmt,source)
local
DAE.Exp e;
list<Statement> stmts;
Option<Statement> elsew;
Ident e_str,t_str;
tuple<DAE.TType, Option<Absyn.Path>> t;
case (e,DAE.PROP(type_ = (DAE.T_BOOL(varLstBool = _),_)),stmts,elsew,source) then DAE.STMT_WHEN(e,stmts,elsew,{},source);
case (e,DAE.PROP(type_ = (DAE.T_ARRAY(arrayType = (DAE.T_BOOL(varLstBool = _),_)),_)),stmts,elsew,source) then DAE.STMT_WHEN(e,stmts,elsew,{},source);
case (e,DAE.PROP(type_ = t),_,_,source)
equation
e_str = ExpressionDump.printExpStr(e);
t_str = Types.unparseType(t);
Error.addSourceMessage(Error.WHEN_CONDITION_TYPE_ERROR, {e_str,t_str}, DAEUtil.getElementSourceFileInfo(source));
then
fail();
end matchcontinue;
end makeWhenA;
public function makeReinit "function: makeReinit
creates a reinit statement in an algorithm
statement, only valid in when algorithm sections."
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.Properties inProperties3;
input DAE.Properties inProperties4;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement:=
matchcontinue (inExp1,inExp2,inProperties3,inProperties4,source)
local DAE.Exp var,val,var_1,val_1; DAE.Properties prop1,prop2;
DAE.Type tp1,tp2;
case (var as DAE.CREF(_,_),val,DAE.PROP(tp1,_),DAE.PROP(tp2,_),source)
equation
(val_1,_) = Types.matchType(val,tp2,DAE.T_REAL_DEFAULT,true);
(var_1,_) = Types.matchType(var,tp1,DAE.T_REAL_DEFAULT,true);
then DAE.STMT_REINIT(var_1,val_1,source);
case (_,_,prop1,prop2,source) equation
Error.addSourceMessage(Error.INTERNAL_ERROR,{"reinit called with wrong args"},DAEUtil.getElementSourceFileInfo(source));
then fail();
// TODO: Add checks for reinit here. 1. First argument must be variable. 2. Expressions must be real.
end matchcontinue;
end makeReinit;
public function makeAssert "function: makeAssert
Creates an assert statement from two expressions.
"
input DAE.Exp cond "condition";
input DAE.Exp msg "message";
input DAE.Properties inProperties3;
input DAE.Properties inProperties4;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement := match (cond,msg,inProperties3,inProperties4,source)
case (cond,msg,DAE.PROP(type_ = (DAE.T_BOOL(varLstBool = _),_)),DAE.PROP(type_ = (DAE.T_STRING(varLstString = _),_)),source) then DAE.STMT_ASSERT(cond,msg,source);
end match;
end makeAssert;
public function makeTerminate "
Creates a terminate statement from message expression.
"
input DAE.Exp msg "message";
input DAE.Properties props;
input DAE.ElementSource source;
output Statement outStatement;
algorithm
outStatement := match (msg,props,source)
case (msg,DAE.PROP(type_ = (DAE.T_STRING(varLstString = _),_)),source) then DAE.STMT_TERMINATE(msg,source);
end match;
end makeTerminate;
public function getCrefFromAlg "Returns all crefs from an algorithm"
input Algorithm alg;
output list<DAE.ComponentRef> crs;
algorithm
crs := Util.listListUnionOnTrue(Util.listMap(getAllExps(alg),Expression.extractCrefsFromExp),ComponentReference.crefEqual);
end getCrefFromAlg;
public function getAllExps "function: getAllExps
This function goes through the Algorithm structure and finds all the
expressions and returns them in a list
"
input Algorithm inAlgorithm;
output list<DAE.Exp> outExpExpLst;
algorithm
outExpExpLst:=
matchcontinue (inAlgorithm)
local
list<DAE.Exp> exps;
list<Statement> stmts;
case DAE.ALGORITHM_STMTS(statementLst = stmts)
equation
exps = getAllExpsStmts(stmts);
then
exps;
end matchcontinue;
end getAllExps;
public function getAllExpsStmts "function: getAllExpsStmts
This function takes a list of statements and returns all expressions and subexpressions
in all statements.
"
input list<Statement> stmts;
output list<DAE.Exp> exps;
algorithm
(_,exps) := DAEUtil.traverseDAEEquationsStmts(stmts,getAllExpsStmtsCollector,{});
end getAllExpsStmts;
function getAllExpsStmtsCollector
input tuple<DAE.Exp,list<DAE.Exp>> itpl;
output tuple<DAE.Exp,list<DAE.Exp>> otpl;
algorithm
otpl := matchcontinue itpl
local
DAE.Exp exp;
list<DAE.Exp> inExps;
case ((exp,inExps)) then Expression.traverseExp(exp,Expression.expressionCollector,inExps);
end matchcontinue;
end getAllExpsStmtsCollector;
end Algorithm;