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

Commit 0fd679d

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix scoping issues.
- Fix InstNode.scopeList for derived classes. - When including the root class in scope names, include the full path of the root to avoid name clashes. Belonging to [master]: - #2474 - OpenModelica/OpenModelica-testsuite#964
1 parent 14c51e0 commit 0fd679d

File tree

3 files changed

+88
-42
lines changed

3 files changed

+88
-42
lines changed

Compiler/NFFrontEnd/NFFunction.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ uniontype Function
267267
end try;
268268

269269
(functionRef, found_scope) := Lookup.lookupFunctionName(functionName, scope, info);
270-
prefix := ComponentRef.fromNodeList(InstNode.scopeList(InstNode.classScope(found_scope)));
270+
prefix := ComponentRef.fromNodeList(InstNode.scopeList(InstNode.classScope(found_scope), includeRoot = true));
271271
functionRef := ComponentRef.append(functionRef, prefix);
272272
end lookupFunction;
273273

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ algorithm
648648
res := Restriction.fromSCode(SCode.getClassRestriction(element));
649649
cls := Class.EXPANDED_DERIVED(ext_node, mod, listArray(dims), prefs, attrs, res);
650650
node := InstNode.updateClass(cls, node);
651-
node := InstNode.setNodeType(InstNodeType.DERIVED_CLASS(), node);
651+
node := InstNode.setNodeType(InstNodeType.DERIVED_CLASS(InstNode.nodeType(node)), node);
652652
end expandClassDerived;
653653

654654
function instDerivedAttributes

Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ uniontype InstNodeType
5959
record BASE_CLASS
6060
"A base class extended by another class."
6161
InstNode parent;
62-
SCode.Element definition;
62+
SCode.Element definition "The extends clause definition.";
6363
end BASE_CLASS;
6464

6565
record DERIVED_CLASS
66+
"A short class definition."
67+
InstNodeType ty "The base node type not considering that it's a derived class.";
6668
end DERIVED_CLASS;
6769

6870
record BUILTIN_CLASS
@@ -396,6 +398,13 @@ uniontype InstNode
396398
end match;
397399
end name;
398400

401+
function className
402+
input InstNode node;
403+
output String name;
404+
algorithm
405+
CLASS_NODE(name = name) := node;
406+
end className;
407+
399408
function scopeName
400409
"Returns the name of a scope, which in the case of a component is the name
401410
of the component's type, and for a class simply the name of the class."
@@ -450,6 +459,13 @@ uniontype InstNode
450459
end match;
451460
end parent;
452461

462+
function classParent
463+
input InstNode node;
464+
output InstNode parent;
465+
algorithm
466+
CLASS_NODE(parentScope = parent) := node;
467+
end classParent;
468+
453469
function derivedParent
454470
input InstNode node;
455471
output InstNode parent;
@@ -762,31 +778,50 @@ uniontype InstNode
762778

763779
function scopeList
764780
input InstNode node;
781+
input Boolean includeRoot = false "Whether to include the root class name or not.";
765782
input list<InstNode> accumScopes = {};
766783
output list<InstNode> scopes;
767784
algorithm
768785
scopes := match node
769-
local
770-
InstNodeType it;
771-
772-
case CLASS_NODE(nodeType = it)
773-
then
774-
match it
775-
case InstNodeType.NORMAL_CLASS()
776-
then scopeList(node.parentScope, node :: accumScopes);
777-
case InstNodeType.BASE_CLASS()
778-
then scopeList(it.parent, accumScopes);
779-
case InstNodeType.BUILTIN_CLASS()
780-
then node :: accumScopes;
781-
else accumScopes;
782-
end match;
783-
786+
case CLASS_NODE() then scopeListClass(node, node.nodeType, includeRoot, accumScopes);
784787
case COMPONENT_NODE(parent = EMPTY_NODE()) then accumScopes;
785-
case COMPONENT_NODE() then scopeList(node.parent, node :: accumScopes);
786-
case IMPLICIT_SCOPE() then scopeList(node.parentScope, accumScopes);
788+
case COMPONENT_NODE() then scopeList(node.parent, includeRoot, node :: accumScopes);
789+
case IMPLICIT_SCOPE() then scopeList(node.parentScope, includeRoot, accumScopes);
790+
else accumScopes;
787791
end match;
788792
end scopeList;
789793

