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

Commit

Permalink
[NF] Hack to fix type of min/max/sum/product.
Browse files Browse the repository at this point in the history
- Patch the type of min/max/sum/product earlier to avoid getting
  polymorphic types in the calls.

Belonging to [master]:
  - #2372
  • Loading branch information
perost authored and OpenModelica-Hudson committed Apr 17, 2018
1 parent 6ca8924 commit a473365
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
52 changes: 35 additions & 17 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -614,6 +614,12 @@ uniontype Call
end for;

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;
Expand Down Expand Up @@ -703,7 +709,6 @@ uniontype Call

end vectorizeCall;


function evaluateCallType
input output Type ty;
input Function fn;
Expand Down Expand Up @@ -802,6 +807,29 @@ uniontype Call
end match;
end evaluateCallTypeDimExp;

function getSpecialReturnType
input Function fn;
input list<Expression> args;
output Type ty;
algorithm
ty := match fn.path
case Absyn.IDENT("min")
then Type.arrayElementType(Expression.typeOf(Expression.unbox(listHead(args))));
case Absyn.IDENT("max")
then Type.arrayElementType(Expression.typeOf(Expression.unbox(listHead(args))));
case Absyn.IDENT("sum")
then Type.arrayElementType(Expression.typeOf(Expression.unbox(listHead(args))));
case Absyn.IDENT("product")
then Type.arrayElementType(Expression.typeOf(Expression.unbox(listHead(args))));
else
algorithm
Error.assertion(false, getInstanceName() + ": unhandled case for " +
Absyn.pathString(fn.path), sourceInfo());
then
fail();
end match;
end getSpecialReturnType;

function typeArgs
input output Call call;
input ExpOrigin.Type origin;
Expand Down Expand Up @@ -1736,15 +1764,13 @@ protected
output Variability var;
protected
Call argtycall;
Function fn;
list<TypedArg> args;
TypedArg start,interval;
algorithm
argtycall as ARG_TYPED_CALL(_, args, _) := typeNormalCall(call, origin, info);
argtycall := typeNormalCall(call, origin, info);
argtycall := matchTypedNormalCall(argtycall, origin, info);
callExp := Expression.CALL(unboxArgs(argtycall));
ty := Type.arrayElementType(Util.tuple32(listHead(args)));
argtycall := unboxArgs(argtycall);
ty := getType(argtycall);
var := variability(argtycall);
callExp := Expression.CALL(argtycall);
// TODO: check basic type in two argument overload.
// check arrays of simple types in one argument overload.
// fix return type.
Expand All @@ -1759,22 +1785,14 @@ protected
output Variability var;
protected
Call argtycall;
Function fn;
Expression arg;
algorithm
// TODO: Rewrite this whole thing.
argtycall := typeNormalCall(call, origin, info);
argtycall := matchTypedNormalCall(argtycall, origin, info);
argtycall := unboxArgs(argtycall);

ty := match argtycall
case TYPED_CALL() algorithm
{arg} := argtycall.arguments;
then Type.arrayElementType(Expression.typeOf(arg));
end match;

ty := getType(argtycall);
var := variability(argtycall);
callExp := Expression.CALL(setType(argtycall, ty));
callExp := Expression.CALL(argtycall);
end typeSumProductCall;

function typeSmoothCall
Expand Down
15 changes: 13 additions & 2 deletions Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -378,6 +378,16 @@ public
end match;
end isUnknown;

function isPolymorphic
input Type ty;
output Boolean isPolymorphic;
algorithm
isPolymorphic := match ty
case POLYMORPHIC() then true;
else false;
end match;
end isPolymorphic;

function firstTupleType
input Type ty;
output Type outTy;
Expand Down Expand Up @@ -484,16 +494,17 @@ public
case Type.REAL() then "Real";
case Type.STRING() then "String";
case Type.BOOLEAN() then "Boolean";
case Type.CLOCK() then "Clock";
case Type.ENUMERATION() then "enumeration " + Absyn.pathString(ty.typePath) +
"(" + stringDelimitList(ty.literals, ", ") + ")";
case Type.ENUMERATION_ANY() then "enumeration(:)";
case Type.CLOCK() then "Clock";
case Type.ARRAY() then toString(ty.elementType) + "[" + stringDelimitList(List.map(ty.dimensions, Dimension.toString), ", ") + "]";
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")";
case Type.FUNCTION() then "function( output " + toString(ty.resultType) + " )";
case Type.NORETCALL() then "()";
case Type.UNKNOWN() then "unknown()";
case Type.COMPLEX() then InstNode.name(ty.cls);
case Type.FUNCTION() then "function( output " + toString(ty.resultType) + " )";
case Type.METABOXED() then "#" + toString(ty.ty);
case Type.POLYMORPHIC() then "<" + ty.name + ">";
case Type.ANY() then "$ANY$";
else
Expand Down

0 comments on commit a473365

Please sign in to comment.