Skip to content

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Replaced all use of DAE.Const with DAE.VarKind, to be able to handle
  discrete variability and avoid unnecessary conversions.
- Added variability check for bindings.
- Fixed bug in unknown dimension inference that would make it fail on
  propagated bindings in some cases.
- Added type check for array dimensions.
- Moved setting a component's type to before typing its children,
  to handle the case where a child is dependent on its parent's type.
- Removed deprecated file NFFunc.mo and deprecated code in NFInstUtil.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Sep 21, 2017
1 parent d3dffea commit 50d02d9
Show file tree
Hide file tree
Showing 20 changed files with 315 additions and 3,358 deletions.
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -753,7 +753,7 @@ function spatialDistribution "Not yet implemented"
input Real x;
input Boolean positiveVelocity;
parameter input Real initialPoints[:](each min = 0, each max = 1) = {0.0, 1.0};
parameter input Real initialValues[size(initialPoints)] = {0.0, 0.0};
parameter input Real initialValues[size(initialPoints, 1)] = {0.0, 0.0};
output Real out0;
output Real out1;
external "builtin";
Expand Down
9 changes: 3 additions & 6 deletions Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -62,7 +62,7 @@ public
record TYPED_BINDING
Expression bindingExp;
Type bindingType;
DAE.Const variability;
DAE.VarKind variability;
Integer propagatedLevels;
SourceInfo info;
end TYPED_BINDING;
Expand Down Expand Up @@ -142,12 +142,9 @@ public

function variability
input Binding binding;
output DAE.Const var;
output DAE.VarKind var;
algorithm
var := match binding
case TYPED_BINDING() then binding.variability;
else DAE.Const.C_UNKNOWN();
end match;
TYPED_BINDING(variability = var) := binding;
end variability;

function getInfo
Expand Down
10 changes: 5 additions & 5 deletions Compiler/NFFrontEnd/NFBuiltin.mo
Expand Up @@ -152,39 +152,39 @@ constant Binding STATESELECT_NEVER_BINDING =
Binding.TYPED_BINDING(
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "never", 1),
STATESELECT_TYPE_TYPE,
DAE.C_CONST(),
DAE.VarKind.CONST(),
-1,
Absyn.dummyInfo);

constant Binding STATESELECT_AVOID_BINDING =
Binding.TYPED_BINDING(
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "avoid", 2),
STATESELECT_TYPE_TYPE,
DAE.C_CONST(),
DAE.VarKind.CONST(),
-1,
Absyn.dummyInfo);

constant Binding STATESELECT_DEFAULT_BINDING =
Binding.TYPED_BINDING(
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "default", 3),
STATESELECT_TYPE_TYPE,
DAE.C_CONST(),
DAE.VarKind.CONST(),
-1,
Absyn.dummyInfo);

constant Binding STATESELECT_PREFER_BINDING =
Binding.TYPED_BINDING(
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "prefer", 4),
STATESELECT_TYPE_TYPE,
DAE.C_CONST(),
DAE.VarKind.CONST(),
-1,
Absyn.dummyInfo);

constant Binding STATESELECT_ALWAYS_BINDING =
Binding.TYPED_BINDING(
Expression.ENUM_LITERAL(STATESELECT_TYPE_TYPE, "always", 5),
STATESELECT_TYPE_TYPE,
DAE.C_CONST(),
DAE.VarKind.CONST(),
-1,
Absyn.dummyInfo);

Expand Down
19 changes: 10 additions & 9 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -50,6 +50,7 @@ import List;
import NFClass.Class;
import ErrorExt;
import Util;
import InstUtil = NFInstUtil;

protected
import NFFunction.NamedArg;
Expand Down Expand Up @@ -171,7 +172,7 @@ uniontype Call
input output Expression callExp;
input SourceInfo info;
output Type ty;
output DAE.Const variability;
output DAE.VarKind variability;
protected
Call call, argtycall;
InstNode fn_node;
Expand All @@ -180,7 +181,7 @@ uniontype Call
Function fn;
list<Expression> args;
list<Type> arg_ty;
list<DAE.Const> arg_var;
list<DAE.VarKind> arg_var;
CallAttributes ca;
list<TypedArg> tyArgs;
algorithm
Expand All @@ -203,9 +204,9 @@ uniontype Call

args := list(Util.tuple31(a) for a in tyArgs);

variability := DAE.C_CONST();
variability := DAE.VarKind.CONST();
for a in tyArgs loop
variability := Types.constAnd(variability,Util.tuple33(a));
variability := InstUtil.variabilityAnd(variability,Util.tuple33(a));
end for;

// Construct the call expression.
Expand Down Expand Up @@ -240,25 +241,25 @@ uniontype Call
local
Expression arg;
Type arg_ty;
DAE.Const arg_const;
DAE.VarKind arg_var;
list<TypedArg> typedArgs;
list<TypedNamedArg> typedNamedArgs;
String name;

case UNTYPED_CALL() algorithm
typedArgs := {};
for arg in call.arguments loop
(arg, arg_ty, arg_const) := Typing.typeExp(arg,info);
typedArgs := (arg, arg_ty, arg_const)::typedArgs;
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
typedArgs := (arg, arg_ty, arg_var)::typedArgs;
end for;

typedArgs := listReverse(typedArgs);

