/
NFSCodeAnalyseRedeclare.mo
2155 lines (1855 loc) · 61 KB
/
NFSCodeAnalyseRedeclare.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 NFSCodeAnalyseRedeclare
" file: NFSCodeAnalyseRedeclare.mo
package: NFSCodeAnalyseRedeclare
description: SCode analysis of redeclares
RCS: $Id: NFSCodeAnalyseRedeclare.mo 13614 2012-10-25 00:03:02Z perost $
This module will do a dryrun of instantiation and find out
where the redeclares are applied. This information is used
by SCodeApplyRedeclare to apply the redeclares to the SCode.
"
public import Absyn;
public import NFInstTypes;
public import NFInstTypesOld;
public import SCode;
public import NFSCodeEnv;
public import NFSCodeFlattenRedeclare;
protected import Debug;
protected import Error;
protected import Flags;
protected import NFInstUtil;
protected import List;
protected import NFSCodeCheck;
protected import SCodeDump;
protected import NFSCodeLookup;
protected import NFSCodeMod;
protected import Util;
protected import System;
public type Binding = NFInstTypesOld.Binding;
public type Dimension = NFInstTypes.Dimension;
public type Element = SCode.Element;
public type Program = SCode.Program;
public type Env = NFSCodeEnv.Env;
public type Modifier = NFInstTypesOld.Modifier;
public type ParamType = NFInstTypes.ParamType;
public type Prefix = NFInstTypes.Prefix;
public type Scope = Absyn.Within;
public type Item = NFSCodeEnv.Item;
public type Redeclarations = list<NFSCodeEnv.Redeclaration>;
public type Replacements = NFSCodeFlattenRedeclare.Replacements;
public type Replacement = NFSCodeFlattenRedeclare.Replacement;
public type InstStack = list<Absyn.Path>;
public constant InstStack emptyInstStack = {};
public constant Integer tmpTickIndex = 3;
public uniontype Info
record RP "redeclare info"
Replacements replacements;
end RP;
record RI "redeclared info, previous item before redeclare"
Item item "previous item";
Env env "the env where we looked it up";
end RI;
record EI "iscope info"
Element element;
Env env;
end EI;
end Info;
type Infos = list<Info>;
public uniontype Kind
record CL "class node"
Absyn.Path name;
end CL;
record CO "component node"
Prefix prefix;
Absyn.Path scope;
end CO;
record EX "extends node"
Absyn.Path name;
end EX;
record RE "redeclared node"
Absyn.Path name;
end RE;
end Kind;
public
uniontype IScope
record IS
Kind kind;
Infos infos;
IScopes parts;
end IS;
end IScope;
public type IScopes = list<IScope>;
public constant IScopes emptyIScopes = {};
public function analyse
"analysis of where the redeclares are applied"
input Absyn.Path inClassPath;
input Env inEnv;
output IScopes outIScopes;
algorithm
outIScopes := matchcontinue(inClassPath, inEnv)
local
IScopes islist;
String name;
Env env;
Item item;
Absyn.Path path;
case (_, _)
equation
//print("Starting the new redeclare analysis phase ...\n");
name = Absyn.pathLastIdent(inClassPath);
(item, path, env) = NFSCodeLookup.lookupClassName(inClassPath, inEnv, Absyn.dummyInfo);
(islist, _) = analyseItem(
item,
env,
NFInstTypesOld.NOMOD(),
NFInstTypes.EMPTY_PREFIX(SOME(path)),
emptyIScopes,
emptyInstStack);
// sort so classes are first in the childs
islist = sortParts(islist);
//print("Number of IScopes: " +& intString(iScopeSize(islist)) +& "\n");
//print("Max Inst Scope: " +& intString(instScopesDepth(islist)) +& "\n");
islist = filterRedeclareIScopes(islist, {});
//print("Filtered Number of IScopes: " +& intString(iScopeSize(islist)) +& "\n");
//print("Filtered Max Inst Scope: " +& intString(instScopesDepth(islist)) +& "\n");
islist = collapseDerivedClassChain(islist, {});
//print("After derived colapse Number of IScopes: " +& intString(iScopeSize(islist)) +& "\n");
//print("After derived colapse Filtered Max Inst Scope: " +& intString(instScopesDepth(islist)) +& "\n");
islist = fixpointCleanRedeclareIScopes(islist);
//print("After fixpoint clean Number of IScopes: " +& intString(iScopeSize(islist)) +& "\n");
//print("After fixpoint clean Filtered Max Inst Scope: " +& intString(instScopesDepth(islist)) +& "\n");
printIScopes(islist);
then
islist;
else
equation
true = Flags.isSet(Flags.FAILTRACE);
name = Absyn.pathString(inClassPath);
Debug.traceln("NFSCodeAnalyseRedeclare.analyse failed on: " +& name);
then
fail();
end matchcontinue;
end analyse;
protected function analyseItem
input Item inItem;
input Env inEnv;
input Modifier inMod;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
output IScopes outIScopes;
output InstStack outInstStack;
algorithm
(outIScopes, outInstStack) :=
matchcontinue(inItem, inEnv, inMod, inPrefix, inIScopesAcc, inInstStack)
local
list<Element> el;
list<tuple<Element, Modifier>> mel;
Absyn.TypeSpec dty, ts, tsFull;
Item item;
Env env, envDerived;
Absyn.Info info;
SCode.Mod smod;
Modifier mod;
NFSCodeEnv.AvlTree cls_and_vars;
String name;
Absyn.ArrayDim dims;
Element scls;
Integer dim_count;
list<NFSCodeEnv.Extends> exts;
SCode.Restriction res;
SCode.Attributes attr;
Option<SCode.Comment> cmt;
InstStack ii;
IScopes islist;
IScope is;
list<NFSCodeEnv.Redeclaration> redeclares;
Replacements replacements;
list<tuple<Item, Env>> previousItem;
Infos infos;
Absyn.Path fullName;
// filter out some classes!
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name, restriction = res),
env = env),
_, _, _, _, _)
equation
true = listMember(name, {"equalityConstraint", "Orientation"});
//is = mkIScope(CL(Absyn.IDENT(name)), {EI(scls, env)}, {});
//islist = is::inIScopesAcc;
islist = inIScopesAcc;
then
(islist, inInstStack);
// filter out operators
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name, restriction = res),
env = env),
_, _, _, _, _)
equation
true = boolOr(SCode.isOperator(scls),
stringEq(name, "Complex"));
islist = inIScopesAcc;
then
(islist, inInstStack);
// extending basic type
case (NFSCodeEnv.CLASS(
cls = SCode.CLASS(name = name),
classType = NFSCodeEnv.BASIC_TYPE()),
_, _, _, _, _)
equation
//is = mkIScope(CL(Absyn.IDENT(name)),{}, {});
//islist = is::inIScopesAcc;
islist = inIScopesAcc;
then
(islist, inInstStack);
// enumerations
case (NFSCodeEnv.CLASS(
cls = SCode.CLASS(name = name, classDef = SCode.ENUMERATION(enumLst = _))),
_, _, _, _, _)
equation
//is = mkIScope(CL(Absyn.IDENT(name)),{}, {});
//islist = is::inIScopesAcc;
islist = inIScopesAcc;
then
(islist, inInstStack);
// a derived class from basic type.
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name, classDef = SCode.DERIVED(dty, smod, attr), info = info)),
_, _, _, _, _)
equation
// Look up the inherited class.
(item as NFSCodeEnv.CLASS(classType = NFSCodeEnv.BASIC_TYPE()), _, env) =
NFSCodeLookup.lookupTypeSpec(dty, inEnv, info);
//is = mkIScope(CL(Absyn.IDENT(name)),{EI(scls, env)}, {});
//islist = is::inIScopesAcc;
islist = inIScopesAcc;
then
(islist, inInstStack);
// a derived class, look up the inherited class and instantiate it.
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name, classDef = SCode.DERIVED(dty, smod, attr), info = info),
env = envDerived),
_, _, _, _, _)
equation
// Look up the inherited class.
(item, ts, env) = NFSCodeLookup.lookupTypeSpec(dty, inEnv, info);
tsFull = NFSCodeEnv.mergeTypeSpecWithEnvPath(ts, env);
(item, env, previousItem) = NFSCodeEnv.resolveRedeclaredItem(item, env);
// Merge the modifiers and instantiate the inherited class.
dims = Absyn.typeSpecDimensions(dty);
dim_count = listLength(dims);
mod = NFSCodeMod.translateMod(smod, "", dim_count, inPrefix, inEnv);
mod = NFSCodeMod.mergeMod(inMod, mod);
// Apply the redeclarations from the derived environment!!!!
// print("getting redeclares: item: " +& NFSCodeEnv.itemStr(item) +& "\n");
redeclares = listAppend(
NFSCodeEnv.getDerivedClassRedeclares(name, ts, envDerived),
NFSCodeFlattenRedeclare.extractRedeclaresFromModifier(smod));
(item, env, replacements) = NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(redeclares, item, env, inEnv, inPrefix);
(islist, ii) = analyseItem(item, env, mod, inPrefix, emptyIScopes, inInstStack);
scls = SCode.setDerivedTypeSpec(scls, tsFull);
infos = mkInfos(previousItem, {RP(replacements), EI(scls, env)});
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
is = mkIScope(CL(fullName), infos, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
// a class with parts, instantiate all elements in it.
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name, classDef = SCode.PARTS(elementLst = el), info = info),
env = {NFSCodeEnv.FRAME(clsAndVars = cls_and_vars)}),
_, _, _, _, _)
equation
// Enter the class scope and look up all class elements.
env = NFSCodeEnv.mergeItemEnv(inItem, inEnv);
// Apply modifications to the elements and instantiate them.
mel = NFSCodeMod.applyModifications(inMod, el, inPrefix, env);
exts = NFSCodeEnv.getEnvExtendsFromTable(env);
(islist, ii) = analyseElementList(mel, exts, env, inPrefix, emptyIScopes, inInstStack);
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
is = mkIScope(CL(fullName), {EI(scls, inEnv)}, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
// a class extends
case (NFSCodeEnv.CLASS(
cls = scls as SCode.CLASS(name = name), classType = NFSCodeEnv.CLASS_EXTENDS(), env = env),
_, _, _, _, _)
equation
//(islist, ii) = analyseClassExtends(scls, inMod, env, inEnv, inPrefix, inIScopesAcc/*emptyIScopes*/, inInstStack);
//fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
//is = mkIScope(CL(fullName), {EI(scls, env)}, islist);
//islist = is::inIScopesAcc;
islist = inIScopesAcc;
ii = inInstStack;
then
(islist, ii);
// a redeclared item
case (NFSCodeEnv.REDECLARED_ITEM(item = item, declaredEnv = env), _, _, _, _, _)
equation
name = NFSCodeEnv.getItemName(item);
(islist, ii) = analyseItem(item, env, inMod, inPrefix, emptyIScopes, inInstStack);
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), env);
is = mkIScope(RE(fullName), {RI(item, inEnv)}, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
// failure
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("NFSCodeAnalyseRedeclare.instClassItem failed on unknown class.\n");
then
fail();
end matchcontinue;
end analyseItem;
protected function analyseElementList
"Helper function to analyseItem."
input list<tuple<Element, Modifier>> inElements;
input list<NFSCodeEnv.Extends> inExtends;
input Env inEnv;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
output IScopes outIScopes;
output InstStack outInstStack;
algorithm
(outIScopes, outInstStack) :=
match(inElements, inExtends, inEnv, inPrefix, inIScopesAcc, inInstStack)
local
tuple<Element, Modifier> elem, new_elem;
list<tuple<Element, Modifier>> rest_el;
list<NFSCodeEnv.Extends> exts;
InstStack ii;
Env env;
IScopes islist;
String str;
list<tuple<Item, Env>> previousItem;
Modifier orig_mod;
case (elem :: rest_el, exts, _, _, islist, _)
equation
(new_elem, orig_mod, env, previousItem) = resolveRedeclaredElement(elem, inEnv, inPrefix);
(islist, exts, ii) = analyseElement_dispatch(new_elem, elem, orig_mod, exts, env, inPrefix, islist, inInstStack, previousItem);
(islist, ii) = analyseElementList(rest_el, exts, inEnv, inPrefix, islist, ii);
then
(islist, ii);
case ({}, {}, _, _, _, _) then (inIScopesAcc, inInstStack);
// analyseElementList takes a list of Extends, which contains the extends
// information from the environment. We should have one Extends element for
// each extends clause, so if we have any left when we've run out of
// elements something has gone very wrong.
case ({}, _ :: _, _, _, _, _)
equation
str = "NFSCodeAnalyseRedeclare.analyseElementList has extends left! \n\t" +&
stringDelimitList(List.map(inExtends, NFSCodeEnv.printExtendsStr), "\n\t");
Error.addMessage(Error.INTERNAL_ERROR, {str});
print(str);
then
fail();
end match;
end analyseElementList;
public function resolveRedeclaredElement
"This function makes sure that an element is up-to-date in case it has been
redeclared. This is achieved by looking the element up in the environment. In
the case that the element has been redeclared, the environment where it should
be instantiated is returned, otherwise the old environment."
input tuple<SCode.Element, Modifier> inElement;
input Env inEnv;
input Prefix inPrefix;
output tuple<SCode.Element, Modifier> outElement;
output Modifier outOriginalMod;
output Env outEnv;
output list<tuple<NFSCodeEnv.Item, Env>> outPreviousItem;
algorithm
(outElement, outOriginalMod, outEnv, outPreviousItem) := match(inElement, inEnv, inPrefix)
local
Modifier mod, omod;
String name;
Item item;
SCode.Element orig_el, new_el;
Env env;
list<tuple<NFSCodeEnv.Item, Env>> previousItem;
// Only components which are actually replaceable needs to be looked up,
// since non-replaceable components can't have been replaced.
case ((orig_el as SCode.COMPONENT(name = name, prefixes =
SCode.PREFIXES(replaceablePrefix = SCode.REPLACEABLE(_))), mod), _, _)
equation
(item, _) = NFSCodeLookup.lookupInClass(name, inEnv);
(NFSCodeEnv.VAR(var = new_el), env, previousItem) = NFSCodeEnv.resolveRedeclaredItem(item, inEnv);
omod = getOriginalMod(orig_el, inEnv, inPrefix);
then
((new_el, mod), omod, env, previousItem);
// Other elements doesn't need to be looked up. Extends may not be
// replaceable, and classes are looked up in the environment anyway. The
// exception is packages with constants, but those are handled in
// instPackageConstants.
else (inElement, NFInstTypesOld.NOMOD(), inEnv, {});
end match;
end resolveRedeclaredElement;
protected function getOriginalMod
input SCode.Element inOriginalElement;
input Env inEnv;
input Prefix inPrefix;
output Modifier outModifier;
algorithm
outModifier := match(inOriginalElement, inEnv, inPrefix)
local
SCode.Ident name;
Absyn.ArrayDim ad;
Integer dim_count;
SCode.Mod smod;
Modifier mod;
case (SCode.COMPONENT(modifications = SCode.NOMOD()), _, _)
then NFInstTypesOld.NOMOD();
case (SCode.COMPONENT(name = name, attributes = SCode.ATTR(arrayDims = ad),
modifications = smod), _, _)
equation
dim_count = listLength(ad);
mod = NFSCodeMod.translateMod(smod, name, dim_count, inPrefix, inEnv);
then
mod;
end match;
end getOriginalMod;
protected function analyseElement_dispatch
"Helper function to analyseElementList.
Dispatches the given element to the correct function for transformation."
input tuple<Element, Modifier> inElement;
input tuple<Element, Modifier> inOriginalElement;
input Modifier inOriginalMod;
input list<NFSCodeEnv.Extends> inExtends;
input Env inEnv;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
input list<tuple<Item, Env>> inPreviousItem;
output IScopes outIScopes;
output list<NFSCodeEnv.Extends> outExtends;
output InstStack outInstStack;
algorithm
(outIScopes, outExtends, outInstStack) :=
matchcontinue(inElement, inOriginalElement, inOriginalMod, inExtends, inEnv, inPrefix, inIScopesAcc, inInstStack, inPreviousItem)
local
Element elem, orig;
Modifier mod;
Redeclarations redecls;
list<NFSCodeEnv.Extends> rest_exts;
InstStack ii;
IScopes islist;
// A component
case ((elem as SCode.COMPONENT(name = _), mod), (orig, _), _, _, _, _, _, _, _)
equation
(islist, ii) = analyseElement(elem, mod, orig, inOriginalMod, inEnv, inPrefix, inIScopesAcc, inInstStack, inPreviousItem);
then
(islist, inExtends, ii);
// A class
case ((elem as SCode.CLASS(name = _), mod), (orig, _), _, _, _, _, _, _, _)
equation
(islist, ii) = analyseElement(elem, mod, orig, inOriginalMod, inEnv, inPrefix, inIScopesAcc, inInstStack, inPreviousItem);
then
(islist, inExtends, ii);
// An extends clause. Transform it it together with the next Extends element from the environment.
case ((elem as SCode.EXTENDS(baseClassPath = _), mod), (orig, _), _,
NFSCodeEnv.EXTENDS(redeclareModifiers = redecls) :: rest_exts, _, _, _, _, _)
equation
(islist, ii) = analyseExtends(elem, mod, redecls, inEnv, inPrefix, inIScopesAcc, inInstStack);
then
(islist, rest_exts, ii);
// We should have one Extends element for each extends clause in the class.
// If we get an extends clause but don't have any Extends elements left,
// something has gone very wrong.
case ((SCode.EXTENDS(baseClassPath = _), _), _, _, _, {}, _, _, _, _)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"NFSCodeAnalyseRedeclare.analyseElement_dispatch ran out of extends!."});
then
fail();
// Ignore any other kind of elements (class definitions, etc.).
else
equation
true = Flags.isSet(Flags.FAILTRACE);
(elem, _) = inElement;
print("Ignoring: " +& SCodeDump.unparseElementStr(elem) +& "\n\t" +&
"in env: " +& NFSCodeEnv.getEnvName(inEnv) +& "\n\t" +&
"inst stack: " +& stringDelimitList(List.map(inInstStack, Absyn.pathString), "\n\t") +& "\n\t" +&
"prefix:" +& Absyn.pathString(NFInstUtil.prefixToPath(inPrefix)) +& "\n");
then
(inIScopesAcc, inExtends, inInstStack);
end matchcontinue;
end analyseElement_dispatch;
protected function analyseElement
input Element inElement;
input Modifier inClassMod;
input Element inOrigElement;
input Modifier inOriginalMod;
input Env inEnv;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
input list<tuple<Item, Env>> inPreviousItem;
output IScopes outIScopes;
output InstStack outInstStack;
algorithm
(outIScopes, outInstStack) :=
matchcontinue(inElement, inClassMod, inOrigElement, inOriginalMod, inEnv, inPrefix, inIScopesAcc, inInstStack, inPreviousItem)
local
Absyn.Info info;
Absyn.Path tpath;
Element comp, orig;
Env env;
Item item;
Redeclarations redecls;
Prefix prefix;
SCode.Mod smod;
Modifier mod;
String name;
InstStack ii;
Option<Absyn.ArrayDim> ad;
IScopes islist;
IScope is;
NFSCodeFlattenRedeclare.Replacements replacements;
list<tuple<Item, Env>> previousItem;
Infos infos;
Absyn.Path fullName;
SCode.Restriction res;
// A component, look up it's type and instantiate that class.
case (SCode.COMPONENT(
name = name,
typeSpec = Absyn.TPATH(tpath, ad),
modifications = smod,
info = info), _, orig, _, _, _, _, _, _)
equation
// Look up the class of the component.
// print("Looking up: " +& Absyn.pathString(tpath) +& " for component: " +& name +& "\n");
(item, tpath, env) = NFSCodeLookup.lookupClassName(tpath, inEnv, info);
tpath = NFSCodeEnv.mergePathWithEnvPath(tpath, env);
false = List.applyAndFold1(inInstStack, boolOr, Absyn.pathEqual, tpath, false);
// add it
ii = tpath::inInstStack;
(item, env, previousItem) = NFSCodeEnv.resolveRedeclaredItem(item, env);
prefix = NFInstUtil.addPrefix(name, {}, inPrefix);
// Check that it's legal to instantiate the class.
NFSCodeCheck.checkInstanceRestriction(item, prefix, info);
// Merge the class modifications with this element's modifications.
mod = NFSCodeMod.translateMod(smod, name, 0, inPrefix, inEnv);
mod = NFSCodeMod.mergeMod(inOriginalMod, mod);
mod = NFSCodeMod.mergeMod(inClassMod, mod);
// Apply redeclarations to the class definition and instantiate it.
redecls = NFSCodeFlattenRedeclare.extractRedeclaresFromModifier(smod);
(item, env, replacements) = NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(redecls, item, env, inEnv, inPrefix);
(islist, ii) = analyseItem(item, env, mod, prefix, emptyIScopes, ii);
// remove it
ii = inInstStack;
comp = inElement;
comp = SCode.setComponentTypeSpec(comp, Absyn.TPATH(tpath, ad));
comp = SCode.mergeComponentModifiers(comp, orig);
infos = mkInfos(List.union(previousItem,inPreviousItem), {RP(replacements), EI(comp, env)});
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
is = mkIScope(CO(prefix,fullName), infos, islist);
islist = Util.if_(List.isEmpty(islist),inIScopesAcc, is::inIScopesAcc);
then
(islist, ii);
// ignore class extends
case (SCode.CLASS(
name = name,
info = info,
classDef = SCode.CLASS_EXTENDS(baseClassName = _)
), _, _, _, _, _, _, _, _)
equation
then
(inIScopesAcc,inInstStack);
// ignore operators
case (SCode.CLASS(
name = name,
info = info,
restriction = res
), _, _, _, _, _, _, _, _)
equation
true = boolOr(SCode.isOperator(inElement), stringEq(name, "Complex"));
then
(inIScopesAcc,inInstStack);
// only replaceable?? functions, packages, classes
case (SCode.CLASS(
name = name,
info = info,
prefixes = SCode.PREFIXES(replaceablePrefix = _ /*SCode.REPLACEABLE(_)*/)),
_, _, _, _, _, _, _, _)
equation
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
// print("Looking up CLASS: " +& Absyn.pathString(fullName) +& "\n");
false = List.applyAndFold1(inInstStack, boolOr, Absyn.pathEqual, fullName, false);
// add it
ii = fullName::inInstStack;
(item, env) = NFSCodeLookup.lookupInClass(name, inEnv);
(item, env, previousItem) = NFSCodeEnv.resolveRedeclaredItem(item, env);
(islist, ii) = analyseItem(item, env, inClassMod, inPrefix, {}, ii);
// remove it
ii = inInstStack;
infos = mkInfos(List.union(previousItem,inPreviousItem), {EI(inElement, env)});
// for debugging
// fullName = Absyn.joinPaths(fullName, Absyn.IDENT("$local"));
is = mkIScope(CL(fullName), infos, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
// for debugging
case (_, _, _, _, _, _, _, _, _)
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("NFSCodeAnalyseRedeclare.instElement ignored element:" +& SCodeDump.unparseElementStr(inElement) +& "\n");
then
fail();
else (inIScopesAcc, inInstStack);
end matchcontinue;
end analyseElement;
protected function analyseExtends
input Element inExtends;
input Modifier inClassMod;
input Redeclarations inRedeclares;
input Env inEnv;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
output IScopes outIScopes;
output InstStack outInstStack;
algorithm
(outIScopes, outInstStack) :=
match(inExtends, inClassMod, inRedeclares, inEnv, inPrefix, inIScopesAcc, inInstStack)
local
Absyn.Path path;
SCode.Mod smod;
Absyn.Info info;
Item item, itemOld;
Env env;
Modifier mod;
SCode.Visibility visibility;
Option<SCode.Annotation> ann;
InstStack ii;
IScopes islist;
IScope is;
Replacements replacements;
Infos infos;
case (SCode.EXTENDS(path, visibility, smod, ann, info),
_, _, _, _, _, _)
equation
// Look up the base class in the environment.
(item, path, env) = NFSCodeLookup.lookupBaseClassName(path, inEnv, info);
itemOld = item;
path = NFSCodeEnv.mergePathWithEnvPath(path, env);
checkRecursiveExtends(path, inEnv, info);
// Instantiate the class.
mod = NFSCodeMod.translateMod(smod, "", 0, inPrefix, inEnv);
mod = NFSCodeMod.mergeMod(inClassMod, mod);
// Apply the redeclarations.
(item, env, replacements) = NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(inRedeclares, item, env, inEnv, inPrefix);
(islist, ii) = analyseItem(item, env, mod, inPrefix, emptyInstStack, inInstStack);
infos = mkInfos({}, {RP(replacements),EI(inExtends, inEnv)});
is = mkIScope(EX(path), infos, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("NFSCodeAnalyseRedeclare.instExtends failed on unknown element.\n");
then
fail();
end match;
end analyseExtends;
public function checkRecursiveExtends
input Absyn.Path inExtendedClass;
input Env inEnv;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inExtendedClass, inEnv, inInfo)
local
Absyn.Path env_path;
String path_str;
case (_, _, _)
equation
env_path = NFSCodeEnv.getEnvPath(inEnv);
false = Absyn.pathPrefixOf(inExtendedClass, env_path);
then
();
else
equation
path_str = Absyn.pathString(inExtendedClass);
Error.addSourceMessage(Error.RECURSIVE_EXTENDS, {path_str}, inInfo);
then
fail();
end matchcontinue;
end checkRecursiveExtends;
protected function analyseClassExtends
input Element inClassExtends;
input Modifier inMod;
input Env inClassEnv;
input Env inEnv;
input Prefix inPrefix;
input IScopes inIScopesAcc;
input InstStack inInstStack;
output IScopes outIScopes;
output InstStack outInstStack;
algorithm
(outIScopes, outInstStack) :=
matchcontinue(inClassExtends, inMod, inClassEnv, inEnv, inPrefix, inIScopesAcc, inInstStack)
local
SCode.ClassDef cdef;
SCode.Mod mod;
Element scls, ext;
Absyn.Path bc_path, fullName;
Absyn.Info info;
String name;
Item item;
Env base_env, ext_env;
Element base_cls, ext_cls, comp_cls;
list<Element> classes;
InstStack ii;
IScopes islist;
IScope is;
case (SCode.CLASS(
name = name,
classDef = SCode.CLASS_EXTENDS(modifications = mod,composition = cdef)),
_, _, _, _, _, _)
equation
(bc_path, info) = getClassExtendsBaseClass(inClassEnv);
ext = SCode.EXTENDS(bc_path, SCode.PUBLIC(), mod, NONE(), info);
cdef = SCode.addElementToCompositeClassDef(ext, cdef);
scls = SCode.setElementClassDefinition(cdef, inClassExtends);
item = NFSCodeEnv.CLASS(scls, inClassEnv, NFSCodeEnv.CLASS_EXTENDS());
(islist, ii) = analyseItem(item, inEnv, inMod, inPrefix, emptyIScopes, inInstStack);
fullName = NFSCodeEnv.mergePathWithEnvPath(Absyn.IDENT(name), inEnv);
is = mkIScope(CL(fullName), {EI(scls, inEnv)}, islist);
islist = is::inIScopesAcc;
then
(islist, ii);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
name = SCode.elementName(inClassExtends);
Debug.traceln("NFSCodeAnalyseRedeclare.instClassExtends failed on " +& name);
then
fail();
end matchcontinue;
end analyseClassExtends;
protected function getClassExtendsBaseClass
input Env inClassEnv;
output Absyn.Path outPath;
output Absyn.Info outInfo;
algorithm
(outPath, outInfo) := matchcontinue(inClassEnv)
local
Absyn.Path bc;
Absyn.Info info;
String name;
case (NFSCodeEnv.FRAME(extendsTable = NFSCodeEnv.EXTENDS_TABLE(
baseClasses = NFSCodeEnv.EXTENDS(baseClass = bc, info = info) :: _)) :: _)
then (bc, info);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
name = NFSCodeEnv.getEnvName(inClassEnv);
Debug.traceln("NFSCodeAnalyseRedeclare.getClassExtendsBaseClass failed on " +& name);
then
fail();
end matchcontinue;
end getClassExtendsBaseClass;
protected function mkInfos
input list<tuple<Item, Env>> inPreviousItems;
input Infos inInfosAcc;
output Infos outInfos;
algorithm
outInfos := match(inPreviousItems, inInfosAcc)
local
list<tuple<Item, Env>> rest;
Item i; Env e;
case ({}, _) then filterInfos(inInfosAcc, {});
case ((i, e)::rest, _) then mkInfos(rest, RI(i, e)::inInfosAcc);
end match;
end mkInfos;
protected function filterInfos
"remove RP({})"
input Infos inInfos;
input Infos inInfosAcc;
output Infos outInfos;
algorithm
outInfos := matchcontinue(inInfos, inInfosAcc)
local
Infos rest;
Info i;
case ({}, _) then inInfosAcc;
case (RP({})::rest, _)
then filterInfos(rest, inInfosAcc);
case (i::rest, _)
then filterInfos(rest, i::inInfosAcc);
end matchcontinue;
end filterInfos;
public function cleanRedeclareIScopes
input IScopes inIScopes;
input IScopes inIScopesAcc;
output IScopes outIScopes;
output Boolean hasChanged;
algorithm
(outIScopes, hasChanged) := matchcontinue(inIScopes, inIScopesAcc)
local
IScope s;
Kind k;
Infos i, i1, i2;
IScopes p, rest, acc, p2;
Boolean b, b1, b2;
Absyn.Path n1, n2;
Element c, e;
Env env;
Absyn.TypeSpec ts;
SCode.Mod mNew, mOld, m;
case ({}, _) then (listReverse(inIScopesAcc), false);
// extends with no kids has no meaning
case (IS(kind = EX(_), parts = {})::rest, acc)
equation
(acc, b) = cleanRedeclareIScopes(rest, acc);
then
(acc, true);
// component targeting a local replaceable derived class
// replaceable HeatTransfer = Blah.Blah.IdealHeatTransfer;
// HeatTransfer heatTransfer(redeclare Medium = Medium)
case (IS(k as CO(_,n1), i1, {IS(CL(n2), i2, p2)})::rest, acc)
equation
// same scope!
//n1 = Absyn.stripLast(n1);
//n2 = Absyn.stripLast(n2);
//true = Absyn.pathEqual(n1, n2);
EI(e, env) = getEIFromInfos(i1);
EI(c, _) = getEIFromInfos(i2);
ts = SCode.getDerivedTypeSpec(c);
mNew = SCode.getDerivedMod(c);
mOld = SCode.getComponentMod(e);
// in this case we merge component modifiers as NEW
// and type modifiers as OLD
m = SCode.mergeModifiers(mOld, mNew);
e = SCode.setComponentTypeSpec(e, ts);
e = SCode.setComponentMod(e, m);
i1 = listAppend(i1, i2);
i1 = {EI(e, env)}; //::i1;