-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFCall.mo
2454 lines (2141 loc) · 73.4 KB
/
NFCall.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, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated uniontype NFCall
import Absyn;
import AbsynUtil;
import DAE;
import Expression = NFExpression;
import NFCallAttributes;
import NFInstNode.InstNode;
import NFPrefixes.Variability;
import Type = NFType;
import Record = NFRecord;
protected
import Binding = NFBinding;
import BuiltinCall = NFBuiltinCall;
import Ceval = NFCeval;
import Component = NFComponent;
import ComponentRef = NFComponentRef;
import Config;
import Dimension = NFDimension;
import ErrorExt;
import EvalFunction = NFEvalFunction;
import Inline = NFInline;
import Inst = NFInst;
import List;
import Lookup = NFLookup;
import MetaModelica.Dangerous.listReverseInPlace;
import Class = NFClass;
import NFFunction.Function;
import NFFunction.FunctionMatchKind;
import NFFunction.MatchedFunction;
import NFFunction.NamedArg;
import NFFunction.TypedArg;
import NFFunction.TypedNamedArg;
import NFInstNode.CachedData;
import NFTyping.ExpOrigin;
import Operator = NFOperator;
import Prefixes = NFPrefixes;
import SCodeUtil;
import SimplifyExp = NFSimplifyExp;
import Subscript = NFSubscript;
import TypeCheck = NFTypeCheck;
import Typing = NFTyping;
import Util;
import Call = NFCall;
protected
import NFCallParameterTree;
type ParameterTree = NFCallParameterTree.Tree;
public
record UNTYPED_CALL
ComponentRef ref;
list<Expression> arguments;
list<NamedArg> named_args;
InstNode call_scope;
end UNTYPED_CALL;
record ARG_TYPED_CALL
ComponentRef ref;
list<TypedArg> arguments;
list<TypedNamedArg> named_args;
InstNode call_scope;
end ARG_TYPED_CALL;
record TYPED_CALL
Function fn;
Type ty;
Variability var;
list<Expression> arguments;
NFCallAttributes attributes;
end TYPED_CALL;
record UNTYPED_ARRAY_CONSTRUCTOR
Expression exp;
list<tuple<InstNode, Expression>> iters;
end UNTYPED_ARRAY_CONSTRUCTOR;
record TYPED_ARRAY_CONSTRUCTOR
Type ty;
Variability var;
Expression exp;
list<tuple<InstNode, Expression>> iters;
end TYPED_ARRAY_CONSTRUCTOR;
record UNTYPED_REDUCTION
ComponentRef ref;
Expression exp;
list<tuple<InstNode, Expression>> iters;
end UNTYPED_REDUCTION;
record TYPED_REDUCTION
Function fn;
Type ty;
Variability var;
Expression exp;
list<tuple<InstNode, Expression>> iters;
Option<Expression> defaultExp;
tuple<Option<Expression>, String, String> foldExp;
end TYPED_REDUCTION;
function instantiate
input Absyn.ComponentRef functionName;
input Absyn.FunctionArgs functionArgs;
input InstNode scope;
input SourceInfo info;
output Expression callExp;
algorithm
callExp := match functionArgs
case Absyn.FUNCTIONARGS() then instNormalCall(functionName, functionArgs, scope, info);
case Absyn.FOR_ITER_FARG() then instIteratorCall(functionName, functionArgs, scope, info);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown call type", sourceInfo());
then
fail();
end match;
end instantiate;
function typeCall
input Expression callExp;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression outExp;
output Type ty;
output Variability var;
protected
NFCall call, ty_call;
list<Expression> args;
ComponentRef cref;
algorithm
Expression.CALL(call = call) := callExp;
outExp := match call
case UNTYPED_CALL(ref = cref)
algorithm
if(BuiltinCall.needSpecialHandling(call)) then
(outExp, ty, var) := BuiltinCall.typeSpecial(call, origin, info);
else
ty_call := typeMatchNormalCall(call, origin, info);
ty := typeOf(ty_call);
var := variability(ty_call);
if isRecordConstructor(ty_call) then
outExp := toRecordExpression(ty_call, ty);
else
if Function.hasUnboxArgs(typedFunction(ty_call)) then
outExp := Expression.CALL(unboxArgs(ty_call));
else
outExp := Expression.CALL(ty_call);
end if;
outExp := Inline.inlineCallExp(outExp);
end if;
end if;
then
outExp;
case UNTYPED_ARRAY_CONSTRUCTOR()
algorithm
(ty_call, ty, var) := typeArrayConstructor(call, origin, info);
then
Expression.CALL(ty_call);
case UNTYPED_REDUCTION()
algorithm
(ty_call, ty, var) := typeReduction(call, origin, info);
then
Expression.CALL(ty_call);
case TYPED_CALL()
algorithm
ty := call.ty;
var := call.var;
then
callExp;
case TYPED_ARRAY_CONSTRUCTOR()
algorithm
ty := call.ty;
var := call.var;
then
callExp;
case TYPED_REDUCTION()
algorithm
ty := call.ty;
var := call.var;
then
callExp;
else
algorithm
Error.assertion(false, getInstanceName() + ": " + Expression.toString(callExp), sourceInfo());
then fail();
end match;
end typeCall;
function typeNormalCall
input output NFCall call;
input ExpOrigin.Type origin;
input SourceInfo info;
algorithm
call := match call
local
list<Function> fnl;
Boolean is_external;
case UNTYPED_CALL()
algorithm
fnl := Function.typeRefCache(call.ref);
then
typeArgs(call, origin, info);
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid function call expression", sourceInfo());
then
fail();
end match;
end typeNormalCall;
function makeTypedCall
input Function fn;
input list<Expression> args;
input Variability variability;
input Type returnType = fn.returnType;
output NFCall call;
protected
NFCallAttributes ca;
algorithm
ca := NFCallAttributes.CALL_ATTR(
Type.isTuple(returnType),
Function.isBuiltin(fn),
Function.isImpure(fn),
Function.isFunctionPointer(fn),
Function.inlineBuiltin(fn),
DAE.NO_TAIL()
);
call := TYPED_CALL(fn, returnType, variability, args, ca);
end makeTypedCall;
function unboxArgs
input output NFCall call;
algorithm
() := match call
case TYPED_CALL()
algorithm
call.arguments := list(Expression.unbox(arg) for arg in call.arguments);
then
();
end match;
end unboxArgs;
function typeMatchNormalCall
input output NFCall call;
input ExpOrigin.Type origin;
input SourceInfo info;
protected
NFCall argtycall;
algorithm
argtycall := typeNormalCall(call, origin, info);
call := matchTypedNormalCall(argtycall, origin, info);
end typeMatchNormalCall;
function matchTypedNormalCall
input output NFCall call;
input ExpOrigin.Type origin;
input SourceInfo info;
protected
Function func;
list<Expression> args;
list<TypedArg> typed_args;
MatchedFunction matchedFunc;
InstNode scope;
Variability var, arg_var;
Type ty;
Expression arg_exp;
algorithm
ARG_TYPED_CALL(call_scope = scope) := call;
matchedFunc := checkMatchingFunctions(call,info);
func := matchedFunc.func;
typed_args := matchedFunc.args;
args := {};
var := Variability.CONSTANT;
for a in typed_args loop
(arg_exp, _, arg_var) := a;
args := arg_exp :: args;
var := Prefixes.variabilityMax(var, arg_var);
end for;
args := listReverseInPlace(args);
ty := Function.returnType(func);
// Hack to fix return type of some builtin functions.
if Type.isPolymorphic(ty) then
ty := getSpecialReturnType(func, args);
end if;
if var == Variability.PARAMETER and Function.isExternal(func) then
// Mark external functions with parameter expressions as non-structural,
// to avoid them being marked as structural unnecessarily.
var := Variability.NON_STRUCTURAL_PARAMETER;
elseif Type.isDiscrete(ty) and var == Variability.CONTINUOUS then
// Functions that return a discrete type, e.g. Integer, should probably be
// treated as implicitly discrete if the arguments are continuous.
var := Variability.IMPLICITLY_DISCRETE;
end if;
ty := evaluateCallType(ty, func, args);
call := makeTypedCall(func, args, var, ty);
// If the matching was a vectorized one then create a map call
// using the vectorization dim. This means going through each argument
// and subscipting it with an iterator for each dim and creating a map call.
if MatchedFunction.isVectorized(matchedFunc) then
call := vectorizeCall(call, matchedFunc.mk, scope, info);
end if;
end matchTypedNormalCall;
function typeOf
input NFCall call;
output Type ty;
algorithm
ty := match call
case TYPED_CALL() then call.ty;
case TYPED_ARRAY_CONSTRUCTOR() then call.ty;
case TYPED_REDUCTION() then call.ty;
else Type.UNKNOWN();
end match;
end typeOf;
function setType
input output NFCall call;
input Type ty;
algorithm
call := match call
case TYPED_CALL() algorithm call.ty := ty; then call;
case TYPED_ARRAY_CONSTRUCTOR() algorithm call.ty := ty; then call;
case TYPED_REDUCTION() algorithm call.ty := ty; then call;
end match;
end setType;
function variability
input NFCall call;
output Variability var;
algorithm
var := match call
local
Boolean var_set;
case UNTYPED_CALL()
algorithm
var_set := true;
if ComponentRef.isSimple(call.ref) then
var := match ComponentRef.firstName(call.ref)
case "change" then Variability.DISCRETE;
case "edge" then Variability.DISCRETE;
case "pre" then Variability.DISCRETE;
case "ndims" then Variability.PARAMETER;
case "cardinality" then Variability.PARAMETER;
else algorithm var_set := false; then Variability.CONTINUOUS;
end match;
end if;
if not var_set then
var := Expression.variabilityList(call.arguments);
for narg in call.named_args loop
var := Prefixes.variabilityMax(var, Expression.variability(Util.tuple22(narg)));
end for;
end if;
then
var;
case UNTYPED_ARRAY_CONSTRUCTOR() then Expression.variability(call.exp);
case UNTYPED_REDUCTION() then Expression.variability(call.exp);
case TYPED_CALL() then call.var;
case TYPED_ARRAY_CONSTRUCTOR() then call.var;
case TYPED_REDUCTION() then call.var;
else algorithm
Error.assertion(false, getInstanceName() + " got untyped call", sourceInfo());
then fail();
end match;
end variability;
function compare
input NFCall call1;
input NFCall call2;
output Integer comp;
algorithm
comp := match (call1, call2)
case (UNTYPED_CALL(), UNTYPED_CALL())
then ComponentRef.compare(call1.ref, call2.ref);
case (TYPED_CALL(), TYPED_CALL())
then AbsynUtil.pathCompare(Function.name(call1.fn), Function.name(call2.fn));
case (UNTYPED_CALL(), TYPED_CALL())
then AbsynUtil.pathCompare(ComponentRef.toPath(call1.ref), Function.name(call2.fn));
case (TYPED_CALL(), UNTYPED_CALL())
then AbsynUtil.pathCompare(Function.name(call1.fn), ComponentRef.toPath(call2.ref));
end match;
if comp == 0 then
comp := Expression.compareList(arguments(call1), arguments(call2));
end if;
end compare;
function isExternal
input NFCall call;
output Boolean isExternal;
algorithm
isExternal := match call
case UNTYPED_CALL() then Class.isExternalFunction(InstNode.getClass(ComponentRef.node(call.ref)));
case ARG_TYPED_CALL() then Class.isExternalFunction(InstNode.getClass(ComponentRef.node(call.ref)));
case TYPED_CALL() then Function.isExternal(call.fn);
else false;
end match;
end isExternal;
function isImpure
input NFCall call;
output Boolean isImpure;
algorithm
isImpure := match call
case UNTYPED_CALL() then Function.isImpure(listHead(Function.getRefCache(call.ref)));
case TYPED_CALL() then Function.isImpure(call.fn) or Function.isOMImpure(call.fn);
else false;
end match;
end isImpure;
function isRecordConstructor
input NFCall call;
output Boolean isConstructor;
algorithm
isConstructor := match call
case UNTYPED_CALL()
then SCodeUtil.isRecord(InstNode.definition(ComponentRef.node(call.ref)));
case TYPED_CALL()
then SCodeUtil.isRecord(InstNode.definition(call.fn.node));
else false;
end match;
end isRecordConstructor;
function isExternalObjectConstructor
input NFCall call;
output Boolean isConstructor;
algorithm
isConstructor := match call
// Only constructors may return external objects...
case TYPED_CALL()
then Type.isExternalObject(call.ty);
else false;
end match;
end isExternalObjectConstructor;
function inlineType
input NFCall call;
output DAE.InlineType inlineTy;
algorithm
inlineTy := match call
case TYPED_CALL(attributes = NFCallAttributes.CALL_ATTR(inlineType = inlineTy))
then inlineTy;
else DAE.InlineType.NO_INLINE();
end match;
end inlineType;
function typedFunction
input NFCall call;
output Function fn;
algorithm
fn := match call
case TYPED_CALL() then call.fn;
case TYPED_ARRAY_CONSTRUCTOR() then NFBuiltinFuncs.ARRAY_FUNC;
case TYPED_REDUCTION() then call.fn;
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped function", sourceInfo());
then
fail();
end match;
end typedFunction;
function functionName
input NFCall call;
output Absyn.Path name;
algorithm
name := match call
case UNTYPED_CALL() then ComponentRef.toPath(call.ref);
case ARG_TYPED_CALL() then ComponentRef.toPath(call.ref);
case TYPED_CALL() then Function.name(call.fn);
case UNTYPED_ARRAY_CONSTRUCTOR() then Absyn.IDENT("array");
case TYPED_ARRAY_CONSTRUCTOR() then Absyn.IDENT("array");
case UNTYPED_REDUCTION() then ComponentRef.toPath(call.ref);
case TYPED_REDUCTION() then Function.name(call.fn);
end match;
end functionName;
function arguments
input NFCall call;
output list<Expression> arguments;
algorithm
arguments := match call
case UNTYPED_CALL() then call.arguments;
case TYPED_CALL() then call.arguments;
end match;
end arguments;
function toRecordExpression
input NFCall call;
input Type ty;
output Expression exp;
algorithm
exp := match call
case TYPED_CALL()
then EvalFunction.evaluateRecordConstructor(call.fn, ty, call.arguments, evaluate = false);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown call", sourceInfo());
then
fail();
end match;
end toRecordExpression;
function toString
input NFCall call;
output String str;
protected
String name, arg_str,c;
Expression argexp;
list<InstNode> iters;
algorithm
str := match call
case UNTYPED_CALL()
algorithm
name := ComponentRef.toString(call.ref);
arg_str := stringDelimitList(list(Expression.toString(arg) for arg in call.arguments), ", ");
then
name + "(" + arg_str + ")";
case ARG_TYPED_CALL()
algorithm
name := ComponentRef.toString(call.ref);
arg_str := stringDelimitList(list(Expression.toString(Util.tuple31(arg)) for arg in call.arguments), ", ");
for arg in call.named_args loop
c := if arg_str == "" then "" else ", ";
arg_str := arg_str + c + Util.tuple41(arg) + " = " + Expression.toString(Util.tuple42(arg));
end for;
then
name + "(" + arg_str + ")";
case UNTYPED_ARRAY_CONSTRUCTOR()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
"{" + arg_str + " for " + c + "}";
case UNTYPED_REDUCTION()
algorithm
name := ComponentRef.toString(call.ref);
arg_str := Expression.toString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
name + "(" + arg_str + " for " + c + ")";
case TYPED_CALL()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := stringDelimitList(list(Expression.toString(arg) for arg in call.arguments), ", ");
then
name + "(" + arg_str + ")";
case TYPED_ARRAY_CONSTRUCTOR()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
"{" + arg_str + " for " + c + "}";
case TYPED_REDUCTION()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := Expression.toString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
name + "(" + arg_str + " for " + c + ")";
end match;
end toString;
function toFlatString
input NFCall call;
output String str;
protected
String name, arg_str,c;
Expression argexp;
list<InstNode> iters;
algorithm
str := match call
case TYPED_CALL()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := stringDelimitList(list(Expression.toFlatString(arg) for arg in call.arguments), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, ")"})
elseif isExternalObjectConstructor(call) then
stringAppendList({Type.toFlatString(call.ty), "(", arg_str, ")"})
else
stringAppendList({Util.makeQuotedIdentifier(name), "(", arg_str, ")"});
case TYPED_ARRAY_CONSTRUCTOR()
algorithm
if isVectorized(call) then
// Vectorized calls contains iterators with illegal Modelica names
// (to avoid name conflicts), to make the flat output legal such
// calls are reverted to their original form here.
str := toFlatString(devectorizeCall(call));
else
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toFlatString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
str := stringAppendList({"{", arg_str, " for ", c, "}"});
end if;
then
str;
case TYPED_REDUCTION()
algorithm
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
arg_str := Expression.toFlatString(call.exp);
c := stringDelimitList(list(InstNode.name(Util.tuple21(iter)) + " in " +
Expression.toFlatString(Util.tuple22(iter)) for iter in call.iters), ", ");
then
if Function.isBuiltin(call.fn) then
stringAppendList({name, "(", arg_str, " for ", c, ")"})
else
stringAppendList({Util.makeQuotedIdentifier(name), "(", arg_str, " for ", c, ")"});
end match;
end toFlatString;
function typedString
"Like toString, but prefixes each argument with its type as a comment."
input NFCall call;
output String str;
protected
String name, arg_str,c;
Expression argexp;
algorithm
str := match call
case ARG_TYPED_CALL()
algorithm
name := ComponentRef.toString(call.ref);
arg_str := stringDelimitList(list("/*" + Type.toString(Util.tuple32(arg)) + "*/ " +
Expression.toString(Util.tuple31(arg)) for arg in call.arguments), ", ");
for arg in call.named_args loop
c := if arg_str == "" then "" else ", ";
arg_str := arg_str + c + Util.tuple41(arg) + " = /*" +
Type.toString(Util.tuple43(arg)) + "*/ " + Expression.toString(Util.tuple42(arg));
end for;
then
name + "(" + arg_str + ")";
case TYPED_CALL()
algorithm
name := AbsynUtil.pathString(Function.name(call.fn));
arg_str := stringDelimitList(list(Expression.toStringTyped(arg) for arg in call.arguments), ", ");
then
name + "(" + arg_str + ")";
else toString(call);
end match;
end typedString;
function toDAE
input NFCall call;
output DAE.Exp daeCall;
algorithm
daeCall := match call
local
String fold_id, res_id;
Option<Expression> fold_exp;
case TYPED_CALL()
then DAE.CALL(
Function.nameConsiderBuiltin(call.fn),
list(Expression.toDAE(e) for e in call.arguments),
NFCallAttributes.toDAE(call.attributes, call.ty));
case TYPED_ARRAY_CONSTRUCTOR()
algorithm
fold_id := Util.getTempVariableIndex();
res_id := Util.getTempVariableIndex();
then
DAE.REDUCTION(
DAE.REDUCTIONINFO(
Function.name(NFBuiltinFuncs.ARRAY_FUNC),
Absyn.COMBINE(),
Type.toDAE(call.ty),
NONE(),
fold_id,
res_id,
NONE()),
Expression.toDAE(call.exp),
list(iteratorToDAE(iter) for iter in call.iters));
case TYPED_REDUCTION()
algorithm
(fold_exp, fold_id, res_id) := call.foldExp;
then
DAE.REDUCTION(
DAE.REDUCTIONINFO(
Function.name(call.fn),
Absyn.COMBINE(),
Type.toDAE(call.ty),
Expression.toDAEValueOpt(call.defaultExp),
fold_id,
res_id,
Expression.toDAEOpt(fold_exp)),
Expression.toDAE(call.exp),
list(iteratorToDAE(iter) for iter in call.iters));
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped call", sourceInfo());
then
fail();
end match;
end toDAE;
function isVectorizeable
input NFCall call;
output Boolean isVect;
algorithm
isVect := match call
local
String name;
case TYPED_CALL(fn = Function.FUNCTION(path = Absyn.IDENT(name = name)))
then match name
case "der" then false;
case "pre" then false;
case "previous" then false;
else true;
end match;
else true;
end match;
end isVectorizeable;
function retype
input output NFCall call;
algorithm
() := match call
local
Type ty;
list<Dimension> dims;
case TYPED_ARRAY_CONSTRUCTOR()
algorithm
dims := {};
for i in listReverse(call.iters) loop
dims := listAppend(Type.arrayDims(Expression.typeOf(Util.tuple22(i))), dims);
end for;
call.ty := Type.liftArrayLeftList(Type.arrayElementType(call.ty), dims);
then
();
else ();
end match;
end retype;
function typeCast
input output Expression callExp;
input Type ty;
protected
NFCall call;
Type cast_ty;
algorithm
Expression.CALL(call = call) := callExp;
callExp := match call
case TYPED_CALL() guard Function.isBuiltin(call.fn)
algorithm
cast_ty := Type.setArrayElementType(call.ty, ty);
then
match AbsynUtil.pathFirstIdent(Function.name(call.fn))
// For 'fill' we can type cast the first argument rather than the
// whole array that 'fill' constructs.
case "fill"
algorithm
call.arguments := Expression.typeCast(listHead(call.arguments), ty) ::
listRest(call.arguments);
call.ty := cast_ty;
then
Expression.CALL(call);
// For diagonal we can type cast the argument rather than the
// matrix that diagonal constructs.
case "diagonal"
algorithm
call.arguments := {Expression.typeCast(listHead(call.arguments), ty)};
call.ty := cast_ty;
then
Expression.CALL(call);
else Expression.CAST(cast_ty, callExp);
end match;
else Expression.CAST(Type.setArrayElementType(typeOf(call), ty), callExp);
end match;
end typeCast;
function containsExp
input Call call;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
res := match call
local
Expression e;
case UNTYPED_CALL()
algorithm
res := Expression.listContains(call.arguments, func);
if not res then
for arg in call.named_args loop
(_, e) := arg;
if Expression.contains(e, func) then
res := true;
break;
end if;
end for;
end if;
then
res;
case ARG_TYPED_CALL()
algorithm
for arg in call.arguments loop
(e, _, _) := arg;
if Expression.contains(e, func) then
res := true;
return;
end if;
end for;
for arg in call.named_args loop
(_, e, _, _) := arg;
if Expression.contains(e, func) then
res := true;
return;
end if;
end for;
then
false;
case TYPED_CALL() then Expression.listContains(call.arguments, func);
case UNTYPED_ARRAY_CONSTRUCTOR() then Expression.contains(call.exp, func);
case TYPED_ARRAY_CONSTRUCTOR() then Expression.contains(call.exp, func);
case UNTYPED_REDUCTION() then Expression.contains(call.exp, func);
case TYPED_REDUCTION() then Expression.contains(call.exp, func);
end match;
end containsExp;
function containsExpShallow
input Call call;
input ContainsPred func;
output Boolean res;
partial function ContainsPred
input Expression exp;
output Boolean res;
end ContainsPred;
algorithm
res := match call
local
Expression e;
case UNTYPED_CALL()
algorithm
res := Expression.listContainsShallow(call.arguments, func);
if not res then
for arg in call.named_args loop
(_, e) := arg;
if func(e) then
res := true;
break;
end if;
end for;
end if;
then
res;
case ARG_TYPED_CALL()
algorithm
for arg in call.arguments loop
(e, _, _) := arg;
if func(e) then
res := true;
return;
end if;
end for;
for arg in call.named_args loop
(_, e, _, _) := arg;
if func(e) then
res := true;
return;
end if;
end for;
then
false;
case TYPED_CALL() then Expression.listContainsShallow(call.arguments, func);
case UNTYPED_ARRAY_CONSTRUCTOR() then func(call.exp);
case TYPED_ARRAY_CONSTRUCTOR() then func(call.exp);
case UNTYPED_REDUCTION() then func(call.exp);
case TYPED_REDUCTION() then func(call.exp);
end match;
end containsExpShallow;
function applyExp
input Call call;
input ApplyFunc func;
partial function ApplyFunc
input Expression exp;
end ApplyFunc;
algorithm
() := match call
local
Expression e;
case UNTYPED_CALL()
algorithm
Expression.applyList(call.arguments, func);