Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit cc9ae78

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improve type casting of some builtins.
- Added special cases for type casting fill and diagonal. - Renamed Expression.typeCastElements to Expression.typeCast, and removed the old unused Expression.typeCast. Belonging to [master]: - #2953
1 parent 482cb0e commit cc9ae78

File tree

6 files changed

+50
-19
lines changed

6 files changed

+50
-19
lines changed

Compiler/NFFrontEnd/NFBuiltinCall.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ protected
593593

594594
if Type.isInteger(ety) then
595595
ty := Type.setArrayElementType(ty, Type.REAL());
596-
arg := Expression.typeCastElements(arg, Type.REAL());
596+
arg := Expression.typeCast(arg, Type.REAL());
597597
elseif not Type.isReal(ety) then
598598
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH,
599599
{"1", ComponentRef.toString(fn_ref), "", Expression.toString(arg),

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,42 @@ uniontype Call
845845
end match;
846846
end retype;
847847

848+
function typeCast
849+
input output Expression callExp;
850+
input Type ty;
851+
protected
852+
Call call;
853+
algorithm
854+
Expression.CALL(call = call) := callExp;
855+
856+
callExp := match call
857+
case TYPED_CALL() guard Function.isBuiltin(call.fn)
858+
then
859+
match Absyn.pathFirstIdent(Function.name(call.fn))
860+
// For 'fill' we can type cast the first argument rather than the
861+
// whole array that 'fill' constructs.
862+
case "fill"
863+
algorithm
864+
call.arguments := Expression.typeCast(listHead(call.arguments), ty) ::
865+
listRest(call.arguments);
866+
then
867+
Expression.CALL(call);
868+
869+
// For diagonal we can type cast the argument rather than the
870+
// matrix that diagonal constructs.
871+
case "diagonal"
872+
algorithm
873+
call.arguments := {Expression.typeCast(listHead(call.arguments), ty)};
874+
then
875+
Expression.CALL(call);
876+
877+
else Expression.CAST(Type.setArrayElementType(call.ty, ty), callExp);
878+
end match;
879+
880+
else Expression.CAST(Type.setArrayElementType(typeOf(call), ty), callExp);
881+
end match;
882+
end typeCast;
883+
848884
protected
849885
function instNormalCall
850886
input Absyn.ComponentRef functionName;

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,9 @@ function evalCast
15371537
input Type castTy;
15381538
output Expression exp;
15391539
algorithm
1540-
exp := Expression.typeCastElements(castExp, Type.elementType(castTy));
1540+
exp := Expression.typeCast(castExp, Type.elementType(castTy));
15411541

1542-
// Expression.typeCastElements will just create a CAST if it can't typecast
1542+
// Expression.typeCast will just create a CAST if it can't typecast
15431543
// the expression, so make sure we actually got something else back.
15441544
() := match exp
15451545
case Expression.CAST()

Compiler/NFFrontEnd/NFExpandExp.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ public
947947
(outExp, expanded) := expand(exp);
948948

949949
if expanded then
950-
outExp := Expression.typeCastElements(outExp, ty);
950+
outExp := Expression.typeCast(outExp, ty);
951951
else
952952
outExp := exp;
953953
end if;

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,6 @@ public
806806
end setType;
807807

808808
function typeCast
809-
input Expression exp;
810-
input Type castTy;
811-
output Expression castExp = CAST(castTy, exp);
812-
end typeCast;
813-
814-
function typeCastElements
815809
input output Expression exp;
816810
input Type ty;
817811
algorithm
@@ -828,18 +822,19 @@ public
828822
case (ARRAY(ty = t, elements = el), _)
829823
algorithm
830824
ety := Type.arrayElementType(ty);
831-
el := list(typeCastElements(e, ety) for e in el);
825+
el := list(typeCast(e, ety) for e in el);
832826
t := Type.setArrayElementType(t, ty);
833827
then
834828
ARRAY(t, el, exp.literal);
835829

836830
case (UNARY(), _)
837-
then UNARY(exp.operator, typeCastElements(exp.exp, ty));
831+
then UNARY(exp.operator, typeCast(exp.exp, ty));
838832

839833
case (IF(), _)
840-
then IF(exp.condition,
841-
typeCastElements(exp.trueBranch, ty),
842-
typeCastElements(exp.falseBranch, ty));
834+
then IF(exp.condition, typeCast(exp.trueBranch, ty), typeCast(exp.falseBranch, ty));
835+
836+
case (CALL(), _)
837+
then Call.typeCast(exp, ty);
843838

844839
else
845840
algorithm
@@ -849,7 +844,7 @@ public
849844
CAST(t, exp);
850845

851846
end match;
852-
end typeCastElements;
847+
end typeCast;
853848

854849
function realValue
855850
input Expression exp;

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,13 +2155,13 @@ algorithm
21552155
// Integer can be cast to Real.
21562156
case (Type.INTEGER(), Type.REAL())
21572157
algorithm
2158-
exp1 := Expression.typeCastElements(exp1, type2);
2158+
exp1 := Expression.typeCast(exp1, type2);
21592159
then
21602160
(type2, MatchKind.CAST);
21612161

21622162
case (Type.REAL(), Type.INTEGER())
21632163
algorithm
2164-
exp2 := Expression.typeCastElements(exp2, type1);
2164+
exp2 := Expression.typeCast(exp2, type1);
21652165
then
21662166
(type1, MatchKind.CAST);
21672167

@@ -2635,7 +2635,7 @@ algorithm
26352635
// Integer can be cast to Real.
26362636
case (Type.INTEGER(), Type.REAL())
26372637
algorithm
2638-
expression := Expression.typeCastElements(expression, expectedType);
2638+
expression := Expression.typeCast(expression, expectedType);
26392639
then
26402640
(expectedType, MatchKind.CAST);
26412641

0 commit comments

Comments
 (0)