Skip to content

Commit

Permalink
Add flag to allow reinit in algorithms (#9525)
Browse files Browse the repository at this point in the history
- Allow reinit to be used in algorithms with the
  `--allowNonStandardModelica=reinitInAlgorithms` flag.
  • Loading branch information
perost committed Oct 17, 2022
1 parent cae642f commit 5da3b1f
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 3 deletions.
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFAlgorithm.mo
Expand Up @@ -259,6 +259,7 @@ protected

case Statement.ASSERT() then ();
case Statement.TERMINATE() then ();
case Statement.REINIT() then ();
case Statement.NORETCALL() then ();
case Statement.RETURN() then ();
case Statement.BREAK() then ();
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -837,6 +837,13 @@ algorithm
case Statement.TERMINATE()
then DAE.Statement.STMT_TERMINATE(Expression.toDAE(stmt.message), stmt.source);

case Statement.REINIT()
algorithm
e1 := Expression.toDAE(stmt.cref);
e2 := Expression.toDAE(stmt.reinitExp);
then
DAE.Statement.STMT_REINIT(e1, e2, stmt.source);

case Statement.NORETCALL()
then DAE.Statement.STMT_NORETCALL(Expression.toDAE(stmt.exp), stmt.source);

Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo
Expand Up @@ -533,6 +533,12 @@ algorithm
then
stmt;

case Statement.REINIT()
algorithm
stmt.reinitExp := evaluateExp(stmt.reinitExp, info);
then
stmt;

case Statement.NORETCALL()
algorithm
stmt.exp := evaluateExp(stmt.exp, info);
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -498,6 +498,13 @@ public
then
();
case Statement.REINIT()
algorithm
collectExpFlatTypes(stmt.cref, types);
collectExpFlatTypes(stmt.reinitExp, types);
then
();
case Statement.NORETCALL()
algorithm
collectExpFlatTypes(stmt.exp, types);
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -1645,6 +1645,13 @@ algorithm
then
Statement.TERMINATE(e1, stmt.source);

case Statement.REINIT()
algorithm
e1 := flattenExp(stmt.cref, prefix);
e2 := flattenExp(stmt.reinitExp, prefix);
then
Statement.REINIT(e1, e2, stmt.source);

case Statement.NORETCALL()
algorithm
e1 := flattenExp(stmt.exp, prefix);
Expand Down Expand Up @@ -2120,6 +2127,13 @@ algorithm
then
();

case Statement.REINIT()
algorithm
funcs := collectExpFuncs(stmt.cref, funcs);
funcs := collectExpFuncs(stmt.reinitExp, funcs);
then
();

case Statement.NORETCALL()
algorithm
funcs := collectExpFuncs(stmt.exp, funcs);
Expand Down
10 changes: 8 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -3092,9 +3092,15 @@ algorithm

case SCode.Statement.ALG_REINIT(info = info)
algorithm
Error.addSourceMessage(Error.REINIT_NOT_IN_WHEN, {}, info);
if not Flags.isConfigFlagSet(Flags.ALLOW_NON_STANDARD_MODELICA, "reinitInAlgorithms") then
Error.addSourceMessage(Error.REINIT_NOT_IN_WHEN, {}, info);
fail();
end if;

exp1 := instExp(scodeStmt.cref, scope, context, info);
exp2 := instExp(scodeStmt.newValue, scope, context, info);
then
fail();
Statement.REINIT(exp1, exp2, makeSource(scodeStmt.comment, info));

case SCode.Statement.ALG_NORETCALL(info = info)
algorithm
Expand Down
57 changes: 57 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFStatement.mo
Expand Up @@ -98,6 +98,12 @@ public
DAE.ElementSource source;
end TERMINATE;

record REINIT
Expression cref;
Expression reinitExp;
DAE.ElementSource source;
end REINIT;

record NORETCALL
Expression exp;
DAE.ElementSource source;
Expand Down Expand Up @@ -154,6 +160,7 @@ public
case WHEN() then stmt.source;
case ASSERT() then stmt.source;
case TERMINATE() then stmt.source;
case REINIT() then stmt.source;
case NORETCALL() then stmt.source;
case WHILE() then stmt.source;
case RETURN() then stmt.source;
Expand Down Expand Up @@ -393,6 +400,13 @@ public
then
();

case Statement.REINIT()
algorithm
func(stmt.cref);
func(stmt.reinitExp);
then
();

case Statement.NORETCALL()
algorithm
func(stmt.exp);
Expand Down Expand Up @@ -478,6 +492,14 @@ public
then
if referenceEq(e1, stmt.message) then stmt else TERMINATE(e1, stmt.source);

case REINIT()
algorithm
e1 := func(stmt.cref);
e2 := func(stmt.reinitExp);
then
if referenceEq(e1, stmt.cref) and referenceEq(e2, stmt.reinitExp)
then stmt else REINIT(e1, e2, stmt.source);

case NORETCALL()
algorithm
e1 := func(stmt.exp);
Expand Down Expand Up @@ -546,6 +568,14 @@ public
then
if referenceEq(e1, stmt.message) then stmt else TERMINATE(e1, stmt.source);

case REINIT()
algorithm
e1 := func(stmt.cref);
e2 := func(stmt.reinitExp);
then
if referenceEq(e1, stmt.cref) and referenceEq(e2, stmt.reinitExp) then
stmt else REINIT(e1, e2, stmt.source);

case NORETCALL()
algorithm
e1 := func(stmt.exp);
Expand Down Expand Up @@ -634,6 +664,13 @@ public
then
();

case Statement.REINIT()
algorithm
arg := func(stmt.cref, arg);
arg := func(stmt.reinitExp, arg);
then
();

case Statement.NORETCALL()
algorithm
arg := func(stmt.exp, arg);
Expand Down Expand Up @@ -782,6 +819,16 @@ public
then
s;

case REINIT()
algorithm
s := IOStream.append(s, "reinit(");
s := IOStream.append(s, Expression.toString(stmt.cref));
s := IOStream.append(s, ", ");
s := IOStream.append(s, Expression.toString(stmt.reinitExp));
s := IOStream.append(s, ")");
then
s;

case NORETCALL()
then IOStream.append(s, Expression.toString(stmt.exp));

Expand Down Expand Up @@ -925,6 +972,16 @@ public
then
s;

case REINIT()
algorithm
s := IOStream.append(s, "reinit(");
s := IOStream.append(s, Expression.toFlatString(stmt.cref));
s := IOStream.append(s, ", ");
s := IOStream.append(s, Expression.toFlatString(stmt.reinitExp));
s := IOStream.append(s, ")");
then
s;

case NORETCALL()
then IOStream.append(s, Expression.toFlatString(stmt.exp));

Expand Down
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -3222,6 +3222,18 @@ algorithm
then
Statement.TERMINATE(e1, st.source);

case Statement.REINIT()
algorithm
if InstContext.inFunction(context) then
Error.addSourceMessage(Error.EXP_INVALID_IN_FUNCTION, {"reinit"},
ElementSource.getInfo(st.source));
fail();
end if;

(e1, e2) := typeReinit(st.cref, st.reinitExp, context, st.source);
then
Statement.REINIT(e1, e2, st.source);

case Statement.NORETCALL()
algorithm
e1 := typeExp(st.exp, context, ElementSource.getInfo(st.source));
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -1387,7 +1387,8 @@ constant ConfigFlag ALLOW_NON_STANDARD_MODELICA = CONFIG_FLAG(146, "allowNonStan
("nonStdEnumerationAsIntegers", Gettext.gettext("Allow enumeration as integer without casting via Integer(Enum).\nSee: https://specification.modelica.org/maint/3.5/class-predefined-types-and-declarations.html#type-conversion-of-enumeration-values-to-string-or-integer")),
("nonStdIntegersAsEnumeration", Gettext.gettext("Allow integer as enumeration without casting via Enum(Integer).\nSee: https://specification.modelica.org/maint/3.5/class-predefined-types-and-declarations.html#type-conversion-of-integer-to-enumeration-values")),
("nonStdDifferentCaseFileVsClassName", Gettext.gettext("Allow directory or file with different case in the name than the contained class name.\nSee: https://specification.modelica.org/maint/3.5/packages.html#mapping-package-class-structures-to-a-hierarchical-file-system")),
("protectedAccess", Gettext.gettext("Allow access of protected elements"))
("protectedAccess", Gettext.gettext("Allow access of protected elements")),
("reinitInAlgorithms", Gettext.gettext("Allow reinit in algorithm sections"))
})),
Gettext.gettext("Flags to allow non-standard Modelica."));

Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -991,6 +991,7 @@ ReductionInvalidTypeMin.mo \
ReductionInvalidTypeMax.mo \
Reinit1.mo \
Reinit2.mo \
Reinit3.mo \
ReinitInvalid1.mo \
ReinitInvalid2.mo \
ReinitInvalid3.mo \
Expand Down
23 changes: 23 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Reinit3.mo
@@ -0,0 +1,23 @@
// name: Reinit3
// keywords:
// status: correct
// cflags: -d=newInst, --allowNonStandardModelica=reinitInAlgorithms
//

model Reinit3
Real x;
algorithm
when time > 1 then
reinit(x, 2);
end when;
end Reinit3;

// Result:
// class Reinit3
// Real x;
// algorithm
// when time > 1.0 then
// reinit(x, 2.0);
// end when;
// end Reinit3;
// endResult

0 comments on commit 5da3b1f

Please sign in to comment.