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

Commit 2bfa83f

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix expansion of cref slices.
Belonging to [master]: - #2609 - OpenModelica/OpenModelica-testsuite#1015
1 parent 2e978c5 commit 2bfa83f

File tree

4 files changed

+78
-20
lines changed

4 files changed

+78
-20
lines changed

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public
690690
case CREF(ty = Type.ARRAY())
691691
algorithm
692692
dims := Type.arrayDims(cref.ty);
693-
subs := Subscript.expandList(cref.subscripts, dims);
693+
subs := Subscript.scalarizeList(cref.subscripts, dims);
694694
subs := List.combination(subs);
695695
then
696696
list(setSubscripts(s, cref) for s in subs);

Compiler/NFFrontEnd/NFExpandExp.mo

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,34 @@ public
111111
output Expression arrayExp;
112112
output Boolean expanded;
113113
protected
114-
list<list<list<Subscript>>> subs;
114+
list<list<Subscript>> subs;
115115
algorithm
116116
(arrayExp, expanded) := match crefExp
117117
case Expression.CREF(cref = ComponentRef.CREF())
118-
guard Type.hasKnownSize(crefExp.ty)
119118
algorithm
120-
subs := expandCref2(crefExp.cref);
121-
122-
if listEmpty(subs) then
119+
if Type.hasZeroDimension(crefExp.ty) then
123120
arrayExp := Expression.ARRAY(Type.ARRAY(Type.arrayElementType(crefExp.ty), {Dimension.fromInteger(0)}), {});
124-
else
121+
expanded := true;
122+
elseif Type.hasKnownSize(crefExp.ty) then
123+
subs := expandCref2(crefExp.cref);
125124
arrayExp := expandCref3(subs, crefExp.cref, Type.arrayElementType(crefExp.ty));
125+
expanded := true;
126+
else
127+
arrayExp := crefExp;
128+
expanded := false;
126129
end if;
127130
then
128-
(arrayExp, true);
131+
(arrayExp, expanded);
129132

130133
else (crefExp, false);
131134
end match;
132135
end expandCref;
133136

134137
function expandCref2
135138
input ComponentRef cref;
136-
input output list<list<list<Subscript>>> subs = {};
139+
input output list<list<Subscript>> subs = {};
137140
protected
138-
list<list<Subscript>> cr_subs = {};
141+
list<Subscript> cr_subs = {};
139142
list<Dimension> dims;
140143

141144
import NFComponentRef.Origin;
@@ -154,7 +157,7 @@ public
154157
end expandCref2;
155158

156159
function expandCref3
157-
input list<list<list<Subscript>>> subs;
160+
input list<list<Subscript>> subs;
158161
input ComponentRef cref;
159162
input Type crefType;
160163
input list<list<Subscript>> accum = {};
@@ -167,26 +170,29 @@ public
167170
end expandCref3;
168171

169172
function expandCref4
170-
input list<list<Subscript>> subs;
173+
input list<Subscript> subs;
171174
input list<Subscript> comb = {};
172175
input list<list<Subscript>> accum = {};
173-
input list<list<list<Subscript>>> restSubs;
176+
input list<list<Subscript>> restSubs;
174177
input ComponentRef cref;
175178
input Type crefType;
176179
output Expression arrayExp;
177180
protected
178181
list<Expression> expl = {};
179182
Type arr_ty;
183+
list<Subscript> slice, rest;
180184
algorithm
181185
arrayExp := match subs
182186
case {} then expandCref3(restSubs, cref, crefType, listReverse(comb) :: accum);
183-
else
187+
188+
case Subscript.EXPANDED_SLICE(indices = slice) :: rest
184189
algorithm
185-
expl := list(expandCref4(listRest(subs), sub :: comb, accum, restSubs, cref, crefType)
186-
for sub in listHead(subs));
190+
expl := list(expandCref4(rest, idx :: comb, accum, restSubs, cref, crefType) for idx in slice);
187191
arr_ty := Type.liftArrayLeft(Expression.typeOf(listHead(expl)), Dimension.fromExpList(expl));
188192
then
189193
Expression.ARRAY(arr_ty, expl);
194+
195+
else expandCref4(listRest(subs), listHead(subs) :: comb, accum, restSubs, cref, crefType);
190196
end match;
191197
end expandCref4;
192198

