Skip to content

Commit

Permalink
[NF] Improve handling of consts/params in records.
Browse files Browse the repository at this point in the history
- Include binding expressions from non-inputs when evaluating record
  calls.
- Change the list of field names in ComplexType.RECORD into a list of
  Record.Field:s, to make it possible to keep track of which fields are
  inputs or not.
  • Loading branch information
perost committed Mar 9, 2020
1 parent 46dda53 commit 9cab1f5
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 32 deletions.
3 changes: 0 additions & 3 deletions .CI/compliance-newinst.failures
Expand Up @@ -110,9 +110,6 @@ ModelicaCompliance.Functions.HigherOrder.PartialApplication2
ModelicaCompliance.Functions.HigherOrder.PartialApplication3
ModelicaCompliance.Functions.HigherOrder.Quadrature1
ModelicaCompliance.Functions.HigherOrder.Quadrature2
ModelicaCompliance.Functions.Records.RecordConstructorConstantModifiable
ModelicaCompliance.Functions.Records.RecordConstructorDefaultValue
ModelicaCompliance.Functions.Records.RecordConstructorDefaultValueDependent
ModelicaCompliance.Functions.Restrictions.FunctionAssignInput
ModelicaCompliance.Inheritance.Flattening.DuplicateInheritedNeqClasses
ModelicaCompliance.Inheritance.Restrictions.ArrayClassWithComp
Expand Down
34 changes: 32 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -38,6 +38,7 @@ import Expression = NFExpression;
import NFInstNode.InstNode;
import NFPrefixes.Variability;
import Type = NFType;
import Record = NFRecord;

protected
import BuiltinCall = NFBuiltinCall;
Expand Down Expand Up @@ -582,8 +583,37 @@ uniontype Call
output Expression exp;
algorithm
exp := match call
case TYPED_CALL()
then Expression.RECORD(Function.name(call.fn), ty, call.arguments);
local
Expression arg;
list<Expression> call_args, local_args, args;
list<Record.Field> fields;

case TYPED_CALL(arguments = call_args)
algorithm
local_args := Function.getLocalArguments(call.fn);
fields := Type.recordFields(ty);
args := {};

for field in fields loop
if Record.Field.isInput(field) then
arg :: call_args := call_args;
else
arg :: local_args := local_args;
end if;

args := arg :: args;
end for;

args := listReverseInPlace(args);
then
Expression.RECORD(Function.name(call.fn), ty, args);

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown call", sourceInfo());
then
fail();

end match;
end toRecordExpression;

Expand Down
22 changes: 11 additions & 11 deletions OMCompiler/Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -51,6 +51,7 @@ import ComplexType = NFComplexType;
import Subscript = NFSubscript;
import NFTyping.TypingError;
import DAE;
import Record = NFRecord;

protected
import NFFunction.Function;
Expand Down Expand Up @@ -701,34 +702,33 @@ function makeRecordBindingExp
protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
list<String> field_names;
list<Expression> args;
list<Record.Field> fields;
Type ty;
InstNode c;
ComponentRef cr;
Expression field_exp;
Expression arg;
algorithm
tree := Class.classTree(InstNode.getClass(typeNode));
comps := ClassTree.getComponents(tree);
fields := {};
field_names := {};
args := {};

for i in arrayLength(comps):-1:1 loop
c := comps[i];
ty := InstNode.getType(c);
cr := ComponentRef.CREF(c, {}, ty, NFComponentRef.Origin.CREF, cref);
field_exp := Expression.CREF(ty, cr);
arg := Expression.CREF(ty, cr);

if Component.variability(InstNode.component(c)) <= Variability.PARAMETER then
field_exp := evalExp_impl(field_exp, EvalTarget.IGNORE_ERRORS());
arg := evalExp_impl(arg, EvalTarget.IGNORE_ERRORS());
end if;

fields := field_exp :: fields;
field_names := InstNode.name(c) :: field_names;
args := arg :: args;
end for;

ty := Type.setRecordFields(field_names, recordType);
exp := Expression.RECORD(InstNode.scopePath(recordNode), ty, fields);
fields := Record.collectRecordFields(typeNode);
ty := Type.setRecordFields(fields, recordType);
exp := Expression.RECORD(InstNode.scopePath(recordNode), ty, args);
end makeRecordBindingExp;

function splitRecordArrayExp
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFComplexType.mo
Expand Up @@ -31,6 +31,7 @@

encapsulated uniontype NFComplexType
import NFInstNode.InstNode;
import Record = NFRecord;

protected
import ComplexType = NFComplexType;
Expand Down Expand Up @@ -59,7 +60,7 @@ public

record RECORD
InstNode constructor;
list<String> fieldNames;
list<Record.Field> fields;
end RECORD;

record EXTERNAL_OBJECT
Expand Down
73 changes: 67 additions & 6 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -69,6 +69,7 @@ public
import NFComponentRef.Origin;
import NFTyping.ExpOrigin;
import Values;
import Record = NFRecord;

uniontype ClockKind
record INFERRED_CLOCK
Expand Down Expand Up @@ -1725,8 +1726,7 @@ public
then DAE.ARRAY(Type.toDAE(exp.ty), Type.isScalarArray(exp.ty),
list(toDAE(e) for e in exp.elements));

case RECORD(ty = Type.COMPLEX(complexTy = ComplexType.RECORD(fieldNames = names)))
then DAE.RECORD(exp.path, list(toDAE(e) for e in exp.elements), names, Type.toDAE(exp.ty));
case RECORD() then toDAERecord(exp.ty, exp.path, exp.elements);

