-
Notifications
You must be signed in to change notification settings - Fork 298
/
NFBuiltinCall.mo
2222 lines (1945 loc) · 75.8 KB
/
NFBuiltinCall.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 NFBuiltinCall
import Absyn;
import AbsynUtil;
import Call = NFCall;
import NFCallAttributes;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFPrefixes.Variability;
import Type = NFType;
import Subscript = NFSubscript;
protected
import Config;
import Ceval = NFCeval;
import ComponentRef = NFComponentRef;
import Dimension = NFDimension;
import List;
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 Prefixes = NFPrefixes;
import TypeCheck = NFTypeCheck;
import Typing = NFTyping;
import Util;
import ExpandExp = NFExpandExp;
import Operator = NFOperator;
import Component = NFComponent;
import NFPrefixes.ConnectorType;
import ClockKind = NFClockKind;
import Structural = NFStructural;
public
function needSpecialHandling
input Call call;
output Boolean special;
algorithm
() := match call
case Call.UNTYPED_CALL()
algorithm
CachedData.FUNCTION(specialBuiltin = special) :=
InstNode.getFuncCache(InstNode.classScope(ComponentRef.node(call.ref)));
then
();
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown call: " +
Call.toString(call), sourceInfo());
then
fail();
end match;
end needSpecialHandling;
function typeSpecial
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef cref;
InstNode fn_node;
Expression first;
list<Expression> rest;
String name;
ExpOrigin.Type next_origin;
algorithm
Call.UNTYPED_CALL(ref = cref) := call;
next_origin := ExpOrigin.setFlag(origin, ExpOrigin.SUBEXPRESSION);
(callExp, ty, variability) := match ComponentRef.firstName(cref)
//case "activeState" guard Config.synchronousFeaturesAllowed() then typeActiveStateCall(call, next_origin, info);
case "actualStream" then typeActualInStreamCall("actualStream", call, next_origin, info);
case "backSample" guard Config.synchronousFeaturesAllowed() then typeBackSampleCall(call, next_origin, info);
case "branch" then typeBranchCall(call, next_origin, info);
case "cardinality" then typeCardinalityCall(call, next_origin, info);
case "cat" then typeCatCall(call, next_origin, info);
case "change" then typeChangeCall(call, next_origin, info);
case "Clock" guard Config.synchronousFeaturesAllowed() then typeClockCall(call, next_origin, info);
case "der" then typeDerCall(call, next_origin, info);
case "diagonal" then typeDiagonalCall(call, next_origin, info);
case "DynamicSelect" then typeDynamicSelectCall("DynamicSelect", call, next_origin, info);
case "edge" then typeEdgeCall(call, next_origin, info);
case "fill" then typeFillCall(call, next_origin, info);
case "getInstanceName" then typeGetInstanceName(call);
//case "hold" guard Config.synchronousFeaturesAllowed() then typeHoldCall(call, next_origin, info);
//case "initialState" guard Config.synchronousFeaturesAllowed() then typeInitialStateCall(call, next_origin, info);
case "initial" then typeDiscreteCall(call, next_origin, info);
case "inStream" then typeActualInStreamCall("inStream", call, next_origin, info);
case "isRoot" then typeIsRootCall(call, next_origin, info);
case "matrix" then typeMatrixCall(call, next_origin, info);
case "max" then typeMinMaxCall("max", call, next_origin, info);
case "min" then typeMinMaxCall("min", call, next_origin, info);
case "ndims" then typeNdimsCall(call, next_origin, info);
//case "noClock" guard Config.synchronousFeaturesAllowed() then typeNoClockCall(call, next_origin, info);
case "noEvent" then typeNoEventCall(call, next_origin, info);
case "ones" then typeZerosOnesCall("ones", call, next_origin, info);
case "potentialRoot" then typePotentialRootCall(call, next_origin, info);
case "pre" then typePreCall(call, next_origin, info);
case "product" then typeProductCall(call, next_origin, info);
case "promote" then typePromoteCall(call, next_origin, info);
case "rooted" then typeRootedCall(call, next_origin, info);
case "root" then typeRootCall(call, next_origin, info);
case "sample" then typeSampleCall(call, next_origin, info);
case "scalar" then typeScalarCall(call, next_origin, info);
case "shiftSample" guard Config.synchronousFeaturesAllowed() then typeShiftSampleCall(call, next_origin, info);
case "smooth" then typeSmoothCall(call, next_origin, info);
case "String" then typeStringCall(call, next_origin, info);
case "subSample" guard Config.synchronousFeaturesAllowed() then typeSubSampleCall(call, next_origin, info);
case "sum" then typeSumCall(call, next_origin, info);
case "superSample" guard Config.synchronousFeaturesAllowed() then typeSuperSampleCall(call, next_origin, info);
case "symmetric" then typeSymmetricCall(call, next_origin, info);
case "terminal" then typeDiscreteCall(call, next_origin, info);
//case "ticksInState" guard Config.synchronousFeaturesAllowed() then typeTicksInStateCall(call, next_origin, info);
//case "timeInState" guard Config.synchronousFeaturesAllowed() then typeTimeInStateCall(call, next_origin, info);
//case "transition" guard Config.synchronousFeaturesAllowed() then typeTransitionCall(call, next_origin, info);
case "transpose" then typeTransposeCall(call, next_origin, info);
case "uniqueRootIndices" then typeUniqueRootIndicesCall(call, next_origin, info);
case "uniqueRoot" then typeUniqueRootCall(call, next_origin, info);
case "vector" then typeVectorCall(call, next_origin, info);
case "zeros" then typeZerosOnesCall("zeros", call, next_origin, info);
else
algorithm
Error.assertion(false, getInstanceName() + " got unhandled builtin function: " + Call.toString(call), sourceInfo());
then
fail();
end match;
end typeSpecial;
function makeSizeExp
input list<Expression> posArgs;
input list<NamedArg> namedArgs;
input SourceInfo info;
output Expression callExp;
protected
Integer argc = listLength(posArgs);
Expression arg1, arg2;
algorithm
assertNoNamedParams("size", namedArgs, info);
callExp := match posArgs
case {arg1} then Expression.SIZE(arg1, NONE());
case {arg1, arg2} then Expression.SIZE(arg1, SOME(arg2));
else
algorithm
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{"size" + List.toString(posArgs, Expression.toString, "", "(", ", ", ")", true),
"size(Any[:, ...]) => Integer[:]\n size(Any[:, ...], Integer) => Integer"}, info);
then
fail();
end match;
end makeSizeExp;
function makeArrayExp
input list<Expression> posArgs;
input list<NamedArg> namedArgs;
input SourceInfo info;
output Expression arrayExp;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Type ty;
algorithm
assertNoNamedParams("array", namedArgs, info);
// array can take any number of arguments, but needs at least one.
if listEmpty(posArgs) then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{"array" + List.toString(posArgs, Expression.toString, "", "(", ", ", ")", true),
"array(Any, Any, ...) => Any[:]"}, info);
fail();
end if;
arrayExp := Expression.makeArray(Type.UNKNOWN(), posArgs);
end makeArrayExp;
function makeCatExp
input Integer n;
input list<Expression> args;
input list<Type> tys;
input Variability variability;
input SourceInfo info;
output Expression callExp;
output Type ty;
protected
Expression arg2;
list<Expression> args2 = {}, res = {};
list<Type> tys2 = tys, tys3;
list<list<Dimension>> dimsLst = {};
list<Dimension> dims;
Type resTy = Type.UNKNOWN(), ty1, ty2, resTyToMatch;
TypeCheck.MatchKind mk;
Integer maxn, pos;
Dimension sumDim;
algorithm
Error.assertion(listLength(args)==listLength(tys) and listLength(args)>=1, getInstanceName() + " got wrong input sizes", sourceInfo());
// First: Get the number of dimensions and the element type
for arg in args loop
ty::tys2 := tys2;
dimsLst := Type.arrayDims(ty) :: dimsLst;
if Type.isEqual(resTy, Type.UNKNOWN()) then
resTy := Type.arrayElementType(ty);
else
(,, ty1, mk) := TypeCheck.matchExpressions(Expression.INTEGER(0), Type.arrayElementType(ty), Expression.INTEGER(0), resTy);
if TypeCheck.isCompatibleMatch(mk) then
resTy := ty1;
end if;
end if;
end for;
maxn := max(listLength(d) for d in dimsLst);
if maxn <> min(listLength(d) for d in dimsLst) then
Error.addSourceMessageAndFail(Error.NF_DIFFERENT_NUM_DIM_IN_ARGUMENTS, {stringDelimitList(list(String(listLength(d)) for d in dimsLst), ", "), "cat"}, info);
end if;
if n < 1 or n > maxn then
Error.addSourceMessageAndFail(Error.NF_CAT_WRONG_DIMENSION, {String(maxn), String(n)}, info);
end if;
tys2 := tys;
tys3 := {};
args2 := {};
pos := listLength(args)+2;
// Second: Try to match the element type of all the arguments
for arg in args loop
ty::tys2 := tys2;
pos := pos-1;
ty2 := Type.setArrayElementType(ty, resTy);
(arg2, ty1, mk) := TypeCheck.matchTypes(ty, ty2, arg, allowUnknown = true);
if TypeCheck.isIncompatibleMatch(mk) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH, {String(pos), "cat", "arg", Expression.toString(arg), Type.toString(ty), Type.toString(ty2)}, info);
end if;
args2 := arg2 :: args2;
tys3 := ty1 :: tys3;
end for;
// Third: We now have matched the element types of all arguments
// Try to match the dimensions as well
resTy := Type.UNKNOWN();
tys2 := tys3;
for arg in args2 loop
ty::tys2 := tys2;
if Type.isEqual(resTy, Type.UNKNOWN()) then
resTy := ty;
else
(,, ty1, mk) := TypeCheck.matchExpressions(Expression.INTEGER(0), ty, Expression.INTEGER(0), resTy);
if TypeCheck.isCompatibleMatch(mk) then
resTy := ty1;
end if;
end if;
end for;
// Got the supertype of the dimensions; trying to match all arguments
// with the concatenated dimension set to unknown.
dims := Type.arrayDims(resTy);
resTyToMatch := Type.ARRAY(Type.arrayElementType(resTy), List.set(dims, n, Dimension.UNKNOWN()));
dims := list(listGet(lst, n) for lst in dimsLst);
sumDim := Dimension.fromInteger(0);
for d in dims loop
// Create the concatenated dimension
sumDim := Dimension.add(sumDim, d);
end for;
resTy := Type.ARRAY(Type.arrayElementType(resTy), List.set(Type.arrayDims(resTy), n, sumDim));
tys2 := tys3;
tys3 := {};
res := {};
pos := listLength(args)+2;
for arg in args2 loop
ty::tys2 := tys2;
pos := pos-1;
(arg2, ty1, mk) := TypeCheck.matchTypes(ty, resTyToMatch, arg, allowUnknown=true);
if TypeCheck.isIncompatibleMatch(mk) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH, {String(pos), "cat", "arg", Expression.toString(arg), Type.toString(ty), Type.toString(resTyToMatch)}, info);
end if;
res := arg2 :: res;
tys3 := ty1 :: tys3;
end for;
// We have all except dimension n having equal sizes; with matching types
ty := resTy;
callExp := Expression.CALL(Call.makeTypedCall(NFBuiltinFuncs.CAT, Expression.INTEGER(n)::res, variability, resTy));
end makeCatExp;
protected
function assertNoNamedParams
input String fnName;
input list<NamedArg> namedArgs;
input SourceInfo info;
algorithm
if not listEmpty(namedArgs) then
Error.addSourceMessage(Error.NO_SUCH_PARAMETER,
{fnName, Util.tuple21(listHead(namedArgs))}, info);
fail();
end if;
end assertNoNamedParams;
function typeStringCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type outType;
output Variability var;
protected
Type arg_ty;
list<TypedArg> args;
list<TypedNamedArg> named_args;
Call ty_call;
algorithm
ty_call as Call.ARG_TYPED_CALL(_, args, named_args) := Call.typeNormalCall(call, origin, info);
(_, arg_ty, _) :: _ := args;
arg_ty := Type.arrayElementType(arg_ty);
if Type.isComplex(arg_ty) then
(callExp, outType, var) := typeOverloadedStringCall(arg_ty, args, named_args, ty_call, origin, info);
else
(callExp, outType, var) := typeBuiltinStringCall(ty_call, origin, info);
end if;
end typeStringCall;
function typeBuiltinStringCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability var;
protected
Call ty_call;
algorithm
ty_call := Call.matchTypedNormalCall(call, origin, info);
ty := Call.typeOf(ty_call);
var := Call.variability(ty_call);
callExp := Expression.CALL(ty_call);
end typeBuiltinStringCall;
function typeOverloadedStringCall
input Type overloadedType;
input list<TypedArg> args;
input list<TypedNamedArg> namedArgs;
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type outType;
output Variability var = Variability.CONSTANT;
protected
ComponentRef fn_ref;
list<Function> candidates;
InstNode recopnode;
MatchedFunction matchedFunc;
list<MatchedFunction> matchedFunctions, exactMatches;
algorithm
Type.COMPLEX(cls=recopnode) := overloadedType;
try
fn_ref := Function.lookupFunctionSimple("'String'", recopnode);
else
// If there's no 'String' overload, let the normal String handler print the error.
typeBuiltinStringCall(call, origin, info);
fail();
end try;
fn_ref := Function.instFunctionRef(fn_ref, InstNode.info(recopnode));
candidates := Function.typeRefCache(fn_ref);
//for fn in candidates loop
// TypeCheck.checkValidOperatorOverload("'String'", fn, recopnode);
//end for;
matchedFunctions := Function.matchFunctionsSilent(candidates, args, namedArgs, info);
exactMatches := MatchedFunction.getExactMatches(matchedFunctions);
if listEmpty(exactMatches) then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.typedString(call), Function.candidateFuncListString(candidates)}, info);
fail();
end if;
if listLength(exactMatches) == 1 then
matchedFunc ::_ := exactMatches;
outType := Function.returnType(matchedFunc.func);
for arg in matchedFunc.args loop
var := Prefixes.variabilityMax(var, Util.tuple33(arg));
end for;
callExp := Expression.CALL(
Call.makeTypedCall(
matchedFunc.func,
list(Util.tuple31(a) for a in matchedFunc.args),
var,
outType));
return;
else
Error.addSourceMessage(Error.AMBIGUOUS_MATCHING_FUNCTIONS_NFINST,
{Call.typedString(call), Function.candidateFuncListString(list(mfn.func for mfn in matchedFunctions))}, info);
fail();
end if;
end typeOverloadedStringCall;
function typeDiscreteCall
"Types a function call that can be typed normally, but which always has
discrete variability regardless of the variability of the arguments."
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability var = Variability.DISCRETE;
protected
Call argtycall;
Function fn;
list<TypedArg> args;
TypedArg start,interval;
algorithm
argtycall := Call.typeMatchNormalCall(call, origin, info);
ty := Call.typeOf(argtycall);
callExp := Expression.CALL(Call.unboxArgs(argtycall));
end typeDiscreteCall;
function typeNdimsCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty = Type.INTEGER();
output Variability variability = Variability.PARAMETER;
protected
list<Expression> args;
list<NamedArg> named_args;
Type arg_ty;
algorithm
Call.UNTYPED_CALL(arguments = args, named_args = named_args) := call;
assertNoNamedParams("ndims", named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "ndims(Any) => Integer"}, info);
fail();
end if;
// The number of dimensions an expression has is always known,
// so we might as well evaluate the ndims call here.
(_, arg_ty, _) := Typing.typeExp(listHead(args), origin, info);
callExp := Expression.INTEGER(Type.dimensionCount(arg_ty));
end typeNdimsCall;
function typePreCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
algorithm
(callExp, ty, variability) := typePreChangeCall("pre", call, origin, info);
end typePreCall;
function typeChangeCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
algorithm
(callExp, ty, variability) := typePreChangeCall("change", call, origin, info);
ty := Type.setArrayElementType(ty, Type.BOOLEAN());
end typeChangeCall;
function typePreChangeCall
input String name;
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability = Variability.DISCRETE;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Variability var;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams(name, named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), ComponentRef.toString(fn_ref) + "(Any) => Any"}, info);
end if;
// pre/change may not be used in a function context.
if ExpOrigin.flagSet(origin, ExpOrigin.FUNCTION) then
Error.addSourceMessageAndFail(Error.EXP_INVALID_IN_FUNCTION,
{ComponentRef.toString(fn_ref)}, info);
end if;
(arg, ty, var) := Typing.typeExp(listHead(args), origin, info);
if not Expression.isCref(arg) then
Error.addSourceMessage(Error.ARGUMENT_MUST_BE_VARIABLE,
{"First", ComponentRef.toString(fn_ref), "<REMOVE ME>"}, info);
fail();
end if;
if var == Variability.CONTINUOUS then
Error.addSourceMessageAndFail(Error.INVALID_ARGUMENT_VARIABILITY,
{"1", ComponentRef.toString(fn_ref), Prefixes.variabilityString(Variability.DISCRETE),
Expression.toString(arg), Prefixes.variabilityString(var)}, info);
end if;
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, var, ty));
end typePreChangeCall;
function typeDerCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
Type ety;
algorithm
// der may not be used in a function context.
if ExpOrigin.flagSet(origin, ExpOrigin.FUNCTION) then
Error.addSourceMessage(Error.EXP_INVALID_IN_FUNCTION, {"der"}, info);
fail();
end if;
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("der", named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "der(Real) => Real"}, info);
end if;
{arg} := args;
(arg, ty, variability) := Typing.typeExp(arg, origin, info);
// The argument of der must be a Real scalar or array.
ety := Type.arrayElementType(ty);
if Type.isInteger(ety) then
ty := Type.setArrayElementType(ty, Type.REAL());
arg := Expression.typeCast(arg, Type.REAL());
elseif not Type.isReal(ety) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"1", ComponentRef.toString(fn_ref), "", Expression.toString(arg),
Type.toString(ty), "Real"}, info);
end if;
// The argument must be differentiable, i.e. not discrete, unless where in a
// scope where everything is discrete (like an initial equation).
if variability == Variability.DISCRETE and not ExpOrigin.flagSet(origin, ExpOrigin.DISCRETE_SCOPE) then
Error.addSourceMessageAndFail(Error.DER_OF_NONDIFFERENTIABLE_EXP,
{Expression.toString(arg)}, info);
end if;
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
end typeDerCall;
function typeDiagonalCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Dimension dim;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("diagonal", named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "diagonal(Any[n]) => Any[n, n]"}, info);
end if;
(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
ty := match ty
case Type.ARRAY(dimensions = {dim})
then Type.ARRAY(ty.elementType, {dim, dim});
else
algorithm
Error.addSourceMessage(Error.ARG_TYPE_MISMATCH,
{"1", ComponentRef.toString(fn_ref), "", Expression.toString(arg),
Type.toString(ty), "Any[:]"}, info);
then
fail();
end match;
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
end typeDiagonalCall;
function typeEdgeCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability = Variability.DISCRETE;
protected
Call argtycall;
Function fn;
list<TypedArg> args;
TypedArg arg;
InstNode fn_node;
NFCallAttributes ca;
algorithm
// edge may not be used in a function context.
if ExpOrigin.flagSet(origin, ExpOrigin.FUNCTION) then
Error.addSourceMessage(Error.EXP_INVALID_IN_FUNCTION, {"edge"}, info);
fail();
end if;
argtycall as Call.ARG_TYPED_CALL(ComponentRef.CREF(node = fn_node), args, _) := Call.typeNormalCall(call, origin, info);
argtycall := Call.matchTypedNormalCall(argtycall, origin, info);
ty := Call.typeOf(argtycall);
callExp := Expression.CALL(Call.unboxArgs(argtycall));
{arg} := args;
if not Expression.isCref(Util.tuple31(arg)) then
Error.addSourceMessage(Error.ARGUMENT_MUST_BE_VARIABLE,
{"First", "edge", "<REMOVE ME>"}, info);
fail();
end if;
end typeEdgeCall;
function typeMinMaxCall
input String name;
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability var;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
Expression arg1, arg2;
Type ty1, ty2;
Variability var1, var2;
TypeCheck.MatchKind mk;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams(name, named_args, info);
(args, ty, var) := match args
case {arg1}
algorithm
(arg1, ty1, var) := Typing.typeExp(arg1, origin, info);
ty := Type.arrayElementType(ty1);
if not (Type.isArray(ty1) and Type.isBasic(ty)) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"1", name, "", Expression.toString(arg1), Type.toString(ty1), "Any[:, ...]"}, info);
end if;
// If the argument is an array with a single element we can just
// return that element instead of making a min/max call.
if Type.isSingleElementArray(ty1) then
callExp := Expression.applySubscript(Subscript.first(listHead(Type.arrayDims(ty1))), arg1);
return;
end if;
then
({arg1}, ty, var);
case {arg1, arg2}
algorithm
(arg1, ty1, var1) := Typing.typeExp(arg1, origin, info);
(arg2, ty2, var2) := Typing.typeExp(arg2, origin, info);
if not Type.isBasic(ty1) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"1", name, "", Expression.toString(arg1), Type.toString(ty1), "Any"}, info);
end if;
if not Type.isBasic(ty2) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"2", name, "", Expression.toString(arg2), Type.toString(ty2), "Any"}, info);
end if;
(arg1, arg2, ty, mk) := TypeCheck.matchExpressions(arg1, ty1, arg2, ty2);
if not TypeCheck.isValidArgumentMatch(mk) then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), name + "(Any[:, ...]) => Any\n" + name + "(Any, Any) => Any"}, info);
end if;
then
({arg1, arg2}, ty, Prefixes.variabilityMax(var1, var2));
else
algorithm
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), name + "(Any[:, ...]) => Any\n" + name + "(Any, Any) => Any"}, info);
then
fail();
end match;
fn := listHead(Function.typeRefCache(fn_ref));
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, ty));
end typeMinMaxCall;
function typeSumCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
Boolean expanded;
Operator op;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("sum", named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "sum(Any[:, ...]) => Any"}, info);
end if;
(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
ty := Type.arrayElementType(ty);
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
end typeSumCall;
function typeProductCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
Boolean expanded;
Operator op;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("product", named_args, info);
if listLength(args) <> 1 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "product(Any[:, ...]) => Any"}, info);
end if;
(arg, ty, variability) := Typing.typeExp(listHead(args), origin, info);
ty := Type.arrayElementType(ty);
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, variability, ty));
end typeProductCall;
function typePromoteCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression exp_arg, n_arg;
Type exp_ty, n_ty;
Variability n_var;
Function fn;
Integer n;
algorithm
if not Config.languageStandardAtLeast(Config.LanguageStandard.experimental) then
Error.addSourceMessageAndFail(Error.EXPERIMENTAL_REQUIRED, {"promote"}, info);
end if;
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("promote", named_args, info);
if listLength(args) <> 2 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "promote(Any[...], Integer) => Any[...]"}, info);
end if;
{exp_arg, n_arg} := args;
(exp_arg, exp_ty, variability) := Typing.typeExp(exp_arg, origin, info);
(n_arg, n_ty, n_var) := Typing.typeExp(n_arg, origin, info);
if not Type.isInteger(n_ty) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"2", "promote", "", Expression.toString(n_arg), Type.toString(n_ty), "Integer"}, info);
end if;
if n_var > Variability.CONSTANT then
Error.addSourceMessageAndFail(Error.INVALID_ARGUMENT_VARIABILITY,
{"2", "promote", Prefixes.variabilityString(Variability.CONSTANT),
Expression.toString(n_arg), Prefixes.variabilityString(n_var)}, info);
end if;
n_arg := Ceval.evalExp(n_arg, Ceval.EvalTarget.GENERIC(info));
n := Expression.integerValue(n_arg);
if n < Type.dimensionCount(exp_ty) then
Error.addSourceMessageAndFail(Error.INVALID_NUMBER_OF_DIMENSIONS_FOR_PROMOTE,
{String(n), String(Type.dimensionCount(exp_ty))}, info);
end if;
(callExp, ty) := Expression.promote(exp_arg, Expression.typeOf(exp_arg), Expression.integerValue(n_arg));
end typePromoteCall;
function typeSmoothCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg1, arg2;
Type ty1, ty2;
Variability var;
Function fn;
TypeCheck.MatchKind mk;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("smooth", named_args, info);
if listLength(args) <> 2 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "smooth(Integer, Any) => Any"}, info);
end if;
{arg1, arg2} := args;
(arg1, ty1, var) := Typing.typeExp(arg1, origin, info);
(arg2, ty2, variability) := Typing.typeExp(arg2, origin, info);
// First argument must be Integer.
if not Type.isInteger(ty1) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"1", ComponentRef.toString(fn_ref), "", Expression.toString(arg1),
Type.toString(ty1), "Integer"}, info);
end if;
// First argument must be a parameter expression.
if var > Variability.PARAMETER then
Error.addSourceMessageAndFail(Error.INVALID_ARGUMENT_VARIABILITY,
{"1", ComponentRef.toString(fn_ref), Prefixes.variabilityString(Variability.PARAMETER),
Expression.toString(arg1), Prefixes.variabilityString(variability)}, info);
end if;
// Second argument must be Real, array of allowed expressions or record
// containing only components of allowed expressions.
// TODO: Also handle records here.
(arg2, ty, mk) := TypeCheck.matchTypes(ty2, Type.setArrayElementType(ty2, Type.REAL()), arg2, true);
if not TypeCheck.isValidArgumentMatch(mk) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
{"2", ComponentRef.toString(fn_ref), "", Expression.toString(arg2),
Type.toString(ty2), "Real\n Real[:, ...]\n Real record\n Real record[:, ...]"}, info);
end if;
{fn} := Function.typeRefCache(fn_ref);
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg1, arg2}, var, ty));
end typeSmoothCall;
function typeFillCall
input Call call;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability;
protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression fill_arg;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
assertNoNamedParams("fill", named_args, info);
// fill can take any number of arguments, but needs at least two.
if listLength(args) < 2 then
Error.addSourceMessageAndFail(Error.NO_MATCHING_FUNCTION_FOUND_NFINST,
{Call.toString(call), "fill(Any, Integer, ...) => Any[:, ...]"}, info);
end if;
fill_arg :: args := args;
// Type the first argument, which is the fill value.
(fill_arg, ty, _) := Typing.typeExp(fill_arg, origin, info);
(callExp, ty, variability) := typeFillCall2(fn_ref, ty, fill_arg, args, origin, info);
end typeFillCall;
function typeFillCall2
input ComponentRef fnRef;
input Type fillType;
input Expression fillArg;
input list<Expression> dimensionArgs;
input ExpOrigin.Type origin;
input SourceInfo info;
output Expression callExp;
output Type ty;
output Variability variability = Variability.CONSTANT;
protected
Expression fill_arg;
list<Expression> ty_args;
Variability arg_var;
Type arg_ty;
Function fn;
list<Dimension> dims;
Boolean evaluated;
algorithm