/
MetaUtil.mo
1374 lines (1273 loc) · 42.8 KB
/
MetaUtil.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-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.
*
*/
package MetaUtil
" file: MetaUtil.mo
package: MetaUtil
description: Different MetaModelica extension functions.
RCS: $Id$
"
public import Absyn;
public import ClassInf;
public import DAE;
public import Debug;
public import Env;
public import SCode;
public import SCodeUtil;
protected import Exp;
protected import Lookup;
protected import RTOpts;
protected import Types;
protected import Util;
public function isList "function: isList
author: KS
Return true if list
"
input DAE.Properties prop;
output Boolean bool;
algorithm
bool :=
matchcontinue (prop)
case (DAE.PROP((DAE.T_LIST(_),_),_)) then true;
case (_) then false;
end matchcontinue;
end isList;
public function simplifyListExp "function: simplifyListExp
Author: KS
Used by Static.elabExp to simplify some cons/list expressions.
"
input DAE.ExpType t;
input DAE.Exp e1;
input DAE.Exp e2;
output DAE.Exp expOut;
algorithm
expOut :=
matchcontinue (t,e1,e2)
local
DAE.Exp localE1,localE2;
DAE.ExpType tLocal;
case (tLocal,localE1,DAE.LIST(_,expList))
local
list<DAE.Exp> expList,expList2;
equation
expList2 = listAppend({localE1},expList);
then DAE.LIST(tLocal,expList2);
case (tLocal,localE1,localE2) then DAE.CONS(tLocal,localE1,localE2);
end matchcontinue;
end simplifyListExp;
public function listToConsCell "function: listToConsCell
Author: KS
In the C-code, a list constructor will consist of
several cons constructor. For instance:
list(1,2,3,4) will be written as
mk_cons(1,mk_cons(2,mk_cons(3,mk_cons(4,mk_nil())))),
(the constants 1,2,3,4 each will be wrapped with a create-constant
function call)
"
input list<String> varList;
input list<DAE.Exp> expList;
output String outString;
algorithm
outString :=
matchcontinue (varList,expList)
case ({},_)
local
String s;
equation
s = "mmc_mk_nil()";
then s;
case (firstVar :: restVar,firstExp :: restExp)
local
String firstVar,s,s2;
list<String> restVar;
list<DAE.Exp> restExp;
DAE.Exp firstExp;
equation
firstVar = createConstantCExp(firstExp,firstVar);
s2 = listToConsCell(restVar,restExp);
s = Util.stringAppendList({"mmc_mk_cons(",firstVar,",",s2,")"});
then s;
end matchcontinue;
end listToConsCell;
public function createConstantCExp "function: createConstantCExp2"
input DAE.Exp exp;
input String inExp;
output String s;
DAE.ExpType expType;
algorithm
expType := Exp.typeof(exp);
s := createConstantCExp2(expType, inExp);
end createConstantCExp;
protected function createConstantCExp2 "function: createConstantCExp2"
input DAE.ExpType exp;
input String inExp;
output String s;
algorithm
s :=
matchcontinue(exp,inExp)
local
String localInExp,outStr;
case (DAE.ET_INT(),localInExp)
equation
outStr = Util.stringAppendList({"mmc_mk_icon(",localInExp,")"});
then outStr;
case (DAE.ET_REAL(),localInExp)
equation
outStr = Util.stringAppendList({"mmc_mk_rcon(",localInExp,")"});
then outStr;
case (DAE.ET_BOOL(),localInExp)
equation
outStr = Util.stringAppendList({"mmc_mk_icon(",localInExp,")"});
then outStr;
case (DAE.ET_STRING(),localInExp)
equation
outStr = Util.stringAppendList({"mmc_mk_scon(",localInExp,")"});
then outStr;
case (DAE.ET_COMPLEX(name = path, varLst = varLst),localInExp)
local
list<DAE.ExpVar> varLst;
list<String> vars, vars1;
list<DAE.ExpType> types;
String str,name;
Absyn.Path path;
equation
vars = Util.listMap(varLst, Exp.varName);
str = stringAppend(localInExp,".");
vars1 = Util.listMap1r(vars, stringAppend, str);
types = Util.listMap(varLst, Exp.varType);
outStr = listToBoxes(vars1,types,-1,Absyn.pathString(path));
then outStr;
case (_,localInExp) then localInExp;
end matchcontinue;
end createConstantCExp2;
public function fixListConstructorsInArgs "function: fixListConstructorsInArgs
Author: KS
In a function call, an Absyn.ARRAY() will be transformed into an Absyn.LIST()
if the corresponding argument type is a list
"
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.ComponentRef funcName;
input list<Absyn.Exp> inArgs;
input list<Absyn.NamedArg> inNamedArgs;
output Env.Cache outCache;
output Env.Env outEnv;
output list<Absyn.Exp> outArgs;
output list<Absyn.NamedArg> outNamedArgs;
algorithm
(outCache,outEnv,outArgs,outNamedArgs) :=
matchcontinue (inCache,inEnv,funcName,inArgs,inNamedArgs)
case (cache,env,fn,args,nargs)
local
Env.Cache cache;
Env.Env env;
Absyn.ComponentRef fn;
Absyn.Path fn2;
list<Absyn.Exp> args;
list<Absyn.NamedArg> nargs;
list<SCode.Element> elemList;
list<DAE.Type> typeList1;
list<DAE.FuncArg> typeList2;
equation
fn2 = Absyn.crefToPath(fn);
(cache,typeList1,_)
= Lookup.lookupFunctionsInEnv(cache,env, fn2);
typeList2 = extractFuncTypes(typeList1);
args = fixListConstructorsInArgs2(typeList2,args,{});
nargs = fixListConstructorsInArgs3(typeList2,nargs,{});
then (cache,env,args,nargs);
case (_,_,_,_,_)
equation
print("could not look up class");
Debug.fprint("failtrace", "- could not lookup class for constant list constructors.");
then fail();
end matchcontinue;
end fixListConstructorsInArgs;
public function extractFuncTypes "function: extractNameAndType
Author: KS
Extracts the name and type.
"
input list<DAE.Type> inElem;
output list<DAE.FuncArg> outList;
algorithm
outList :=
matchcontinue(inElem)
case ({}) then {};
case ((DAE.T_FUNCTION(typeList,_,_),_) :: {})
local
list<DAE.FuncArg> typeList;
equation
then typeList;
case (_) then {}; // If a function has more than one definition we do not
// bother. SHOULD BE FIXED
end matchcontinue;
end extractFuncTypes;
public function fixListConstructorsInArgs2 "function: fixListConstructorsInArgs2
author: KS
"
input list<DAE.FuncArg> inTypes;
input list<Absyn.Exp> inArgs;
input list<Absyn.Exp> accList;
output list<Absyn.Exp> outArgs;
algorithm
outArgs :=
matchcontinue (inTypes,inArgs,accList)
case ({},localInArgs,_)
local
list<Absyn.Exp> localInArgs;
equation
then localInArgs;
case (localInTypes,localInArgs,localAccList)
local
list<DAE.FuncArg> localInTypes;
list<Absyn.Exp> localInArgs,localAccList;
equation
localInArgs = fixListConstructorsInArgs2Helper(localInTypes,localInArgs,localAccList);
then localInArgs;
end matchcontinue;
end fixListConstructorsInArgs2;
public function fixListConstructorsInArgs2Helper "function: fixListConstructorsInArgs2
Author: KS
Helper function to fixListConstructorsInArgs
"
input list<DAE.FuncArg> inTypes;
input list<Absyn.Exp> inArgs;
input list<Absyn.Exp> accList;
output list<Absyn.Exp> outArgs;
algorithm
outArgs :=
matchcontinue (inTypes,inArgs,accList)
local
list<Absyn.Exp> localAccList;
case (_,{},localAccList) then localAccList;
case ({},_,localAccList)
equation
Debug.fprint("failtrace", "- wrong number of arguments in function call?.");
then fail();
case ((_,(DAE.T_LIST(_),_)) :: restTypes,Absyn.ARRAY(expList) :: restArgs,localAccList)
local
list<Absyn.Exp> expList,restArgs;
list<DAE.FuncArg> restTypes;
equation
expList = transformArrayNodesToListNodes(expList,{});
localAccList = listAppend(localAccList,{Absyn.LIST(expList)});
localAccList = fixListConstructorsInArgs2Helper(restTypes,restArgs,localAccList);
then localAccList;
case (_ :: restTypes,firstArg :: restArgs,localAccList)
local
Absyn.Exp firstArg;
list<Absyn.Exp> restArgs;
list<DAE.FuncArg> restTypes;
equation
localAccList = listAppend(localAccList,{firstArg});
localAccList = fixListConstructorsInArgs2Helper(restTypes,restArgs,localAccList);
then localAccList;
end matchcontinue;
end fixListConstructorsInArgs2Helper;
public function fixListConstructorsInArgs3 "function: fixListConstructorsInArgs2
author: KS
"
input list<DAE.FuncArg> inTypes;
input list<Absyn.NamedArg> inNamedArgs;
input list<Absyn.NamedArg> accList;
output list<Absyn.NamedArg> outArgs;
algorithm
outArgs :=
matchcontinue (inTypes,inNamedArgs,accList)
case ({},localInArgs,_)
local
list<Absyn.NamedArg> localInArgs;
equation
then localInArgs;
case (localInTypes,localInArgs,localAccList)
local
list<DAE.FuncArg> localInTypes;
list<Absyn.NamedArg> localInArgs,localAccList;
equation
localInArgs = fixListConstructorsInArgs3Helper(localInTypes,localInArgs,localAccList);
then localInArgs;
end matchcontinue;
end fixListConstructorsInArgs3;
public function fixListConstructorsInArgs3Helper "function: fixListConstructorsInArgs3
Author: KS
Helper function to fixListConstructorsInArgs
"
input list<DAE.FuncArg> inTypes;
input list<Absyn.NamedArg> inNamedArgs;
input list<Absyn.NamedArg> accList;
output list<Absyn.NamedArg> outArgs;
algorithm
outArgs :=
matchcontinue (inTypes,inNamedArgs,accList)
local
list<Absyn.NamedArg> localAccList;
case (_,{},localAccList) then localAccList;
case (argTypes,Absyn.NAMEDARG(id,Absyn.ARRAY(expList)) :: restArgs,localAccList)
local
list<Absyn.Exp> expList;
Absyn.Ident id;
list<DAE.FuncArg> argTypes;
list<Absyn.NamedArg> restArgs;
equation
((DAE.T_LIST(_),_)) = findArgType(id,argTypes);
expList = transformArrayNodesToListNodes(expList,{});
localAccList = listAppend(localAccList,{Absyn.NAMEDARG(id,Absyn.LIST(expList))});
localAccList = fixListConstructorsInArgs3Helper(argTypes,restArgs,localAccList);
then localAccList;
case (argTypes,firstArg :: restArgs,localAccList)
local
Absyn.NamedArg firstArg;
list<DAE.FuncArg> argTypes;
list<Absyn.NamedArg> restArgs;
equation
localAccList = listAppend(localAccList,{firstArg});
localAccList = fixListConstructorsInArgs3Helper(argTypes,restArgs,localAccList);
then localAccList;
end matchcontinue;
end fixListConstructorsInArgs3Helper;
public function findArgType "function: findArgType
Author: KS
Helper function to fixListConstructorsInArgs
"
input Absyn.Ident id;
input list<DAE.FuncArg> argTypes;
output DAE.Type outType;
algorithm
outType :=
matchcontinue (id,argTypes)
local
Absyn.Ident localId;
case (localId,{}) then DAE.T_INTEGER_DEFAULT; // Return DUMMIE (this case should not happend)
case (localId,(localId2,t) :: _)
local
DAE.Type t;
Absyn.Ident localId2;
equation
true = (localId ==& localId2);
then t;
case (localId,_ :: restList)
local
list<DAE.FuncArg> restList;
DAE.Type t;
equation
t = findArgType(localId,restList);
then t;
end matchcontinue;
end findArgType;
public function transformArrayNodesToListNodes "function: transformArrayNodesToListNodes"
input list<Absyn.Exp> inList;
input list<Absyn.Exp> accList;
output list<Absyn.Exp> outList;
algorithm
outList :=
matchcontinue (inList,accList)
local
list<Absyn.Exp> localAccList;
case ({},localAccList) then localAccList;
case (Absyn.ARRAY({}) :: restList,localAccList)
local
list<Absyn.Exp> restList;
equation
localAccList = listAppend(localAccList,{Absyn.LIST({})});
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
case (Absyn.ARRAY(es) :: restList,localAccList)
local
list<Absyn.Exp> es,restList;
equation
es = transformArrayNodesToListNodes(es,{});
localAccList = listAppend(localAccList,{Absyn.LIST(es)});
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
case (firstExp :: restList,localAccList)
local
list<Absyn.Exp> restList;
Absyn.Exp firstExp;
equation
localAccList = listAppend(localAccList,{firstExp});
localAccList = transformArrayNodesToListNodes(restList,localAccList);
then localAccList;
end matchcontinue;
end transformArrayNodesToListNodes;
public function createListType "function: createListType"
input DAE.Type inType;
input Integer numLists;
output DAE.Type outType;
algorithm
outType :=
matchcontinue (inType,numLists)
local
DAE.Type localT;
case (localT,0) then localT;
case (localT,n)
local
Integer n;
DAE.Type t;
equation
t = (DAE.T_LIST(localT),NONE());
t = createListType(t,n-1);
then t;
end matchcontinue;
end createListType;
public function getTypeFromProp "function: getTypeFromProp"
input DAE.Properties inProp;
output DAE.Type outType;
algorithm
outType :=
matchcontinue (inProp)
case (DAE.PROP(t,_))
local DAE.Type t; equation then t;
end matchcontinue;
end getTypeFromProp;
/*
public function typeMatching
input DAE.Type t;
input list<DAE.Properties> propList;
output Boolean outBool;
algorithm
outBool :=
matchcontinue (t,propList)
local
Boolean b;
DAE.Type tLocal;
list<DAE.Properties> restList;
case (_,{}) then true;
case (tLocal as (DAE.T_INTEGER(_),_),DAE.PROP((DAE.T_INTEGER(_),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_REAL(_),_),DAE.PROP((DAE.T_REAL(_),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_STRING(_),_),DAE.PROP((DAE.T_STRING(_),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_BOOL(_),_),DAE.PROP((DAE.T_BOOL(_),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_NOTYPE(),_),DAE.PROP((DAE.T_NOTYPE(),_),_) :: restList)
equation
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_COMPLEX(ClassInf.RECORD(s1),_,_),_),
DAE.PROP((DAE.T_COMPLEX(ClassInf.RECORD(s2),_,_),_),_) :: restList)
local String s1,s2;
equation
true = (s1 ==& s2);
b = typeMatching(tLocal,restList);
then b;
case (tLocal as (DAE.T_LIST(t1),_),
DAE.PROP((DAE.T_LIST(t2),_),_) :: restList)
local String s1,s2;
equation
true = (s1 ==& s2);
b = typeMatching(tLocal,restList);
then b;
case (_,_) then false;
end matchcontinue;
end typeMatching; */
/*
public function createMatchcontinueResultVars "function: createMatchcontinueResultVars"
input Env.Cache cache;
input Env.Env env;
input list<Absyn.Exp> refList;
input Integer num;
input list<Absyn.ElementItem> accDeclList;
input list<Absyn.Exp> accVarList;
output Env.Cache outCache;
output list<Absyn.ElementItem> outDecls;
output list<Absyn.Exp> outVarList;
algorithm
(outCache,outDecls,outVarList) :=
matchcontinue (cache,env,refList,num)
local
Env.Cache localCache;
Env.Env localEnv;
list<Absyn.ElementItem> localAccDeclList;
list<Absyn.Exp> localAccVarList;
case (localCache,_,{},_,localAccDeclList,localAccVarList)
then (localCache,localAccDeclList,localAccVarList);
case (localCache,localEnv,Absyn.CREF(Absyn.CREF_IDENT(c,{})) :: restExp,n,localAccDeclList,localAccVarList)
local
Absyn.Ident varName,c;
list<Absyn.ElementItem> varList;
list<Absyn.Exp> restExp;
Integer n;
DAE.TType t;
Absyn.TypeSpec t2;
equation
(localCache,DAE.TYPES_VAR(_,_,_,(t,_),_),_,_) = Lookup.lookupIdent(localCache,localEnv,c);
t2 = typeConvert(t);
varName = stringAppend("RES__",intString(n));
varList = Util.listCreate(Absyn.ELEMENTITEM(Absyn.ELEMENT(
false,NONE(),Absyn.UNSPECIFIED(),"component",
Absyn.COMPONENTS(Absyn.ATTR(false,false,Absyn.VAR(),Absyn.BIDIR(),{}),
t2,
{Absyn.COMPONENTITEM(Absyn.COMPONENT(varName,{},NONE()),NONE(),NONE())}),
Absyn.INFO("f",false,0,0,0,0),NONE())));
localAccVarList = listAppend(localAccVarList,{Absyn.CREF(Absyn.CREF_IDENT(varName,{}))});
localAccDeclList = listAppend(localAccDeclList,varList);
(localCache,localAccDeclList,localAccVarList) = createMatchcontinueResultVars(
localCache,localEnv,restExp,n+1,localAccDeclList,localAccVarList);
then (localCache,localAccDeclList,localAccVarList);
case (_,_,_,_,_,_) then fail();
end matchcontinue;
end createMatchcontinueResultVars;
*/
public function getListOfStrings
input list<SCode.Element> els;
output list<String> outStrings;
algorithm
outStrings := matchcontinue(els)
local
list<SCode.Element> rest;
list<String> slst;
String n;
case({})
then {};
case(SCode.CLASSDEF(name = n)::rest)
equation
slst = getListOfStrings(rest);
then n::slst;
case(_) then fail();
end matchcontinue;
end getListOfStrings;
//Check if a class has a certain restriction, added by simbj
public function classHasMetaRestriction
input SCode.Class cl;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue(cl)
local
SCode.Restriction re1,re2;
case(SCode.CLASS(restriction = SCode.R_METARECORD(_,_)))
then true;
case(_) then false;
end matchcontinue;
end classHasMetaRestriction;
//Check if a class has a certain restriction, added by simbj
public function classHasRestriction
input SCode.Class cl;
input SCode.Restriction re;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue(cl,re)
local
SCode.Restriction re1,re2;
case(SCode.CLASS(restriction = re1),re2)
equation
equality(re1 = re2);
then true;
case(_,_) then false;
end matchcontinue;
end classHasRestriction;
public function createMetaClassesInProgram "function: createMetaClassesInProgram
Adds metarecord classes to the AST. This function handles a whole program,
including packages."
input Absyn.Program program;
output Absyn.Program out;
algorithm
out := matchcontinue(program)
local
list<Absyn.Class> classes, metaClassesFlat;
list<list<Absyn.Class>> metaClasses;
Absyn.Within w;
Absyn.Program p;
Absyn.TimeStamp ts;
case (program)
equation
false = RTOpts.acceptMetaModelicaGrammar();
then program;
case (Absyn.PROGRAM(classes = classes,within_ = w,globalBuildTimes=ts))
equation
metaClasses = Util.listMap(classes, createMetaClasses);
metaClassesFlat = Util.listFlatten(metaClasses);
classes = Util.listMap(classes, createMetaClassesFromPackage);
classes = listAppend(classes, metaClassesFlat);
then
Absyn.PROGRAM(classes,w,ts);
end matchcontinue;
end createMetaClassesInProgram;
protected function createMetaClassesFromPackage "function: createMetaClassesFromPackages
Helper function to createMetaClassesInProgram"
input Absyn.Class cl;
output Absyn.Class out;
algorithm
out := matchcontinue(cl)
local
list<Absyn.Class> classes, metaClassesFlat;
list<list<Absyn.Class>> metaClasses;
String name;
Boolean partialPrefix;
Boolean finalPrefix;
Boolean encapsulatedPrefix;
Absyn.Restriction restriction;
Absyn.ClassDef body;
Absyn.Info info;
list<Absyn.ClassPart> classParts;
Option<String> comment;
case (Absyn.CLASS(body=Absyn.PARTS(classParts=classParts,comment=comment),name=name,partialPrefix=partialPrefix,finalPrefix=finalPrefix,encapsulatedPrefix=encapsulatedPrefix,restriction=restriction,info=info))
equation
classParts = Util.listMap(classParts,createMetaClassesFromClassParts);
body = Absyn.PARTS(classParts,comment);
then Absyn.CLASS(name,partialPrefix,finalPrefix,encapsulatedPrefix,restriction,body,info);
case (cl) then cl;
end matchcontinue;
end createMetaClassesFromPackage;
protected function createMetaClassesFromClassParts
input Absyn.ClassPart classPart;
output Absyn.ClassPart out;
algorithm
out := matchcontinue (classPart)
local
list<Absyn.ElementItem> els;
list<list<Absyn.ElementItem>> lels;
case (Absyn.PUBLIC(els))
equation
lels = Util.listMap(els, createMetaClassesFromElementItem);
els = Util.listFlatten(lels);
then Absyn.PUBLIC(els);
case (Absyn.PROTECTED(els))
equation
lels = Util.listMap(els, createMetaClassesFromElementItem);
els = Util.listFlatten(lels);
then Absyn.PROTECTED(els);
case (classPart) then classPart;
end matchcontinue;
end createMetaClassesFromClassParts;
protected function createMetaClassesFromElementItem
input Absyn.ElementItem elementItem;
output list<Absyn.ElementItem> out;
algorithm
out := matchcontinue(elementItem)
local
Boolean finalPrefix;
Option<Absyn.RedeclareKeywords> redeclareKeywords;
Absyn.InnerOuter innerOuter;
String name;
Absyn.ElementSpec specification;
Absyn.Info info;
Option<Absyn.ConstrainClass> constrainClass;
Boolean replaceable_;
Absyn.Class class_, cl2;
list<Absyn.Class> metaClasses, classes;
list<Absyn.ElementItem> elementItems;
case (Absyn.ELEMENTITEM(Absyn.ELEMENT(specification=Absyn.CLASSDEF(replaceable_=replaceable_,class_=class_),finalPrefix=finalPrefix,redeclareKeywords=redeclareKeywords,innerOuter=innerOuter,name=name,info=info,constrainClass=constrainClass)))
equation
metaClasses = createMetaClasses(class_);
cl2 = createMetaClassesFromPackage(class_);
classes = cl2 :: metaClasses;
elementItems = Util.listMap1r(classes,setElementItemClass,elementItem);
then elementItems;
case (elementItem) then {elementItem};
end matchcontinue;
end createMetaClassesFromElementItem;
protected function setElementItemClass
input Absyn.ElementItem elementItem;
input Absyn.Class class_;
output Absyn.ElementItem out;
algorithm
out := matchcontinue (elementItem,class_)
local
Boolean finalPrefix;
Option<Absyn.RedeclareKeywords> redeclareKeywords;
Absyn.InnerOuter innerOuter;
String name;
Absyn.ElementSpec specification;
Absyn.Info info;
Option<Absyn.ConstrainClass> constrainClass;
Boolean replaceable_;
Absyn.Class class_;
list<Absyn.Class> metaClasses, classes;
list<Absyn.ElementItem> elementItems;
case (Absyn.ELEMENTITEM(Absyn.ELEMENT(specification=Absyn.CLASSDEF(replaceable_=replaceable_),finalPrefix=finalPrefix,redeclareKeywords=redeclareKeywords,innerOuter=innerOuter,name=name,info=info,constrainClass=constrainClass)),class_)
then Absyn.ELEMENTITEM(Absyn.ELEMENT(finalPrefix,redeclareKeywords,innerOuter,name,Absyn.CLASSDEF(replaceable_,class_),info,constrainClass));
end matchcontinue;
end setElementItemClass;
//Added by simbj
//Analyze the AST, find union type and extend the AST with metarecords
public function createMetaClasses
input Absyn.Class cl;
output list<Absyn.Class> clstout;
algorithm
clstout := matchcontinue(cl)
local
list<Absyn.ClassPart> cls;
list<Absyn.ElementItem> els;
list<Absyn.Class> cllst;
SCode.ClassDef d_1;
SCode.Restriction r_1;
Absyn.Class c;
String n;
Boolean p,f,e;
Absyn.Restriction r;
Absyn.ClassDef d;
Absyn.Info file_info;
case(c as Absyn.CLASS(name = n,partialPrefix = p,finalPrefix = f,encapsulatedPrefix = e,restriction = r,
body = d as Absyn.PARTS(classParts = cls as {Absyn.PUBLIC(contents = els)},comment = _),info = file_info))
equation
r_1 = SCodeUtil.translateRestriction(c, r); // uniontype will not get elaborated!
equality(r_1 = SCode.R_UNIONTYPE);
els = fixElementItems(els,n,0);
cllst = convertElementsToClasses(els);
then cllst;
case(_) then {};
end matchcontinue;
end createMetaClasses;
//Added by simbj
//Helper function
function convertElementsToClasses
input list<Absyn.ElementItem> els;
output list<Absyn.Class> outcls;
algorithm
outcls := matchcontinue(els)
local
list<Absyn.ElementItem> rest;
Absyn.Class c;
list<Absyn.Class> clst;
String uniontypeName, n;
Boolean p,f,e;
Absyn.Restriction r;
Absyn.ClassDef b;
String fn;
Boolean ro;
Integer index,lns,cns,lne,cne;
case({}) then {};
case(Absyn.ELEMENTITEM(element = Absyn.ELEMENT(specification = Absyn.CLASSDEF(class_ = c)))::rest)
equation
clst = convertElementsToClasses(rest);
then
c::clst;
end matchcontinue;
end convertElementsToClasses;
/* Removed 2009-08-19. The args are now generated like any Modelica components.
* This means we don't have to write cases for every single datatype. /sjoelund
function createFunctionArgsList //helper function for uniontypes
input SCode.Class c;
input Env.Cache inCache;
input Env.Env inEnv;
output list<tuple<SCode.Ident, tuple<DAE.TType, Option<Absyn.Path>>>> fargs;
algorithm
fargs := matchcontinue(c,inCache,inEnv)
local
list<SCode.Element> els;
Env.Cache cache;
Env.Env env;
case(SCode.CLASS(restriction = SCode.R_METARECORD(_,_),classDef = SCode.PARTS(elementLst = els)),cache,env)
then createFunctionArgsList2(els,cache,env);
end matchcontinue;
end createFunctionArgsList;
function createFunctionArgsList2
input list<SCode.Element> els;
input Env.Cache inCache;
input Env.Env inEnv;
output list<tuple<SCode.Ident, tuple<DAE.TType, Option<Absyn.Path>>>> fargs;
algorithm
fargs := matchcontinue(els,inCache,inEnv)
local
SCode.Element e;
list<SCode.Element> rest;
SCode.Ident name,typeName;
DAE.Type t;
Env.Cache cache;
Env.Env env,env_1;
Absyn.Path path;
list<tuple<SCode.Ident, tuple<DAE.TType, Option<Absyn.Path>>>> fargs;
SCode.Class cl;
case({},cache,env)
then {};
// Special case for recursive uniontypes. sjoelund 2009-05-13
case(SCode.COMPONENT(component = name,typeSpec = Absyn.TPATH(path = path))::rest,cache,env)
equation
(cache,cl,env_1) = Lookup.lookupClass(cache,env,path,true);
SCode.CLASS(restriction = SCode.R_UNIONTYPE()) = cl;
t = createUnionType(cl,NONE);
fargs = createFunctionArgsList2(rest,cache,env);
then (name,t)::fargs;
// Special case for (non-uniontype) records
case(SCode.COMPONENT(component = name,typeSpec = Absyn.TPATH(path = path))::rest,cache,env)
equation
(cache,cl as SCode.CLASS(restriction = SCode.R_RECORD()),_) = Lookup.lookupClass(cache,env,path,true);
(cache,(DAE.T_FUNCTION(_,t),_),env_1) = Lookup.lookupType(cache, env, path, false);
fargs = createFunctionArgsList2(rest,cache,env);
then (name,t)::fargs;
case(SCode.COMPONENT(component = name,typeSpec = Absyn.TPATH(path = path))::rest,cache,env)
equation
(cache,t,env_1) = Lookup.lookupType(cache, env, path, false);
fargs = createFunctionArgsList2(rest,cache,env);
then (name,t)::fargs;
case(SCode.COMPONENT(component = name,typeSpec = Absyn.TPATH(path = path))::rest,cache,env)
equation
//failure(Lookup.lookupType(cache, env, path, false));
t = reparseType(path);
fargs = createFunctionArgsList2(rest,cache,env);
then (name,t)::fargs;
case (SCode.COMPONENT(component = name)::_,_,_)
equation
Debug.fprint("failtrace", "MetaUtil.createFunctionArgsList2 failed: ");
Debug.fprint("failtrace", name);
Debug.fprint("failtrace", "\n");
then fail();
end matchcontinue;
end createFunctionArgsList2;
*/
//Added by simbj
//Reparses type and returns the real type
//NOTE: This is probably a bad way to do this, should be moved, maybe to the parser?
function reparseType
input Absyn.Path path;
output DAE.Type outType;
algorithm
outType := matchcontinue(path)
local
DAE.Type t;
case(Absyn.IDENT("Integer")) then DAE.T_INTEGER_DEFAULT;
case(Absyn.IDENT("Real")) then DAE.T_REAL_DEFAULT;
case(Absyn.IDENT("String")) then DAE.T_STRING_DEFAULT;
case(Absyn.IDENT("Boolean")) then DAE.T_BOOL_DEFAULT;
end matchcontinue;
end reparseType;
public function mmc_mk_box "function: mmc_mk_box
Chooses mmc_mk_box<n>(ctor, ...) or mmc_mk_box(<n>
depending on the size of the box. /sjoelund"
input Integer numBoxes;
input String val;
output String out;
String numStr,tmp1,tmp2,tmp3;
algorithm
numStr := intString(numBoxes);
tmp1 := "(" +& numStr +& ",";
tmp2 := numStr +& "(";
tmp3 := Util.if_(numBoxes > 9, tmp1, tmp2); // mmc_mk_box<n>( or mmc_mk_box(<n>,
out := Util.stringAppendList({"mmc_mk_box",tmp3,val,")"});
end mmc_mk_box;
//Generates the mk_box<size>(<index>,<data>::<data>)
public function listToBoxes "function: listToBoxes
MetaModelica extension, added by simbj"
input list<String> varList;
input list<DAE.ExpType> expList;
input Integer index;
input String name;
output String outString;
algorithm
outString := matchcontinue (varList,expList,index,name)
local
String boxStr,expStr;
list<String> varList;
Integer numberOfVariables,index;
String tmp1,tmp2,tmp3,tmp4,tmp5;
case (varList,expList,index,name)
equation
numberOfVariables = listLength(expList)+1;
expStr = createExpStr(varList,expList);
tmp1 = intString(numberOfVariables);
tmp2 = intString(index+3 /* 0 and 1 are reserved by other types. 2 is reserved by regular records */);
tmp3 = "(" +& tmp1 +& ",";
tmp4 = tmp1 +& "(";
tmp5 = Util.if_(numberOfVariables > 9, tmp3, tmp4); // mmc_mk_box<n>( or mmc_mk_box(<n>,
name = Util.stringReplaceChar(name, ".", "_");
expStr = Util.stringAppendList({tmp2,",&",name,"__desc",expStr});
expStr = mmc_mk_box(numberOfVariables, expStr);
then expStr;
case (_,_,_,_)
equation
Debug.fprint("failtrace", "- MetaUtil.listToBoxes failed\n");
then fail();
end matchcontinue;
end listToBoxes;
public function createExpStr
input list<String> varList;
input list<DAE.ExpType> expList;
output String outString;
algorithm
outString := matchcontinue(varList,expList)
local
list<String> restVar;
list<DAE.ExpType> restExp;
String firstVar,restStr;
DAE.ExpType firstExp;
case ({},{}) then "";
case (firstVar::restVar,firstExp::restExp)
equation
firstVar = createConstantCExp2(firstExp,firstVar);
restStr = createExpStr(restVar,restExp);
firstVar = Util.stringAppendList({",",firstVar,restStr});
then firstVar;
end matchcontinue;
end createExpStr;
/* These functions are helper functions for the Uniontypes, added by simbj */
public function getRestriction
input Absyn.Element el;
output Absyn.Restriction res;
algorithm
res := matchcontinue(el)
local
Absyn.ElementSpec spec;