-
Notifications
You must be signed in to change notification settings - Fork 299
/
Mod.mo
2377 lines (2209 loc) · 76.5 KB
/
Mod.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 Mod
" file: Mod.mo
package: Mod
description: Modification handling
RCS: $Id$
Modifications are simply the same kind of modifications used in
the Absyn module.
This type is very similar to SCode.Mod.
The main difference is that it uses DAE.Exp for the expressions.
Expressions stored here are prefixed and typechecked.
The datatype itself is moved to the Types module, in Types.mo, to prevent circular dependencies."
public import Absyn;
public import DAE;
public import Env;
public import Prefix;
public import SCode;
public import RTOpts;
public import InnerOuter;
public
type Ident = String;
type InstanceHierarchy = InnerOuter.InstHierarchy "an instance hierarchy";
protected import Ceval;
protected import ClassInf;
protected import DAEUtil;
protected import Dump;
protected import Debug;
protected import Error;
protected import Exp;
protected import Inst;
protected import PrefixUtil;
protected import Print;
protected import Static;
protected import Types;
protected import Util;
protected import Values;
protected import ValuesUtil;
public function elabMod "
This function elaborates on the expressions in a modification and
turns them into global expressions. This is done because the
expressions in modifications must be elaborated on in the context
they are provided in, and not the context they are used in."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input SCode.Mod inMod;
input Boolean inBoolean;
output Env.Cache outCache;
output DAE.Mod outMod;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outMod) := matchcontinue (inCache,inEnv,inIH,inPrefix,inMod,inBoolean)
local
Boolean impl,finalPrefix;
list<DAE.SubMod> subs_1;
list<Env.Frame> env;
Prefix.Prefix pre;
SCode.Mod m,mod;
Absyn.Each each_;
list<SCode.SubMod> subs;
DAE.Exp e_1,e_2;
DAE.Properties prop;
Option<Values.Value> e_val;
Absyn.Exp e,e1;
list<tuple<SCode.Element, DAE.Mod>> elist_1;
list<SCode.Element> elist;
Ident str;
Env.Cache cache;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
// no modifications
case (cache,_,_,_,SCode.NOMOD(),impl) then (cache,DAE.NOMOD(),DAEUtil.emptyDae); /* impl */
// no top binding
case (cache,env,ih,pre,(m as SCode.MOD(finalPrefix = finalPrefix,eachPrefix = each_,subModLst = subs,absynExpOption = NONE)),impl)
equation
(cache,subs_1,dae1) = elabSubmods(cache, env, ih, pre, subs, impl);
then
(cache,DAE.MOD(finalPrefix,each_,subs_1,NONE),dae1);
// Only elaborate expressions with non-delayed type checking, see SCode.MOD.
case (cache,env,ih,pre,(m as SCode.MOD(finalPrefix = finalPrefix,eachPrefix = each_,subModLst = subs,absynExpOption = SOME((e,false)))),impl)
equation
(cache,subs_1,dae1) = elabSubmods(cache, env, ih, pre, subs, impl);
// print("Mod.elabMod: calling elabExp on mod exp: " +& Dump.printExpStr(e) +& " in env: " +& Env.printEnvPathStr(env) +& "\n");
(cache,e_1,prop,_,dae2) = Static.elabExp(cache, env, e, impl, NONE, true);
(cache,e_val) = elabModValue(cache, env, e_1, prop);
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre)
"Bug: will cause elaboration of parameters without value to fail,
But this can be ok, since a modifier is present, giving it a value from outer modifications.." ;
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,DAE.MOD(finalPrefix,each_,subs_1,SOME(DAE.TYPED(e_2,e_val,prop,SOME(e)))),dae);
// Delayed type checking
case (cache,env,ih,pre,(m as SCode.MOD(finalPrefix = finalPrefix,eachPrefix = each_,subModLst = subs,absynExpOption = SOME((e,true)))),impl)
equation
// print("Mod.elabMod: delayed mod : " +& Dump.printExpStr(e) +& " in env: " +& Env.printEnvPathStr(env) +& "\n");
(cache,subs_1,dae) = elabSubmods(cache, env, ih, pre, subs, impl);
then
(cache,DAE.MOD(finalPrefix,each_,subs_1,SOME(DAE.UNTYPED(e))),dae);
// redeclarations
case (cache,env,ih,pre,(m as SCode.REDECL(finalPrefix = finalPrefix,elementLst = elist)),impl)
equation
//elist_1 = Inst.addNomod(elist);
(elist_1,dae) = elabModRedeclareElements(cache,env,ih,pre,finalPrefix,elist,impl);
then
(cache,DAE.REDECL(finalPrefix,elist_1),dae);
// failure
case (cache,env,ih,pre,mod,impl)
equation
/*Debug.fprint("failtrace", "#-- elab_mod ");
str = SCode.printModStr(mod);
Debug.fprint("failtrace", str);
Debug.fprint("failtrace", " failed\n");
print("elab mod failed, mod:");print(str);print("\n");
print("env:");print(Env.printEnvStr(env));print("\n");*/
/* elab mod can fail? */
then
fail();
end matchcontinue;
end elabMod;
public function elabModForBasicType "
Same as elabMod, but if a named Mod is not part of a basic type, fail instead."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input SCode.Mod inMod;
input Boolean inBoolean;
output Env.Cache outCache;
output DAE.Mod outMod;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outMod,outDae) := matchcontinue (inCache,inEnv,inIH,inPrefix,inMod,inBoolean)
case (inCache,inEnv,inIH,inPrefix,inMod,inBoolean)
equation
checkIfModsAreBasicTypeMods(inMod);
(outCache,outMod,outDae) = elabMod(inCache,inEnv,inIH,inPrefix,inMod,inBoolean);
then (outCache,outMod,outDae);
end matchcontinue;
end elabModForBasicType;
protected function checkIfModsAreBasicTypeMods "
Verifies that a list of submods only have named modifications that could be
used for basic types."
input SCode.Mod mod;
algorithm
_ := matchcontinue mod
local
list<SCode.SubMod> subs;
case SCode.NOMOD() then ();
case SCode.MOD(subModLst = subs)
equation
checkIfSubmodsAreBasicTypeMods(subs);
then ();
end matchcontinue;
end checkIfModsAreBasicTypeMods;
protected function checkIfSubmodsAreBasicTypeMods "
Verifies that a list of submods only have named modifications that could be
used for basic types."
input list<SCode.SubMod> subs;
algorithm
_ := matchcontinue subs
local
SCode.Mod mod;
String ident;
case {} then ();
case SCode.NAMEMOD(ident = ident)::subs
equation
true = ClassInf.isBasicTypeComponentName(ident);
checkIfSubmodsAreBasicTypeMods(subs);
then ();
case SCode.IDXMOD(an = mod)::subs
equation
checkIfModsAreBasicTypeMods(mod);
checkIfSubmodsAreBasicTypeMods(subs);
then ();
end matchcontinue;
end checkIfSubmodsAreBasicTypeMods;
protected function elabModRedeclareElements
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input Boolean finalPrefix;
input list<SCode.Element> elts;
input Boolean impl;
output list<tuple<SCode.Element, DAE.Mod>> modElts "the elaborated modifiers";
output DAE.DAElist outDae "contain functions";
algorithm
(modElts,outDae) := matchcontinue(inCache,inEnv,inIH,inPrefix,finalPrefix,elts,impl)
local
Env.Cache cache; Env.Env env; Prefix.Prefix pre; Boolean f,fi,repl,p,enc,prot;
Absyn.InnerOuter io;
list<SCode.Element> elts;
SCode.Ident cn,cn2,compname;
Option<SCode.Comment> cmt;
SCode.Restriction restr;
Absyn.TypeSpec tp,tp1;
DAE.Mod emod;
SCode.Attributes attr;
SCode.Mod mod;
Option<Absyn.Exp> cond;
Option<Absyn.Info> info;
Option<Absyn.ConstrainClass> cc;
Option<SCode.Comment> cmt;
Absyn.Info i;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
/* the empty case */
case(cache,env,_,pre,f,{},_) then ({},DAEUtil.emptyDae);
// Only derived classdefinitions supported in redeclares for now. TODO: What is allowed according to spec?
case(cache,env,ih,pre,f,SCode.CLASSDEF(cn,fi,repl,SCode.CLASS(cn2,p,enc,restr,SCode.DERIVED(tp,mod,attr1,cmt),i),cc)::elts,impl)
local
Absyn.ElementAttributes attr1;
Option<Absyn.ConstrainClass> cc;
equation
(cache,emod,dae1) = elabMod(cache,env,ih,pre,mod,impl);
(modElts,dae2) = elabModRedeclareElements(cache,env,ih,pre,f,elts,impl);
(cache,tp1) = elabModQualifyTypespec(cache,env,tp);
dae = DAEUtil.joinDaes(dae1,dae2);
then ((SCode.CLASSDEF(cn,fi,repl,SCode.CLASS(cn,p,enc,restr,SCode.DERIVED(tp1,mod,attr1,cmt),i),cc),emod)::modElts, dae);
// replaceable type E=enumeration(e1,...,en), E=enumeration(:)
case(cache,env,ih,pre,f,SCode.CLASSDEF(cn,fi,repl,SCode.CLASS(cn2,p,enc,restr,SCode.ENUMERATION(enumLst,comment),i),cc)::elts,impl)
local
list<SCode.Enum> enumLst;
Option<SCode.Comment> comment;
Option<Absyn.ConstrainClass> cc;
equation
(modElts,dae) = elabModRedeclareElements(cache,env,ih,pre,f,elts,impl);
then ((SCode.CLASSDEF(cn,fi,repl,SCode.CLASS(cn,p,enc,restr,SCode.ENUMERATION(enumLst,comment),i),cc),DAE.NOMOD())::modElts,dae);
// redeclare of component declaration
case(cache,env,ih,pre,f,SCode.COMPONENT(compname,io,fi,repl,prot,attr,tp,mod,cmt,cond,info,cc)::elts,impl) equation
(cache,emod,dae1) = elabMod(cache,env,ih,pre,mod,impl);
(modElts,dae2) = elabModRedeclareElements(cache,env,ih,pre,f,elts,impl);
(cache,tp1) = elabModQualifyTypespec(cache,env,tp);
dae = DAEUtil.joinDaes(dae1,dae2);
then ((SCode.COMPONENT(compname,io,fi,repl,prot,attr,tp1,mod,cmt,cond,info,cc),emod)::modElts,dae);
end matchcontinue;
end elabModRedeclareElements;
protected function elabModQualifyTypespec
"Help function to elabModRedeclareElements.
This function makes sure that type specifiers, i.e. class names, in redeclarations are looked up in the correct environment.
This is achieved by making them fully qualified."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.TypeSpec tp;
output Env.Cache outCache;
output Absyn.TypeSpec outTp;
algorithm
(outCache,outTp) := matchcontinue(inCache,inEnv,tp)
local
Env.Cache cache; Env.Env env;
Option<Absyn.ArrayDim> ad;
Absyn.Path p,p1;
case (cache, env,Absyn.TPATH(p,ad)) equation
(cache,p1) = Inst.makeFullyQualified(cache,env,p);
then (cache,Absyn.TPATH(p1,ad));
end matchcontinue;
end elabModQualifyTypespec;
protected function elabModValue
"function: elabModValue
author: PA
Helper function to elabMod. Builds values from modifier expressions if possible.
Tries to Constant evaluate an expressions an create a Value option for it."
input Env.Cache inCache;
input Env.Env inEnv;
input DAE.Exp inExp;
input DAE.Properties inProp;
output Env.Cache outCache;
output Option<Values.Value> outValuesValueOption;
algorithm
(outCache,outValuesValueOption) :=
matchcontinue (inCache,inEnv,inExp,inProp)
case (_,_,_,_)
local
Values.Value v;
Ceval.Msg msg;
Env.Cache cache;
equation
// Don't ceval variables.
failure(DAE.C_VAR() = Types.propAllConst(inProp));
// Show error messages from ceval only if the expression is not a parameter.
msg = Util.if_(Util.equal(DAE.C_PARAM(), Types.propAllConst(inProp)), Ceval.NO_MSG(), Ceval.MSG());
(cache,v,_) = Ceval.ceval(inCache, inEnv, inExp, false, NONE, NONE, msg);
then
(cache,SOME(v));
// Constant evaluation failed, return no value.
case (_,_,_,_) then (inCache, NONE);
end matchcontinue;
end elabModValue;
public function unelabMod
"function: unelabMod
Transforms Mod back to SCode.Mod, loosing type information."
input DAE.Mod inMod;
output SCode.Mod outMod;
algorithm
outMod:=
matchcontinue (inMod)
local
list<SCode.SubMod> subs_1;
DAE.Mod m,mod;
Boolean finalPrefix;
Absyn.Each each_;
list<DAE.SubMod> subs;
Absyn.Exp e,e_1,absynExp;
Ident es,s;
DAE.Properties p;
list<SCode.Element> elist_1;
list<tuple<SCode.Element, DAE.Mod>> elist;
case (DAE.NOMOD()) then SCode.NOMOD();
case ((m as DAE.MOD(finalPrefix = finalPrefix,each_ = each_,subModLst = subs,eqModOption = NONE)))
equation
subs_1 = unelabSubmods(subs);
then
SCode.MOD(finalPrefix,each_,subs_1,NONE);
case ((m as DAE.MOD(finalPrefix = finalPrefix,each_ = each_,subModLst = subs,eqModOption = SOME(DAE.UNTYPED(e)))))
equation
subs_1 = unelabSubmods(subs);
then
SCode.MOD(finalPrefix,each_,subs_1,SOME((e,false))); // Default type checking non-delayed
case ((m as DAE.MOD(finalPrefix = finalPrefix,each_ = each_,subModLst = subs,
eqModOption = SOME(DAE.TYPED(e,_,p,SOME(absynExp))))))
local DAE.Exp e;
equation
//es = Exp.printExpStr(e);
subs_1 = unelabSubmods(subs);
e_1 = absynExp; //Exp.unelabExp(e);
then
SCode.MOD(finalPrefix,each_,subs_1,SOME((e_1,false))); // default typechecking non-delayed
case ((m as DAE.MOD(finalPrefix = finalPrefix,each_ = each_,subModLst = subs,
eqModOption = SOME(DAE.TYPED(e,_,p,NONE())))))
local DAE.Exp e;
equation
//es = Exp.printExpStr(e);
subs_1 = unelabSubmods(subs);
e_1 = Exp.unelabExp(e);
then
SCode.MOD(finalPrefix,each_,subs_1,SOME((e_1,false))); // default typechecking non-delayed
case ((m as DAE.REDECL(finalPrefix = finalPrefix,tplSCodeElementModLst = elist)))
equation
elist_1 = Util.listMap(elist, Util.tuple21);
then
SCode.REDECL(finalPrefix,elist_1);
case (mod)
equation
Print.printBuf("#-- Mod.elabUntypedMod failed: " +& printModStr(mod) +& "\n");
print("- Mod.elabUntypedMod failed :" +& printModStr(mod) +& "\n");
then
fail();
end matchcontinue;
end unelabMod;
protected function unelabSubmods
"function: unelabSubmods
Helper function to unelabMod."
input list<DAE.SubMod> inTypesSubModLst;
output list<SCode.SubMod> outSCodeSubModLst;
algorithm
outSCodeSubModLst:=
matchcontinue (inTypesSubModLst)
local
list<SCode.SubMod> x_1,xs_1,res;
DAE.SubMod x;
list<DAE.SubMod> xs;
case ({}) then {};
case ((x :: xs))
equation
x_1 = unelabSubmod(x);
xs_1 = unelabSubmods(xs);
res = listAppend(x_1, xs_1);
then
res;
end matchcontinue;
end unelabSubmods;
protected function unelabSubmod
"function: unelabSubmod
This function unelaborates on a submodification."
input DAE.SubMod inSubMod;
output list<SCode.SubMod> outSCodeSubModLst;
algorithm
outSCodeSubModLst:=
matchcontinue (inSubMod)
local
SCode.Mod m_1;
Ident i;
DAE.Mod m;
list<Absyn.Subscript> ss_1;
list<Integer> ss;
case (DAE.NAMEMOD(ident = i,mod = m))
equation
m_1 = unelabMod(m);
then
{SCode.NAMEMOD(i,m_1)};
case (DAE.IDXMOD(integerLst = ss,mod = m))
equation
ss_1 = unelabSubscript(ss);
m_1 = unelabMod(m);
then
{SCode.IDXMOD(ss_1,m_1)};
end matchcontinue;
end unelabSubmod;
protected function unelabSubscript
input list<Integer> inIntegerLst;
output list<SCode.Subscript> outSCodeSubscriptLst;
algorithm
outSCodeSubscriptLst:=
matchcontinue (inIntegerLst)
local
list<Absyn.Subscript> xs;
Integer i;
list<Integer> is;
case ({}) then {};
case ((i :: is))
equation
xs = unelabSubscript(is);
then
(Absyn.SUBSCRIPT(Absyn.INTEGER(i)) :: xs);
end matchcontinue;
end unelabSubscript;
public function updateMod
"function: updateMod
This function updates an untyped modification to a typed one, by looking
up the type of the modifier in the environment and update it."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input DAE.Mod inMod;
input Boolean inBoolean;
output Env.Cache outCache;
output DAE.Mod outMod;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outMod,outDae) :=
matchcontinue (inCache,inEnv,inIH,inPrefix,inMod,inBoolean)
local
Boolean impl,f;
DAE.Mod m;
list<DAE.SubMod> subs_1,subs;
DAE.Exp e_1,e_2;
DAE.Properties prop,p;
Option<Values.Value> e_val;
list<Env.Frame> env;
Prefix.Prefix pre;
Absyn.Each each_;
Absyn.Exp e;
Option<Absyn.Exp> eOpt;
Env.Cache cache;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
case (cache,_,_,_,DAE.NOMOD(),impl) then (cache,DAE.NOMOD(),DAEUtil.emptyDae);
case (cache,_,_,_,(m as DAE.REDECL(finalPrefix = _)),impl) then (cache,m,DAEUtil.emptyDae);
case (cache,env,ih,pre,(m as DAE.MOD(finalPrefix = f,each_ = each_,subModLst = subs,eqModOption = SOME(DAE.UNTYPED(e)))),impl)
equation
(cache,subs_1,dae1) = updateSubmods(cache, env, ih, pre, subs, impl);
(cache,e_1,prop,_,dae2) = Static.elabExp(cache, env, e, impl, NONE, true);
(cache,e_val) = elabModValue(cache,env,e_1,prop);
(cache,e_2) = PrefixUtil.prefixExp(cache, env, ih, e_1, pre);
Debug.fprint("updmod", "Updated mod: ");
Debug.fprintln("updmod", Debug.fcallret1("updmod", printModStr, DAE.MOD(f,each_,subs_1,SOME(DAE.TYPED(e_2,NONE,prop,SOME(e)))),""));
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,DAE.MOD(f,each_,subs_1,SOME(DAE.TYPED(e_2,e_val,prop,SOME(e)))),dae);
case (cache,env,ih,pre,DAE.MOD(finalPrefix = f,each_ = each_,subModLst = subs,eqModOption = SOME(DAE.TYPED(e_1,e_val,p,eOpt))),impl)
equation
(cache,subs_1,dae) = updateSubmods(cache, env, ih, pre, subs, impl);
then
(cache,DAE.MOD(f,each_,subs_1,SOME(DAE.TYPED(e_1,e_val,p,eOpt))),dae);
case (cache,env,ih,pre,DAE.MOD(finalPrefix = f,each_ = each_,subModLst = subs,eqModOption = NONE),impl)
equation
(cache,subs_1,dae) = updateSubmods(cache, env, ih, pre, subs, impl);
then
(cache,DAE.MOD(f,each_,subs_1,NONE),dae);
case (cache,env,ih,pre,m,impl)
local String str;
equation
true = RTOpts.debugFlag("failtrace");
str = printModStr(m);
Debug.traceln("- Mod.updateMod failed mod: " +& str);
then
fail();
end matchcontinue;
end updateMod;
protected function updateSubmods ""
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input list<DAE.SubMod> inTypesSubModLst;
input Boolean inBoolean;
output Env.Cache outCache;
output list<DAE.SubMod> outTypesSubModLst;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outTypesSubModLst,outDae):=
matchcontinue (inCache,inEnv,inIH,inPrefix,inTypesSubModLst,inBoolean)
local
Boolean impl;
list<DAE.SubMod> x_1,xs_1,res,xs;
list<Env.Frame> env;
Prefix.Prefix pre;
DAE.SubMod x;
Env.Cache cache;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
case (cache,_,ih,_,{},impl) then (cache,{},DAEUtil.emptyDae); /* impl */
case (cache,env,ih,pre,(x :: xs),impl)
equation
(cache,x_1,dae1) = updateSubmod(cache, env, ih, pre, x, impl);
(cache,xs_1,dae2) = updateSubmods(cache, env, ih, pre, xs, impl);
res = insertSubmods(x_1, xs_1, env, pre);
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,res,dae);
end matchcontinue;
end updateSubmods;
protected function updateSubmod " "
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input DAE.SubMod inSubMod;
input Boolean inBoolean;
output Env.Cache outCache;
output list<DAE.SubMod> outTypesSubModLst;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outTypesSubModLst,outDae):=
matchcontinue (outCache,inEnv,inIH,inPrefix,inSubMod,inBoolean)
local
DAE.Mod m_1,m;
list<Env.Frame> env;
Prefix.Prefix pre;
Ident i;
Boolean impl;
list<DAE.SubMod> smods;
Env.Cache cache;
list<Integer> idxmod;
DAE.DAElist dae;
InstanceHierarchy ih;
case (cache,env,ih,pre,DAE.NAMEMOD(ident = i,mod = m),impl) /* impl */
equation
(cache,m_1,dae) = updateMod(cache, env, ih, pre, m, impl);
then
(cache,{DAE.NAMEMOD(i,m_1)},dae);
case (cache,env,ih,pre,DAE.IDXMOD(mod = m,integerLst=idxmod),impl)
equation
(cache,m_1,dae) = updateMod(cache, env, ih, pre, m, impl) "Static.elab_subscripts (env,ss) => (ss\',true) &" ;
then
(cache,{DAE.IDXMOD(idxmod,m_1)},dae);
end matchcontinue;
end updateSubmod;
public function elabUntypedMod "function elabUntypedMod
This function is used to convert SCode.Mod into Mod, without
adding correct type information. Instead, a undefined type will be
given to the modification. This is used when modifications of e.g.
elements in base classes used. For instance,
model test extends A(x=y); end test; // both x and y are defined in A
The modifier x=y must be merged with outer modifiers, thus it needs
to be converted to Mod.
Notice that the correct type information must be updated later on."
input SCode.Mod inMod;
input Env.Env inEnv;
input Prefix.Prefix inPrefix;
output DAE.Mod outMod;
algorithm
outMod := matchcontinue (inMod,inEnv,inPrefix)
local
list<DAE.SubMod> subs_1;
SCode.Mod m,mod;
Boolean finalPrefix;
Absyn.Each each_;
list<SCode.SubMod> subs;
list<Env.Frame> env;
Prefix.Prefix pre;
Absyn.Exp e;
list<tuple<SCode.Element, DAE.Mod>> elist_1;
list<SCode.Element> elist;
Ident s;
case (SCode.NOMOD(),_,_) then DAE.NOMOD();
case ((m as SCode.MOD(finalPrefix = finalPrefix,eachPrefix = each_,subModLst = subs,absynExpOption = NONE)),env,pre)
equation
subs_1 = elabUntypedSubmods(subs, env, pre);
then
DAE.MOD(finalPrefix,each_,subs_1,NONE);
case ((m as SCode.MOD(finalPrefix = finalPrefix,eachPrefix = each_,subModLst = subs,absynExpOption = SOME((e,_)))),env,pre)
equation
subs_1 = elabUntypedSubmods(subs, env, pre);
then
DAE.MOD(finalPrefix,each_,subs_1,SOME(DAE.UNTYPED(e)));
case ((m as SCode.REDECL(finalPrefix = finalPrefix,elementLst = elist)),env,pre)
equation
elist_1 = Inst.addNomod(elist);
then
DAE.REDECL(finalPrefix,elist_1);
case (mod,env,pre)
equation
print("- elab_untyped_mod ");
s = SCode.printModStr(mod);
print(s);
print(" failed\n");
then
fail();
end matchcontinue;
end elabUntypedMod;
protected function elabSubmods
"function: elabSubmods
This function helps elabMod by recusively elaborating on a list of submodifications."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input list<SCode.SubMod> inSCodeSubModLst;
input Boolean inBoolean;
output Env.Cache outCache;
output list<DAE.SubMod> outTypesSubModLst;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outTypesSubModLst,outDAe) :=
matchcontinue (inCache,inEnv,inIH,inPrefix,inSCodeSubModLst,inBoolean)
local
Boolean impl;
list<DAE.SubMod> x_1,xs_1,res;
list<Env.Frame> env;
Prefix.Prefix pre;
SCode.SubMod x;
list<SCode.SubMod> xs;
Env.Cache cache;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
case (cache,_,_,_,{},impl) then (cache,{},DAEUtil.emptyDae); /* impl */
case (cache,env,ih,pre,(x :: xs),impl)
equation
(cache,x_1,dae1) = elabSubmod(cache, env, ih, pre, x, impl);
(cache,xs_1,dae2) = elabSubmods(cache, env, ih, pre, xs, impl);
res = insertSubmods(x_1, xs_1, env, pre);
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,res,dae);
end matchcontinue;
end elabSubmods;
protected function elabSubmod
"function: elabSubmod
This function elaborates on a submodification, turning an
SCode.SubMod into one or more DAE.SubMod."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Prefix.Prefix inPrefix;
input SCode.SubMod inSubMod;
input Boolean inBoolean;
output Env.Cache outCache;
output list<DAE.SubMod> outTypesSubModLst;
output DAE.DAElist outDae "contain functions";
algorithm
(outCache,outTypesSubModLst,outDae) :=
matchcontinue (inCache,inEnv,inIH,inPrefix,inSubMod,inBoolean)
local
DAE.Mod m_1;
list<Env.Frame> env;
Prefix.Prefix pre;
Ident i;
SCode.Mod m;
Boolean impl;
list<DAE.Subscript> ss_1;
list<DAE.SubMod> smods;
list<Absyn.Subscript> ss;
Env.Cache cache;
DAE.DAElist dae,dae1,dae2;
InstanceHierarchy ih;
case (cache,env,ih,pre,SCode.NAMEMOD(ident = i,A = m),impl) /* impl */
equation
(cache,m_1,dae) = elabMod(cache, env, ih, pre, m, impl);
then
(cache,{DAE.NAMEMOD(i,m_1)},dae);
case (cache,env,ih,pre,SCode.IDXMOD(subscriptLst = ss,an = m),impl)
equation
(cache,ss_1,DAE.C_CONST(),dae1) = Static.elabSubscripts(cache,env, ss, impl);
(cache,m_1,dae2) = elabMod(cache, env, ih, pre, m, impl);
smods = makeIdxmods(ss_1, m_1);
dae = DAEUtil.joinDaes(dae1,dae2);
then
(cache,smods,dae);
end matchcontinue;
end elabSubmod;
protected function elabUntypedSubmods "function: elabUntypedSubmods
This function helps `elab_untyped_mod\' by recusively elaborating on a list
of submodifications.
"
input list<SCode.SubMod> inSCodeSubModLst;
input Env.Env inEnv;
input Prefix.Prefix inPrefix;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inSCodeSubModLst,inEnv,inPrefix)
local
list<DAE.SubMod> x_1,xs_1,res;
SCode.SubMod x;
list<SCode.SubMod> xs;
list<Env.Frame> env;
Prefix.Prefix pre;
case ({},_,_) then {};
case ((x :: xs),env,pre)
equation
x_1 = elabUntypedSubmod(x, env, pre);
xs_1 = elabUntypedSubmods(xs, env, pre);
res = insertSubmods(x_1, xs_1, env, pre);
then
res;
end matchcontinue;
end elabUntypedSubmods;
protected function elabUntypedSubmod "function: elabUntypedSubmod
This function elaborates on a submodification, turning an
`SCode.SubMod\' into one or more `DAE.SubMod\'s, wihtout type information.
"
input SCode.SubMod inSubMod;
input Env.Env inEnv;
input Prefix.Prefix inPrefix;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inSubMod,inEnv,inPrefix)
local
DAE.Mod m_1;
Ident i;
SCode.Mod m;
list<Env.Frame> env;
Prefix.Prefix pre;
list<Absyn.Subscript> subcr;
case (SCode.NAMEMOD(ident = i,A = m),env,pre)
equation
m_1 = elabUntypedMod(m, env, pre);
then
{DAE.NAMEMOD(i,m_1)};
case (SCode.IDXMOD(subscriptLst = subcr,an = m),env,pre)
equation
m_1 = elabUntypedMod(m, env, pre);
then
{DAE.IDXMOD({-1},m_1)};
end matchcontinue;
end elabUntypedSubmod;
protected function makeIdxmods "function: makeIdxmods
From a list of list of integers, this function creates a list of
sub-modifications of the `IDXMOD\' variety.
"
input list<DAE.Subscript> inExpSubscriptLst;
input DAE.Mod inMod;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inExpSubscriptLst,inMod)
local
Integer x;
DAE.Mod m;
list<DAE.SubMod> mods,mods_1;
list<DAE.Subscript> xs;
case ({DAE.INDEX(exp = DAE.ICONST(integer = x))},m) then {DAE.IDXMOD({x},m)};
case ((DAE.INDEX(exp = DAE.ICONST(integer = x)) :: xs),m)
equation
mods = makeIdxmods(xs, m);
mods_1 = prefixIdxmods(mods, x);
then
mods_1;
case ((DAE.SLICE(exp = DAE.ARRAY(array = x)) :: xs),m)
local list<DAE.Exp> x;
equation
Print.printBuf("= expand_slice\n");
mods = expandSlice(x, xs, 1, m);
then
mods;
case ((DAE.WHOLEDIM() :: xs),m)
equation
print("# Sorry, [:] slices are not handled in modifications\n");
then
fail();
case(xs,m) equation
print("Mod.makeIdxmods failed for mod:");print(printModStr(m));print("\n");
print("subs =");print(Util.stringDelimitList(Util.listMap(xs,Exp.printSubscriptStr),","));
print("\n");
then fail();
end matchcontinue;
end makeIdxmods;
protected function prefixIdxmods "function: prefixIdxmods
This function adds a subscript to each `DAE.IDXMOD\' in a list of
submodifications.
"
input list<DAE.SubMod> inTypesSubModLst;
input Integer inInteger;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inTypesSubModLst,inInteger)
local
list<DAE.SubMod> mods_1,mods;
list<Integer> l;
DAE.Mod m;
Integer i;
case ({},_) then {};
case ((DAE.IDXMOD(integerLst = l,mod = m) :: mods),i)
equation
mods_1 = prefixIdxmods(mods, i);
then
(DAE.IDXMOD((i :: l),m) :: mods_1);
end matchcontinue;
end prefixIdxmods;
protected function expandSlice "function: expandSlice
This function goes through an array slice modification and creates
an singly indexed modification for each index in the slice. For
example, `x{2:3} = y\' is changed into `x{2} = y{1}\' and
`x{3} = y{2}\'.
"
input list<DAE.Exp> inExpExpLst;
input list<DAE.Subscript> inExpSubscriptLst;
input Integer inInteger;
input DAE.Mod inMod;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inExpExpLst,inExpSubscriptLst,inInteger,inMod)
local
DAE.Exp e_1,x,e,e_2;
tuple<DAE.TType, Option<Absyn.Path>> t_1,t;
list<DAE.SubMod> mods1,mods2,mods;
Integer n_1,n;
list<DAE.Exp> xs;
list<DAE.Subscript> ss;
DAE.Mod m,mod;
Boolean finalPrefix;
Absyn.Each each_;
Option<Values.Value> e_val;
DAE.Const const;
Ident str;
case ({},_,_,_) then {};
case ({},_,_,_) then {};
case ((x :: xs),ss,n,(m as DAE.MOD(finalPrefix = finalPrefix,each_ = each_,subModLst = {},
eqModOption = SOME(DAE.TYPED(e,e_val,DAE.PROP(t,const),_)))))
equation
e_2 = DAE.ICONST(n);
e_1 = Exp.simplify(DAE.ASUB(e,{e_2}));
t_1 = Types.unliftArray(t);
mods1 = makeIdxmods((DAE.INDEX(x) :: ss),
DAE.MOD(finalPrefix,each_,{},
SOME(DAE.TYPED(e_1,e_val,DAE.PROP(t_1,const),NONE()))));
n_1 = n + 1;
mods2 = expandSlice(xs, ss, n_1, m);
mods = listAppend(mods1, mods2);
then
mods;
case (_,_,_,mod)
equation
str = printModStr(mod);
Error.addMessage(Error.ILLEGAL_SLICE_MOD, {str});
then
fail();
end matchcontinue;
end expandSlice;
protected function expandList "function: expandList
This utility function takes a list of integer values and a list of
list of integers, and for each integer in the first and each list
in the second list creates a
list with that integer as head and the second list as tail. All
resulting lists are collected in a list and returned.
"
input list<Values.Value> inValuesValueLst;
input list<list<Integer>> inIntegerLstLst;
output list<list<Integer>> outIntegerLstLst;
algorithm
outIntegerLstLst:=
matchcontinue (inValuesValueLst,inIntegerLstLst)
local
list<list<Integer>> l1,l2,l,yy,ys;
list<Values.Value> xx,xs;
Integer x;
list<Integer> y;
case ({},_) then {};
case (_,{}) then {};
case ((xx as (Values.INTEGER(integer = x) :: xs)),(yy as (y :: ys)))
equation
l1 = expandList(xx, ys);
l2 = expandList(xs, yy);
l = listAppend(l1, l2);
then
((x :: y) :: l);
end matchcontinue;
end expandList;
protected function insertSubmods "function: insertSubmods
This function repeatedly calls `insert_submod\' to incrementally
insert several sub-modifications.
"
input list<DAE.SubMod> inTypesSubModLst1;
input list<DAE.SubMod> inTypesSubModLst2;
input Env.Env inEnv3;
input Prefix.Prefix inPrefix4;
output list<DAE.SubMod> outTypesSubModLst;
algorithm
outTypesSubModLst:=
matchcontinue (inTypesSubModLst1,inTypesSubModLst2,inEnv3,inPrefix4)
local
list<DAE.SubMod> x_1,xs_1,l,xs,y;
DAE.SubMod x;
list<Env.Frame> env;
Prefix.Prefix pre;
case ({},_,_,_) then {};