Skip to content

Commit

Permalink
Fix the check for outer elements in functions (#8919)
Browse files Browse the repository at this point in the history
- Make sure we don't fail due to not instantiated outer components
  before we get to the error check.
  • Loading branch information
perost committed May 10, 2022
1 parent 0744f5a commit 9cc8609
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -168,6 +168,7 @@ public
case TYPED_COMPONENT() then component.classInst;
case ITERATOR(ty = Type.COMPLEX(cls = classInst)) then classInst;
case ITERATOR() then InstNode.ITERATOR_NODE(Expression.EMPTY(component.ty));
else InstNode.EMPTY_NODE();
end match;
end classInstance;

Expand Down Expand Up @@ -612,6 +613,13 @@ public
end match;
end innerOuter;

function isInnerOuter
input Component component;
output Boolean isInnerOuter;
algorithm
isInnerOuter := innerOuter(component) <> InnerOuter.NOT_INNER_OUTER;
end isInnerOuter;

function isInner
input Component component;
output Boolean isInner;
Expand Down
30 changes: 18 additions & 12 deletions OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -2087,18 +2087,30 @@ protected
input InstNode component;
output Direction direction;
protected
Component comp;
ConnectorType.Type cty;
InnerOuter io;
Visibility vis;
Variability var;
algorithm
comp := InstNode.component(InstNode.resolveOuter(component));
// Outer components are not instantiated, so check this first to make sure
// it's safe to e.g. fetch the attributes of the component.
io := Component.innerOuter(comp);
// Function components may not be inner/outer.
if io <> InnerOuter.NOT_INNER_OUTER then
Error.addSourceMessage(Error.INNER_OUTER_FORMAL_PARAMETER,
{Prefixes.innerOuterString(io), InstNode.name(component)},
InstNode.info(InstNode.resolveOuter(component)));
fail();
end if;
Attributes.ATTRIBUTES(
connectorType = cty,
direction = direction,
innerOuter = io) := Component.getAttributes(InstNode.component(component));
vis := InstNode.visibility(component);
var := Component.variability(InstNode.component(component));
variability = var) := Component.getAttributes(comp);
// Function components may not be connectors.
if ConnectorType.isFlowOrStream(cty) then
Expand All @@ -2108,15 +2120,9 @@ protected
fail();
end if;
// Function components may not be inner/outer.
if io <> InnerOuter.NOT_INNER_OUTER then
Error.addSourceMessage(Error.INNER_OUTER_FORMAL_PARAMETER,
{Prefixes.innerOuterString(io), InstNode.name(component)},
InstNode.info(component));
fail();
end if;
// Formal parameters must be public, other function variables must be protected.
vis := InstNode.visibility(component);
if direction <> Direction.NONE then
if vis == Visibility.PROTECTED then
Error.addSourceMessage(Error.PROTECTED_FORMAL_FUNCTION_VAR,
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -1767,6 +1767,7 @@ uniontype InstNode
isRec := match node
case CLASS_NODE() then Restriction.isRecord(Class.restriction(Pointer.access(node.cls)));
case COMPONENT_NODE() then isRecord(Component.classInstance(Pointer.access(node.component)));
else false;
end match;
end isRecord;

Expand Down
28 changes: 28 additions & 0 deletions testsuite/flattening/modelica/declarations/FunctionRestriction6.mo
@@ -0,0 +1,28 @@
// name: FunctionRestriction6
// keywords:
// status: incorrect
// cflags: -d=newInst
//

function f
input Real x;
output Real y;
protected
outer Real z;
algorithm
y := x * z;
end f;

model FunctionRestriction6
Real x = f(1.0);
end FunctionRestriction6;

// Result:
// Error processing file: FunctionRestriction6.mo
// [flattening/modelica/declarations/FunctionRestriction6.mo:11:3-11:15:writable] Error: Invalid prefix outer on formal parameter z.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
//
// Execution failed!
// endResult
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/declarations/Makefile
Expand Up @@ -47,6 +47,7 @@ FlowDeclConnector.mo \
FlowDeclRecord.mo \
FlowDeclRecord2.mo \
FlowDeclType.mo \
FunctionRestriction6.mo \
InputDeclConnector.mo \
InputDeclRecord.mo \
InputDeclType.mo \
Expand Down

0 comments on commit 9cc8609

Please sign in to comment.