@@ -41,6 +41,7 @@ protected
4141 import ComponentRef = NFComponentRef ;
4242 import Equation = NFEquation ;
4343 import Expression = NFExpression ;
44+ import ExpandExp = NFExpandExp ;
4445
4546public
4647 function verify
@@ -89,11 +90,7 @@ protected
8990 Equation . Branch . BRANCH (body = body) := branch;
9091 crefs2 := whenEquationBranchCrefs(body);
9192
92- if not List . isEqualOnTrue(crefs1, crefs2, ComponentRef . isEqual) then
93- Error . addSourceMessage(Error . DIFFERENT_VARIABLES_SOLVED_IN_ELSEWHEN ,
94- {}, ElementSource . getInfo(source));
95- fail();
96- end if ;
93+ checkCrefSetEquality(crefs1, crefs2, Error . DIFFERENT_VARIABLES_SOLVED_IN_ELSEWHEN , source);
9794 end for ;
9895 end verifyWhenEquation;
9996
@@ -105,8 +102,12 @@ protected
105102 algorithm
106103 for eq in eql loop
107104 crefs := match eq
108- case Equation . EQUALITY () then whenEquationEqualityCrefs(eq. lhs, crefs);
109- case Equation . IF () then whenEquationIfCrefs(eq. branches, eq. source, crefs);
105+ case Equation . EQUALITY () then whenEquationEqualityCrefs(eq. lhs, crefs);
106+ case Equation . CREF_EQUALITY () then eq. lhs :: crefs;
107+ case Equation . ARRAY_EQUALITY () then whenEquationEqualityCrefs(eq. lhs, crefs);
108+ case Equation . REINIT () then whenEquationEqualityCrefs(eq. cref, crefs);
109+ case Equation . IF () then whenEquationIfCrefs(eq. branches, eq. source, crefs);
110+ else crefs;
110111 end match;
111112 end for ;
112113
@@ -144,15 +145,56 @@ protected
144145 crefs2 := whenEquationBranchCrefs(body);
145146
146147 // All the branches must have the same set of crefs on the lhs.
147- if not List . isEqualOnTrue(crefs1, crefs2, ComponentRef . isEqual) then
148- Error . addSourceMessage(Error . WHEN_IF_VARIABLE_MISMATCH ,
149- {}, ElementSource . getInfo(source));
150- fail();
151- end if ;
148+ checkCrefSetEquality(crefs1, crefs2, Error . WHEN_IF_VARIABLE_MISMATCH , source);
152149 end for ;
153150
154151 crefs := listAppend(crefs1, crefs);
155152 end whenEquationIfCrefs;
156153
154+ function checkCrefSetEquality
155+ input list< ComponentRef > crefs1;
156+ input list< ComponentRef > crefs2;
157+ input Error . Message errMsg;
158+ input DAE . ElementSource source;
159+ algorithm
160+ // Assume the user isn't mixing different ways of subscripting array
161+ // varibles in the different branches, and just check the sets as is.
162+ if List . isEqualOnTrue(crefs1, crefs2, ComponentRef . isEqual) then
163+ return ;
164+ end if ;
165+
166+ // If the sets didn't match, expand arrays into scalars and try again.
167+ if List . isEqualOnTrue(expandCrefSet(crefs1), expandCrefSet(crefs2), ComponentRef . isEqual) then
168+ return ;
169+ end if ;
170+
171+ // Couldn't get the sets to match, print an error and fail.
172+ Error . addSourceMessage(errMsg, {}, ElementSource . getInfo(source));
173+ fail();
174+ end checkCrefSetEquality;
175+
176+ function expandCrefSet
177+ input list< ComponentRef > crefs;
178+ output list< ComponentRef > outCrefs = {};
179+ protected
180+ Expression exp;
181+ list< Expression > expl;
182+ algorithm
183+ for cref in crefs loop
184+ exp := Expression . fromCref(cref);
185+ exp := ExpandExp . expandCref(exp);
186+
187+ if Expression . isArray(exp) then
188+ expl := Expression . arrayElements(exp);
189+ outCrefs := listAppend(list(Expression . toCref(e) for e in expl), outCrefs);
190+ else
191+ outCrefs := cref :: outCrefs;
192+ end if ;
193+ end for ;
194+
195+ outCrefs := List . sort(outCrefs, ComponentRef . isGreater);
196+ outCrefs := List . sortedUnique(outCrefs, ComponentRef . isEqual);
197+ end expandCrefSet;
198+
157199 annotation(__OpenModelica_Interface= "frontend" );
158200end NFVerifyModel ;
0 commit comments