/
SCodeDependency.mo
2018 lines (1797 loc) · 60.9 KB
/
SCodeDependency.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 SCodeDependency
" file: SCodeDependency.mo
package: SCodeDependency
description: SCode dependency analysis.
RCS: $Id$
Dependency analysis for SCode.
"
public import Absyn;
public import SCode;
public import SCodeEnv;
public type Env = SCodeEnv.Env;
protected import Debug;
protected import Error;
protected import List;
protected import RTOpts;
protected import SCodeCheck;
protected import SCodeFlattenRedeclare;
protected import SCodeLookup;
protected import SCodeUtil;
protected import Util;
protected type Item = SCodeEnv.Item;
protected type Extends = SCodeEnv.Extends;
protected type FrameType = SCodeEnv.FrameType;
protected type Import = Absyn.Import;
public function analyse
"This is the entry point of the dependency analysis. The dependency analysis
is done in three steps: first it analyses the program and marks each element in
the program that's used. The it goes through the used classes and checks if
they contain any class extends, and if so it checks of those class extends are
used or not. Finally it collects the used elements and builds a new program
and environment that only contains those elements."
input Absyn.Path inClassName;
input Env inEnv;
input SCode.Program inProgram;
output SCode.Program outProgram;
output Env outEnv;
algorithm
analyseClass(inClassName, inEnv, Absyn.dummyInfo);
analyseClassExtends(inEnv);
(outEnv, outProgram) := collectUsedProgram(inEnv, inProgram, inClassName);
end analyse;
protected function analyseClass
"Analyses a class by looking up the class, marking it as used and recursively
analysing it's contents."
input Absyn.Path inClassName;
input Env inEnv;
input Absyn.Info inInfo;
protected
Item item;
Env env;
algorithm
_ := matchcontinue(inClassName, inEnv, inInfo)
local
Item item;
Env env;
case (_, _, _)
equation
(item, env) = lookupClass(inClassName, inEnv, inInfo, true);
checkItemIsClass(item);
analyseItem(item, env);
then
();
else
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- SCodeDependency.analyseClass failed for " +&
Absyn.pathString(inClassName) +& " in " +&
SCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end analyseClass;
protected function lookupClass
"Lookup a class in the environment. The reason why SCodeLookup is not used
directly is because we need to look up each part of the class path and mark
them as used."
input Absyn.Path inPath;
input Env inEnv;
input Absyn.Info inInfo;
input Boolean inPrintError;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := matchcontinue(inPath, inEnv, inInfo, inPrintError)
local
Item item;
Env env;
String name_str, env_str;
case (_, _, _, _)
equation
(item, env) = lookupClass2(inPath, inEnv, inInfo, inPrintError);
then
(item, env);
case (_, _, _, true)
equation
name_str = Absyn.pathString(inPath);
env_str = SCodeEnv.getEnvName(inEnv);
Error.addSourceMessage(Error.LOOKUP_ERROR, {name_str, env_str}, inInfo);
then
fail();
end matchcontinue;
end lookupClass;
protected function lookupClass2
"Help function to lookupClass, does the actual look up."
input Absyn.Path inPath;
input Env inEnv;
input Absyn.Info inInfo;
input Boolean inPrintError;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := match(inPath, inEnv, inInfo, inPrintError)
local
Item item;
Env env;
String id;
Absyn.Path rest_path;
SCodeEnv.Frame frame;
case (Absyn.IDENT(name = _), _, _, _)
equation
(item, _, env) =
SCodeLookup.lookupName(inPath, inEnv, inInfo, NONE());
then
(item, env);
case (Absyn.QUALIFIED(name = id, path = rest_path), _, _, _)
equation
(item, _, env) =
SCodeLookup.lookupName(Absyn.IDENT(id), inEnv, inInfo, NONE());
analyseItem(item, env);
(item, env) = lookupNameInItem(rest_path, item, env, inPrintError);
then
(item, env);
case (Absyn.FULLYQUALIFIED(path = rest_path), _, _, _)
equation
env = SCodeEnv.getEnvTopScope(inEnv);
(item, env) = lookupClass2(rest_path, env, inInfo, inPrintError);
then
(item, env);
end match;
end lookupClass2;
protected function lookupNameInItem
input Absyn.Path inName;
input Item inItem;
input Env inEnv;
input Boolean inPrintError;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := match(inName, inItem, inEnv, inPrintError)
local
Absyn.Path type_path;
SCode.Mod mods;
Absyn.Info info;
Env env, type_env;
SCodeEnv.Frame class_env;
list<SCodeEnv.Redeclaration> redeclares;
Item item;
case (_, _, {}, _) then (inItem, inEnv);
case (_, SCodeEnv.VAR(var = SCode.COMPONENT(typeSpec =
Absyn.TPATH(path = type_path), modifications = mods, info = info)), _, _)
equation
(item, type_env) = lookupClass(type_path, inEnv, info, inPrintError);
redeclares = SCodeFlattenRedeclare.extractRedeclaresFromModifier(
mods, inEnv);
(item, type_env) = SCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(
redeclares, item, type_env, inEnv);
(item, env) = lookupNameInItem(inName, item, type_env, inPrintError);
then
(item, env);
case (_, SCodeEnv.CLASS(cls = SCode.CLASS(info = info), env = {class_env}), _, _)
equation
env = SCodeEnv.enterFrame(class_env, inEnv);
(item, env) = lookupClass(inName, env, info, inPrintError);
then
(item, env);
end match;
end lookupNameInItem;
protected function checkItemIsClass
"Checks that the found item really is a class, otherwise prints an error
message."
input Item inItem;
algorithm
_ := match(inItem)
local
String name;
Absyn.Info info;
case SCodeEnv.CLASS(cls = _) then ();
// We found a component instead, which might happen if the user tries to use
// a variable name as a type.
case SCodeEnv.VAR(var = SCode.COMPONENT(name = name, info = info))
equation
Error.addSourceMessage(Error.LOOKUP_TYPE_FOUND_COMP, {name}, info);
then
fail();
end match;
end checkItemIsClass;
protected function analyseItem
"Analyses an item."
input Item inItem;
input Env inEnv;
algorithm
_ := matchcontinue(inItem, inEnv)
local
SCode.ClassDef cdef;
SCodeEnv.Frame cls_env;
Env env;
Absyn.Info info;
SCode.Ident name;
SCode.Restriction res;
SCode.Element cls;
// Check if the item is already marked as used, then we can stop here.
case (_, _)
equation
true = SCodeEnv.isItemUsed(inItem);
then
();
// A component, mark it and it's environment as used.
case (SCodeEnv.VAR(var = _), env)
equation
markItemAsUsed(inItem, env);
then
();
// A class without an environment is one of the builtin types (Real, etc.),
// so we don't need to do anything here.
case (SCodeEnv.CLASS(env = {}), _) then ();
// A normal class, mark it and it's environment as used, and recursively
// analyse it's contents.
case (SCodeEnv.CLASS(cls = cls as SCode.CLASS(classDef = cdef,
restriction = res, info = info), env = {cls_env}), env)
equation
markItemAsUsed(inItem, env);
env = SCodeEnv.enterFrame(cls_env, env);
analyseClassDef(cdef, res, env, false, info);
analyseMetaType(res, env, info);
_ :: env = env;
analyseRedeclaredClass(cls, env);
then
();
else
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- SCodeDependency.analyseItem failed on " +&
SCodeEnv.getItemName(inItem) +& " in " +&
SCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end analyseItem;
protected function markItemAsUsed
"Marks an item and it's environment as used."
input Item inItem;
input Env inEnv;
algorithm
_ := match(inItem, inEnv)
local
SCodeEnv.Frame cls_env;
Util.StatefulBoolean is_used;
String name;
case (SCodeEnv.VAR(isUsed = is_used), _)
equation
Util.setStatefulBoolean(is_used, true);
markEnvAsUsed(inEnv);
then
();
case (SCodeEnv.CLASS(env = {cls_env}, cls = SCode.CLASS(name = name)), _)
equation
markFrameAsUsed(cls_env);
markEnvAsUsed(inEnv);
then
();
end match;
end markItemAsUsed;
protected function markFrameAsUsed
"Marks a single frame as used."
input SCodeEnv.Frame inFrame;
protected
Util.StatefulBoolean is_used;
algorithm
SCodeEnv.FRAME(isUsed = is_used) := inFrame;
Util.setStatefulBoolean(is_used, true);
end markFrameAsUsed;
protected function markEnvAsUsed
"Marks an environment as used. This is done by marking each frame as used, and
for each frame we also analyse the class it represents to make sure we don't
miss anything in the enclosing scopes of an item."
input Env inEnv;
algorithm
_ := matchcontinue(inEnv)
local
Util.StatefulBoolean is_used;
Env rest_env;
SCodeEnv.Frame f;
case ((f as SCodeEnv.FRAME(isUsed = is_used)) :: rest_env)
equation
false = Util.getStatefulBoolean(is_used);
markEnvAsUsed2(f, rest_env);
Util.setStatefulBoolean(is_used, true);
markEnvAsUsed(rest_env);
then
();
else then ();
end matchcontinue;
end markEnvAsUsed;
protected function markEnvAsUsed2
"Helper function to markEnvAsUsed. Checks if the given frame belongs to a
class, and if that's the case calls analyseClass on that class."
input SCodeEnv.Frame inFrame;
input SCodeEnv.Env inEnv;
algorithm
_ := match(inFrame, inEnv)
local
String name;
case (SCodeEnv.FRAME(frameType = SCodeEnv.IMPLICIT_SCOPE()), _) then ();
case (SCodeEnv.FRAME(name = SOME(name)), _)
equation
analyseClass(Absyn.IDENT(name), inEnv, Absyn.dummyInfo);
then
();
end match;
end markEnvAsUsed2;
protected function analyseClassDef
"Analyses the contents of a class definition."
input SCode.ClassDef inClassDef;
input SCode.Restriction inRestriction;
input Env inEnv;
input Boolean inInModifierScope;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inClassDef, inRestriction, inEnv, inInModifierScope, inInfo)
local
list<SCode.Element> el;
SCode.Element e1, e2;
Absyn.Ident bc;
SCode.Mod mods;
Absyn.TypeSpec ty;
list<SCode.Equation> nel, iel;
list<SCode.AlgorithmSection> nal, ial;
Option<SCode.Comment> cmt;
list<SCode.Annotation> annl;
Option<SCode.ExternalDecl> ext_decl;
Boolean is_ext_obj;
Env ty_env, env;
Item ty_item;
SCode.Attributes attr;
list<Absyn.Path> paths;
// A class made of parts, analyse elements, equation, algorithms, etc.
case (SCode.PARTS(elementLst = el, normalEquationLst = nel,
initialEquationLst = iel, normalAlgorithmLst = nal,
initialAlgorithmLst = ial, externalDecl = ext_decl,
annotationLst = annl, comment = cmt), _, _, _, _)
equation
List.map2_0(el, analyseElement, inEnv, inRestriction);
List.map1_0(nel, analyseEquation, inEnv);
List.map1_0(iel, analyseEquation, inEnv);
List.map1_0(nal, analyseAlgorithm, inEnv);
List.map1_0(ial, analyseAlgorithm, inEnv);
analyseExternalDecl(ext_decl, inEnv, inInfo);
List.map2_0(annl, analyseAnnotation, inEnv, inInfo);
analyseComment(cmt, inEnv, inInfo);
then
();
// The previous case failed, which might happen for an external object.
// Check if the class definition is an external object and analyse it if
// that's the case.
case (SCode.PARTS(elementLst = el), _, _, _, _)
equation
isExternalObject(el, inEnv, inInfo);
analyseClass(Absyn.IDENT("constructor"), inEnv, inInfo);
analyseClass(Absyn.IDENT("destructor"), inEnv, inInfo);
then
();
// A class extends.
case (SCode.CLASS_EXTENDS(baseClassName = bc), _, _, _, _)
equation
Error.addSourceMessage(Error.INTERNAL_ERROR,
{"SCodeDependency.analyseClassDef failed on CLASS_EXTENDS"}, inInfo);
then
fail();
// A derived class definition.
case (SCode.DERIVED(typeSpec = ty, modifications = mods, attributes = attr, comment = cmt),
_, _ :: env, _, _)
equation
env = Util.if_(inInModifierScope, inEnv, env);
analyseTypeSpec(ty, env, inInfo);
(ty_item, ty_env) = SCodeLookup.lookupTypeSpec(ty, env, inInfo);
ty_env = SCodeEnv.mergeItemEnv(ty_item, ty_env);
// TODO! Analyse array dimensions from attributes!
analyseModifier(mods, inEnv, ty_env, inInfo);
analyseComment(cmt, inEnv, inInfo);
then
();
// Other cases which doesn't need to be analysed.
case (SCode.ENUMERATION(enumLst = _), _, _, _, _) then ();
case (SCode.OVERLOAD(pathLst = paths), _, _, _, _)
equation
List.map2_0(paths,analyseClass,inEnv,inInfo);
then ();
case (SCode.PDER(functionPath = _), _, _, _, _) then ();
end matchcontinue;
end analyseClassDef;
protected function isExternalObject
"Checks if a class definition is an external object."
input list<SCode.Element> inElements;
input Env inEnv;
input Absyn.Info inInfo;
protected
list<SCode.Element> el;
list<String> el_names;
algorithm
// Remove all 'extends ExternalObject'.
el := List.filter(inElements, isNotExternalObject);
// Check if length of the new list is different to the old, i.e. if we
// actually found and removed any 'extends ExternalObject'.
false := (listLength(el) == listLength(inElements));
// Ok, we have an external object, check that it's valid.
el_names := List.map(el, SCode.elementName);
checkExternalObject(el_names, inEnv, inInfo);
end isExternalObject;
protected function isNotExternalObject
"Fails on 'extends ExternalObject', otherwise succeeds."
input SCode.Element inElement;
algorithm
_ := match(inElement)
case SCode.EXTENDS(baseClassPath = Absyn.IDENT("ExternalObject")) then fail();
else then ();
end match;
end isNotExternalObject;
protected function checkExternalObject
"Checks that an external object is valid, i.e. has exactly one constructor and
one destructor."
input list<String> inElements;
input Env inEnv;
input Absyn.Info inInfo;
algorithm
_ := match(inElements, inEnv, inInfo)
local
String env_str;
Boolean has_con, has_des;
// Ok, we have both a constructor and a destructor.
case ({"constructor", "destructor"}, _, _) then ();
case ({"destructor", "constructor"}, _, _) then ();
// Otherwise it's not valid, so print an error message.
else
equation
has_con = List.isMemberOnTrue(
"constructor", inElements, stringEqual);
has_des = List.isMemberOnTrue(
"destructor", inElements, stringEqual);
env_str = SCodeEnv.getEnvName(inEnv);
checkExternalObject2(inElements, has_con, has_des, env_str, inInfo);
then
fail();
end match;
end checkExternalObject;
protected function checkExternalObject2
"Helper function to checkExternalObject. Prints an error message depending on
what the external object contained."
input list<String> inElements;
input Boolean inHasConstructor;
input Boolean inHasDestructor;
input String inObjectName;
input Absyn.Info inInfo;
algorithm
_ := match(inElements, inHasConstructor, inHasDestructor, inObjectName, inInfo)
local
list<String> el;
String el_str;
// The external object contains both a constructor and a destructor, so it
// has to also contain some invalid elements.
case (el, true, true, _, _)
equation
// Remove the constructor and destructor from the list of elements.
(el, _) = List.deleteMemberOnTrue("constructor", el, stringEqual);
(el, _) = List.deleteMemberOnTrue("destructor", el, stringEqual);
// Print an error message with the rest of the elements.
el_str = stringDelimitList(el, ", ");
el_str = "contains invalid elements: " +& el_str;
Error.addSourceMessage(Error.INVALID_EXTERNAL_OBJECT,
{inObjectName, el_str}, inInfo);
then
();
// The external object is missing a constructor.
case (_, false, true, _, _)
equation
Error.addSourceMessage(Error.INVALID_EXTERNAL_OBJECT,
{inObjectName, "missing constructor"}, inInfo);
then
();
// The external object is missing a destructor.
case (_, true, false, _, _)
equation
Error.addSourceMessage(Error.INVALID_EXTERNAL_OBJECT,
{inObjectName, "missing destructor"}, inInfo);
then
();
// The external object is missing both a constructor and a destructor.
case (_, false, false, _, _)
equation
Error.addSourceMessage(Error.INVALID_EXTERNAL_OBJECT,
{inObjectName, "missing both constructor and destructor"}, inInfo);
then
();
end match;
end checkExternalObject2;
protected function analyseMetaType
"If a metarecord is analysed we need to also analyse it's parent uniontype."
input SCode.Restriction inRestriction;
input Env inEnv;
input Absyn.Info inInfo;
algorithm
_ := match(inRestriction, inEnv, inInfo)
local
Absyn.Path union_name;
case (SCode.R_METARECORD(name = union_name), _, _)
equation
analyseClass(union_name, inEnv, inInfo);
then
();
else then ();
end match;
end analyseMetaType;
protected function analyseRedeclaredClass
"If a class is a redeclaration of an inherited class we need to also analyse
the inherited class."
input SCode.Element inClass;
input Env inEnv;
algorithm
_ := matchcontinue(inClass, inEnv)
local
Item item;
String name;
case (SCode.CLASS(name = _), _)
equation
false = SCode.isElementRedeclare(inClass);
then ();
case (SCode.CLASS(name = name), _)
equation
item = SCodeEnv.CLASS(inClass, SCodeEnv.emptyEnv, SCodeEnv.USERDEFINED());
analyseRedeclaredClass2(item, inEnv);
then
();
end matchcontinue;
end analyseRedeclaredClass;
protected function analyseRedeclaredClass2
input Item inItem;
input Env inEnv;
algorithm
_ := matchcontinue(inItem, inEnv)
local
String name;
Item item;
Env env;
SCode.Element cls;
Absyn.Info info;
case (SCodeEnv.CLASS(cls = cls as SCode.CLASS(name = name, info = info)), _)
equation
(item, env) = SCodeLookup.lookupRedeclaredClassByItem(inItem, inEnv, info);
markItemAsUsed(item, env);
then
();
else
equation
true = RTOpts.debugFlag("failtrace");
Debug.traceln("- SCodeDependency.analyseRedeclaredClass2 failed for " +&
SCodeEnv.getItemName(inItem) +& " in " +&
SCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end analyseRedeclaredClass2;
protected function analyseElement
"Analyses an element."
input SCode.Element inElement;
input Env inEnv;
input SCode.Restriction inClassRestriction;
algorithm
_ := match(inElement, inEnv, inClassRestriction)
local
Absyn.Path bc;
SCode.Mod mods;
Absyn.TypeSpec ty;
Absyn.Info info;
SCode.Attributes attr;
Option<Absyn.Exp> cond_exp;
Option<SCode.ConstrainClass> cc;
SCode.Element cls;
Item ty_item;
Env ty_env, env;
SCode.Ident name;
SCode.Prefixes prefixes;
// Fail on 'extends ExternalObject' so we can handle it as a special case in
// analyseClassDef.
case (SCode.EXTENDS(baseClassPath = Absyn.IDENT("ExternalObject")), _, _)
then fail();
// An extends-clause.
case (SCode.EXTENDS(baseClassPath = bc, modifications = mods, info = info),
_, _)
equation
env = SCodeEnv.removeExtendFromLocalScope(bc, inEnv);
true = checkNotExtendsDependent(bc, env, info);
analyseExtends(bc, env, info);
(ty_item, _, ty_env) =
SCodeLookup.lookupBaseClassName(bc, env, info);
ty_env = SCodeEnv.mergeItemEnv(ty_item, ty_env);
analyseModifier(mods, inEnv, ty_env, info);
then
();
// A component.
case (SCode.COMPONENT(name = name, attributes = attr, typeSpec = ty,
modifications = mods, condition = cond_exp, prefixes = prefixes, info = info), _, _)
equation
markAsUsedOnRestriction(name, inClassRestriction, inEnv, info);
analyseAttributes(attr, inEnv, info);
analyseTypeSpec(ty, inEnv, info);
(ty_item, ty_env) = SCodeLookup.lookupTypeSpec(ty, inEnv, info);
ty_env = SCodeEnv.mergeItemEnv(ty_item, ty_env);
SCodeCheck.checkRecursiveComponentDeclaration(name, info, ty_env,
ty_item, inEnv);
analyseModifier(mods, inEnv, ty_env, info);
analyseOptExp(cond_exp, inEnv, info);
analyseConstrainClass(SCode.replaceableOptConstraint(SCode.prefixesReplaceable(prefixes)), inEnv, info);
then
();
// equalityConstraints may not be explicitly used but might be needed anyway
// (if the record is used in a connect for example), so always mark it as used.
case (SCode.CLASS(name = name as "equalityConstraint", info = info), _, _)
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
();
else then ();
end match;
end analyseElement;
protected function markAsUsedOnRestriction
input SCode.Ident inName;
input SCode.Restriction inRestriction;
input Env inEnv;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inName, inRestriction, inEnv, inInfo)
local
SCodeEnv.AvlTree cls_and_vars;
Item item;
Util.StatefulBoolean is_used;
case (_, _, SCodeEnv.FRAME(clsAndVars = cls_and_vars) :: _, _)
equation
true = markAsUsedOnRestriction2(inRestriction);
SCodeEnv.VAR(isUsed = is_used) = SCodeEnv.avlTreeGet(cls_and_vars, inName);
Util.setStatefulBoolean(is_used, true);
then
();
else then ();
end matchcontinue;
end markAsUsedOnRestriction;
protected function markAsUsedOnRestriction2
input SCode.Restriction inRestriction;
output Boolean isRestricted;
algorithm
isRestricted := match(inRestriction)
case SCode.R_CONNECTOR(isExpandable = _) then true;
case SCode.R_RECORD() then true;
else false;
end match;
end markAsUsedOnRestriction2;
protected function checkNotExtendsDependent
"Wrapper function for checkNotExtendsDependent2 which adds the error message
count to the function arguments."
input Absyn.Path inBaseClass;
input Env inEnv;
input Absyn.Info inInfo;
output Boolean outResult;
algorithm
outResult := checkNotExtendsDependent2(inBaseClass, inEnv, inInfo,
Error.getNumErrorMessages());
end checkNotExtendsDependent;
protected function checkNotExtendsDependent2
"The Modelica specification 3.2 says (section 5.6.1): 'The lookup of the names
of extended classes should give the same result before and after flattening
the extends. One should not find any element used during this flattening by
lookup through the extends-clauses.' This means that it's not allowed to have
a name in an extends-clause that's inherited from another extends-clause. This
function checks this, and returns true if an extends doesn't depend on an
extend in the local scope."
input Absyn.Path inBaseClass;
input Env inEnv;
input Absyn.Info inInfo;
input Integer inErrorCount;
output Boolean outResult;
algorithm
outResult := matchcontinue(inBaseClass, inEnv, inInfo, inErrorCount)
local
Absyn.Path bc;
Absyn.Ident id;
String bc_name;
case (_, _, _, _)
equation
id = Absyn.pathFirstIdent(inBaseClass);
(bc, _) = SCodeLookup.lookupBaseClass(id, inEnv, inInfo);
bc_name = Absyn.pathString(bc);
Error.addSourceMessage(Error.EXTENDS_INHERITED_FROM_LOCAL_EXTENDS,
{bc_name, id}, inInfo);
then
false;
else
equation
true = intEq(inErrorCount, Error.getNumErrorMessages());
then
true;
end matchcontinue;
end checkNotExtendsDependent2;
protected function analyseExtends
"Analyses an extends-clause."
input Absyn.Path inClassName;
input Env inEnv;
input Absyn.Info inInfo;
protected
Item item;
Env env;
algorithm
(item, _, env) := SCodeLookup.lookupBaseClassName(inClassName, inEnv, inInfo);
analyseItem(item, env);
end analyseExtends;
protected function analyseAttributes
"Analyses a components attributes (actually only the array dimensions)."
input SCode.Attributes inAttributes;
input Env inEnv;
input Absyn.Info inInfo;
protected
Absyn.ArrayDim ad;
algorithm
SCode.ATTR(arrayDims = ad) := inAttributes;
List.map2_0(ad, analyseSubscript, inEnv, inInfo);
end analyseAttributes;
protected function analyseModifier
"Analyses a modifier."
input SCode.Mod inModifier;
input Env inEnv;
input Env inTypeEnv;
input Absyn.Info inInfo;
algorithm
_ := match(inModifier, inEnv, inTypeEnv, inInfo)
local
list<SCode.Element> el;
list<SCode.SubMod> sub_mods;
Option<tuple<Absyn.Exp, Boolean>> bind_exp;
// No modifier.
case (SCode.NOMOD(), _, _, _) then ();
// A normal modifier, analyse it's submodifiers and optional binding.
case (SCode.MOD(subModLst = sub_mods, binding = bind_exp), _, _, _)
equation
List.map2_0(sub_mods, analyseSubMod, (inEnv, inTypeEnv), inInfo);
analyseModBinding(bind_exp, inEnv, inInfo);
then
();
// A redeclaration modifier, analyse the redeclarations.
case (SCode.REDECL(elementLst = el), _, _, _)
equation
List.map2_0(el, analyseRedeclareModifier, inEnv, inTypeEnv);
then
();
end match;
end analyseModifier;
protected function analyseRedeclareModifier
"Analyses a redeclaration modifier element."
input SCode.Element inElement;
input Env inEnv;
input Env inTypeEnv;
algorithm
_ := match(inElement, inEnv, inTypeEnv)
local
SCode.ClassDef cdef;
Option<Absyn.ConstrainClass> cc;
Absyn.Info info;
SCode.Restriction restr;
SCode.Prefixes prefixes;
SCode.Ident name;
Item item;
// Class definitions are not analysed in analyseElement but are needed here
// in case a class is redeclared.
case (SCode.CLASS(prefixes = prefixes, classDef = cdef,
restriction = restr, info = info), _, _)
equation
analyseClassDef(cdef, restr, inEnv, true, info);
analyseConstrainClass(SCode.replaceableOptConstraint(SCode.prefixesReplaceable(prefixes)), inEnv, info);
then
();
// Otherwise we can just use analyseElements.
else
equation
analyseElement(inElement, inEnv, SCode.R_CLASS());
then
();
end match;
end analyseRedeclareModifier;
protected function analyseConstrainClass
"Analyses a constrain class, i.e. given by constrainedby."
input Option<SCode.ConstrainClass> inCC;
input Env inEnv;
input Absyn.Info inInfo;
algorithm
_ := match(inCC, inEnv, inInfo)
local
Absyn.Path path;
SCode.Mod mod;
Env env;
case (SOME(SCode.CONSTRAINCLASS(constrainingClass = path, modifier = mod)), _, _)
equation
analyseClass(path, inEnv, inInfo);
(_, _, env) = SCodeLookup.lookupClassName(path, inEnv, inInfo);
analyseModifier(mod, inEnv, env, inInfo);
then
();
else ();
end match;
end analyseConstrainClass;
protected function analyseSubMod
"Analyses a submodifier."
input SCode.SubMod inSubMod;
input tuple<Env, Env> inEnv;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inSubMod, inEnv, inInfo)
local
SCode.Ident ident;
SCode.Mod m;
list<SCode.Subscript> subs;
Env env, env2, ty_env;
Item item;
case (SCode.NAMEMOD(ident = ident, A = m), (env, ty_env), _)
equation
analyseNameMod(ident, env, ty_env, m, inInfo);
then
();
case (SCode.IDXMOD(subscriptLst = subs, an = m), (env, ty_env), _)
equation
analyseModifier(m, env, ty_env, inInfo);
then
();
end matchcontinue;
end analyseSubMod;
protected function analyseNameMod
input SCode.Ident inIdent;
input Env inEnv;
input Env inTypeEnv;
input SCode.Mod inMod;
input Absyn.Info inInfo;
protected
Option<Item> item;
Option<Env> env;
algorithm
(item, env) := lookupNameMod(Absyn.IDENT(inIdent), inTypeEnv, inInfo);
analyseNameMod2(inIdent, item, env, inEnv, inTypeEnv, inMod, inInfo);
end analyseNameMod;
protected function analyseNameMod2
input SCode.Ident inIdent;
input Option<Item> inItem;
input Option<Env> inItemEnv;
input Env inEnv;
input Env inTypeEnv;
input SCode.Mod inModifier;
input Absyn.Info inInfo;
algorithm
_ := match(inIdent, inItem, inItemEnv, inEnv, inTypeEnv, inModifier, inInfo)
local