Skip to content

Commit

Permalink
Use enclosing scope for extends in replaceable classes (#10431)
Browse files Browse the repository at this point in the history
- Dump the enclosing scope path instead of the instance tree path for
  extended names in replaceable classes in getModelInstance.
  • Loading branch information
perost committed Mar 22, 2023
1 parent 992700b commit 00c7cfd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
13 changes: 11 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -648,7 +648,10 @@ uniontype InstNode
the enclosing class. In the case of a component it is the enclosing class of
the component's type."
input InstNode node;
input Boolean ignoreRedeclare = false;
output InstNode scope;
protected
InstNode orig_node;
algorithm
scope := match node
case CLASS_NODE(nodeType = InstNodeType.DERIVED_CLASS())
Expand All @@ -665,6 +668,10 @@ uniontype InstNode
else
parentScope(scope);

case CLASS_NODE(nodeType = InstNodeType.REDECLARED_CLASS(originalNode = SOME(orig_node)))
guard ignoreRedeclare
then parentScope(orig_node);

case CLASS_NODE() then node.parentScope;
case COMPONENT_NODE() then parentScope(Component.classInstance(Pointer.access(node.component)));
case IMPLICIT_SCOPE() then node.parentScope;
Expand All @@ -674,22 +681,24 @@ uniontype InstNode
function enclosingScopePath
"Returns the enclosing scopes of a node as a path."
input InstNode node;
input Boolean ignoreRedeclare = false;
output Absyn.Path path;
algorithm
path := AbsynUtil.stringListPath(
list(InstNode.name(n) for n in enclosingScopeList(node)));
list(InstNode.name(n) for n in enclosingScopeList(node, ignoreRedeclare)));
end enclosingScopePath;

function enclosingScopeList
"Returns the enclosing scopes of a node as a list of nodes."
input InstNode node;
input Boolean ignoreRedeclare = false;
output list<InstNode> res = {};
protected
InstNode scope = node;
algorithm
while not isTopScope(scope) loop
res := scope :: res;
scope := classScope(parentScope(scope));
scope := classScope(parentScope(scope, ignoreRedeclare));
end while;
end enclosingScopeList;

Expand Down
7 changes: 6 additions & 1 deletion OMCompiler/Compiler/Script/NFApi.mo
Expand Up @@ -1173,6 +1173,11 @@ function dumpJSONNodePath
output JSON json = dumpJSONPath(InstNode.scopePath(node, ignoreBaseClass = true));
end dumpJSONNodePath;

function dumpJSONNodeEnclosingPath
input InstNode node;
output JSON json = dumpJSONPath(InstNode.enclosingScopePath(node, ignoreRedeclare = true));
end dumpJSONNodeEnclosingPath;

function dumpJSONPath
input Absyn.Path path;
output JSON json = JSON.makeString(AbsynUtil.pathString(path));
Expand Down Expand Up @@ -1258,7 +1263,7 @@ algorithm
algorithm
try
derivedNode := Lookup.lookupName(path, scope, NFInstContext.RELAXED, false);
json := JSON.addPair("baseClass", dumpJSONNodePath(derivedNode), json);
json := JSON.addPair("baseClass", dumpJSONNodeEnclosingPath(derivedNode), json);
else
end try;

Expand Down
Expand Up @@ -135,7 +135,7 @@ getModelInstance(M, prettyPrint = true);
// }
// }
// },
// \"baseClass\": \"C\",
// \"baseClass\": \"A.C\",
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 13,
Expand Down
107 changes: 107 additions & 0 deletions testsuite/openmodelica/instance-API/GetModelInstanceReplaceable4.mos
@@ -0,0 +1,107 @@
// name: GetModelInstanceReplaceable4
// keywords:
// status: correct
// cflags: -d=newInst
//
//

loadString("
package P
model A
replaceable model B
Real x;
end B;

replaceable model C
Real y;
end C;

replaceable model D = C constrainedby C;
end A;

model M
A a;
end M;
end P;
");

getModelInstance(P.M, prettyPrint = true);

// Result:
// true
// "{
// \"name\": \"P.M\",
// \"restriction\": \"model\",
// \"elements\": [
// {
// \"$kind\": \"component\",
// \"name\": \"a\",
// \"type\": {
// \"name\": \"P.A\",
// \"restriction\": \"model\",
// \"elements\": [
// {
// \"$kind\": \"class\",
// \"name\": \"B\",
// \"prefixes\": {
// \"replaceable\": true
// },
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 4,
// \"columnStart\": 19,
// \"lineEnd\": 6,
// \"columnEnd\": 12
// }
// },
// {
// \"$kind\": \"class\",
// \"name\": \"C\",
// \"prefixes\": {
// \"replaceable\": true
// },
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 8,
// \"columnStart\": 19,
// \"lineEnd\": 10,
// \"columnEnd\": 12
// }
// },
// {
// \"$kind\": \"class\",
// \"name\": \"D\",
// \"prefixes\": {
// \"replaceable\": {
// \"constrainedby\": \"C\"
// }
// },
// \"baseClass\": \"P.A.C\",
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 12,
// \"columnStart\": 19,
// \"lineEnd\": 12,
// \"columnEnd\": 31
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 3,
// \"columnStart\": 5,
// \"lineEnd\": 13,
// \"columnEnd\": 10
// }
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"lineStart\": 15,
// \"columnStart\": 5,
// \"lineEnd\": 17,
// \"columnEnd\": 10
// }
// }"
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Expand Up @@ -47,6 +47,7 @@ GetModelInstanceMod4.mos \
GetModelInstanceReplaceable1.mos \
GetModelInstanceReplaceable2.mos \
GetModelInstanceReplaceable3.mos \
GetModelInstanceReplaceable4.mos \
GetModelInstanceStateMachine1.mos \


Expand Down

0 comments on commit 00c7cfd

Please sign in to comment.