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

Commit

Permalink
NFInst improvements.
Browse files Browse the repository at this point in the history
- Ported state machine for checking lookup restrictions from old new
  inst to new inst.
- Many other lookup improvements.
- Fixed cloning of instances so that modifiers don't stick to classes.
- Other minor instantiation fixes.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Dec 4, 2016
1 parent 974e2ad commit da294f8
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 146 deletions.
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4318,6 +4318,16 @@ algorithm
end match;
end crefFirstIdent;

public function crefSecondIdent
input ComponentRef cref;
output Ident ident;
algorithm
ident := match cref
case CREF_QUAL() then crefFirstIdent(cref.componentRef);
case CREF_FULLYQUALIFIED() then crefSecondIdent(cref.componentRef);
end match;
end crefSecondIdent;

public function crefFirstCref
"Returns the first part of a cref."
input ComponentRef inCref;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFFlatten.mo
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ algorithm

else
algorithm
assert(true, "flattenComponent got unknown component");
assert(false, "flattenComponent got unknown component");
then
fail();

Expand Down
64 changes: 33 additions & 31 deletions Compiler/NFFrontEnd/NFInst.mo
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ algorithm

// Update the instantiated class inside the top component.
top_comp := Component.setClassInstance(cls, top_comp);
top_comp_node := ComponentNode.replaceComponent(top_comp, top_comp_node);
// Then update the component inside the top component node, which makes sure
// that this change is propagated to any component which uses this node as
// their parent (since the component in the node is inside an array).
top_comp_node := ComponentNode.updateComponent(top_comp, top_comp_node);

cls := Typing.typeClass(cls, top_comp_node);
dae := NFFlatten.flattenClass(cls);
Expand Down Expand Up @@ -362,9 +365,9 @@ algorithm
// Add component ID:s to the scope.
idx := 1;
for c in components loop
// // TODO: Handle components with the same name.
// // TODO: If duplicate components should be handled here, also
// // remove them from the list of components.
// TODO: Handle components with the same name.
// TODO: If duplicate components should be handled here, also
// remove them from the list of components.
scope := ClassTree.add(scope, ComponentNode.name(c),
ClassTree.Entry.COMPONENT(idx), ClassTree.addConflictReplace);

Expand All @@ -384,7 +387,7 @@ algorithm

else
algorithm
Error.addInternalError("NFInst.expandClass2 got unknown class.", Absyn.dummyInfo);
assert(false, getInstanceName() + " got unknown class");
then
fail();

Expand Down Expand Up @@ -464,7 +467,7 @@ algorithm

else
algorithm
Error.addInternalError("NFInst.expandExtends got unknown element!", Absyn.dummyInfo);
assert(false, getInstanceName() + " got unknown element");
then
fail();

Expand Down Expand Up @@ -585,6 +588,8 @@ algorithm

end match;
end for;

instance.elements := elements;
then
();

Expand Down Expand Up @@ -618,8 +623,7 @@ algorithm
// Any other component shouldn't show up here.
else
algorithm
Error.addInternalError("NFInst.addInheritedComponentRefs got unknown component.",
ComponentNode.info(cn));
assert(false, getInstanceName() + " got unknown component.");
then
fail();
end match;
Expand Down Expand Up @@ -659,32 +663,30 @@ function instClass
input Modifier modifier;
input ComponentNode parent;
protected
Instance i, i_mod;
Instance i;
array<ComponentNode> components;
ClassTree.Tree scope;
Modifier type_mod, mod;
list<Modifier> type_mods, inst_type_mods;
Binding binding;
InstNode n, cur_scope;
list<ComponentNode> ext_nodes;
Component c;
String name;
algorithm
i := InstNode.instance(node);

() := match i
() := match InstNode.instance(node)
// A normal class.
case Instance.EXPANDED_CLASS(elements = scope)
case Instance.EXPANDED_CLASS()
algorithm
// Clone the instance node, since each component needs a unique instance.
node := InstNode.clone(node);
i := InstNode.instance(node);
Instance.EXPANDED_CLASS(modifier = mod, extendsNodes = ext_nodes) := i;

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

// Instantiate all the extends nodes first.
Instance.EXPANDED_CLASS(extendsNodes = ext_nodes) := i_mod;
for ext in ext_nodes loop
Component.EXTENDS_NODE(node = n) := ComponentNode.component(ext);
// No modifier, the modifier on the extends clause has already been
Expand All @@ -694,25 +696,28 @@ algorithm

