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

Commit 6f42885

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improvements to handling of 'when'.
- Replace NFInst.EquationScope with ExpOrigin. - Extend the structural parameter marking phase to also mark variables assigned in 'when' as implicitly discrete. - Add more error checking for how 'when' is used. Belonging to [master]: - #2480 - OpenModelica/OpenModelica-testsuite#965
1 parent 9b09552 commit 6f42885

File tree

8 files changed

+682
-92
lines changed

8 files changed

+682
-92
lines changed

Compiler/NFFrontEnd/NFAlgorithm.mo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public
4343
ElementSource source;
4444
end ALGORITHM;
4545

46+
partial function ApplyFn
47+
input Statement alg;
48+
end ApplyFn;
49+
50+
function applyList
51+
input list<Algorithm> algs;
52+
input ApplyFn func;
53+
algorithm
54+
for alg in algs loop
55+
for s in alg.statements loop
56+
Statement.apply(s, func);
57+
end for;
58+
end for;
59+
end applyList;
60+
61+
function apply
62+
input Algorithm alg;
63+
input ApplyFn func;
64+
algorithm
65+
for s in alg.statements loop
66+
Statement.apply(s, func);
67+
end for;
68+
end apply;
69+
4670
function mapExp
4771
input output Algorithm alg;
4872
input MapFunc func;

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,93 @@ public
145145
output SourceInfo info = ElementSource.getInfo(source(eq));
146146
end info;
147147

148+
partial function ApplyFn
149+
input Equation eq;
150+
end ApplyFn;
151+
152+
function applyList
153+
input list<Equation> eql;
154+
input ApplyFn func;
155+
algorithm
156+
for eq in eql loop
157+
apply(eq, func);
158+
end for;
159+
end applyList;
160+
161+
function apply
162+
input Equation eq;
163+
input ApplyFn func;
164+
algorithm
165+
() := match eq
166+
case FOR()
167+
algorithm
168+
for e in eq.body loop
169+
apply(e, func);
170+
end for;
171+
then
172+
();
173+
174+
case IF()
175+
algorithm
176+
for b in eq.branches loop
177+
for e in Util.tuple22(b) loop
178+
apply(e, func);
179+
end for;
180+
end for;
181+
then
182+
();
183+
184+
case WHEN()
185+
algorithm
186+
for b in eq.branches loop
187+
for e in Util.tuple22(b) loop
188+
apply(e, func);
189+
end for;
190+
end for;
191+
then
192+
();
193+
194+
else ();
195+
end match;
196+
197+
func(eq);
198+
end apply;
199+
200+
partial function MapFn
201+
input output Equation eq;
202+
end MapFn;
203+
204+
function map
205+
input output Equation eq;
206+
input MapFn func;
207+
algorithm
208+
() := match eq
209+
case FOR()
210+
algorithm
211+
eq.body := list(map(e, func) for e in eq.body);
212+
then
213+
();
214+
215+
case IF()
216+
algorithm
217+
eq.branches := list((Util.tuple21(b), list(map(e, func) for e in Util.tuple22(b)))
218+
for b in eq.branches);
219+
then
220+
();
221+
222+
case WHEN()
223+
algorithm
224+
eq.branches := list((Util.tuple21(b), list(map(e, func) for e in Util.tuple22(b)))
225+
for b in eq.branches);
226+
then
227+
();
228+
229+
else ();
230+
end match;
231+
232+
eq := func(eq);
233+
end map;
234+
148235
partial function MapExpFn
149236
input output Expression MapExpFn;
150237
end MapExpFn;

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,227 @@ public
20012001
end match;
20022002
end foldSubscript;
20032003

