Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
[NF] Record fixes.
Browse files Browse the repository at this point in the history
- Fill in all field names when constructing a record expression during
  constant evaluation.
- Use correct node in Inst.makeRecordComplexType.

Belonging to [master]:
  - #2827
  • Loading branch information
perost authored and OpenModelica-Hudson committed Dec 12, 2018
1 parent d8ca873 commit 32d10a9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
6 changes: 5 additions & 1 deletion Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -595,22 +595,26 @@ protected
ClassTree tree;
array<InstNode> comps;
list<Expression> fields;
list<String> field_names;
Type ty;
InstNode c;
ComponentRef cr;
algorithm
tree := Class.classTree(InstNode.getClass(typeNode));
comps := ClassTree.getComponents(tree);
fields := {};
field_names := {};

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

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

Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -1897,7 +1897,7 @@ protected
algorithm
cls_node := if SCode.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));
fields := list(InstNode.name(c) for c in Record.collectRecordParams(cls_node));
ty := ComplexType.RECORD(cls_node, fields);
end makeRecordComplexType;

Expand Down
64 changes: 40 additions & 24 deletions Compiler/NFFrontEnd/NFRecord.mo
Expand Up @@ -101,7 +101,7 @@ algorithm
Inst.instExpressions(ctor_node);

// Collect the record fields.
(inputs, locals) := collectRecordParams(InstNode.getClass(ctor_node));
(inputs, locals) := collectRecordParams(ctor_node);

// Create the output record element, using the instance created above as both parent and type.
out_comp := Component.UNTYPED_COMPONENT(ctor_node, listArray({}),
Expand All @@ -121,37 +121,29 @@ algorithm
end instDefaultConstructor;

function collectRecordParams
input Class recClass;
input InstNode recNode;
output list<InstNode> inputs = {};
output list<InstNode> locals = {};
protected
Class cls;
array<InstNode> components;
InstNode n;
Component comp;
array<InstNode> comps;
array<Mutable<InstNode>> pcomps;
ClassTree tree;
algorithm
tree := Class.classTree(recClass);
tree := Class.classTree(InstNode.getClass(recNode));

() := match tree
case ClassTree.FLAT_TREE(components = components)
case ClassTree.FLAT_TREE(components = comps)
algorithm
for i in arrayLength(components):-1:1 loop
n := components[i];

if InstNode.isEmpty(n) then
continue;
end if;

comp := InstNode.component(n);

if InstNode.isProtected(n) or
Component.isConst(comp) and Component.hasBinding(comp) then
locals := n :: locals;
else
n := InstNode.updateComponent(Component.makeInput(comp), n);
inputs := n :: inputs;
end if;
for i in arrayLength(comps):-1:1 loop
(inputs, locals) := collectRecordParam(comps[i], inputs, locals);
end for;
then
();

case ClassTree.INSTANTIATED_TREE(components = pcomps)
algorithm
for i in arrayLength(pcomps):-1:1 loop
(inputs, locals) := collectRecordParam(Mutable.access(pcomps[i]), inputs, locals);
end for;
then
();
Expand All @@ -165,5 +157,29 @@ algorithm
end match;
end collectRecordParams;

function collectRecordParam
input InstNode component;
input output list<InstNode> inputs;
input output list<InstNode> locals;
protected
Component comp;
algorithm
if InstNode.isEmpty(component) then
return;
end if;

if InstNode.isProtected(component) then
locals := component :: locals;
end if;

comp := InstNode.component(component);

if Component.isConst(comp) and Component.hasBinding(comp) then
locals := component :: locals;
else
inputs := component :: inputs;
end if;
end collectRecordParam;

annotation(__OpenModelica_Interface="frontend");
end NFRecord;
15 changes: 15 additions & 0 deletions Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -800,6 +800,21 @@ public
end match;
end lookupRecordFieldType;

function setRecordFields
input list<String> fields;
input output Type recordType;
algorithm
recordType := match recordType
local
InstNode rec_node;

case COMPLEX(complexTy = ComplexType.RECORD(constructor = rec_node))
then COMPLEX(recordType.cls, ComplexType.RECORD(rec_node, fields));

else recordType;
end match;
end setRecordFields;

function enumName
input Type ty;
output Absyn.Path name;
Expand Down

0 comments on commit 32d10a9

Please sign in to comment.