Skip to content

Commit

Permalink
Don't evaluate dims that refer to function inputs (#9023)
Browse files Browse the repository at this point in the history
Fixes #9017
  • Loading branch information
perost committed May 25, 2022
1 parent 94a591f commit d31fbb2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -180,6 +180,16 @@ public
end match;
end isIterator;

function isInput
input ComponentRef cref;
output Boolean res;
algorithm
res := match cref
case CREF() then InstNode.isInput(cref.node);
else false;
end match;
end isInput;

function node
input ComponentRef cref;
output InstNode node;
Expand Down
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -282,6 +282,16 @@ public
end match;
end isCref;

function isFunctionInputCref
input Expression exp;
output Boolean res;
algorithm
res := match exp
case CREF() then ComponentRef.isInput(ComponentRef.last(exp.cref));
else false;
end match;
end isFunctionInputCref;

function isWildCref
input Expression exp;
output Boolean wild;
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -612,7 +612,8 @@ algorithm
end if;
else
// For functions, only evaluate constant and structural parameter expressions.
if var <= Variability.STRUCTURAL_PARAMETER then
if var <= Variability.STRUCTURAL_PARAMETER and
not Expression.contains(exp, Expression.isFunctionInputCref) then
exp := Ceval.tryEvalExp(exp);
end if;
end if;
Expand Down
58 changes: 58 additions & 0 deletions testsuite/flattening/modelica/scodeinst/FunctionRecordArg5.mo
@@ -0,0 +1,58 @@
// name: FunctionRecordArg5
// keywords:
// status: correct
// cflags: -d=newInst
//

model FunctionRecordArg5
record BaseRecord
constant Integer nx = 4;
end BaseRecord;

record ExtendRecord
extends BaseRecord(nx=6);
end ExtendRecord;

function initArr
input BaseRecord r = ExtendRecord();
output Real x_1;
protected
Real x[r.nx-1];
algorithm
x := zeros(r.nx - 1);
x_1 := x[1];
end initArr;

BaseRecord r = ExtendRecord();
Real x_1;
equation
x_1 = initArr(r);
end FunctionRecordArg5;

// Result:
// function FunctionRecordArg5.BaseRecord "Automatically generated record constructor for FunctionRecordArg5.BaseRecord"
// input Integer nx = 4;
// output BaseRecord res;
// end FunctionRecordArg5.BaseRecord;
//
// function FunctionRecordArg5.ExtendRecord "Automatically generated record constructor for FunctionRecordArg5.ExtendRecord"
// input Integer nx = 6;
// output ExtendRecord res;
// end FunctionRecordArg5.ExtendRecord;
//
// function FunctionRecordArg5.initArr
// input FunctionRecordArg5.BaseRecord r = FunctionRecordArg5.ExtendRecord(6);
// output Real x_1;
// protected Real[r.nx - 1] x;
// algorithm
// x := fill(0.0, r.nx - 1);
// x_1 := x[1];
// end FunctionRecordArg5.initArr;
//
// class FunctionRecordArg5
// constant Integer r.nx = 6;
// Real x_1;
// equation
// x_1 = FunctionRecordArg5.initArr(r);
// end FunctionRecordArg5;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -652,6 +652,7 @@ FunctionRecordArg1.mo \
FunctionRecordArg2.mo \
FunctionRecordArg3.mo \
FunctionRecordArg4.mo \
FunctionRecordArg5.mo \
FunctionRecursive1.mo \
FunctionRecursive2.mo \
FunctionSections1.mo \
Expand Down

0 comments on commit d31fbb2

Please sign in to comment.