Skip to content

Commit

Permalink
Fix redeclaration of dimensions.
Browse files Browse the repository at this point in the history
- Store the scope of a dimension in raw dimensions so that redeclared
  dimensions can be instantiated in the correct scope.
  • Loading branch information
perost committed Nov 13, 2020
1 parent ad57653 commit d68b30b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFDimension.mo
Expand Up @@ -49,6 +49,7 @@ public

record RAW_DIM
Absyn.Subscript dim;
InstNode scope;
end RAW_DIM;

record UNTYPED
Expand Down
13 changes: 5 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -700,7 +700,7 @@ algorithm
end if;

attrs := instDerivedAttributes(sattrs);
dims := list(Dimension.RAW_DIM(d) for d in AbsynUtil.typeSpecDimensions(ty));
dims := list(Dimension.RAW_DIM(d, InstNode.parent(node)) for d in AbsynUtil.typeSpecDimensions(ty));
mod := Class.getModifier(cls);

res := Restriction.fromSCode(SCodeUtil.getClassRestriction(element));
Expand Down Expand Up @@ -1491,7 +1491,7 @@ algorithm
mod := Modifier.addParent(node, mod);
checkOuterComponentMod(mod, component, node);

dims := list(Dimension.RAW_DIM(d) for d in component.attributes.arrayDims);
dims := list(Dimension.RAW_DIM(d, parent) for d in component.attributes.arrayDims);
binding := if useBinding then Modifier.binding(mod) else NFBinding.EMPTY_BINDING;
condition := Binding.fromAbsyn(component.condition, false, {node}, parent, info);

Expand Down Expand Up @@ -2138,7 +2138,6 @@ end checkRecursiveDefinition;

function instDimension
input output Dimension dimension;
input InstNode scope;
input InstContext.Type context;
input SourceInfo info;
algorithm
Expand All @@ -2153,7 +2152,7 @@ algorithm
case Absyn.NOSUB() then Dimension.UNKNOWN();
case Absyn.SUBSCRIPT()
algorithm
exp := instExp(dim.subscript, scope, context, info);
exp := instExp(dim.subscript, dimension.scope, context, info);
then
Dimension.UNTYPED(exp, false);
end match;
Expand All @@ -2173,7 +2172,6 @@ protected
ClassTree cls_tree;
Restriction res;
array<Dimension> dims;
InstNode dim_scope;
SourceInfo info;
Type ty;
InstContext.Type next_context;
Expand Down Expand Up @@ -2238,11 +2236,10 @@ algorithm
algorithm
sections := instExpressions(cls.baseClass, scope, sections, context);

dim_scope := InstNode.parent(node);
info := InstNode.info(node);

for i in 1:arrayLength(dims) loop
dims[i] := instDimension(dims[i], dim_scope, context, info);
dims[i] := instDimension(dims[i], context, info);
end for;

if Restriction.isRecord(cls.restriction) then
Expand Down Expand Up @@ -2393,7 +2390,7 @@ algorithm
instExpressions(c.classInst, node, context = context);

for i in 1:arrayLength(dims) loop
dims[i] := instDimension(dims[i], InstNode.parent(node), context, c.info);
dims[i] := instDimension(dims[i], context, c.info);
end for;

// This is to avoid instantiating the same component multiple times,
Expand Down
2 changes: 2 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -764,6 +764,8 @@ RecursiveInst3.mo \
RedeclareClass1.mo \
RedeclareClassComponent.mo \
RedeclareComponentClass.mo \
RedeclareDim1.mo \
RedeclareDim2.mo \
RedeclareElementClass1.mo \
RedeclareElementClass2.mo \
RedeclareElementComp1.mo \
Expand Down
23 changes: 23 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RedeclareDim1.mo
@@ -0,0 +1,23 @@
// name: RedeclareDim1
// keywords: redeclare
// status: correct
// cflags: -d=newInst
//

model A
Real x[:];
end A;

model RedeclareDim1
parameter Integer n = 3;
A a(redeclare Real x[n]);
end RedeclareDim1;

// Result:
// class RedeclareDim1
// final parameter Integer n = 3;
// Real a.x[1];
// Real a.x[2];
// Real a.x[3];
// end RedeclareDim1;
// endResult
24 changes: 24 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RedeclareDim2.mo
@@ -0,0 +1,24 @@
// name: RedeclareDim2
// keywords: redeclare
// status: correct
// cflags: -d=newInst
//

package P
type RealArray = Real[:];
end P;

model RedeclareDim2
parameter Integer n = 3;
package MP = P(redeclare type RealArray = Real[n]);
MP.RealArray x;
end RedeclareDim2;

// Result:
// class RedeclareDim2
// final parameter Integer n = 3;
// Real x[1];
// Real x[2];
// Real x[3];
// end RedeclareDim2;
// endResult

0 comments on commit d68b30b

Please sign in to comment.