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

Commit

Permalink
[NF] Fix scoping of redeclared components.
Browse files Browse the repository at this point in the history
- Add InstNodeType to component nodes too, and use it to keep track of
  the parent of the replaced component. This is then used by
  InstNode.scopeList, so that crefs pointing to redeclared components
  are generated correctly.

Belonging to [master]:
  - #2904
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 28, 2019
1 parent 247f185 commit be0943f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 37 deletions.
75 changes: 50 additions & 25 deletions Compiler/NFFrontEnd/NFBuiltin.mo
Expand Up @@ -160,25 +160,35 @@ constant ClassTree REAL_CLASS_TREE = ClassTree.FLAT_TREE(
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("unit", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("displayUnit", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("min", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("max", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("nominal", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("unbounded", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("stateSelect", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(STATESELECT_TYPE, Modifier.NOMOD())), InstNode.EMPTY_NODE())
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(STATESELECT_TYPE,
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());
Expand Down Expand Up @@ -208,15 +218,20 @@ constant ClassTree INTEGER_CLASS_TREE = ClassTree.FLAT_TREE(
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("min", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("max", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());
Expand All @@ -240,11 +255,14 @@ constant ClassTree BOOLEAN_CLASS_TREE = ClassTree.FLAT_TREE(
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());
Expand All @@ -271,11 +289,14 @@ constant ClassTree STRING_CLASS_TREE = ClassTree.FLAT_TREE(
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());
Expand Down Expand Up @@ -333,11 +354,14 @@ constant ClassTree CLOCK_CLASS_TREE = ClassTree.FLAT_TREE(
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.CLOCK(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.CLOCK(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.CLOCK(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.CLOCK(),
Modifier.NOMOD())), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_COMP())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());
Expand All @@ -362,7 +386,8 @@ constant InstNode TIME =
NFComponent.INPUT_ATTR,
NONE(),
Absyn.dummyInfo)),
InstNode.EMPTY_NODE());
InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

constant ComponentRef TIME_CREF = ComponentRef.CREF(TIME, {}, Type.REAL(), Origin.CREF, ComponentRef.EMPTY());

Expand Down
18 changes: 12 additions & 6 deletions Compiler/NFFrontEnd/NFBuiltinFuncs.mo
Expand Up @@ -81,39 +81,44 @@ constant Component INT_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NO

constant InstNode INT_PARAM = InstNode.COMPONENT_NODE("i",
Visibility.PUBLIC,
Pointer.createImmutable(INT_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(INT_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

// Default Real parameter.
constant Component REAL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.REAL(), NFBinding.EMPTY_BINDING, NFBinding.EMPTY_BINDING, NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode REAL_PARAM = InstNode.COMPONENT_NODE("r",
Visibility.PUBLIC,
Pointer.createImmutable(REAL_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(REAL_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

// Default Boolean parameter.
constant Component BOOL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.BOOLEAN(), NFBinding.EMPTY_BINDING, NFBinding.EMPTY_BINDING, NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode BOOL_PARAM = InstNode.COMPONENT_NODE("b",
Visibility.PUBLIC,
Pointer.createImmutable(BOOL_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(BOOL_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

// Default String parameter.
constant Component STRING_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.STRING(), NFBinding.EMPTY_BINDING, NFBinding.EMPTY_BINDING, NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode STRING_PARAM = InstNode.COMPONENT_NODE("s",
Visibility.PUBLIC,
Pointer.createImmutable(STRING_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(STRING_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

// Default enumeration(:) parameter.
constant Component ENUM_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.ENUMERATION_ANY(), NFBinding.EMPTY_BINDING, NFBinding.EMPTY_BINDING, NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode ENUM_PARAM = InstNode.COMPONENT_NODE("e",
Visibility.PUBLIC,
Pointer.createImmutable(ENUM_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(ENUM_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

// Integer(e)
constant array<NFInstNode.CachedData> EMPTY_NODE_CACHE = listArrayLiteral({
Expand Down Expand Up @@ -289,7 +294,8 @@ constant Component CLOCK_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_

constant InstNode CLOCK_PARAM = InstNode.COMPONENT_NODE("s",
Visibility.PUBLIC,
Pointer.createImmutable(CLOCK_COMPONENT), InstNode.EMPTY_NODE());
Pointer.createImmutable(CLOCK_COMPONENT), InstNode.EMPTY_NODE(),
InstNodeType.NORMAL_COMP());

constant InstNode CLOCK_DUMMY_NODE = NFInstNode.CLASS_NODE("Clock",
DUMMY_ELEMENT, Visibility.PUBLIC, Pointer.createImmutable(Class.NOT_INSTANTIATED()),
Expand Down
13 changes: 11 additions & 2 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -88,6 +88,7 @@ import SimplifyExp = NFSimplifyExp;
import Restriction = NFRestriction;
import EvalConstants = NFEvalConstants;
import SimplifyModel = NFSimplifyModel;
import InstNodeType = NFInstNode.InstNodeType;

public
type FunctionTree = FunctionTreeImpl.Tree;
Expand Down Expand Up @@ -649,7 +650,11 @@ algorithm
algorithm
iter := match ComponentRef.node(prefix)
case prefix_node as InstNode.COMPONENT_NODE()
then InstNode.COMPONENT_NODE("$i", prefix_node.visibility, Pointer.create(Component.ITERATOR(Type.INTEGER(), Variability.IMPLICITLY_DISCRETE, Component.info(Pointer.access(prefix_node.component)))), prefix_node.parent);
then InstNode.COMPONENT_NODE(
"$i", prefix_node.visibility,
Pointer.create(Component.ITERATOR(Type.INTEGER(), Variability.IMPLICITLY_DISCRETE,
Component.info(Pointer.access(prefix_node.component)))),
prefix_node.parent, InstNodeType.NORMAL_COMP());
end match;
{Dimension.INTEGER(size = stop)} := dimensions;
range := Expression.RANGE(Type.INTEGER(), Expression.INTEGER(1), NONE(), Expression.INTEGER(stop));
Expand Down Expand Up @@ -679,7 +684,11 @@ algorithm
algorithm
iter := match ComponentRef.node(prefix)
case prefix_node as InstNode.COMPONENT_NODE()
then InstNode.COMPONENT_NODE("$i", prefix_node.visibility, Pointer.create(Component.ITERATOR(Type.INTEGER(), Variability.IMPLICITLY_DISCRETE, Component.info(Pointer.access(prefix_node.component)))), prefix_node.parent);
then InstNode.COMPONENT_NODE(
"$i", prefix_node.visibility,
Pointer.create(Component.ITERATOR(Type.INTEGER(), Variability.IMPLICITLY_DISCRETE,
Component.info(Pointer.access(prefix_node.component)))),
prefix_node.parent, InstNodeType.NORMAL_COMP());
end match;
{Dimension.INTEGER(size = stop)} := dimensions;
range := Expression.RANGE(Type.INTEGER(), Expression.INTEGER(1), NONE(), Expression.INTEGER(stop));
Expand Down
8 changes: 6 additions & 2 deletions Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -1400,6 +1400,8 @@ protected
Component.Attributes attr;
array<Dimension> dims;
Option<SCode.Comment> cmt;
InstNode rdcl_node;
InstNodeType rdcl_type;
algorithm
// Check that the redeclare element actually is a component.
if not InstNode.isComponent(redeclareNode) then
Expand All @@ -1409,9 +1411,11 @@ algorithm
fail();
end if;

instComponent(redeclareNode, outerAttr, constrainingMod, true);
rdcl_type := InstNodeType.REDECLARED_COMP(InstNode.parent(originalNode));
rdcl_node := InstNode.setNodeType(rdcl_type, redeclareNode);
instComponent(rdcl_node, outerAttr, constrainingMod, true);
orig_comp := InstNode.component(originalNode);
rdcl_comp := InstNode.component(redeclareNode);
rdcl_comp := InstNode.component(rdcl_node);

new_comp := match (orig_comp, rdcl_comp)
case (Component.UNTYPED_COMPONENT(), Component.UNTYPED_COMPONENT())
Expand Down
24 changes: 22 additions & 2 deletions Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -80,6 +80,13 @@ uniontype InstNodeType
record ROOT_CLASS
"The root of the instance tree, i.e. the class that the instantiation starts from."
end ROOT_CLASS;

record NORMAL_COMP
end NORMAL_COMP;

record REDECLARED_COMP
InstNode parent "The parent of the replaced component";
end REDECLARED_COMP;
end InstNodeType;

encapsulated package NodeTree
Expand Down Expand Up @@ -226,6 +233,7 @@ uniontype InstNode
Visibility visibility;
Pointer<Component> component;
InstNode parent "The instance that this component is part of.";
InstNodeType nodeType;
end COMPONENT_NODE;

record INNER_OUTER_NODE
Expand Down Expand Up @@ -288,7 +296,7 @@ uniontype InstNode
algorithm
SCode.COMPONENT(name = name, prefixes = SCode.PREFIXES(visibility = vis)) := definition;
node := COMPONENT_NODE(name, Prefixes.visibilityFromSCode(vis),
Pointer.create(Component.new(definition)), parent);
Pointer.create(Component.new(definition)), parent, InstNodeType.NORMAL_COMP());
end newComponent;

function newExtends
Expand All @@ -313,7 +321,8 @@ uniontype InstNode
input InstNode parent;
output InstNode node;
algorithm
node := COMPONENT_NODE(name, Visibility.PUBLIC, Pointer.create(component), parent);
node := COMPONENT_NODE(name, Visibility.PUBLIC, Pointer.create(component),
parent, InstNodeType.NORMAL_COMP());
end fromComponent;

function isClass
Expand Down Expand Up @@ -760,6 +769,12 @@ uniontype InstNode
then
();

case COMPONENT_NODE()
algorithm
node.nodeType := nodeType;
then
();

else ();
end match;
end setNodeType;
Expand Down Expand Up @@ -859,8 +874,13 @@ uniontype InstNode
output list<InstNode> scopes;
algorithm
scopes := match node
local
InstNode parent;

case CLASS_NODE() then scopeListClass(node, node.nodeType, includeRoot, accumScopes);
case COMPONENT_NODE(parent = EMPTY_NODE()) then accumScopes;
case COMPONENT_NODE(nodeType = InstNodeType.REDECLARED_COMP(parent = parent))
then scopeList(parent, includeRoot, node :: accumScopes);
case COMPONENT_NODE() then scopeList(node.parent, includeRoot, node :: accumScopes);
case IMPLICIT_SCOPE() then scopeList(node.parentScope, includeRoot, accumScopes);
else accumScopes;
Expand Down

0 comments on commit be0943f

Please sign in to comment.