Skip to content

Commit

Permalink
Improve handling of inherited inner/outer elements (#10675)
Browse files Browse the repository at this point in the history
- Use the instance parent as the start scope when looking for inner
  elements.
- Change InstNode.instanceParent to resolve derived nodes before looking
  up the parent, not after.
  • Loading branch information
perost committed May 10, 2023
1 parent 848f1e8 commit c9210b3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFClassTree.mo
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public
// If the instance is an empty node, use the cloned clsNode as the instance.
if InstNode.isEmpty(instance) then
instance := clsNode;
parent_scope := InstNode.parent(clsNode);
parent_scope := InstNode.instanceParent(clsNode);
else
parent_scope := instance;
inst_scope := scope;
Expand Down
6 changes: 3 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,11 @@ uniontype InstNode
output InstNode parent;
algorithm
parent := match node
case CLASS_NODE() then getDerivedNode(node.parentScope);
case CLASS_NODE() then parent(getDerivedNode(node));
case COMPONENT_NODE(nodeType = InstNodeType.REDECLARED_COMP(parent = parent))
then getDerivedNode(parent);
case COMPONENT_NODE() then getDerivedNode(node.parent);
case IMPLICIT_SCOPE() then getDerivedNode(node.parentScope);
case COMPONENT_NODE() then parent(getDerivedNode(node));
case IMPLICIT_SCOPE() then parent(getDerivedNode(node));
else EMPTY_NODE();
end match;
end instanceParent;
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFLookup.mo
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ protected
InstNode prev_scope = scope;
algorithm
while not InstNode.isEmpty(cur_scope) loop
cur_scope := InstNode.getDerivedNode(cur_scope);

try
// Check if we have an element with the same name as the outer node in this scope.
innerNode := InstNode.resolveOuter(Class.lookupElement(name, InstNode.getClass(cur_scope)));
Expand Down
51 changes: 51 additions & 0 deletions testsuite/flattening/modelica/scodeinst/InnerOuter11.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// name: InnerOuter11
// keywords:
// status: correct
// cflags: -d=newInst
//

partial function A
input Real x;
output Real y;
end A;

partial model M1
outer function f = A;
end M1;

model M2
extends M1;
Real x = f(time);
end M2;

partial model M3
M2 m2;
end M3;

partial model M4
extends M3;
end M4;

model M5
extends M4;
end M5;

model InnerOuter11
function B
input Real x;
output Real y = x;
end B;
inner function f = B;
M5 m;
end InnerOuter11;

// Result:
// function InnerOuter11.f
// input Real x;
// output Real y = x;
// end InnerOuter11.f;
//
// class InnerOuter11
// Real m.m2.x = InnerOuter11.f(time);
// end InnerOuter11;
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ InnerOuter7.mo \
InnerOuter8.mo \
InnerOuter9.mo \
InnerOuter10.mo \
InnerOuter11.mo \
InnerOuterClass1.mo \
InnerOuterDuplicate1.mo \
InnerOuterInvalidMod1.mo \
Expand Down
36 changes: 18 additions & 18 deletions testsuite/openmodelica/interactive-API/Obfuscation2.mos
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -100,7 +100,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down Expand Up @@ -472,8 +472,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -503,7 +503,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down Expand Up @@ -875,8 +875,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -906,7 +906,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down Expand Up @@ -1518,8 +1518,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -1549,7 +1549,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down Expand Up @@ -2381,8 +2381,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -2412,7 +2412,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down Expand Up @@ -3024,8 +3024,8 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// protected Real fb \"= f_nonlinear(b) - y_zero\";
// protected Real fc;
// protected Boolean found = false;
// protected Real x_min2 = x_min - x_eps;
// protected Real x_max2 = x_max + x_eps;
// protected Real x_min2 = x_min - 1e-10;
// protected Real x_max2 = x_max + 1e-10;
// protected Real a = x_min2 \"Current best minimum interval value\";
// protected Real b = x_max2 \"Current best maximum interval value\";
// algorithm
Expand Down Expand Up @@ -3055,7 +3055,7 @@ instantiateModel(Modelica.Fluid.Examples.BranchingDynamicPipes); getErrorString(
// fb := fc;
// fc := fa;
// end if;
// tol := 2.0 * eps * abs(b) + x_tol;
// tol := 2.0 * 1e-15 * abs(b) + x_tol;
// m := (c - b) / 2.0;
// if abs(m) <= tol or fb == 0.0 then
// found := true;
Expand Down

0 comments on commit c9210b3

Please sign in to comment.