-
Notifications
You must be signed in to change notification settings - Fork 297
/
NFApi.mo
793 lines (684 loc) · 26.9 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
/*
* 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;
protected
import Inst = NFInst;
import Builtin = NFBuiltin;
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.Modifier;
import NFModifier.ModifierScope;
import Operator = NFOperator;
import Equation = NFEquation;
import Statement = NFStatement;
import NFType.Type;
import Subscript = NFSubscript;
import Connector = NFConnector;
import Connection = NFConnection;
import Algorithm = NFAlgorithm;
import InstContext = NFInstContext;
import Absyn.Path;
import AbsynToSCode;
import Array;
import ComplexType = NFComplexType;
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 InstUtil = NFInstUtil;
import Interactive;
import List;
import Lookup = NFLookup;
import MetaModelica.Dangerous;
import NFCall.Call;
import NFCeval;
import NFClassTree.ClassTree;
import NFFlatten.FunctionTree;
import NFFunction.Function;
import NFInst;
import NFInstNode.CachedData;
import NFInstNode.NodeTree;
import NFPrefixes.{Variability, Purity};
import NFSections.Sections;
import OperatorOverloading = NFOperatorOverloading;
import Package = NFPackage;
import Prefixes = NFPrefixes;
import Record = NFRecord;
import Restriction = NFRestriction;
import Scalarize = NFScalarize;
import SimplifyExp = NFSimplifyExp;
import SimplifyModel = NFSimplifyModel;
import System;
import Typing = NFTyping;
import UnitCheck = NFUnitCheck;
import Util;
import Variable = NFVariable;
import VerifyModel = NFVerifyModel;
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;
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;
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;
algorithm
stringLst := {};
Absyn.ANNOTATION(el) := inAnnotation;
for e in listReverse(el) loop
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, NFInstContext.RELAXED, info);
(exp, ty, var) := Typing.typeExp(exp, NFInstContext.CLASS, info);
// exp := NFCeval.evalExp(exp);
exp := SimplifyExp.simplify(Expression.stripBindingInfo(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, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.expand(anncls);
inst_anncls := NFInst.instClass(inst_anncls, Modifier.create(smod, annName, ModifierScope.CLASS(annName), {inst_cls, inst_anncls}, inst_cls), NFComponent.DEFAULT_ATTR, true, 0, inst_cls, NFInstContext.NO_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 = NFInstContext.RELAXED);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
if (stringEq(annName, "Icon") or stringEq(annName, "Diagram")) 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, NFInstContext.RELAXED, info);
(exp, ty, var) := Typing.typeExp(exp, NFInstContext.CLASS, info);
// exp := NFCeval.evalExp(exp);
exp := SimplifyExp.simplify(Expression.stripBindingInfo(exp));
str := str + ", " + Expression.toString(exp);
else
// just don't fail!
end try;
end if;
then
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, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.instantiate(anncls, context = 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_anncls, context = NFInstContext.RELAXED);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
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;
algorithm
// 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 := 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, NFInstContext.RELAXED, info);
(exp, ty, var) := Typing.typeExp(exp, NFInstContext.CLASS, info);
// exp := NFCeval.evalExp(exp);
exp := SimplifyExp.simplify(Expression.stripBindingInfo(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, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.expand(anncls);
inst_anncls := NFInst.instClass(inst_anncls, Modifier.create(smod, annName, ModifierScope.CLASS(annName), {inst_cls, inst_anncls}, inst_cls), NFComponent.DEFAULT_ATTR, true, 0, inst_cls, NFInstContext.NO_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 = NFInstContext.RELAXED);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
stringAppendList({annName, "(", str, ")"});
case Absyn.MODIFICATION(
path = Absyn.IDENT(annName),
modification = SOME(Absyn.CLASSMOD(_, Absyn.NOMOD())))
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, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
inst_anncls := NFInst.instantiate(anncls, context = 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_anncls, context = NFInstContext.RELAXED);
// Mark structural parameters.
NFInst.updateImplicitVariability(inst_anncls, Flags.isSet(Flags.EVAL_PARAM));
dae := frontEndBack(inst_anncls, annName);
str := DAEUtil.getVariableBindingsStr(DAEUtil.daeElements(dae));
then
stringAppendList({annName, "(", str, ")"});
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;
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);
// if is derived qualify in the parent
if InstNode.isDerivedClass(expanded_cls) then
cls := Lookup.lookupClassName(pathToQualify, InstNode.classParent(expanded_cls), NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
else // qualify in the class
cls := Lookup.lookupClassName(pathToQualify, expanded_cls, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false);
end if;
qualPath := InstNode.scopePath(cls, true);
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));
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 := Interactive.modelicaAnnotationProgram(Config.getAnnotationVersion());
graphicProgramSCode := AbsynToSCode.translateAbsyn2SCode(placementProgram);
program := listAppend(graphicProgramSCode, program);
// gather here all the flags to disable expansion
// and scalarization if -d=-nfScalarize is on
if not Flags.isSet(Flags.NF_SCALARIZE) then
// make sure we don't expand anything
FlagsUtil.set(Flags.NF_EXPAND_OPERATIONS, false);
FlagsUtil.set(Flags.NF_EXPAND_FUNC_ARGS, false);
end if;
System.setUsesCardinality(false);
// Create a root node from the given top-level classes.
top := NFInst.makeTopNode(program);
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
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);
// 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;
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);
// Flatten and simplify the model.
flat_model := Flatten.flatten(inst_cls, name);
flat_model := EvalConstants.evaluate(flat_model);
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) 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(" + name + ")");
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);
// 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);
// 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;
annotation(__OpenModelica_Interface="backend");
end NFApi;