Skip to content

Commit

Permalink
[NF] propagate hide result to primitive types (#9859)
Browse files Browse the repository at this point in the history
* [NF] propagate hide result to primitive types
fixes ticket #4346
  • Loading branch information
kabdelhak committed Dec 6, 2022
1 parent 2c491b7 commit c0ce050
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
36 changes: 34 additions & 2 deletions OMCompiler/Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -3472,10 +3472,38 @@ algorithm
end match;
end isInlineTypeSubMod;

public function appendAnnotationToCommentOption
input SCode.Annotation inAnnotation;
input Option<SCode.Comment> inComment;
input Boolean check_replace = false;
output Option<SCode.Comment> 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<SCode.SubMod> 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
Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -1101,6 +1101,36 @@ uniontype InstNode
end match;
end scopeListClass;

function getAnnotation
input String name;
input InstNode node;
output Option<SCode.SubMod> mod = NONE();
algorithm

if InstNode.isComponent(node) then
mod := match Component.comment(InstNode.component(node))
local
list<SCode.SubMod> 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",
Expand Down
35 changes: 35 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -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<SCode.SubMod> 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;
Expand Down

0 comments on commit c0ce050

Please sign in to comment.