/
NFApi.mo
2124 lines (1821 loc) · 66.3 KB
/
NFApi.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 NFApi
import Absyn;
import AbsynUtil;
import SCode;
import DAE;
import NFModifier.Modifier;
protected
import Inst = NFInst;
import NFBinding.Binding;
import NFComponent.Component;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFClass.Class;
import NFInstNode.InstNode;
import NFInstNode.InstNodeType;
import NFModifier.ModifierScope;
import Equation = NFEquation;
import NFType.Type;
import Subscript = NFSubscript;
import Connection = NFConnection;
import InstContext = NFInstContext;
import Absyn.Path;
import AbsynToSCode;
import Config;
import ConvertDAE = NFConvertDAE;
import DAEUtil;
import Dump;
import EvalConstants = NFEvalConstants;
import ErrorExt;
import ExecStat.{execStat,execStatReset};
import FBuiltin;
import Flags;
import FlagsUtil;
import FlatModel = NFFlatModel;
import Flatten = NFFlatten;
import Global;
import InteractiveUtil;
import JSON;
import List;
import Lookup = NFLookup;
import MetaModelica.Dangerous;
import NFCall.Call;
import Ceval = NFCeval;
import NFClassTree.ClassTree;
import NFFlatten.FunctionTree;
import NFPrefixes.{Variability};
import NFSections.Sections;
import Package = NFPackage;
import Parser;
import Prefixes = NFPrefixes;
import Restriction = NFRestriction;
import Scalarize = NFScalarize;
import SimplifyExp = NFSimplifyExp;
import SimplifyModel = NFSimplifyModel;
import SymbolTable;
import Typing = NFTyping;
import UnitCheck = NFUnitCheck;
import Util;
import Variable = NFVariable;
import VerifyModel = NFVerifyModel;
import SCodeUtil;
import ElementSource;
import InstSettings = NFInst.InstSettings;
import Testsuite;
import MetaModelica.Dangerous.listReverseInPlace;
public
function evaluateAnnotation
"Instantiates the annotation class, gets the DAE and populates the annotation result"
input Absyn.Program absynProgram;
input Absyn.Path classPath;
input Absyn.Annotation inAnnotation;
output String outString = "";
protected
Boolean b, s;
algorithm
b := FlagsUtil.set(Flags.SCODE_INST, true);
s := FlagsUtil.set(Flags.NF_SCALARIZE, true); // #5689
try
outString := evaluateAnnotation_dispatch(absynProgram, classPath, inAnnotation);
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
else
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
fail();
end try;
end evaluateAnnotation;
protected
function evaluateAnnotation_dispatch
"Instantiates the annotation class, gets the DAE and populates the annotation result"
input Absyn.Program absynProgram;
input Absyn.Path classPath;
input Absyn.Annotation inAnnotation;
input Boolean addAnnotationName = false;
output String outString = "";
protected
InstNode top, cls, inst_cls, anncls, inst_anncls;
String name, clsName, annName, str;
FlatModel flat_model;
FunctionTree funcs;
SCode.Program program;
DAE.FunctionTree daeFuncs;
Absyn.Path fullClassPath;
list<Absyn.ElementArg> el = {};
list<String> stringLst = {};
Absyn.Exp absynExp;
Expression exp, save;
DAE.Exp dexp;
list<Absyn.ComponentItem> items;
Option<Absyn.ConstrainClass> cc;
SourceInfo info;
list<Absyn.ElementArg> mod, stripped_mod, graphics_mod;
Absyn.EqMod eqmod;
SCode.Mod smod;
DAE.DAElist dae;
Type ty;
Variability var;
InstContext.Type context;
algorithm
stringLst := {};
Absyn.ANNOTATION(el) := inAnnotation;
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.ANNOTATION);
for e in listReverse(el) loop
e := AbsynUtil.createChoiceArray(e);
str := matchcontinue e
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD({}, eqmod as Absyn.EQMOD(absynExp))),
info = info)
algorithm
// no need for the class if there are no crefs
if AbsynUtil.onlyLiteralsInEqMod(eqmod) then
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
else
// run the front-end front
(program, name, inst_cls) := frontEndFront(absynProgram, classPath);
end if;
exp := NFInst.instExp(absynExp, inst_cls, context, info);
(exp, ty, var) := Typing.typeExp(exp, context, info);
// exp := NFCeval.evalExp(exp);
exp := SimplifyExp.simplify(exp);
str := Expression.toString(exp);
then
stringAppendList({annName, "=", str});
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD(mod, Absyn.NOMOD())),
info = info)
algorithm
// no need for the class if there are no crefs
if AbsynUtil.onlyLiteralsInAnnotationMod(mod) then
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
else
// run the front-end front
(program, name, inst_cls) := frontEndFront(absynProgram, classPath);
end if;
(stripped_mod, graphics_mod) := AbsynUtil.stripGraphicsAndInteractionModification(mod);
smod := AbsynToSCode.translateMod(SOME(Absyn.CLASSMOD(stripped_mod, Absyn.NOMOD())), SCode.NOT_FINAL(), SCode.NOT_EACH(), info);
anncls := Lookup.lookupClassName(Absyn.IDENT(annName), inst_cls, context, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.expand(anncls);
inst_anncls := NFInst.instClass(inst_anncls, Modifier.create(smod, annName, ModifierScope.CLASS(annName), inst_cls), NFAttributes.DEFAULT_ATTR, true, 0, inst_cls, context);
// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
NFInst.instExpressions(inst_anncls, context = context);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName, false);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
if (listMember(annName, {"Icon", "Diagram", "choices"})) and not listEmpty(graphics_mod) then
try
{Absyn.MODIFICATION(modification = SOME(Absyn.CLASSMOD(eqMod = Absyn.EQMOD(exp = absynExp))))} := graphics_mod;
exp := NFInst.instExp(absynExp, inst_cls, context, info);
(exp, ty, var) := Typing.typeExp(exp, context, info);
save := exp;
try
exp := NFCeval.evalExp(save);
else
exp := EvalConstants.evaluateExp(save, info);
end try;
exp := SimplifyExp.simplify(exp);
str := str + "," + Expression.toString(exp);
else
// just don't fail!
end try;
end if;
then
if addAnnotationName
then stringAppendList({annName, "(", str, ")"})
else str;
case Absyn.MODIFICATION(path = Absyn.IDENT(annName), modification = NONE(), info = info)
algorithm
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
anncls := Lookup.lookupClassName(Absyn.IDENT(annName), inst_cls, context, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.instantiate(anncls, context = context);
// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
NFInst.instExpressions(inst_anncls, context = context);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName, false);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
if addAnnotationName
then stringAppendList({annName, "(", str, ")"})
else str;
case Absyn.MODIFICATION(path = Absyn.IDENT(annName), info = info)
algorithm
str := "error evaluating: annotation(" + Dump.unparseElementArgStr(e) + ")";
str := Util.escapeQuotes(str);
then
stringAppendList({annName, "(\"", str, "\")"});
end matchcontinue;
stringLst := str :: stringLst;
end for;
outString := stringDelimitList(stringLst, ", ");
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.evaluateAnnotation_dispatch("+ AbsynUtil.pathString(classPath) + " annotation(" + stringDelimitList(List.map(el, Dump.unparseElementArgStr), ", ") + ")");
end if;
end evaluateAnnotation_dispatch;
public
function evaluateAnnotations
"Instantiates the annotation class, gets the DAE and populates the annotation result"
input Absyn.Program absynProgram;
input Absyn.Path classPath;
input list<Absyn.Element> inElements;
output list<String> outStringLst = {};
protected
Boolean b, s;
algorithm
b := FlagsUtil.set(Flags.SCODE_INST, true);
s := FlagsUtil.set(Flags.NF_SCALARIZE, true); // #5689
try
outStringLst := evaluateAnnotations_dispatch(absynProgram, classPath, inElements);
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
else
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
fail();
end try;
end evaluateAnnotations;
protected
function evaluateAnnotations_dispatch
"Instantiates the annotation class, gets the DAE and populates the annotation result"
input Absyn.Program absynProgram;
input Absyn.Path classPath;
input list<Absyn.Element> inElements;
output list<String> outStringLst = {};
protected
InstNode top, cls, inst_cls, anncls, inst_anncls;
String name, clsName, annName, str;
FlatModel flat_model;
FunctionTree funcs;
SCode.Program program;
DAE.FunctionTree daeFuncs;
Absyn.Path fullClassPath;
list<list<Absyn.ElementArg>> elArgs = {}, el = {};
list<String> stringLst = {};
Absyn.Exp absynExp;
Expression exp;
DAE.Exp dexp;
list<Absyn.ComponentItem> items;
Option<Absyn.ConstrainClass> cc;
SourceInfo info;
list<Absyn.ElementArg> mod, anns;
Absyn.EqMod eqmod;
SCode.Mod smod;
DAE.DAElist dae;
Type ty;
Variability var;
Option<Absyn.Comment> cmt;
InstContext.Type context;
algorithm
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.ANNOTATION);
// handle the annotations
for i in inElements loop
elArgs := matchcontinue i
case Absyn.ELEMENT(specification = Absyn.COMPONENTS(components = items), constrainClass = cc)
algorithm
el := AbsynUtil.getAnnotationsFromItems(items, AbsynUtil.getAnnotationsFromConstraintClass(cc));
then
listAppend(el, elArgs);
case Absyn.ELEMENT(specification = Absyn.COMPONENTS())
then {}::elArgs;
case Absyn.ELEMENT(specification = Absyn.CLASSDEF(
class_ = Absyn.CLASS(body = Absyn.DERIVED(comment = cmt))),
constrainClass = cc)
algorithm
anns := match cmt
case SOME(Absyn.COMMENT(annotation_ = SOME(Absyn.ANNOTATION(anns))))
then anns;
else {};
end match;
then
listAppend(anns, AbsynUtil.getAnnotationsFromConstraintClass(cc))::elArgs;
case Absyn.ELEMENT(specification = Absyn.COMPONENTS())
then {} :: elArgs;
case Absyn.ELEMENT(specification = Absyn.CLASSDEF(class_ = Absyn.CLASS(body = Absyn.DERIVED())))
then {} :: elArgs;
else elArgs;
end matchcontinue;
end for;
for l in elArgs loop
stringLst := {};
for e in listReverse(l) loop
str := evaluateAnnotation_dispatch(absynProgram, classPath, Absyn.ANNOTATION({e}), true);
/* try to use evaluateAnnotation_dispatch instead
e := AbsynUtil.createChoiceArray(e);
str := matchcontinue e
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD({}, eqmod as Absyn.EQMOD(absynExp))),
info = info)
algorithm
// no need for the class if there are no crefs
if AbsynUtil.onlyLiteralsInEqMod(eqmod) then
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
else
// run the front-end front
(program, name, inst_cls) := frontEndFront(absynProgram, classPath);
end if;
exp := NFInst.instExp(absynExp, inst_cls, context, info);
(exp, ty, var) := Typing.typeExp(exp, context, info);
// exp := NFCeval.evalExp(exp);
exp := SimplifyExp.simplify(exp);
str := Expression.toString(exp);
then
stringAppendList({annName, "=", str});
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD(mod, Absyn.NOMOD())),
info = info)
algorithm
// no need for the class if there are no crefs
if AbsynUtil.onlyLiteralsInAnnotationMod(mod) then
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
else
// run the front-end front
(program, name, inst_cls) := frontEndFront(absynProgram, classPath);
end if;
smod := AbsynToSCode.translateMod(SOME(Absyn.CLASSMOD(mod, Absyn.NOMOD())), SCode.NOT_FINAL(), SCode.NOT_EACH(), info);
anncls := Lookup.lookupClassName(Absyn.IDENT(annName), inst_cls, context, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.expand(anncls);
inst_anncls := NFInst.instClass(inst_anncls, Modifier.create(smod, annName, ModifierScope.CLASS(annName), inst_cls), NFComponent.DEFAULT_ATTR, true, 0, inst_cls, context);
// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
NFInst.instExpressions(inst_anncls, context = context);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName, false);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
stringAppendList({annName, "(", str, ")"});
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD(_, _)))
then stringAppendList({annName, "(error)"});
case Absyn.MODIFICATION(path = Absyn.IDENT(annName), modification = NONE(), info = info)
algorithm
(program, top) := mkTop(absynProgram, annName);
inst_cls := top;
anncls := Lookup.lookupClassName(Absyn.IDENT(annName), inst_cls, context, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.instantiate(anncls, context = context);
// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
NFInst.instExpressions(inst_anncls, context = context);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName, false);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
stringAppendList({annName, "(", str, ")"});
case Absyn.MODIFICATION(path = Absyn.IDENT(annName), modification = NONE(), info = info)
then stringAppendList({annName, "(error)"});
end matchcontinue;
*/
stringLst := str :: stringLst;
end for;
str := stringDelimitList(stringLst, ", ");
outStringLst := stringAppendList({"{", str, "}"}) :: outStringLst;
end for;
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.evaluateAnnotations_dispatch("+ AbsynUtil.pathString(classPath) + " annotation(" + stringDelimitList(List.map(List.flatten(elArgs), Dump.unparseElementArgStr), ", ") + ")");
end if;
end evaluateAnnotations_dispatch;
public
function mkFullyQual
input Absyn.Program absynProgram;
input Absyn.Path classPath;
input Absyn.Path pathToQualify;
output Absyn.Path qualPath = pathToQualify;
protected
InstNode top, expanded_cls, cls;
SCode.Program program;
String name, id1, id2;
Boolean b, s;
InstContext.Type context;
algorithm
// do some quick checks
// classPath is already fully qualified
// check if the paths start with the same id and the second path is qualified
_ := match (classPath, pathToQualify)
case (Absyn.QUALIFIED(id1, _), Absyn.QUALIFIED(id2, _)) guard id1 == id2
algorithm
return;
then ();
else ();
end match;
// else, do the hard stuff!
b := FlagsUtil.set(Flags.SCODE_INST, true);
s := FlagsUtil.set(Flags.NF_SCALARIZE, true); // #5689
try
if not Flags.isSet(Flags.NF_API_NOISE) then
ErrorExt.setCheckpoint("NFApi.mkFullyQual");
end if;
// run the front-end front
(program, name, expanded_cls) := frontEndLookup(absynProgram, classPath);
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.FAST_LOOKUP);
// if is derived qualify in the parent
if InstNode.isDerivedClass(expanded_cls) then
cls := Lookup.lookupClassName(pathToQualify, InstNode.classParent(expanded_cls), context, AbsynUtil.dummyInfo, checkAccessViolations = false);
else // qualify in the class
cls := Lookup.lookupClassName(pathToQualify, expanded_cls, context, AbsynUtil.dummyInfo, checkAccessViolations = false);
end if;
qualPath := InstNode.fullPath(cls);
if not Flags.isSet(Flags.NF_API_NOISE) then
ErrorExt.rollBack("NFApi.mkFullyQual");
end if;
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
else
// do not fail, just return the Absyn path
qualPath := pathToQualify;
if not Flags.isSet(Flags.NF_API_NOISE) then
ErrorExt.rollBack("NFApi.mkFullyQual");
end if;
FlagsUtil.set(Flags.SCODE_INST, b);
FlagsUtil.set(Flags.NF_SCALARIZE, s);
end try;
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.mkFullyQual(" + AbsynUtil.pathString(classPath) + ", " + AbsynUtil.pathString(pathToQualify) + ") -> " + AbsynUtil.pathString(qualPath));
end if;
end mkFullyQual;
protected
function frontEndFront
input Absyn.Program absynProgram;
input Absyn.Path classPath;
output SCode.Program program;
output String name;
output InstNode inst_cls;
protected
list<tuple<tuple<Absyn.Program, Absyn.Path>, tuple<SCode.Program, String, InstNode>>> cache;
algorithm
cache := getGlobalRoot(Global.instNFInstCacheIndex);
if not listEmpty(cache) then
for i in cache loop
if referenceEq(absynProgram, Util.tuple21(Util.tuple21(i))) then
if AbsynUtil.pathEqual(classPath, Util.tuple22(Util.tuple21(i))) then
(program, name, inst_cls) := Util.tuple22(i);
return;
end if;
// program changed, wipe the cache!
cache := {};
setGlobalRoot(Global.instNFInstCacheIndex, cache);
break;
end if;
end for;
end if;
(program, name, inst_cls) := frontEndFront_dispatch(absynProgram, classPath);
if listLength(cache) > 100 then
// trim it down, keep 10
cache := List.firstN(cache, 10);
end if;
cache := ((absynProgram,classPath), (program, name, inst_cls))::cache;
setGlobalRoot(Global.instNFInstCacheIndex, cache);
end frontEndFront;
protected
function mkTop
input Absyn.Program absynProgram;
input String name;
output SCode.Program program;
output InstNode top;
protected
SCode.Program scode_builtin, graphicProgramSCode;
Absyn.Program placementProgram;
InstNode cls;
list<tuple<Absyn.Program, tuple<SCode.Program, InstNode>>> cache;
Boolean update = true;
algorithm
cache := getGlobalRoot(Global.instNFNodeCacheIndex);
if not listEmpty(cache) then
// if absyn is the same, all fine, reuse
if referenceEq(absynProgram, Util.tuple21(listHead(cache))) then
(program, top) := Util.tuple22(listHead(cache));
InstNode.clearGeneratedInners(top);
update := false;
else
update := true;
cache := {};
setGlobalRoot(Global.instNFNodeCacheIndex, cache);
end if;
end if;
if update then
(_, scode_builtin) := FBuiltin.getInitialFunctions();
program := AbsynToSCode.translateAbsyn2SCode(absynProgram);
program := listAppend(scode_builtin, program);
placementProgram := InteractiveUtil.modelicaAnnotationProgram(Config.getAnnotationVersion());
graphicProgramSCode := AbsynToSCode.translateAbsyn2SCode(placementProgram);
Inst.resetGlobalFlags();
// Create a root node from the given top-level classes.
top := NFInst.makeTopNode(program, graphicProgramSCode);
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.mkTop("+ name +")");
end if;
cache := {(absynProgram, (program, top))};
setGlobalRoot(Global.instNFNodeCacheIndex, cache);
end if;
end mkTop;
protected
function frontEndFront_dispatch
input Absyn.Program absynProgram;
input Absyn.Path classPath;
output SCode.Program program;
output String name;
output InstNode inst_cls;
protected
InstNode top, cls;
algorithm
name := AbsynUtil.pathString(classPath);
(program, top) := mkTop(absynProgram, name);
// Look up the class to instantiate and mark it as the root class.
cls := Lookup.lookupClassName(classPath, top, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
cls := InstNode.setNodeType(InstNodeType.ROOT_CLASS(InstNode.EMPTY_NODE()), cls);
// Instantiate the class.
inst_cls := NFInst.instantiate(cls, context = NFInstContext.RELAXED);
NFInst.insertGeneratedInners(inst_cls, top, NFInstContext.RELAXED);
// Instantiate expressions (i.e. anything that can contains crefs, like
// bindings, dimensions, etc). This is done as a separate step after
// instantiation to make sure that lookup is able to find the correct nodes.
NFInst.instExpressions(inst_cls, context = NFInstContext.RELAXED);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_cls, Flags.isSet(Flags.EVAL_PARAM));
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.frontEndFront_dispatch(" + name + ")");
end if;
end frontEndFront_dispatch;
protected
function frontEndBack
input InstNode inst_cls;
input String name;
input Boolean scalarize = true;
output DAE.DAElist dae;
protected
InstNode top;
String clsName, annName, str;
FlatModel flat_model;
FunctionTree funcs;
SCode.Program scode_builtin, program, graphicProgramSCode;
SCode.Element scls, sAnnCls;
Absyn.Program placementProgram;
DAE.FunctionTree daeFuncs;
Absyn.Path fullClassPath;
list<list<Absyn.ElementArg>> elArgs, el = {};
list<String> stringLst = {};
Absyn.Exp absynExp;
Expression exp;
DAE.Exp dexp;
list<Absyn.ComponentItem> items;
Option<Absyn.ConstrainClass> cc;
SourceInfo info;
list<Absyn.ElementArg> mod;
SCode.Mod smod;
algorithm
// Type the class.
Typing.typeClass(inst_cls, NFInstContext.RELAXED);
// Flatten and simplify the model.
flat_model := Flatten.flatten(inst_cls, name);
flat_model := EvalConstants.evaluate(flat_model, NFInstContext.RELAXED);
flat_model := UnitCheck.checkUnits(flat_model);
flat_model := SimplifyModel.simplify(flat_model);
flat_model := Package.collectConstants(flat_model);
funcs := Flatten.collectFunctions(flat_model);
// Scalarize array components in the flat model.
if Flags.isSet(Flags.NF_SCALARIZE) /* and scalarize */ then
flat_model := Scalarize.scalarize(flat_model);
else
// Remove empty arrays from variables
flat_model.variables := List.filterOnFalse(flat_model.variables, Variable.isEmptyArray);
end if;
VerifyModel.verify(flat_model);
// Convert the flat model to a DAE.
(dae, daeFuncs) := ConvertDAE.convert(flat_model, funcs);
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.frontEndBack(" + AbsynUtil.pathString(InstNode.enclosingScopePath(inst_cls)) + ", name: " + name + ", scalarize: " + boolString(scalarize) + ")");
end if;
end frontEndBack;
protected
function frontEndLookup
input Absyn.Program absynProgram;
input Absyn.Path classPath;
output SCode.Program program;
output String name;
output InstNode expanded_cls;
protected
list<tuple<tuple<Absyn.Program, Absyn.Path>, tuple<SCode.Program, String, InstNode>>> cache;
algorithm
cache := getGlobalRoot(Global.instNFLookupCacheIndex);
if not listEmpty(cache) then
for i in cache loop
if referenceEq(absynProgram, Util.tuple21(Util.tuple21(i))) then
if AbsynUtil.pathEqual(classPath, Util.tuple22(Util.tuple21(i))) then
(program, name, expanded_cls) := Util.tuple22(i);
return;
end if;
// program changed, wipe the cache!
cache := {};
setGlobalRoot(Global.instNFLookupCacheIndex, cache);
break;
end if;
end for;
end if;
(program, name, expanded_cls) := frontEndLookup_dispatch(absynProgram, classPath);
if listLength(cache) > 100 then
// trim it down, keep 10
cache := List.firstN(cache, 10);
end if;
cache := ((absynProgram,classPath), (program, name, expanded_cls))::cache;
setGlobalRoot(Global.instNFLookupCacheIndex, cache);
end frontEndLookup;
protected
function frontEndLookup_dispatch
input Absyn.Program absynProgram;
input Absyn.Path classPath;
output SCode.Program program;
output String name;
output InstNode expanded_cls;
protected
SCode.Program scode_builtin, graphicProgramSCode;
Absyn.Program placementProgram;
InstNode top, cls;
list<tuple<Absyn.Program, tuple<SCode.Program, InstNode>>> cache;
Boolean update = true;
algorithm
name := AbsynUtil.pathString(classPath);
(program, top) := mkTop(absynProgram, name);
cls := Inst.lookupRootClass(classPath, top, InstContext.set(NFInstContext.RELAXED, NFInstContext.FAST_LOOKUP));
// Expand the class.
expanded_cls := NFInst.expand(cls);
if Flags.isSet(Flags.EXEC_STAT) then
execStat("NFApi.frontEndLookup_dispatch("+ name +")");
end if;
end frontEndLookup_dispatch;
public
function getInheritedClasses
input Absyn.Path classPath;
input Absyn.Program program;
output list<Absyn.Path> extendsPaths;
protected
InstNode cls_node;
Class cls;
algorithm
(_, _, cls_node) := frontEndLookup(program, classPath);
if not InstNode.isClass(cls_node) then
extendsPaths := {};
return;
end if;
cls := InstNode.getClass(cls_node);
extendsPaths := match cls
case Class.EXPANDED_DERIVED() then {InstNode.fullPath(cls.baseClass, true)};
else list(InstNode.fullPath(e, true) for e in ClassTree.getExtends(Class.classTree(cls)));
end match;
end getInheritedClasses;
uniontype InstanceTree
record COMPONENT
InstNode node;
InstanceTree cls;
end COMPONENT;
record CLASS
InstNode node;
list<InstanceTree> elements;
Boolean isExtends;
end CLASS;
record EMPTY
end EMPTY;
end InstanceTree;
function getModelInstance
input Absyn.Path classPath;
input String modifier;
input Boolean prettyPrint;
output Values.Value res;
protected
InstNode top, cls_node;
JSON json;
InstContext.Type context;
InstanceTree inst_tree;
InstSettings inst_settings;
String str;
Modifier mod;
algorithm
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.CLASS);
context := InstContext.set(context, NFInstContext.INSTANCE_API);
inst_settings := InstSettings.SETTINGS(mergeExtendsSections = false);
(_, top) := mkTop(SymbolTable.getAbsyn(), AbsynUtil.pathString(classPath));
mod := parseModifier(modifier, top);
cls_node := Inst.lookupRootClass(classPath, top, context);
cls_node := Inst.instantiateRootClass(cls_node, context, mod);
inst_tree := buildInstanceTree(cls_node);
Inst.instExpressions(cls_node, context = context, settings = inst_settings);
Inst.updateImplicitVariability(cls_node, Flags.isSet(Flags.EVAL_PARAM));
Typing.typeClassType(cls_node, NFBinding.EMPTY_BINDING, context, cls_node);
Typing.typeComponents(cls_node, context);
Typing.typeBindings(cls_node, context);
json := dumpJSONInstanceTree(inst_tree, cls_node);
res := Values.STRING(JSON.toString(json, prettyPrint));
end getModelInstance;
function getModelInstanceIcon
input Absyn.Path classPath;
input Boolean prettyPrint;
output Values.Value res;
protected
InstNode top, cls_node;
InstContext.Type context;
JSON json;
algorithm
context := InstContext.set(NFInstContext.RELAXED, NFInstContext.CLASS);
context := InstContext.set(context, NFInstContext.INSTANCE_API);
(_, top) := mkTop(SymbolTable.getAbsyn(), AbsynUtil.pathString(classPath));
cls_node := Inst.lookupRootClass(classPath, top, context);
cls_node := InstNode.resolveInner(cls_node);
json := dumpJSONInstanceIcon(cls_node);
res := Values.STRING(JSON.toString(json, prettyPrint));
end getModelInstanceIcon;
function parseModifier
input String modifierValue;
input InstNode scope;
output Modifier outMod;
protected
Absyn.Modification amod;
SCode.Mod smod;
algorithm
try
// stringMod parses a single modifier ("x(start = 1) = 2"), but here we want
// to parse just a class modifier ("(x = 1, y = 2)"). So we add a dummy name
// to the string and then extract the modifier from the ElementArg.
Absyn.ElementArg.MODIFICATION(modification = SOME(amod)) :=
Parser.stringMod("dummy" + modifierValue);
// Then translate the Absyn mod to a Modifier, using the given scope (it
// doesn't matter much which scope it is, it just needs some scope or the
// instantiation will fail later).
smod := AbsynToSCode.translateMod(SOME(amod),
SCode.Final.NOT_FINAL(), SCode.Each.NOT_EACH(), AbsynUtil.dummyInfo);
outMod := Modifier.create(smod, "", NFModifier.ModifierScope.COMPONENT(""), scope);
else
outMod := Modifier.NOMOD();
end try;
end parseModifier;
function buildInstanceTree
input InstNode node;
input Boolean isDerived = false;
output InstanceTree tree;
protected
InstNode cls_node;
Class cls;
ClassTree cls_tree;
list<InstanceTree> elems;
algorithm
cls_node := InstNode.resolveInner(node);
cls := InstNode.getClass(cls_node);
if not isDerived and Class.isOnlyBuiltin(cls) then
tree := InstanceTree.EMPTY();
return;
end if;
cls_tree := Class.classTree(cls);
tree := match (cls, cls_tree)
case (Class.EXPANDED_DERIVED(), _)
algorithm
elems := {buildInstanceTree(cls.baseClass, isDerived = true)};
then
InstanceTree.CLASS(node, elems, isDerived);
case (_, ClassTree.INSTANTIATED_TREE())
algorithm
elems := buildInstanceTreeElements(InstNode.definition(cls_node), cls_tree);
then
InstanceTree.CLASS(node, elems, isDerived);
case (_, ClassTree.FLAT_TREE())
then InstanceTree.CLASS(node, {}, isDerived);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown class tree", sourceInfo());
then
fail();
end match;
end buildInstanceTree;
function buildInstanceTreeElements
input SCode.Element classDefinition;
input ClassTree classTree;
output list<InstanceTree> elements = {};
protected
list<SCode.Element> scode_elems;
array<Mutable<InstNode>> clss, comps;
array<InstNode> exts;
Integer cls_index = 1, comp_index = 1, ext_index = 1;
InstanceTree tree;
list<Integer> local_comps;
algorithm
ClassTree.INSTANTIATED_TREE(classes = clss, components = comps, exts = exts,
localComponents = local_comps) := classTree;
scode_elems := SCodeUtil.getClassElements(classDefinition);
if not listEmpty(local_comps) then
comp_index :: local_comps := local_comps;
end if;
for e in scode_elems loop
elements := match e
case SCode.Element.EXTENDS()
algorithm
tree := buildInstanceTree(exts[ext_index], isDerived = true);