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

Commit

Permalink
[NF] Type-check pre operator
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #2009
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Nov 13, 2017
1 parent c536259 commit 38bf65a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
78 changes: 77 additions & 1 deletion Compiler/NFFrontEnd/NFCall.mo
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ uniontype Call
algorithm

(call, ty, variability) := match callExp
case Expression.CALL(UNTYPED_CALL(ref=ComponentRef.CREF(node=InstNode.CLASS_NODE(name="pre"))))
then typePreCall(callExp.call, info);
case Expression.CALL(UNTYPED_CALL()) then typeNormalCall(callExp.call, info);
case Expression.CALL(UNTYPED_MAP_CALL()) then typeMapIteratorCall(callExp.call, info);
end match;
Expand Down Expand Up @@ -408,7 +410,7 @@ uniontype Call

// Type the arguments.
argtycall := typeArgs(call,info);
// Match the arguments with the expeted ones.
// Match the arguments with the expected ones.
(fn,tyArgs) := matchFunctions(argtycall,info);

args := list(Util.tuple31(a) for a in tyArgs);
Expand Down Expand Up @@ -440,6 +442,80 @@ uniontype Call
end match;
end typeNormalCall;

function typePreCall
input output Call call;
input SourceInfo info;
output Type ty;
output Variability variability;
protected
Call argtycall;
InstNode fn_node;
list<Function> fnl;
Boolean fn_typed, special;
Function fn;
Expression e;
list<Type> arg_ty;
list<Variability> arg_var;
CallAttributes ca;
list<TypedArg> tyArgs;
list<TypedNamedArg> nargs;
algorithm
(call , ty, variability) := match call
case UNTYPED_CALL(ref = ComponentRef.CREF(node = fn_node))
algorithm
// Fetch the cached function(s).
CachedData.FUNCTION({fn}, fn_typed, special) := InstNode.getFuncCache(fn_node);

// Type the function(s) if not already done.
if not fn_typed then
fn := Function.typeFunction(fn);
InstNode.setFuncCache(fn_node, CachedData.FUNCTION({fn}, true, special));
end if;

// Type the arguments.
ARG_TYPED_CALL(arguments=tyArgs, named_args=nargs) := typeArgs(call,info);
if not listEmpty(nargs) or listLength(tyArgs) <> 1 then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND, {toString(call), "<REMOVE ME>", "pre(<T>) => <T>"}, info);
end if;

{(e,ty,variability)} := tyArgs;

_ := match e
case Expression.CREF() then ();
else
algorithm
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND, {toString(call), "<REMOVE ME>", "pre(variable) => <T>"}, info);
then fail();
end match;

// TODO: Allow record components? Arrays?
if not Type.isBasic(ty) then
Error.addSourceMessage(Error.NO_MATCHING_FUNCTION_FOUND, {toString(call), "<REMOVE ME>", "pre(basicType)"}, info);
fail();
end if;

// TODO: Check if we are in function context?

ca := CallAttributes.CALL_ATTR(
ty,
Type.isTuple(ty),
Function.isBuiltin(fn),
Function.isImpure(fn),
Function.isFunctionPointer(fn),
Function.inlineBuiltin(fn),
DAE.NO_TAIL());

then
(TYPED_CALL(fn, {e}, ca), ty, variability);

else
algorithm
assert(false, getInstanceName() + " got invalid function call expression");
then
fail();
end match;
end typePreCall;

function typeArgs
input output Call call;
input SourceInfo info;
Expand Down
1 change: 1 addition & 0 deletions Compiler/NFFrontEnd/NFFunction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ uniontype Function
else
special := match fn.path
case Absyn.IDENT(name = "size") then true;
case Absyn.IDENT(name = "pre") then true;
else false;
end match;
end if;
Expand Down
16 changes: 16 additions & 0 deletions Compiler/NFFrontEnd/NFType.mo
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ public
end match;
end isScalarArray;

function isBasic
input Type ty;
output Boolean isNumeric;
algorithm
isNumeric := match ty
case REAL() then true;
case INTEGER() then true;
case BOOLEAN() then true;
case STRING() then true;
case ENUMERATION() then true;
case CLOCK() then true;
case FUNCTION() then isBasic(ty.resultType);
else false;
end match;
end isBasic;

function isBasicNumeric
input Type ty;
output Boolean isNumeric;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFTypeCheck.mo
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ algorithm
{N2,M} := Type.arrayDims(inType2);
if (not isValidMatrixMultiplyDims(N1,N2)) then
binaryArrayOpError(inExp1,inType1,inExp2,inType2,inOp,
"\n: Dimensions error in Vecto Matrix multiplication.");
"\n: Dimensions error in Vector Matrix multiplication.");
fail();
else
ty1 := Type.elementType(inType1);
Expand Down

0 comments on commit 38bf65a

Please sign in to comment.