Skip to content

Commit

Permalink
Redeclare improvements (#8249)
Browse files Browse the repository at this point in the history
- Improve the handling of constraining class modifiers for both
  components and classes.

Fixes #8092
  • Loading branch information
perost committed Nov 30, 2021
1 parent 1140171 commit 14546fe
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
63 changes: 45 additions & 18 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -822,7 +822,7 @@ algorithm
// Redeclare classes with redeclare modifiers. Redeclared components could
// also be handled here, but since each component is only instantiated once
// it's more efficient to apply the redeclare when instantiating them instead.
redeclareClasses(cls_tree);
redeclareClasses(cls_tree, node);

// Instantiate the extends nodes.
ClassTree.mapExtends(cls_tree,
Expand Down Expand Up @@ -1197,10 +1197,11 @@ end applyModifier;

function redeclareClasses
input output ClassTree tree;
input InstNode parent;
protected
InstNode cls_node, redecl_node;
Class cls;
Modifier mod;
Modifier mod, cc_mod;
algorithm
() := match tree
case ClassTree.INSTANTIATED_TREE()
Expand All @@ -1211,8 +1212,9 @@ algorithm
mod := Class.getModifier(cls);

if Modifier.isRedeclare(mod) then
Modifier.REDECLARE(element = redecl_node, outerMod = mod) := mod;
cls_node := redeclareClass(redecl_node, cls_node, mod);
Modifier.REDECLARE(element = redecl_node, outerMod = mod, constrainingMod = cc_mod) := mod;
cc_mod := getConstrainingMod(InstNode.definition(cls_node), parent, cc_mod);
cls_node := redeclareClass(redecl_node, cls_node, mod, cc_mod);
Mutable.update(cls_ptr, cls_node);
end if;
end for;
Expand Down Expand Up @@ -1260,7 +1262,7 @@ protected
algorithm
rdcl_node := Mutable.access(redeclareCls);
repl_node := Mutable.access(replaceableCls);
rdcl_node := redeclareClass(rdcl_node, repl_node, Modifier.NOMOD());
rdcl_node := redeclareClass(rdcl_node, repl_node, Modifier.NOMOD(), Modifier.NOMOD());
outCls := Mutable.create(rdcl_node);
end redeclareClassElement;

Expand All @@ -1284,12 +1286,14 @@ function redeclareClass
input InstNode redeclareNode;
input InstNode originalNode;
input Modifier outerMod;
input Modifier constrainingMod;
output InstNode redeclaredNode;
protected
InstNode orig_node;
Class orig_cls, rdcl_cls, new_cls;
Class.Prefixes prefs;
InstNodeType node_ty;
Modifier mod;
algorithm
// Check that the redeclare element is actually a class.
if not InstNode.isClass(redeclareNode) then
Expand All @@ -1304,6 +1308,10 @@ algorithm
partialInstClass(redeclareNode);
rdcl_cls := InstNode.getClass(redeclareNode);

mod := Class.getModifier(rdcl_cls);
mod := Modifier.merge(mod, constrainingMod);
mod := Modifier.merge(outerMod, mod);

prefs := mergeRedeclaredClassPrefixes(Class.getPrefixes(orig_cls),
Class.getPrefixes(rdcl_cls), redeclareNode);

Expand Down Expand Up @@ -1332,7 +1340,7 @@ algorithm
node_ty := InstNodeType.BASE_CLASS(InstNode.parent(orig_node), InstNode.definition(orig_node));
orig_node := InstNode.setNodeType(node_ty, orig_node);
rdcl_cls.elements := ClassTree.setClassExtends(orig_node, rdcl_cls.elements);
rdcl_cls.modifier := Modifier.merge(outerMod, rdcl_cls.modifier);
rdcl_cls.modifier := mod;
rdcl_cls.prefixes := prefs;
then
rdcl_cls;
Expand All @@ -1346,12 +1354,12 @@ algorithm
else
new_cls := match (orig_cls, rdcl_cls)
case (Class.PARTIAL_BUILTIN(), _)
then redeclareEnum(rdcl_cls, orig_cls, prefs, outerMod, redeclareNode, originalNode);
then redeclareEnum(rdcl_cls, orig_cls, prefs, mod, redeclareNode, originalNode);

case (_, Class.PARTIAL_CLASS())
algorithm
rdcl_cls.prefixes := prefs;
rdcl_cls.modifier := Modifier.merge(outerMod, rdcl_cls.modifier);
rdcl_cls.modifier := mod;
then
rdcl_cls;

Expand Down Expand Up @@ -1447,13 +1455,7 @@ algorithm
instComponentDef(def, Modifier.NOMOD(), inner_mod, NFComponent.DEFAULT_ATTR,
useBinding, comp_node, parent, instLevel, originalAttr, next_context);

cc_smod := SCodeUtil.getConstrainingMod(def);
if not SCodeUtil.isEmptyMod(cc_smod) then
name := InstNode.name(node);
cc_def_mod := Modifier.create(cc_smod, name, ModifierScope.COMPONENT(name), parent);
cc_mod := Modifier.merge(cc_mod, cc_def_mod);
end if;

cc_mod := getConstrainingMod(def, parent, cc_mod);
cc_mod := Modifier.merge(cc_mod, innerMod);

outer_mod := Modifier.merge(InstNode.getModifier(rdcl_node), outer_mod);
Expand Down Expand Up @@ -1556,10 +1558,14 @@ function instElementModifier
protected
Modifier cc_mod;
algorithm
cc_mod := instConstrainingMod(element, parent);
mod := Modifier.fromElement(element, parent);
mod := propagateRedeclaredMod(mod, component);
mod := Modifier.merge(mod, cc_mod);

if InstNode.isRedeclared(component) then
mod := propagateRedeclaredMod(mod, component);
else
cc_mod := instConstrainingMod(element, parent);
mod := Modifier.merge(mod, cc_mod);
end if;
end instElementModifier;

function instConstrainingMod
Expand All @@ -1583,6 +1589,27 @@ algorithm
end match;
end instConstrainingMod;

function getConstrainingMod
input SCode.Element element;
input InstNode parent;
input Modifier outerMod;
output Modifier ccMod;
protected
String name;
SCode.Mod cc_smod;
ModifierScope mod_scope;
algorithm
cc_smod := SCodeUtil.getConstrainingMod(element);

if not SCodeUtil.isEmptyMod(cc_smod) then
name := SCodeUtil.elementName(element);
ccMod := Modifier.create(cc_smod, name, ModifierScope.fromElement(element), parent);
ccMod := Modifier.merge(outerMod, ccMod);
else
ccMod := outerMod;
end if;
end getConstrainingMod;

function propagateRedeclaredMod
input Modifier mod;
input InstNode component;
Expand Down
11 changes: 11 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -1383,6 +1383,17 @@ uniontype InstNode
end match;
end isRedeclare;

function isRedeclared
input InstNode node;
output Boolean redeclared;
algorithm
redeclared := match nodeType(node)
case InstNodeType.REDECLARED_COMP() then true;
case InstNodeType.REDECLARED_CLASS() then true;
else false;
end match;
end isRedeclared;

function isProtectedBaseClass
input InstNode node;
output Boolean isProtected;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo
Expand Up @@ -657,7 +657,7 @@ public
case WHOLE() then DAE.WHOLEDIM();
else
algorithm
Error.assertion(false, getInstanceName() + " failed on unknown subscript", sourceInfo());
Error.assertion(false, getInstanceName() + " failed on unknown subscript " + toString(subscript), sourceInfo());
then
fail();
end match;
Expand Down
19 changes: 19 additions & 0 deletions testsuite/flattening/modelica/scodeinst/ConstrainingClass3.mo
@@ -0,0 +1,19 @@
// name: ConstrainingClass3
// keywords:
// status: correct
// cflags: -d=newInst
//

model A
replaceable Real x constrainedby Real(start = 1.0);
end A;

model ConstrainingClass3
A a(redeclare replaceable Real x(min = 1.0) constrainedby Real(start = 2.0));
end ConstrainingClass3;

// Result:
// class ConstrainingClass3
// Real a.x(min = 1.0, start = 2.0);
// end ConstrainingClass3;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -297,6 +297,7 @@ const7.mo \
const8.mo \
ConstrainingClass1.mo \
ConstrainingClass2.mo \
ConstrainingClass3.mo \
ConstrainingClassFunc1.mo \
DimCyclic1.mo \
DimCyclic2.mo \
Expand Down

0 comments on commit 14546fe

Please sign in to comment.