This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
NFTypeCheck.mo
2907 lines (2628 loc) · 92.8 KB
/
NFTypeCheck.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 NFTypeCheck
" file: NFTypeCheck.mo
package: NFTypeCheck
description: SCodeInst type checking.
Functions used by SCodeInst for type checking and type conversion where needed.
"
import DAE;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFBinding.Binding;
import NFPrefixes.Variability;
protected
import Debug;
import DAEExpression = Expression;
import Error;
import ExpressionDump;
import List;
import Types;
import Operator = NFOperator;
import Type = NFType;
import Class = NFClass.Class;
import ClassTree = NFClassTree;
import InstUtil = NFInstUtil;
import DAEUtil;
import Prefixes = NFPrefixes;
import Restriction = NFRestriction;
import ComplexType = NFComplexType;
import NFOperator.Op;
import NFTyping.ExpOrigin;
import NFFunction.Function;
import NFFunction.TypedArg;
import NFFunction.FunctionMatchKind;
import NFFunction.MatchedFunction;
import NFCall.Call;
import BuiltinCall = NFBuiltinCall;
import NFCall.CallAttributes;
import ComponentRef = NFComponentRef;
import ErrorExt;
import NFBuiltin;
import SimplifyExp = NFSimplifyExp;
import MetaModelica.Dangerous.*;
import OperatorOverloading = NFOperatorOverloading;
import ExpandExp = NFExpandExp;
import NFFunction.Slot;
public
type MatchKind = enumeration(
EXACT "Exact match",
CAST "Matched by casting, e.g. Integer to real",
UNKNOWN_EXPECTED "The expected type was unknown",
UNKNOWN_ACTUAL "The actual type was unknown",
GENERIC "Matched with a generic type e.g. function F<T> input T i; end F; F(1)",
PLUG_COMPATIBLE "Component by component matching, e.g. class A R r; end A; is plug compatible with class B R r; end B;",
NOT_COMPATIBLE
);
function isCompatibleMatch
input MatchKind kind;
output Boolean isCompatible = kind <> MatchKind.NOT_COMPATIBLE;
end isCompatibleMatch;
function isIncompatibleMatch
input MatchKind kind;
output Boolean isIncompatible = kind == MatchKind.NOT_COMPATIBLE;
end isIncompatibleMatch;
function isExactMatch
input MatchKind kind;
output Boolean isCompatible = kind == MatchKind.EXACT;
end isExactMatch;
function isCastMatch
input MatchKind kind;
output Boolean isCast = kind == MatchKind.CAST;
end isCastMatch;
function isGenericMatch
input MatchKind kind;
output Boolean isCast = kind == MatchKind.GENERIC;
end isGenericMatch;
function isValidAssignmentMatch
input MatchKind kind;
output Boolean v = kind == MatchKind.EXACT
or kind == MatchKind.CAST
or kind == MatchKind.PLUG_COMPATIBLE;
end isValidAssignmentMatch;
function isValidArgumentMatch
input MatchKind kind;
output Boolean v = kind == MatchKind.EXACT
or kind == MatchKind.CAST
or kind == MatchKind.GENERIC
or kind == MatchKind.PLUG_COMPATIBLE;
end isValidArgumentMatch;
function isValidPlugCompatibleMatch
input MatchKind kind;
output Boolean v = kind == MatchKind.EXACT
or kind == MatchKind.PLUG_COMPATIBLE
;
end isValidPlugCompatibleMatch;
function checkBinaryOperation
input Expression exp1;
input Type type1;
input Variability var1;
input Operator operator;
input Expression exp2;
input Type type2;
input Variability var2;
input SourceInfo info;
output Expression binaryExp;
output Type resultType;
algorithm
if Type.isComplex(Type.arrayElementType(type1)) or
Type.isComplex(Type.arrayElementType(type2)) then
(binaryExp,resultType) := checkOverloadedBinaryOperator(exp1, type1, var1, operator, exp2, type2, var2, info);
else
(binaryExp, resultType) := match operator.op
case Op.ADD then checkBinaryOperationAdd(exp1, type1, exp2, type2, info);
case Op.SUB then checkBinaryOperationSub(exp1, type1, exp2, type2, info);
case Op.MUL then checkBinaryOperationMul(exp1, type1, exp2, type2, info);
case Op.DIV then checkBinaryOperationDiv(exp1, type1, exp2, type2, info, isElementWise = false);
case Op.POW then checkBinaryOperationPow(exp1, type1, exp2, type2, info);
case Op.ADD_EW then checkBinaryOperationEW(exp1, type1, exp2, type2, Op.ADD, info);
case Op.SUB_EW then checkBinaryOperationEW(exp1, type1, exp2, type2, Op.SUB, info);
case Op.MUL_EW then checkBinaryOperationEW(exp1, type1, exp2, type2, Op.MUL, info);
case Op.DIV_EW then checkBinaryOperationDiv(exp1, type1, exp2, type2, info, isElementWise = true);
case Op.POW_EW then checkBinaryOperationPowEW(exp1, type1, exp2, type2, info);
end match;
end if;
end checkBinaryOperation;
public function checkOverloadedBinaryOperator
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
String op_str;
list<Function> candidates;
Type ety1, ety2;
algorithm
op_str := Operator.symbol(op,"'");
ety1 := Type.arrayElementType(type1);
ety2 := Type.arrayElementType(type2);
candidates := OperatorOverloading.lookupOperatorFunctionsInType(op_str, ety1);
// Only collect operators from both types if they're not the same type.
if not Type.isEqual(ety1, ety2) then
candidates := listAppend(OperatorOverloading.lookupOperatorFunctionsInType(op_str, ety2), candidates);
end if;
// Give up if no operator functions could be found.
if listEmpty(candidates) then
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, type2}, info);
end if;
(outExp, outType) := matchOverloadedBinaryOperator(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
end checkOverloadedBinaryOperator;
function matchOverloadedBinaryOperator
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
input Boolean showErrors = true;
output Expression outExp;
output Type outType;
protected
list<TypedArg> args;
FunctionMatchKind matchKind;
MatchedFunction matchedFunc;
list<MatchedFunction> matchedFunctions, exactMatches;
Function fn;
Operator.Op oop;
algorithm
args := {(exp1, type1, var1), (exp2, type2, var2)};
matchedFunctions := Function.matchFunctionsSilent(candidates, args, {}, info);
// We only allow exact matches for operator overloading. e.g. no casting or generic matches.
exactMatches := MatchedFunction.getExactMatches(matchedFunctions);
if listEmpty(exactMatches) then
// TODO: new error mentioning overloaded operators.
ErrorExt.setCheckpoint("NFTypeCheck:implicitConstruction");
try
(outExp, outType) := implicitConstructAndMatch(candidates, exp1, type1, op, exp2, type2, info);
if showErrors then
ErrorExt.delCheckpoint("NFTypeCheck:implicitConstruction");
else
ErrorExt.rollBack("NFTypeCheck:implicitConstruction");
end if;
else
ErrorExt.rollBack("NFTypeCheck:implicitConstruction");
if Type.isArray(type1) or Type.isArray(type2) then
oop := op.op;
if oop == Op.ADD or oop == Op.SUB then
(outExp, outType) :=
checkOverloadedBinaryArrayAddSub(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
elseif oop == Op.MUL then
(outExp, outType) :=
checkOverloadedBinaryArrayMul(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
elseif oop == Op.DIV then
(outExp, outType) :=
checkOverloadedBinaryArrayDiv(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, type2}, info, showErrors);
end if;
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, type2}, info, showErrors);
end if;
end try;
elseif listLength(exactMatches) == 1 then
matchedFunc ::_ := exactMatches;
fn := matchedFunc.func;
outType := Function.returnType(fn);
outExp := Expression.CALL(
Call.makeTypedCall(
matchedFunc.func,
list(Util.tuple31(a) for a in matchedFunc.args),
Prefixes.variabilityMax(var1, var2),
outType));
else
if showErrors then
Error.addSourceMessage(Error.AMBIGUOUS_MATCHING_OPERATOR_FUNCTIONS_NFINST,
{Expression.toString(Expression.BINARY(exp1, op, exp2)),
Function.candidateFuncListString(list(mfn.func for mfn in matchedFunctions))}, info);
end if;
fail();
end if;
end matchOverloadedBinaryOperator;
function checkOverloadedBinaryArrayAddSub
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
Expression e1, e2;
MatchKind mk;
algorithm
// For addition or subtraction both sides must have the same type.
(e1, e2, _, mk) := matchExpressions(exp1, type1, exp2, type2, true);
if not isCompatibleMatch(mk) then
printUnresolvableTypeError(Expression.BINARY(e1, op, e2), {type1, type2}, info);
end if;
e1 := ExpandExp.expand(e1);
e2 := ExpandExp.expand(e2);
(outExp, outType) :=
checkOverloadedBinaryArrayAddSub2(e1, type1, var1, op, e2, type2, var2, candidates, info);
end checkOverloadedBinaryArrayAddSub;
function checkOverloadedBinaryArrayAddSub2
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
algorithm
(outExp, outType) := match (exp1, exp2)
local
Type ty, ty1, ty2;
Expression e, e2;
list<Expression> expl, expl1, expl2;
case (Expression.ARRAY(elements = expl1), Expression.ARRAY(elements = expl2))
algorithm
expl := {};
if listEmpty(expl1) then
// If the arrays are empty, match against the element types to get the expected return type.
ty1 := Type.arrayElementType(type1);
ty2 := Type.arrayElementType(type2);
try
(_, ty) := matchOverloadedBinaryOperator(
Expression.EMPTY(ty1), ty1, var1, op, Expression.EMPTY(ty2), ty2, var2, candidates, info, showErrors = false);
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, type2}, info);
end try;
else
ty1 := Type.unliftArray(type1);
ty2 := Type.unliftArray(type2);
for e1 in expl1 loop
e2 :: expl2 := expl2;
(e, ty) := checkOverloadedBinaryArrayAddSub2(e1, ty1, var1, op, e2, ty2, var2, candidates, info);
expl := e :: expl;
end for;
expl := listReverseInPlace(expl);
end if;
outType := Type.setArrayElementType(type1, ty);
outExp := Expression.makeArray(outType, expl, literal = exp1.literal and exp2.literal);
then
(outExp, outType);
else matchOverloadedBinaryOperator(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
end match;
end checkOverloadedBinaryArrayAddSub2;
function checkOverloadedBinaryArrayMul
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
Boolean valid;
list<Dimension> dims1, dims2;
Dimension dim11, dim12, dim21, dim22;
algorithm
dims1 := Type.arrayDims(type1);
dims2 := Type.arrayDims(type2);
(valid, outExp) := match (dims1, dims2)
// scalar * array = array
case ({}, {_})
algorithm
outExp := checkOverloadedBinaryScalarArray(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
then
(true, outExp);
// array * scalar = array
case ({_}, {})
algorithm
outExp := checkOverloadedBinaryArrayScalar(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
then
(true, outExp);
// matrix[n, m] * vector[m] = vector[n]
case ({dim11, dim12}, {dim21})
algorithm
valid := Dimension.isEqual(dim12, dim21);
// TODO: Implement me!
outExp := Expression.BINARY(exp1, op, exp2);
valid := false;
then
(valid, outExp);
// matrix[n, m] * matrix[m, p] = vector[n, p]
case ({dim11, dim12}, {dim21, dim22})
algorithm
valid := Dimension.isEqual(dim12, dim21);
// TODO: Implement me!
outExp := Expression.BINARY(exp1, op, exp2);
valid := false;
then
(valid, outExp);
// scalar * scalar should never get here.
// vector * vector and vector * matrix are undefined for overloaded operators.
else (false, Expression.BINARY(exp1, op, exp2));
end match;
if not valid then
printUnresolvableTypeError(outExp, {type1, type2}, info);
end if;
outType := Expression.typeOf(outExp);
end checkOverloadedBinaryArrayMul;
function checkOverloadedBinaryScalarArray
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
algorithm
(outExp, outType) := checkOverloadedBinaryScalarArray2(
exp1, type1, var1, op, ExpandExp.expand(exp2), type2, var2, candidates, info);
end checkOverloadedBinaryScalarArray;
function checkOverloadedBinaryScalarArray2
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
list<Expression> expl;
Type ty;
algorithm
(outExp, outType) := match exp2
case Expression.ARRAY(elements = {})
algorithm
try
ty := Type.unliftArray(type2);
(_, outType) := matchOverloadedBinaryOperator(
exp1, type1, var1, op, Expression.EMPTY(type2), ty, var2, candidates, info, showErrors = false);
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, exp2.ty}, info);
end try;
outType := Type.setArrayElementType(exp2.ty, outType);
then
(Expression.makeArray(outType, {}), outType);
case Expression.ARRAY(elements = expl)
algorithm
ty := Type.unliftArray(type2);
expl := list(checkOverloadedBinaryScalarArray2(exp1, type1, var1, op, e, ty, var2, candidates, info) for e in expl);
outType := Type.setArrayElementType(exp2.ty, Expression.typeOf(listHead(expl)));
then
(Expression.makeArray(outType, expl), outType);
else matchOverloadedBinaryOperator(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
end match;
end checkOverloadedBinaryScalarArray2;
function checkOverloadedBinaryArrayScalar
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
algorithm
(outExp, outType) := checkOverloadedBinaryArrayScalar2(
ExpandExp.expand(exp1), type1, var1, op, exp2, type2, var2, candidates, info);
end checkOverloadedBinaryArrayScalar;
function checkOverloadedBinaryArrayScalar2
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
Expression e1;
list<Expression> expl;
Type ty;
algorithm
(outExp, outType) := match exp1
case Expression.ARRAY(elements = {})
algorithm
try
ty := Type.unliftArray(type1);
(_, outType) := matchOverloadedBinaryOperator(
Expression.EMPTY(type1), ty, var1, op, exp2, type2, var2, candidates, info, showErrors = false);
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, exp1.ty}, info);
end try;
outType := Type.setArrayElementType(exp1.ty, outType);
then
(Expression.makeArray(outType, {}), outType);
case Expression.ARRAY(elements = expl)
algorithm
ty := Type.unliftArray(type1);
expl := list(checkOverloadedBinaryArrayScalar2(e, ty, var1, op, exp2, type2, var2, candidates, info) for e in expl);
outType := Type.setArrayElementType(exp1.ty, Expression.typeOf(listHead(expl)));
then
(Expression.makeArray(outType, expl), outType);
else matchOverloadedBinaryOperator(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
end match;
end checkOverloadedBinaryArrayScalar2;
function checkOverloadedBinaryArrayDiv
input Expression exp1;
input Type type1;
input Variability var1;
input Operator op;
input Expression exp2;
input Type type2;
input Variability var2;
input list<Function> candidates;
input SourceInfo info;
output Expression outExp;
output Type outType;
algorithm
if Type.isArray(type1) and Type.isScalar(type2) then
(outExp, outType) := checkOverloadedBinaryArrayScalar(exp1, type1, var1, op, exp2, type2, var2, candidates, info);
else
printUnresolvableTypeError(Expression.BINARY(exp1, op, exp2), {type1, type2}, info);
end if;
end checkOverloadedBinaryArrayDiv;
function implicitConstructAndMatch
input list<Function> candidates;
input Expression inExp1;
input Type inType1;
input Operator op;
input Expression inExp2;
input Type inType2;
input SourceInfo info;
output Expression outExp;
output Type outType;
protected
list<InstNode> inputs;
InstNode in1, in2, scope;
MatchKind mk1,mk2;
ComponentRef fn_ref;
Function operfn;
list<tuple<Function, list<Expression>, Variability>> matchedfuncs = {};
Expression exp1,exp2;
Type ty, arg1_ty, arg2_ty;
Variability var;
Boolean matched;
SourceInfo arg1_info, arg2_info;
algorithm
exp1 := inExp1; exp2 := inExp2;
for fn in candidates loop
in1 :: in2 :: _ := fn.inputs;
arg1_ty := InstNode.getType(in1);
arg2_ty := InstNode.getType(in2);
arg1_info := InstNode.info(in1);
arg2_info := InstNode.info(in2);
// Try to implicitly construct a matching record from the first argument.
(matchedfuncs, matched) :=
implicitConstructAndMatch2(inExp1, inType1, inExp2, arg1_ty,
arg1_info, arg2_ty, arg2_info, InstNode.classScope(in2), fn, false, matchedfuncs);
if matched then
continue;
end if;
// Try to implicitly construct a matching record from the second argument.
(matchedfuncs, matched) :=
implicitConstructAndMatch2(inExp2, inType2, inExp1, arg2_ty,
arg2_info, arg1_ty, arg1_info, InstNode.classScope(in1), fn, true, matchedfuncs);
end for;
if listLength(matchedfuncs) == 1 then
(operfn, {exp1,exp2}, var)::_ := matchedfuncs;
outType := Function.returnType(operfn);
outExp := Expression.CALL(Call.makeTypedCall(operfn, {exp1, exp2}, var, outType));
else
Error.addSourceMessage(Error.AMBIGUOUS_MATCHING_OPERATOR_FUNCTIONS_NFINST,
{Expression.toString(Expression.BINARY(exp1, op, exp2)),
Function.candidateFuncListString(list(Util.tuple31(fn) for fn in matchedfuncs))}, info);
fail();
end if;
end implicitConstructAndMatch;
function implicitConstructAndMatch2
input Expression exp1;
input Type type1;
input Expression exp2;
input Type paramType1;
input SourceInfo paramInfo1;
input Type paramType2;
input SourceInfo paramInfo2;
input InstNode scope;
input Function fn;
input Boolean reverseArgs;
input output list<tuple<Function, list<Expression>, Variability>> matchedFns;
output Boolean matched;
protected
ComponentRef fn_ref;
Expression e1, e2;
MatchKind mk;
Variability var;
Type ty;
algorithm
(e1, _, mk) := matchTypes(paramType1, type1, exp1, false);
// We only want overloaded constructors when trying to implicitly construct.
// Default constructors are not considered.
if mk == MatchKind.EXACT then
fn_ref := Function.instFunction(Absyn.CREF_IDENT("'constructor'", {}), scope, paramInfo2);
e2 := Expression.CALL(NFCall.UNTYPED_CALL(fn_ref, {exp2}, {}, scope));
(e2, ty, var) := Call.typeCall(e2, 0, paramInfo1);
(_, _, mk) := matchTypes(paramType2, ty, e2, false);
if mk == MatchKind.EXACT then
matchedFns := (fn, if reverseArgs then {e2, e1} else {e1, e2}, var) :: matchedFns;
matched := true;
else
matched := false;
end if;
else
matched := false;
end if;
end implicitConstructAndMatch2;
//function checkValidBinaryOperatorOverload
// input String oper_name;
// input Function oper_func;
// input InstNode rec_node;
//protected
// SourceInfo info;
//algorithm
// info := InstNode.info(oper_func.node);
// checkOneOutput(oper_name, oper_func.outputs, rec_node, info);
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, info);
// checkTwoInputs(oper_name, oper_func.inputs, rec_node, info);
//end checkValidBinaryOperatorOverload;
//function checkValidOperatorOverload
// input String oper_name;
// input Function oper_func;
// input InstNode rec_node;
//protected
// Type ty1, ty2;
// InstNode out_class;
//algorithm
// () := match oper_name
// case "'constructor'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'0'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'+'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'-'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'*'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'/'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'^'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), rec_node, InstNode.info(oper_func.node));
// then ();
// case "'and'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), NFBuiltin.BOOLEAN_NODE, InstNode.info(oper_func.node));
// then ();
// case "'or'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), NFBuiltin.BOOLEAN_NODE, InstNode.info(oper_func.node));
// then ();
// case "'not'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), NFBuiltin.BOOLEAN_NODE, InstNode.info(oper_func.node));
// then ();
// case "'String'" algorithm
// checkOneOutput(oper_name, oper_func.outputs, rec_node, InstNode.info(oper_func.node));
// checkOutputType(oper_name, List.first(oper_func.outputs), NFBuiltin.STRING_NODE, InstNode.info(oper_func.node));
// then ();
//
// else ();
//
// end match;
//end checkValidOperatorOverload;
//public
//function checkOneOutput
// input String oper_name;
// input list<InstNode> outputs;
// input InstNode rec_node;
// input SourceInfo info;
//protected
// InstNode out_class;
//algorithm
// if listLength(outputs) <> 1 then
// Error.addSourceMessage(Error.OPERATOR_OVERLOADING_WARNING,
// {"Overloaded " + oper_name + " operator functions are required to have exactly one output. Found "
// + intString(listLength(outputs))}, info);
// end if;
//end checkOneOutput;
//
//public
//function checkTwoInputs
// input String oper_name;
// input list<InstNode> inputs;
// input InstNode rec_node;
// input SourceInfo info;
//protected
// InstNode out_class;
//algorithm
// if listLength(inputs) < 2 then
// Error.addSourceMessage(Error.OPERATOR_OVERLOADING_WARNING,
// {"Binary overloaded " + oper_name + " operator functions are required to have at least two inputs. Found "
// + intString(listLength(inputs))}, info);
// end if;
//end checkTwoInputs;
//
//function checkOutputType
// input String oper_name;
// input InstNode outc;
// input InstNode expected;
// input SourceInfo info;
//protected
// InstNode out_class;
//algorithm
// out_class := InstNode.classScope(outc);
// if not InstNode.isSame(out_class, expected) then
// Error.addSourceMessage(Error.OPERATOR_OVERLOADING_WARNING,
// {"Wrong type for output of overloaded operator function '"+ oper_name +
// "'. Expected '" + InstNode.scopeName(expected) + "' Found '" + InstNode.scopeName(outc) + "'"}, info);
// end if;
//end checkOutputType;
function checkBinaryOperationAdd
input Expression exp1;
input Type type1;
input Expression exp2;
input Type type2;
input SourceInfo info;
output Expression binaryExp;
output Type resultType;
protected
Expression e1, e2;
MatchKind mk;
Boolean valid;
algorithm
(e1, e2, resultType, mk) := matchExpressions(exp1, type1, exp2, type2, true);
valid := isCompatibleMatch(mk);
valid := match Type.arrayElementType(resultType)
case Type.INTEGER() then valid;
case Type.REAL() then valid;
case Type.STRING() then valid;
else false;
end match;
binaryExp := Expression.BINARY(e1, Operator.makeAdd(resultType), e2);
if not valid then
printUnresolvableTypeError(binaryExp, {type1, type2}, info);
end if;
end checkBinaryOperationAdd;
function checkBinaryOperationSub
input Expression exp1;
input Type type1;
input Expression exp2;
input Type type2;
input SourceInfo info;
output Expression binaryExp;
output Type resultType;
protected
Expression e1, e2;
MatchKind mk;
Boolean valid;
algorithm
(e1, e2, resultType, mk) := matchExpressions(exp1, type1, exp2, type2, true);
valid := isCompatibleMatch(mk);
valid := match Type.arrayElementType(resultType)
case Type.INTEGER() then valid;
case Type.REAL() then valid;
else false;
end match;
binaryExp := Expression.BINARY(e1, Operator.makeSub(resultType), e2);
if not valid then
printUnresolvableTypeError(binaryExp, {type1, type2}, info);
end if;
end checkBinaryOperationSub;
function checkBinaryOperationMul
input Expression exp1;
input Type type1;
input Expression exp2;
input Type type2;
input SourceInfo info;
output Expression binaryExp;
output Type resultType;
protected
Expression e1, e2;
Type ty1, ty2;
list<Dimension> dims1, dims2;
Dimension dim11, dim12, dim21, dim22;
MatchKind mk;
Op op;
Boolean valid;
algorithm
ty1 := Type.arrayElementType(type1);
ty2 := Type.arrayElementType(type2);
(e1, e2, resultType, mk) := matchExpressions(exp1, ty1, exp2, ty2, true);
valid := isCompatibleMatch(mk);
valid := match resultType
case Type.INTEGER() then valid;
case Type.REAL() then valid;
else false;
end match;
dims1 := Type.arrayDims(type1);
dims2 := Type.arrayDims(type2);
(resultType, op) := match (dims1, dims2)
// scalar * scalar = scalar
case ({}, {}) then (resultType, Op.MUL);
// scalar * array = array
case ({}, _) then (Type.ARRAY(resultType, dims2), Op.MUL_SCALAR_ARRAY);
// array * scalar = array
case (_, {}) then (Type.ARRAY(resultType, dims1), Op.MUL_ARRAY_SCALAR);
// vector[n] * vector[n] = scalar
case ({dim11}, {dim21})
algorithm
valid := Dimension.isEqual(dim11, dim21);
then
(resultType, Op.SCALAR_PRODUCT);
// vector[n] * matrix[n, m] = vector[m]
case ({dim11}, {dim21, dim22})
algorithm
valid := Dimension.isEqual(dim11, dim21);
then
(Type.ARRAY(resultType, {dim22}), Op.MUL_VECTOR_MATRIX);
// matrix[n, m] * vector[m] = vector[n]
case ({dim11, dim12}, {dim21})
algorithm
valid := Dimension.isEqual(dim12, dim21);
then
(Type.ARRAY(resultType, {dim11}), Op.MUL_MATRIX_VECTOR);
// matrix[n, m] * matrix[m, p] = vector[n, p]
case ({dim11, dim12}, {dim21, dim22})
algorithm
valid := Dimension.isEqual(dim12, dim21);
then
(Type.ARRAY(resultType, {dim11, dim22}), Op.MATRIX_PRODUCT);
else
algorithm
valid := false;
then
(resultType, Op.MUL);
end match;
binaryExp := Expression.BINARY(e1, Operator.OPERATOR(resultType, op), e2);
if not valid then
printUnresolvableTypeError(binaryExp, {type1, type2}, info);
end if;
end checkBinaryOperationMul;
function checkBinaryOperationDiv
input Expression exp1;
input Type type1;
input Expression exp2;
input Type type2;
input SourceInfo info;
input Boolean isElementWise;
output Expression binaryExp;
output Type resultType;
protected
Expression e1, e2;
Type ty1, ty2;
MatchKind mk;
Boolean valid;
Operator op;
algorithm
// Division always returns a Real value, so instead of checking if the types
// are compatible with each other we check if each type is compatible with Real.
(e1, ty1, mk) := matchTypes(type1, Type.setArrayElementType(type1, Type.REAL()), exp1, true);
valid := isCompatibleMatch(mk);
(e2, ty2, mk) := matchTypes(type2, Type.setArrayElementType(type2, Type.REAL()), exp2, true);
valid := valid and isCompatibleMatch(mk);
// Division is always element-wise, the only difference between / and ./ is
// which operands they accept.
(resultType, op) := match (Type.isArray(ty1), Type.isArray(ty2), isElementWise)
// scalar / scalar or scalar ./ scalar
case (false, false, _ ) then (ty1, Operator.makeDiv(ty1));
// array / scalar or array ./ scalar
case (_ , false, _ ) then (ty1, Operator.OPERATOR(ty1, Op.DIV_ARRAY_SCALAR));
// scalar ./ array
case (false, _ , true) then (ty2, Operator.OPERATOR(ty2, Op.DIV_SCALAR_ARRAY));
// array ./ array
case (true , _ , true)
algorithm
// If both operands are arrays, check that their dimensions are compatible.
(_, _, mk) := matchArrayTypes(ty1, ty2, e1, true);
valid := valid and isCompatibleMatch(mk);
then
(ty1, Operator.makeDiv(ty1));
// Anything else is an error.
else
algorithm
valid := false;
then
(ty1, Operator.makeDiv(ty1));
end match;
binaryExp := Expression.BINARY(e1, op, e2);
if not valid then
printUnresolvableTypeError(binaryExp, {type1, type2}, info);
end if;
end checkBinaryOperationDiv;
function checkBinaryOperationPow
input Expression exp1;
input Type type1;
input Expression exp2;
input Type type2;
input SourceInfo info;
output Expression binaryExp;
output Type resultType;
protected
Expression e1, e2;