/
InstUtil.mo
8920 lines (8109 loc) · 306 KB
/
InstUtil.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 package InstUtil
" file: InstUtil.mo
package: InstUtil
description: Instantiation utilities
This package supports Inst*.mo
"
import Absyn;
import ClassInf;
import DAE;
import FCore;
import GlobalScript;
import InnerOuter;
import InstTypes;
import Mod;
import Prefix;
import SCode;
import UnitAbsyn;
import Values;
import HashTable;
import HashTable5;
protected
import DoubleEndedList;
import List;
import BaseHashTable;
import Expression;
import Error;
import Util;
import ComponentReference;
import Patternm;
import DAEUtil;
import DAEDump;
import Types;
import Debug;
import PrefixUtil;
import ElementSource;
import ExpressionDump;
import Flags;
import FGraph;
import FNode;
import SCodeDump;
import Lookup;
import ValuesUtil;
import Static;
import Ceval;
import Dump;
import Config;
import Inst;
import InstFunction;
import InstSection;
import System;
import ErrorExt;
import InstExtends;
import Graph;
import UnitAbsynBuilder;
import UnitChecker;
import NFSCodeFlatten;
import HashSet;
import BaseHashSet;
import MetaModelica.Dangerous.listReverseInPlace;
protected type Ident = DAE.Ident "an identifier";
protected type InstanceHierarchy = InnerOuter.InstHierarchy "an instance hierarchy";
protected type InstDims = list<list<DAE.Dimension>>;
public function newIdent
"This function creates a new, unique identifer.
The same name is never returned twice."
output DAE.ComponentRef outComponentRef;
protected
Integer i;
String is,s;
algorithm
i := tick();
is := intString(i);
s := stringAppend("__TMP__", is);
outComponentRef := ComponentReference.makeCrefIdent(s,DAE.T_UNKNOWN_DEFAULT,{});
end newIdent;
protected function isNotFunction
"This function returns true if the Class is not a function."
input SCode.Element cls;
output Boolean res;
algorithm
res := SCode.isFunction(cls);
res := boolNot(res);
end isNotFunction;
public function scodeFlatten
input SCode.Program inProgram;
input Absyn.Path inPath;
output SCode.Program outProgram;
algorithm
outProgram := matchcontinue(inProgram, inPath)
case (_, _)
equation
false = Flags.isSet(Flags.DO_SCODE_DEP);
then
inProgram;
case (_, Absyn.IDENT(""))
equation
outProgram = scodeFlattenProgram(inProgram);
then
outProgram;
else
equation
(outProgram, _) = NFSCodeFlatten.flattenClassInProgram(inPath, inProgram);
then
outProgram;
end matchcontinue;
end scodeFlatten;
protected function scodeFlattenProgram
input SCode.Program inProgram;
output SCode.Program outProgram;
algorithm
outProgram := matchcontinue(inProgram)
case (_)
equation
ErrorExt.setCheckpoint("scodeFlattenProgram");
outProgram = NFSCodeFlatten.flattenCompleteProgram(inProgram);
ErrorExt.delCheckpoint("scodeFlattenProgram");
then
outProgram;
else
equation
ErrorExt.rollBack("scodeFlattenProgram");
then
inProgram;
end matchcontinue;
end scodeFlattenProgram;
public function reEvaluateInitialIfEqns "
Author BZ
This is a backpatch to fix the case of 'connection.isRoot' in initial if equations.
After the class is instantiated a second sweep is done to check the initial if equations conditions.
If all conditions are constant, we return only the 'correct' branch equations."
input FCore.Cache cache;
input FCore.Graph env;
input DAE.DAElist dae;
input Boolean isTopCall;
output DAE.DAElist odae;
algorithm
odae := match(cache,env,dae,isTopCall)
local
list<DAE.Element> elems;
case(_,_,DAE.DAE(elementLst = elems),true)
equation
elems = listReverse(List.fold2r(elems,reEvaluateInitialIfEqns2,cache,env,{}));
then
DAE.DAE(elems);
case(_,_,_,false) then dae;
end match;
end reEvaluateInitialIfEqns;
protected function reEvaluateInitialIfEqns2 ""
input list<DAE.Element> acc;
input DAE.Element elem;
input FCore.Cache inCache;
input FCore.Graph env;
output list<DAE.Element> oelems;
algorithm
oelems := matchcontinue (acc,elem,inCache,env)
local
list<DAE.Exp> conds;
list<Values.Value> valList;
list<list<DAE.Element>> tbs;
list<DAE.Element> fb,selectedBranch;
DAE.ElementSource source;
list<Boolean> blist;
FCore.Cache cache;
case (_,DAE.INITIAL_IF_EQUATION(condition1 = conds, equations2=tbs, equations3=fb),cache,_)
equation
//print(" (Initial if)To ceval: " + stringDelimitList(List.map(conds,ExpressionDump.printExpStr),", ") + "\n");
(_,valList,_) = Ceval.cevalList(cache,env, conds, true, NONE(), Absyn.NO_MSG(),0);
//print(" Ceval res: ("+stringDelimitList(List.map(valList,ValuesUtil.printValStr),",")+")\n");
blist = List.map(valList,ValuesUtil.valueBool);
selectedBranch = List.findBoolList(blist, tbs, fb);
selectedBranch = makeDAEElementInitial(selectedBranch);
then listAppend(selectedBranch,acc);
else elem::acc;
end matchcontinue;
end reEvaluateInitialIfEqns2;
protected function makeDAEElementInitial "
Author BZ
Helper function for reEvaluateInitialIfEqns, makes the contenst of an initial if equation initial."
input list<DAE.Element> inElems;
output list<DAE.Element> outElems;
algorithm
outElems := matchcontinue(inElems)
local
DAE.Element elem;
DAE.ComponentRef cr;
DAE.Exp e1,e2;
DAE.ElementSource s;
list<DAE.Exp> expl;
list<list<DAE.Element>> tbs ;
list<DAE.Element> fb;
DAE.Algorithm al;
DAE.Dimensions dims;
list<DAE.Element> elems;
case({}) then {};
case(DAE.DEFINE(cr,e1,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIALDEFINE(cr,e1,s)::outElems;
case(DAE.ARRAY_EQUATION(dims,e1,e2,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIAL_ARRAY_EQUATION(dims,e1,e2,s)::outElems;
case(DAE.EQUATION(e1,e2,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIALEQUATION(e1,e2,s)::outElems;
case(DAE.IF_EQUATION(expl,tbs,fb,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIAL_IF_EQUATION(expl,tbs,fb,s)::outElems;
case(DAE.ALGORITHM(al,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIALALGORITHM(al,s)::outElems;
case(DAE.COMPLEX_EQUATION(e1,e2,s)::elems)
equation
outElems = makeDAEElementInitial(elems);
then
DAE.INITIAL_COMPLEX_EQUATION(e1,e2,s)::outElems;
case(elem::elems) // safe "last case" since we can not fail in cases above.
equation
outElems = makeDAEElementInitial(elems);
then
elem::outElems;
end matchcontinue;
end makeDAEElementInitial;
public function lookupTopLevelClass
"Looks up a top level class with the given name."
input String inName;
input SCode.Program inProgram;
input Boolean inPrintError;
output SCode.Element outClass;
algorithm
outClass := matchcontinue(inName, inProgram, inPrintError)
local
SCode.Element cls;
case (_, _, _)
equation
cls = List.getMemberOnTrue(inName, inProgram, SCode.isClassNamed);
then
cls;
case (_, _, true)
equation
Error.addMessage(Error.LOAD_MODEL_ERROR, {inName});
then
fail();
end matchcontinue;
end lookupTopLevelClass;
public function fixInstClassType
"Fixes the type of a class if it is uniontype or function reference.
These are MetaModelica extensions."
input DAE.Type ty;
input Boolean isPartialFn;
output DAE.Type outType;
algorithm
outType := matchcontinue (ty,isPartialFn)
local
String name;
Absyn.Path path1, path2;
case (_,_)
equation
{path1} = Types.getTypeSource(ty);
name = Absyn.pathLastIdent(path1);
path2 = Absyn.stripLast(path1);
"$Code" = Absyn.pathLastIdent(path2);
path2 = Absyn.stripLast(path2);
"OpenModelica" = Absyn.pathLastIdent(path2);
then Util.assoc(name,{
("Expression", DAE.T_CODE(DAE.C_EXPRESSION(),DAE.emptyTypeSource)),
("ExpressionOrModification", DAE.T_CODE(DAE.C_EXPRESSION_OR_MODIFICATION(),DAE.emptyTypeSource)),
("TypeName", DAE.T_CODE(DAE.C_TYPENAME(),DAE.emptyTypeSource)),
("VariableName", DAE.T_CODE(DAE.C_VARIABLENAME(),DAE.emptyTypeSource)),
("VariableNames", DAE.T_CODE(DAE.C_VARIABLENAMES(),DAE.emptyTypeSource))
});
case (_,false) then ty;
case (_,true) then Types.makeFunctionPolymorphicReference(ty);
end matchcontinue;
end fixInstClassType;
public function updateEnumerationEnvironment
input FCore.Cache inCache;
input FCore.Graph inEnv;
input DAE.Type inType;
input SCode.Element inClass;
input ClassInf.State inCi_State;
output FCore.Cache outCache;
output FCore.Graph outEnv;
algorithm
(outCache,outEnv) := matchcontinue(inCache,inEnv,inType,inClass,inCi_State)
local
FCore.Cache cache;
FCore.Graph env,env_1;
DAE.Type ty;
SCode.Element c;
list<String> names;
list<DAE.Var> vars;
Absyn.Path p,pname;
case (cache,env,DAE.T_ENUMERATION(names = names, literalVarLst = vars, source = {p}),_,ClassInf.ENUMERATION(pname))
equation
(cache,env_1) = updateEnumerationEnvironment1(cache,env,Absyn.pathString(pname),names,vars,p);
then
(cache,env_1);
case (cache,env,_,_,_) then (cache,env);
end matchcontinue;
end updateEnumerationEnvironment;
protected function updateEnumerationEnvironment1
"update enumeration value in environment"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Ident inName;
input list<String> inNames;
input list<DAE.Var> inVars;
input Absyn.Path inPath;
output FCore.Cache outCache;
output FCore.Graph outEnv;
algorithm
(outCache,outEnv) := match(inCache,inEnv,inName,inNames,inVars,inPath)
local
FCore.Cache cache;
FCore.Graph env,env_1,env_2,compenv;
String name,nn;
list<String> names;
list<DAE.Var> vars;
DAE.Var var, new_var;
DAE.Type ty;
FCore.Status instStatus;
Absyn.Path p;
DAE.Attributes attributes;
DAE.Binding binding;
Option<DAE.Const> cnstOpt;
case (cache,env,_,nn::names,(DAE.TYPES_VAR(ty = ty))::vars,p)
equation
// get Var
(cache,DAE.TYPES_VAR(name,attributes,_,binding,cnstOpt),
_,_,_,compenv) =
Lookup.lookupIdentLocal(cache, env, nn);
// print("updateEnumerationEnvironment1 -> component: " + name + " ty: " + Types.printTypeStr(ty) + "\n");
// change type
new_var = DAE.TYPES_VAR(name,attributes,ty,binding,cnstOpt);
// update
env_1 = FGraph.updateComp(env, new_var, FCore.VAR_DAE(), compenv);
// next
(cache,env_2) = updateEnumerationEnvironment1(cache,env_1,name,names,vars,p);
then
(cache,env_2);
case (cache,env,_,{},_,_) then (cache,env);
end match;
end updateEnumerationEnvironment1;
public function updateDeducedUnits "updates the deduced units in each DAE.VAR"
input Boolean callScope;
input UnitAbsyn.InstStore store;
input DAE.DAElist dae;
output DAE.DAElist outDae;
algorithm
outDae := match (callScope,store,dae)
local
HashTable.HashTable ht;
array<Option<UnitAbsyn.Unit>> vec;
list<DAE.Element> elts;
/* Only traverse on top scope */
case (true,UnitAbsyn.INSTSTORE(UnitAbsyn.STORE(vec,_),ht,_),DAE.DAE(elts))
equation
elts = List.map2(elts,updateDeducedUnits2,vec,ht);
then DAE.DAE(elts);
else dae;
end match;
end updateDeducedUnits;
protected function updateDeducedUnits2 "updates the deduced units in each DAE.VAR"
input DAE.Element elt;
input array<Option<UnitAbsyn.Unit>> vec;
input HashTable.HashTable ht;
output DAE.Element oelt;
algorithm
oelt := matchcontinue (elt,vec,ht)
local
Integer indx;
String unitStr;
UnitAbsyn.Unit unit;
Option<DAE.VariableAttributes> varOpt;
DAE.ComponentRef cr;
/* Only traverse on top scope */
case ((DAE.VAR(componentRef=cr,variableAttributesOption=varOpt as SOME(DAE.VAR_ATTR_REAL(unit = NONE())))),_,_)
equation
indx = BaseHashTable.get(cr,ht);
SOME(unit) = vec[indx];
unitStr = UnitAbsynBuilder.unit2str(unit);
varOpt = DAEUtil.setUnitAttr(varOpt,DAE.SCONST(unitStr));
then DAEUtil.setVariableAttributes(elt,varOpt);
else elt;
end matchcontinue;
end updateDeducedUnits2;
public function reportUnitConsistency "reports CONSISTENT or INCOMPLETE error message depending on content of store"
input Boolean topScope;
input UnitAbsyn.InstStore store;
algorithm
_ := matchcontinue(topScope,store)
local
Boolean complete; UnitAbsyn.Store st;
case(_,_)
equation
false = Flags.getConfigBool(Flags.UNIT_CHECKING);
then
();
case(true,UnitAbsyn.INSTSTORE(st,_,SOME(UnitAbsyn.CONSISTENT())))
equation
(complete,_) = UnitChecker.isComplete(st);
Error.addMessage(if complete then Error.CONSISTENT_UNITS else Error.INCOMPLETE_UNITS,{});
then
();
else ();
end matchcontinue;
end reportUnitConsistency;
protected function extractConnectorPrefix
"Author: BZ, 2009-09
Extract the part before the conector ex: a.b.c.connector_d.e would return a.b.c"
input DAE.ComponentRef connectorRef;
output DAE.ComponentRef prefixCon;
algorithm
prefixCon := matchcontinue(connectorRef)
local
DAE.ComponentRef child;
String name;
list<DAE.Subscript> subs;
DAE.Type ty;
// If the bottom var is a connector, then it is not an outside connector. (spec 0.1.2)
case(DAE.CREF_IDENT(_,_,_))
equation
// print(name + " is not a outside connector \n");
then
fail();
case(DAE.CREF_QUAL(name,(ty as DAE.T_COMPLEX(complexClassType=ClassInf.CONNECTOR(_,_))),subs,_))
then ComponentReference.makeCrefIdent(name,ty,subs);
case(DAE.CREF_QUAL(name,ty,subs,child))
equation
child = extractConnectorPrefix(child);
then
ComponentReference.makeCrefQual(name,ty,subs,child);
end matchcontinue;
end extractConnectorPrefix;
protected function updateCrefTypesWithConnectorPrefix "
Author: BZ, 2009-09
Helper function for updateTypesInUnconnectedConnectors2"
input DAE.ComponentRef cr1,cr2;
output DAE.ComponentRef outCref;
algorithm outCref := matchcontinue(cr1,cr2)
local
String name,name2;
DAE.ComponentRef child,child2;
DAE.Type ty;
list<DAE.Subscript> subs;
case (DAE.CREF_IDENT(name,ty,subs),DAE.CREF_QUAL(name2,_,_,child2))
equation
true = stringEq(name,name2);
then
ComponentReference.makeCrefQual(name,ty,subs,child2);
case (DAE.CREF_QUAL(name,ty,subs,child),DAE.CREF_QUAL(name2,_,_,child2))
equation
true = stringEq(name,name2);
outCref = updateCrefTypesWithConnectorPrefix(child,child2);
then
ComponentReference.makeCrefQual(name,ty,subs,outCref);
else
equation
print(" ***** FAILURE with " + ComponentReference.printComponentRefStr(cr1) + " _and_ " + ComponentReference.printComponentRefStr(cr2) + "\n");
then fail();
end matchcontinue;
end updateCrefTypesWithConnectorPrefix;
protected function checkClassEqual
input SCode.Element c1;
input SCode.Element c2;
output Boolean areEqual;
algorithm
areEqual := matchcontinue(c1, c2)
local
SCode.Restriction r;
list<SCode.AlgorithmSection> normalAlgorithmLst1,normalAlgorithmLst2;
list<SCode.AlgorithmSection> initialAlgorithmLst1,initialAlgorithmLst2;
SCode.ClassDef cd1, cd2;
// when +g=MetaModelica, check class equality!
case (_,_)
equation
true = Config.acceptMetaModelicaGrammar();
failure(equality(c1 = c2));
then
false;
// check the types for equality!
case (SCode.CLASS(restriction = SCode.R_TYPE()),_)
equation
failure(equality(c1 = c2));
then
false;
// anything else but functions, do not check equality
case (SCode.CLASS(restriction = r),_)
equation
false = SCode.isFunctionRestriction(r);
then
true;
// check the class equality only for functions, made of parts
case (SCode.CLASS(classDef=SCode.PARTS(normalAlgorithmLst=normalAlgorithmLst1, initialAlgorithmLst=initialAlgorithmLst1)),
SCode.CLASS(classDef=SCode.PARTS(normalAlgorithmLst=normalAlgorithmLst2, initialAlgorithmLst=initialAlgorithmLst2)))
equation
// only check if algorithm list lengths are the same!
true = intEq(listLength(normalAlgorithmLst1), listLength(normalAlgorithmLst2));
true = intEq(listLength(initialAlgorithmLst1), listLength(initialAlgorithmLst2));
then
true;
// check the class equality only for functions, made of derived
case (SCode.CLASS(classDef=cd1 as SCode.DERIVED()),
SCode.CLASS(classDef=cd2 as SCode.DERIVED()))
equation
// only check class definitions are the same!
equality(cd1 = cd2);
then
true;
// anything else, false!
else false;
end matchcontinue;
end checkClassEqual;
public function prefixEqualUnlessBasicType
"Checks if two prefixes are equal, unless the class is a
basic type, i.e. all reals, integers, enumerations with
the same name, etc. are equal."
input Prefix.Prefix pre1;
input Prefix.Prefix pre2;
input SCode.Element cls;
algorithm
_ := matchcontinue (pre1, pre2, cls)
local
// adrpo: TODO! FIXME!, I think here we should have pre1 = Prefix.CLASSPRE(variability1) == pre2 = Prefix.CLASSPRE(variability2)
// don't care about prefix for:
// - enumerations
// - types as they cannot have components
// - predefined types as they cannot have components
case (_, _, SCode.CLASS(restriction = SCode.R_ENUMERATION())) then ();
// case (_, _, SCode.CLASS(restriction = SCode.R_TYPE())) then ();
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_ENUMERATION())) then ();
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_INTEGER())) then ();
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_REAL())) then ();
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_STRING())) then ();
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_BOOLEAN())) then ();
// BTH
case (_, _, SCode.CLASS(restriction = SCode.R_PREDEFINED_CLOCK())) then ();
// don't care about prefix for:
// - Real, String, Integer, Boolean
case (_, _, SCode.CLASS(name = "Real")) then ();
case (_, _, SCode.CLASS(name = "Integer")) then ();
case (_, _, SCode.CLASS(name = "String")) then ();
case (_, _, SCode.CLASS(name = "Boolean")) then ();
// BTH
case (_, _, SCode.CLASS(name = "Clock"))
equation
true = intGe(Flags.getConfigEnum(Flags.LANGUAGE_STANDARD), 33);
then ();
// anything else, check for equality!
else
equation
equality(pre1 = pre2);
then ();
end matchcontinue;
end prefixEqualUnlessBasicType;
public function isBuiltInClass "
Author: BZ, this function identifies built in classes."
input String className;
output Boolean b;
algorithm
b := match(className)
case("Real") then true;
case("Integer") then true;
case("String") then true;
case("Boolean") then true;
// BTH
case("Clock") then intGe(Flags.getConfigEnum(Flags.LANGUAGE_STANDARD), 33);
else false;
end match;
end isBuiltInClass;
protected function equalityConstraintOutputDimension
input list<SCode.Element> inElements;
output Integer outDimension;
algorithm
outDimension := match(inElements)
local
list<SCode.Element> tail;
Integer dim;
case({}) equation
then 0;
case(SCode.COMPONENT(attributes = SCode.ATTR(
direction = Absyn.OUTPUT(),
arrayDims = {Absyn.SUBSCRIPT(Absyn.INTEGER(dim))}
)) :: _) equation
then dim;
case(_ :: tail) equation
dim = equalityConstraintOutputDimension(tail);
then dim;
end match;
end equalityConstraintOutputDimension;
public function equalityConstraint
" Tests if the given elements contain equalityConstraint function and returns
corresponding DAE.EqualityConstraint."
input FCore.Graph inEnv;
input list<SCode.Element> inCdefelts;
input SourceInfo info;
output DAE.EqualityConstraint outResult=NONE();
protected
list<SCode.Element> els;
Absyn.Path path;
Integer dimension;
DAE.InlineType inlineType;
algorithm
try
SOME(path) := FGraph.getScopePath(inEnv);
path := Absyn.joinPaths(path, Absyn.IDENT("equalityConstraint"));
path := Absyn.makeFullyQualified(path);
else
return;
end try;
for el in inCdefelts loop
try
SCode.CLASS(name = "equalityConstraint", restriction = SCode.R_FUNCTION(), classDef = SCode.PARTS(elementLst = els)) := el;
dimension := equalityConstraintOutputDimension(els);
inlineType := isInlineFunc(el);
outResult := SOME((path, dimension, inlineType));
return;
else
end try;
end for;
end equalityConstraint;
public function handleUnitChecking
"@author: adrpo
do this unit checking ONLY if we have the flag!"
input FCore.Cache cache;
input FCore.Graph env;
input UnitAbsyn.InstStore inStore;
input Prefix.Prefix pre;
input DAE.DAElist compDAE;
input list<DAE.DAElist> daes;
input String className "for debugging";
output FCore.Cache outCache;
output FCore.Graph outEnv;
output UnitAbsyn.InstStore outStore;
algorithm
(outCache,outEnv,outStore) := match(cache,env,inStore,pre,compDAE,daes,className)
local
DAE.DAElist daetemp;
UnitAbsyn.UnitTerms ut;
UnitAbsyn.InstStore store;
// do nothing if we don't have to do unit checking
case (_,_,store,_,_,_,_)
guard
not Flags.getConfigBool(Flags.UNIT_CHECKING)
then
(cache,env,store);
case (_,_,store,_,_,_,_)
equation
// Perform unit checking/dimensional analysis
//(daetemp,_) = ConnectUtil.equations(csets,pre,false,ConnectionGraph.EMPTY); // ToDO. calculation of connect eqns done twice. remove in future.
// equations from components (dae1) not considered, they are checked in resp recursive call
// but bindings on scalar variables must be considered, therefore passing dae1 separately
//daetemp = DAEUtil.joinDaeLst(daetemp::daes);
daetemp = DAEUtil.joinDaeLst(daes);
(store,ut)= UnitAbsynBuilder.instBuildUnitTerms(env,daetemp,compDAE,store);
//print("built store for "+className+"\n");
//UnitAbsynBuilder.printInstStore(store);
//print("terms for "+className+"\n");
//UnitAbsynBuilder.printTerms(ut);
UnitAbsynBuilder.registerUnitWeights(cache,env,compDAE);
// perform the check
store = UnitChecker.check(ut,store);
//print("store for "+className+"\n");
//UnitAbsynBuilder.printInstStore(store);
//print("dae1="+DAEDump.dumpDebugDAE(DAE.DAE(dae1))+"\n");
then
(cache,env,store);
end match;
end handleUnitChecking;
protected function checkExtendsRestrictionMatch
"see Modelica Specfification 3.1, 7.1.3 Restrictions on the Kind of Base Class"
input SCode.Restriction r1;
input SCode.Restriction r2;
algorithm
_ := match(r1, r2)
// package can be extendended by package
case (SCode.R_PACKAGE(), SCode.R_PACKAGE()) then ();
// normal function -> normal function
case (SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(_)), SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(_))) then ();
// external function -> normal function
case (SCode.R_FUNCTION(SCode.FR_EXTERNAL_FUNCTION(_)), SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(_))) then ();
// operator function -> normal function
case (SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION()), SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(_))) then ();
// operator function -> operator function
case (SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION()), SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION())) then ();
// type -> type
case (SCode.R_TYPE(), SCode.R_TYPE()) then ();
// record -> record
case (SCode.R_RECORD(_), SCode.R_RECORD(_)) then ();
// connector -> type
case (SCode.R_CONNECTOR(_), SCode.R_TYPE()) then ();
// connector -> record
case (SCode.R_CONNECTOR(_), SCode.R_RECORD(_)) then ();
// connector -> connector
case (SCode.R_CONNECTOR(_), SCode.R_CONNECTOR(_)) then ();
// block -> record
case (SCode.R_BLOCK(), SCode.R_RECORD(false)) then ();
// block -> block
case (SCode.R_BLOCK(), SCode.R_BLOCK()) then ();
// model -> record
case (SCode.R_MODEL(), SCode.R_RECORD(false)) then ();
// model -> block
case (SCode.R_MODEL(), SCode.R_BLOCK()) then ();
// model -> model
case (SCode.R_MODEL(), SCode.R_MODEL()) then ();
// class??? same restrictions as model?
// model -> class
case (SCode.R_MODEL(), SCode.R_CLASS()) then ();
// class -> model
case (SCode.R_CLASS(), SCode.R_MODEL()) then ();
// class -> record
case (SCode.R_CLASS(), SCode.R_RECORD(_)) then ();
// class -> block
case (SCode.R_CLASS(), SCode.R_BLOCK()) then ();
// class -> class
case (SCode.R_CLASS(), SCode.R_CLASS()) then ();
// operator -> operator
case (SCode.R_OPERATOR(), SCode.R_OPERATOR()) then ();
end match;
end checkExtendsRestrictionMatch;
protected function checkExtendsForTypeRestiction
"@author: adrpo
This function will check extends for Modelica 3.1 restrictions"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input InnerOuter.InstHierarchy inIH;
input SCode.Restriction inRestriction;
input list<SCode.Element> inSCodeElementLst;
algorithm
_ := matchcontinue(inCache, inEnv, inIH, inRestriction, inSCodeElementLst)
local
Absyn.Path p;
SCode.Restriction r1, r2, r;
String id;
// check the basics ....
// type or connector can be extended by a type
case (_, _, _, r, {SCode.EXTENDS(baseClassPath=Absyn.IDENT(id))})
equation
true = listMember(r, {SCode.R_TYPE(), SCode.R_CONNECTOR(false), SCode.R_CONNECTOR(true)});
true = listMember(id, {"Real", "Integer", "Boolean", "String"});
then ();
//BTH same as above but extended with Clock type if Flags.SYNCHRONOUS_FEATURES == true
case (_, _, _, r, {SCode.EXTENDS(baseClassPath=Absyn.IDENT(id))})
equation
true = intGe(Flags.getConfigEnum(Flags.LANGUAGE_STANDARD), 33);
true = listMember(r, {SCode.R_TYPE(), SCode.R_CONNECTOR(false), SCode.R_CONNECTOR(true)});
true = listMember(id, {"Real", "Integer", "Boolean", "String", "Clock"});
then ();
// we haven't found the class, do nothing
case (_, _, _, _, {SCode.EXTENDS(baseClassPath=p)})
equation
failure((_, _, _) = Lookup.lookupClass(inCache, inEnv, p));
then ();
// we found te class, check the restriction
case (_, _, _, r1, {SCode.EXTENDS(baseClassPath=p)})
equation
(_,SCode.CLASS(restriction=r2),_) = Lookup.lookupClass(inCache,inEnv,p);
checkExtendsRestrictionMatch(r1, r2);
then ();
// make some waves that this is not correct
case (_, _, _, r1, {SCode.EXTENDS(baseClassPath=p)})
equation
(_,SCode.CLASS(restriction=r2),_) = Lookup.lookupClass(inCache, inEnv, p);
print("Error!: " + SCodeDump.restrString(r1) + " " + FGraph.printGraphPathStr(inEnv) +
" cannot be extended by " + SCodeDump.restrString(r2) + " " + Absyn.pathString(p) + " due to derived/base class restrictions.\n");
then fail();
end matchcontinue;
end checkExtendsForTypeRestiction;
public function checkDerivedRestriction
input SCode.Restriction parentRestriction;
input SCode.Restriction childRestriction;
input SCode.Ident childName;
output Boolean b;
protected
Boolean b1, b2, b3, b4;
list<String> strLst;
list<SCode.Restriction> rstLst;
algorithm
// BTH add Clock type to both lists if Flags.SYNCHRONOUS_FEATURES == true
strLst := if intGe(Flags.getConfigEnum(Flags.LANGUAGE_STANDARD), 33)
then {"Real", "Integer", "String", "Boolean", "Clock"}
else {"Real", "Integer", "String", "Boolean"};
b1 := listMember(childName, strLst);
rstLst := if intGe(Flags.getConfigEnum(Flags.LANGUAGE_STANDARD), 33)
then {SCode.R_TYPE(), SCode.R_PREDEFINED_INTEGER(), SCode.R_PREDEFINED_REAL(), SCode.R_PREDEFINED_STRING(), SCode.R_PREDEFINED_BOOLEAN(), SCode.R_PREDEFINED_CLOCK()}
else {SCode.R_TYPE(), SCode.R_PREDEFINED_INTEGER(), SCode.R_PREDEFINED_REAL(), SCode.R_PREDEFINED_STRING(), SCode.R_PREDEFINED_BOOLEAN()};
b2 := listMember(childRestriction, rstLst);
b3 := valueEq(parentRestriction, SCode.R_TYPE());
//b2 := listMember(childRestriction, {SCode.R_TYPE(), SCode.R_ENUMERATION(), SCode.R_PREDEFINED_INTEGER(), SCode.R_PREDEFINED_REAL(), SCode.R_PREDEFINED_STRING(), SCode.R_PREDEFINED_BOOLEAN(), SCode.R_PREDEFINED_ENUMERATION()});
//b3 := boolOr(valueEq(parentRestriction, SCode.R_TYPE()), valueEq(parentRestriction, SCode.R_ENUMERATION()));
b4 := valueEq(parentRestriction, SCode.R_CONNECTOR(false)) or valueEq(parentRestriction, SCode.R_CONNECTOR(true));
// basically if child or parent is a type or basic type or parent is a connector and child is a type
b := boolOr(b1, boolOr(b2, boolOr(b3, boolAnd(boolOr(b1,b2), b4))));
end checkDerivedRestriction;
public function matchModificationToComponents "
Author: BZ, 2009-05
This function is called from instClassDef, recursivly remove modifers on each component.
What ever is left in modifier is printed as a warning. That means that we have modifiers on a component that does not exist."
input list<SCode.Element> inElems;
input DAE.Mod inmod;
input String callingScope;
algorithm
_ := matchcontinue(inElems, inmod, callingScope)
local
SCode.Element elem;
String cn,s1,s2;
list<SCode.Element> elems;
DAE.Mod mod;
case(_,DAE.NOMOD(),_) then ();
case(_,DAE.MOD(subModLst={}),_) then ();
case({},_,_)
equation
s1 = Mod.prettyPrintMod(inmod,0);
s2 = s1 + " not found in <" + callingScope + ">";
// Line below can be used for testing test-suite for dangling modifiers when getErrorString() is not called.
//print(" *** ERROR Unused modifer...: " + s2 + "\n");
Error.addMessage(Error.UNUSED_MODIFIER,{s2});
then
fail();
case((SCode.COMPONENT(name=cn))::elems,mod,_)
equation
mod = Mod.removeMod(mod,cn);
matchModificationToComponents(elems,mod,callingScope);
then
();
case((SCode.EXTENDS())::elems,_,_)
equation matchModificationToComponents(elems,inmod,callingScope); then ();
//TODO: only remove modifiers on replaceable classes, make special case for redeclaration of local classes
case((SCode.CLASS(name=cn,prefixes=SCode.PREFIXES()))::elems,mod,_)
equation
mod = Mod.removeMod(mod,cn);
matchModificationToComponents(elems,mod,callingScope);
then ();
case((SCode.IMPORT())::elems,_,_)
equation
matchModificationToComponents(elems,inmod,callingScope);
then ();
case( (SCode.CLASS(prefixes=SCode.PREFIXES(replaceablePrefix=SCode.NOT_REPLACEABLE())))::elems,_,_)
equation
matchModificationToComponents(elems,inmod,callingScope);
then ();
end matchcontinue;
end matchModificationToComponents;
protected function elementNameMember
"Returns true if the given element is in the list"
input tuple<SCode.Element, DAE.Mod> inElement;
input list<SCode.Element> els;
output Boolean isNamed;
algorithm
isNamed := listMember(Util.tuple21(inElement), els);
end elementNameMember;
public function extractConstantPlusDepsTpl "
Author: adrpo, see extractConstantPlusDeps for comments"
input list<tuple<SCode.Element, DAE.Mod>> inComps;
input Option<DAE.ComponentRef> ocr;
input list<SCode.Element> allComps;
input String className;
input list<SCode.Equation> ieql;
input list<SCode.Equation> iieql;
input list<SCode.AlgorithmSection> ialgs;
input list<SCode.AlgorithmSection> iialgs;
output list<tuple<SCode.Element, DAE.Mod>> oel;