-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFFlatModel.mo
1024 lines (896 loc) · 31.3 KB
/
NFFlatModel.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 uniontype NFFlatModel
import Equation = NFEquation;
import Algorithm = NFAlgorithm;
import Variable = NFVariable;
protected
import Statement = NFStatement;
import NFFunction.Function;
import Expression = NFExpression;
import Type = NFType;
import Binding = NFBinding;
import Dimension = NFDimension;
import ComplexType = NFComplexType;
import NFInstNode.InstNode;
import IOStream;
import NFSubscript.Subscript;
import Class = NFClass;
import NFClassTree.ClassTree;
import Component = NFComponent;
import NFComponentRef.ComponentRef;
import DAE.ElementSource;
import MetaModelica.Dangerous.listReverseInPlace;
import Util;
import Prefixes = NFPrefixes;
import NFPrefixes.Visibility;
import FlatModelicaUtil = NFFlatModelicaUtil;
import UnorderedMap;
import Typing = NFTyping;
import ErrorExt;
import Lookup = NFLookup;
import InstContext = NFInstContext;
import FlatModel = NFFlatModel;
type TypeMap = UnorderedMap<Absyn.Path, Type>;
public
record FLAT_MODEL
String name;
list<Variable> variables;
list<Equation> equations;
list<Equation> initialEquations;
list<Algorithm> algorithms;
list<Algorithm> initialAlgorithms;
ElementSource source;
end FLAT_MODEL;
function mapExp
input output FlatModel flatModel;
input MapFn fn;
partial function MapFn
input output Expression exp;
end MapFn;
algorithm
flatModel.variables := list(Variable.mapExpShallow(v, fn) for v in flatModel.variables);
flatModel.equations := Equation.mapExpList(flatModel.equations, fn);
flatModel.initialEquations := Equation.mapExpList(flatModel.initialEquations, fn);
flatModel.algorithms := Algorithm.mapExpList(flatModel.algorithms, fn);
flatModel.initialAlgorithms := Algorithm.mapExpList(flatModel.initialAlgorithms, fn);
end mapExp;
function mapEquations
input output FlatModel flatModel;
input MapFn fn;
partial function MapFn
input output Equation eq;
end MapFn;
algorithm
flatModel.equations := list(Equation.map(eq, fn) for eq in flatModel.equations);
flatModel.initialEquations := list(Equation.map(eq, fn) for eq in flatModel.initialEquations);
end mapEquations;
function mapAlgorithms
input output FlatModel flatModel;
input MapFn fn;
partial function MapFn
input output Algorithm alg;
end MapFn;
algorithm
flatModel.algorithms := list(fn(alg) for alg in flatModel.algorithms);
flatModel.initialAlgorithms := list(fn(alg) for alg in flatModel.initialAlgorithms);
end mapAlgorithms;
function toString
input FlatModel flatModel;
input Boolean printBindingTypes = false;
output String str = IOStream.string(toStream(flatModel, printBindingTypes));
end toString;
function printString
input FlatModel flatModel;
input Boolean printBindingTypes = false;
protected
IOStream.IOStream s;
algorithm
s := toStream(flatModel, printBindingTypes);
IOStream.print(s, IOStream.stdOutput);
end printString;
function toStream
input FlatModel flatModel;
input Boolean printBindingTypes = false;
output IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := appendStream(flatModel, printBindingTypes, s);
end toStream;
function appendStream
input FlatModel flatModel;
input Boolean printBindingTypes = false;
input output IOStream.IOStream s;
algorithm
s := IOStream.append(s, "class " + flatModel.name + "\n");
for v in flatModel.variables loop
s := Variable.toStream(v, " ", printBindingTypes, s);
s := IOStream.append(s, ";\n");
end for;
if not listEmpty(flatModel.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toStreamList(flatModel.initialEquations, " ", s);
end if;
if not listEmpty(flatModel.equations) then
s := IOStream.append(s, "equation\n");
s := Equation.toStreamList(flatModel.equations, " ", s);
end if;
for alg in flatModel.initialAlgorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "initial algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;
for alg in flatModel.algorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;
s := IOStream.append(s, "end " + flatModel.name + ";\n");
end appendStream;
function toFlatString
"Returns a string containing the flat Modelica representation of the given model."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
output String str = IOStream.string(toFlatStream(flatModel, functions, printBindingTypes));
end toFlatString;
function printFlatString
"Prints a flat Modelica representation of the given model to standard output."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
protected
IOStream.IOStream s;
algorithm
s := toFlatStream(flatModel, functions, printBindingTypes);
IOStream.print(s, IOStream.stdOutput);
end printFlatString;
function toFlatStream
"Returns a new IOStream containing the flat Modelica representation of the given model."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
output IOStream.IOStream s;
algorithm
s := IOStream.create(flatModel.name, IOStream.IOStreamType.LIST());
s := appendFlatStream(flatModel, functions, printBindingTypes, s);
end toFlatStream;
function appendFlatStream
"Appends the flat Modelica representation of the given model to an existing IOStream."
input FlatModel flatModel;
input list<Function> functions;
input Boolean printBindingTypes = false;
input output IOStream.IOStream s;
protected
FlatModel flat_model = flatModel;
Visibility visibility = Visibility.PUBLIC;
algorithm
s := IOStream.append(s, "model '" + flat_model.name + "'");
s := FlatModelicaUtil.appendElementSourceCommentString(flat_model.source, s);
s := IOStream.append(s, "\n");
flat_model.variables := reconstructRecordInstances(flat_model.variables);
for fn in functions loop
if not (Function.isDefaultRecordConstructor(fn) or Function.isExternalObjectConstructorOrDestructor(fn)) then
s := Function.toFlatStream(fn, s);
s := IOStream.append(s, ";\n\n");
end if;
end for;
for ty in collectFlatTypes(flat_model, functions) loop
s := Type.toFlatDeclarationStream(ty, s);
s := IOStream.append(s, ";\n\n");
end for;
for v in flat_model.variables loop
if visibility <> Variable.visibility(v) then
visibility := Variable.visibility(v);
s := IOStream.append(s, Prefixes.visibilityString(visibility));
s := IOStream.append(s, "\n");
end if;
s := Variable.toFlatStream(v, " ", printBindingTypes, s);
s := IOStream.append(s, ";\n");
end for;
if not listEmpty(flat_model.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toFlatStreamList(flat_model.initialEquations, " ", s);
end if;
if not listEmpty(flat_model.equations) then
s := IOStream.append(s, "equation\n");
s := Equation.toFlatStreamList(flat_model.equations, " ", s);
end if;
for alg in flat_model.initialAlgorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "initial algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end if;
end for;
for alg in flat_model.algorithms loop
if not listEmpty(alg.statements) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end if;
end for;
s := FlatModelicaUtil.appendElementSourceCommentAnnotation(flat_model.source, " ", ";\n", s);
s := IOStream.append(s, "end '" + flat_model.name + "';\n");
end appendFlatStream;
function collectFlatTypes
input FlatModel flatModel;
input list<Function> functions;
output list<Type> outTypes;
protected
TypeMap types;
algorithm
types := UnorderedMap.new<Type>(AbsynUtil.pathHash, AbsynUtil.pathEqual);
List.map1_0(flatModel.variables, collectVariableFlatTypes, types);
List.map1_0(flatModel.equations, collectEquationFlatTypes, types);
List.map1_0(flatModel.initialEquations, collectEquationFlatTypes, types);
List.map1_0(flatModel.algorithms, collectAlgorithmFlatTypes, types);
List.map1_0(flatModel.initialAlgorithms, collectAlgorithmFlatTypes, types);
List.map1_0(functions, collectFunctionFlatTypes, types);
outTypes := UnorderedMap.valueList(types);
outTypes := list(typeFlatType(ty) for ty in outTypes);
end collectFlatTypes;
function collectVariableFlatTypes
input Variable var;
input TypeMap types;
algorithm
collectFlatType(var.ty, types);
collectBindingFlatTypes(var.binding, types);
for attr in var.typeAttributes loop
collectBindingFlatTypes(Util.tuple22(attr), types);
end for;
end collectVariableFlatTypes;
function collectFlatType
input Type ty;
input TypeMap types;
algorithm
() := match ty
case Type.ENUMERATION()
algorithm
UnorderedMap.tryAdd(ty.typePath, ty, types);
then
();
case Type.ARRAY()
algorithm
Dimension.foldExpList(ty.dimensions, collectExpFlatTypes_traverse, types);
collectFlatType(ty.elementType, types);
then
();
case Type.COMPLEX(complexTy = ComplexType.RECORD())
algorithm
UnorderedMap.tryAdd(InstNode.scopePath(ty.cls), ty, types);
then
();
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT())
algorithm
UnorderedMap.tryAdd(InstNode.scopePath(ty.cls), ty, types);
then
();
else ();
end match;
end collectFlatType;
function collectBindingFlatTypes
input Binding binding;
input TypeMap types;
algorithm
if Binding.isExplicitlyBound(binding) then
collectExpFlatTypes(Binding.getTypedExp(binding), types);
end if;
end collectBindingFlatTypes;
function collectEquationFlatTypes
input Equation eq;
input TypeMap types;
algorithm
() := match eq
case Equation.EQUALITY()
algorithm
collectExpFlatTypes(eq.lhs, types);
collectExpFlatTypes(eq.rhs, types);
collectFlatType(eq.ty, types);
then
();
case Equation.ARRAY_EQUALITY()
algorithm
collectExpFlatTypes(eq.lhs, types);
collectExpFlatTypes(eq.rhs, types);
collectFlatType(eq.ty, types);
then
();
case Equation.FOR()
algorithm
if isSome(eq.range) then
collectExpFlatTypes(Util.getOption(eq.range), types);
end if;
List.map1_0(eq.body, collectEquationFlatTypes, types);
then
();
case Equation.IF()
algorithm
List.map1_0(eq.branches, collectEqBranchFlatTypes, types);
then
();
case Equation.WHEN()
algorithm
List.map1_0(eq.branches, collectEqBranchFlatTypes, types);
then
();
case Equation.ASSERT()
algorithm
collectExpFlatTypes(eq.condition, types);
collectExpFlatTypes(eq.message, types);
collectExpFlatTypes(eq.level, types);
then
();
case Equation.TERMINATE()
algorithm
collectExpFlatTypes(eq.message, types);
then
();
case Equation.REINIT()
algorithm
collectExpFlatTypes(eq.reinitExp, types);
then
();
case Equation.NORETCALL()
algorithm
collectExpFlatTypes(eq.exp, types);
then
();
else ();
end match;
end collectEquationFlatTypes;
function collectEqBranchFlatTypes
input Equation.Branch branch;
input TypeMap types;
algorithm
() := match branch
case Equation.Branch.BRANCH()
algorithm
collectExpFlatTypes(branch.condition, types);
List.map1_0(branch.body, collectEquationFlatTypes, types);
then
();
else ();
end match;
end collectEqBranchFlatTypes;
function collectAlgorithmFlatTypes
input Algorithm alg;
input TypeMap types;
algorithm
collectStatementsFlatTypes(alg.statements, types);
end collectAlgorithmFlatTypes;
function collectStatementsFlatTypes
input list<Statement> statements;
input TypeMap types;
algorithm
for s in statements loop
collectStatementFlatTypes(s, types);
end for;
end collectStatementsFlatTypes;
function collectStatementFlatTypes
input Statement stmt;
input TypeMap types;
algorithm
() := match stmt
case Statement.ASSIGNMENT()
algorithm
collectExpFlatTypes(stmt.lhs, types);
collectExpFlatTypes(stmt.rhs, types);
collectFlatType(stmt.ty, types);
then
();
case Statement.FOR()
algorithm
collectStatementsFlatTypes(stmt.body, types);
collectExpFlatTypes(Util.getOption(stmt.range), types);
then
();
case Statement.IF()
algorithm
List.map1_0(stmt.branches, collectStmtBranchFlatTypes, types);
then
();
case Statement.WHEN()
algorithm
List.map1_0(stmt.branches, collectStmtBranchFlatTypes, types);
then
();
case Statement.ASSERT()
algorithm
collectExpFlatTypes(stmt.condition, types);
collectExpFlatTypes(stmt.message, types);
collectExpFlatTypes(stmt.level, types);
then
();
case Statement.TERMINATE()
algorithm
collectExpFlatTypes(stmt.message, types);
then
();
case Statement.REINIT()
algorithm
collectExpFlatTypes(stmt.cref, types);
collectExpFlatTypes(stmt.reinitExp, types);
then
();
case Statement.NORETCALL()
algorithm
collectExpFlatTypes(stmt.exp, types);
then
();
case Statement.WHILE()
algorithm
collectExpFlatTypes(stmt.condition, types);
collectStatementsFlatTypes(stmt.body, types);
then
();
else ();
end match;
end collectStatementFlatTypes;
function collectStmtBranchFlatTypes
input tuple<Expression, list<Statement>> branch;
input TypeMap types;
algorithm
collectExpFlatTypes(Util.tuple21(branch), types);
collectStatementsFlatTypes(Util.tuple22(branch), types);
end collectStmtBranchFlatTypes;
function collectExpFlatTypes
input Expression exp;
input TypeMap types;
algorithm
Expression.fold(exp, collectExpFlatTypes_traverse, types);
end collectExpFlatTypes;
function collectExpFlatTypes_traverse
input Expression exp;
input output TypeMap types;
algorithm
() := match exp
case Expression.SUBSCRIPTED_EXP()
guard Flags.getConfigBool(Flags.MODELICA_OUTPUT)
algorithm
collectSubscriptedFlatType(exp.exp, exp.subscripts, exp.ty, types);
then
();
else
algorithm
collectFlatType(Expression.typeOf(exp), types);
then
();
end match;
end collectExpFlatTypes_traverse;
function collectFunctionFlatTypes
input Function fn;
input TypeMap types;
algorithm
ClassTree.applyComponents(Class.classTree(InstNode.getClass(fn.node)),
function collectComponentFlatTypes(types = types));
if not Function.isExternal(fn) then
collectStatementsFlatTypes(Function.getBody(fn), types);
end if;
end collectFunctionFlatTypes;
function collectComponentFlatTypes
input InstNode component;
input TypeMap types;
protected
Component comp;
algorithm
comp := InstNode.component(component);
collectFlatType(Component.getType(comp), types);
collectBindingFlatTypes(Component.getBinding(comp), types);
end collectComponentFlatTypes;
function collectSubscriptedFlatType
input Expression exp;
input list<Subscript> subs;
input Type subscriptedTy;
input TypeMap types;
protected
Type exp_ty;
list<Type> sub_tyl;
list<Dimension> dims;
list<String> strl;
String name;
algorithm
exp_ty := Expression.typeOf(exp);
dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs));
sub_tyl := list(Dimension.subscriptType(d) for d in dims);
name := Type.subscriptedTypeName(exp_ty, sub_tyl);
UnorderedMap.tryAdd(Absyn.IDENT(name), Type.SUBSCRIPTED(name, exp_ty, sub_tyl, subscriptedTy), types);
end collectSubscriptedFlatType;
function reconstructRecordInstances
input list<Variable> variables;
output list<Variable> outVariables = {};
protected
list<Variable> rest_vars = variables, record_vars;
Variable var;
ComponentRef parent_cr;
Type parent_ty;
Integer field_count;
algorithm
while not listEmpty(rest_vars) loop
var :: rest_vars := rest_vars;
parent_cr := ComponentRef.rest(var.name);
if not ComponentRef.isEmpty(parent_cr) then
parent_ty := ComponentRef.nodeType(parent_cr);
if Type.isRecord(parent_ty) then
field_count := listLength(Type.recordFields(parent_ty));
(record_vars, rest_vars) := List.split(rest_vars, field_count - 1);
record_vars := var :: record_vars;
var := reconstructRecordInstance(parent_cr, record_vars);
end if;
end if;
outVariables := var :: outVariables;
end while;
outVariables := listReverseInPlace(outVariables);
end reconstructRecordInstances;
function reconstructRecordInstance
input ComponentRef recordName;
input list<Variable> variables;
output Variable recordVar;
protected
InstNode record_node;
Component record_comp;
Type record_ty;
list<Expression> field_exps;
Expression record_exp;
Binding record_binding;
algorithm
record_node := ComponentRef.node(recordName);
record_comp := InstNode.component(record_node);
record_ty := ComponentRef.nodeType(recordName);
// Reconstruct the record instance binding if possible. If any field is
// missing a binding we assume that the record instance didn't have a
// binding in the first place, or that the binding was moved to an equation
// during flattening.
field_exps := {};
for v in variables loop
if Binding.hasExp(v.binding) then
field_exps := Binding.getExp(v.binding) :: field_exps;
else
field_exps := {};
break;
end if;
end for;
if listEmpty(field_exps) then
record_binding := NFBinding.EMPTY_BINDING;
else
field_exps := listReverseInPlace(field_exps);
record_exp := Expression.makeRecord(InstNode.scopePath(InstNode.classScope(record_node)), record_ty, field_exps);
record_binding := Binding.makeFlat(record_exp, Component.variability(record_comp), NFBinding.Source.GENERATED);
end if;
recordVar := Variable.VARIABLE(recordName, record_ty, record_binding, InstNode.visibility(record_node),
Component.getAttributes(record_comp), {}, {}, Component.comment(record_comp), InstNode.info(record_node), NFBackendExtension.DUMMY_BACKEND_INFO);
end reconstructRecordInstance;
function typeFlatType
input output Type ty;
algorithm
() := match ty
case Type.COMPLEX(complexTy = ComplexType.RECORD())
algorithm
Typing.typeBindings(ty.cls, NFInstContext.CLASS);
then
();
else ();
end match;
end typeFlatType;
protected
type ObfuscationMap = UnorderedMap<InstNode, String>;
public
function obfuscate
input output FlatModel flatModel;
protected
ObfuscationMap obfuscation_map;
Boolean only_encrypted;
algorithm
only_encrypted := Flags.getConfigString(Flags.OBFUSCATE) == "encrypted";
obfuscation_map := UnorderedMap.new<String>(InstNode.hash, InstNode.refEqual);
for v in flatModel.variables loop
addObfuscatedVariable(v, only_encrypted, obfuscation_map);
end for;
if UnorderedMap.isEmpty(obfuscation_map) then
return;
end if;
flatModel.variables := list(obfuscateVariable(v, obfuscation_map) for v in flatModel.variables);
flatModel := mapEquations(flatModel, function obfuscateEquation(obfuscationMap = obfuscation_map));
flatModel := mapAlgorithms(flatModel, function obfuscateAlgorithm(obfuscationMap = obfuscation_map));
end obfuscate;
function addObfuscatedVariable
input Variable var;
input Boolean onlyEncrypted;
input ObfuscationMap obfuscationMap;
protected
SourceInfo info;
String filename;
list<InstNode> nodes;
algorithm
if Variable.isProtected(var) and (not onlyEncrypted or Variable.isEncrypted(var)) then
nodes := ComponentRef.nodes(var.name);
nodes := List.trim(nodes, InstNode.isPublic);
for node in nodes loop
UnorderedMap.tryAdd(node, "n" + String(UnorderedMap.size(obfuscationMap) + 1), obfuscationMap);
end for;
end if;
end addObfuscatedVariable;
function obfuscateVariable
input output Variable var;
input ObfuscationMap obfuscationMap;
algorithm
var.name := obfuscateCref(var.name, obfuscationMap);
var.comment := obfuscateCommentOpt(var.comment, ComponentRef.node(var.name), obfuscationMap);
var := Variable.mapExpShallow(var, function obfuscateExp(obfuscationMap = obfuscationMap));
end obfuscateVariable;
function obfuscateCref
input output ComponentRef cref;
input ObfuscationMap obfuscationMap;
protected
Option<String> name;
algorithm
() := match cref
case ComponentRef.CREF()
algorithm
name := UnorderedMap.get(cref.node, obfuscationMap);
if isSome(name) then
cref.node := InstNode.rename(Util.getOption(name), cref.node);
end if;
cref.subscripts := list(Subscript.mapShallowExp(s,
function obfuscateExp(obfuscationMap = obfuscationMap)) for s in cref.subscripts);
then
();
else ();
end match;
end obfuscateCref;
function obfuscateExp
input output Expression exp;
input ObfuscationMap obfuscationMap;
algorithm
exp := Expression.map(exp, function obfuscateExp_impl(obfuscationMap = obfuscationMap));
end obfuscateExp;
function obfuscateExpOpt
input output Option<Expression> exp;
input ObfuscationMap obfuscationMap;
algorithm
if isSome(exp) then
exp := SOME(obfuscateExp(Util.getOption(exp), obfuscationMap));
end if;
end obfuscateExpOpt;
function obfuscateExp_impl
input output Expression exp;
input ObfuscationMap obfuscationMap;
algorithm
() := match exp
case Expression.CREF()
algorithm
exp.cref := obfuscateCref(exp.cref, obfuscationMap);
then
();
else ();
end match;
end obfuscateExp_impl;
function obfuscateEquation
input output Equation eq;
input ObfuscationMap obfuscationMap;
algorithm
eq := Equation.setSource(obfuscateSource(Equation.source(eq), Equation.scope(eq), obfuscationMap), eq);
eq := Equation.mapExpShallow(eq, function obfuscateExp(obfuscationMap = obfuscationMap));
end obfuscateEquation;
function obfuscateAlgorithm
input output Algorithm alg;
input ObfuscationMap obfuscationMap;
algorithm
alg.source := obfuscateSource(alg.source, alg.scope, obfuscationMap);
alg.inputs := list(obfuscateCref(e, obfuscationMap) for e in alg.inputs);
alg.outputs := list(obfuscateCref(e, obfuscationMap) for e in alg.outputs);
alg.statements := list(Statement.map(s,
function obfuscateStatement(scope = alg.scope, obfuscationMap = obfuscationMap)) for s in alg.statements);
end obfuscateAlgorithm;
function obfuscateStatement
input output Statement stmt;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
stmt := Statement.setSource(obfuscateSource(Statement.source(stmt), scope, obfuscationMap), stmt);
stmt := Statement.mapExpShallow(stmt, function obfuscateExp(obfuscationMap = obfuscationMap));
end obfuscateStatement;
function obfuscateSource
input output DAE.ElementSource source;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
source.comment := list(obfuscateComment(c, scope, obfuscationMap) for c in source.comment);
end obfuscateSource;
function obfuscateCommentOpt
input output Option<SCode.Comment> comment;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
comment := Util.applyOption(comment,
function obfuscateComment(scope = scope, obfuscationMap = obfuscationMap));
end obfuscateCommentOpt;
function obfuscateComment
input output SCode.Comment comment;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
comment.annotation_ := obfuscateAnnotationOpt(comment.annotation_, scope, obfuscationMap);
comment.comment := NONE();
end obfuscateComment;
function obfuscateAnnotationOpt
input output Option<SCode.Annotation> ann;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
ann := Util.applyOption(ann,
function obfuscateAnnotation(scope = scope, obfuscationMap = obfuscationMap));
end obfuscateAnnotationOpt;
function obfuscateAnnotation
input output SCode.Annotation ann;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
ann.modification := obfuscateAnnotationMod(ann.modification, scope, obfuscationMap);
end obfuscateAnnotation;
function obfuscateAnnotationMod
input output SCode.Mod mod;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
() := match mod
case SCode.Mod.MOD()
algorithm
mod.subModLst := list(obfuscateAnnotationSubMod(s, scope, obfuscationMap)
for s guard isAllowedAnnotation(s) in mod.subModLst);
mod.binding := obfuscateAbsynExpOpt(mod.binding, scope, obfuscationMap);
then
();
else ();
end match;
end obfuscateAnnotationMod;
function isAllowedAnnotation
input SCode.SubMod mod;
output Boolean allowed;
algorithm
allowed := match mod.ident
case "Icon" then false;
case "Diagram" then false;
case "Dialog" then false;
case "IconMap" then false;
case "DiagramMap" then false;
case "Placement" then false;
case "Text" then false;
case "Line" then false;
case "defaultComponentName" then false;
case "defaultComponentPrefixes" then false;
case "missingInnerMessage" then false;
case "obsolete" then false;
case "unassignedMessage" then false;
case "Protection" then false;
case "Authorization" then false;
else not Util.stringStartsWith("__", mod.ident);
end match;
end isAllowedAnnotation;
function obfuscateAnnotationSubMod
input output SCode.SubMod mod;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
mod.mod := obfuscateAnnotationMod(mod.mod, scope, obfuscationMap);
end obfuscateAnnotationSubMod;
function obfuscateAbsynExpOpt
input output Option<Absyn.Exp> exp;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
exp := Util.applyOption(exp,
function obfuscateAbsynExp(scope = scope, obfuscationMap = obfuscationMap));
end obfuscateAbsynExpOpt;
function obfuscateAbsynExp
input output Absyn.Exp exp;
input InstNode scope;
input ObfuscationMap obfuscationMap;
algorithm
exp := AbsynUtil.traverseExp(exp, function obfuscateAbsynExpTraverse(scope = scope), obfuscationMap);
end obfuscateAbsynExp;
function obfuscateAbsynExpTraverse
input output Absyn.Exp exp;
input InstNode scope;
input output ObfuscationMap obfuscationMap;
algorithm
() := match exp
case Absyn.Exp.CREF()
algorithm
exp.componentRef := obfuscateAbsynCref(exp.componentRef, scope, obfuscationMap);
then
();
else ();
end match;
end obfuscateAbsynExpTraverse;
function obfuscateAbsynCref
input output Absyn.ComponentRef cref;
input InstNode scope;
input ObfuscationMap obfuscationMap;
protected
ComponentRef inst_cref;
list<InstNode> nodes;
algorithm
ErrorExt.setCheckpoint(getInstanceName());
try
inst_cref := Lookup.lookupCref(cref, scope, NFInstContext.RELAXED);
nodes := list(ComponentRef.node(c) for c in ComponentRef.toListReverse(inst_cref, includeScope = false));
cref := obfuscateAbsynCref2(cref, nodes, obfuscationMap);
else
end try;
ErrorExt.rollBack(getInstanceName());
end obfuscateAbsynCref;
function obfuscateAbsynCref2
input output Absyn.ComponentRef cref;
input list<InstNode> nodes;
input ObfuscationMap obfuscationMap;
protected
InstNode node;
list<InstNode> rest_nodes;
algorithm
() := match (cref, nodes)
case (Absyn.ComponentRef.CREF_FULLYQUALIFIED(), _)
algorithm
cref.componentRef := obfuscateAbsynCref2(cref.componentRef, nodes, obfuscationMap);
then
();
case (Absyn.ComponentRef.CREF_QUAL(), node :: rest_nodes)
guard InstNode.name(node) == cref.name
algorithm
cref.name := UnorderedMap.getOrDefault(node, obfuscationMap, cref.name);
cref.componentRef := obfuscateAbsynCref2(cref.componentRef, rest_nodes, obfuscationMap);
then
();
case (Absyn.ComponentRef.CREF_IDENT(), node :: _)
guard InstNode.name(node) == cref.name
algorithm
cref.name := UnorderedMap.getOrDefault(node, obfuscationMap, cref.name);