/
NFCall.mo
1200 lines (1062 loc) · 37.2 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-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package NFCall
import Absyn;
import DAE;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFPrefixes.Variability;
import Type = NFType;
protected
import BuiltinCall = NFBuiltinCall;
import Ceval = NFCeval;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import ErrorExt;
import Inline = NFInline;
import Inst = NFInst;
import List;
import Lookup = NFLookup;
import MetaModelica.Dangerous.listReverseInPlace;
import NFBinding.Binding;
import NFClass.Class;
import NFComponent.Component;
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 Prefixes = NFPrefixes;
import TypeCheck = NFTypeCheck;
import Typing = NFTyping;
import Util;
public
uniontype CallAttributes
record CALL_ATTR
Type ty "The type of the return value, if several return values this is undefined";
Boolean tuple_ "tuple" ;
Boolean builtin "builtin Function call" ;
Boolean isImpure "if the function has prefix *impure* is true, else false";
Boolean isFunctionPointerCall;
DAE.InlineType inlineType;
DAE.TailCall tailCall "Input variables of the function if the call is tail-recursive";
end CALL_ATTR;
function toDAE
input CallAttributes attr;
output DAE.CallAttributes fattr;
algorithm
fattr := DAE.CALL_ATTR(Type.toDAE(attr.ty), attr.tuple_, attr.builtin,
attr.isImpure, attr.isFunctionPointerCall, attr.inlineType, attr.tailCall);
end toDAE;
end CallAttributes;
protected
type ParameterTree = ParameterTreeImpl.Tree;
encapsulated package ParameterTreeImpl
import BaseAvlTree;
import Expression = NFExpression;
extends BaseAvlTree(redeclare type Key = String,
redeclare type Value = Expression);
redeclare function extends keyStr
algorithm
outString := inKey;
end keyStr;
redeclare function extends valueStr
algorithm
outString := Expression.toString(inValue);
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := stringCompare(inKey1, inKey2);
end keyCompare;
annotation(__OpenModelica_Interface="util");
end ParameterTreeImpl;
public
uniontype Call
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;
CallAttributes attributes;
end TYPED_CALL;
// Right now this represents only array() calls.
// Any other mapping call e.g. F(i for i in ...) is converted to
// array(F(i) for i in ...) at instIteratorCall().
// So the fn is always NFBuiltinFuncs.ARRAY_FUNC.
// Note that F(i for i in ...) only allows
// calling functions with just one argument according to the current
// grammar anyway. array(F(i,j) for i in ..) makes this multi calls possible
// in Modelica code.
// If you need to have more mapping calls e.g list() at some point just add them
// and make use of fn;
record UNTYPED_MAP_CALL
// Function fn;
Expression exp;
list<tuple<InstNode, Expression>> iters;
end UNTYPED_MAP_CALL;
record TYPED_MAP_CALL
// Function fn;
Type ty;
Variability var;
Expression exp;
list<tuple<InstNode, Expression>> iters;
end TYPED_MAP_CALL;
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
Call call;
list<Expression> args;
ComponentRef cref;
algorithm
outExp := match callExp
case Expression.CALL(UNTYPED_CALL(ref = cref))
algorithm
if(BuiltinCall.needSpecialHandling(callExp.call)) then
(outExp, ty, var) := BuiltinCall.typeSpecial(callExp.call, origin, info);
else
call := typeMatchNormalCall(callExp.call, origin, info);
ty := typeOf(call);
var := variability(call);
if isRecordConstructor(call) then
outExp := toRecordExpression(call, ty);
else
outExp := Expression.CALL(call);
outExp := Inline.inlineCallExp(outExp);
end if;
end if;
then
outExp;
case Expression.CALL(UNTYPED_MAP_CALL())
algorithm
call := typeMapIteratorCall(callExp.call, origin, info);
ty := typeOf(call);
var := variability(call);
then
Expression.CALL(call);
case Expression.CALL(call as TYPED_CALL())
algorithm
ty := call.ty;
var := call.var;
then
callExp;
case Expression.CALL(call as TYPED_MAP_CALL())
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 Call 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);
// Don't evaluate constants or structural parameters for external functions,
// the code generation can't handle it in some cases (see bug #4904).
// TODO: Remove this when #4904 is fixed.
is_external := if listEmpty(fnl) then false else Function.isExternal(listHead(fnl));
then
typeArgs(call, not is_external, 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 Call call;
protected
CallAttributes ca;
algorithm
ca := CallAttributes.CALL_ATTR(
returnType,
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 Call 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 Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
protected
Call argtycall;
algorithm
argtycall := typeNormalCall(call, origin, info);
call := matchTypedNormalCall(argtycall, origin, info);
end typeMatchNormalCall;
function matchTypedNormalCall
input output Call 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 intBitAnd(origin, ExpOrigin.FUNCTION) == 0 then
ty := evaluateCallType(ty, func, args);
end if;
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 Call call;
output Type ty;
algorithm
ty := match call
case TYPED_CALL() then call.ty;
case TYPED_MAP_CALL() then call.ty;
else Type.UNKNOWN();
end match;
end typeOf;
function setType
input output Call call;
input Type ty;
algorithm
call := match call
case TYPED_CALL() algorithm call.ty := ty; then call;
case TYPED_MAP_CALL() algorithm call.ty := ty; then call;
end match;
end setType;
function variability
input Call 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_MAP_CALL() then Expression.variability(call.exp);
case TYPED_CALL() then call.var;
case TYPED_MAP_CALL() then call.var;
else algorithm
Error.assertion(false, getInstanceName() + " got untyped call", sourceInfo());
then fail();
end match;
end variability;
function compare
input Call call1;
input Call 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 Absyn.pathCompare(Function.name(call1.fn), Function.name(call2.fn));
case (UNTYPED_CALL(), TYPED_CALL())
then Absyn.pathCompare(ComponentRef.toPath(call1.ref), Function.name(call2.fn));
case (TYPED_CALL(), UNTYPED_CALL())
then Absyn.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 Call 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 isRecordConstructor
input Call call;
output Boolean isConstructor;
algorithm
isConstructor := match call
case UNTYPED_CALL()
then SCode.isRecord(InstNode.definition(ComponentRef.node(call.ref)));
case TYPED_CALL()
then SCode.isRecord(InstNode.definition(call.fn.node));
else false;
end match;
end isRecordConstructor;
function inlineType
input Call call;
output DAE.InlineType inlineTy;
algorithm
inlineTy := match call
case TYPED_CALL(attributes = CallAttributes.CALL_ATTR(inlineType = inlineTy))
then inlineTy;
else DAE.InlineType.NO_INLINE();
end match;
end inlineType;
function typedFunction
input Call call;
output Function fn;
algorithm
fn := match call
case TYPED_CALL() then call.fn;
case TYPED_MAP_CALL() then NFBuiltinFuncs.ARRAY_FUNC;
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped function", sourceInfo());
then
fail();
end match;
end typedFunction;
function arguments
input Call 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 Call call;
input Type ty;
output Expression exp;
algorithm
exp := match call
case TYPED_CALL()
then Expression.RECORD(Function.name(call.fn), ty, call.arguments);
end match;
end toRecordExpression;
function toString
input Call 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_MAP_CALL()
algorithm
name := Absyn.pathString(Function.name(NFBuiltinFuncs.ARRAY_FUNC));
arg_str := Expression.toString(call.exp);
then
name + "(" + arg_str + ")";
case TYPED_CALL()
algorithm
name := Absyn.pathString(Function.name(call.fn));
arg_str := stringDelimitList(list(Expression.toString(arg) for arg in call.arguments), ", ");
then
name + "(" + arg_str + ")";
case TYPED_MAP_CALL()
algorithm
name := Absyn.pathString(Function.name(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
name + "(" + arg_str + " for " + c + ")";
end match;
end toString;
function typedString
"Like toString, but prefixes each argument with its type as a comment."
input Call 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 := Absyn.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 Call call;
output DAE.Exp daeCall;
algorithm
daeCall := match call
case TYPED_CALL()
then DAE.CALL(
Function.nameConsiderBuiltin(call.fn),
list(Expression.toDAE(e) for e in call.arguments),
CallAttributes.toDAE(call.attributes));
case TYPED_MAP_CALL()
then DAE.REDUCTION(
DAE.REDUCTIONINFO(
Function.name(NFBuiltinFuncs.ARRAY_FUNC),
Absyn.COMBINE(),
Type.toDAE(call.ty),
NONE(),
String(Util.getTempVariableIndex()),
String(Util.getTempVariableIndex()),
NONE()),
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;
protected
function instNormalCall
input Absyn.ComponentRef functionName;
input Absyn.FunctionArgs functionArgs;
input InstNode scope;
input SourceInfo info;
output Expression callExp;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
algorithm
(args, named_args) := instArgs(functionArgs, scope, info);
callExp := match Absyn.crefFirstIdent(functionName)
// size creates Expression.SIZE instead of Expression.CALL.
case "size" then BuiltinCall.makeSizeExp(args, named_args, info);
// array() call with no iterators creates Expression.ARRAY instead of Expression.CALL.
// If it had iterators then it will not reach here. The args would have been parsed to
// Absyn.FOR_ITER_FARG and that is handled in instIteratorCall.
case "array" then BuiltinCall.makeArrayExp(args, named_args, info);
else algorithm
(fn_ref, _, _) := Function.instFunc(functionName,scope,info);
then
Expression.CALL(UNTYPED_CALL(fn_ref, args, named_args, scope));
end match;
end instNormalCall;
function instArgs
input Absyn.FunctionArgs args;
input InstNode scope;
input SourceInfo info;
output list<Expression> posArgs;
output list<NamedArg> namedArgs;
algorithm
(posArgs, namedArgs) := match args
case Absyn.FUNCTIONARGS()
algorithm
posArgs := list(Inst.instExp(a, scope, info) for a in args.args);
namedArgs := list(instNamedArg(a, scope, info) for a in args.argNames);
then
(posArgs, namedArgs);
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown function args", sourceInfo());
then
fail();
end match;
end instArgs;
function instNamedArg
input Absyn.NamedArg absynArg;
input InstNode scope;
input SourceInfo info;
output NamedArg arg;
protected
String name;
Absyn.Exp exp;
algorithm
Absyn.NAMEDARG(argName = name, argValue = exp) := absynArg;
arg := (name, Inst.instExp(exp, scope, info));
end instNamedArg;
function instIteratorCall
input Absyn.ComponentRef functionName;
input Absyn.FunctionArgs functionArgs;
input InstNode scope;
input SourceInfo info;
output Expression callExp;
protected
ComponentRef fn_ref, arr_fn_ref;
Expression exp;
list<tuple<InstNode, Expression>> iters;
Call call;
Boolean is_builtin_reduction, is_array;
algorithm
(is_builtin_reduction, is_array) := match Absyn.crefFirstIdent(functionName)
case "$array" then (false, true);
case "array" then (false, true);
case "min" then (true, false);
case "max" then (true, false);
case "sum" then (true, false);
case "product" then (true, false);
else (false, false);
end match;
(exp, iters) := instIteratorCallArgs(functionArgs, scope, info);
// If it is one of the builtin functions above the call operates as a "reduction"
// (think of it like just a call to the overload of the function that takes array as argument.)
// We handle it by making a call to the builtin function with an array as argument.
// Which is valid since all these builtin functions accept array arguments anyway.
if is_builtin_reduction then
// start by making an array map call.
call := UNTYPED_MAP_CALL(exp, iters);
// wrap the array call in the given function
// e.g. sum(array(i for i in ...)).
fn_ref := Function.instFunc(functionName, scope, info);
call := UNTYPED_CALL(fn_ref, {Expression.CALL(call)}, {}, scope);
else
// Otherwise, make an array call with the original function call as an argument.
// But only if the original function is not array() itself.
// e.g. Change myfunc(i for i in ...) TO array(myfunc(i) for i in ...).
if not is_array then
fn_ref := Function.instFunc(functionName, scope, info);
call := UNTYPED_CALL(fn_ref, {exp}, {}, scope);
exp := Expression.CALL(call);
end if;
call := UNTYPED_MAP_CALL(exp, iters);
end if;
callExp := Expression.CALL(call);
end instIteratorCall;
function instIteratorCallArgs
input Absyn.FunctionArgs args;
input InstNode scope;
input SourceInfo info;
output Expression exp;
output list<tuple<InstNode, Expression>> iters;
algorithm
_ := match args
local
InstNode for_scope;
case Absyn.FOR_ITER_FARG()
algorithm
(for_scope, iters) := instIterators(args.iterators, scope, info);
exp := Inst.instExp(args.exp, for_scope, info);
then
();
end match;
end instIteratorCallArgs;
function instIterators
input list<Absyn.ForIterator> inIters;
input InstNode scope;
input SourceInfo info;
output InstNode outScope = scope;
output list<tuple<InstNode, Expression>> outIters = {};
protected
Expression range;
InstNode iter;
algorithm
for i in inIters loop
range := Inst.instExp(Util.getOption(i.range), outScope, info);
(outScope, iter) := Inst.addIteratorToScope(i.name, outScope, info);
outIters := (iter, range) :: outIters;
end for;
outIters := listReverse(outIters);
end instIterators;
function typeMapIteratorCall
input output Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Type ty;
output Variability variability;
protected
Expression arg, range;
Type iter_ty;
Binding binding;
Variability iter_var;
InstNode iter;
list<Dimension> dims = {};
list<tuple<InstNode, Expression>> iters = {};
algorithm
(call, ty, variability) := match call
// This is always a call to the function array()/$array(). See instIteratorCall.
// Other mapping function calls are already wrapped by array() at this point.
case UNTYPED_MAP_CALL()
algorithm
variability := Variability.CONSTANT;
for i in call.iters loop
(iter, range) := i;
(range, iter_ty, iter_var) := Typing.typeIterator(iter, range, origin, structural = false);
dims := listAppend(Type.arrayDims(iter_ty), dims);
variability := Variability.variabilityMax(variability, iter_var);
iters := (iter, range) :: iters;
end for;
iters := listReverseInPlace(iters);
(arg, ty) := Typing.typeExp(call.exp, origin, info);
ty := Type.liftArrayLeftList(ty, dims);
then
(TYPED_MAP_CALL(ty, variability, arg, iters), ty, variability);
else
algorithm
Error.assertion(false, getInstanceName() + " got invalid function call expression", sourceInfo());
then
fail();
end match;
end typeMapIteratorCall;
function typeArgs
input output Call call;
input Boolean replaceConstants;
input ExpOrigin.Type origin;
input SourceInfo info;
algorithm
call := match call
local
Expression arg;
Type arg_ty;
Variability arg_var;
list<TypedArg> typedArgs;
list<TypedNamedArg> typedNamedArgs;
String name;
case UNTYPED_CALL()
algorithm
typedArgs := {};
for arg in call.arguments loop
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info, replaceConstants = replaceConstants);
typedArgs := (arg, arg_ty, arg_var) :: typedArgs;
end for;
typedArgs := listReverse(typedArgs);
typedNamedArgs := {};
for narg in call.named_args loop
(name,arg) := narg;
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info, replaceConstants = replaceConstants);
typedNamedArgs := (name, arg, arg_ty, arg_var) :: typedNamedArgs;
end for;
typedNamedArgs := listReverse(typedNamedArgs);
then
ARG_TYPED_CALL(call.ref, typedArgs, typedNamedArgs, call.call_scope);
end match;
end typeArgs;
function checkMatchingFunctions
input Call call;
input SourceInfo info;
output MatchedFunction matchedFunc;
protected
list<MatchedFunction> matchedFunctions, exactMatches;
Function func;
list<Function> allfuncs;
InstNode fn_node;
Integer numerr = Error.getNumErrorMessages();
list<Integer> errors;
algorithm
ErrorExt.setCheckpoint("NFCall:checkMatchingFunctions");
matchedFunctions := {};
_ := match call
case ARG_TYPED_CALL(ref = ComponentRef.CREF(node = fn_node)) algorithm
allfuncs := Function.getCachedFuncs(fn_node);
matchedFunctions := Function.matchFunctions(allfuncs, call.arguments, call.named_args, info);
then
();
end match;
if listEmpty(matchedFunctions) then
// Don't show error messages for overloaded functions, it leaks
// implementation details and usually doesn't provide any more info than
// what the "no match found" error gives anyway.
if listLength(allfuncs) > 1 then
ErrorExt.rollBack("NFCall:checkMatchingFunctions");
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{typedString(call), Function.candidateFuncListString(allfuncs)}, info);
// Only show the error message for no matching functions if no other error
// was shown.
// functions that for some reason failed to match without giving any error.
elseif numerr == Error.getNumErrorMessages() then
ErrorExt.rollBack("NFCall:checkMatchingFunctions");
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{typedString(call), Function.candidateFuncListString(allfuncs)}, info);
else
ErrorExt.delCheckpoint("NFCall:checkMatchingFunctions");
end if;
fail();
end if;
// If we have at least one matching function then we discard all error messages
// about matching. We have one matching func if we reach here.
ErrorExt.rollBack("NFCall:checkMatchingFunctions");
if listLength(matchedFunctions) == 1 then
matchedFunc ::_ := matchedFunctions;
// Overwrite the actuall function name with the overload name
// for builtin functions.
if Function.isBuiltin(matchedFunc.func) then
func := matchedFunc.func;
func.path := Function.nameConsiderBuiltin(func);
matchedFunc.func := func;
end if;
return;
end if;
if listLength(matchedFunctions) > 1 then
exactMatches := MatchedFunction.getExactMatches(matchedFunctions);
if listLength(exactMatches) == 1 then
matchedFunc ::_ := exactMatches;
// Overwrite the actuall function name with the overload name
// for builtin functions.
if Function.isBuiltin(matchedFunc.func) then
func := matchedFunc.func;
func.path := Function.nameConsiderBuiltin(func);
matchedFunc.func := func;
end if;
return;
else
matchedFunctions := resolveOverloadedVsDefaultConstructorAmbigutiy(matchedFunctions);
if listLength(matchedFunctions) == 1 then
matchedFunc ::_ := matchedFunctions;
return;
else
Error.addSourceMessage(Error.AMBIGUOUS_MATCHING_FUNCTIONS_NFINST,
{typedString(call), Function.candidateFuncListString(list(mfn.func for mfn in matchedFunctions))}, info);
fail();
end if;
end if;
end if;
end checkMatchingFunctions;
function resolveOverloadedVsDefaultConstructorAmbigutiy
input list<MatchedFunction> matchedFunctions;
output list<MatchedFunction> outMatches;
algorithm
// We have at least two exact matches. find the default constructor (if there is one) and remove it from the list
// so that it
// - doesn't cause ambiguities if there is only one other match left OR
// - it doesn't appear in the error messages in the case of more than one overloaded constructor matches.
outMatches := list(m for m guard not Function.isDefaultRecordConstructor(m.func) in matchedFunctions);
end resolveOverloadedVsDefaultConstructorAmbigutiy;
function iteratorToDAE
input tuple<InstNode, Expression> iter;
output DAE.ReductionIterator diter;
protected
InstNode iter_node;
Expression iter_range;
Component c;
Binding b;
algorithm
(iter_node, iter_range) := iter;
diter := DAE.REDUCTIONITER(InstNode.name(iter_node), Expression.toDAE(iter_range), NONE(),
Type.toDAE(Expression.typeOf(iter_range)));
end iteratorToDAE;