Skip to content

Commit 12d1ede

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Collect/replace package constants in functions too. - Fix naming of functions by appending the scope path. Belonging to [master]: - OpenModelica/OMCompiler#2059 - OpenModelica/OpenModelica-testsuite#797
1 parent b9cba5b commit 12d1ede

File tree

7 files changed

+211
-16
lines changed

7 files changed

+211
-16
lines changed

Compiler/NFFrontEnd/NFComponent.mo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,31 @@ uniontype Component
346346
end match;
347347
end getBinding;
348348

349+
function setBinding
350+
input Binding binding;
351+
input output Component component;
352+
algorithm
353+
() := match component
354+
case UNTYPED_COMPONENT()
355+
algorithm
356+
component.binding := binding;
357+
then
358+
();
359+
360+
case TYPED_COMPONENT()
361+
algorithm
362+
component.binding := binding;
363+
then
364+
();
365+
366+
case ITERATOR()
367+
algorithm
368+
component.binding := binding;
369+
then
370+
();
371+
end match;
372+
end setBinding;
373+
349374
function hasBinding
350375
input Component component;
351376
output Boolean b;

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ public
108108
SourceInfo info;
109109
end NORETCALL;
110110

111+
function mapExpList
112+
input output list<Equation> eql;
113+
input MapFn func;
114+
115+
partial function MapFn
116+
input output Expression exp;
117+
end MapFn;
118+
algorithm
119+
eql := list(mapExp(eq, func) for eq in eql);
120+
end mapExpList;
121+
111122
function mapExp
112123
input output Equation eq;
113124
input MapFn func;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,49 @@ algorithm
936936
if not Function.isCollected(fn) then
937937
Function.collect(fn);
938938
funcs := FunctionTree.add(funcs, Function.name(fn), fn);
939+
funcs := collectClassFunctions(fn.node, funcs);
939940
end if;
940941
end flattenFunction;
941942

943+
function collectClassFunctions
944+
input InstNode clsNode;
945+
input output FunctionTree funcs;
946+
protected
947+
Class cls;
948+
ClassTree cls_tree;
949+
Sections sections;
950+
Component comp;
951+
Binding binding;
952+
algorithm
953+
cls := InstNode.getClass(clsNode);
954+
955+
() := match cls
956+
case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE(), sections = sections)
957+
algorithm
958+
for c in cls_tree.components loop
959+
comp := InstNode.component(c);
960+
binding := Component.getBinding(comp);
961+
962+
if Binding.isBound(binding) then
963+
funcs := collectExpFuncs(Binding.getTypedExp(binding), funcs);
964+
end if;
965+
end for;
966+
967+
() := match sections
968+
case Sections.SECTIONS()
969+
algorithm
970+
funcs := List.fold(sections.algorithms, collectAlgorithmFuncs, funcs);
971+
then
972+
();
973+
974+
else ();
975+
end match;
976+
then
977+
();
978+
979+
else ();
980+
end match;
981+
end collectClassFunctions;
982+
942983
annotation(__OpenModelica_Interface="frontend");
943984
end NFFlatten;

Compiler/NFFrontEnd/NFFunction.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ uniontype Function
196196
InstNode found_scope;
197197
LookupState state;
198198
Absyn.Path functionPath;
199+
ComponentRef prefix;
199200
algorithm
200201
try
201202
// Make sure the name is a path.
@@ -206,8 +207,8 @@ uniontype Function
206207
end try;
207208

208209
(functionRef, found_scope) := Lookup.lookupCallableName(functionName, scope, info);
209-
// (functionRef, found_scope, state) := Lookup.lookupCref(functionName, scope, info);
210-
210+
prefix := ComponentRef.fromNodeList(InstNode.scopeList(found_scope));
211+
functionRef := ComponentRef.append(functionRef, prefix);
211212
end lookupFunction;
212213

213214
function instFunc

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ algorithm
141141
// Replace or collect package constants depending on the
142142
// replacePackageConstants debug flag.
143143
if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then
144-
elems := Package.replaceConstants(elems);
144+
(elems, funcs) := Package.replaceConstants(elems, funcs);
145145
else
146-
elems := Package.collectConstants(elems);
146+
elems := Package.collectConstants(elems, funcs);
147147
end if;
148148

149149
elems := Scalarize.scalarize(elems, name);

Compiler/NFFrontEnd/NFPackage.mo

Lines changed: 103 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
encapsulated package NFPackage
3333
import NFFlatten.Elements;
34+
import NFFlatten.FunctionTree;
3435

3536
protected
3637
import ExecStat.execStat;
@@ -44,6 +45,10 @@ protected
4445
import NFInstNode.InstNode;
4546
import Typing = NFTyping;
4647
import Ceval = NFCeval;
48+
import NFFunction.Function;
49+
import NFClass.Class;
50+
import Sections = NFSections;
51+
import ClassTree = NFClassTree;
4752

4853
public
4954
type Constants = ConstantsSetImpl.Tree;
@@ -69,6 +74,7 @@ public
6974
public
7075
function collectConstants
7176
input output Elements elements;
77+
input FunctionTree functions;
7278
protected
7379
list<tuple<ComponentRef, Binding>> comps = {};
7480
Binding binding;
@@ -78,14 +84,9 @@ public
7884
constants := List.fold(elements.components, collectComponentConstants, constants);
7985
constants := Equation.foldExpList(elements.equations, collectExpConstants, constants);
8086
constants := Equation.foldExpList(elements.initialEquations, collectExpConstants, constants);
81-
82-
for alg in elements.algorithms loop
83-
constants := Statement.foldExpList(alg, collectExpConstants, constants);
84-
end for;
85-
86-
for alg in elements.initialAlgorithms loop
87-
constants := Statement.foldExpList(alg, collectExpConstants, constants);
88-
end for;
87+
constants := Statement.foldExpListList(elements.algorithms, collectExpConstants, constants);
88+
constants := Statement.foldExpListList(elements.initialAlgorithms, collectExpConstants, constants);
89+
constants := FunctionTree.fold(functions, collectFuncConstants, constants);
8990

