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

Commit 1997057

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Various fixes.
- Check that connector subscripts are parameter expressions. - Split tuple equations when the call is evaluated into a tuple expression, same as tuple assigments. Belonging to [master]: - OpenModelica/OpenModelica#126 - #3035
1 parent 8a2def5 commit 1997057

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
lines changed

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ public
159159
DAE.ElementSource source;
160160
end NORETCALL;
161161

162+
function makeEquality
163+
input Expression lhs;
164+
input Expression rhs;
165+
input Type ty;
166+
input DAE.ElementSource src;
167+
output Equation eq;
168+
algorithm
169+
eq := EQUALITY(lhs, rhs, ty, src);
170+
annotation(__OpenModelica_EarlyInline=true);
171+
end makeEquality;
172+
162173
function makeBranch
163174
input Expression condition;
164175
input list<Equation> body;

Compiler/NFFrontEnd/NFSimplifyModel.mo

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,7 @@ algorithm
143143
Expression e, lhs, rhs;
144144
Type ty;
145145

146-
case Equation.EQUALITY()
147-
algorithm
148-
ty := Type.mapDims(eq.ty, simplifyDimension);
149-
150-
if not Type.isEmptyArray(ty) then
151-
lhs := removeEmptyTupleElements(SimplifyExp.simplify(eq.lhs));
152-
rhs := removeEmptyFunctionArguments(SimplifyExp.simplify(eq.rhs));
153-
equations := Equation.EQUALITY(lhs, rhs, ty, eq.source) :: equations;
154-
end if;
155-
then
156-
equations;
146+
case Equation.EQUALITY() then simplifyEqualityEquation(eq, equations);
157147

158148
case Equation.ARRAY_EQUALITY()
159149
algorithm
@@ -211,6 +201,34 @@ algorithm
211201
end match;
212202
end simplifyEquation;
213203

204+
function simplifyEqualityEquation
205+
input Equation eq;
206+
input output list<Equation> equations;
207+
protected
208+
Expression lhs, rhs;
209+
Type ty;
210+
DAE.ElementSource src;
211+
algorithm
212+
Equation.EQUALITY(lhs = lhs, rhs = rhs, ty = ty, source = src) := eq;
213+
ty := Type.mapDims(ty, simplifyDimension);
214+
215+
if Type.isEmptyArray(ty) then
216+
return;
217+
end if;
218+
219+
lhs := SimplifyExp.simplify(lhs);
220+
lhs := removeEmptyTupleElements(lhs);
221+
rhs := SimplifyExp.simplify(rhs);
222+
rhs := removeEmptyFunctionArguments(rhs);
223+
224+
equations := match (lhs, rhs)
225+
case (Expression.TUPLE(), Expression.TUPLE())
226+
then simplifyTupleElement(lhs.elements, rhs.elements, ty, src, Equation.makeEquality, equations);
227+
228+
else Equation.EQUALITY(lhs, rhs, ty, src) :: equations;
229+
end match;
230+
end simplifyEqualityEquation;
231+
214232
function simplifyAlgorithms
215233
input list<Algorithm> algs;
216234
output list<Algorithm> outAlgs = {};
@@ -325,21 +343,30 @@ algorithm
325343

326344
statements := match (lhs, rhs)
327345
case (Expression.TUPLE(), Expression.TUPLE())
328-
then simplifyTupleAssignment(lhs.elements, rhs.elements, ty, src, statements);
346+
then simplifyTupleElement(lhs.elements, rhs.elements, ty, src, Statement.makeAssignment, statements);
329347

330348
else Statement.ASSIGNMENT(lhs, rhs, ty, src) :: statements;
331349
end match;
332350
end simplifyAssignment;
333351

