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

Commit fb72969

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix record constructor DAE issue.
- Create a real record constructor node, instead of using the record class itself when creating a record constructor function. Belonging to [master]: - #2304
1 parent cd42dc7 commit fb72969

File tree

5 files changed

+108
-37
lines changed

5 files changed

+108
-37
lines changed

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ uniontype Class
148148
Restriction.ENUMERATION());
149149
end fromEnumeration;
150150

151+
function makeRecordConstructor
152+
input list<InstNode> inputs;
153+
input list<InstNode> locals;
154+
input InstNode out;
155+
output Class cls;
156+
protected
157+
ClassTree tree;
158+
algorithm
159+
tree := ClassTree.fromRecordConstructor(inputs, locals, out);
160+
cls := EXPANDED_CLASS(tree, Modifier.NOMOD(), DEFAULT_PREFIXES, Restriction.FUNCTION());
161+
end makeRecordConstructor;
162+
151163
function initExpandedClass
152164
input output Class cls;
153165
algorithm

Compiler/NFFrontEnd/NFClassTree.mo

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,38 @@ public
615615
InstNode.updateClass(cls, clsNode);
616616
end instantiate;
617617

618+
function fromRecordConstructor
619+
input list<InstNode> inputs;
620+
input list<InstNode> locals;
621+
input InstNode out;
622+
output ClassTree tree = EMPTY;
623+
protected
624+
LookupTree.Tree ltree = LookupTree.new();
625+
Integer i = 1;
626+
array<Mutable<InstNode>> comps;
627+
algorithm
628+
comps := arrayCreateNoInit(listLength(inputs) + listLength(locals) + 1,
629+
Mutable.create(InstNode.EMPTY_NODE()));
630+
631+
for ci in inputs loop
632+
comps[i] := Mutable.create(ci);
633+
ltree := addLocalElement(InstNode.name(ci), LookupTree.Entry.COMPONENT(i), tree, ltree);
634+
i := i + 1;
635+
end for;
636+
637+
for cl in locals loop
638+
comps[i] := Mutable.create(cl);
639+
ltree := addLocalElement(InstNode.name(cl), LookupTree.Entry.COMPONENT(i), tree, ltree);
640+
i := i + 1;
641+
end for;
642+
643+
comps[i] := Mutable.create(out);
644+
ltree := addLocalElement(InstNode.name(out), LookupTree.Entry.COMPONENT(i), tree, ltree);
645+
646+
tree := INSTANTIATED_TREE(ltree, listArray({}), comps, List.intRange(i),
647+
listArray({}), listArray({}), DuplicateTree.new());
648+
end fromRecordConstructor;
649+
618650
function mapRedeclareChains
619651
input ClassTree tree;
620652
input FuncT func;

Compiler/NFFrontEnd/NFComponent.mo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,30 @@ uniontype Component
463463
output Boolean isInput = direction(component) == Direction.INPUT;
464464
end isInput;
465465

466+
function makeInput
467+
input output Component component;
468+
protected
469+
Attributes attr;
470+
algorithm
471+
() := match component
472+
case UNTYPED_COMPONENT(attributes = attr)
473+
algorithm
474+
attr.direction := Direction.INPUT;
475+
component.attributes := attr;
476+
then
477+
();
478+
479+
case TYPED_COMPONENT(attributes = attr)
480+
algorithm
481+
attr.direction := Direction.INPUT;
482+
component.attributes := attr;
483+
then
484+
();
485+
486+
else ();
487+
end match;
488+
end makeInput;
489+
466490
function isOutput
467491
input Component component;
468492
output Boolean isOutput = direction(component) == Direction.OUTPUT;

