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

Commit

Permalink
New instantiation improvements.
Browse files Browse the repository at this point in the history
- Initial support for class modifiers.
- Fixed cloning of component types.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Nov 21, 2016
1 parent 592cec8 commit e68f0a0
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 54 deletions.
33 changes: 24 additions & 9 deletions Compiler/NFFrontEnd/NFComponentNode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ uniontype ComponentNode
input Integer nodeIndex;
input Integer componentIndex;
algorithm
component := clone(component);
component := setComponent(Component.COMPONENT_REF(nodeIndex, componentIndex), component);
component := replaceComponent(Component.COMPONENT_REF(nodeIndex, componentIndex), component);
end newReference;

function name
Expand Down Expand Up @@ -139,7 +138,7 @@ uniontype ComponentNode
end match;
end component;

function setComponent
function updateComponent
input Component component;
input output ComponentNode node;
algorithm
Expand All @@ -150,7 +149,20 @@ uniontype ComponentNode
then
node;
end match;
end setComponent;
end updateComponent;

function replaceComponent
input Component component;
input output ComponentNode node;
algorithm
_ := match node
case COMPONENT_NODE()
algorithm
node.component := arrayCreate(1, component);
then
();
end match;
end replaceComponent;

function definition
input ComponentNode node;
Expand Down Expand Up @@ -183,13 +195,16 @@ uniontype ComponentNode
end info;

function clone
input ComponentNode node;
output ComponentNode clone;
input output ComponentNode node;
algorithm
clone := match node
() := match node
case COMPONENT_NODE()
then COMPONENT_NODE(node.name, node.definition, arrayCopy(node.component), node.parent);
else node;
algorithm
node.component := arrayCopy(node.component);
then
();

else ();
end match;
end clone;

Expand Down
77 changes: 41 additions & 36 deletions Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import Typing = NFTyping;

public
function instClassInProgram
"Instantiates a class given by its fully qualified path, with the result being
a DAE."
input Absyn.Path classPath;
input SCode.Program program;
output DAE.DAElist dae;
Expand All @@ -77,19 +79,23 @@ protected
algorithm
System.startTimer();

// Create a root node from the given top-level classes.
top := makeTopNode(program);

// Look up the class to instantiate and mark it as the root class.
cls := Lookup.lookupClassName(classPath, top, Absyn.dummyInfo);
cls := InstNode.setNodeType(InstNodeType.ROOT_CLASS(), cls);