9091
for c in Constants.listKeys(constants) loop
9192
binding := Component.getBinding(InstNode.component(ComponentRef.node(c)));
@@ -99,12 +100,14 @@ public
99100

100101
function replaceConstants
101102
input output Elements elements;
103+
input output FunctionTree functions;
102104
algorithm
103105
elements.components := list(replaceComponentConstants(c) for c in elements.components);
104-
elements.equations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.equations);
105-
elements.initialEquations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.initialEquations);
106-
elements.algorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.algorithms);
107-
elements.initialAlgorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.initialAlgorithms);
106+
elements.equations := Equation.mapExpList(elements.equations, replaceExpConstants);
107+
elements.initialEquations := Equation.mapExpList(elements.initialEquations, replaceExpConstants);
108+
elements.algorithms := Statement.mapExpListList(elements.algorithms, replaceExpConstants);
109+
elements.initialAlgorithms := Statement.mapExpListList(elements.initialAlgorithms, replaceExpConstants);
110+
functions := FunctionTree.map(functions, replaceFuncConstants);
108111
execStat(getInstanceName());
109112
end replaceConstants;
110113

@@ -165,6 +168,50 @@ public
165168
end match;
166169
end collectExpConstants_traverser;
167170

171+
function collectFuncConstants
172+
input Absyn.Path name;
173+
input Function func;
174+
input output Constants constants;
175+
protected
176+
Class cls;
177+
array<InstNode> comps;
178+
Sections sections;
179+
algorithm
180+
cls := InstNode.getClass(func.node);
181+
182+
() := match cls
183+
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps),
184+
sections = sections)
185+
algorithm
186+
for c in comps loop
187+
constants := collectBindingConstants(
188+
Component.getBinding(InstNode.component(c)), constants);
189+
end for;
190+
191+
() := match sections
192+
case Sections.SECTIONS()
193+
algorithm
194+
constants := Statement.foldExpListList(sections.algorithms, collectExpConstants, constants);
195+
then
196+
();
197+
198+
case Sections.EXTERNAL()
199+
algorithm
200+
for arg in sections.args loop
201+
constants := collectExpConstants(arg, constants);
202+
end for;
203+
then
204+
();
205+
206+
else ();
207+
end match;
208+
then
209+
();
210+
211+
else ();
212+
end match;
213+
end collectFuncConstants;
214+
168215
function replaceComponentConstants
169216
input output tuple<ComponentRef, Binding> component;
170217
protected
@@ -209,5 +256,49 @@ public
209256
end match;
210257
end replaceExpConstants_traverser;
211258

259+
function replaceFuncConstants
260+
input Absyn.Path name;
261+
input output Function func;
262+
protected
263+
Class cls;
264+
array<InstNode> comps;
265+
Sections sections;
266+
Component comp;
267+
Binding binding, eval_binding;
268+
algorithm
269+
cls := InstNode.getClass(func.node);
270+
271+
() := match cls
272+
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps),
273+
sections = sections)
274+
algorithm
275+
for c in comps loop
276+
comp := InstNode.component(c);
277+
binding := Component.getBinding(comp);
278+
eval_binding := replaceBindingConstants(binding);
279+
280+
if not referenceEq(binding, eval_binding) then
281+
comp := Component.setBinding(eval_binding, comp);
282+
InstNode.updateComponent(comp, c);
283+
end if;
284+
end for;
285+
286+
() := match sections
287+
case Sections.SECTIONS()
288+
algorithm
289+
sections.algorithms := Statement.mapExpListList(sections.algorithms, replaceExpConstants);
290+
cls.sections := sections;
291+
InstNode.updateClass(cls, func.node);
292+
then
293+
();
294+
295+
else ();
296+
end match;
297+
then
298+
();
299+
300+
end match;
301+
end replaceFuncConstants;
302+
212303
annotation(__OpenModelica_Interface="frontend");
213304
end NFPackage;

Compiler/NFFrontEnd/NFStatement.mo

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ public
125125
end match;
126126
end info;
127127

128+
function mapExpListList
129+
input output list<list<Statement>> stmtl;
130+
input MapFunc func;
131+
132+
partial function MapFunc
133+
input output Expression exp;
134+
end MapFunc;
135+
algorithm
136+
stmtl := list(mapExpList(s, func) for s in stmtl);
137+
end mapExpListList;
138+
128139
function mapExpList
129140
input output list<Statement> stmtl;
130141
input MapFunc func;
@@ -204,6 +215,21 @@ public
204215
end match;
205216
end mapExp;
206217

218+
function foldExpListList<ArgT>
219+
input list<list<Statement>> stmt;
220+
input FoldFunc func;
221+
input output ArgT arg;
222+
223+
partial function FoldFunc
224+
input Expression exp;
225+
input output ArgT arg;
226+
end FoldFunc;
227+
algorithm
228+
for s in stmt loop
229+
arg := foldExpList(s, func, arg);
230+
end for;
231+
end foldExpListList;
232+
207233
function foldExpList<ArgT>
208234
input list<Statement> stmt;
209235
input FoldFunc func;

0 commit comments

Comments
 (0)