typedNamedArgs := {};
for narg in call.named_args loop
(name,arg) := narg;
(arg, arg_ty, arg_const) := Typing.typeExp(arg,info);
typedNamedArgs := (name, arg, arg_ty, arg_const)::typedNamedArgs;
(arg, arg_ty, arg_var) := Typing.typeExp(arg,info);
typedNamedArgs := (name, arg, arg_ty, arg_var)::typedNamedArgs;
end for;
listReverse(typedNamedArgs);

Expand Down
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -45,7 +45,7 @@ import Type = NFType;

uniontype EvalTarget
record DIMENSION
String name;
InstNode component;
Integer index;
Expression exp;
SourceInfo info;
Expand Down Expand Up @@ -235,7 +235,7 @@ algorithm
case EvalTarget.DIMENSION()
algorithm
Error.addSourceMessage(Error.STRUCTURAL_PARAMETER_OR_CONSTANT_WITH_NO_BINDING,
{Expression.toString(exp), target.name}, target.info);
{Expression.toString(exp), InstNode.name(target.component)}, target.info);
then
fail();

Expand Down
11 changes: 0 additions & 11 deletions Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -163,17 +163,6 @@ uniontype Class
end match;
end isBuiltin;

function getType
input Class cls;
output Type ty;
algorithm
ty := match cls
case PARTIAL_BUILTIN() then cls.ty;
case INSTANCED_BUILTIN() then cls.ty;
else Type.UNKNOWN();
end match;
end getType;

function classTree
input Class cls;
output ClassTree tree;
Expand Down
14 changes: 13 additions & 1 deletion Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -228,7 +228,7 @@ uniontype Component
algorithm
ty := match component
case TYPED_COMPONENT() then component.ty;
case UNTYPED_COMPONENT() then Class.getType(InstNode.getClass(component.classInst));
case UNTYPED_COMPONENT() then InstNode.getType(component.classInst);
case ITERATOR() then component.ty;
else Type.UNKNOWN();
end match;
Expand Down Expand Up @@ -467,6 +467,18 @@ uniontype Component
else false;
end match;
end isRedeclare;

function dimensionCount
input Component component;
output Integer count;
algorithm
count := match component
case UNTYPED_COMPONENT() then arrayLength(component.dimensions);
case TYPED_COMPONENT() then listLength(Type.arrayDims(component.ty));
else 0;
end match;
end dimensionCount;

end Component;

annotation(__OpenModelica_Interface="frontend");
Expand Down
15 changes: 3 additions & 12 deletions Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -101,20 +101,11 @@ public

function getVariability
input ComponentRef cref;
output DAE.Const var;
output DAE.VarKind var;
algorithm
var := match cref
case CREF()
then
match Component.variability(InstNode.component(cref.node))
case DAE.VarKind.VARIABLE() then DAE.Const.C_VAR();
case DAE.VarKind.DISCRETE() then DAE.Const.C_VAR();
case DAE.VarKind.PARAM() then DAE.Const.C_PARAM();
case DAE.VarKind.CONST() then DAE.Const.C_CONST();
else DAE.Const.C_UNKNOWN();
end match;

else DAE.Const.C_UNKNOWN();
case CREF() then Component.variability(InstNode.component(cref.node));
else DAE.VarKind.VARIABLE();
end match;
end getVariability;

Expand Down
47 changes: 42 additions & 5 deletions Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -905,7 +905,7 @@ public
String s;
Expression e;
Type t;
DAE.Const c;
DAE.VarKind v;

case Call.UNTYPED_CALL()
algorithm
Expand All @@ -926,15 +926,15 @@ public
tnargs := {};

for arg in call.arguments loop
(e, t, c) := arg;
(e, t, v) := arg;
e := traverse(e, func);
targs := (e, t, c) :: targs;
targs := (e, t, v) :: targs;
end for;

for arg in call.named_args loop
(s, e, t, c) := arg;
(s, e, t, v) := arg;
e := traverse(e, func);
tnargs := (s, e, t, c) :: tnargs;
tnargs := (s, e, t, v) :: tnargs;
end for;
then
Call.ARG_TYPED_CALL(call.ref, listReverse(targs), listReverse(tnargs));
Expand Down Expand Up @@ -1089,5 +1089,42 @@ public
end match;
end expandCref4;

function arrayFirstScalar
"Returns the first scalar element of an array. Fails if the array is empty."
input Expression arrayExp;
output Expression exp;
algorithm
exp := match arrayExp
case ARRAY() then arrayFirstScalar(listHead(arrayExp.elements));
else arrayExp;
end match;
end arrayFirstScalar;

function arrayAllEqual
"Checks if all scalar elements in an array are equal to each other."
input Expression arrayExp;
output Boolean allEqual;
algorithm
allEqual := matchcontinue arrayExp
case ARRAY()
then arrayAllEqual2(arrayExp, arrayFirstScalar(arrayExp));
else true;
end matchcontinue;
end arrayAllEqual;

function arrayAllEqual2
input Expression arrayExp;
input Expression element;
output Boolean allEqual;
algorithm
allEqual := match arrayExp
case ARRAY(elements = ARRAY() :: _)
then List.map1BoolAnd(arrayExp.elements, arrayAllEqual2, element);
case ARRAY()
then List.map1BoolAnd(arrayExp.elements, isEqual, element);
else true;
end match;
end arrayAllEqual2;

annotation(__OpenModelica_Interface="frontend");
end NFExpression;

0 comments on commit 50d02d9

Please sign in to comment.