From f1476ee56265aed39571c4ed315c0b4b9335a2a7 Mon Sep 17 00:00:00 2001 From: perost Date: Thu, 18 Aug 2022 15:18:41 +0200 Subject: [PATCH] Stop at root classes in InstNode.scopePathClass (#9307) - Stop when reaching a root class in InstNode.scopePathClass to conform to the definition of fully qualified name used for getInstanceName in the specification. --- .../Compiler/NFFrontEnd/NFBuiltinCall.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFCeval.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFClass.mo | 2 +- .../Compiler/NFFrontEnd/NFEvalFunction.mo | 4 +- OMCompiler/Compiler/NFFrontEnd/NFFunction.mo | 2 +- OMCompiler/Compiler/NFFrontEnd/NFInst.mo | 6 +- OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo | 62 ++++++++++++------- .../NFFrontEnd/NFOperatorOverloading.mo | 4 +- OMCompiler/Compiler/Script/NFApi.mo | 6 +- 9 files changed, 53 insertions(+), 37 deletions(-) diff --git a/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo b/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo index b1e193f6e19..31e83e3e141 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo @@ -1843,7 +1843,7 @@ protected InstNode scope; algorithm Call.UNTYPED_CALL(call_scope = scope) := call; - result := Expression.STRING(AbsynUtil.pathString(InstNode.scopePath(scope, includeRoot = true))); + result := Expression.STRING(AbsynUtil.pathString(InstNode.rootPath(scope))); end typeGetInstanceName; function typeClockCall diff --git a/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo b/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo index 20e1b506fc0..7e4558f6922 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFCeval.mo @@ -798,7 +798,7 @@ algorithm args := arg :: args; end for; - exp := Expression.makeRecord(InstNode.scopePath(recordNode, includeRoot = true), recordType, args); + exp := Expression.makeRecord(InstNode.fullPath(recordNode), recordType, args); end makeRecordBindingExp; function evalTypename diff --git a/OMCompiler/Compiler/NFFrontEnd/NFClass.mo b/OMCompiler/Compiler/NFFrontEnd/NFClass.mo index abd5d170b7e..25deb83ae98 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFClass.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFClass.mo @@ -780,7 +780,7 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES( ty as Type.COMPLEX(complexTy = ComplexType.RECORD(ty_node)) := getType(cls, clsNode); fields := ClassTree.getComponents(classTree(cls)); args := list(Binding.getExp(Component.getImplicitBinding(InstNode.component(f))) for f in fields); - exp := Expression.makeRecord(InstNode.scopePath(ty_node, includeRoot = true), ty, args); + exp := Expression.makeRecord(InstNode.fullPath(ty_node), ty, args); end makeRecordExp; function toFlatStream diff --git a/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo b/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo index 372b0c18987..c98ff0e42fd 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo @@ -396,7 +396,7 @@ algorithm UnorderedMap.apply(local_map, function applyBindingReplacement(map = local_map)); bindings := UnorderedMap.valueList(local_map); then - Expression.makeRecord(InstNode.scopePath(cls_node, includeRoot = true), cls.ty, bindings); + Expression.makeRecord(InstNode.fullPath(cls_node), cls.ty, bindings); case Class.TYPED_DERIVED() then buildRecordBinding(cls.baseClass, map, mutableParams); end match; @@ -1441,7 +1441,7 @@ algorithm expl := getExternalOutputResult(c, map) :: expl; end for; - exp := Expression.makeRecord(InstNode.scopePath(cls_node, includeRoot = true), + exp := Expression.makeRecord(InstNode.fullPath(cls_node), InstNode.getType(cls_node), listReverseInPlace(expl)); else Error.assertion(false, getInstanceName() + diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFunction.mo b/OMCompiler/Compiler/NFFrontEnd/NFFunction.mo index 01ee5be2eeb..af8efa8916a 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFunction.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFunction.mo @@ -398,7 +398,7 @@ uniontype Function case CachedData.FUNCTION() then (); else algorithm - node := instFunction2(InstNode.scopePath(node, includeRoot = true), node, context, info); + node := instFunction2(InstNode.fullPath(node), node, context, info); then (); end match; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo index 55cceef3436..4c2cac7600f 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo @@ -2182,11 +2182,9 @@ algorithm InstNode.cacheInitFunc(node); if SCodeUtil.isOperatorRecord(InstNode.definition(node)) then - OperatorOverloading.instConstructor( - InstNode.scopePath(node, includeRoot = true), node, context, InstNode.info(node)); + OperatorOverloading.instConstructor(InstNode.fullPath(node), node, context, InstNode.info(node)); else - Record.instDefaultConstructor( - InstNode.scopePath(node, includeRoot = true), node, context, InstNode.info(node)); + Record.instDefaultConstructor(InstNode.fullPath(node), node, context, InstNode.info(node)); end if; then (); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo b/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo index b5b5e58e184..564de5edc63 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo @@ -1078,9 +1078,27 @@ uniontype InstNode end match; end scopeListClass; + type ScopeType = enumeration( + RELATIVE "Stops at a root class and doesn't include the root", + INCLUDING_ROOT "Stops at a root class and includes the root", + FULL "Stops at the top scope" + ); + + function rootPath + input InstNode node; + input Boolean ignoreBaseClass = false "Ignore that a class is a base class if true."; + output Absyn.Path path = scopePath(node, ScopeType.INCLUDING_ROOT, ignoreBaseClass); + end rootPath; + + function fullPath + input InstNode node; + input Boolean ignoreBaseClass = false "Ignore that a class is a base class if true."; + output Absyn.Path path = scopePath(node, ScopeType.FULL, ignoreBaseClass); + end fullPath; + function scopePath input InstNode node; - input Boolean includeRoot = false "Whether to include the root class name or not."; + input ScopeType scopeType = ScopeType.RELATIVE; input Boolean ignoreBaseClass = false "Ignore that a class is a base class if true."; output Absyn.Path path; algorithm @@ -1091,12 +1109,12 @@ uniontype InstNode case CLASS_NODE(nodeType = it) then match it - case InstNodeType.BASE_CLASS() guard not ignoreBaseClass then scopePath(it.parent, includeRoot); - else scopePath2(node.parentScope, includeRoot, Absyn.IDENT(node.name)); + case InstNodeType.BASE_CLASS() guard not ignoreBaseClass then scopePath(it.parent, scopeType); + else scopePath2(node.parentScope, scopeType, Absyn.IDENT(node.name)); end match; - case COMPONENT_NODE() then scopePath2(node.parent, includeRoot, Absyn.IDENT(node.name)); - case IMPLICIT_SCOPE() then scopePath(node.parentScope, includeRoot); + case COMPONENT_NODE() then scopePath2(node.parent, scopeType, Absyn.IDENT(node.name)); + case IMPLICIT_SCOPE() then scopePath(node.parentScope, scopeType); // For debugging. else Absyn.IDENT(name(node)); @@ -1105,13 +1123,13 @@ uniontype InstNode function scopePath2 input InstNode node; - input Boolean includeRoot; + input ScopeType scopeType; input Absyn.Path accumPath; output Absyn.Path path; algorithm path := match node - case CLASS_NODE() then scopePathClass(node, node.nodeType, includeRoot, accumPath); - case COMPONENT_NODE() then scopePath2(node.parent, includeRoot, Absyn.QUALIFIED(node.name, accumPath)); + case CLASS_NODE() then scopePathClass(node, node.nodeType, scopeType, accumPath); + case COMPONENT_NODE() then scopePath2(node.parent, scopeType, Absyn.QUALIFIED(node.name, accumPath)); else accumPath; end match; end scopePath2; @@ -1119,30 +1137,32 @@ uniontype InstNode function scopePathClass input InstNode node; input InstNodeType ty; - input Boolean includeRoot; + input ScopeType scopeType; input Absyn.Path accumPath; output Absyn.Path path; algorithm path := match ty case InstNodeType.NORMAL_CLASS() - then scopePath2(classParent(node), includeRoot, Absyn.QUALIFIED(className(node), accumPath)); + then scopePath2(classParent(node), scopeType, Absyn.QUALIFIED(className(node), accumPath)); case InstNodeType.BASE_CLASS() - then scopePath2(ty.parent, includeRoot, accumPath); + then scopePath2(ty.parent, scopeType, accumPath); case InstNodeType.DERIVED_CLASS() - then scopePathClass(node, ty.ty, includeRoot, accumPath); + then scopePathClass(node, ty.ty, scopeType, accumPath); case InstNodeType.BUILTIN_CLASS() then Absyn.QUALIFIED(className(node), accumPath); case InstNodeType.TOP_SCOPE() then accumPath; case InstNodeType.ROOT_CLASS() - then if includeRoot then - scopePath2(classParent(node), includeRoot, Absyn.QUALIFIED(className(node), accumPath)) - else - accumPath; + then if scopeType == ScopeType.FULL then + scopePath2(classParent(node), scopeType, Absyn.QUALIFIED(className(node), accumPath)) + elseif scopeType == ScopeType.INCLUDING_ROOT then + Absyn.QUALIFIED(className(node), accumPath) + else + accumPath; case InstNodeType.REDECLARED_CLASS() - then scopePath2(ty.parent, includeRoot, Absyn.QUALIFIED(className(node), accumPath)); + then scopePath2(ty.parent, scopeType, Absyn.QUALIFIED(className(node), accumPath)); case InstNodeType.IMPLICIT_SCOPE() - then scopePath2(classParent(node), includeRoot, accumPath); + then scopePath2(classParent(node), scopeType, accumPath); else algorithm Error.assertion(false, getInstanceName() + " got unknown node type", sourceInfo()); @@ -1648,8 +1668,7 @@ uniontype InstNode else algorithm - state := Restriction.toDAE(Class.restriction(cls), - scopePath(clsNode, includeRoot = true)); + state := Restriction.toDAE(Class.restriction(cls), fullPath(clsNode)); then DAE.Type.T_COMPLEX(state, {}, NONE()); @@ -1691,8 +1710,7 @@ uniontype InstNode else algorithm - state := Restriction.toDAE(Class.restriction(cls), - scopePath(clsNode, includeRoot = true)); + state := Restriction.toDAE(Class.restriction(cls), fullPath(clsNode)); vars := ConvertDAE.makeTypeVars(clsNode); outType := DAE.Type.T_COMPLEX(state, vars, NONE()); Pointer.update(clsNode.cls, Class.DAE_TYPE(outType)); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFOperatorOverloading.mo b/OMCompiler/Compiler/NFFrontEnd/NFOperatorOverloading.mo index 1dad4bcc68a..4e44cd24a41 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFOperatorOverloading.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFOperatorOverloading.mo @@ -72,7 +72,7 @@ public // If it has an overloaded constructor, instantiate it and add the // function(s) to the record node. (_, ctor_node) := Function.instFunctionRef(ctor_ref, context, info); - ctor_path := InstNode.scopePath(ctor_node, includeRoot = true); + ctor_path := InstNode.fullPath(ctor_node); for f in Function.getCachedFuncs(ctor_node) loop checkOperatorConstructorOutput(f, Class.lastBaseClass(recordNode), ctor_path, info); @@ -124,7 +124,7 @@ public algorithm if not SCodeUtil.isElementEncapsulated(InstNode.definition(operatorNode)) then Error.addSourceMessage(Error.OPERATOR_NOT_ENCAPSULATED, - {AbsynUtil.pathString(InstNode.scopePath(operatorNode, includeRoot = true))}, + {AbsynUtil.pathString(InstNode.fullPath(operatorNode))}, InstNode.info(operatorNode)); fail(); end if; diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index 8b4418f97c2..c34b6b0815c 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -530,7 +530,7 @@ algorithm cls := Lookup.lookupClassName(pathToQualify, expanded_cls, NFInstContext.RELAXED, AbsynUtil.dummyInfo, checkAccessViolations = false); end if; - qualPath := InstNode.scopePath(cls, true); + qualPath := InstNode.fullPath(cls); if not Flags.isSet(Flags.NF_API_NOISE) then ErrorExt.rollBack("NFApi.mkFullyQual"); @@ -820,8 +820,8 @@ algorithm cls := InstNode.getClass(cls_node); extendsPaths := match cls - case Class.EXPANDED_DERIVED() then {InstNode.scopePath(cls.baseClass, true, true)}; - else list(InstNode.scopePath(e, true, true) for e in ClassTree.getExtends(Class.classTree(cls))); + case Class.EXPANDED_DERIVED() then {InstNode.fullPath(cls.baseClass, true)}; + else list(InstNode.fullPath(e, true) for e in ClassTree.getExtends(Class.classTree(cls))); end match; end getInheritedClasses;