/
Inst.mo
15237 lines (14150 loc) · 608 KB
/
Inst.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 Inst
" file: Inst.mo
package: Inst
description: Model instantiation
RCS: $Id$
This module is responsible for instantiation of Modelica models.
The instantation is the process of instantiating model components,
flattening inheritance and generating equations from connect statements.
The instantiation process takes Modelica AST as defined in SCode and
produces variables and equations and algorithms, etc. as defined in DAE.
This module uses Lookup to lookup classes and variables from the
environment defined in Env. It uses Connect for generating equations from
connect statements. The type system defined in Types is used for
variable instantiation and type . DAE.Mod is used for modifiers and
merging of modifiers.
The extends language feature is performed by InstExtends. Instantiation of
algorithm sections and equation sections (including connections) is done
by InstSection.
There are basically four different ways/granularities of instantiation.
1. Using partialInstClassIn which only instantiates class definitions.
This function is used for looking up class definitions in e.g. packages.
For example, if looking up the class A.B.C, a new scope is opened and
A is partially instantiated in that scope using partialInstClassIn.
2. Function implicit instantiation. is the last argument of type bool to
instClassIn. It is needed since instantiation of functions is needed
to generate code for functions and there are cases where such
instantiations differ
from standard function instantiation. For example
function foo
input Real x{:};
...
end foo;
should be possible to instantiate even though the dimension size of x is
not known.
3. Implicit instantiation controlled by the next last argument to
instClassIn.
This is also needed, when a DAE should not be generated.
It is not clear when this is needed, perhaps it can be removed in the
future.
4. ???"
// public imports
public import Absyn;
public import ClassInf;
public import Connect;
public import ConnectionGraph;
public import DAE;
public import Env;
public import InnerOuter;
public import Mod;
public import Prefix;
public import RTOpts;
public import SCode;
public import UnitAbsyn;
public
constant Boolean alwaysUnroll = true;
constant Boolean neverUnroll = false;
// the index of the inst hash table in the global table
constant Integer instHashIndex = 0;
// **
// These type aliases are introduced to make the code a little more readable.
// **
public
type Ident = DAE.Ident "an identifier";
public
type InstanceHierarchy = InnerOuter.InstHierarchy "an instance hierarchy";
public uniontype CallingScope "
Calling scope is used to determine when unconnected flow variables should be set to zero."
record TOP_CALL "this is a top call" end TOP_CALL;
record INNER_CALL "this is an inner call" end INNER_CALL;
end CallingScope;
public type InstDims = list<list<DAE.Subscript>>
"Changed from list<Subscript> to list<list<Subscript>>. One list for each scope.
This so when instantiating classes extending from primitive types can collect the dimension of -one- surrounding scope to create type.
E.g. RealInput p[3]; gives the list {3} for this scope and other lists for outer (in instance hierachy) scopes";
// protected imports
protected import BaseHashTable;
protected import Builtin;
protected import Ceval;
protected import ConnectUtil;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Dump;
protected import Error;
protected import ErrorExt;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import HashTable;
protected import HashTable5;
protected import InstSection;
protected import InstExtends;
protected import Interactive;
protected import Lookup;
protected import MetaUtil;
protected import ModUtil;
protected import OptManager;
protected import PrefixUtil;
protected import SCodeUtil;
protected import Static;
protected import Types;
protected import UnitAbsynBuilder;
protected import UnitChecker;
protected import UnitParserExt;
protected import Util;
protected import Values;
protected import ValuesUtil;
protected import System;
public function newIdent
"function: newIdent
This function creates a new, unique identifer.
The same name is never returned twice."
output DAE.ComponentRef outComponentRef;
Integer i;
String is,s;
algorithm
i := tick();
is := intString(i);
s := stringAppend("__TMP__", is);
outComponentRef := ComponentReference.makeCrefIdent(s,DAE.ET_OTHER(),{});
end newIdent;
protected function isNotFunction
"function: isNotFunction
This function returns true if the Class is not a function."
input SCode.Class cls;
output Boolean res;
algorithm
res := SCode.isFunction(cls);
res := boolNot(res);
end isNotFunction;
public function instantiate
"function: instantiate
To instantiate a Modelica program, an initial environment is
built, containing the predefined types. Then the program is
instantiated by the function instProgram"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
output Env.Cache outCache;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
output SCode.Program outProgram;
algorithm
(outCache,outIH,outDAElist,outProgram) := matchcontinue (inCache,inIH,inProgram)
local
list<SCode.Class> pnofunc,pfunc,p,p_1;
list<Env.Frame> env,envimpl,envimpl_1;
list<String> pfuncnames,pnofuncnames;
String str1,str2;
DAE.DAElist lfunc,lnofunc,l;
Env.Cache cache;
InstanceHierarchy oIH1, oIH2, iIH;
case (cache,iIH,p)
equation
// Debug.fprintln("insttr", "instantiate");
pnofunc = Util.listSelect(p, isNotFunction);
pfunc = Util.listSelect(p, SCode.isFunction);
(cache,env) = Builtin.initialEnv(cache);
// Debug.fprintln("insttr", "Instantiating functions");
// pfuncnames = Util.listMap(pfunc, SCode.className);
// str1 = Util.stringDelimitList(pfuncnames, ", ");
// Debug.fprint("insttr", "Instantiating functions: ");
// Debug.fprintln("insttr", str1);
envimpl = Env.extendFrameClasses(env, p) "pfunc" ;
(cache,envimpl_1,oIH1,lfunc) = instProgramImplicit(cache, envimpl, iIH, pfunc);
// Debug.fprint("insttr", "Instantiating other classes: ");
// pnofuncnames = Util.listMap(pnofunc, SCode.className);
// str2 = Util.stringDelimitList(pnofuncnames, ", ");
// Debug.fprintln("insttr", str2);
(cache, oIH2, lnofunc) = instProgram(cache, envimpl_1, oIH1, pnofunc);
l = DAEUtil.joinDaes(lfunc, lnofunc);
// p_1 = addElaboratedFuncsToProgram(cache,env,p); // stefan
then
(cache,oIH2,l,p);
case (_,_,_)
equation
//Debug.fprintln("failtrace", "Inst.instantiate failed");
then
fail();
end matchcontinue;
end instantiate;
public function instantiateImplicit
"function: instantiateImplicit
Implicit instantiation of a program can be used for e.g. code generation
of functions, since a function must be implicitly instantiated in order to
generate code from it."
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
output Env.Cache outCache;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
algorithm
(outCache,outIH,outDAElist) := matchcontinue (inCache,inIH,inProgram)
local
list<Env.Frame> env,env_1;
DAE.DAElist l;
list<SCode.Class> p;
Env.Cache cache;
case (cache,inIH,p)
equation
// Debug.fprintln("insttr", "instantiate_implicit");
(cache,env) = Builtin.initialEnv(cache);
env_1 = Env.extendFrameClasses(env, p);
(cache,_,outIH,l) = instProgramImplicit(cache,env_1,inIH, p);
then
(cache,outIH,l);
case (_,_,_)
equation
//Debug.fprintln("failtrace", "Inst.instantiateImplicit failed");
then
fail();
end matchcontinue;
end instantiateImplicit;
public function instantiateClass
"function: instantiateClass
To enable interactive instantiation, an arbitrary class in the program
needs to be possible to instantiate. This function performs the same
action as instProgram, but given a specific class to instantiate.
First, all the class definitions are added to the environment without
modifications, and then the specified class is instantiated in the
function instClassInProgram"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
algorithm
(outCache,outEnv,outIH,outDAElist) := matchcontinue (inCache,inIH,inProgram,inPath)
local
Absyn.Path cr,path;
list<Env.Frame> env,env_1,env_2;
DAE.DAElist dae1,dae,dae2;
list<SCode.Class> cdecls;
String name2,n,pathstr,name,cname_str;
SCode.Class cdef;
Env.Cache cache;
InstanceHierarchy ih;
ConnectionGraph.ConnectionGraph graph;
DAE.ElementSource source "the origin of the element";
DAE.FunctionTree funcs;
list<DAE.Element> daeElts;
case (cache,ih,{},cr)
equation
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.IDENT(name = name2))) /* top level class */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,dae1) = instClassDecls(cache, env, ih, cdecls, path);
(cache,env_2,ih,dae2) = instClassInProgram(cache, env_1, ih, cdecls, path);
// check the models for balancing
//Debug.fcall2("checkModel", checkModelBalancing, SOME(path), dae1);
//Debug.fcall2("checkModel", checkModelBalancing, SOME(path), dae2);
// set the source of this element
source = DAEUtil.addElementSourcePartOfOpt(DAE.emptyElementSource, Env.getEnvPath(env));
daeElts = DAEUtil.daeElements(dae2);
dae2 = DAE.DAE({DAE.COMP(name2,daeElts,source,NONE())});
// let the GC collect these as they are used only by Inst!
setGlobalRoot(instHashIndex, emptyInstHashTable());
setGlobalRoot(Types.memoryIndex, Types.createEmptyTypeMemory());
then
(cache,env_2,ih,dae2);
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.QUALIFIED(name = name))) /* class in package */
equation
pathstr = Absyn.pathString(path);
//System.startTimer();
//print("\nBuiltinMaking");
(cache,env) = Builtin.initialEnv(cache);
//System.stopTimer();
//print("\nBuiltinMaking: " +& realString(System.getTimerIntervalTime()));
//System.startTimer();
//print("\nInstClassDecls");
(cache,env_1,ih,_) = instClassDecls(cache, env, ih, cdecls, path);
//System.stopTimer();
//print("\nInstClassDecls: " +& realString(System.getTimerIntervalTime()));
//System.startTimer();
//print("\nLookupClass");
(cache,(cdef as SCode.CLASS(name = n)),env_2) = Lookup.lookupClass(cache, env_1, path, true);
//System.stopTimer();
//print("\nLookupClass: " +& realString(System.getTimerIntervalTime()));
//System.startTimer();
//print("\nInstClass");
(cache,env_2,ih,_,dae,_,_,_,_,graph) = instClass(cache,env_2,ih,
UnitAbsynBuilder.emptyInstStore(),DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cdef, {}, false, TOP_CALL(), ConnectionGraph.EMPTY) "impl";
//System.stopTimer();
//print("\nInstClass: " +& realString(System.getTimerIntervalTime()));
//System.startTimer();
//print("\nOverConstrained");
// deal with Overconstrained connections
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae, Absyn.pathString(path));
//System.stopTimer();
//print("\nOverconstrained: " +& realString(System.getTimerIntervalTime()));
//System.startTimer();
//print("\nReEvaluateIf");
//print(" ********************** backpatch 1 **********************\n");
dae = reEvaluateInitialIfEqns(cache,env_2,dae,true);
//System.stopTimer();
//print("\nReEvaluateIf: " +& realString(System.getTimerIntervalTime()));
// check the model for balancing
// Debug.fcall2("checkModel", checkModelBalancing, SOME(path), dae);
//System.startTimer();
//print("\nSetSource+DAE");
// set the source of this element
source = DAEUtil.addElementSourcePartOfOpt(DAE.emptyElementSource, Env.getEnvPath(env));
daeElts = DAEUtil.daeElements(dae);
dae = DAE.DAE({DAE.COMP(pathstr,daeElts,source,NONE())});
//System.stopTimer();
//print("\nSetSource+DAE: " +& realString(System.getTimerIntervalTime()));
// let the GC collect these as they are used only by Inst!
setGlobalRoot(instHashIndex, emptyInstHashTable());
setGlobalRoot(Types.memoryIndex, Types.createEmptyTypeMemory());
then
(cache, env_2, ih, dae);
case (cache,ih,cdecls,path) /* error instantiating */
equation
cname_str = Absyn.pathString(path);
Error.addMessage(Error.ERROR_FLATTENING, {cname_str});
then
fail();
end matchcontinue;
end instantiateClass;
protected 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 constand, we return only the 'correct' branch equations."
input Env.Cache cache;
input Env.Env env;
input DAE.DAElist dae;
input Boolean isTopCall;
output DAE.DAElist odae;
algorithm odae := matchcontinue(cache,env,dae,isTopCall)
local
DAE.FunctionTree funcs;
list<DAE.Element> elems;
case(cache,env,DAE.DAE(elementLst = elems),true)
equation
elems = reEvaluateInitialIfEqns2(cache,env,elems);
then
DAE.DAE(elems);
case(_,_,dae,false) then dae;
end matchcontinue;
end reEvaluateInitialIfEqns;
protected function reEvaluateInitialIfEqns2 ""
input Env.Cache cache;
input Env.Env env;
input list<DAE.Element> elems;
output list<DAE.Element> oelems;
algorithm oelems := matchcontinue(cache,env,elems)
local
list<DAE.Exp> conds;
list<Values.Value> valList;
list<list<DAE.Element>> tbs;
list<DAE.Element> fb,selectedBranch;
DAE.Element elem;
DAE.ElementSource source;
list<Boolean> blist;
case(_,_,{}) then {};
case(cache,env,(elem as DAE.INITIAL_IF_EQUATION(condition1 = conds, equations2=tbs, equations3=fb, source=source))::elems)
equation
//print(" (Initial if)To ceval: " +& Util.stringDelimitList(Util.listMap(conds,ExpressionDump.printExpStr),", ") +& "\n");
(cache,valList) = Ceval.cevalList(cache,env, conds, true,NONE(), Ceval.NO_MSG());
//print(" Ceval res: ("+&Util.stringDelimitList(Util.listMap(valList,ValuesUtil.printValStr),",")+&")\n");
blist = Util.listMap(valList,ValuesUtil.valueBool);
selectedBranch = Util.selectList(blist, tbs, fb);
selectedBranch = makeDAEElementInitial(selectedBranch);
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
oelems = listAppend(selectedBranch,oelems);
//print("RETURN _INITIAL_ DAE: " +& DAEDump.dumpDAEElementsStr(DAE.DAE(selectedBranch,DAE.AVLTREENODE(NONE(),0,NONE(),NONE()))) +& "\n");
//print(" INSTEAD OF: " +& DAEDump.dumpDAEElementsStr(DAE.DAE({elem},DAE.AVLTREENODE(NONE(),0,NONE(),NONE()))) +& "\n");
then
oelems;
case(cache,env,elem::elems)
equation
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
then
elem::oelems;
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,e3;
DAE.ElementSource s;
list<DAE.Exp> expl;
list<list<DAE.Element>> tbs ;
list<DAE.Element> fb;
DAE.Algorithm al;
list<DAE.Dimension> dims;
case({}) then {};
case(DAE.DEFINE(cr,e1,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALDEFINE(cr,e1,s)::outElems;
case(DAE.ARRAY_EQUATION(dims,e1,e2,s)::_)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_ARRAY_EQUATION(dims,e1,e2,s)::outElems;
case(DAE.EQUATION(e1,e2,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALEQUATION(e1,e2,s)::outElems;
case(DAE.IF_EQUATION(expl,tbs,fb,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_IF_EQUATION(expl,tbs,fb,s)::outElems;
case(DAE.ALGORITHM(al,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIALALGORITHM(al,s)::outElems;
case(DAE.COMPLEX_EQUATION(e1,e2,s)::inElems)
equation
outElems = makeDAEElementInitial(inElems);
then
DAE.INITIAL_COMPLEX_EQUATION(e1,e2,s)::outElems;
case(elem::inElems) // safe "last case" since we can not fail in cases above.
equation
outElems = makeDAEElementInitial(inElems);
then
elem::outElems;
end matchcontinue;
end makeDAEElementInitial;
public function instantiatePartialClass
"Author: BZ, 2009-07
This is a function for instantiating partial 'top' classes.
It does so by converting the partial class into a non partial class.
Currently used by: MathCore.modelEquations, CevalScript.checkModel"
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
algorithm
(outCache,outEnv,outIH,outDAElist) := matchcontinue (inCache,inIH,inProgram,inPath)
local
Absyn.Path cr,path;
list<Env.Frame> env,env_1,env_2;
DAE.DAElist dae1,dae;
list<SCode.Class> cdecls;
String name2,n,pathstr,name,cname_str;
SCode.Class cdef;
Env.Cache cache;
InstanceHierarchy ih;
DAE.ElementSource source "the origin of the element";
list<DAE.Element> daeElts;
DAE.FunctionTree funcs;
case (cache,ih,{},cr)
equation
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.IDENT(name = name2))) /* top level class */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,dae1) = instClassDecls(cache, env, ih, cdecls, path);
cdecls = Util.listMap1(cdecls,SCode.classSetPartial,false);
(cache,env_2,ih,dae) = instClassInProgram(cache, env_1, ih, cdecls, path);
// set the source of this element
source = DAEUtil.addElementSourcePartOfOpt(DAE.emptyElementSource, Env.getEnvPath(env));
daeElts = DAEUtil.daeElements(dae);
dae = DAE.DAE({DAE.COMP(name2,daeElts,source,NONE())});
then
(cache,env_2,ih,dae);
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.QUALIFIED(name = name))) /* class in package */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,_) = instClassDecls(cache, env, ih, cdecls, path);
(cache,(cdef as SCode.CLASS(name = n)),env_2) = Lookup.lookupClass(cache,env_1, path, true);
cdef = SCode.classSetPartial(cdef, false);
(cache,env_2,ih,_,dae,_,_,_,_,_) =
instClass(cache, env_2, ih, UnitAbsynBuilder.emptyInstStore(),DAE.NOMOD(), Prefix.NOPRE(),
Connect.emptySet, cdef, {}, false, TOP_CALL(), ConnectionGraph.EMPTY) "impl" ;
pathstr = Absyn.pathString(path);
// set the source of this element
source = DAEUtil.addElementSourcePartOfOpt(DAE.emptyElementSource, Env.getEnvPath(env));
daeElts = DAEUtil.daeElements(dae);
dae = DAE.DAE({DAE.COMP(pathstr,daeElts,source,NONE())});
then
(cache,env_2,ih,dae);
case (cache,ih,cdecls,path) /* error instantiating */
equation
cname_str = Absyn.pathString(path);
//print(" Error flattening partial, errors: " +& ErrorExt.printMessagesStr() +& "\n");
Error.addMessage(Error.ERROR_FLATTENING, {cname_str});
then
fail();
end matchcontinue;
end instantiatePartialClass;
public function instantiateClassImplicit
"function: instantiateClassImplicit
author: PA
Similar to instantiate_class, i.e. instantation of arbitrary classes
but this one instantiates the class implicit, which is less costly."
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDAElist;
algorithm
(outCache,outEnv,outIH,outDAElist) := matchcontinue (inCache,inIH,inProgram,inPath)
local
Absyn.Path cr,path;
list<Env.Frame> env,env_1,env_2;
DAE.DAElist dae1,dae;
list<SCode.Class> cdecls;
String name2,n,name;
SCode.Class cdef;
Env.Cache cache;
InstanceHierarchy ih;
case (cache,ih,{},cr)
equation
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.IDENT(name = name2))) /* top level class */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,dae1) = instClassDecls(cache, env, ih, cdecls, path);
(cache,env_2,ih,dae) = instClassInProgramImplicit(cache, env_1, ih, cdecls, path);
then
(cache,env_2,ih,dae);
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.QUALIFIED(name = name))) /* class in package */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,_) = instClassDecls(cache, env, ih, cdecls, path);
(cache,(cdef as SCode.CLASS(name = n)),env_2) = Lookup.lookupClass(cache,env_1, path, true);
env_2 = Env.extendFrameC(env_2, cdef);
(cache, env, ih, dae) = implicitInstantiation(cache, env_2, ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cdef, {});
then
(cache,env,ih,dae);
case (_,_,_,_)
equation
print("-Inst.instantiateClassImplicit failed\n");
then
fail();
end matchcontinue;
end instantiateClassImplicit;
public function instantiateFunctionImplicit
"function: instantiateFunctionImplicit
author: PA
Similar to instantiateClassImplict, i.e. instantation of arbitrary
classes but this one instantiates the class implicit for functions."
input Env.Cache inCache;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
algorithm
(outCache,outEnv,outIH) := matchcontinue (inCache,inIH,inProgram,inPath)
local
Absyn.Path cr,path;
list<Env.Frame> env,env_1,env_2;
DAE.DAElist dae1,dae;
list<SCode.Class> cdecls;
String name2,n,name,s;
SCode.Class cdef;
Env.Cache cache;
DAE.DAElist daelst;
InstanceHierarchy ih;
// Fully qualified paths
case (cache,ih,cdecls,Absyn.FULLYQUALIFIED(path))
equation
(cache,env,ih) = instantiateFunctionImplicit(cache,ih,cdecls,path);
then
(cache,env,ih);
case (cache,ih,{},cr)
equation
Error.addMessage(Error.NO_CLASSES_LOADED, {});
then
fail();
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.IDENT(name = name2))) /* top level class */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,_) = instClassDecls(cache, env, ih, cdecls, path);
(cache,env_2,ih) = instFunctionInProgramImplicit(cache, env_1, ih, cdecls, path);
then
(cache,env_2,ih);
case (cache,ih,(cdecls as (_ :: _)),(path as Absyn.QUALIFIED(name = name))) /* class in package */
equation
(cache,env) = Builtin.initialEnv(cache);
(cache,env_1,ih,_) = instClassDecls(cache, env, ih, cdecls, path);
(cache,(cdef as SCode.CLASS(name = n)),env_2) = Lookup.lookupClass(cache,env_1, path, true);
env_2 = Env.extendFrameC(env_2, cdef);
(cache,env,ih) = implicitFunctionInstantiation(cache, env_2, ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, cdef, {});
then
(cache,env,ih);
case (_,_,_,path)
equation
//print("-instantiateFunctionImplicit ");print(Absyn.pathString(path));print(" failed\n");
true = RTOpts.debugFlag("failtrace");
Debug.fprint("failtrace", "-Inst.instantiateFunctionImplicit " +& Absyn.pathString(path) +& " failed\n");
then
fail();
end matchcontinue;
end instantiateFunctionImplicit;
protected function instClassInProgram
"function: instClassInProgram
Instantitates a specifc class in a Program.
The class must reside on top level."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDae;
algorithm
(outCache,outEnv,outIH,outDae) := matchcontinue (inCache,inEnv,inIH,inProgram,inPath)
local
DAE.DAElist dae, dae2;
list<Env.Frame> env_1,env;
SCode.Class c;
String name,name2;
list<SCode.Class> cs;
Absyn.Path path;
Env.Cache cache;
InstanceHierarchy ih;
ConnectionGraph.ConnectionGraph graph;
// The class and the path match => instantiate the class.
case (cache,env,ih,((c as SCode.CLASS(name = name)) :: cs),Absyn.IDENT(name = name2))
equation
true = stringEq(name, name2);
(cache,env_1,ih,_,dae,_,_,_,_,graph) = instClass(cache,env, ih,
UnitAbsynBuilder.emptyInstStore(), DAE.NOMOD(), Prefix.NOPRE(),
Connect.emptySet, c, {}, false, TOP_CALL(), ConnectionGraph.EMPTY) "impl" ;
// deal with Overconstrained connections
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae, name);
//print(" ********************** backpatch 2 **********************\n");
dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);
// check the models for balancing
//Debug.fcall2("checkModel",checkModelBalancing,SOME(inPath),dae);
then
(cache,env_1,ih,dae);
// The class does not match the path, and no more classes left => error.
case (cache,env,ih,((c as SCode.CLASS(name = name)) :: {}),(path as Absyn.IDENT(name = name2)))
equation
false = stringEq(name, name2);
false = stringEq(name2, "");
Error.addMessage(Error.LOAD_MODEL_ERROR, {name2});
then
fail();
// The class does not match the path, but there are more classes left => continue searching for a matching class.
case (cache,env,ih,((c as SCode.CLASS(name = name)) :: cs),(path as Absyn.IDENT(name = name2)))
equation
false = stringEq(name, name2);
_::_ = cs; // non empty list
(cache,env,ih,dae) = instClassInProgram(cache, env, ih, cs, path);
then
(cache,env,ih,dae);
case (cache,env,ih,{},_)
then (cache,env,ih,DAEUtil.emptyDae);
case (cache,env,ih,_,_)
equation
Debug.fprintln("failtrace", "Inst.instClassInProgram failed");
then fail();
end matchcontinue;
end instClassInProgram;
protected function instClassInProgramImplicit
"function: instClassInProgramImplicit
Instantitates a specifc class in a Program using implicit instatiation.
The class must reside on top level."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDae;
algorithm
(outCache,outEnv,outIH,outDae) := matchcontinue (inCache,inEnv,inIH,inProgram,inPath)
local
list<Env.Frame> env_1,env;
DAE.DAElist dae;
SCode.Class c;
String name,name2;
list<SCode.Class> cs;
Absyn.Path path;
Env.Cache cache;
InstanceHierarchy ih;
case (cache,env,ih,((c as SCode.CLASS(name = name)) :: cs),Absyn.IDENT(name = name2))
equation
true = stringEq(name, name2);
env = Env.extendFrameC(env, c);
(cache,env_1,ih,dae) = implicitInstantiation(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {}) ;
then
(cache,env_1,ih,dae);
case (cache,env,ih,((c as SCode.CLASS(name = name)) :: cs),(path as Absyn.IDENT(name = name2)))
equation
false = stringEq(name, name2);
(cache,env,ih,dae) = instClassInProgramImplicit(cache, env, ih, cs, path);
then
(cache,env,ih,dae);
case (cache,env,ih,{},_)
then (cache,env,ih,DAEUtil.emptyDae);
case (_,env,ih,_,_)
equation
Debug.fprint("failtrace", "Inst.instClassInProgramImplicit failed");
then fail();
end matchcontinue;
end instClassInProgramImplicit;
protected function instFunctionInProgramImplicit
"function: instFunctionInProgramImplicit
Instantitates a specific function in a Program using implicit instatiation.
The class must reside on top level."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
algorithm
(outCache,outEnv,outIH) := matchcontinue (inCache,inEnv,inIH,inProgram,inPath)
local
list<Env.Frame> env_1,env;
DAE.DAElist dae;
SCode.Class c;
String name1,name2;
list<SCode.Class> cs;
Absyn.Path path;
Env.Cache cache;
InstanceHierarchy ih;
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),Absyn.IDENT(name = name2))
equation
true = stringEq(name1, name2);
env = Env.extendFrameC(env, c);
(cache,env_1,ih) = implicitFunctionInstantiation(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {});
then
(cache,env_1,ih);
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),(path as Absyn.IDENT(name = name2)))
equation
false = stringEq(name1, name2);
(cache,env,ih) = instFunctionInProgramImplicit(cache,env,ih, cs, path);
then
(cache,env,ih);
case (cache,env,ih,{},_) then (cache,env,ih);
case (cache,env,ih,_,_) then fail();
end matchcontinue;
end instFunctionInProgramImplicit;
protected function instClassDecls
"function: instClassDecls
This function instantiated class definitions, i.e.
adding the class definitions to the environment.
See also partialInstClassIn."
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input SCode.Program inProgram;
input SCode.Path inPath;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output DAE.DAElist outDae;
algorithm
(outCache,outEnv,outIH,outDae) := matchcontinue (inCache,inEnv,inIH,inProgram,inPath)
local
list<Env.Frame> env_1,env_2,env;
DAE.DAElist dae1,dae2,dae;
SCode.Class c;
String name1,name2,str;
list<SCode.Class> cs;
Absyn.Path ref;
Env.Cache cache;
InstanceHierarchy ih;
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),(ref as Absyn.IDENT(name = name2)))
equation
false = stringEq(name1, name2);
(cache,env_1,ih,dae1) = instClassDecl(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {}) ;
(cache,env_2,ih,dae2) = instClassDecls(cache,env_1,ih, cs, ref);
dae = DAEUtil.joinDaes(dae1, dae2);
then
(cache,env_2,ih,dae);
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),(ref as Absyn.IDENT(name = name2)))
equation
true = stringEq(name1, name2);
(cache,env_1,ih,dae2) = instClassDecls(cache,env,ih, cs, ref);
then
(cache,env_1,ih,dae2);
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),(ref as Absyn.QUALIFIED(name = name2)))
equation
true = stringEq(name1, name2);
(cache,env_1,ih,dae1) = instClassDecl(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {});
(cache,env_2,ih,dae2) = instClassDecls(cache,env_1,ih, cs, ref);
dae = DAEUtil.joinDaes(dae1, dae2);
then
(cache,env_2,ih,dae);
case (cache,env,ih,((c as SCode.CLASS(name = name1)) :: cs),(ref as Absyn.QUALIFIED(name = name2)))
equation
false = stringEq(name1, name2);
(cache,env_1,ih,dae1) = instClassDecl(cache,env,ih, DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {}) ;
(cache,env_2,ih,dae2) = instClassDecls(cache,env_1,ih, cs, ref);
dae = DAEUtil.joinDaes(dae1, dae2);
then
(cache,env_2,ih,dae);
case (cache,env,ih,{},_) then (cache,env,ih,DAEUtil.emptyDae);
case (_,_,ih,_,ref)
equation
print("Inst.instClassDecls failed\n ref =" +& Absyn.pathString(ref) +& "\n");
then
fail();
end matchcontinue;
end instClassDecls;
public function makeEnvFromProgram
"function: makeEnvFromProgram
This function takes a SCode.Program and builds
an environment, excluding the class in A1."
input Env.Cache inCache;
input SCode.Program prog;
input SCode.Path c;
output Env.Cache outCache;
output Env.Env env_1;
list<Env.Frame> env;
Env.Cache cache;
algorithm
(cache,env) := Builtin.initialEnv(inCache);
(outCache,env_1,_) := addProgramToEnv(cache,env,InnerOuter.emptyInstHierarchy, prog, c);
end makeEnvFromProgram;
public function makeSimpleEnvFromProgram
"function: makeSimpleEnvFromProgram
Similar as to makeEnvFromProgram, but not using the complete builtin
environment, but a more simple one without the builtin operators.
See also: Builtin.simpleInitialEnv."
input Env.Cache inCache;
input SCode.Program prog;
input SCode.Path c;
output Env.Cache outCache;
output Env.Env env_1;
list<Env.Frame> env;
algorithm
env := Builtin.simpleInitialEnv();
(outCache,env_1,_) := addProgramToEnv(inCache,env,InnerOuter.emptyInstHierarchy, prog, c);
end makeSimpleEnvFromProgram;
protected function addProgramToEnv
"function: addProgramToEnv
Adds all classes in a Program to the environment."
input Env.Cache inCache;
input Env.Env env;
input InstanceHierarchy inIH;
input SCode.Program p;
input SCode.Path path;
output Env.Cache outCache;
output Env.Env env_1;
output InstanceHierarchy outIH;
algorithm
(outCache,env_1,outIH,_) := instClassDecls(inCache,env,inIH, p, path);
end addProgramToEnv;
protected function instProgram
"function: instProgram
Instantiating a Modelica program is the same as instantiating the
last class definition in the source file. First all the class
definitions is added to the environment without modifications, and