Skip to content

Commit

Permalink
[NF] Improve typing of record field dimensions.
Browse files Browse the repository at this point in the history
- Handle typing of record field dimensions where the record instance has
  a binding.

Belonging to [master]:
  - OpenModelica/OMCompiler#2711
  - OpenModelica/OpenModelica-testsuite#1048
  • Loading branch information
perost authored and OpenModelica-Hudson committed Oct 10, 2018
1 parent 521d55e commit bac7d12
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 23 deletions.
40 changes: 40 additions & 0 deletions Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -250,6 +250,46 @@ public
end match;
end isRecordExp;

function recordFieldBinding
input String fieldName;
input Binding recordBinding;
output Binding fieldBinding = recordBinding;
protected
Expression exp;
Type ty;
Variability var;
algorithm
fieldBinding := match fieldBinding
case UNTYPED_BINDING()
algorithm
fieldBinding.bindingExp := Expression.recordElement(fieldName, fieldBinding.bindingExp);
then
fieldBinding;

case TYPED_BINDING()
algorithm
exp := Expression.recordElement(fieldName, fieldBinding.bindingExp);
ty := Expression.typeOf(exp);
var := Expression.variability(exp);
then
TYPED_BINDING(exp, ty, var, fieldBinding.parents, fieldBinding.isEach, fieldBinding.info);

case FLAT_BINDING()
algorithm
exp := Expression.recordElement(fieldName, fieldBinding.bindingExp);
var := Expression.variability(exp);
then
FLAT_BINDING(exp, var);

case CEVAL_BINDING()
algorithm
fieldBinding.bindingExp := Expression.recordElement(fieldName, fieldBinding.bindingExp);
then
fieldBinding;

end match;
end recordFieldBinding;

function variability
input Binding binding;
output Variability var;
Expand Down
27 changes: 27 additions & 0 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -239,6 +239,9 @@ algorithm
then
Expression.tupleElement(exp1, exp.ty, exp.index);

case Expression.RECORD_ELEMENT()
then evalRecordElement(exp, target);

case Expression.MUTABLE()
algorithm
exp1 := evalExp(Mutable.access(exp.exp), target);
Expand Down Expand Up @@ -2800,6 +2803,30 @@ algorithm
result := Expression.applySubscripts(subs, result);
end evalSubscriptedExp;

function evalRecordElement
input Expression exp;
input EvalTarget target;
output Expression result;
protected
Expression e;
Integer index;
algorithm
Expression.RECORD_ELEMENT(recordExp = e, index = index) := exp;
e := evalExp(e, target);

result := match e
case Expression.RECORD()
then listGet(e.elements, index);

else
algorithm
Error.assertion(false, getInstanceName() + " could not evaluate " +
Expression.toString(exp), sourceInfo());
then
fail();
end match;
end evalRecordElement;

protected

function printUnboundError
Expand Down
16 changes: 16 additions & 0 deletions Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -206,6 +206,22 @@ uniontype Class
(node, isImport) := ClassTree.lookupElement(name, classTree(cls));
end lookupElement;

function lookupComponentIndex
input String name;
input Class cls;
output Integer index;
algorithm
index := ClassTree.lookupComponentIndex(name, classTree(cls));
end lookupComponentIndex;

function nthComponent
input Integer index;
input Class cls;
output InstNode component;
algorithm
component := ClassTree.nthComponent(index, classTree(cls));
end nthComponent;

function lookupAttributeBinding
input String name;
input Class cls;
Expand Down
13 changes: 13 additions & 0 deletions Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -861,6 +861,19 @@ public
LookupTree.get(lookupTree(tree), name);
end lookupComponentIndex;

function nthComponent
input Integer index;
input ClassTree tree;
output InstNode component;
algorithm
component := match tree
case PARTIAL_TREE() then arrayGet(tree.components, index);
case EXPANDED_TREE() then arrayGet(tree.components, index);
case INSTANTIATED_TREE() then Mutable.access(arrayGet(tree.components, index));
case FLAT_TREE() then arrayGet(tree.components, index);
end match;
end nthComponent;

function mapClasses
input ClassTree tree;
input FuncT func;
Expand Down
85 changes: 85 additions & 0 deletions Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -283,6 +283,13 @@ public
Type ty;
end TUPLE_ELEMENT;

record RECORD_ELEMENT
Expression recordExp;
Integer index;
String fieldName;
Type ty;
end RECORD_ELEMENT;

record BOX "MetaModelica boxed value"
Expression exp;
end BOX;
Expand Down Expand Up @@ -613,6 +620,17 @@ public
then
comp;

case RECORD_ELEMENT()
algorithm
RECORD_ELEMENT(recordExp = e1, index = i) := exp2;
comp := Util.intCompare(exp1.index, i);

if comp == 0 then
comp := compare(exp1.recordExp, e1);
end if;
then
comp;

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression.", sourceInfo());
Expand Down Expand Up @@ -694,6 +712,7 @@ public
case UNBOX() then exp.ty;
case SUBSCRIPTED_EXP() then exp.ty;
case TUPLE_ELEMENT() then exp.ty;
case RECORD_ELEMENT() then exp.ty;
case BOX() then Type.METABOXED(typeOf(exp.exp));
case MUTABLE() then typeOf(Mutable.access(exp.exp));
case EMPTY() then exp.ty;
Expand Down Expand Up @@ -723,6 +742,7 @@ public
case UNBOX() algorithm exp.ty := ty; then ();
case SUBSCRIPTED_EXP() algorithm exp.ty := ty; then ();
case TUPLE_ELEMENT() algorithm exp.ty := ty; then ();
case RECORD_ELEMENT() algorithm exp.ty := ty; then ();
else ();
end match;
end setType;
Expand Down Expand Up @@ -1351,6 +1371,7 @@ public
case CAST() then "CAST(" + Type.toString(exp.ty) + ", " + toString(exp.exp) + ")";
case SUBSCRIPTED_EXP() then toString(exp.exp) + Subscript.toStringList(exp.subscripts);
case TUPLE_ELEMENT() then toString(exp.tupleExp) + "[" + intString(exp.index) + "]";
case RECORD_ELEMENT() then toString(exp.recordExp) + "[field: " + exp.fieldName + "]";
case MUTABLE() then toString(Mutable.access(exp.exp));
case EMPTY() then "#EMPTY#";

