This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
NFSCodeDependency.mo
2241 lines (1990 loc) · 68.1 KB
/
NFSCodeDependency.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-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, 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 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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 NFSCodeDependency
" file: NFSCodeDependency.mo
package: NFSCodeDependency
description: SCode dependency analysis.
Dependency analysis for SCode.
"
public import Absyn;
public import SCode;
public import NFInstPrefix;
public import NFSCodeEnv;
public type Env = NFSCodeEnv.Env;
protected import Config;
protected import Debug;
protected import Error;
protected import Flags;
protected import List;
protected import Mutable;
protected import NFSCodeCheck;
protected import NFSCodeFlattenRedeclare;
protected import NFSCodeLookup;
protected import SCodeDump;
protected import System;
protected import Util;
protected type Item = NFSCodeEnv.Item;
protected type Extends = NFSCodeEnv.Extends;
protected type FrameType = NFSCodeEnv.FrameType;
protected type Import = Absyn.Import;
protected import NFSCodeEnv.EnvTree;
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 SourceInfo inInfo;
algorithm
_ := matchcontinue(inClassName, inEnv, inInfo)
local
Item item;
Env env;
case (_, _, _)
equation
(item, env) = lookupClass(inClassName, inEnv, true, inInfo,
SOME(Error.LOOKUP_ERROR));
checkItemIsClass(item);
analyseItem(item, env);
then
();
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- NFSCodeDependency.analyseClass failed for " +
Absyn.pathString(inClassName) + " in " +
NFSCodeEnv.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 Boolean inBuiltinPossible "True if the path can be a builtin, otherwise false.";
input SourceInfo inInfo;
input Option<Error.Message> inErrorType;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := matchcontinue(inPath, inEnv, inBuiltinPossible, inInfo, inErrorType)
local
Item item;
Env env;
String name_str, env_str;
Error.Message error_id;
case (_, _, _, _, _)
equation
(item, env) = lookupClass2(inPath, inEnv, inBuiltinPossible, inInfo, inErrorType);
(item, env, _) = NFSCodeEnv.resolveRedeclaredItem(item, env);
then
(item, env);
case (_, _, _, _, SOME(error_id))
equation
name_str = Absyn.pathString(inPath);
env_str = NFSCodeEnv.getEnvName(inEnv);
Error.addSourceMessage(error_id, {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 Boolean inBuiltinPossible;
input SourceInfo inInfo;
input Option<Error.Message> inErrorType;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := match(inPath, inEnv, inBuiltinPossible, inInfo, inErrorType)
local
Item item;
Env env;
String id;
Absyn.Path rest_path;
case (Absyn.IDENT(), _, true, _, _)
equation
(item, _, env) =
NFSCodeLookup.lookupNameSilent(inPath, inEnv, inInfo);
then
(item, env);
case (Absyn.IDENT(), _, false, _, _)
equation
(item, _, env) =
NFSCodeLookup.lookupNameSilentNoBuiltin(inPath, inEnv, inInfo);
then
(item, env);
// Special case for the baseclass of a class extends. Should be looked up
// among the inherited elements of the enclosing class.
case (Absyn.QUALIFIED(name = "$ce", path = Absyn.IDENT(name = id)), _ :: env, _, _, _)
equation
(item, env) = NFSCodeLookup.lookupInheritedName(id, env);
then
(item, env);
case (Absyn.QUALIFIED(name = id, path = rest_path), _, _, _, _)
equation
(item, _, env) =
NFSCodeLookup.lookupNameSilent(Absyn.IDENT(id), inEnv, inInfo);
(item, env, _) = NFSCodeEnv.resolveRedeclaredItem(item, env);
analyseItem(item, env);
(item, env) = lookupNameInItem(rest_path, item, env, inErrorType);
then
(item, env);
case (Absyn.FULLYQUALIFIED(path = rest_path), _, _, _, _)
equation
env = NFSCodeEnv.getEnvTopScope(inEnv);
(item, env) = lookupClass2(rest_path, env, false, inInfo, inErrorType);
then
(item, env);
end match;
end lookupClass2;
protected function lookupNameInItem
input Absyn.Path inName;
input Item inItem;
input Env inEnv;
input Option<Error.Message> inErrorType;
output Item outItem;
output Env outEnv;
algorithm
(outItem, outEnv) := match(inName, inItem, inEnv, inErrorType)
local
Absyn.Path type_path;
SCode.Mod mods;
SourceInfo info;
Env env, type_env;
NFSCodeEnv.Frame class_env;
list<NFSCodeEnv.Redeclaration> redeclares;
Item item;
case (_, _, {}, _) then (inItem, inEnv);
case (_, NFSCodeEnv.VAR(var = SCode.COMPONENT(typeSpec =
Absyn.TPATH(path = type_path), modifications = mods, info = info)), _, _)
equation
(item, type_env) = lookupClass(type_path, inEnv, true, info, inErrorType);
true = NFSCodeEnv.isClassItem(item);
redeclares = NFSCodeFlattenRedeclare.extractRedeclaresFromModifier(mods);
(item, type_env, _) = NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(
redeclares, item, type_env, inEnv, NFInstPrefix.emptyPrefix);
(item, env) = lookupNameInItem(inName, item, type_env, inErrorType);
then
(item, env);
case (_, NFSCodeEnv.CLASS(cls = SCode.CLASS(info = info), env = {class_env}), _, _)
equation
env = NFSCodeEnv.enterFrame(class_env, inEnv);
(item, env) = lookupClass(inName, env, false, info, inErrorType);
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;
SourceInfo info;
case NFSCodeEnv.CLASS() then ();
// We found a component instead, which might happen if the user tries to use
// a variable name as a type.
case NFSCodeEnv.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
// Check if the item is already marked as used, then we can stop here.
if NFSCodeEnv.isItemUsed(inItem) then
return;
end if;
_ := match(inItem, inEnv)
local
SCode.ClassDef cdef;
NFSCodeEnv.Frame cls_env;
Env env;
SourceInfo info;
SCode.Restriction res;
SCode.Element cls;
SCode.Comment cmt;
// A component, mark it and it's environment as used.
case (NFSCodeEnv.VAR(), env)
equation
markItemAsUsed(inItem, env);
then
();
// A basic type, nothing to be done.
case (NFSCodeEnv.CLASS(classType = NFSCodeEnv.BASIC_TYPE()), _) then ();
// A normal class, mark it and it's environment as used, and recursively
// analyse its contents.
case (NFSCodeEnv.CLASS(cls = cls as SCode.CLASS(classDef = cdef,
restriction = res, info = info, cmt = cmt), env = {cls_env}), env)
equation
markItemAsUsed(inItem, env);
env = NFSCodeEnv.enterFrame(cls_env, env);
if (if cls.name=="cardinality" then match inEnv case {NFSCodeEnv.FRAME(name=NONE())} then true; else false; end match else false) then
System.setUsesCardinality(true);
end if;
analyseClassDef(cdef, res, env, false, info);
analyseMetaType(res, env, info);
analyseComment(cmt, env, info);
_ :: env = env;
analyseRedeclaredClass(cls, env);
then
();
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- NFSCodeDependency.analyseItem failed on " +
NFSCodeEnv.getItemName(inItem) + " in " +
NFSCodeEnv.getEnvName(inEnv));
then
fail();
end match;
end analyseItem;
protected function analyseItemIfRedeclares
"Analyses an item again if there were some redeclares applied to the environment"
input NFSCodeFlattenRedeclare.Replacements inRepls;
input Item inItem;
input Env inEnv;
algorithm
_ := matchcontinue(inRepls, inItem, inEnv)
local
Item i;
NFSCodeEnv.Frame cls_frm;
Env env;
// no replacements happened on the environemnt! do nothing
case ({}, _, _) then ();
case (_, _, _)
equation
_::env = inEnv;
//i = NFSCodeEnv.setItemEnv(inItem, {cls_frm});
analyseItemNoStopOnUsed(inItem, env);
then ();
end matchcontinue;
end analyseItemIfRedeclares;
protected function analyseItemNoStopOnUsed
"Analyses an item."
input Item inItem;
input Env inEnv;
algorithm
_ := matchcontinue(inItem, inEnv)
local
SCode.ClassDef cdef;
NFSCodeEnv.Frame cls_env;
Env env;
SourceInfo info;
SCode.Restriction res;
SCode.Element cls;
SCode.Comment cmt;
// A component, mark it and it's environment as used.
case (NFSCodeEnv.VAR(), env)
equation
markItemAsUsed(inItem, env);
then
();
// A basic type, nothing to be done.
case (NFSCodeEnv.CLASS(classType = NFSCodeEnv.BASIC_TYPE()), _) then ();
// A normal class, mark it and it's environment as used, and recursively
// analyse it's contents.
case (NFSCodeEnv.CLASS(cls = cls as SCode.CLASS(classDef = cdef,
restriction = res, info = info, cmt = cmt), env = {cls_env}), env)
equation
markItemAsUsed(inItem, env);
env = NFSCodeEnv.enterFrame(cls_env, env);
analyseClassDef(cdef, res, env, false, info);
analyseMetaType(res, env, info);
analyseComment(cmt, env, info);
_ :: env = env;
analyseRedeclaredClass(cls, env);
then
();
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- NFSCodeDependency.analyseItemNoStopOnUsed failed on " +
NFSCodeEnv.getItemName(inItem) + " in " +
NFSCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end analyseItemNoStopOnUsed;
protected function markItemAsUsed
"Marks an item and it's environment as used."
input Item inItem;
input Env inEnv;
algorithm
_ := match(inItem, inEnv)
local
NFSCodeEnv.Frame cls_env;
Mutable<Boolean> is_used;
String name;
case (NFSCodeEnv.VAR(isUsed = SOME(is_used)), _)
equation
Mutable.update(is_used, true);
markEnvAsUsed(inEnv);
then
();
case (NFSCodeEnv.VAR(isUsed = NONE()), _) then ();
case (NFSCodeEnv.CLASS(env = {cls_env}, cls = SCode.CLASS()), _)
equation
markFrameAsUsed(cls_env);
markEnvAsUsed(inEnv);
then
();
end match;
end markItemAsUsed;
protected function markFrameAsUsed
"Marks a single frame as used."
input NFSCodeEnv.Frame inFrame;
algorithm
_ := match(inFrame)
local
Mutable<Boolean> is_used;
case NFSCodeEnv.FRAME(isUsed = SOME(is_used))
equation
Mutable.update(is_used, true);
then
();
else ();
end match;
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
Mutable<Boolean> is_used;
Env rest_env;
NFSCodeEnv.Frame f;
case ((f as NFSCodeEnv.FRAME(isUsed = SOME(is_used))) :: rest_env)
equation
false = Mutable.access(is_used);
markEnvAsUsed2(f, rest_env);
Mutable.update(is_used, true);
markEnvAsUsed(rest_env);
then
();
else ();
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 NFSCodeEnv.Frame inFrame;
input NFSCodeEnv.Env inEnv;
algorithm
_ := match(inFrame, inEnv)
local
String name;
case (NFSCodeEnv.FRAME(frameType = NFSCodeEnv.IMPLICIT_SCOPE()), _) then ();
case (NFSCodeEnv.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 SourceInfo inInfo;
algorithm
_ := matchcontinue(inClassDef, inRestriction, inEnv, inInModifierScope, inInfo)
local
list<SCode.Element> el;
Absyn.Ident bc;
SCode.Mod mods;
Absyn.TypeSpec ty;
list<SCode.Equation> nel, iel;
list<SCode.AlgorithmSection> nal, ial;
SCode.Comment cmt;
list<SCode.Annotation> annl;
Option<SCode.ExternalDecl> ext_decl;
Env ty_env, env, nore_env;
Item ty_item;
SCode.Attributes attr;
list<Absyn.Path> paths;
list<NFSCodeEnv.Redeclaration> redecls;
NFSCodeFlattenRedeclare.Replacements repls;
// 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), _, _, _, _)
equation
analyseElements(el, 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);
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(), _, _, _, _)
equation
Error.addSourceMessage(Error.INTERNAL_ERROR,
{"NFSCodeDependency.analyseClassDef failed on CLASS_EXTENDS"}, inInfo);
then
fail();
// A derived class definition.
case (SCode.DERIVED(typeSpec = ty, modifications = mods),
_, _ :: env, _, _)
equation
env = if inInModifierScope then inEnv else env;
nore_env = NFSCodeEnv.removeRedeclaresFromLocalScope(env);
analyseTypeSpec(ty, nore_env, inInfo);
(ty_item, _, ty_env) = NFSCodeLookup.lookupTypeSpec(ty, env, inInfo);
(ty_item, ty_env, _) = NFSCodeEnv.resolveRedeclaredItem(ty_item, ty_env);
ty_env = NFSCodeEnv.mergeItemEnv(ty_item, ty_env);
redecls = NFSCodeFlattenRedeclare.extractRedeclaresFromModifier(mods);
(ty_item, ty_env, repls) =
NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(redecls, ty_item, ty_env, inEnv, NFInstPrefix.emptyPrefix);
analyseItemIfRedeclares(repls, ty_item, ty_env);
analyseModifier(mods, inEnv, ty_env, inInfo);
then
();
// Other cases which doesn't need to be analysed.
case (SCode.ENUMERATION(), _, _, _, _) then ();
case (SCode.OVERLOAD(pathLst = paths), _, _, _, _)
algorithm
if not Config.synchronousFeaturesAllowed() and Absyn.pathFirstIdent(listHead(paths)) == "OMC_NO_CLOCK" then
List.map2_0({listHead(paths)},analyseClass,inEnv,inInfo);
else
List.map2_0(paths,analyseClass,inEnv,inInfo);
end if;
then ();
case (SCode.PDER(), _, _, _, _) 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 SourceInfo inInfo;
protected
list<SCode.Element> el;
list<String> el_names;
algorithm
// Remove all 'extends ExternalObject'.
el := List.filterOnTrue(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.filterMap(el, elementName);
checkExternalObject(el_names, inEnv, inInfo);
end isExternalObject;
protected function elementName
input SCode.Element inElement;
output String outString;
algorithm
outString := match(inElement)
local
String name;
Absyn.Path bc;
case SCode.COMPONENT(name = name) then name;
case SCode.CLASS(name = name) then name;
case SCode.DEFINEUNIT(name = name) then name;
case SCode.EXTENDS(baseClassPath = bc)
equation
name = Absyn.pathString(bc);
name = "extends " + name;
then
name;
end match;
end elementName;
protected function isNotExternalObject
"False on 'extends ExternalObject', otherwise true."
input SCode.Element inElement;
output Boolean b;
algorithm
b := match(inElement)
case SCode.EXTENDS(baseClassPath = Absyn.IDENT("ExternalObject")) then false;
else true;
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 SourceInfo 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 = NFSCodeEnv.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 SourceInfo 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 SourceInfo 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 ();
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(), _)
equation
false = SCode.isElementRedeclare(inClass);
then ();
case (SCode.CLASS(), _)
equation
item = NFSCodeEnv.CLASS(inClass, NFSCodeEnv.emptyEnv, NFSCodeEnv.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;
SourceInfo info;
case (NFSCodeEnv.CLASS(cls=SCode.CLASS( info = info)), _)
equation
(item, env) = NFSCodeLookup.lookupRedeclaredClassByItem(inItem, inEnv, info);
analyseItem(item, env);
then
();
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- NFSCodeDependency.analyseRedeclaredClass2 failed for " +
NFSCodeEnv.getItemName(inItem) + " in " +
NFSCodeEnv.getEnvName(inEnv));
then
fail();
end matchcontinue;
end analyseRedeclaredClass2;
protected function analyseElements
input list<SCode.Element> inElements;
input Env inEnv;
input SCode.Restriction inClassRestriction;
protected
list<Extends> exts;
algorithm
exts := NFSCodeEnv.getEnvExtendsFromTable(inEnv);
analyseElements2(inElements, inEnv, exts, inClassRestriction);
end analyseElements;
protected function analyseElements2
input list<SCode.Element> inElements;
input Env inEnv;
input list<Extends> inExtends;
input SCode.Restriction inClassRestriction;
algorithm
_ := match(inElements, inEnv, inExtends, inClassRestriction)
local
SCode.Element el;
list<SCode.Element> rest_el;
list<Extends> exts;
case (el :: rest_el, _, _, _)
equation
exts = analyseElement(el, inEnv, inExtends, inClassRestriction);
analyseElements2(rest_el, inEnv, exts, inClassRestriction);
then
();
case ({}, _, _, _) then ();
end match;
end analyseElements2;
protected function analyseElement
"Analyses an element."
input SCode.Element inElement;
input Env inEnv;
input list<Extends> inExtends;
input SCode.Restriction inClassRestriction;
output list<Extends> outExtends;
algorithm
outExtends := match(inElement, inEnv, inExtends, inClassRestriction)
local
Absyn.Path bc, bc2;
SCode.Mod mods;
Absyn.TypeSpec ty;
SourceInfo info;
SCode.Attributes attr;
Option<Absyn.Exp> cond_exp;
Item ty_item;
Env ty_env;
SCode.Ident name;
SCode.Prefixes prefixes;
SCode.Restriction res;
String str;
list<Extends> exts;
list<NFSCodeEnv.Redeclaration> redecls;
NFSCodeFlattenRedeclare.Replacements repls;
// 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(modifications = mods, info = info), _,
NFSCodeEnv.EXTENDS(baseClass = bc) :: exts, _)
equation
//print("bc = " + Absyn.pathString(bc) + "\n");
//print("bc2 = " + Absyn.pathString(bc2) + "\n");
(ty_item, _, ty_env) =
NFSCodeLookup.lookupBaseClassName(bc, inEnv, info);
analyseExtends(bc, inEnv, info);
ty_env = NFSCodeEnv.mergeItemEnv(ty_item, ty_env);
analyseModifier(mods, inEnv, ty_env, info);
then
exts;
// A component.
case (SCode.COMPONENT(name = name, attributes = attr, typeSpec = ty,
modifications = mods, condition = cond_exp, prefixes = prefixes, info = info), _, _, _)
equation
// *always* keep constants and parameters!
// markAsUsedOnConstant(name, attr, inEnv, info);
markAsUsedOnRestriction(name, inClassRestriction, inEnv, info);
analyseAttributes(attr, inEnv, info);
analyseTypeSpec(ty, inEnv, info);
(ty_item, _, ty_env) = NFSCodeLookup.lookupTypeSpec(ty, inEnv, info);
(ty_item, ty_env, _) = NFSCodeEnv.resolveRedeclaredItem(ty_item, ty_env);
ty_env = NFSCodeEnv.mergeItemEnv(ty_item, ty_env);
NFSCodeCheck.checkRecursiveComponentDeclaration(name, info, ty_env, ty_item, inEnv);
redecls = NFSCodeFlattenRedeclare.extractRedeclaresFromModifier(mods);
(ty_item, ty_env,_) =
NFSCodeFlattenRedeclare.replaceRedeclaredElementsInEnv(redecls, ty_item, ty_env, inEnv, NFInstPrefix.emptyPrefix);
// analyseItemIfRedeclares(repls, ty_item, ty_env);
analyseModifier(mods, inEnv, ty_env, info);
analyseOptExp(cond_exp, inEnv, info);
analyseConstrainClass(SCode.replaceableOptConstraint(SCode.prefixesReplaceable(prefixes)), inEnv, info);
then
inExtends;
//operators in operator record might be used later.
case (SCode.CLASS(name = name, restriction=SCode.R_OPERATOR(), info = info), _, _, SCode.R_RECORD(true))
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
//operators in any other class type are error.
case (SCode.CLASS(name = name, restriction=SCode.R_OPERATOR(), info = info), _, _, _)
equation
str = SCodeDump.restrString(inClassRestriction);
Error.addSourceMessage(Error.OPERATOR_FUNCTION_NOT_EXPECTED, {name, str}, info);
then fail();
//operator functions in operator record might be used later.
case (SCode.CLASS(name = name, restriction=SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION()), info = info), _, _, SCode.R_RECORD(true))
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
//operators functions in any other class type are error.
case (SCode.CLASS(name = name, restriction=SCode.R_FUNCTION(SCode.FR_OPERATOR_FUNCTION()), info = info), _, _, _)
equation
str = SCodeDump.restrString(inClassRestriction);
Error.addSourceMessage(Error.OPERATOR_FUNCTION_NOT_EXPECTED, {name, str}, info);
then
fail();
//functions in operator might be used later.
case (SCode.CLASS(name = name, restriction=res, info = info), _, _, SCode.R_OPERATOR())
equation
// Allowing external functions to be used operator functions
true = SCode.isFunctionOrExtFunctionRestriction(res);
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
//operators should only contain function definitions
case (SCode.CLASS(name = name, restriction = res, info = info), _, _, SCode.R_OPERATOR())
equation
false = SCode.isFunctionOrExtFunctionRestriction(res);
str = SCodeDump.restrString(res);
Error.addSourceMessage(Error.OPERATOR_FUNCTION_EXPECTED, {name, str}, info);
then
fail();
// 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
inExtends;
case (SCode.CLASS(name = name, info = info, classDef=SCode.CLASS_EXTENDS()), _, _, _)
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
// inner/innerouter classes may not be explicitly used but might be needed anyway
case (SCode.CLASS(name = name, prefixes = SCode.PREFIXES(innerOuter = Absyn.INNER()), info = info), _, _, _)
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
// inner/innerouter classes may not be explicitly used but might be needed anyway
case (SCode.CLASS(name = name, prefixes = SCode.PREFIXES(innerOuter = Absyn.INNER_OUTER()), info = info), _, _, _)
equation
analyseClass(Absyn.IDENT(name), inEnv, info);
then
inExtends;
else inExtends;
end match;
end analyseElement;
protected function markAsUsedOnConstant
input SCode.Ident inName;
input SCode.Attributes inAttr;
input Env inEnv;
input SourceInfo inInfo;
algorithm
_ := matchcontinue(inName, inAttr, inEnv, inInfo)