Compiler/NFFrontEnd/NFSubscript.mo

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public
6363
Expression slice;
6464
end SLICE;
6565

66+
record EXPANDED_SLICE
67+
list<Subscript> indices;
68+
end EXPANDED_SLICE;
69+
6670
record WHOLE end WHOLE;
6771

6872
function fromExp
@@ -295,6 +299,8 @@ public
295299
case UNTYPED() then Expression.toString(subscript.exp);
296300
case INDEX() then Expression.toString(subscript.index);
297301
case SLICE() then Expression.toString(subscript.slice);
302+
case EXPANDED_SLICE()
303+
then List.toString(subscript.indices, toString, "", "{", ", ", "}", false);
298304
case WHOLE() then ":";
299305
end match;
300306
end toString;
@@ -338,7 +344,7 @@ public
338344
end match;
339345
end toDimension;
340346

341-
function expand
347+
function scalarize
342348
input Subscript subscript;
343349
input Dimension dimension;
344350
output list<Subscript> subscripts;
@@ -350,9 +356,9 @@ public
350356
case WHOLE()
351357
then RangeIterator.map(RangeIterator.fromDim(dimension), makeIndex);
352358
end match;
353-
end expand;
359+
end scalarize;
354360

355-
function expandList
361+
function scalarizeList
356362
input list<Subscript> subscripts;
357363
input list<Dimension> dimensions;
358364
output list<list<Subscript>> outSubscripts = {};
@@ -363,7 +369,7 @@ public
363369
algorithm
364370
for s in subscripts loop
365371
dim :: rest_dims := rest_dims;
366-
subs := expand(s, dim);
372+
subs := scalarize(s, dim);
367373

368374
if listEmpty(subs) then
369375
outSubscripts := {};
@@ -384,6 +390,42 @@ public
384390
end if;
385391
end for;
386392

393+
outSubscripts := listReverse(outSubscripts);
394+
end scalarizeList;
395+
396+
function expand
397+
input Subscript subscript;
398+
input Dimension dimension;
399+
output Subscript outSubscript;
400+
algorithm
401+
outSubscript := match subscript
402+
case INDEX() then subscript;
403+
case SLICE()
404+
then EXPANDED_SLICE(list(INDEX(e) for e in Expression.arrayElements(ExpandExp.expand(subscript.slice))));
405+
case WHOLE() then EXPANDED_SLICE(RangeIterator.map(RangeIterator.fromDim(dimension), makeIndex));
406+
end match;
407+
end expand;
408+
409+
function expandList
410+
input list<Subscript> subscripts;
411+
input list<Dimension> dimensions;
412+
output list<Subscript> outSubscripts = {};
413+
protected
414+
Dimension dim;
415+
list<Dimension> rest_dims = dimensions;
416+
Subscript sub;
417+
algorithm
418+
for s in subscripts loop
419+
dim :: rest_dims := rest_dims;
420+
sub := expand(s, dim);
421+
outSubscripts := sub :: outSubscripts;
422+
end for;
423+
424+
for d in rest_dims loop
425+
sub := EXPANDED_SLICE(RangeIterator.map(RangeIterator.fromDim(d), makeIndex));
426+
outSubscripts := sub :: outSubscripts;
427+
end for;
428+
387429
outSubscripts := listReverse(outSubscripts);
388430
end expandList;
389431

Compiler/NFFrontEnd/NFType.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,16 @@ public
539539
end match;
540540
end hasKnownSize;
541541

542+
function hasZeroDimension
543+
input Type ty;
544+
output Boolean hasZero;
545+
algorithm
546+
hasZero := match ty
547+
case ARRAY() then List.exist(ty.dimensions, Dimension.isZero);
548+
else false;
549+
end match;
550+
end hasZeroDimension;
551+
542552
function mapDims
543553
input output Type ty;
544554
input FuncT func;

0 commit comments

Comments
 (0)