/
SCodeFlatten.mo
2176 lines (1935 loc) · 59.1 KB
/
SCodeFlatten.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 SCodeFlatten
" file: SCodeFlatten.mo
package: SCodeFlatten
description: SCode flattening
RCS: $Id$
This module flattens the SCode representation by removing all extends, imports
and redeclares, and fully qualifying class names.
"
public import Absyn;
public import SCode;
protected import Debug;
protected import Error;
protected import Util;
protected import Dump;
protected type Import = Absyn.Import;
protected uniontype ImportTable
record IMPORT_TABLE
list<Import> qualifiedImports;
list<Import> unqualifiedImports;
end IMPORT_TABLE;
end ImportTable;
protected uniontype ExtendsTable
record EXTENDS_TABLE
list<Absyn.Path> baseClasses;
end EXTENDS_TABLE;
end ExtendsTable;
protected uniontype FrameType
record NORMAL_SCOPE end NORMAL_SCOPE;
record ENCAPSULATED_SCOPE end ENCAPSULATED_SCOPE;
record IMPLICIT_SCOPE end IMPLICIT_SCOPE;
end FrameType;
protected uniontype Frame
record FRAME
Option<String> name;
FrameType frameType;
AvlTree clsAndVars;
ExtendsTable extendsTable;
ImportTable importTable;
end FRAME;
end Frame;
protected uniontype Item
record VAR
SCode.Element var;
end VAR;
record CLASS
SCode.Class cls;
Env env;
end CLASS;
record BUILTIN
String name;
end BUILTIN;
end Item;
protected type Env = list<Frame>;
protected constant Env emptyEnv = {};
protected constant Item BUILTIN_REAL = BUILTIN("Real");
protected constant Item BUILTIN_INTEGER = BUILTIN("Integer");
protected constant Item BUILTIN_BOOLEAN = BUILTIN("Boolean");
protected constant Item BUILTIN_STRING = BUILTIN("String");
protected constant Item BUILTIN_STATESELECT = BUILTIN("StateSelect");
protected constant Item BUILTIN_EXTERNALOBJECT = BUILTIN("ExternalObject");
public function flatten
input SCode.Program inProgram;
output SCode.Program outProgram;
protected
Env env;
algorithm
env := newEnvironment(NONE());
env := buildInitialEnv();
env := extendEnvWithClasses(inProgram, env);
outProgram := flattenProgram(inProgram, env);
end flatten;
protected function flattenProgram
input SCode.Program inProgram;
input Env inEnv;
output SCode.Program outProgram;
algorithm
outProgram := Util.listMap1(inProgram, lookupClassNames, inEnv);
end flattenProgram;
protected function lookupClassNames
input SCode.Class inClass;
input Env inEnv;
output SCode.Class outClass;
protected
SCode.Ident name;
Boolean part_pre, encap_pre;
SCode.Restriction restriction;
SCode.ClassDef cdef;
Absyn.Info info;
Env env;
algorithm
SCode.CLASS(name, part_pre, encap_pre, restriction, cdef, info) := inClass;
env := enterScope(inEnv, name);
cdef := lookupClassDefNames(cdef, env);
outClass := SCode.CLASS(name, part_pre, encap_pre, restriction, cdef, info);
end lookupClassNames;
protected function lookupClassDefNames
input SCode.ClassDef inClassDef;
input Env inEnv;
output SCode.ClassDef outClassDef;
algorithm
outClassDef := match(inClassDef, inEnv)
local
list<SCode.Element> el, ex, cl, im, co, ud;
list<SCode.Equation> neql, ieql;
list<SCode.AlgorithmSection> nal, ial;
Option<Absyn.ExternalDecl> extdecl;
list<SCode.Annotation> annl;
Option<SCode.Comment> cmt;
SCode.ClassDef cdef;
Env env;
case (SCode.PARTS(el, neql, ieql, nal, ial, extdecl, annl, cmt), env)
equation
env = Util.listFold(el, extendEnvWithClassExtends, env);
// Lookup elements.
el = Util.listMap1(el, lookupElement, env);
// Lookup equations and algorithm names.
neql = Util.listMap1(neql, lookupEquation, env);
ieql = Util.listMap1(ieql, lookupEquation, env);
nal = Util.listMap1(nal, lookupAlgorithm, env);
ial = Util.listMap1(ial, lookupAlgorithm, env);
cdef = SCode.PARTS(el, neql, ieql, nal, ial, extdecl, annl, cmt);
then
cdef;
//case (SCode.CLASS_EXTENDS
//case (SCode.DERIVED
case (SCode.ENUMERATION(enumLst = _), _) then inClassDef;
//case (SCode.OVERLOAD
//case (SCode.PDER
else then inClassDef;
end match;
end lookupClassDefNames;
protected function lookupElement
input SCode.Element inElement;
input Env inEnv;
output SCode.Element outElement;
algorithm
outElement := match(inElement, inEnv)
// Lookup component types, modifications and conditions.
case (SCode.COMPONENT(component = _), _)
then lookupComponent(inElement, inEnv);
// Lookup class definitions.
case (SCode.CLASSDEF(name = _), _)
then lookupClassDefElementNames(inElement, inEnv);
// Lookup base class and modifications in extends clauses.
case (SCode.EXTENDS(baseClassPath = _), _)
then lookupExtends(inElement, inEnv);
else then inElement;
end match;
end lookupElement;
protected function lookupClassDefElementNames
input SCode.Element inClassDefElement;
input Env inEnv;
output SCode.Element outClassDefElement;
protected
SCode.Ident name;
Boolean fp, rp;
SCode.Class cls;
Option<Absyn.ConstrainClass> cc;
algorithm
SCode.CLASSDEF(name, fp, rp, cls, cc) := inClassDefElement;
cls := lookupClassNames(cls, inEnv);
outClassDefElement := SCode.CLASSDEF(name, fp, rp, cls, cc);
end lookupClassDefElementNames;
protected function lookupComponent
input SCode.Element inComponent;
input Env inEnv;
output SCode.Element outComponent;
protected
SCode.Ident name;
Absyn.InnerOuter io;
Boolean fp, rp, pp;
SCode.Attributes attr;
Absyn.TypeSpec type_spec;
SCode.Mod mod;
Option<SCode.Comment> cmt;
Option<Absyn.Exp> cond;
Option<Absyn.Info> info;
Option<Absyn.ConstrainClass> cc;
algorithm
SCode.COMPONENT(name, io, fp, rp, pp, attr, type_spec, mod, cmt, cond,
info, cc) := inComponent;
(_, type_spec, _) := lookupTypeSpec(type_spec, inEnv);
mod := lookupModifier(mod, inEnv);
cond := lookupOptExp(cond, inEnv);
outComponent := SCode.COMPONENT(name, io, fp, rp, pp, attr, type_spec, mod,
cmt, cond, info, cc);
end lookupComponent;
protected function lookupTypeSpec
input Absyn.TypeSpec inTypeSpec;
input Env inEnv;
output Item outItem;
output Absyn.TypeSpec outTypeSpec;
output Env outTypeEnv;
algorithm
(outItem, outTypeSpec, outTypeEnv) := matchcontinue(inTypeSpec, inEnv)
local
Absyn.Path path;
Absyn.Ident name;
Option<Absyn.ArrayDim> array_dim;
Item item;
Env env;
case (Absyn.TPATH(path, array_dim), _)
equation
(item, path, SOME(env)) = lookupName(path, inEnv);
then
(item, Absyn.TPATH(path, array_dim), env);
end matchcontinue;
end lookupTypeSpec;
protected function lookupExtends
input SCode.Element inExtends;
input Env inEnv;
output SCode.Element outExtends;
protected
Absyn.Path path;
SCode.Mod mod;
Option<SCode.Annotation> ann;
Absyn.Info info;
Env env;
algorithm
SCode.EXTENDS(path, mod, ann, info) := inExtends;
env := removeExtendsFromLocalScope(inEnv);
(_, path, _) := lookupName(path, env);
mod := lookupModifier(mod, inEnv);
outExtends := SCode.EXTENDS(path, mod, ann, info);
end lookupExtends;
protected function lookupEquation
input SCode.Equation inEquation;
input Env inEnv;
output SCode.Equation outEquation;
protected
SCode.EEquation equ;
algorithm
SCode.EQUATION(equ) := inEquation;
(equ, _) := SCode.traverseEEquations(equ, (lookupEEquationTraverser, inEnv));
outEquation := SCode.EQUATION(equ);
end lookupEquation;
protected function lookupEEquationTraverser
input tuple<SCode.EEquation, Env> inTuple;
output tuple<SCode.EEquation, Env> outTuple;
algorithm
outTuple := match(inTuple)
local
SCode.EEquation equ;
SCode.Ident iter_name;
Env env;
case ((equ as SCode.EQ_FOR(index = iter_name), env))
equation
env = extendEnvWithIterators({(iter_name, NONE())}, env);
(equ, _) = SCode.traverseEEquationExps(equ, (traverseExp, env));
then
((equ, env));
case ((equ, env))
equation
(equ, _) = SCode.traverseEEquationExps(equ, (traverseExp, env));
then
((equ, env));
end match;
end lookupEEquationTraverser;
protected function traverseExp
input tuple<Absyn.Exp, Env> inTuple;
output tuple<Absyn.Exp, Env> outTuple;
protected
Absyn.Exp exp;
Env env;
algorithm
(exp, env) := inTuple;
(exp, (_, _, env)) := Absyn.traverseExpBidir(exp,
(lookupExpTraverserEnter, lookupExpTraverserExit, env));
outTuple := (exp, env);
end traverseExp;
protected function lookupAlgorithm
input SCode.AlgorithmSection inAlgorithm;
input Env inEnv;
output SCode.AlgorithmSection outAlgorithm;
protected
list<SCode.Statement> statements;
algorithm
SCode.ALGORITHM(statements) := inAlgorithm;
statements := Util.listMap1(statements, lookupStatement, inEnv);
outAlgorithm := SCode.ALGORITHM(statements);
end lookupAlgorithm;
protected function lookupStatement
input SCode.Statement inStatement;
input Env inEnv;
output SCode.Statement outStatement;
protected
Env env;
algorithm
(outStatement, _) := SCode.traverseStatements(inStatement,
(lookupStatementTraverser, inEnv));
end lookupStatement;
protected function lookupStatementTraverser
input tuple<SCode.Statement, Env> inTuple;
output tuple<SCode.Statement, Env> outTuple;
algorithm
outTuple := match(inTuple)
local
Absyn.ForIterators iters;
Env env;
SCode.Statement stmt;
case ((stmt as SCode.ALG_FOR(iterators = iters), env))
equation
env = extendEnvWithIterators(iters, env);
(stmt, _) = SCode.traverseStatementExps(stmt, (traverseExp, env));
then
((stmt, env));
case ((stmt, env))
equation
(stmt, _) = SCode.traverseStatementExps(stmt, (traverseExp, env));
then
((stmt, env));
end match;
end lookupStatementTraverser;
protected function lookupModifier
input SCode.Mod inMod;
input Env inEnv;
output SCode.Mod outMod;
algorithm
outMod := match(inMod, inEnv)
local
Boolean fp;
Absyn.Each ep;
list<SCode.SubMod> sub_mods;
Option<tuple<Absyn.Exp, Boolean>> opt_exp;
list<SCode.Element> el;
case (SCode.MOD(fp, ep, sub_mods, opt_exp), _)
equation
opt_exp = lookupModOptExp(opt_exp, inEnv);
sub_mods = Util.listMap1(sub_mods, lookupSubMod, inEnv);
then
SCode.MOD(fp, ep, sub_mods, opt_exp);
case (SCode.REDECL(fp, el), _)
equation
//print("SCodeFlatten.lookupModifier: REDECL\n");
then
//fail();
inMod;
case (SCode.NOMOD(), _) then inMod;
end match;
end lookupModifier;
protected function lookupModOptExp
input Option<tuple<Absyn.Exp, Boolean>> inOptExp;
input Env inEnv;
output Option<tuple<Absyn.Exp, Boolean>> outOptExp;
algorithm
outOptExp := match(inOptExp, inEnv)
local
Absyn.Exp exp;
Boolean delay_elab;
case (SOME((exp, delay_elab)), _)
equation
exp = lookupExp(exp, inEnv);
then
SOME((exp, delay_elab));
case (NONE(), _) then inOptExp;
end match;
end lookupModOptExp;
protected function lookupSubMod
input SCode.SubMod inSubMod;
input Env inEnv;
output SCode.SubMod outSubMod;
algorithm
outSubMod := match(inSubMod, inEnv)
local
SCode.Ident ident;
list<SCode.Subscript> subs;
SCode.Mod mod;
case (SCode.NAMEMOD(ident = ident, A = mod), _)
equation
mod = lookupModifier(mod, inEnv);
then
SCode.NAMEMOD(ident, mod);
case (SCode.IDXMOD(subscriptLst = subs, an = mod), _)
equation
subs = Util.listMap1(subs, lookupSubscript, inEnv);
mod = lookupModifier(mod, inEnv);
then
SCode.IDXMOD(subs, mod);
end match;
end lookupSubMod;
protected function lookupSubscript
input SCode.Subscript inSub;
input Env inEnv;
output SCode.Subscript outSub;
algorithm
outSub := match(inSub, inEnv)
local
Absyn.Exp exp;
case (Absyn.SUBSCRIPT(subScript = exp), _)
equation
exp = lookupExp(exp, inEnv);
then
Absyn.SUBSCRIPT(exp);
case (Absyn.NOSUB(), _) then inSub;
end match;
end lookupSubscript;
protected function lookupExp
input Absyn.Exp inExp;
input Env inEnv;
output Absyn.Exp outExp;
algorithm
(outExp, _) := Absyn.traverseExpBidir(inExp,
(lookupExpTraverserEnter, lookupExpTraverserExit, inEnv));
end lookupExp;
protected function lookupOptExp
input Option<Absyn.Exp> inExp;
input Env inEnv;
output Option<Absyn.Exp> outExp;
algorithm
outExp := match(inExp, inEnv)
local
Absyn.Exp exp;
case (SOME(exp), _)
equation
exp = lookupExp(exp, inEnv);
then
SOME(exp);
case (NONE(), _) then NONE();
end match;
end lookupOptExp;
protected function lookupExpTraverserEnter
input tuple<Absyn.Exp, Env> inTuple;
output tuple<Absyn.Exp, Env> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
Env env;
Absyn.ComponentRef cref;
Absyn.FunctionArgs args;
Absyn.Exp exp;
Absyn.ForIterators iters;
case ((Absyn.CREF(componentRef = cref), env))
equation
cref = lookupComponentRef(cref, env);
then
((Absyn.CREF(cref), env));
case ((exp as Absyn.CALL(functionArgs =
Absyn.FOR_ITER_FARG(iterators = iters)), env))
equation
env = extendEnvWithIterators(iters, env);
then
((exp, env));
case ((Absyn.CALL(function_ = cref, functionArgs = args), env))
equation
cref = lookupComponentRef(cref, env);
// TODO: handle function arguments
then
((Absyn.CALL(cref, args), env));
case ((Absyn.PARTEVALFUNCTION(function_ = cref, functionArgs = args), env))
equation
cref = lookupComponentRef(cref, env);
// TODO: handle function arguments
then
((Absyn.PARTEVALFUNCTION(cref, args), env));
else then inTuple;
end matchcontinue;
end lookupExpTraverserEnter;
protected function lookupExpTraverserExit
input tuple<Absyn.Exp, Env> inTuple;
output tuple<Absyn.Exp, Env> outTuple;
algorithm
outTuple := match(inTuple)
local
Absyn.Exp e;
Env env;
case ((e as Absyn.CALL(functionArgs = Absyn.FOR_ITER_FARG(iterators = _)),
FRAME(frameType = IMPLICIT_SCOPE()) :: env))
then
((e, env));
else then inTuple;
end match;
end lookupExpTraverserExit;
protected function lookupBuiltinType
input Absyn.Ident inName;
output Item outItem;
algorithm
outItem := match(inName)
case "Real" then BUILTIN_REAL;
case "Integer" then BUILTIN_INTEGER;
case "Boolean" then BUILTIN_BOOLEAN;
case "String" then BUILTIN_STRING;
case "StateSelect" then BUILTIN_STATESELECT;
case "ExternalObject" then BUILTIN_EXTERNALOBJECT;
end match;
end lookupBuiltinType;
protected function lookupName
"Looks up a simple or qualified name in the environment and returns the
environment item corresponding to the name, the fully qualified path for the
name and optionally the enclosing scope of the name if the name references a
class."
input Absyn.Path inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outName;
output Option<Env> outEnv;
algorithm
(outItem, outName, outEnv) := matchcontinue(inName, inEnv)
local
Absyn.Ident id;
Item item;
Absyn.Path path, new_path;
Env env;
Option<Env> item_env;
case (Absyn.IDENT(name = id), _)
equation
item = lookupBuiltinType(id);
then
(item, inName, SOME(emptyEnv));
// Simple name.
case (Absyn.IDENT(name = id), _)
equation
(item, new_path, env) = lookupSimpleName(id, inEnv);
then
(item, new_path, SOME(env));
// Simple name.
case (Absyn.IDENT(name = id), _)
equation
print("lookupName failed for " +& id +& "\n");
(item, new_path, env) = lookupSimpleName(id, inEnv);
then
(item, new_path, SOME(env));
// Qualified name.
case (Absyn.QUALIFIED(name = id, path = path), _)
equation
// Look up the first identifier.
(item, new_path, env) = lookupSimpleName(id, inEnv);
// Look up the rest of the name in the environment of the first
// identifier.
(item, path, env) = lookupNameInItem(path, item, env);
path = Absyn.joinPaths(new_path, path);
then
(item, path, SOME(env));
else
equation
print("- SCodeFlatten.lookupName failed for " +&
Absyn.pathString(inName) +& " in " +&
Absyn.pathString(getEnvPath(inEnv)) +& "\n");
then
fail();
end matchcontinue;
end lookupName;
protected function lookupComponentRef
"Look up a component reference in the environment and returns it fully
qualified."
input Absyn.ComponentRef inCref;
input Env inEnv;
output Absyn.ComponentRef outCref;
algorithm
outCref := matchcontinue(inCref, inEnv)
local
Absyn.ComponentRef cref;
case (Absyn.CREF_QUAL(name = "StateSelect", subScripts = {},
componentRef = Absyn.CREF_IDENT(name = _)), _)
then inCref;
case (_, _)
equation
// First look up all subscripts, because all subscripts should be found
// in the enclosing scope of the component reference.
cref = lookupComponentRefSubs(inCref, inEnv);
// Then look up the component reference itself.
cref = lookupComponentRef2(cref, inEnv);
then
cref;
case (_, _)
equation
print("lookupComponentRef failed for " +&
Absyn.printComponentRefStr(inCref) +& "\n");
// First look up all subscripts, because all subscripts should be found
// in the enclosing scope of the component reference.
cref = lookupComponentRefSubs(inCref, inEnv);
// Then look up the component reference itself.
cref = lookupComponentRef2(cref, inEnv);
then
cref;
else
equation
print("- SCodeFlatten.lookupComponentRef failed for " +&
Absyn.printComponentRefStr(inCref) +& " in " +&
Absyn.pathString(getEnvPath(inEnv)) +& "\n");
then
fail();
end matchcontinue;
end lookupComponentRef;
protected function lookupComponentRef2
input Absyn.ComponentRef inCref;
input Env inEnv;
output Absyn.ComponentRef outCref;
algorithm
outCref := match(inCref, inEnv)
local
Absyn.ComponentRef cref, rest_cref;
Absyn.Ident name;
list<Absyn.Subscript> subs;
Absyn.Path path, new_path;
Env env;
Item item;
case (Absyn.CREF_IDENT(name, subs), inEnv)
equation
(_, path, _) = lookupSimpleName(name, inEnv);
cref = Absyn.pathToCrefWithSubs(path, subs);
then
cref;
case (Absyn.CREF_QUAL(name, subs, rest_cref), inEnv)
equation
// Lookup the first identifier.
(item, new_path, env) = lookupSimpleName(name, inEnv);
cref = Absyn.pathToCrefWithSubs(new_path, subs);
// Lookup the rest of the cref in the enclosing scope of the first
// identifier.
(item, rest_cref) = lookupCrefInItem(rest_cref, item, env);
cref = joinCrefs(cref, rest_cref);
then
cref;
case (Absyn.CREF_FULLYQUALIFIED(componentRef = cref), inEnv)
equation
cref = lookupComponentRef2(cref, inEnv);
then
Absyn.CREF_FULLYQUALIFIED(cref);
end match;
end lookupComponentRef2;
protected function joinCrefs
input Absyn.ComponentRef inCref1;
input Absyn.ComponentRef inCref2;
output Absyn.ComponentRef outCref;
algorithm
outCref := match(inCref1, inCref2)
case (_, Absyn.CREF_FULLYQUALIFIED(componentRef = _)) then inCref2;
else then Absyn.joinCrefs(inCref1, inCref2);
end match;
end joinCrefs;
protected function lookupComponentRefSubs
input Absyn.ComponentRef inCref;
input Env inEnv;
output Absyn.ComponentRef outCref;
algorithm
outCref := match(inCref, inEnv)
local
Absyn.Ident name;
Absyn.ComponentRef cref;
list<Absyn.Subscript> subs;
case (Absyn.CREF_IDENT(name, subs), _)
equation
subs = Util.listMap1(subs, lookupSubscript, inEnv);
then
Absyn.CREF_IDENT(name, subs);
case (Absyn.CREF_QUAL(name, subs, cref), _)
equation
subs = Util.listMap1(subs, lookupSubscript, inEnv);
cref = lookupComponentRefSubs(cref, inEnv);
then
Absyn.CREF_QUAL(name, subs, cref);
case (Absyn.CREF_FULLYQUALIFIED(componentRef = cref), _)
equation
cref = lookupComponentRefSubs(cref, inEnv);
then
Absyn.CREF_FULLYQUALIFIED(cref);
end match;
end lookupComponentRefSubs;
protected function lookupSimpleName
input Absyn.Ident inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Env outEnv;
algorithm
(SOME(outItem), SOME(outPath), SOME(outEnv)) :=
lookupSimpleName2(inName, inEnv);
end lookupSimpleName;
protected function lookupSimpleName2
input Absyn.Ident inName;
input Env inEnv;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inEnv)
local
FrameType frame_type;
Env rest_env;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
Option<Env> opt_env;
case (_, _)
equation
(opt_item, opt_path, opt_env) = lookupInLocalScope(inName, inEnv);
then
(opt_item, opt_path, opt_env);
case (_, FRAME(frameType = frame_type) :: rest_env)
equation
frameNotEncapsulated(frame_type);
(opt_item, opt_path, opt_env) = lookupSimpleName2(inName, rest_env);
then
(opt_item, opt_path, opt_env);
end matchcontinue;
end lookupSimpleName2;
protected function frameNotEncapsulated
input FrameType frameType;
algorithm
_ := match(frameType)
case ENCAPSULATED_SCOPE() then fail();
else then ();
end match;
end frameNotEncapsulated;
protected function lookupInLocalScope
"Looks up a simple identifier in the environment. Returns SOME(item) if an
item is found, NONE() if a partial match was found (for example when the name
matches the import name of an import, but the imported class couldn't be
found), or fails if no match is found."
input Absyn.Ident inName;
input Env inEnv;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inEnv)
local
AvlTree cls_and_vars;
Env rest_env, env;
Item item;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
list<Import> imps;
FrameType frame_type;
Absyn.Path path;
Option<Env> opt_env;
// Look among the locally declared components.
case (_, FRAME(clsAndVars = cls_and_vars) :: _)
equation
item = avlTreeGet(cls_and_vars, inName);
then
(SOME(item), SOME(Absyn.IDENT(inName)), SOME(inEnv));
// Look among the inherited components.
case (_, _)
equation
(item, path, _, env) = lookupInBaseClasses(inName, inEnv);
then
(SOME(item), SOME(path), SOME(env));
// Look among the qualified imports.
case (_, FRAME(importTable = IMPORT_TABLE(qualifiedImports = imps)) :: _)
equation
(opt_item, opt_path, opt_env) =
lookupInQualifiedImports(inName, imps, inEnv);
then
(opt_item, opt_path, opt_env);
// Look among the unqualified imports.
case (_, FRAME(importTable = IMPORT_TABLE(unqualifiedImports = imps)) :: _)
equation
(opt_item, opt_path, opt_env) =
lookupInUnqualifiedImports(inName, imps, inEnv);
then
(opt_item, opt_path, opt_env);
// Look in the next scope only if the current scope is an implicit scope
// created by a for loop.
case (_, FRAME(frameType = IMPLICIT_SCOPE()) :: rest_env)
equation
(opt_item, opt_path, opt_env) = lookupInLocalScope(inName, rest_env);
then
(opt_item, opt_path, opt_env);
end matchcontinue;
end lookupInLocalScope;
protected function lookupInBaseClasses
"Looks up an identifier by following the extends clauses in a scope."
input Absyn.Ident inName;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Absyn.Path outBaseClass;
output Env outEnv;
Env env;
list<Absyn.Path> bcl;
algorithm
FRAME(extendsTable = EXTENDS_TABLE(baseClasses = bcl as _ :: _)) :: _ := inEnv;
// We need to remove the extends from the current scope, because the names of
// extended classes should not be found by lookup through the extends-clauses
// (Modelica Specification 3.2, section 5.6.1.).
env := removeExtendsFromLocalScope(inEnv);
(outItem, outPath, outBaseClass, outEnv) :=
lookupInBaseClasses2(inName, bcl, env);
end lookupInBaseClasses;
protected function lookupInBaseClasses2
input Absyn.Ident inName;
input list<Absyn.Path> inBaseClasses;
input Env inEnv;
output Item outItem;
output Absyn.Path outPath;
output Absyn.Path outBaseClass;
output Env outEnv;
algorithm
(outItem, outPath, outBaseClass, outEnv) :=
matchcontinue(inName, inBaseClasses, inEnv)
local
Absyn.Path bc, path;
list<Absyn.Path> rest_bc;
Item item;
Env env;
// Look in the first base class.
case (_, bc :: _, inEnv)
equation
(item, _, SOME(env)) = lookupName(bc, inEnv);
(item, path, env) = lookupNameInItem(Absyn.IDENT(inName), item, env);
then
(item, path, bc, env);
// No match, check the rest of the base classes.
case (_, _ :: rest_bc, _)
equation
(item, path, bc, env) = lookupInBaseClasses2(inName, rest_bc, inEnv);
then
(item, path, bc, env);
end matchcontinue;
end lookupInBaseClasses2;
protected function lookupInQualifiedImports
input Absyn.Ident inName;
input list<Import> inImports;
input Env inEnv;
output Option<Item> outItem;
output Option<Absyn.Path> outPath;
output Option<Env> outEnv;
algorithm
(outItem, outPath, outEnv) := matchcontinue(inName, inImports, inEnv)
local
Absyn.Ident name;
Absyn.Path path;
Item item;
list<Import> rest_imps;
Import imp;
Option<Item> opt_item;
Option<Absyn.Path> opt_path;
Option<Env> opt_env;
Env env;
// No match, search the rest of the list of imports.
case (_, Absyn.NAMED_IMPORT(name = name) :: rest_imps, _)
equation
false = stringEqual(inName, name);
(opt_item, opt_path, opt_env) =
lookupInQualifiedImports(inName, rest_imps, inEnv);
then
(opt_item, opt_path, opt_env);
// Match, look up the fully qualified import path.
case (_, Absyn.NAMED_IMPORT(name = name, path = path) :: _, _)
equation
true = stringEqual(inName, name);
(item, path, env) = lookupFullyQualified(path, inEnv);
then
(SOME(item), SOME(path), SOME(env));
// Partial match, return NONE(). This is when only part of the import path
// can be found, in which case we should stop looking further.