Skip to content

Commit 19ce60a

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Minor fixes for type attributes.
- 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
1 parent c9c4c30 commit 19ce60a

File tree

5 files changed

+84
-16
lines changed

5 files changed

+84
-16
lines changed

Compiler/NFFrontEnd/NFClassTree.mo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,11 @@ public
613613
then
614614
();
615615

616-
case Class.EXPANDED_DERIVED()
616+
case Class.EXPANDED_DERIVED(baseClass = node)
617617
algorithm
618-
(node, _, classCount, compCount) := instantiate(cls.baseClass);
618+
node := InstNode.setNodeType(
619+
InstNodeType.BASE_CLASS(clsNode, InstNode.definition(node)), node);
620+
(node, _, classCount, compCount) := instantiate(node);
619621
cls.baseClass := node;
620622
then
621623
();

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,6 +2733,30 @@ public
27332733
Expression.ARRAY(elements = elements) := array;
27342734
end arrayElements;
27352735

2736+
function arrayScalarElements
2737+
input Expression exp;
2738+
output list<Expression> elements;
2739+
algorithm
2740+
elements := listReverseInPlace(arrayScalarElements_impl(exp, {}));
2741+
end arrayScalarElements;
2742+
2743+
function arrayScalarElements_impl
2744+
input Expression exp;
2745+
input output list<Expression> elements;
2746+
algorithm
2747+
elements := match exp
2748+
case ARRAY()
2749+
algorithm
2750+
for e in exp.elements loop
2751+
elements := arrayScalarElements_impl(e, elements);
2752+
end for;
2753+
then
2754+
elements;
2755+
2756+
else exp :: elements;
2757+
end match;
2758+
end arrayScalarElements_impl;
2759+
27362760
function hasArrayCall
27372761
"Returns true if the given expression contains a function call that returns
27382762
an array, otherwise false."

Compiler/NFFrontEnd/NFExpressionIterator.mo

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected
3434
import ExpressionIterator = NFExpressionIterator;
3535
import ComponentRef = NFComponentRef;
3636
import BindingOrigin = NFBindingOrigin;
37+
import NFInstNode.InstNode;
3738

3839
public
3940
import Expression = NFExpression;
@@ -55,6 +56,11 @@ public
5556
record NONE_ITERATOR
5657
end NONE_ITERATOR;
5758

59+
record REPEAT_ITERATOR
60+
list<Expression> current;
61+
list<Expression> all;
62+
end REPEAT_ITERATOR;
63+
5864
function fromExp
5965
input Expression exp;
6066
output ExpressionIterator iterator;
@@ -108,9 +114,17 @@ public
108114
output ExpressionIterator iterator;
109115
algorithm
110116
iterator := match binding
117+
local
118+
list<Expression> expl;
119+
120+
case Binding.TYPED_BINDING() guard BindingOrigin.isFromClass(binding.origin)
121+
algorithm
122+
expl := Expression.arrayScalarElements(binding.bindingExp);
123+
then
124+
if listLength(expl) == 1 then EACH_ITERATOR(listHead(expl)) else REPEAT_ITERATOR(expl, expl);
125+
111126
case Binding.TYPED_BINDING()
112-
then if Binding.isEach(binding) or BindingOrigin.isFromClass(binding.origin) then
113-
EACH_ITERATOR(binding.bindingExp) else fromExp(binding.bindingExp);
127+
then if binding.isEach then EACH_ITERATOR(binding.bindingExp) else fromExp(binding.bindingExp);
114128

115129
case Binding.FLAT_BINDING()
116130
then SCALAR_ITERATOR(binding.bindingExp);
@@ -126,6 +140,7 @@ public
126140
case SCALAR_ITERATOR() then true;
127141
case EACH_ITERATOR() then true;
128142
case NONE_ITERATOR() then false;
143+
case REPEAT_ITERATOR() then true;
129144
end match;
130145
end hasNext;
131146

@@ -155,6 +170,17 @@ public
155170
then (NONE_ITERATOR(), iterator.exp);
156171

157172
case EACH_ITERATOR() then (iterator, iterator.exp);
173+
174+
case REPEAT_ITERATOR(rest, arr)
175+
algorithm
176+
if not listEmpty(rest) then
177+
next :: rest := rest;
178+
else
179+
next :: rest := arr;
180+
end if;
181+
then
182+
(REPEAT_ITERATOR(rest, arr), next);
183+
158184
end match;
159185
end next;
160186

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,10 +2056,16 @@ algorithm
20562056

20572057
crefExp := match comp
20582058
case Component.ITERATOR()
2059-
then Expression.CREF(Type.UNKNOWN(), ComponentRef.makeIterator(cref.node, comp.ty));
2059+
algorithm
2060+
checkUnsubscriptableCref(cref, cref.subscripts, info);
2061+
then
2062+
Expression.CREF(Type.UNKNOWN(), ComponentRef.makeIterator(cref.node, comp.ty));
20602063

20612064
case Component.ENUM_LITERAL()
2062-
then comp.literal;
2065+
algorithm
2066+
checkUnsubscriptableCref(cref, cref.subscripts, info);
2067+
then
2068+
comp.literal;
20632069

20642070
case Component.TYPE_ATTRIBUTE()
20652071
algorithm
@@ -2078,6 +2084,7 @@ algorithm
20782084

20792085
end match;
20802086
else
2087+
checkUnsubscriptableCref(cref, cref.subscripts, info);
20812088
ty := InstNode.getType(cref.node);
20822089

20832090
ty := match ty
@@ -2101,6 +2108,18 @@ algorithm
21012108
end match;
21022109
end instCref;
21032110

2111+
function checkUnsubscriptableCref
2112+
input ComponentRef cref;
2113+
input list<Subscript> subscripts;
2114+
input SourceInfo info;
2115+
algorithm
2116+
if not listEmpty(subscripts) then
2117+
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
2118+
{ComponentRef.toString(cref), String(listLength(subscripts)), "0"}, info);
2119+
fail();
2120+
end if;
2121+
end checkUnsubscriptableCref;
2122+
21042123
function instCrefSubscripts
21052124
input output ComponentRef cref;
21062125
input InstNode scope;
@@ -2109,19 +2128,10 @@ algorithm
21092128
() := match cref
21102129
local
21112130
ComponentRef rest_cr;
2112-
Integer dims;
21132131

21142132
case ComponentRef.CREF()
21152133
algorithm
21162134
if not listEmpty(cref.subscripts) then
2117-
dims := InstNode.countDimensions(cref.node, 1);
2118-
2119-
if listLength(cref.subscripts) > dims then
2120-
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
2121-
{ComponentRef.toString(cref), String(listLength(cref.subscripts)), String(dims)}, info);
2122-
fail();
2123-
end if;
2124-
21252135
cref.subscripts := list(instSubscript(s, scope, info) for s in cref.subscripts);
21262136
end if;
21272137

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ algorithm
862862
if isSome(binding_origin.node) then
863863
SOME(mod_parent) := binding_origin.node;
864864
else
865-
mod_parent := component;
865+
mod_parent := InstNode.getDerivedNode(component);
866866
end if;
867867

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

1351+
if listLength(subscripts) > listLength(dims) then
1352+
Error.addSourceMessage(Error.WRONG_NUMBER_OF_SUBSCRIPTS,
1353+
{ComponentRef.toString(cref), String(listLength(subscripts)), String(listLength(dims))}, info);
1354+
fail();
1355+
end if;
1356+
13511357
for s in subscripts loop
13521358
dim :: dims := dims;
13531359
(sub, var) := typeSubscript(s, dim, cref, i, next_origin, info);

0 commit comments

Comments
 (0)