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

Commit

Permalink
[NF] Implement structural parameter handling.
Browse files Browse the repository at this point in the history
- Added Variability.STRUCTURAL_PARAMETER used to mark structural
  parameters.
- Changed the condition for evaluating bindings to include structural
  parameters, and changed the conditions for evaluation various
  expressions to exclude non-structural parameters.
- Added Variability.IMPLICITLY_DISCRETE used for discrete-time variables
  that are not declared as such, e.g. Integer, Boolean, etc variables.

Belonging to [master]:
  - #2335
  - OpenModelica/OpenModelica-testsuite#912
  • Loading branch information
perost authored and OpenModelica-Hudson committed Apr 3, 2018
1 parent 0616b65 commit 87a0162
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 59 deletions.
68 changes: 47 additions & 21 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -150,8 +150,8 @@ uniontype Call
CallAttributes attributes;
end TYPED_CALL;

// Right now this represnts only array() calls.
// Any other mapping call e.g. F(i for i in ...) is conveted to
// Right now this represents only array() calls.
// Any other mapping call e.g. F(i for i in ...) is converted to
// array(F(i) for i in ...) at instIteratorCall().
// So the fn is always NFBuiltinFuncs.ARRAY_FUNC.

Expand Down Expand Up @@ -432,7 +432,7 @@ uniontype Call
input SourceInfo info;
output Expression outExp;
output Type ty;
output Variability variability;
output Variability var;
protected
Call call;
list<Expression> args;
Expand All @@ -441,11 +441,11 @@ uniontype Call
case Expression.CALL(UNTYPED_CALL())
algorithm
if(builtinSpecialHandling(callExp.call)) then
(outExp, ty, variability) := typeSpecialBuiltinFunction(callExp.call, origin, info);
(outExp, ty, var) := typeSpecialBuiltinFunction(callExp.call, origin, info);
else
call := typeMatchNormalCall(callExp.call, origin, info);
ty := getType(call);
variability := getVariability(call);
var := variability(call);

if isRecordConstructor(call) then
outExp := toRecordExpression(call, ty);
Expand All @@ -460,21 +460,21 @@ uniontype Call
algorithm
call := typeMapIteratorCall(callExp.call, origin, info);
ty := getType(call);
variability := getVariability(call);
var := variability(call);
then
Expression.CALL(call);

case Expression.CALL(call as TYPED_CALL())
algorithm
ty := call.ty;
variability := call.var;
var := call.var;
then
callExp;

case Expression.CALL(call as TYPED_MAP_CALL())
algorithm
ty := call.ty;
variability := call.var;
var := call.var;
then
callExp;

Expand Down Expand Up @@ -971,20 +971,47 @@ uniontype Call
end match;
end setType;

function variabilityOf
function variability
input Call call;
output Variability var;
algorithm
var := match call
local
Boolean var_set;

case UNTYPED_CALL()
algorithm
var_set := true;

if ComponentRef.isSimple(call.ref) then
var := match ComponentRef.firstName(call.ref)
case "change" then Variability.DISCRETE;
case "edge" then Variability.DISCRETE;
case "pre" then Variability.DISCRETE;
case "ndims" then Variability.PARAMETER;
case "cardinality" then Variability.PARAMETER;
else algorithm var_set := false; then Variability.CONTINUOUS;
end match;
end if;

if not var_set then
var := Expression.variabilityList(call.arguments);

for narg in call.named_args loop
var := Prefixes.variabilityMax(var, Expression.variability(Util.tuple22(narg)));
end for;
end if;
then
var;

case UNTYPED_MAP_CALL() then Expression.variability(call.exp);
case TYPED_CALL() then call.var;
case TYPED_MAP_CALL() then call.var;
else algorithm
Error.assertion(false, getInstanceName() + " got untyped call", sourceInfo());
then fail();
end match;
end variabilityOf;

function getVariability = variabilityOf;
end variability;

function makeBuiltinCat
input Integer n;
Expand Down Expand Up @@ -1040,7 +1067,7 @@ uniontype Call
ty::tys2 := tys2;
pos := pos-1;
ty2 := Type.setArrayElementType(ty, resTy);
(arg2, ty1, mk) := TypeCheck.matchTypes(ty, ty2, arg);
(arg2, ty1, mk) := TypeCheck.matchTypes(ty, ty2, arg, allowUnknown = true);
if TypeCheck.isIncompatibleMatch(mk) then
Error.addSourceMessageAndFail(Error.ARG_TYPE_MISMATCH, {String(pos), "cat", "arg", Expression.toString(arg), Type.toString(ty), Type.toString(ty2)}, info);
end if;
Expand Down Expand Up @@ -1449,7 +1476,7 @@ protected

call := matchTypedNormalCall(call, origin, info);
outType := getType(call);
var := getVariability(call);
var := variability(call);
callExp := Expression.CALL(call);
end typeStringCall;

Expand Down Expand Up @@ -1547,7 +1574,7 @@ protected
fail();
end if;

if var > Variability.DISCRETE then
if var == Variability.CONTINUOUS then
Error.addSourceMessageAndFail(Error.INVALID_ARGUMENT_VARIABILITY,
{"1", ComponentRef.toString(fn_ref), Prefixes.variabilityString(Variability.DISCRETE),
Expression.toString(arg), Prefixes.variabilityString(var)}, info);
Expand Down Expand Up @@ -1717,7 +1744,7 @@ protected
argtycall := matchTypedNormalCall(argtycall, origin, info);
callExp := Expression.CALL(unboxArgs(argtycall));
ty := Type.arrayElementType(Util.tuple32(listHead(args)));
var := getVariability(argtycall);
var := variability(argtycall);
// TODO: check basic type in two argument overload.
// check arrays of simple types in one argument overload.
// fix return type.
Expand Down Expand Up @@ -1746,7 +1773,7 @@ protected
then Type.arrayElementType(Expression.typeOf(arg));
end match;

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