Expand Down Expand Up @@ -1502,6 +1523,9 @@ public
case TUPLE_ELEMENT()
then DAE.TSUB(toDAE(exp.tupleExp), exp.index, Type.toDAE(exp.ty));

case RECORD_ELEMENT()
then DAE.RSUB(toDAE(exp.recordExp), exp.index, exp.fieldName, Type.toDAE(exp.ty));

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression '" + toString(exp) + "'", sourceInfo());
Expand Down Expand Up @@ -1714,6 +1738,12 @@ public
then
if referenceEq(exp.tupleExp, e1) then exp else TUPLE_ELEMENT(e1, exp.index, exp.ty);

case RECORD_ELEMENT()
algorithm
e1 := map(exp.recordExp, func);
then
if referenceEq(exp.recordExp, e1) then exp else RECORD_ELEMENT(e1, exp.index, exp.fieldName, exp.ty);

case BOX()
algorithm
e1 := map(exp.exp, func);
Expand Down Expand Up @@ -2040,6 +2070,12 @@ public
then
if referenceEq(exp.tupleExp, e1) then exp else TUPLE_ELEMENT(e1, exp.index, exp.ty);

case RECORD_ELEMENT()
algorithm
e1 := func(exp.recordExp);
then
if referenceEq(exp.recordExp, e1) then exp else RECORD_ELEMENT(e1, exp.index, exp.fieldName, exp.ty);

case BOX()
algorithm
e1 := func(exp.exp);
Expand Down Expand Up @@ -2341,6 +2377,7 @@ public
List.fold(exp.subscripts, function Subscript.foldExp(func = func), result);

case TUPLE_ELEMENT() then fold(exp.tupleExp, func, arg);
case RECORD_ELEMENT() then fold(exp.recordExp, func, arg);
case BOX() then fold(exp.exp, func, arg);
case MUTABLE() then fold(Mutable.access(exp.exp), func, arg);
else arg;
Expand Down Expand Up @@ -2596,6 +2633,7 @@ public
();

case TUPLE_ELEMENT() algorithm apply(exp.tupleExp, func); then ();
case RECORD_ELEMENT() algorithm apply(exp.recordExp, func); then ();
case BOX() algorithm apply(exp.exp, func); then ();
case MUTABLE() algorithm apply(Mutable.access(exp.exp), func); then ();
else ();
Expand Down Expand Up @@ -2912,6 +2950,12 @@ public
then
if referenceEq(exp.tupleExp, e1) then exp else TUPLE_ELEMENT(e1, exp.index, exp.ty);

case RECORD_ELEMENT()
algorithm
(e1, arg) := mapFold(exp.recordExp, func, arg);
then
if referenceEq(exp.recordExp, e1) then exp else RECORD_ELEMENT(e1, exp.index, exp.fieldName, exp.ty);

case MUTABLE()
algorithm
(e1, arg) := mapFold(Mutable.access(exp.exp), func, arg);
Expand Down Expand Up @@ -3225,6 +3269,12 @@ public
then
if referenceEq(exp.tupleExp, e1) then exp else TUPLE_ELEMENT(e1, exp.index, exp.ty);

case RECORD_ELEMENT()
algorithm
(e1, arg) := func(exp.recordExp, arg);
then
if referenceEq(exp.recordExp, e1) then exp else RECORD_ELEMENT(e1, exp.index, exp.fieldName, exp.ty);

case MUTABLE()
algorithm
(e1, arg) := func(Mutable.access(exp.exp), arg);
Expand Down Expand Up @@ -3535,6 +3585,9 @@ public
case TUPLE_ELEMENT()
then contains(exp.tupleExp, func);

case RECORD_ELEMENT()
then contains(exp.recordExp, func);

case BOX() then contains(exp.exp, func);
else false;
end match;
Expand Down Expand Up @@ -4171,6 +4224,7 @@ public
case SUBSCRIPTED_EXP()
then Prefixes.variabilityMax(variability(exp.exp), Subscript.variabilityList(exp.subscripts));
case TUPLE_ELEMENT() then variability(exp.tupleExp);
case RECORD_ELEMENT() then variability(exp.recordExp);
case BOX() then variability(exp.exp);
else
algorithm
Expand Down Expand Up @@ -4303,6 +4357,37 @@ public
end match;
end tupleElement;

function recordElement
input String elementName;
input Expression recordExp;
output Expression outExp;
algorithm
outExp := match recordExp
local
InstNode node;
Class cls;
Type ty;
Integer index;

case RECORD(ty = Type.COMPLEX(cls = node))
algorithm
cls := InstNode.getClass(node);
index := Class.lookupComponentIndex(elementName, cls);
then
listGet(recordExp.elements, index);

else
algorithm
Type.COMPLEX(cls = node) := typeOf(recordExp);
cls := InstNode.getClass(node);
index := Class.lookupComponentIndex(elementName, cls);
ty := InstNode.getType(Class.nthComponent(index, cls));
then
RECORD_ELEMENT(recordExp, index, elementName, ty);

end match;
end recordElement;

function splitRecord
input Expression recordExp;
output list<Expression> recordFields;
Expand Down

0 comments on commit bac7d12

Please sign in to comment.