From c0ce050e56ae4217935431c04e23264b7e9836c5 Mon Sep 17 00:00:00 2001 From: kabdelhak <38032125+kabdelhak@users.noreply.github.com> Date: Tue, 6 Dec 2022 15:07:35 +0100 Subject: [PATCH] [NF] propagate hide result to primitive types (#9859) * [NF] propagate hide result to primitive types fixes ticket #4346 --- OMCompiler/Compiler/FrontEnd/SCodeUtil.mo | 36 ++++++++++++++++++-- OMCompiler/Compiler/NFFrontEnd/NFInst.mo | 4 +++ OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo | 30 ++++++++++++++++ OMCompiler/Compiler/NFFrontEnd/NFVariable.mo | 35 +++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/OMCompiler/Compiler/FrontEnd/SCodeUtil.mo b/OMCompiler/Compiler/FrontEnd/SCodeUtil.mo index 8d93a614dce..a34e4ad48b1 100644 --- a/OMCompiler/Compiler/FrontEnd/SCodeUtil.mo +++ b/OMCompiler/Compiler/FrontEnd/SCodeUtil.mo @@ -3472,10 +3472,38 @@ algorithm end match; end isInlineTypeSubMod; +public function appendAnnotationToCommentOption + input SCode.Annotation inAnnotation; + input Option inComment; + input Boolean check_replace = false; + output Option outComment; +algorithm + outComment := match inComment + local + SCode.Comment comment; + case SOME(comment) then SOME(appendAnnotationToComment(inAnnotation, comment, check_replace)); + else SOME(SCode.COMMENT(SOME(inAnnotation), NONE())); + end match; +end appendAnnotationToCommentOption; + public function appendAnnotationToComment input SCode.Annotation inAnnotation; input SCode.Comment inComment; + input Boolean check_replace = false; output SCode.Comment outComment; +protected + function isNotElem + input SCode.SubMod mod; + input list mods; + output Boolean b = true; + algorithm + for m in mods loop + if (mod.ident == m.ident) then + b := false; + return; + end if; + end for; + end isNotElem; algorithm outComment := match(inAnnotation, inComment) local @@ -3491,8 +3519,12 @@ algorithm case (SCode.ANNOTATION(modification = SCode.MOD(subModLst = mods1)), SCode.COMMENT(SOME(SCode.ANNOTATION(SCode.MOD(fp, ep, mods2, b, info))), cmt)) - equation - mods2 = listAppend(mods1, mods2); + algorithm + if not check_replace then + mods2 := listAppend(mods1, mods2); + else + mods2 := listAppend(mods1, List.filterOnTrue(mods2, function isNotElem(mods = mods1))); + end if; then SCode.COMMENT(SOME(SCode.ANNOTATION(SCode.MOD(fp, ep, mods2, b, info))), cmt); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo index 9e63d25d5eb..4b135e4679b 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo @@ -217,6 +217,10 @@ algorithm flatModel := InstUtil.combineSubscripts(flatModel); + // propagate hide result attribute + // ticket #4346 + flatModel.variables := list(Variable.propagateAnnotation("HideResult", false, var) for var in flatModel.variables); + if Flags.getConfigString(Flags.OBFUSCATE) == "protected" or Flags.getConfigString(Flags.OBFUSCATE) == "encrypted" then flatModel := FlatModel.obfuscate(flatModel); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo b/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo index bcca39d1240..ba1ba04a2f1 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo @@ -1101,6 +1101,36 @@ uniontype InstNode end match; end scopeListClass; + function getAnnotation + input String name; + input InstNode node; + output Option mod = NONE(); + algorithm + + if InstNode.isComponent(node) then + mod := match Component.comment(InstNode.component(node)) + local + list subModLst; + Boolean done = false; + + case SOME(SCode.COMMENT(annotation_=SOME(SCode.ANNOTATION(modification = SCode.MOD(subModLst = subModLst))))) + algorithm + for sm in subModLst loop + if sm.ident == name then + mod := SOME(sm); + done := true; + break; + end if; + end for; + if not done then + mod := getAnnotation(name, parent(node)); + end if; + then mod; + else getAnnotation(name, parent(node)); + end match; + end if; + end getAnnotation; + 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", diff --git a/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo b/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo index 57fcbe89e95..be9a6c468a7 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo @@ -290,6 +290,41 @@ public binding := NFBinding.EMPTY_BINDING; end lookupTypeAttribute; + function propagateAnnotation + input String name; + input Boolean overwrite; + input output Variable var; + protected + InstNode node; + Option mod; + protected + SCode.Annotation anno; + algorithm + if ComponentRef.isCref(var.name) then + node := ComponentRef.node(var.name); + // InstNode.getAnnotation is recursive and returns the first annotation found. + // if the original is supposed to be overwritten, skip the node itself and look at the parent + if overwrite then + mod := match node + case InstNode.COMPONENT_NODE() then InstNode.getAnnotation(name, node.parent); + else NONE(); + end match; + else + mod := InstNode.getAnnotation(name, node); + end if; + + if isSome(mod) then + anno := SCode.ANNOTATION(modification = SCode.MOD( + finalPrefix = SCode.NOT_FINAL(), + eachPrefix = SCode.NOT_EACH(), + subModLst = {Util.getOption(mod)}, + binding = NONE(), + info = sourceInfo())); + var.comment := SCodeUtil.appendAnnotationToCommentOption(anno, var.comment, true); + end if; + end if; + end propagateAnnotation; + function mapExp input output Variable var; input MapFn fn;