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

Commit

Permalink
Initial enumeration support in NFInst.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 25, 2017
1 parent 49dc8a6 commit dce7606
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 98 deletions.
26 changes: 9 additions & 17 deletions Compiler/NFFrontEnd/NFBinding.mo
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,20 @@
*
*/


encapsulated package NFBinding
" file: NFMod.mo
package: NFMod
description: A type for bindings in NFInst.
"

encapsulated uniontype NFBinding
public
import DAE;
import NFExpression.Expression;
import NFInstNode.InstNode;
import SCode;
import Type = NFType;
import DAE;
import NFExpression.Expression;
import NFInstNode.InstNode;
import SCode;
import Type = NFType;

protected
import Dump;
import ExpressionDump;
import Dump;
import ExpressionDump;
import Binding = NFBinding;

public
uniontype Binding
record UNBOUND end UNBOUND;

record RAW_BINDING
Expand Down Expand Up @@ -165,7 +159,5 @@ public
end match;
end toString;

end Binding;

annotation(__OpenModelica_Interface="frontend");
end NFBinding;
20 changes: 20 additions & 0 deletions Compiler/NFFrontEnd/NFClass.mo
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ uniontype Class

record PARTIAL_BUILTIN
Type ty;
ClassTree.Tree elements;
array<InstNode> components;
Modifier modifier;
end PARTIAL_BUILTIN;

record INSTANCED_BUILTIN
Type ty;
ClassTree.Tree elements;
array<InstNode> components;
list<Modifier> attributes;
end INSTANCED_BUILTIN;

Expand Down Expand Up @@ -197,6 +201,8 @@ uniontype Class
components := match cls
case EXPANDED_CLASS() then cls.components;
case INSTANCED_CLASS() then cls.components;
case PARTIAL_BUILTIN() then cls.components;
case INSTANCED_BUILTIN() then cls.components;
end match;
end components;

Expand Down Expand Up @@ -284,6 +290,8 @@ uniontype Class
scope := match cls
case EXPANDED_CLASS() then cls.elements;
case INSTANCED_CLASS() then cls.elements;
case PARTIAL_BUILTIN() then cls.elements;
case INSTANCED_BUILTIN() then cls.elements;
end match;

element := ClassTree.get(scope, name);
Expand Down Expand Up @@ -418,6 +426,18 @@ uniontype Class
();
end match;
end updateExtends;

function getType
input Class cls;
output Type ty;
algorithm
ty := match cls
case PARTIAL_BUILTIN() then cls.ty;
case INSTANCED_BUILTIN() then cls.ty;
else Type.UNKNOWN();
end match;
end getType;

end Class;

annotation(__OpenModelica_Interface="frontend");
Expand Down
15 changes: 13 additions & 2 deletions Compiler/NFFrontEnd/NFComponent.mo
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
encapsulated package NFComponent

import DAE;
import NFBinding.Binding;
import Binding = NFBinding;
import NFClass.Class;
import Dimension = NFDimension;
import NFInstNode.InstNode;
import NFMod.Modifier;
Expand All @@ -53,6 +54,15 @@ constant Component.Attributes DEFAULT_ATTR =
DAE.NOT_INNER_OUTER(),
DAE.PUBLIC());

constant Component.Attributes CONST_ATTR =
Component.Attributes.ATTRIBUTES(
DAE.NON_CONNECTOR(),
DAE.NON_PARALLEL(),
DAE.VARIABLE(),
DAE.BIDIR(),
DAE.NOT_INNER_OUTER(),
DAE.PUBLIC());