// Instantiate all local components. This will skip inherited
// components, since those have already been instantiated.
components := Array.map(Instance.components(i_mod),
components := Array.map(Instance.components(i),
function instComponent(parent = parent, scope = node));

// Update the instance node with the new instance.
i_mod := Instance.INSTANCED_CLASS(scope, components,
i.equations, i.initialEquations, i.algorithms, i.initialAlgorithms);
node := InstNode.setInstance(i_mod, node);
i := Instance.instExpandedClass(components, i);
node := InstNode.setInstance(i, node);
then
();

// A builtin type.
case Instance.PARTIAL_BUILTIN()
algorithm
// Clone the instance node, since each component needs a unique instance.
node := InstNode.clone(node);
inst_type_mods := {};
i := InstNode.instance(node);
Instance.PARTIAL_BUILTIN(name = name, modifier = mod) := i;

// Merge any outer modifiers on the class with the class' own modifier.
type_mod := Modifier.merge(modifier, i.modifier);
type_mod := Modifier.merge(modifier, mod);

// If the modifier isn't empty, instantiate it.
inst_type_mods := {};
if not Modifier.isEmpty(type_mod) then
type_mods := Modifier.toList(type_mod);
cur_scope := InstNode.parentScope(node);
Expand All @@ -734,8 +739,8 @@ algorithm
end for;
end if;

i_mod := Instance.INSTANCED_BUILTIN(i.name, inst_type_mods);
node := InstNode.setInstance(i_mod, node);
i := Instance.INSTANCED_BUILTIN(name, inst_type_mods);
node := InstNode.setInstance(i, node);
then
();

Expand Down Expand Up @@ -772,7 +777,6 @@ algorithm
case Component.COMPONENT_DEF(definition = comp as SCode.COMPONENT())
algorithm
name := ComponentNode.name(node);
node := ComponentNode.clone(node);
node := ComponentNode.setOrphanParent(parent, node);

// Merge the modifier from the component.
Expand Down Expand Up @@ -1140,8 +1144,7 @@ algorithm

else
algorithm
Error.addInternalError("NFInst.instEEquation: Unknown equation",
SCode.getEEquationInfo(scodeEq));
assert(false, getInstanceName() + " got unknown equation");
then
fail();

Expand Down Expand Up @@ -1269,8 +1272,7 @@ algorithm

else
algorithm
Error.addInternalError("NFInst.instStatement: Unknown statement",
SCode.getStatementInfo(scodeStmt));
assert(false, getInstanceName() + " got unknown statement");
then
fail();

Expand Down
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFInstNode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ uniontype InstNode

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

Expand Down
34 changes: 23 additions & 11 deletions Compiler/NFFrontEnd/NFInstance.mo
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ uniontype Instance
instance := EXPANDED_CLASS(classes, {}, listArray({}), Modifier.NOMOD(), {}, {}, {}, {});
end initExpandedClass;

function instExpandedClass
input array<ComponentNode> components;
input Instance expandedClass;
output Instance instancedClass;
algorithm
instancedClass := match expandedClass
case EXPANDED_CLASS()
then INSTANCED_CLASS(expandedClass.elements, components,
expandedClass.equations, expandedClass.initialEquations,
expandedClass.algorithms, expandedClass.initialAlgorithms);
end match;
end instExpandedClass;

function components
input Instance instance;
output array<ComponentNode> components;
Expand Down Expand Up @@ -288,8 +301,7 @@ uniontype Instance

else
algorithm
Error.addInternalError("NFInstance.setModifier got unmodifiable instance!\n",
Absyn.dummyInfo);
assert(false, getInstanceName() + " got unmodifiable instance");
then
fail();

Expand All @@ -315,23 +327,23 @@ uniontype Instance
local
ClassTree.Tree tree;

case PARTIAL_CLASS()
algorithm
instance.classes := ClassTree.map(instance.classes, cloneEntry);
then
();
//case PARTIAL_CLASS()
// algorithm
// instance.classes := ClassTree.map(instance.classes, cloneEntry);
// then
// ();

case EXPANDED_CLASS()
algorithm
instance.elements := ClassTree.map(instance.elements, cloneEntry);
Array.map(instance.components, ComponentNode.clone);
//instance.elements := ClassTree.map(instance.elements, cloneEntry);
instance.components := Array.map(instance.components, ComponentNode.clone);
then
();

case INSTANCED_CLASS()
algorithm
instance.elements := ClassTree.map(instance.elements, cloneEntry);
Array.map(instance.components, ComponentNode.clone);
//instance.elements := ClassTree.map(instance.elements, cloneEntry);
instance.components := Array.map(instance.components, ComponentNode.clone);
then
();

Expand Down

0 comments on commit da294f8

Please sign in to comment.