334-
function simplifyTupleAssignment
335-
"Helper function to simplifyAssignment.
352+
function simplifyTupleElement<ElementT>
353+
"Helper function to simplifyEqualityEquation/simplifyAssignment.
336354
Handles Expression.TUPLE() := Expression.TUPLE() assignments by splitting
337355
them into a separate assignment statement for each pair of tuple elements."
338356
input list<Expression> lhsTuple;
339357
input list<Expression> rhsTuple;
340358
input Type ty;
341359
input DAE.ElementSource src;
342-
input output list<Statement> statements;
360+
input MakeElement makeFn;
361+
input output list<ElementT> statements;
362+
363+
partial function MakeElement
364+
input Expression lhs;
365+
input Expression rhs;
366+
input Type ty;
367+
input DAE.ElementSource src;
368+
output ElementT element;
369+
end MakeElement;
343370
protected
344371
Expression rhs;
345372
list<Expression> rest_rhs = rhsTuple;
@@ -353,10 +380,10 @@ algorithm
353380
ety :: rest_ty := rest_ty;
354381

355382
if not Expression.isWildCref(lhs) then
356-
statements := Statement.ASSIGNMENT(lhs, rhs, ety, src) :: statements;
383+
statements := makeFn(lhs, rhs, ety, src) :: statements;
357384
end if;
358385
end for;
359-
end simplifyTupleAssignment;
386+
end simplifyTupleElement;
360387

361388
function removeEmptyTupleElements
362389
"Replaces tuple elements that has one or more zero dimension with _."

Compiler/NFFrontEnd/NFStatement.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ public
111111
DAE.ElementSource source;
112112
end FAILURE;
113113

114+
function makeAssignment
115+
input Expression lhs;
116+
input Expression rhs;
117+
input Type ty;
118+
input DAE.ElementSource src;
119+
output Statement stmt;
120+
algorithm
121+
stmt := ASSIGNMENT(lhs, rhs, ty, src);
122+
annotation(__OpenModelica_EarlyInline=true);
123+
end makeAssignment;
124+
114125
function makeIf
115126
input list<tuple<Expression, list<Statement>>> branches;
116127
input DAE.ElementSource src;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,7 @@ function checkConnector
24682468
input SourceInfo info;
24692469
protected
24702470
ComponentRef cr;
2471+
list<Subscript> subs;
24712472
algorithm
24722473
() := match connExp
24732474
case Expression.CREF(cref = cr as ComponentRef.CREF(origin = Origin.CREF))
@@ -2481,6 +2482,17 @@ algorithm
24812482
Error.addSourceMessageAndFail(Error.INVALID_CONNECTOR_FORM,
24822483
{ComponentRef.toString(cr)}, info);
24832484
end if;
2485+
2486+
if ComponentRef.subscriptsVariability(cr) > Variability.PARAMETER then
2487+
subs := ComponentRef.subscriptsAllFlat(cr);
2488+
for sub in subs loop
2489+
if Subscript.variability(sub) > Variability.PARAMETER then
2490+
Error.addSourceMessage(Error.CONNECTOR_NON_PARAMETER_SUBSCRIPT,
2491+
{Expression.toString(connExp), Subscript.toString(sub)}, info);
2492+
fail();
2493+
end if;
2494+
end for;
2495+
end if;
24842496
then
24852497
();
24862498

Compiler/Util/Error.mo

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,8 @@ public constant Message NON_PARAMETER_ITERATOR_RANGE = MESSAGE(109, TRANSLATION(
348348
Util.gettext("The iteration range %s is not a constant or parameter expression."));
349349
public constant Message IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY = MESSAGE(110, TRANSLATION(), ERROR(),
350350
Util.gettext("Identifier %s of implicit for iterator must be present as array subscript in the loop body."));
351-
352-
// UNUSED ID 111
353-
351+
public constant Message CONNECTOR_NON_PARAMETER_SUBSCRIPT = MESSAGE(111, TRANSLATION(), ERROR(),
352+
Util.gettext("Connector ‘%s‘ has non-parameter subscript ‘%s‘."));
354353
public constant Message LOOKUP_CLASS_VIA_COMP_COMP = MESSAGE(112, TRANSLATION(), ERROR(),
355354
Util.gettext("Illegal access of class '%s' via a component when looking for '%s'."));
356355
public constant Message SUBSCRIPTED_FUNCTION_CALL = MESSAGE(113, TRANSLATION(), ERROR(),

0 commit comments

Comments
 (0)