Skip to content

Commit

Permalink
Refactor typing of builtin Connections.* functions (#8529)
Browse files Browse the repository at this point in the history
- Add functions to type and check arguments of the builtin Connections.*
  functions, to reduce code repetition.
- Add toString debug function to NFConnections.
  • Loading branch information
perost committed Feb 10, 2022
1 parent cea85cf commit 803a3c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
73 changes: 42 additions & 31 deletions OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo
Expand Up @@ -1330,6 +1330,36 @@ protected
System.setUsesCardinality(true);
end typeCardinalityCall;

function typeConnectionsArgs
input list<Expression> args;
input InstContext.Type context;
input SourceInfo info;
input ComponentRef fnRef;
output list<Expression> outArgs = {};
protected
Integer index = 1;
algorithm
for arg in args loop
outArgs := typeConnectionsArg(arg, context, info, fnRef, index) :: outArgs;
index := index + 1;
end for;

outArgs := listReverseInPlace(outArgs);
end typeConnectionsArgs;

function typeConnectionsArg
input Expression arg;
input InstContext.Type context;
input SourceInfo info;
input ComponentRef fnRef;
input Integer index;
output Expression outArg;
output Type outType;
algorithm
(outArg, outType) := Typing.typeExp(arg, context, info);
checkConnectionsArgument(outArg, outType, fnRef, index, info);
end typeConnectionsArg;

function typeBranchCall
input Call call;
input InstContext.Type context;
Expand All @@ -1342,7 +1372,6 @@ protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg1, arg2;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
Expand All @@ -1358,16 +1387,10 @@ protected
{ComponentRef.toString(fn_ref)}, info);
end if;

{arg1, arg2} := args;

(arg1, ty) := Typing.typeExp(arg1, context, info);
checkConnectionsArgument(arg1, ty, fn_ref, 1, info);
(arg2, ty) := Typing.typeExp(arg2, context, info);
checkConnectionsArgument(arg2, ty, fn_ref, 2, info);

args := typeConnectionsArgs(args, context, info, fn_ref);
{fn} := Function.typeRefCache(fn_ref);
ty := Type.NORETCALL();
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg1, arg2}, var, purity, ty));
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, purity, ty));
end typeBranchCall;

function typeIsRootCall
Expand All @@ -1382,7 +1405,6 @@ protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
Expand All @@ -1398,12 +1420,10 @@ protected
{ComponentRef.toString(fn_ref)}, info);
end if;

(arg, ty) := Typing.typeExp(listHead(args), context, info);
checkConnectionsArgument(arg, ty, fn_ref, 1, info);

args := typeConnectionsArgs(args, context, info, fn_ref);
{fn} := Function.typeRefCache(fn_ref);
ty := Type.BOOLEAN();
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, var, purity, ty));
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, purity, ty));
end typeIsRootCall;

function typePotentialRootCall
Expand Down Expand Up @@ -1449,8 +1469,7 @@ protected

arg1 :: args := args;

(arg1, ty) := Typing.typeExp(arg1, context, info);
checkConnectionsArgument(arg1, ty, fn_ref, 1, info);
arg1 := typeConnectionsArg(arg1, context, info, fn_ref, 1);

if args_len == 2 then
arg2 := listHead(args);
Expand Down Expand Up @@ -1482,7 +1501,6 @@ protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
Expand All @@ -1498,12 +1516,10 @@ protected
{ComponentRef.toString(fn_ref)}, info);
end if;

(arg, ty) := Typing.typeExp(listHead(args), context, info);
checkConnectionsArgument(arg, ty, fn_ref, 1, info);

args := typeConnectionsArgs(args, context, info, fn_ref);
{fn} := Function.typeRefCache(fn_ref);
ty := Type.NORETCALL();
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, var, purity, ty));
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, purity, ty));
end typeRootCall;

function typeRootedCall
Expand All @@ -1518,7 +1534,6 @@ protected
ComponentRef fn_ref;
list<Expression> args;
list<NamedArg> named_args;
Expression arg;
Function fn;
algorithm
Call.UNTYPED_CALL(ref = fn_ref, arguments = args, named_args = named_args) := call;
Expand All @@ -1534,16 +1549,15 @@ protected
{ComponentRef.toString(fn_ref)}, info);
end if;

(arg, ty) := Typing.typeExp(listHead(args), context, info);
checkConnectionsArgument(arg, ty, fn_ref, 1, info);
args := typeConnectionsArgs(args, context, info, fn_ref);

if ComponentRef.isSimple(fn_ref) then
Error.addSourceMessage(Error.DEPRECATED_API_CALL, {"rooted", "Connections.rooted"}, info);
end if;

{fn} := Function.typeRefCache(fn_ref);
ty := Type.BOOLEAN();
callExp := Expression.CALL(Call.makeTypedCall(fn, {arg}, var, purity, ty));
callExp := Expression.CALL(Call.makeTypedCall(fn, args, var, purity, ty));
end typeRootedCall;

function typeUniqueRootCall
Expand Down Expand Up @@ -1592,8 +1606,7 @@ protected

arg1 :: args := args;

(arg1, ty) := Typing.typeExp(arg1, context, info);
checkConnectionsArgument(arg1, ty, fn_ref, 1, info);
arg1 := typeConnectionsArg(arg1, context, info, fn_ref, 1);

if args_len == 2 then
arg2 := listHead(args);
Expand Down Expand Up @@ -1666,10 +1679,8 @@ protected

arg1 :: arg2 :: args := args;

(arg1, ty1) := Typing.typeExp(arg1, context, info);
checkConnectionsArgument(arg1, ty1, fn_ref, 1, info);
(arg2, ty2) := Typing.typeExp(arg2, context, info);
checkConnectionsArgument(arg2, ty2, fn_ref, 1, info);
(arg1, ty1) := typeConnectionsArg(arg1, context, info, fn_ref, 1);
(arg2, ty2) := typeConnectionsArg(arg2, context, info, fn_ref, 2);

if args_len == 3 then
arg3 := listHead(args);
Expand Down
19 changes: 19 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFConnections.mo
Expand Up @@ -202,6 +202,25 @@ public
end if;
end makeConnectors;

function toString
input Connections conns;
output String str;
protected
list<String> strl = {};
algorithm
strl := "FLOWS:" :: strl;
for f in conns.flows loop
strl := Connector.toString(f) :: strl;
end for;

strl := "\nCONNECTIONS:" :: strl;
for c in conns.connections loop
strl := Connection.toString(c) :: strl;
end for;

strl := listReverseInPlace(strl);
str := stringDelimitList(strl, "\n");
end toString;

annotation(__OpenModelica_Interface="frontend");
end NFConnections;

0 comments on commit 803a3c0

Please sign in to comment.