From 12d1ede4db658e0fa89bbab24e3edc5793922a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Wed, 29 Nov 2017 16:11:31 +0100 Subject: [PATCH] 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 --- Compiler/NFFrontEnd/NFComponent.mo | 25 +++++++ Compiler/NFFrontEnd/NFEquation.mo | 11 +++ Compiler/NFFrontEnd/NFFlatten.mo | 41 ++++++++++ Compiler/NFFrontEnd/NFFunction.mo | 5 +- Compiler/NFFrontEnd/NFInst.mo | 4 +- Compiler/NFFrontEnd/NFPackage.mo | 115 ++++++++++++++++++++++++++--- Compiler/NFFrontEnd/NFStatement.mo | 26 +++++++ 7 files changed, 211 insertions(+), 16 deletions(-) diff --git a/Compiler/NFFrontEnd/NFComponent.mo b/Compiler/NFFrontEnd/NFComponent.mo index c70ddad9d83..6fdafc56a10 100644 --- a/Compiler/NFFrontEnd/NFComponent.mo +++ b/Compiler/NFFrontEnd/NFComponent.mo @@ -346,6 +346,31 @@ uniontype Component end match; end getBinding; + function setBinding + input Binding binding; + input output Component component; + algorithm + () := match component + case UNTYPED_COMPONENT() + algorithm + component.binding := binding; + then + (); + + case TYPED_COMPONENT() + algorithm + component.binding := binding; + then + (); + + case ITERATOR() + algorithm + component.binding := binding; + then + (); + end match; + end setBinding; + function hasBinding input Component component; output Boolean b; diff --git a/Compiler/NFFrontEnd/NFEquation.mo b/Compiler/NFFrontEnd/NFEquation.mo index 7c98e339567..e438665cfd7 100644 --- a/Compiler/NFFrontEnd/NFEquation.mo +++ b/Compiler/NFFrontEnd/NFEquation.mo @@ -108,6 +108,17 @@ public SourceInfo info; end NORETCALL; + function mapExpList + input output list eql; + input MapFn func; + + partial function MapFn + input output Expression exp; + end MapFn; + algorithm + eql := list(mapExp(eq, func) for eq in eql); + end mapExpList; + function mapExp input output Equation eq; input MapFn func; diff --git a/Compiler/NFFrontEnd/NFFlatten.mo b/Compiler/NFFrontEnd/NFFlatten.mo index 74c8a7db772..087b4ff4445 100644 --- a/Compiler/NFFrontEnd/NFFlatten.mo +++ b/Compiler/NFFrontEnd/NFFlatten.mo @@ -936,8 +936,49 @@ algorithm if not Function.isCollected(fn) then Function.collect(fn); funcs := FunctionTree.add(funcs, Function.name(fn), fn); + funcs := collectClassFunctions(fn.node, funcs); end if; end flattenFunction; +function collectClassFunctions + input InstNode clsNode; + input output FunctionTree funcs; +protected + Class cls; + ClassTree cls_tree; + Sections sections; + Component comp; + Binding binding; +algorithm + cls := InstNode.getClass(clsNode); + + () := match cls + case Class.INSTANCED_CLASS(elements = cls_tree as ClassTree.FLAT_TREE(), sections = sections) + algorithm + for c in cls_tree.components loop + comp := InstNode.component(c); + binding := Component.getBinding(comp); + + if Binding.isBound(binding) then + funcs := collectExpFuncs(Binding.getTypedExp(binding), funcs); + end if; + end for; + + () := match sections + case Sections.SECTIONS() + algorithm + funcs := List.fold(sections.algorithms, collectAlgorithmFuncs, funcs); + then + (); + + else (); + end match; + then + (); + + else (); + end match; +end collectClassFunctions; + annotation(__OpenModelica_Interface="frontend"); end NFFlatten; diff --git a/Compiler/NFFrontEnd/NFFunction.mo b/Compiler/NFFrontEnd/NFFunction.mo index 91b38357c21..7beecd969a8 100644 --- a/Compiler/NFFrontEnd/NFFunction.mo +++ b/Compiler/NFFrontEnd/NFFunction.mo @@ -196,6 +196,7 @@ uniontype Function InstNode found_scope; LookupState state; Absyn.Path functionPath; + ComponentRef prefix; algorithm try // Make sure the name is a path. @@ -206,8 +207,8 @@ uniontype Function end try; (functionRef, found_scope) := Lookup.lookupCallableName(functionName, scope, info); - // (functionRef, found_scope, state) := Lookup.lookupCref(functionName, scope, info); - + prefix := ComponentRef.fromNodeList(InstNode.scopeList(found_scope)); + functionRef := ComponentRef.append(functionRef, prefix); end lookupFunction; function instFunc diff --git a/Compiler/NFFrontEnd/NFInst.mo b/Compiler/NFFrontEnd/NFInst.mo index fecac615b22..83551ac46bd 100644 --- a/Compiler/NFFrontEnd/NFInst.mo +++ b/Compiler/NFFrontEnd/NFInst.mo @@ -141,9 +141,9 @@ algorithm // Replace or collect package constants depending on the // replacePackageConstants debug flag. if Flags.isSet(Flags.REPLACE_PACKAGE_CONSTS) then - elems := Package.replaceConstants(elems); + (elems, funcs) := Package.replaceConstants(elems, funcs); else - elems := Package.collectConstants(elems); + elems := Package.collectConstants(elems, funcs); end if; elems := Scalarize.scalarize(elems, name); diff --git a/Compiler/NFFrontEnd/NFPackage.mo b/Compiler/NFFrontEnd/NFPackage.mo index 44cddd53927..c25863026a9 100644 --- a/Compiler/NFFrontEnd/NFPackage.mo +++ b/Compiler/NFFrontEnd/NFPackage.mo @@ -31,6 +31,7 @@ encapsulated package NFPackage import NFFlatten.Elements; + import NFFlatten.FunctionTree; protected import ExecStat.execStat; @@ -44,6 +45,10 @@ protected import NFInstNode.InstNode; import Typing = NFTyping; import Ceval = NFCeval; + import NFFunction.Function; + import NFClass.Class; + import Sections = NFSections; + import ClassTree = NFClassTree; public type Constants = ConstantsSetImpl.Tree; @@ -69,6 +74,7 @@ public public function collectConstants input output Elements elements; + input FunctionTree functions; protected list> comps = {}; Binding binding; @@ -78,14 +84,9 @@ public constants := List.fold(elements.components, collectComponentConstants, constants); constants := Equation.foldExpList(elements.equations, collectExpConstants, constants); constants := Equation.foldExpList(elements.initialEquations, collectExpConstants, constants); - - for alg in elements.algorithms loop - constants := Statement.foldExpList(alg, collectExpConstants, constants); - end for; - - for alg in elements.initialAlgorithms loop - constants := Statement.foldExpList(alg, collectExpConstants, constants); - end for; + constants := Statement.foldExpListList(elements.algorithms, collectExpConstants, constants); + constants := Statement.foldExpListList(elements.initialAlgorithms, collectExpConstants, constants); + constants := FunctionTree.fold(functions, collectFuncConstants, constants); for c in Constants.listKeys(constants) loop binding := Component.getBinding(InstNode.component(ComponentRef.node(c))); @@ -99,12 +100,14 @@ public function replaceConstants input output Elements elements; + input output FunctionTree functions; algorithm elements.components := list(replaceComponentConstants(c) for c in elements.components); - elements.equations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.equations); - elements.initialEquations := list(Equation.mapExp(eq, replaceExpConstants) for eq in elements.initialEquations); - elements.algorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.algorithms); - elements.initialAlgorithms := list(Statement.mapExpList(s, replaceExpConstants) for s in elements.initialAlgorithms); + elements.equations := Equation.mapExpList(elements.equations, replaceExpConstants); + elements.initialEquations := Equation.mapExpList(elements.initialEquations, replaceExpConstants); + elements.algorithms := Statement.mapExpListList(elements.algorithms, replaceExpConstants); + elements.initialAlgorithms := Statement.mapExpListList(elements.initialAlgorithms, replaceExpConstants); + functions := FunctionTree.map(functions, replaceFuncConstants); execStat(getInstanceName()); end replaceConstants; @@ -165,6 +168,50 @@ public end match; end collectExpConstants_traverser; + function collectFuncConstants + input Absyn.Path name; + input Function func; + input output Constants constants; + protected + Class cls; + array comps; + Sections sections; + algorithm + cls := InstNode.getClass(func.node); + + () := match cls + case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps), + sections = sections) + algorithm + for c in comps loop + constants := collectBindingConstants( + Component.getBinding(InstNode.component(c)), constants); + end for; + + () := match sections + case Sections.SECTIONS() + algorithm + constants := Statement.foldExpListList(sections.algorithms, collectExpConstants, constants); + then + (); + + case Sections.EXTERNAL() + algorithm + for arg in sections.args loop + constants := collectExpConstants(arg, constants); + end for; + then + (); + + else (); + end match; + then + (); + + else (); + end match; + end collectFuncConstants; + function replaceComponentConstants input output tuple component; protected @@ -209,5 +256,49 @@ public end match; end replaceExpConstants_traverser; + function replaceFuncConstants + input Absyn.Path name; + input output Function func; + protected + Class cls; + array comps; + Sections sections; + Component comp; + Binding binding, eval_binding; + algorithm + cls := InstNode.getClass(func.node); + + () := match cls + case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE(components = comps), + sections = sections) + algorithm + for c in comps loop + comp := InstNode.component(c); + binding := Component.getBinding(comp); + eval_binding := replaceBindingConstants(binding); + + if not referenceEq(binding, eval_binding) then + comp := Component.setBinding(eval_binding, comp); + InstNode.updateComponent(comp, c); + end if; + end for; + + () := match sections + case Sections.SECTIONS() + algorithm + sections.algorithms := Statement.mapExpListList(sections.algorithms, replaceExpConstants); + cls.sections := sections; + InstNode.updateClass(cls, func.node); + then + (); + + else (); + end match; + then + (); + + end match; + end replaceFuncConstants; + annotation(__OpenModelica_Interface="frontend"); end NFPackage; diff --git a/Compiler/NFFrontEnd/NFStatement.mo b/Compiler/NFFrontEnd/NFStatement.mo index 8b35f2ee2b5..67cca400622 100644 --- a/Compiler/NFFrontEnd/NFStatement.mo +++ b/Compiler/NFFrontEnd/NFStatement.mo @@ -125,6 +125,17 @@ public end match; end info; + function mapExpListList + input output list> stmtl; + input MapFunc func; + + partial function MapFunc + input output Expression exp; + end MapFunc; + algorithm + stmtl := list(mapExpList(s, func) for s in stmtl); + end mapExpListList; + function mapExpList input output list stmtl; input MapFunc func; @@ -204,6 +215,21 @@ public end match; end mapExp; + function foldExpListList + input list> stmt; + input FoldFunc func; + input output ArgT arg; + + partial function FoldFunc + input Expression exp; + input output ArgT arg; + end FoldFunc; + algorithm + for s in stmt loop + arg := foldExpList(s, func, arg); + end for; + end foldExpListList; + function foldExpList input list stmt; input FoldFunc func;