Skip to content

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Move instantiation of expressions to NFInst instead of NFTyping.
- Removed UNTYPED_* equations and statements, they are needed anymore
    since we don't need to store absyn expressions now.
- Fixed many instantiation issues.
- Implemented simple constant evaluation.
- Implemented new component reference type: NFComponentRef.
- Removed NFPrefix, use NFComponentRef instead.
- Temporarily disabled handling of for loops (wasn't really working
    anyway).
- Temporarily disabled handling of functions and records.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Feb 16, 2017
1 parent fc47277 commit 3532cb4
Show file tree
Hide file tree
Showing 19 changed files with 3,257 additions and 3,027 deletions.
6 changes: 3 additions & 3 deletions Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -52,7 +52,7 @@ public
end RAW_BINDING;

record UNTYPED_BINDING
Absyn.Exp bindingExp;
Expression bindingExp;
Boolean isProcessing;
InstNode scope;
Integer propagatedDims;
Expand Down Expand Up @@ -103,7 +103,7 @@ public

function untypedExp
input Binding binding;
output Option<Absyn.Exp> exp;
output Option<Expression> exp;
algorithm
exp := match binding
case UNTYPED_BINDING() then SOME(binding.bindingExp);
Expand Down Expand Up @@ -153,7 +153,7 @@ public
string := match binding
case UNBOUND() then "";
case RAW_BINDING() then prefix + Dump.printExpStr(binding.bindingExp);
case UNTYPED_BINDING() then prefix + Dump.printExpStr(binding.bindingExp);
case UNTYPED_BINDING() then prefix + Expression.toString(binding.bindingExp);
case TYPED_BINDING() then prefix + Expression.toString(binding.bindingExp);
end match;
end toString;
Expand Down
129 changes: 129 additions & 0 deletions Compiler/NFFrontEnd/NFCeval.mo
@@ -0,0 +1,129 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

encapsulated package NFCeval

import Binding = NFBinding;
import ComponentRef = NFComponentRef;
import NFComponent.Component;
import NFExpression.Expression;
import NFInstNode.InstNode;
import Typing = NFTyping;
import Error;

uniontype EvalTarget
record DIMENSION
String name;
Integer index;
Expression exp;
SourceInfo info;
end DIMENSION;

record IGNORE_ERRORS end IGNORE_ERRORS;
end EvalTarget;

function evalExp
input output Expression exp;
input EvalTarget target;
algorithm
exp := match exp
local
InstNode c;
Binding binding;

case Expression.CREF(cref = ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE()))
algorithm
Typing.typeComponentBinding(c, InstNode.parent(c));
binding := Component.getBinding(InstNode.component(c));
then
evalBinding(binding, exp, target);

case Expression.CREF(cref = ComponentRef.CREF(node = c as InstNode.CLASS_NODE()))
then evalTypename(c, exp, target);

else exp;
end match;
end evalExp;

protected

function evalBinding
input Binding binding;
input Expression originExp "The expression the binding came from, e.g. a cref.";
input EvalTarget target;
output Expression exp;
algorithm
exp := match binding
case Binding.TYPED_BINDING() then evalExp(binding.bindingExp, target);
case Binding.UNBOUND()
algorithm
printUnboundError(target, originExp);
then
originExp;
else
algorithm
assert(false, getInstanceName() + " failed on untyped binding");
then
fail();
end match;
end evalBinding;

function printUnboundError
input EvalTarget target;
input Expression exp;
algorithm
() := match target
case EvalTarget.DIMENSION()
algorithm
Error.addSourceMessage(Error.STRUCTURAL_PARAMETER_OR_CONSTANT_WITH_NO_BINDING,
{Expression.toString(exp), target.name}, target.info);
then
fail();

else ();
end match;
end printUnboundError;

function evalTypename
input InstNode node;
input Expression originExp;
input EvalTarget target;
output Expression exp;
algorithm
exp := match target
case EvalTarget.DIMENSION() then originExp;

else originExp;
end match;
end evalTypename;

annotation(__OpenModelica_Interface="frontend");
end NFCeval;
67 changes: 1 addition & 66 deletions Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -93,10 +93,6 @@ uniontype Class
array<InstNode> extendsNodes;
array<InstNode> components;
Modifier modifier;
list<Equation> equations;
list<Equation> initialEquations;
list<list<Statement>> algorithms;
list<list<Statement>> initialAlgorithms;
end EXPANDED_CLASS;

record INSTANCED_CLASS
Expand Down Expand Up @@ -129,66 +125,9 @@ uniontype Class
input ClassTree.Tree classes;
output Class cls;
algorithm
cls := EXPANDED_CLASS(classes, listArray({}), listArray({}), Modifier.NOMOD(), {}, {}, {}, {});
cls := EXPANDED_CLASS(classes, listArray({}), listArray({}), Modifier.NOMOD());
end initExpandedClass;

function instExpandedClass
input array<InstNode> components;
input array<InstNode> extendsNodes;
input Class expandedClass;
output Class instancedClass;
protected
list<Equation> eqs;
list<Equation> ieqs;
list<list<Statement>> algs;
list<list<Statement>> ialgs;
algorithm
instancedClass := match expandedClass
case EXPANDED_CLASS()
algorithm
eqs := expandedClass.equations;
ieqs := expandedClass.initialEquations;
algs := expandedClass.algorithms;
ialgs := expandedClass.initialAlgorithms;

// ***TODO***: Sections should *not* be appended here, they need to be
// instantiated and typed in the correct scope. They should be
// collected from the extends nodes when flattening the class.
then
INSTANCED_CLASS(expandedClass.elements, extendsNodes, components, eqs, ieqs, algs, ialgs);
end match;
end instExpandedClass;

function collectInherited
input InstNode cls;
input list<Equation> i_eqs;
input list<Equation> i_ieqs;
input list<list<Statement>> i_algs;
input list<list<Statement>> i_ialgs;
output list<Equation> eqs;
output list<Equation> ieqs;
output list<list<Statement>> algs;
output list<list<Statement>> ialgs;
protected
Class inheritedClass;
algorithm
inheritedClass := InstNode.getClass(cls);
(eqs, ieqs, algs, ialgs) :=
match inheritedClass
case EXPANDED_CLASS()
algorithm
eqs := listAppend(i_eqs, inheritedClass.equations);
ieqs := listAppend(i_ieqs, inheritedClass.initialEquations);
algs := listAppend(i_algs, inheritedClass.algorithms);
ialgs := listAppend(i_ialgs, inheritedClass.initialAlgorithms);
for ext in inheritedClass.extendsNodes loop
(eqs, ieqs, algs, ialgs) := collectInherited(ext, eqs, ieqs, algs, ialgs);
end for;
then
(eqs, ieqs, algs, ialgs);
end match;
end collectInherited;

function components
input Class cls;
output array<InstNode> components;
Expand Down Expand Up @@ -267,10 +206,6 @@ uniontype Class
input output Class cls;
algorithm
cls := match cls
case EXPANDED_CLASS()
then EXPANDED_CLASS(cls.elements, cls.extendsNodes, cls.components,
cls.modifier, equations, initialEquations, algorithms, initialAlgorithms);

case INSTANCED_CLASS()
then INSTANCED_CLASS(cls.elements, cls.extendsNodes, cls.components,
equations, initialEquations, algorithms, initialAlgorithms);
Expand Down

0 comments on commit 3532cb4

Please sign in to comment.