Skip to content

Commit 648de42

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
[Cpp] Consider array slices with less subscripts than dimensions
The remaining dimensions are treated as whole dim. Belonging to [master]: - OpenModelica/OMCompiler#2768 - OpenModelica/OpenModelica-testsuite#1070
1 parent 47d8ecf commit 648de42

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

Compiler/Template/CodegenCppCommon.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ case component as CREF(componentRef=cr, ty=ty) then
368368
else if crefIsScalar(cr, context) then
369369
contextCref(cr, context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
370370
else
371-
if crefSubIsScalar(cr) then
371+
if boolAnd(intEq(listLength(crefSubs(cr)), listLength(crefDims(cr))), crefSubIsScalar(cr)) then
372372
// The array subscript results in a scalar
373373
let arrName = contextCref(crefStripLastSubs(cr), context,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
374374
let arrayType = expTypeShort(ty)

Compiler/Template/SimCodeTV.mo

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,11 @@ package ComponentReference
32673267
output list<DAE.ComponentRef> outCref;
32683268
end expandCref;
32693269

3270+
function crefHasScalarSubscripts
3271+
input DAE.ComponentRef cr;
3272+
output Boolean hasScalarSubs;
3273+
end crefHasScalarSubscripts;
3274+
32703275
function crefIsScalarWithAllConstSubs
32713276
input DAE.ComponentRef inCref;
32723277
output Boolean isScalar;
@@ -3341,11 +3346,6 @@ package Expression
33413346
output Boolean areConstant;
33423347
end subscriptConstants;
33433348

3344-
function crefHasScalarSubscripts
3345-
input DAE.ComponentRef cr;
3346-
output Boolean hasScalarSubs;
3347-
end crefHasScalarSubscripts;
3348-
33493349
function typeof
33503350
input DAE.Exp inExp;
33513351
output DAE.Type outType;

SimulationRuntime/cpp/Include/Core/Math/ArraySlice.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ class ArraySliceConst: public BaseArray<T> {
101101
ArraySliceConst(const BaseArray<T> &baseArray, const vector<Slice> &slice)
102102
: BaseArray<T>(baseArray.isStatic(), false)
103103
, _baseArray(baseArray)
104-
, _isets(slice.size())
105-
, _idxs(slice.size())
106-
, _baseIdx(slice.size())
104+
, _isets(baseArray.getNumDims())
105+
, _idxs(baseArray.getNumDims())
106+
, _baseIdx(baseArray.getNumDims())
107107
, _tmp_data(NULL) {
108108

109-
if (baseArray.getNumDims() != slice.size())
109+
if (baseArray.getNumDims() < slice.size())
110110
throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,
111-
"Wrong dimensions for ArraySlice");
111+
"Wrong slices exceeding array dimensions");
112112
// create an explicit index set per dimension,
113113
// except for all indices that are indicated with an empty index set
114114
size_t dim, size;
@@ -145,6 +145,11 @@ class ArraySliceConst: public BaseArray<T> {
145145
_dims.push_back(size);
146146
dit++;
147147
}
148+
// use all indices of remaining dims
149+
for (; dim <= baseArray.getNumDims(); dim++) {
150+
_isets[dim - 1] = NULL;
151+
_dims.push_back(_baseArray.getDim(dim));
152+
}
148153
}
149154

150155
virtual ~ArraySliceConst() {

0 commit comments

Comments
 (0)