Compiler/NFFrontEnd/NFRecord.mo

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import NFInstUtil;
6060
import NFPrefixes.Variability;
6161
import NFPrefixes.Visibility;
6262
import NFFunction.Function;
63-
import ClassTree = NFClassTree.ClassTree;
63+
import NFClassTree.ClassTree;
6464
import ComplexType = NFComplexType;
6565
import ComponentRef = NFComponentRef;
6666
import ErrorExt;
@@ -71,16 +71,18 @@ function instConstructors
7171
input Absyn.Path path;
7272
input output InstNode node;
7373
input SourceInfo info;
74-
protected
75-
Class cls;
76-
list<InstNode> inputs, outputs, locals;
77-
InstNode out_rec, ctor_over;
78-
DAE.FunctionAttributes attr;
79-
Pointer<Boolean> collected;
80-
Absyn.Path con_path;
74+
protected
75+
Class cls;
76+
list<InstNode> inputs, outputs, locals, params;
77+
InstNode out_rec, ctor_over, def_ctor_node;
78+
DAE.FunctionAttributes attr;
79+
Pointer<Boolean> collected;
80+
Absyn.Path con_path;
8181
ComponentRef con_ref;
8282
Boolean ctor_defined;
83-
algorithm
83+
Component out_comp;
84+
Class def_ctor_cls;
85+
algorithm
8486

8587
// See if we have overloaded costructors.
8688
try
@@ -122,25 +124,19 @@ function instConstructors
122124
collected := Pointer.create(false);
123125
con_path := Absyn.suffixPath(path,"'constructor'.'$default'");
124126

125-
// Create an output record element for the default constructor.
126-
// create a TYPED_COMPONENT here since this output "default constructed" record is not part
127-
// of the class node. So it will not be typed later by typeFunction. Instead of changing
128-
// things there just handle it here.
129-
out_rec := InstNode.COMPONENT_NODE("$out" + InstNode.name(node),
130-
Visibility.PUBLIC,
131-
Pointer.createImmutable(Component.TYPED_COMPONENT(
132-
node,
133-
Type.COMPLEX(node, ComplexType.CLASS()),
134-
Binding.UNBOUND(),
135-
Binding.UNBOUND(),
136-
NFComponent.OUTPUT_ATTR,
137-
NONE(),
138-
Absyn.dummyInfo)),
139-
0,
140-
node);
141-
142-
InstNode.cacheAddFunc(node, Function.FUNCTION(con_path, node, inputs, {out_rec}, locals, {}, Type.UNKNOWN(), attr, collected), false);
127+
// Create a new node for the default constructor.
128+
def_ctor_node := InstNode.replaceClass(Class.NOT_INSTANTIATED(), node);
129+
130+
// Create the output record element, using the node created above as parent.
131+
out_comp := Component.UNTYPED_COMPONENT(node, listArray({}), Binding.UNBOUND(),
132+
Binding.UNBOUND(), NFComponent.OUTPUT_ATTR, NONE(), Absyn.dummyInfo);
133+
out_rec := InstNode.fromComponent("$out" + InstNode.name(node), out_comp, def_ctor_node);
134+
135+
// Make a record constructor class and update the node with it.
136+
def_ctor_cls := Class.makeRecordConstructor(inputs, locals, out_rec);
137+
def_ctor_node := InstNode.updateClass(def_ctor_cls, def_ctor_node);
143138

139+
InstNode.cacheAddFunc(node, Function.FUNCTION(con_path, def_ctor_node, inputs, {out_rec}, locals, {}, Type.UNKNOWN(), attr, collected), false);
144140
end instConstructors;
145141

146142

@@ -166,18 +162,12 @@ algorithm
166162
n := Mutable.access(components[i]);
167163
comp := InstNode.component(n);
168164

169-
if InstNode.isProtected(n) then
165+
if InstNode.isProtected(n) or
166+
Component.isConst(comp) and Component.hasBinding(comp) then
170167
locals := n :: locals;
171168
else
172-
if Component.isConst(comp) then
173-
if not Component.hasBinding(comp) then
174-
inputs := n :: inputs;
175-
else
176-
locals := n :: locals;
177-
end if;
178-
else
179-
inputs := n :: inputs;
180-
end if;
169+
n := InstNode.replaceComponent(Component.makeInput(comp), n);
170+
inputs := n :: inputs;
181171
end if;
182172
end for;
183173
then

Compiler/Util/Array.mo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,5 +913,18 @@ algorithm
913913
exists := false;
914914
end exist;
915915

916+
function insertList<T>
917+
input output array<T> arr;
918+
input list<T> lst;
919+
input Integer startPos;
920+
protected
921+
Integer i = startPos;
922+
algorithm
923+
for e in lst loop
924+
arr[i] := e;
925+
i := i + 1;
926+
end for;
927+
end insertList;
928+
916929
annotation(__OpenModelica_Interface="util");
917930
end Array;

0 commit comments

Comments
 (0)