Skip to content

Commit

Permalink
[NF] Minor fixes for type attributes.
Browse files Browse the repository at this point in the history
- Set correct basetype in ClassTree.instantiate for derived classes.
- Fixed scalarization of non-each class modifiers on array components.
- Moved subscript count check from the instantiation to the typing,
  since dimensions from types aren't added to components until then.
- Use correct parent node when type checking type attributes.

Belonging to [master]:
  - OpenModelica/OMCompiler#2384
  - OpenModelica/OpenModelica-testsuite#929
  • Loading branch information
perost authored and OpenModelica-Hudson committed Apr 21, 2018
1 parent c9c4c30 commit 19ce60a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -613,9 +613,11 @@ public
then
();

case Class.EXPANDED_DERIVED()
case Class.EXPANDED_DERIVED(baseClass = node)
algorithm
(node, _, classCount, compCount) := instantiate(cls.baseClass);
node := InstNode.setNodeType(
InstNodeType.BASE_CLASS(clsNode, InstNode.definition(node)), node);
(node, _, classCount, compCount) := instantiate(node);
cls.baseClass := node;
then
();
Expand Down
24 changes: 24 additions & 0 deletions Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -2733,6 +2733,30 @@ public
Expression.ARRAY(elements = elements) := array;
end arrayElements;

function arrayScalarElements
input Expression exp;
output list<Expression> elements;
algorithm
elements := listReverseInPlace(arrayScalarElements_impl(exp, {}));
end arrayScalarElements;

function arrayScalarElements_impl
input Expression exp;
input output list<Expression> elements;
algorithm
elements := match exp
case ARRAY()
algorithm
for e in exp.elements loop
elements := arrayScalarElements_impl(e, elements);
end for;
then
elements;

else exp :: elements;
end match;
end arrayScalarElements_impl;

function hasArrayCall
"Returns true if the given expression contains a function call that returns
an array, otherwise false."
Expand Down
30 changes: 28 additions & 2 deletions Compiler/NFFrontEnd/NFExpressionIterator.mo
Expand Up @@ -34,6 +34,7 @@ protected
import ExpressionIterator = NFExpressionIterator;
import ComponentRef = NFComponentRef;
import BindingOrigin = NFBindingOrigin;
import NFInstNode.InstNode;

public
import Expression = NFExpression;
Expand All @@ -55,6 +56,11 @@ public
record NONE_ITERATOR
end NONE_ITERATOR;

record REPEAT_ITERATOR
list<Expression> current;
list<Expression> all;
end REPEAT_ITERATOR;

function fromExp
input Expression exp;
output ExpressionIterator iterator;
Expand Down Expand Up @@ -108,9 +114,17 @@ public
output ExpressionIterator iterator;
algorithm
iterator := match binding
local
list<Expression> expl;

case Binding.TYPED_BINDING() guard BindingOrigin.isFromClass(binding.origin)
algorithm
expl := Expression.arrayScalarElements(binding.bindingExp);
then
if listLength(expl) == 1 then EACH_ITERATOR(listHead(expl)) else REPEAT_ITERATOR(expl, expl);

case Binding.TYPED_BINDING()
then if Binding.isEach(binding) or BindingOrigin.isFromClass(binding.origin) then
EACH_ITERATOR(binding.bindingExp) else fromExp(binding.bindingExp);
then if binding.isEach then EACH_ITERATOR(binding.bindingExp) else fromExp(binding.bindingExp);

case Binding.FLAT_BINDING()
then SCALAR_ITERATOR(binding.bindingExp);
Expand All @@ -126,6 +140,7 @@ public
case SCALAR_ITERATOR() then true;
case EACH_ITERATOR() then true;
case NONE_ITERATOR() then false;
case REPEAT_ITERATOR() then true;
end match;
end hasNext;

Expand Down Expand Up @@ -155,6 +170,17 @@ public
then (NONE_ITERATOR(), iterator.exp);

case EACH_ITERATOR() then (iterator, iterator.exp);

case REPEAT_ITERATOR(rest, arr)
algorithm
if not listEmpty(rest) then
next :: rest := rest;
else
next :: rest := arr;
end if;
then
(REPEAT_ITERATOR(rest, arr), next);

end match;
end next;

Expand Down
32 changes: 21 additions & 11 deletions Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -2056,10 +2056,16 @@ algorithm

crefExp := match comp
case Component.ITERATOR()
then Expression.CREF(Type.UNKNOWN(), ComponentRef.makeIterator(cref.node, comp.ty));
algorithm
checkUnsubscriptableCref(cref, cref.subscripts, info);
then
Expression.CREF(Type.UNKNOWN(), ComponentRef.makeIterator(cref.node, comp.ty));

case Component.ENUM_LITERAL()
then comp.literal;
algorithm
checkUnsubscriptableCref(cref, cref.subscripts, info);
then
comp.literal;

case Component.TYPE_ATTRIBUTE()
algorithm
Expand All @@ -2078,6 +2084,7 @@ algorithm

end match;
else
checkUnsubscriptableCref(cref, cref.subscripts, info);
ty := InstNode.getType(cref.node);

ty := match ty
Expand All @@ -2101,6 +2108,18 @@ algorithm
end match;
end instCref;

function checkUnsubscriptableCref
input ComponentRef cref;
input list<Subscript> subscripts;
input SourceInfo info;
algorithm
if not listEmpty(subscripts) then
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
{ComponentRef.toString(cref), String(listLength(subscripts)), "0"}, info);
fail();
end if;
end checkUnsubscriptableCref;

function instCrefSubscripts
input output ComponentRef cref;
input InstNode scope;
Expand All @@ -2109,19 +2128,10 @@ algorithm
() := match cref
local
ComponentRef rest_cr;
Integer dims;

case ComponentRef.CREF()
algorithm
if not listEmpty(cref.subscripts) then
dims := InstNode.countDimensions(cref.node, 1);

if listLength(cref.subscripts) > dims then
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
{ComponentRef.toString(cref), String(listLength(cref.subscripts)), String(dims)}, info);
fail();
end if;

cref.subscripts := list(instSubscript(s, scope, info) for s in cref.subscripts);
end if;

Expand Down
8 changes: 7 additions & 1 deletion Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -862,7 +862,7 @@ algorithm
if isSome(binding_origin.node) then
SOME(mod_parent) := binding_origin.node;
else
mod_parent := component;
mod_parent := InstNode.getDerivedNode(component);
end if;

// Type and type check the attribute.
Expand Down Expand Up @@ -1348,6 +1348,12 @@ algorithm
next_origin := intBitOr(origin, ExpOrigin.SUBSCRIPT);
i := 1;

if listLength(subscripts) > listLength(dims) then
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
{ComponentRef.toString(cref), String(listLength(subscripts)), String(listLength(dims))}, info);
fail();
end if;

for s in subscripts loop
dim :: dims := dims;
(sub, var) := typeSubscript(s, dim, cref, i, next_origin, info);
Expand Down

0 comments on commit 19ce60a

Please sign in to comment.