2004+
function applyList
2005+
input list<Expression> expl;
2006+
input ApplyFunc func;
2007+
2008+
partial function ApplyFunc
2009+
input Expression exp;
2010+
end ApplyFunc;
2011+
algorithm
2012+
for e in expl loop
2013+
apply(e, func);
2014+
end for;
2015+
end applyList;
2016+
2017+
function apply
2018+
input Expression exp;
2019+
input ApplyFunc func;
2020+
2021+
partial function ApplyFunc
2022+
input Expression exp;
2023+
end ApplyFunc;
2024+
algorithm
2025+
() := match exp
2026+
local
2027+
Expression e;
2028+
2029+
case CREF() algorithm applyCref(exp.cref, func); then ();
2030+
case ARRAY() algorithm applyList(exp.elements, func); then ();
2031+
2032+
case MATRIX()
2033+
algorithm
2034+
for row in exp.elements loop
2035+
applyList(row, func);
2036+
end for;
2037+
then
2038+
();
2039+
2040+
case RANGE(step = SOME(e))
2041+
algorithm
2042+
apply(exp.start, func);
2043+
apply(e, func);
2044+
apply(exp.stop, func);
2045+
then
2046+
();
2047+
2048+
case RANGE()
2049+
algorithm
2050+
apply(exp.start, func);
2051+
apply(exp.stop, func);
2052+
then
2053+
();
2054+
2055+
case TUPLE() algorithm applyList(exp.elements, func); then ();
2056+
case RECORD() algorithm applyList(exp.elements, func); then ();
2057+
case CALL() algorithm applyCall(exp.call, func); then ();
2058+
2059+
case SIZE(dimIndex = SOME(e))
2060+
algorithm
2061+
apply(exp.exp, func);
2062+
apply(e, func);
2063+
then
2064+
();
2065+
2066+
case SIZE() algorithm apply(exp.exp, func); then ();
2067+
2068+
case BINARY()
2069+
algorithm
2070+
apply(exp.exp1, func);
2071+
apply(exp.exp2, func);
2072+
then
2073+
();
2074+
2075+
case UNARY() algorithm apply(exp.exp, func); then ();
2076+
2077+
case LBINARY()
2078+
algorithm
2079+
apply(exp.exp1, func);
2080+
apply(exp.exp2, func);
2081+
then
2082+
();
2083+
2084+
case LUNARY() algorithm apply(exp.exp, func); then ();
2085+
2086+
case RELATION()
2087+
algorithm
2088+
apply(exp.exp1, func);
2089+
apply(exp.exp2, func);
2090+
then
2091+
();
2092+
2093+
case IF()
2094+
algorithm
2095+
apply(exp.condition, func);
2096+
apply(exp.trueBranch, func);
2097+
apply(exp.falseBranch, func);
2098+
then
2099+
();
2100+
2101+
case CAST() algorithm apply(exp.exp, func); then ();
2102+
case UNBOX() algorithm apply(exp.exp, func); then ();
2103+
2104+
case SUBSCRIPTED_EXP()
2105+
algorithm
2106+
apply(exp.exp, func);
2107+
applyList(exp.subscripts, func);
2108+
then
2109+
();
2110+
2111+
case TUPLE_ELEMENT() algorithm apply(exp.tupleExp, func); then ();
2112+
case BOX() algorithm apply(exp.exp, func); then ();
2113+
case MUTABLE() algorithm apply(Mutable.access(exp.exp), func); then ();
2114+
else ();
2115+
end match;
2116+
2117+
func(exp);
2118+
end apply;
2119+
2120+
function applyCall
2121+
input Call call;
2122+
input ApplyFunc func;
2123+
2124+
partial function ApplyFunc
2125+
input Expression exp;
2126+
end ApplyFunc;
2127+
algorithm
2128+
() := match call
2129+
local
2130+
Expression e;
2131+
2132+
case Call.UNTYPED_CALL()
2133+
algorithm
2134+
applyList(call.arguments, func);
2135+
2136+
for arg in call.named_args loop
2137+
(_, e) := arg;
2138+
apply(e, func);
2139+
end for;
2140+
then
2141+
();
2142+
2143+
case Call.ARG_TYPED_CALL()
2144+
algorithm
2145+
for arg in call.arguments loop
2146+
(e, _, _) := arg;
2147+
apply(e, func);
2148+
end for;
2149+
2150+
for arg in call.named_args loop
2151+
(_, e, _, _) := arg;
2152+
apply(e, func);
2153+
end for;
2154+
then
2155+
();
2156+
2157+
case Call.TYPED_CALL()
2158+
algorithm
2159+
applyList(call.arguments, func);
2160+
then
2161+
();
2162+
2163+
case Call.UNTYPED_MAP_CALL()
2164+
algorithm
2165+
apply(call.exp, func);
2166+
2167+
for i in call.iters loop
2168+
apply(Util.tuple22(i), func);
2169+
end for;
2170+
then
2171+
();
2172+
2173+
case Call.TYPED_MAP_CALL()
2174+
algorithm
2175+
apply(call.exp, func);
2176+
2177+
for i in call.iters loop
2178+
apply(Util.tuple22(i), func);
2179+
end for;
2180+
then
2181+
();
2182+
2183+
end match;
2184+
end applyCall;
2185+
2186+
function applyCref
2187+
input ComponentRef cref;
2188+
input ApplyFunc func;
2189+
2190+
partial function ApplyFunc
2191+
input Expression exp;
2192+
end ApplyFunc;
2193+
algorithm
2194+
() := match cref
2195+
case ComponentRef.CREF()
2196+
algorithm
2197+
for s in cref.subscripts loop
2198+
applyCrefSubscript(s, func);
2199+
end for;
2200+
2201+
applyCref(cref.restCref, func);
2202+
then
2203+
();
2204+
2205+
else ();
2206+
end match;
2207+
end applyCref;
2208+
2209+
function applyCrefSubscript
2210+
input Subscript subscript;
2211+
input ApplyFunc func;
2212+
2213+
partial function ApplyFunc
2214+
input Expression exp;
2215+
end ApplyFunc;
2216+
algorithm
2217+
() := match subscript
2218+
case Subscript.UNTYPED() algorithm apply(subscript.exp, func); then ();
2219+
case Subscript.INDEX() algorithm apply(subscript.index, func); then ();
2220+
case Subscript.SLICE() algorithm apply(subscript.slice, func); then ();
2221+
case Subscript.WHOLE() then ();
2222+
end match;
2223+
end applyCrefSubscript;
2224+
20042225
function mapFold<ArgT>
20052226
input Expression exp;
20062227
input MapFunc func;

0 commit comments

Comments
 (0)