case RANGE()
then DAE.RANGE(
Expand Down Expand Up @@ -1807,6 +1807,37 @@ public
end match;
end toDAE;

function toDAERecord
input Type ty;
input Absyn.Path path;
input list<Expression> args;
output DAE.Exp exp;
protected
list<String> field_names = {};
Expression arg;
list<Expression> rest_args = args;
list<DAE.Exp> dargs = {};
algorithm
for field in Type.recordFields(ty) loop
arg :: rest_args := rest_args;

() := match field
case Record.Field.INPUT()
algorithm
field_names := field.name :: field_names;
dargs := toDAE(arg) :: dargs;
then
();

else ();
end match;
end for;

field_names := listReverseInPlace(field_names);
dargs := listReverseInPlace(dargs);
exp := DAE.RECORD(path, dargs, field_names, Type.toDAE(ty));
end toDAERecord;

function toDAEValueOpt
input Option<Expression> exp;
output Option<Values.Value> value = Util.applyOption(exp, toDAEValue);
Expand All @@ -1820,7 +1851,8 @@ public
local
Type ty;
list<Values.Value> vals;
list<String> fields;
list<Record.Field> fields;
list<String> field_names;

case INTEGER() then Values.INTEGER(exp.value);
case REAL() then Values.REAL(exp.value);
Expand All @@ -1835,8 +1867,7 @@ public
then
ValuesUtil.makeArray(vals);

case RECORD(ty = Type.COMPLEX(complexTy = ComplexType.RECORD(fieldNames = fields)))
then Values.RECORD(exp.path, list(toDAEValue(e) for e in exp.elements), fields, -1);
case RECORD() then toDAEValueRecord(exp.ty, exp.path, exp.elements);

else
algorithm
Expand All @@ -1846,6 +1877,37 @@ public
end match;
end toDAEValue;

function toDAEValueRecord
input Type ty;
input Absyn.Path path;
input list<Expression> args;
output Values.Value value;
protected
list<String> field_names = {};
Expression arg;
list<Expression> rest_args = args;
list<Values.Value> values = {};
algorithm
for field in Type.recordFields(ty) loop
arg :: rest_args := rest_args;

() := match field
case Record.Field.INPUT()
algorithm
field_names := field.name :: field_names;
values := toDAEValue(arg) :: values;
then
();

else ();
end match;
end for;

field_names := listReverseInPlace(field_names);
values := listReverseInPlace(values);
value := Values.RECORD(path, values, field_names, -1);
end toDAEValueRecord;

function dimensionCount
input Expression exp;
output Integer dimCount;
Expand Down Expand Up @@ -5079,7 +5141,6 @@ public
end match;
end nthRecordElement;


function splitRecordCref
input Expression exp;
output Expression outExp;
Expand Down
11 changes: 6 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -193,7 +193,7 @@ algorithm

if Binding.isBound(b) then
b := flattenBinding(b, ComponentRef.rest(prefix));
bindings := getRecordBindings(b);
bindings := getRecordBindings(b, comps);

Error.assertion(listLength(bindings) == arrayLength(comps),
getInstanceName() + " got record binding with wrong number of elements for " +
Expand Down Expand Up @@ -475,18 +475,19 @@ end isTypeAttributeNamed;

function getRecordBindings
input Binding binding;
output list<Binding> recordBindings;
input array<InstNode> comps;
output list<Binding> recordBindings = {};
protected
Expression binding_exp;
list<Expression> expl;
Variability var;
algorithm
binding_exp := Binding.getTypedExp(binding);
var := Binding.variability(binding);

// Convert the expressions in the record expression into bindings.
recordBindings := match binding_exp
case Expression.RECORD() then
list(if Expression.isEmpty(e) then
case Expression.RECORD()
then list(if Expression.isEmpty(e) then
// The binding for a record field might be Expression.EMPTY if it comes
// from an evaluated function call where it wasn't assigned a value.
NFBinding.EMPTY_BINDING
Expand Down
18 changes: 18 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -1771,6 +1771,24 @@ uniontype Function
output Boolean isPartial = InstNode.isPartial(fn.node);
end isPartial;

function getLocalArguments
input Function fn;
output list<Expression> localArgs = {};
protected
Binding binding;
algorithm
for l in fn.locals loop
if InstNode.isComponent(l) then
binding := Component.getBinding(InstNode.component(l));
Error.assertion(Binding.hasExp(binding),
getInstanceName() + " got local component without binding", sourceInfo());
localArgs := Binding.getExp(binding) :: localArgs;
end if;
end for;

localArgs := listReverseInPlace(localArgs);
end getLocalArguments;

protected
function collectParams
"Sorts all the function parameters as inputs, outputs and locals."
Expand Down
5 changes: 2 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -2212,12 +2212,11 @@ function makeRecordComplexType
output ComplexType ty;
protected
InstNode cls_node;
list<String> inputs;
list<String> fields;
list<Record.Field> fields;
algorithm
cls_node := if SCodeUtil.isOperatorRecord(InstNode.definition(node))
then InstNode.classScope(node) else InstNode.classScope(InstNode.getDerivedNode(node));
fields := list(InstNode.name(c) for c in Record.collectRecordParams(cls_node));
fields := Record.collectRecordFields(cls_node);
ty := ComplexType.RECORD(cls_node, fields);
end makeRecordComplexType;

Expand Down

0 comments on commit 9cab1f5

Please sign in to comment.