From 6d23817b629df3b3955f3714ab05dfd6dc6c13cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Tue, 24 Nov 2020 17:14:03 +0100 Subject: [PATCH] Clean up Absyn.Path function in AbsynUtil a bit - Add pathSetFirstIdent. - Change behaviour of pathSetLastIdent to replace the last identifier with a string instead of a path, since that's what the name implies and also how it's actually used. - Change behaviour of pathContains to take a string identifier instead of a path and rename it pathContainsIdent, since that's how it's used. - Remove pathReplaceIdent and change the only use of the function to use pathSetLastIdent instead, since that's what it did. - Remove unused and rather specialized functions pathTwoLastIdents, prefixOptPath, and pathReplaceFirstIdent. --- .../Compiler/BackEnd/EvaluateFunctions.mo | 2 +- OMCompiler/Compiler/FFrontEnd/FCore.mo | 2 +- OMCompiler/Compiler/FrontEnd/AbsynUtil.mo | 151 +++++------------- OMCompiler/Compiler/FrontEnd/InstFunction.mo | 2 +- OMCompiler/Compiler/FrontEnd/SCodeSimplify.mo | 2 +- 5 files changed, 44 insertions(+), 115 deletions(-) diff --git a/OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo b/OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo index b3dfa4ee7f1..7afd7e37eda 100644 --- a/OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo +++ b/OMCompiler/Compiler/BackEnd/EvaluateFunctions.mo @@ -1557,7 +1557,7 @@ algorithm // Update the path. s := AbsynUtil.pathLastIdent(funcOut.path); s := s + "_eval" + intString(idx); - funcOut.path := AbsynUtil.pathReplaceIdent(AbsynUtil.makeNotFullyQualified(funcOut.path), s); + funcOut.path := AbsynUtil.pathSetLastIdent(AbsynUtil.makeNotFullyQualified(funcOut.path), s); // Update the type. funcOut.type_ := updateFunctionType(funcOut.type_, outputs, origOutputs); diff --git a/OMCompiler/Compiler/FFrontEnd/FCore.mo b/OMCompiler/Compiler/FFrontEnd/FCore.mo index b65e4dc244c..1af96555a1f 100644 --- a/OMCompiler/Compiler/FFrontEnd/FCore.mo +++ b/OMCompiler/Compiler/FFrontEnd/FCore.mo @@ -733,7 +733,7 @@ algorithm else lastId := AbsynUtil.pathLastIdent(inPath); lastId := getRecordConstructorName(lastId); - outPath := AbsynUtil.pathSetLastIdent(inPath, AbsynUtil.makeIdentPathFromString(lastId)); + outPath := AbsynUtil.pathSetLastIdent(inPath, lastId); end if; end getRecordConstructorPath; diff --git a/OMCompiler/Compiler/FrontEnd/AbsynUtil.mo b/OMCompiler/Compiler/FrontEnd/AbsynUtil.mo index 3bf91385d21..b24a7cb6513 100644 --- a/OMCompiler/Compiler/FrontEnd/AbsynUtil.mo +++ b/OMCompiler/Compiler/FrontEnd/AbsynUtil.mo @@ -1829,20 +1829,6 @@ algorithm end match; end stringListPathReversed2; -public function pathTwoLastIdents "Returns the two last idents of a path" - input Absyn.Path inPath; - output Absyn.Path outTwoLast; -algorithm - outTwoLast := match(inPath) - local - Absyn.Path p; - - case Absyn.QUALIFIED(path = Absyn.IDENT()) then inPath; - case Absyn.QUALIFIED(path = p) then pathTwoLastIdents(p); - case Absyn.FULLYQUALIFIED(path = p) then pathTwoLastIdents(p); - end match; -end pathTwoLastIdents; - public function pathLastIdent "Returns the last ident (after last dot) in a path" input Absyn.Path inPath; @@ -1859,6 +1845,21 @@ algorithm end match; end pathLastIdent; +public function pathSetLastIdent + "Replaces the last identifier in the path." + input Absyn.Path path; + input String ident; + output Absyn.Path outPath; +algorithm + outPath := match path + case Absyn.IDENT() then Absyn.IDENT(ident); + case Absyn.QUALIFIED() + then Absyn.QUALIFIED(path.name, pathSetLastIdent(path.path, ident)); + case Absyn.FULLYQUALIFIED() + then Absyn.FULLYQUALIFIED(pathSetLastIdent(path.path, ident)); + end match; +end pathSetLastIdent; + public function pathLast "Returns the last ident (after last dot) in a path" input output Absyn.Path path; @@ -1887,6 +1888,20 @@ algorithm end match; end pathFirstIdent; +public function pathSetFirstIdent + "Replaces the first identifier in a path." + input Absyn.Path path; + input String ident; + output Absyn.Path outPath; +algorithm + outPath := match path + case Absyn.IDENT() then Absyn.IDENT(ident); + case Absyn.QUALIFIED() then Absyn.QUALIFIED(ident, path.path); + case Absyn.FULLYQUALIFIED() + then Absyn.FULLYQUALIFIED(pathSetFirstIdent(path.path, ident)); + end match; +end pathSetFirstIdent; + public function pathFirstPath input Absyn.Path inPath; output Absyn.Path outPath; @@ -1981,21 +1996,6 @@ algorithm outPath := Absyn.QUALIFIED(prefix, path); end prefixPath; -public function prefixOptPath - "Prefixes an optional path with an identifier." - input Absyn.Ident prefix; - input Option optPath; - output Option outPath; -algorithm - outPath := match(prefix, optPath) - local - Absyn.Path path; - - case (_, NONE()) then SOME(Absyn.IDENT(prefix)); - case (_, SOME(path)) then SOME(Absyn.QUALIFIED(prefix, path)); - end match; -end prefixOptPath; - public function suffixPath "Adds a suffix to a path. Ex: suffixPath(a.b.c, 'd') => a.b.c.d" @@ -2078,24 +2078,6 @@ algorithm end match; end pathToStringListWork; -public function pathReplaceFirstIdent " - Replaces the first part of a path with a replacement path: - (a.b.c, d.e) => d.e.b.c - (a, b.c.d) => b.c.d -" - input Absyn.Path path; - input Absyn.Path replPath; - output Absyn.Path outPath; -algorithm - outPath := match(path,replPath) - local - Absyn.Path p; - // Should not be possible to replace FQ paths - case (Absyn.QUALIFIED(path = p), _) then joinPaths(replPath,p); - case (Absyn.IDENT(), _) then replPath; - end match; -end pathReplaceFirstIdent; - public function addSubscriptsLast "Function for appending subscripts at end of last ident" input Absyn.ComponentRef icr; @@ -2298,32 +2280,19 @@ algorithm end match; end crefRemovePrefix; -public function pathContains - "Author BZ, - checks if one Absyn.IDENT(..) is contained in path." - input Absyn.Path fullPath; - input Absyn.Path pathId; - output Boolean b; +public function pathContainsIdent + "Returns whether any identifier in the path is equal to the given identifier." + input Absyn.Path path; + input String ident; + output Boolean res; algorithm - b := match (fullPath,pathId) - local - String str1,str2; - Absyn.Path qp; - Boolean b1,b2; - - case(Absyn.IDENT(str1),Absyn.IDENT(str2)) then stringEq(str1,str2); - - case(Absyn.QUALIFIED(str1,qp),Absyn.IDENT(str2)) - equation - b1 = stringEq(str1,str2); - b2 = pathContains(qp,pathId); - b1 = boolOr(b1,b2); - then - b1; - - case(Absyn.FULLYQUALIFIED(qp),_) then pathContains(qp,pathId); + res := match path + case Absyn.IDENT() then path.name == ident; + case Absyn.QUALIFIED() + then path.name == ident or pathContainsIdent(path.path, ident); + case Absyn.FULLYQUALIFIED() then pathContainsIdent(path.path, ident); end match; -end pathContains; +end pathContainsIdent; public function pathContainsString "Author OT, @@ -3702,19 +3671,6 @@ algorithm end match; end getIteratorIndexedCrefs; -public function pathReplaceIdent - input Absyn.Path path; - input String last; - output Absyn.Path out; -algorithm - out := match (path,last) - local Absyn.Path p; String n,s; - case (Absyn.FULLYQUALIFIED(p),s) equation p = pathReplaceIdent(p,s); then Absyn.FULLYQUALIFIED(p); - case (Absyn.QUALIFIED(n,p),s) equation p = pathReplaceIdent(p,s); then Absyn.QUALIFIED(n,p); - case (Absyn.IDENT(),s) then Absyn.IDENT(s); - end match; -end pathReplaceIdent; - public function getFileNameFromInfo input SourceInfo inInfo; output String inFileName; @@ -4607,33 +4563,6 @@ algorithm Absyn.COMPONENTITEM(component=Absyn.COMPONENT(name=name)) := c; end componentName; -public function pathSetLastIdent - input Absyn.Path inPath; - input Absyn.Path inLastIdent; - output Absyn.Path outPath; -algorithm - outPath := match(inPath, inLastIdent) - local - Absyn.Path p; - String n; - - case (Absyn.IDENT(), _) then inLastIdent; - - case (Absyn.QUALIFIED(n, p), _) - equation - p = pathSetLastIdent(p, inLastIdent); - then - Absyn.QUALIFIED(n, p); - - case (Absyn.FULLYQUALIFIED(p), _) - equation - p = pathSetLastIdent(p, inLastIdent); - then - Absyn.FULLYQUALIFIED(p); - - end match; -end pathSetLastIdent; - public function expContainsInitial "@author: returns true if expression contains initial()" diff --git a/OMCompiler/Compiler/FrontEnd/InstFunction.mo b/OMCompiler/Compiler/FrontEnd/InstFunction.mo index bf5b249fcd5..42b024aa5e7 100644 --- a/OMCompiler/Compiler/FrontEnd/InstFunction.mo +++ b/OMCompiler/Compiler/FrontEnd/InstFunction.mo @@ -838,7 +838,7 @@ algorithm cache = InstUtil.addFunctionsToDAE(cache, {func}, SCode.NOT_PARTIAL()); // add the instance record constructor too! - path = AbsynUtil.pathSetLastIdent(path, AbsynUtil.makeIdentPathFromString(name)); + path = AbsynUtil.pathSetLastIdent(path, name); fixedTy = DAE.T_COMPLEX(ClassInf.RECORD(path), vars, eqCo); fargs = Types.makeFargsList(inputs); funcTy = DAE.T_FUNCTION(fargs, fixedTy, DAE.FUNCTION_ATTRIBUTES_DEFAULT, path); diff --git a/OMCompiler/Compiler/FrontEnd/SCodeSimplify.mo b/OMCompiler/Compiler/FrontEnd/SCodeSimplify.mo index 66ce9b7becd..3d808170965 100644 --- a/OMCompiler/Compiler/FrontEnd/SCodeSimplify.mo +++ b/OMCompiler/Compiler/FrontEnd/SCodeSimplify.mo @@ -171,7 +171,7 @@ algorithm // handle extends Modelica.Icons.* case (SCode.EXTENDS(baseClassPath = bcp)::rest) equation - true = AbsynUtil.pathContains(bcp, Absyn.IDENT("Icons")); + true = AbsynUtil.pathContains(bcp, "Icons"); els = simplifyElements(rest); then els;