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

Commit fc29404

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix when-branch cref set check.
- Use lists instead of AVL trees for the sets, since the order of insertion changes the structure of the trees such that structural equality can't be used for the check. - Remove BaseAvlSet.isEqual, it's no longer needed and might be misleading due to only checking structural equality. Belonging to [master]: - #3009 - OpenModelica/OpenModelica-testsuite#1150
1 parent ec459e7 commit fc29404

File tree

3 files changed

+24
-51
lines changed

3 files changed

+24
-51
lines changed

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,18 @@ public
568568
end match;
569569
end isEqual;
570570

571+
function isLess
572+
input ComponentRef cref1;
573+
input ComponentRef cref2;
574+
output Boolean isLess = compare(cref1, cref2) < 0;
575+
end isLess;
576+
577+
function isGreater
578+
input ComponentRef cref1;
579+
input ComponentRef cref2;
580+
output Boolean isGreater = compare(cref1, cref2) > 0;
581+
end isGreater;
582+
571583
function isPrefix
572584
input ComponentRef cref1;
573585
input ComponentRef cref2;

Compiler/NFFrontEnd/NFVerifyModel.mo

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ encapsulated uniontype NFVerifyModel
3535
protected
3636
import List;
3737
import Error;
38-
import BaseAvlSet;
3938
import DAE;
4039
import ElementSource;
4140
import ExecStat.execStat;
@@ -55,26 +54,6 @@ public
5554
end verify;
5655

5756
protected
58-
type CrefSet = CrefSetImpl.Tree;
59-
60-
encapsulated package CrefSetImpl
61-
import BaseAvlSet;
62-
import ComponentRef = NFComponentRef;
63-
extends BaseAvlSet;
64-
65-
redeclare type Key = ComponentRef;
66-
67-
redeclare function extends keyStr
68-
algorithm
69-
outString := ComponentRef.toString(inKey);
70-
end keyStr;
71-
72-
redeclare function extends keyCompare
73-
algorithm
74-
outResult := ComponentRef.compare(inKey1, inKey2);
75-
end keyCompare;
76-
end CrefSetImpl;
77-
7857
function verifyEquation
7958
input Equation eq;
8059
algorithm
@@ -94,7 +73,7 @@ protected
9473
input list<Equation.Branch> branches;
9574
input DAE.ElementSource source;
9675
protected
97-
CrefSet crefs1, crefs2;
76+
list<ComponentRef> crefs1, crefs2;
9877
list<Equation.Branch> rest_branches;
9978
list<Equation> body;
10079
algorithm
@@ -110,7 +89,7 @@ protected
11089
Equation.Branch.BRANCH(body = body) := branch;
11190
crefs2 := whenEquationBranchCrefs(body);
11291

113-
if not CrefSet.isEqual(crefs1, crefs2) then
92+
if not List.isEqualOnTrue(crefs1, crefs2, ComponentRef.isEqual) then
11493
Error.addSourceMessage(Error.DIFFERENT_VARIABLES_SOLVED_IN_ELSEWHEN,
11594
{}, ElementSource.getInfo(source));
11695
fail();
@@ -122,24 +101,25 @@ protected
122101
"Helper function to verifyWhenEquation, returns the set of crefs that the
123102
given list of equations contains on the lhs."
124103
input list<Equation> eql;
125-
output CrefSet crefs;
104+
output list<ComponentRef> crefs = {};
126105
algorithm
127-
crefs := CrefSet.new();
128-
129106
for eq in eql loop
130107
crefs := match eq
131108
case Equation.EQUALITY() then whenEquationEqualityCrefs(eq.lhs, crefs);
132109
case Equation.IF() then whenEquationIfCrefs(eq.branches, eq.source, crefs);
133110
end match;
134111
end for;
112+
113+
crefs := List.sort(crefs, ComponentRef.isGreater);
114+
crefs := List.sortedUnique(crefs, ComponentRef.isEqual);
135115
end whenEquationBranchCrefs;
136116

137117
function whenEquationEqualityCrefs
138118
input Expression lhsExp;
139-
input output CrefSet crefs;
119+
input output list<ComponentRef> crefs;
140120
algorithm
141121
crefs := match lhsExp
142-
case Expression.CREF() then CrefSet.add(crefs, lhsExp.cref);
122+
case Expression.CREF() then lhsExp.cref :: crefs;
143123
case Expression.TUPLE()
144124
then List.fold(lhsExp.elements, whenEquationEqualityCrefs, crefs);
145125
end match;
@@ -150,9 +130,9 @@ protected
150130
of the same set of crefs, and adds that set to the given set of crefs."
151131
input list<Equation.Branch> branches;
152132
input DAE.ElementSource source;
153-
input output CrefSet crefs;
133+
input output list<ComponentRef> crefs;
154134
protected
155-
CrefSet crefs1, crefs2;
135+
list<ComponentRef> crefs1, crefs2;
156136
list<Equation.Branch> rest_branches;
157137
list<Equation> body;
158138
algorithm
@@ -164,14 +144,14 @@ protected
164144
crefs2 := whenEquationBranchCrefs(body);
165145

166146
// All the branches must have the same set of crefs on the lhs.
167-
if not CrefSet.isEqual(crefs1, crefs2) then
147+
if not List.isEqualOnTrue(crefs1, crefs2, ComponentRef.isEqual) then
168148
Error.addSourceMessage(Error.WHEN_IF_VARIABLE_MISMATCH,
169149
{}, ElementSource.getInfo(source));
170150
fail();
171151
end if;
172152
end for;
173153

174-
crefs := CrefSet.join(crefs, crefs1);
154+
crefs := listAppend(crefs1, crefs);
175155
end whenEquationIfCrefs;
176156

177157
annotation(__OpenModelica_Interface="frontend");

Compiler/Util/BaseAvlSet.mo

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,6 @@ algorithm
175175
end match;
176176
end isEmpty;
177177

178-
function isEqual
179-
input Tree tree1;
180-
input Tree tree2;
181-
output Boolean isEqual;
182-
algorithm
183-
isEqual := match (tree1, tree2)
184-
case (NODE(), NODE())
185-
then tree1.height == tree2.height and
186-
keyCompare(tree1.key, tree2.key) == 0 and
187-
isEqual(tree1.left, tree2.left) and
188-
isEqual(tree1.right, tree2.right);
189-
190-
case (LEAF(), LEAF())
191-
then keyCompare(tree1.key, tree2.key) == 0;
192-
193-
else true;
194-
end match;
195-
end isEqual;
196-
197178
function listKeys
198179
"Converts the tree to a flat list of keys (in order)."
199180
input Tree inTree;

0 commit comments

Comments
 (0)