/
Env.mo
2449 lines (2226 loc) · 71.6 KB
/
Env.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.
*
*/
encapsulated package Env
"
file: Env.mo
package: Env
description: Environmane management
RCS: $Id$
An environment is a stack of frames, where each frame contains a
number of class and variable bindings.
Each frame consist of:
- a frame name (corresponding to the class partially instantiated in that frame)
- a binary tree containing a list of classes
- a binary tree containing a list of functions (functions are overloaded so serveral
function names can exist)
- a list of unnamed items consisting of import statements
As an example lets consider the following Modelica code:
package A
package B
import Modelica.SIunits.;
constant Voltage V=3.3;
function foo
end foo;
model M1
Real x,y;
end M1;
model M2
end M2;
end B;
end A;
When instantiating M1 we will first create the environment for its surrounding scope
by a recursive instantiation on A.B giving the environment:
{
FRAME(\"A\", {Class:B},{},{},false) ,
FRAME(\"B\", {Class:M1, Class:M2, Variable:V}, {Type:foo},
{import Modelica.SIunits.},false)
}
Then, the class M1 is instantiated in a new scope/Frame giving the environment:
{
FRAME(\"A\", {Class:B},{},{},false) ,
FRAME(\"B\", {Class:M1, Class:M2, Variable:V}, {Type:foo},
{Import Modelica.SIunits.},false),
FRAME(\"M1, {Variable:x, Variable:y},{},{},false)
}
NOTE: The instance hierachy (components and variables) and the class hierachy
(packages and classes) are combined into the same data structure, enabling a
uniform lookup mechanism "
// public imports
public import Absyn;
public import ClassInf;
public import DAE;
public import SCode;
public type Ident = String " An identifier is just a string " ;
public type Env = list<Frame> "an environment is a list of frames";
public uniontype Cache
record CACHE
Option<array<EnvCache>> envCache "The cache contains of environments from which classes can be found";
Option<Env> initialEnv "and the initial environment";
array<DAE.FunctionTree> functions "set of Option<DAE.Function>; NONE() means instantiation started; SOME() means it's finished";
end CACHE;
end Cache;
public uniontype EnvCache
record ENVCACHE
"Cache for environments. The cache consists of a tree
of environments from which lookupcan be performed."
CacheTree envTree;
end ENVCACHE;
end EnvCache;
public uniontype CacheTree
record CACHETREE
Ident name;
Env env;
list<CacheTree> children;
end CACHETREE;
end CacheTree;
type CSetsType = tuple<list<DAE.ComponentRef>,DAE.ComponentRef>;
public uniontype ScopeType
record FUNCTION_SCOPE end FUNCTION_SCOPE;
record CLASS_SCOPE end CLASS_SCOPE;
end ScopeType;
public
uniontype Frame
record FRAME
Option<Ident> optName "Optional class name";
Option<ScopeType> optType "Optional scope type";
AvlTree clsAndVars "List of uniquely named classes and variables";
AvlTree types "List of types, which DOES NOT need to be uniquely named, eg. size may have several types";
list<Item> imports "list of unnamed items (imports)";
CSetsType connectionSet "current connection set crefs";
Boolean isEncapsulated "encapsulated bool=true means that FRAME is created due to encapsulated class";
list<SCode.Element> defineUnits "list of units defined in the frame";
end FRAME;
end Frame;
public uniontype InstStatus
"Used to distinguish between different phases of the instantiation of a component
A component is first added to environment untyped. It can thereafter be instantiated to get its type
and finally instantiated to produce the DAE. These three states are indicated by this datatype."
record VAR_UNTYPED "Untyped variables, initially added to env"
end VAR_UNTYPED;
record VAR_TYPED "Typed variables, when instantiation to get type has been performed"
end VAR_TYPED;
record VAR_DAE "Typed variables that also have been instantiated to generate dae. Required to distinguish
between typed variables without DAE to know when to skip multiply declared dae elements"
end VAR_DAE;
end InstStatus;
public
uniontype Item
record VAR
DAE.Var instantiated "instantiated component" ;
Option<tuple<SCode.Element, DAE.Mod>> declaration "declaration if not fully instantiated.";
InstStatus instStatus "if it untyped, typed or fully instantiated (dae)";
Env env "The environment of the instantiated component. Contains e.g. all sub components";
end VAR;
record CLASS
SCode.Class class_;
Env env;
end CLASS;
record TYPE
list<DAE.Type> list_ "list since several types with the same name can exist in the same scope (overloading)" ;
end TYPE;
record IMPORT
Absyn.Import import_;
end IMPORT;
end Item;
// protected imports
protected import ComponentReference;
protected import DAEDump;
protected import DAEUtil;
protected import Debug;
protected import Dump;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Print;
protected import Util;
protected import Types;
protected import OptManager;
protected import RTOpts;
public constant Env emptyEnv={} "- Values" ;
public function emptyCache
"returns an empty cache"
output Cache cache;
protected
Option<array<EnvCache>> envCache;
array<EnvCache> arr;
array<DAE.FunctionTree> instFuncs;
algorithm
//print("EMPTYCACHE\n");
arr := arrayCreate(1, ENVCACHE(CACHETREE("$global",emptyEnv,{})));
envCache := Util.if_(OptManager.getOption("envCache"),SOME(arr),NONE());
instFuncs := arrayCreate(1, DAEUtil.emptyFuncTree);
cache := CACHE(envCache,NONE(),instFuncs);
end emptyCache;
public constant String forScopeName="$for loop scope$" "a unique scope used in for equations";
public constant String forIterScopeName="$foriter loop scope$" "a unique scope used in for iterators";
public constant String matchScopeName="$match scope$" "a unique scope used by match expressions";
public constant String caseScopeName="$case scope$" "a unique scope used by match expressions; to be removed when local decls are deprecated";
public constant list<String> implicitScopeNames={forScopeName,forIterScopeName,matchScopeName,caseScopeName};
// functions for dealing with the environment
public function newEnvironment
"Creates a new empty environment."
output Env newEnv;
algorithm
newEnv := Util.listCreate(newFrame(false,NONE(),NONE()));
end newEnvironment;
protected function newFrame "function: newFrame
This function creates a new frame, which includes setting up the
hashtable for the frame."
input Boolean enc;
input Option<Ident> inName;
input Option<ScopeType> inType;
output Frame outFrame;
protected
AvlTree httypes;
AvlTree ht;
DAE.ComponentRef cref_;
algorithm
ht := avlTreeNew();
httypes := avlTreeNew();
cref_ := ComponentReference.makeCrefIdent("",DAE.ET_OTHER(),{});
outFrame := FRAME(inName,inType,ht,httypes,{},({},cref_),enc,{});
end newFrame;
public function isTyped "
Author BZ 2008-06
This function checks wheter an InstStatus is typed or not.
Currently used by Inst.updateComponentsInEnv."
input InstStatus is;
output Boolean b;
algorithm
b := matchcontinue(is)
case(VAR_UNTYPED()) then false;
case(_) then true;
end matchcontinue;
end isTyped;
public function openScope "function: openScope
Opening a new scope in the environment means adding a new frame on
top of the stack of frames. If the scope is not the top scope a classname
of the scope should be provided such that a name for the scope can be
derived, see nameScope."
input Env inEnv;
input Boolean inBoolean;
input Option<Ident> inIdentOption;
input Option<ScopeType> inTypeOption;
output Env outEnv;
protected
Frame f;
algorithm
f := newFrame(inBoolean, inIdentOption, inTypeOption);
outEnv := f :: inEnv;
end openScope;
public function inForLoopScope "returns true if environment has a frame that is a for loop"
input Env env;
output Boolean res;
algorithm
res := matchcontinue(env)
local String name;
case(FRAME(optName = SOME(name))::_)
equation
true = stringEq(name, forScopeName);
then true;
case(_) then false;
end matchcontinue;
end inForLoopScope;
public function inForIterLoopScope "returns true if environment has a frame that is a for iterator 'loop'"
input Env env;
output Boolean res;
algorithm
res := matchcontinue(env)
local String name;
case(FRAME(optName = SOME(name))::_)
equation
true = stringEq(name, forIterScopeName);
then true;
case(_) then false;
end matchcontinue;
end inForIterLoopScope;
public function stripForLoopScope "strips for loop scopes"
input Env env;
output Env outEnv;
algorithm
outEnv := matchcontinue(env)
local String name;
case(FRAME(optName = SOME(name))::env)
equation
true = stringEq(name, forScopeName);
env = stripForLoopScope(env);
then env;
case(env) then env;
end matchcontinue;
end stripForLoopScope;
public function getScopeName "function: getScopeName
Returns the name of a scope, if no name exist, the function fails."
input Env inEnv;
output Ident name;
algorithm
name:= match (inEnv)
case ((FRAME(optName = SOME(name))::_)) then (name);
end match;
end getScopeName;
public function getScopeNames "function: getScopeName
Returns the name of a scope, if no name exist, the function fails."
input Env inEnv;
output list<Ident> names;
algorithm
names := matchcontinue (inEnv)
local String name;
// empty list
case ({}) then {};
// frame with a name
case ((FRAME(optName = SOME(name))::inEnv))
equation
names = getScopeNames(inEnv);
then
name::names;
// frame without a name
case ((FRAME(optName = NONE())::inEnv))
equation
names = getScopeNames(inEnv);
then
"-NONAME-"::names;
end matchcontinue;
end getScopeNames;
public function updateEnvClasses "Updates the classes of the top frame on the env passed as argument to the environment
passed as second argument"
input Env env;
input Env classEnv;
output Env outEnv;
algorithm
outEnv := match(env,classEnv)
local
Option<Ident> optName;
Option<ScopeType> st;
AvlTree clsAndVars, types ;
list<Item> imports;
list<Frame> fs;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crefs;
Boolean enc;
list<SCode.Element> defineUnits;
case(FRAME(optName,st,clsAndVars,types,imports,crefs,enc,defineUnits)::fs,classEnv)
equation
clsAndVars = updateEnvClassesInTree(clsAndVars,classEnv);
then
FRAME(optName,st,clsAndVars,types,imports,crefs,enc,defineUnits)::fs;
end match;
end updateEnvClasses;
protected function updateEnvClassesInTree "Help function to updateEnvClasses"
input AvlTree tree;
input Env classEnv;
output AvlTree outTree;
algorithm
outTree := matchcontinue(tree,classEnv)
local
SCode.Class cl;
Option<AvlTree> l,r;
AvlKey k;
Env env;
Item item;
Integer h;
// Classes
case(AVLTREENODE(SOME(AVLTREEVALUE(k,CLASS(cl,env))),h,l,r),classEnv) equation
l = updateEnvClassesInTreeOpt(l,classEnv);
r = updateEnvClassesInTreeOpt(r,classEnv);
then AVLTREENODE(SOME(AVLTREEVALUE(k,CLASS(cl,classEnv))),h,l,r);
// Other items
case(AVLTREENODE(SOME(AVLTREEVALUE(k,item)),h,l,r),classEnv) equation
l = updateEnvClassesInTreeOpt(l,classEnv);
r = updateEnvClassesInTreeOpt(r,classEnv);
then AVLTREENODE(SOME(AVLTREEVALUE(k,item)),h,l,r);
// nothing
case(AVLTREENODE(NONE(),h,l,r),classEnv) equation
l = updateEnvClassesInTreeOpt(l,classEnv);
r = updateEnvClassesInTreeOpt(r,classEnv);
then AVLTREENODE(NONE(),h,l,r);
end matchcontinue;
end updateEnvClassesInTree;
protected function updateEnvClassesInTreeOpt "Help function to updateEnvClassesInTree"
input Option<AvlTree> tree;
input Env classEnv;
output Option<AvlTree> outTree;
algorithm
outTree := match(tree,classEnv)
local AvlTree t;
case(NONE(),classEnv) then NONE();
case(SOME(t),classEnv) equation
t = updateEnvClassesInTree(t,classEnv);
then SOME(t);
end match;
end updateEnvClassesInTreeOpt;
public function extendFrameC "function: extendFrameC
This function adds a class definition to the environment."
input Env inEnv;
input SCode.Class inClass;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv,inClass)
local
AvlTree httypes;
AvlTree ht,ht_1;
Env env,fs;
Option<Ident> id;
Option<ScopeType> st;
list<AvlValue> imps;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
SCode.Class c;
Ident n;
list<SCode.Element> defineUnits;
case ((env as (FRAME(id,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs)),(c as SCode.CLASS(name = n)))
equation
(ht_1) = avlTreeAdd(ht, n, CLASS(c,env));
then
(FRAME(id,st,ht_1,httypes,imps,crs,encflag,defineUnits) :: fs);
case (_,_)
equation
print("- Env.extendFrameC FAILED\n");
then
fail();
end matchcontinue;
end extendFrameC;
public function extendFrameClasses "function: extendFrameClasses
Adds all clases in a Program to the environment."
input Env inEnv;
input SCode.Program inProgram;
output Env outEnv;
algorithm
outEnv := match (inEnv,inProgram)
local
Env env,env_1,env_2;
SCode.Class c;
list<SCode.Class> cs;
case (env,{}) then env;
case (env,(c :: cs))
equation
env_1 = extendFrameC(env, c);
env_2 = extendFrameClasses(env_1, cs);
then
env_2;
end match;
end extendFrameClasses;
public function removeComponentsFromFrameV "function: removeComponentsFromFrameV
This function removes all components from frame."
input Env inEnv;
output Env outEnv;
algorithm
outEnv := match (inEnv)
local
AvlTree httypes;
AvlTree ht;
Option<Ident> id;
Option<ScopeType> st;
list<AvlValue> imps;
Env fs;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
list<SCode.Element> defineUnits;
case ((FRAME(id,st,_,httypes,imps,crs,encflag,defineUnits) :: fs))
equation
// make an empty component env!
ht = avlTreeNew();
then
(FRAME(id,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs);
end match;
end removeComponentsFromFrameV;
public function extendFrameV "function: extendFrameV
This function adds a component to the environment."
input Env inEnv1;
input DAE.Var inVar2;
input Option<tuple<SCode.Element, DAE.Mod>> inTplSCodeElementTypesModOption3;
input InstStatus instStatus;
input Env inEnv5;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv1,inVar2,inTplSCodeElementTypesModOption3,instStatus,inEnv5)
local
AvlTree httypes;
AvlTree ht,ht_1;
Option<Ident> id;
Option<ScopeType> st;
list<AvlValue> imps;
Env fs,env,remember;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
InstStatus i;
DAE.Var v;
Ident n;
Option<tuple<SCode.Element, DAE.Mod>> c;
list<SCode.Element> defineUnits;
case ((FRAME(id,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),(v as DAE.TYPES_VAR(name = n)),c,i,env) /* environment of component */
equation
//failure((_)= avlTreeGet(ht, n));
(ht_1) = avlTreeAdd(ht, n, VAR(v,c,i,env));
then
(FRAME(id,st,ht_1,httypes,imps,crs,encflag,defineUnits) :: fs);
// Variable already added, perhaps from baseclass
case (remember as (FRAME(id,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),
(v as DAE.TYPES_VAR(name = n)),c,i,env) /* environment of component */
equation
(_)= avlTreeGet(ht, n);
then
(remember);
end matchcontinue;
end extendFrameV;
public function updateFrameV "function: updateFrameV
This function updates a component already added to the environment, but
that prior to the update did not have any binding. I.e this function is
called in the second stage of instantiation with declare before use."
input Env inEnv1;
input DAE.Var inVar2;
input InstStatus instStatus;
input Env inEnv4;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv1,inVar2,instStatus,inEnv4)
local
Boolean encflag;
InstStatus i;
Option<tuple<SCode.Element, DAE.Mod>> c;
AvlTree httypes;
AvlTree ht,ht_1;
Option<Ident> sid;
Option<ScopeType> st;
list<AvlValue> imps;
Env fs,env,frames;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
DAE.Var v;
Ident n,id;
list<SCode.Element> defineUnits;
case ({},_,i,_) then {}; /* fully instantiated env of component */
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),(v as DAE.TYPES_VAR(name = n)),i,env)
equation
VAR(_,c,_,_) = avlTreeGet(ht, n);
(ht_1) = avlTreeAdd(ht, n, VAR(v,c,i,env));
then
(FRAME(sid,st,ht_1,httypes,imps,crs,encflag,defineUnits) :: fs);
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),(v as DAE.TYPES_VAR(name = n)),i,env) /* Also check frames above, e.g. when variable is in base class */
equation
frames = updateFrameV(fs, v, i, env);
then
(FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: frames);
case ((FRAME(sid,st,ht, httypes,imps,crs,encflag,defineUnits) :: fs),DAE.TYPES_VAR(name = n),_,_)
equation
/*Print.printBuf("- update_frame_v, variable ");
Print.printBuf(n);
Print.printBuf(" not found\n rest of env:");
printEnv(fs);
Print.printBuf("\n");*/
then
(FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs);
case (_,(v as DAE.TYPES_VAR(name = id)),_,_)
equation
print("- update_frame_v failed\n");
print(" - variable: ");
print(Types.printVarStr(v));
print("\n");
then
fail();
end matchcontinue;
end updateFrameV;
public function extendFrameT "function: extendFrameT
This function adds a type to the environment. Types in the
environment are used for looking up constants etc. inside class
definitions, such as packages. For each type in the environment,
there is a class definition with the same name in the
environment."
input Env inEnv;
input Ident inIdent;
input DAE.Type inType;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv,inIdent,inType)
local
list<tuple<DAE.TType, Option<Absyn.Path>>> tps;
AvlTree httypes_1,httypes;
AvlTree ht;
Option<Ident> sid;
Option<ScopeType> st;
list<AvlValue> imps;
Env fs;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
Ident n;
tuple<DAE.TType, Option<Absyn.Path>> t;
list<SCode.Element> defineUnits;
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),n,t)
equation
TYPE(tps) = avlTreeGet(httypes, n) "Other types with that name allready exist, add this type as well" ;
(httypes_1) = avlTreeAdd(httypes, n, TYPE((t :: tps)));
then
(FRAME(sid,st,ht,httypes_1,imps,crs,encflag,defineUnits) :: fs);
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),n,t)
equation
failure(TYPE(_) = avlTreeGet(httypes, n)) "No other types exists" ;
(httypes_1) = avlTreeAdd(httypes, n, TYPE({t}));
then
(FRAME(sid,st,ht,httypes_1,imps,crs,encflag,defineUnits) :: fs);
end matchcontinue;
end extendFrameT;
public function extendFrameI "function: extendFrameI
Adds an import statement to the environment."
input Env inEnv;
input Absyn.Import inImport;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv,inImport)
local
Option<Ident> sid;
Option<ScopeType> st;
AvlTree httypes;
AvlTree ht;
list<AvlValue> imps;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
Absyn.Import imp;
Env fs,env;
list<SCode.Element> defineUnits;
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),imp)
equation
false = memberImportList(imps,imp);
then (FRAME(sid,st,ht,httypes,(IMPORT(imp) :: imps),crs,encflag,defineUnits) :: fs);
case (env,imp) then env;
end matchcontinue;
end extendFrameI;
public function extendFrameDefunit "
Adds a defineunit to the environment."
input Env inEnv;
input SCode.Element defunit;
output Env outEnv;
algorithm
outEnv := match (inEnv,defunit)
local
Option<Ident> sid;
Option<ScopeType> st;
AvlTree httypes;
AvlTree ht;
list<AvlValue> imps;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crs;
Boolean encflag;
Env fs;
list<SCode.Element> defineUnits;
case ((FRAME(sid,st,ht,httypes,imps,crs,encflag,defineUnits) :: fs),defunit)
then (FRAME(sid,st,ht,httypes,imps,crs,encflag,defunit::defineUnits) :: fs);
end match;
end extendFrameDefunit;
public function extendFrameForIterator
"Adds a for loop iterator to the environment."
input Env env;
input String name;
input DAE.Type type_;
input DAE.Binding binding;
input SCode.Variability variability;
input Option<DAE.Const> constOfForIteratorRange;
output Env new_env;
algorithm
new_env := match(env, name, type_, binding, variability, constOfForIteratorRange)
local
Env new_env_1;
case (_, _, _, _,variability,constOfForIteratorRange)
equation
new_env_1 = extendFrameV(env,
DAE.TYPES_VAR(
name,
DAE.ATTR(false, false, SCode.RW(), variability, Absyn.BIDIR(), Absyn.UNSPECIFIED()),
false,
type_,
binding,
constOfForIteratorRange),
NONE(), VAR_UNTYPED(), {});
then new_env_1;
end match;
end extendFrameForIterator;
protected function memberImportList "Returns true if import exist in imps"
input list<Item> imps;
input Absyn.Import imp;
output Boolean res "true if import exist in imps, false otherwise";
algorithm
res := matchcontinue (imps,imp)
local
list<Item> ims;
Absyn.Import imp2;
// first import in the list matches
case (IMPORT(imp2)::ims,imp)
equation
true = Absyn.importEqual(imp2, imp);
then true;
// move to next
case (_::ims,imp)
equation
res=memberImportList(ims,imp);
then res;
// other alternatives
case (_,_) then false;
end matchcontinue;
end memberImportList;
/*
public function addBcFrame "function: addBcFrame
author: PA
Adds a baseclass frame to the environment from the baseclass environment
to the list of base classes of the top frame of the passed environment."
input Env inEnv1;
input Env inEnv2;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv1,inEnv2)
local
Option<Ident> sid;
AvlTree tps;
AvlTree cls;
list<AvlValue> imps;
Env bc,fs;
tuple<list<DAE.ComponentRef>,DAE.ComponentRef> crefs;
Boolean enc;
Frame f;
list<SCode.Element> defineUnits;
case ((FRAME(sid,cls,tps,imps,bc,crefs,enc,defineUnits) :: fs),(f :: _))
then (FRAME(sid,cls,tps,imps,(f :: bc),crefs,enc,defineUnits) :: fs);
end matchcontinue;
end addBcFrame;
*/
public function topFrame "function: topFrame
Returns the top frame."
input Env inEnv;
output Frame outFrame;
algorithm
outFrame := match (inEnv)
local
Frame fr,elt;
Env lst;
case ({fr}) then fr;
case ((elt :: (lst as (_ :: _))))
equation
fr = topFrame(lst);
then
fr;
end match;
end topFrame;
/*
public function enclosingScopeEnv "function: enclosingScopeEnv
@author: adrpo
Returns the environment with the current scope frame removed."
input Env inEnv;
output Env outEnv;
algorithm
outEnv := matchcontinue (inEnv)
local
Env rest;
case ({}) then {};
case (_ :: rest)
then
rest;
end matchcontinue;
end enclosingScopeEnv;
*/
public function getClassName
input Env inEnv;
output Ident name;
algorithm
name := match (inEnv)
local Ident n;
case FRAME(optName = SOME(n))::_ then n;
end match;
end getClassName;
public function getEnvName "returns the FQ name of the environment, see also getEnvPath"
input Env env;
output Absyn.Path path;
algorithm
path := matchcontinue(env)
case(env) equation
SOME(path) = getEnvPath(env);
then path;
case _
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- Env.getEnvName failed");
_ = getEnvPath(env);
then fail();
end matchcontinue;
end getEnvName;
public function getEnvPath "function: getEnvPath
This function returns all partially instantiated parents as an Absyn.Path
option I.e. it collects all identifiers of each frame until it reaches
the topmost unnamed frame. If the environment is only the topmost frame,
NONE() is returned."
input Env inEnv;
output Option<Absyn.Path> outAbsynPathOption;
algorithm
outAbsynPathOption := match (inEnv)
local
Ident id;
Absyn.Path path,path_1;
Env rest;
case ({FRAME(optName = SOME(id)),FRAME(optName = NONE())}) then SOME(Absyn.IDENT(id));
case ((FRAME(optName = SOME(id)) :: rest))
equation
SOME(path) = getEnvPath(rest);
path_1 = Absyn.joinPaths(path, Absyn.IDENT(id));
then
SOME(path_1);
else NONE();
end match;
end getEnvPath;
public function getEnvPathNoImplicitScope "function: getEnvPath
This function returns all partially instantiated parents as an Absyn.Path
option I.e. it collects all identifiers of each frame until it reaches
the topmost unnamed frame. If the environment is only the topmost frame,
NONE() is returned."
input Env inEnv;
output Option<Absyn.Path> outAbsynPathOption;
algorithm
outAbsynPathOption := matchcontinue (inEnv)
local
Ident id;
Absyn.Path path,path_1;
Env rest;
case ((FRAME(optName = SOME(id)) :: rest))
equation
true = listMember(id,implicitScopeNames);
then getEnvPathNoImplicitScope(rest);
case ((FRAME(optName = SOME(id)) :: rest))
equation
false = listMember(id,implicitScopeNames);
SOME(path) = getEnvPathNoImplicitScope(rest);
path_1 = Absyn.joinPaths(path, Absyn.IDENT(id));
then
SOME(path_1);
case (FRAME(optName = SOME(id))::rest)
equation
false = listMember(id,implicitScopeNames);
NONE() = getEnvPathNoImplicitScope(rest);
then SOME(Absyn.IDENT(id));
case (_) then NONE();
end matchcontinue;
end getEnvPathNoImplicitScope;
public function joinEnvPath "function: joinEnvPath
Used to join an Env with an Absyn.Path (probably an IDENT)"
input Env inEnv;
input Absyn.Path inPath;
output Absyn.Path outPath;
algorithm
outPath := matchcontinue(inEnv,inPath)
local
Absyn.Path envPath;
case (inEnv,inPath)
equation
SOME(envPath) = getEnvPath(inEnv);
envPath = Absyn.joinPaths(envPath,inPath);
then envPath;
case (inEnv,inPath)
equation
NONE() = getEnvPath(inEnv);
then inPath;
end matchcontinue;
end joinEnvPath;
public function printEnvPathStr "function: printEnvPathStr
Retrive the environment path as a string, see getEnvPath."
input Env inEnv;
output String outString;
algorithm
outString := matchcontinue (inEnv)
local
Absyn.Path path;
Ident pathstr;
Env env;
case (env)
equation
SOME(path) = getEnvPath(env);
pathstr = Absyn.pathString(path);
then
pathstr;
case (env) then "<global scope>";
end matchcontinue;
end printEnvPathStr;
public function printEnvPath "function: printEnvPath
Print the environment path to the Print buffer.
See also getEnvPath"
input Env inEnv;
algorithm
_ := matchcontinue (inEnv)
local
Absyn.Path path;
Ident pathstr;
Env env;
case (env)
equation
SOME(path) = getEnvPath(env);
pathstr = Absyn.pathString(path);
Print.printBuf(pathstr);
then
();
case (env)
equation
Print.printBuf("TOPENV");
then
();
end matchcontinue;
end printEnvPath;
public function printEnvStr "function: printEnvStr
Print the environment as a string."
input Env inEnv;
output String outString;
algorithm
outString := match (inEnv)
local
Ident s1,s2,res;
Frame fr;
Env frs;
case {} then "Empty env\n";
case (fr :: frs)
equation
s1 = printFrameStr(fr);
s2 = printEnvStr(frs);
res = stringAppend(s1, s2);
then
res;
end match;
end printEnvStr;
public function printEnv "function: printEnv
Print the environment to the Print buffer."
input Env e;
Ident s;
algorithm
s := printEnvStr(e);
Print.printBuf(s);
end printEnv;
public function printEnvConnectionCrefs "prints the connection crefs of the top frame"
input Env env;
algorithm