constant Component.Attributes INPUT_ATTR =
Component.Attributes.ATTRIBUTES(
DAE.NON_CONNECTOR(),
Expand Down Expand Up @@ -164,6 +174,7 @@ uniontype Component
algorithm
ty := match component
case TYPED_COMPONENT() then component.ty;
case UNTYPED_COMPONENT() then Class.getType(InstNode.getClass(component.classInst));
else Type.UNKNOWN();
end match;
end getType;
Expand Down Expand Up @@ -213,7 +224,7 @@ uniontype Component

function getBinding
input Component component;
output NFBinding.Binding b;
output Binding b;
algorithm
b := match component
case UNTYPED_COMPONENT() then component.binding;
Expand Down
18 changes: 12 additions & 6 deletions Compiler/NFFrontEnd/NFExpression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ uniontype Expression
Boolean value;
end BOOLEAN;

record ENUM
Absyn.Path name;
record ENUM_LITERAL
Type ty;
Integer index;
end ENUM;
end ENUM_LITERAL;

record CREF
InstNode component;
Expand Down Expand Up @@ -518,7 +518,7 @@ uniontype Expression
i := match exp
case INTEGER() then exp.value;
case BOOLEAN() then if exp.value then 1 else 0;
case ENUM() then exp.index;
case ENUM_LITERAL() then exp.index;
end match;
end toInteger;

Expand All @@ -535,7 +535,9 @@ uniontype Expression
case STRING() then exp.value;
case BOOLEAN() then String(exp.value);

case ENUM() then Absyn.pathString(exp.name);
case ENUM_LITERAL(ty = t as Type.ENUMERATION())
then Absyn.pathString(t.typePath) + "." + listGet(t.literals, exp.index);

case CREF() then Prefix.toString(exp.prefix);
case ARRAY() then "{" + stringDelimitList(List.map(exp.elements, toString), ", ") + "}";

Expand Down Expand Up @@ -574,11 +576,15 @@ uniontype Expression
output DAE.Exp dexp;
algorithm
dexp := match exp
local
Absyn.Path path;

case INTEGER() then DAE.ICONST(exp.value);
case REAL() then DAE.RCONST(exp.value);
case STRING() then DAE.SCONST(exp.value);
case BOOLEAN() then DAE.BCONST(exp.value);
case ENUM() then DAE.ENUM_LITERAL(exp.name, exp.index);
case ENUM_LITERAL(ty = Type.ENUMERATION(typePath = path))
then DAE.ENUM_LITERAL(path, exp.index);

case CREF()
then DAE.CREF(Prefix.toCref(exp.prefix), DAE.T_UNKNOWN_DEFAULT);
Expand Down
52 changes: 26 additions & 26 deletions Compiler/NFFrontEnd/NFFlatten.mo
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ encapsulated package NFFlatten
"

import Inst = NFInst;
import NFBinding.Binding;
import Binding = NFBinding;
import NFClass.Class;
import NFComponent.Component;
import NFEquation.Equation;
Expand Down Expand Up @@ -192,9 +192,9 @@ algorithm
then flattenArrayBoolDim(element, rest_dims, prefix, subscripts,
scalarFunc, elements);

case Dimension.ENUM()
then flattenArrayEnumDim(element, dim.enumTypeName, dim.literals,
rest_dims, prefix, subscripts, scalarFunc, elements);
//case Dimension.ENUM()
// then flattenArrayEnumDim(element, dim.enumTypeName, dim.literals,
// rest_dims, prefix, subscripts, scalarFunc, elements);

else
algorithm
Expand Down Expand Up @@ -239,28 +239,28 @@ algorithm
elements := flattenArray(element, restDims, prefix, scalarFunc, elements, subs);
end flattenArrayBoolDim;

function flattenArrayEnumDim<ElementT>
input ElementT element;
input Absyn.Path typeName;
input list<String> literals;
input list<Dimension> restDims;
input Prefix prefix;
input list<Subscript> subscripts;
input ExpandScalarFunc scalarFunc;
input output list<DAE.Element> elements;
protected
Integer i = 1;
Expression enum_exp;
list<Subscript> subs;
algorithm
for l in literals loop
enum_exp := Expression.ENUM(Absyn.suffixPath(typeName, l), i);
i := i + 1;

subs := Subscript.INDEX(enum_exp) :: subscripts;
elements := flattenArray(element, restDims, prefix, scalarFunc, elements, subs);
end for;
end flattenArrayEnumDim;
//function flattenArrayEnumDim<ElementT>
// input ElementT element;
// input Absyn.Path typeName;
// input list<String> literals;
// input list<Dimension> restDims;
// input Prefix prefix;
// input list<Subscript> subscripts;
// input ExpandScalarFunc scalarFunc;
// input output list<DAE.Element> elements;
//protected
// Integer i = 1;
// Expression enum_exp;
// list<Subscript> subs;
//algorithm
// for l in literals loop
// enum_exp := Expression.ENUM_LITERAL(Absyn.suffixPath(typeName, l), i);
// i := i + 1;
//
// subs := Subscript.INDEX(enum_exp) :: subscripts;
// elements := flattenArray(element, restDims, prefix, scalarFunc, elements, subs);
// end for;
//end flattenArrayEnumDim;

function flattenScalar
input Component component;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFFunc.mo
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ encapsulated package NFFunc
Functions used by NFInst for handling functions.
"

import NFBinding.Binding;
import Binding = NFBinding;
import NFClass.Class;
import NFComponent.Component;
import Dimension = NFDimension;
Expand Down
71 changes: 57 additions & 14 deletions Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ import Absyn;
import SCode;

import Builtin = NFBuiltin;
import NFBinding.Binding;
import Binding = NFBinding;
import NFComponent.Component;
import Dimension = NFDimension;
import Expression = NFExpression;
import NFClass.ClassTree;
import NFClass.Class;
import NFInstNode.InstNode;
Expand Down Expand Up @@ -286,19 +287,66 @@ algorithm
cls := match definition
local
SCode.ClassDef cdef;
ClassTree.Tree classes;
ClassTree.Tree class_tree;
list<SCode.Element> elements;
Type ty;
array<InstNode> comps;

case SCode.CLASS(classDef = cdef as SCode.PARTS())
algorithm
(classes, elements) := makeScope(cdef.elementLst, scope);
(class_tree, elements) := makeScope(cdef.elementLst, scope);
then
Class.PARTIAL_CLASS(classes, elements, Modifier.NOMOD());
Class.PARTIAL_CLASS(class_tree, elements, Modifier.NOMOD());

case SCode.CLASS(classDef = cdef as SCode.ENUMERATION())
algorithm
ty := makeEnumerationType(cdef.enumLst, scope);
(class_tree, comps) := makeEnumerationScope(cdef.enumLst, ty, scope);
then
Class.PARTIAL_BUILTIN(ty, class_tree, comps, Modifier.NOMOD());

else Class.PARTIAL_CLASS(ClassTree.new(), {}, Modifier.NOMOD());
end match;
end partialInstClass2;

function makeEnumerationType
input list<SCode.Enum> literals;
input InstNode scope;
output Type ty;
protected
list<String> lits;
Absyn.Path path;
algorithm
path := InstNode.path(scope);
lits := list(e.literal for e in literals);
ty := Type.ENUMERATION(path, lits);
end makeEnumerationType;

function makeEnumerationScope
input list<SCode.Enum> literals;
input Type enumType;
input InstNode enumClass;
output ClassTree.Tree scope;
output array<InstNode> literalNodes;
protected
list<InstNode> lit_nodes = {};
SCode.Element enum_def = InstNode.definition(enumClass);
SourceInfo info = SCode.elementInfo(enum_def);
Binding binding;
Component comp;
Integer index = 1;
algorithm
for lit in literals loop
binding := Binding.TYPED_BINDING(Expression.ENUM_LITERAL(enumType, index),
enumType, DAE.C_CONST(), 0, info);
comp := Component.TYPED_COMPONENT(enumClass, enumType, binding, NFComponent.CONST_ATTR);
lit_nodes := InstNode.fromComponent(lit.literal, comp, enum_def, enumClass) :: lit_nodes;
end for;

scope := addComponentsToScope(lit_nodes, ClassTree.new());
literalNodes := listArray(lit_nodes);
end makeEnumerationScope;

function expandClass
input output InstNode node;
algorithm
Expand Down Expand Up @@ -385,14 +433,6 @@ algorithm
then
node;

// transform SCode.ENUMERATION into a normal class
case SCode.CLASS(name=name, prefixes = prefixes, classDef = SCode.ENUMERATION(enumLst), cmt = cmt, info = info)
algorithm
cls := SCodeUtil.expandEnumeration(name, enumLst, prefixes, cmt, info);
node := InstNode.setDefinition(cls, node);
then
expandClass2(node);

else
algorithm
assert(false, getInstanceName() + " got unknown class");
Expand Down Expand Up @@ -719,6 +759,7 @@ protected
Binding binding;
InstNode n, cur_scope, par;
Type ty;
ClassTree.Tree tree;
algorithm
() := match InstNode.getClass(node)
// A normal class.
Expand Down Expand Up @@ -753,7 +794,9 @@ algorithm
// Clone the node, since each component needs a unique type.
node := InstNode.clone(node);
c := InstNode.getClass(node);
Class.PARTIAL_BUILTIN(ty = ty, modifier = mod) := c;

Class.PARTIAL_BUILTIN(ty = ty, elements = tree,
components = components, modifier = mod) := c;

// Merge any outer modifiers on the class with the class' own modifier.
type_mod := Modifier.merge(modifier, mod);
Expand Down Expand Up @@ -781,7 +824,7 @@ algorithm
end for;
end if;

c := Class.INSTANCED_BUILTIN(ty, inst_type_mods);
c := Class.INSTANCED_BUILTIN(ty, tree, components, inst_type_mods);
node := InstNode.updateClass(c, node);
then
();
Expand Down

0 comments on commit dce7606

Please sign in to comment.