Expand Down Expand Up @@ -1872,8 +1899,7 @@ protected
for arg in dimensionArgs loop
(arg, arg_ty, arg_var) := Typing.typeExp(arg, origin, info);

if arg_var <= Variability.PARAMETER then
arg := Ceval.evalExp(arg, Ceval.EvalTarget.IGNORE_ERRORS());
if arg_var <= Variability.STRUCTURAL_PARAMETER then
arg := SimplifyExp.simplifyExp(arg);
arg_ty := Expression.typeOf(arg);
end if;
Expand All @@ -1896,7 +1922,7 @@ protected
{fn} := typeCachedFunctions(fnRef);
ty := Type.ARRAY(fillType, dims);

if variability <= Variability.PARAMETER and intBitAnd(origin, ExpOrigin.FUNCTION) == 0 then
if variability <= Variability.STRUCTURAL_PARAMETER and intBitAnd(origin, ExpOrigin.FUNCTION) == 0 then
callExp := Ceval.evalBuiltinCall(fn, ty_args, Ceval.EvalTarget.IGNORE_ERRORS());
else
callExp := Expression.CALL(makeBuiltinCall2(fn, ty_args, ty, variability));
Expand Down Expand Up @@ -2247,7 +2273,7 @@ protected
argtycall := matchTypedNormalCall(argtycall, origin, info);

ty := getType(argtycall);
var := getVariability(argtycall);
var := variability(argtycall);
callExp := Expression.CALL(unboxArgs(argtycall));
// TODO: Check cardinality restrictions, 3.7.2.3.
end typeCardinalityCall;
Expand Down
30 changes: 28 additions & 2 deletions Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -96,11 +96,11 @@ constant Component.Attributes CONSTANT_ATTR =
Replaceable.NOT_REPLACEABLE()
);

constant Component.Attributes DISCRETE_ATTR =
constant Component.Attributes IMPL_DISCRETE_ATTR =
Component.Attributes.ATTRIBUTES(
ConnectorType.POTENTIAL,
Parallelism.NON_PARALLEL,
Variability.DISCRETE,
Variability.IMPLICITLY_DISCRETE,
Direction.NONE,
InnerOuter.NOT_INNER_OUTER,
false,
Expand Down Expand Up @@ -516,6 +516,32 @@ uniontype Component
end match;
end variability;

function setVariability
input Variability variability;
input output Component component;
algorithm
() := match component
local
Attributes attr;

case UNTYPED_COMPONENT(attributes = attr)
algorithm
attr.variability := variability;
component.attributes := attr;
then
();

case TYPED_COMPONENT(attributes = attr)
algorithm
attr.variability := variability;
component.attributes := attr;
then
();

else ();
end match;
end setVariability;

function isConst
input Component component;
output Boolean isConst = variability(component) == Variability.CONSTANT;
Expand Down
64 changes: 62 additions & 2 deletions Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -44,6 +44,7 @@ protected
import System;
import NFClass.Class;
import List;
import Prefixes = NFPrefixes;

import ComponentRef = NFComponentRef;

Expand Down Expand Up @@ -147,6 +148,16 @@ public
end match;
end isEmpty;

function isSimple
input ComponentRef cref;
output Boolean isSimple;
algorithm
isSimple := match cref
case CREF(restCref = EMPTY()) then true;
else false;
end match;
end isSimple;

function node
input ComponentRef cref;
output InstNode node;
Expand Down Expand Up @@ -241,15 +252,41 @@ public
end match;
end getSubscriptedType2;

function getVariability
function nodeVariability
"Returns the variability of the component node the cref refers to."
input ComponentRef cref;
output Variability var;
algorithm
var := match cref
case CREF() then Component.variability(InstNode.component(cref.node));
else Variability.CONTINUOUS;
end match;
end getVariability;
end nodeVariability;

function subscriptsVariability
input ComponentRef cref;
input output Variability var = Variability.CONSTANT;
algorithm
() := match cref
case CREF(origin = Origin.CREF)
algorithm
for sub in cref.subscripts loop
var := Prefixes.variabilityMax(var, Subscript.variability(sub));
end for;
then
();

else ();
end match;
end subscriptsVariability;

function variability
"Returns the variability of the cref, with the variability of the subscripts
taken into account."
input ComponentRef cref;
output Variability var = Prefixes.variabilityMax(nodeVariability(cref),
subscriptsVariability(cref));
end variability;

function addSubscript
input Subscript subscript;
Expand Down Expand Up @@ -358,6 +395,29 @@ public
end match;
end transferSubscripts;

function foldSubscripts<ArgT>
input ComponentRef cref;
input FuncT func;
input output ArgT arg;

partial function FuncT
input Subscript subscript;
input output ArgT arg;
end FuncT;
algorithm
arg := match cref
case CREF(origin = Origin.CREF)
algorithm
for sub in cref.subscripts loop
arg := func(sub, arg);
end for;
then
foldSubscripts(cref.restCref, func, arg);

else arg;
end match;
end foldSubscripts;

function compare
input ComponentRef cref1;
input ComponentRef cref2;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -149,7 +149,7 @@ algorithm
then
DAE.VAR(
dcref,
Prefixes.variabilityToDAE(attr.variability, ty),
Prefixes.variabilityToDAE(attr.variability),
Prefixes.directionToDAE(dir),
Prefixes.parallelismToDAE(attr.parallelism),
Prefixes.visibilityToDAE(vis),
Expand Down

0 comments on commit 87a0162

Please sign in to comment.