// Create an anonymous component from the class to instantiate and use it as
// the root of the instance tree.
top_comp := Component.UNTYPED_COMPONENT(cls, listArray({}),
Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
top_comp_node := ComponentNode.COMPONENT_NODE("<top>",
InstNode.definition(cls), listArray({top_comp}), ComponentNode.EMPTY_NODE());

// Instantiate, type and flatten the class.
cls := instantiate(cls, Modifier.NOMOD(), top_comp_node);

cls := Typing.typeClass(cls, top_comp_node);

dae := NFFlatten.flattenClass(cls);

System.stopTimer();
Expand All @@ -114,6 +120,7 @@ algorithm
end expand;

function makeTopNode
"Creates an instance node from the given list of top-level classes."
input list<SCode.Element> topClasses;
output InstNode topNode;
protected
Expand Down Expand Up @@ -153,19 +160,22 @@ algorithm

for e in elements loop
() := match e
// A class, create a new instance node for it and add it to the tree.
case SCode.CLASS()
algorithm
node := InstNode.new(e.name, e, parentScope);
scope := addClassToScope(e.name, ClassTree.Entry.CLASS(node), e.info, scope);
then
();

// A component, add it to the list of components and extends.
case SCode.COMPONENT()
algorithm
componentsExtends := e :: componentsExtends;
then
();

// An extends clause, add it to the list of components and extends.
case SCode.EXTENDS()
algorithm
componentsExtends := e :: componentsExtends;
Expand Down Expand Up @@ -275,9 +285,9 @@ algorithm
algorithm
(classes, elements) := makeScope(cdef.elementLst, scope);
then
Instance.PARTIAL_CLASS(classes, elements);
Instance.PARTIAL_CLASS(classes, elements, Modifier.NOMOD());

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

Expand Down Expand Up @@ -311,21 +321,23 @@ algorithm
list<list<Statement>> alg, ialg;
Option<InstNode> special_ext;
InstNode n;
Modifier mod;

case SCode.CLASS(classDef = SCode.DERIVED(typeSpec = ty, modifications = der_mod))
algorithm
ext := SCode.EXTENDS(Absyn.typeSpecPath(ty), SCode.PUBLIC(),
der_mod, NONE(), Absyn.dummyInfo);
def.classDef := SCode.PARTS({ext}, {}, {}, {}, {}, {}, {}, NONE());
i := Instance.PARTIAL_CLASS(ClassTree.new(), {ext});
i := Instance.PARTIAL_CLASS(ClassTree.new(), {ext},
Instance.getModifier(InstNode.instance(node)));
node := InstNode.setInstance(i, node);
node := InstNode.setDefinition(def, node);
then
expandClass2(node);

case SCode.CLASS(classDef = cdef as SCode.PARTS())
algorithm
Instance.PARTIAL_CLASS(classes = scope, elements = elements) :=
Instance.PARTIAL_CLASS(classes = scope, elements = elements, modifier = mod) :=
InstNode.instance(node);

// Change the instance to an expanded instance, to avoid instantiation loops.
Expand All @@ -337,8 +349,11 @@ algorithm

if isSome(special_ext) then
SOME(n) := special_ext;
node := InstNode.setInstance(InstNode.instance(n), node);
node := n;
i := InstNode.instance(n);
mod := Modifier.merge(Instance.getModifier(i), mod);
i := Instance.setModifier(mod, i);
_ := InstNode.setInstance(i, node);
node := InstNode.setInstance(i, n);
else
// Add component ID:s to the scope.
idx := 1;
Expand All @@ -357,7 +372,7 @@ algorithm
alg := instAlgorithmSections(cdef.normalAlgorithmLst, node);
ialg := instAlgorithmSections(cdef.initialAlgorithmLst, node);

i := Instance.EXPANDED_CLASS(scope, listArray(components), eq, ieq, alg, ialg);
i := Instance.EXPANDED_CLASS(scope, listArray(components), mod, eq, ieq, alg, ialg);
node := InstNode.setInstance(i, node);
end if;
then
Expand Down Expand Up @@ -446,6 +461,7 @@ algorithm
end expandExtends;

function applyModifier
"Takes a modifier and applies the submodifiers in it to an instance."
input Modifier modifier;
input output Instance instance;
input ModifierScope modifierScope;
Expand Down Expand Up @@ -479,14 +495,17 @@ algorithm
// Modifier is for a component, add it to the component in the array.
case ClassTree.Entry.COMPONENT()
algorithm
components[entry.index] :=
ComponentNode.apply(components[entry.index], Component.setModifier, m);
ComponentNode.apply(components[entry.index], Component.setModifier, m);
then
();

case ClassTree.Entry.CLASS()
algorithm
print("IMPLEMENT ME: Class modifier.\n");
// If a class is modified it's probably going to be used, so we
// might as well partially instantiate it now to simplify the
// modifier handling a bit.
entry.node := partialInstClass(entry.node);
InstNode.apply(entry.node, Instance.setModifier, m);
then
();

Expand Down Expand Up @@ -564,14 +583,16 @@ function instClass
input Modifier modifier;
input ComponentNode parent;
protected
Instance i = InstNode.instance(node), i_mod;
Instance i, i_mod;
array<ComponentNode> components;
ClassTree.Tree scope;
Modifier type_mod;
Modifier type_mod, mod;
list<Modifier> type_mods, inst_type_mods;
Binding binding;
InstNode cur_scope;
algorithm
i := InstNode.instance(node);

() := match i
// A normal class.
case Instance.EXPANDED_CLASS(elements = scope)
Expand All @@ -580,6 +601,7 @@ algorithm
node := InstNode.clone(node);

// Apply the modifier to the instance.
mod := Modifier.merge(modifier, i.modifier);
i_mod := applyModifier(modifier, i,
ModifierScope.CLASS_SCOPE(InstNode.name(node)));

Expand Down Expand Up @@ -654,14 +676,15 @@ algorithm
case Component.COMPONENT_DEF(modifier = comp_mod as Modifier.REDECLARE())
algorithm
component := Component.COMPONENT_DEF(comp_mod.element, Modifier.NOMOD());
node := ComponentNode.setComponent(component, node);
node := ComponentNode.updateComponent(component, node);
node := instComponent(node, parent, comp_mod.scope);
then
();

case Component.COMPONENT_DEF(definition = comp as SCode.COMPONENT())
algorithm
name := ComponentNode.name(node);
node := ComponentNode.clone(node);
node := ComponentNode.setParent(parent, node);

// Merge the modifier from the component.
Expand All @@ -683,15 +706,15 @@ algorithm
// Instantiate attributes and create the untyped component.
attr := instComponentAttributes(comp.attributes, comp.prefixes);
inst_comp := Component.UNTYPED_COMPONENT(cls, listArray(dims), binding, attr, comp.info);
node := ComponentNode.setComponent(inst_comp, node);
node := ComponentNode.updateComponent(inst_comp, node);
then
();

case Component.EXTENDS_NODE()
algorithm
cls := instClass(component.node, Modifier.NOMOD(), parent);
component.node := cls;
node := ComponentNode.setComponent(component, node);
node := ComponentNode.updateComponent(component, node);
then
();

Expand Down Expand Up @@ -741,12 +764,6 @@ algorithm
end match;
end instTypeSpec;

function instModifier
input output Modifier modifier;
algorithm

end instModifier;

function instDimension
input Absyn.Subscript subscript;
input InstNode scope;
Expand All @@ -764,19 +781,7 @@ algorithm
exp := instExp(exp, scope);
then
Dimension.UNTYPED_DIM(exp, false);
//case Absyn.SUBSCRIPT(subscript = exp)
// algorithm
// dim := match exp
// // Convert integer and boolean literals directly to the appropriate dimension.
// case Absyn.Exp.INTEGER() then DAE.Dimension.DIM_INTEGER(exp.value);
// else
// algorithm // Any other expression needs to be instantiated.
// (dim_exp, tree) := Inst.instExp(exp, tree);
// then
// DAE.Dimension.DIM_EXP(dim_exp);
// end match;
// then
// new(dim);

end match;
end instDimension;

Expand Down
30 changes: 28 additions & 2 deletions Compiler/NFFrontEnd/NFInstNode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,39 @@ uniontype InstNode
output InstNode clone;
algorithm
clone := match node
local
array<Instance> i;

case INST_NODE()
then INST_NODE(node.name, node.definition, arrayCopy(node.instance),
node.parentScope, node.nodeType);
algorithm
//i := arrayCreate(1, Instance.clone(node.instance[1]));
i := arrayCopy(node.instance);
then
INST_NODE(node.name, node.definition, i, node.parentScope, node.nodeType);

else node;
end match;
end clone;

function apply<ArgT>
input output InstNode node;
input FuncType func;
input ArgT arg;

partial function FuncType
input ArgT arg;
input output Instance node;
end FuncType;
algorithm
() := match node
case INST_NODE()
algorithm
node.instance[1] := func(arg, node.instance[1]);
then
();
end match;
end apply;

function scopePrefix
input InstNode node;
input output Prefix prefix = Prefix.NO_PREFIX();
Expand Down

0 comments on commit e68f0a0

Please sign in to comment.