Skip to content

Commit

Permalink
Remove non-top-level direction earlier (#11562)
Browse files Browse the repository at this point in the history
- Remove non-top-level variable directions during the instantiation
  instead of during the conversion to DAE, so it applies to the NB too.
  • Loading branch information
perost committed Nov 13, 2023
1 parent 16a960f commit 1b57f2d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
37 changes: 2 additions & 35 deletions OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -103,15 +103,13 @@ end convertStatements;
protected
uniontype VariableConversionSettings
record VARIABLE_CONVERSION_SETTINGS
Boolean useLocalDirection;
Integer exposeLocalIOs;
Boolean isFunctionParameter;
Boolean addTypeToSource;
end VARIABLE_CONVERSION_SETTINGS;
end VariableConversionSettings;

constant VariableConversionSettings FUNCTION_VARIABLE_CONVERSION_SETTINGS =
VARIABLE_CONVERSION_SETTINGS(false, 0, true, false);
VARIABLE_CONVERSION_SETTINGS(true, false);

function convertVariables
input list<Variable> variables;
Expand All @@ -120,8 +118,6 @@ protected
VariableConversionSettings settings;
algorithm
settings := VariableConversionSettings.VARIABLE_CONVERSION_SETTINGS(
useLocalDirection = Flags.getConfigBool(Flags.USE_LOCAL_DIRECTION),
exposeLocalIOs = Flags.getConfigInt(Flags.EXPOSE_LOCAL_IOS),
isFunctionParameter = false,
addTypeToSource = Flags.isSet(Flags.INFO_XML_OPERATIONS) or Flags.isSet(Flags.VISUAL_XML)
);
Expand Down Expand Up @@ -172,23 +168,11 @@ algorithm

var := match attr
case Attributes.ATTRIBUTES()
algorithm
// Strip input/output from non top-level components unless
// --useLocalDirection=true has been set.
// Alternatively strip input/output from non connectors and from protected connectors if
// --nonStdExposeLocalIOs has been set to respective level.
if attr.direction == Direction.NONE or settings.useLocalDirection or
(settings.exposeLocalIOs > 0 and attr.connectorType <> ConnectorType.NON_CONNECTOR and
vis == Visibility.PUBLIC and ComponentRef.depth(cref) <= settings.exposeLocalIOs + 1) then
dir := attr.direction;
else
dir := getComponentDirection(attr.direction, cref);
end if;
then
DAE.VAR(
dcref,
Prefixes.variabilityToDAE(attr.variability),
Prefixes.directionToDAE(dir),
Prefixes.directionToDAE(attr.direction),
Prefixes.parallelismToDAE(attr.parallelism),
Prefixes.visibilityToDAE(vis),
dty,
Expand Down Expand Up @@ -226,23 +210,6 @@ algorithm
end match;
end addComponentTypeToSource;

function getComponentDirection
"Returns the given direction if the cref refers to a top-level component,
a component in a top-level connector, or a component in a top-level input
component, otherwise returns Direction.NONE."
input output Direction dir;
input ComponentRef cref;
protected
ComponentRef rest_cref = ComponentRef.rest(cref);
algorithm
dir := match rest_cref
case ComponentRef.EMPTY() then dir;
case ComponentRef.CREF()
then if InstNode.isConnector(rest_cref.node) or InstNode.isInput(rest_cref.node) then
getComponentDirection(dir, rest_cref) else Direction.NONE;
end match;
end getComponentDirection;

function convertVarAttributes
input list<tuple<String, Binding>> attrs;
input Type ty;
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -1023,5 +1023,19 @@ public
end for;
end hasArrayConnections;
function removeNonTopLevelDirections
input output FlatModel flatModel;
protected
Integer expose_local_ios;
algorithm
// Keep the declared directions if --useLocalDirection=true has been set.
if Flags.getConfigBool(Flags.USE_LOCAL_DIRECTION) then
return;
end if;
expose_local_ios := Flags.getConfigInt(Flags.EXPOSE_LOCAL_IOS);
flatModel.variables := list(Variable.removeNonTopLevelDirection(v, expose_local_ios) for v in flatModel.variables);
end removeNonTopLevelDirections;
annotation(__OpenModelica_Interface="frontend");
end NFFlatModel;
4 changes: 3 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -144,7 +144,7 @@ protected
InstNode top, cls, inst_cls;
String name;
InstContext.Type context;
Integer var_count, eq_count;
Integer var_count, eq_count, expose_local_ios;
algorithm
//Inst_test(program);
resetGlobalFlags();
Expand Down Expand Up @@ -245,6 +245,8 @@ algorithm
// ticket #4346
flatModel.variables := list(Variable.propagateAnnotation("HideResult", false, var) for var in flatModel.variables);

flatModel := FlatModel.removeNonTopLevelDirections(flatModel);

if Flags.getConfigString(Flags.OBFUSCATE) == "protected" or
Flags.getConfigString(Flags.OBFUSCATE) == "encrypted" then
flatModel := FlatModel.obfuscate(flatModel);
Expand Down
45 changes: 42 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -365,9 +365,48 @@ public
end if;
end propagateAnnotation;

partial function MapFn
input output Expression exp;
end MapFn;
function removeNonTopLevelDirection
"Removes input/output prefixes from a variable that's not a top-level
component, a component in a top-level connector, or a component in a
top-level input component. exposeLocalIOs can be used to keep the direction
for variables at lower levels as well, where 0 means top-level, 1 the level
below that, and so on."
input output Variable var;
input Integer exposeLocalIOs;
protected
ComponentRef rest_name;
InstNode node;
Attributes attr;
algorithm
if var.attributes.direction == Direction.NONE then
return;
end if;

if exposeLocalIOs > 0 and
var.attributes.connectorType <> ConnectorType.NON_CONNECTOR and
var.visibility == Visibility.PUBLIC and
ComponentRef.depth(var.name) < exposeLocalIOs then
return;
end if;

rest_name := ComponentRef.rest(var.name);
while not ComponentRef.isEmpty(rest_name) loop
node := ComponentRef.node(rest_name);

if not (InstNode.isConnector(node) or InstNode.isInput(node)) then
attr := var.attributes;
attr.direction := Direction.NONE;
var.attributes := attr;
return;
end if;

rest_name := ComponentRef.rest(rest_name);
end while;
end removeNonTopLevelDirection;

partial function MapFn
input output Expression exp;
end MapFn;

function mapExp
input output Variable var;
Expand Down

0 comments on commit 1b57f2d

Please sign in to comment.