From 1b57f2d2bc129f2fb9e0a0d57235a09531443453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Mon, 13 Nov 2023 13:37:05 +0100 Subject: [PATCH] Remove non-top-level direction earlier (#11562) - Remove non-top-level variable directions during the instantiation instead of during the conversion to DAE, so it applies to the NB too. --- .../Compiler/NFFrontEnd/NFConvertDAE.mo | 37 +-------------- OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo | 14 ++++++ OMCompiler/Compiler/NFFrontEnd/NFInst.mo | 4 +- OMCompiler/Compiler/NFFrontEnd/NFVariable.mo | 45 +++++++++++++++++-- 4 files changed, 61 insertions(+), 39 deletions(-) diff --git a/OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo b/OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo index fa75f6531c7..d400eacbda5 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFConvertDAE.mo @@ -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 variables; @@ -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) ); @@ -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, @@ -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> attrs; input Type ty; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo b/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo index e9a4453624c..e5b58a7ea54 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo @@ -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; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo index 2a02937044f..4d24d4aa6fd 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFInst.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFInst.mo @@ -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(); @@ -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); diff --git a/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo b/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo index 42dd210f851..9277a7d34b4 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFVariable.mo @@ -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;