794+
function scopeListClass
795+
input InstNode clsNode;
796+
input InstNodeType ty;
797+
input Boolean includeRoot;
798+
input list<InstNode> accumScopes = {};
799+
output list<InstNode> scopes;
800+
algorithm
801+
scopes := match ty
802+
case InstNodeType.NORMAL_CLASS()
803+
then scopeList(parent(clsNode), includeRoot, clsNode :: accumScopes);
804+
case InstNodeType.BASE_CLASS()
805+
then scopeList(ty.parent, includeRoot, accumScopes);
806+
case InstNodeType.DERIVED_CLASS()
807+
then scopeListClass(clsNode, ty.ty, includeRoot, accumScopes);
808+
case InstNodeType.BUILTIN_CLASS()
809+
then clsNode :: accumScopes;
810+
case InstNodeType.TOP_SCOPE()
811+
then accumScopes;
812+
case InstNodeType.ROOT_CLASS()
813+
then if includeRoot then
814+
scopeList(parent(clsNode), includeRoot, clsNode :: accumScopes)
815+
else
816+
accumScopes;
817+
else
818+
algorithm
819+
Error.assertion(false, getInstanceName() + " got unknown node type", sourceInfo());
820+
then
821+
fail();
822+
end match;
823+
end scopeListClass;
824+
790825
function scopePath
791826
input InstNode node;
792827
input Boolean includeRoot = false "Whether to include the root class name or not.";
@@ -818,32 +853,43 @@ uniontype InstNode
818853
output Absyn.Path path;
819854
algorithm
820855
path := match node
821-
local
822-
InstNodeType it;
823-
824-
case CLASS_NODE(nodeType = it)
825-
then
826-
match it
827-
case InstNodeType.NORMAL_CLASS()
828-
then scopePath2(node.parentScope, includeRoot, Absyn.QUALIFIED(node.name, accumPath));
829-
case InstNodeType.BASE_CLASS()
830-
then scopePath2(it.parent, includeRoot, accumPath);
831-
case InstNodeType.DERIVED_CLASS()
832-
then scopePath2(node.parentScope, includeRoot, Absyn.QUALIFIED(node.name, accumPath));
833-
case InstNodeType.BUILTIN_CLASS()
834-
then Absyn.QUALIFIED(node.name, accumPath);
835-
case InstNodeType.ROOT_CLASS()
836-
then if includeRoot then Absyn.QUALIFIED(node.name, accumPath) else accumPath;
837-
else accumPath;
838-
end match;
839-
840-
case COMPONENT_NODE()
841-
then scopePath2(node.parent, includeRoot, Absyn.QUALIFIED(node.name, accumPath));
842-
856+
case CLASS_NODE() then scopePathClass(node, node.nodeType, includeRoot, accumPath);
857+
case COMPONENT_NODE() then scopePath2(node.parent, includeRoot, Absyn.QUALIFIED(node.name, accumPath));
843858
else accumPath;
844859
end match;
845860
end scopePath2;
846861

862+
function scopePathClass
863+
input InstNode node;
864+
input InstNodeType ty;
865+
input Boolean includeRoot;
866+
input Absyn.Path accumPath;
867+
output Absyn.Path path;
868+
algorithm
869+
path := match ty
870+
case InstNodeType.NORMAL_CLASS()
871+
then scopePath2(classParent(node), includeRoot, Absyn.QUALIFIED(className(node), accumPath));
872+
case InstNodeType.BASE_CLASS()
873+
then scopePath2(ty.parent, includeRoot, accumPath);
874+
case InstNodeType.DERIVED_CLASS()
875+
then scopePathClass(node, ty.ty, includeRoot, accumPath);
876+
case InstNodeType.BUILTIN_CLASS()
877+
then Absyn.QUALIFIED(className(node), accumPath);
878+
case InstNodeType.TOP_SCOPE()
879+
then accumPath;
880+
case InstNodeType.ROOT_CLASS()
881+
then if includeRoot then
882+
scopePath2(classParent(node), includeRoot, Absyn.QUALIFIED(className(node), accumPath))
883+
else
884+
accumPath;
885+
else
886+
algorithm
887+
Error.assertion(false, getInstanceName() + " got unknown node type", sourceInfo());
888+
then
889+
fail();
890+
end match;
891+
end scopePathClass;
892+
847893
function isInput
848894
input InstNode node;
849895
output Boolean isInput;

0 commit comments

Comments
 (0)