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

Commit 1a8d982

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix generation of record constructors.
- Generate record constructors from new instances of the records, so that they don't share child instances with the record instances that triggered the generation. Belonging to [master]: - #2439
1 parent 2fefbbb commit 1a8d982

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Compiler/NFFrontEnd/NFRecord.mo

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,30 @@ protected
125125
InstNode ctor_node, out_rec;
126126
Component out_comp;
127127
Class ctor_cls;
128+
InstNode ty_node;
128129
algorithm
129-
(inputs, locals) := collectRecordParams(node);
130-
attr := DAE.FUNCTION_ATTRIBUTES_DEFAULT;
131-
collected := Pointer.create(false);
130+
// The node we get is usually a record instance, with applied modifiers and so on.
131+
// So the first thing we do is to create a "pure" instance of the record.
132+
node := Lookup.lookupLocalSimpleName(InstNode.name(node), InstNode.parentScope(node));
133+
node := Inst.instantiate(node);
134+
Inst.instExpressions(node);
132135

133-
// Create a new node for the default constructor.
134-
ctor_node := InstNode.replaceClass(Class.NOT_INSTANTIATED(), node);
136+
// Collect the record fields.
137+
(inputs, locals) := collectRecordParams(node);
135138

136-
// Create the output record element, using the node created above as parent.
137-
out_comp := Component.UNTYPED_COMPONENT(node, listArray({}), Binding.UNBOUND(NONE()),
138-
Binding.UNBOUND(NONE()), NFComponent.OUTPUT_ATTR, NONE(), Absyn.dummyInfo);
139-
out_rec := InstNode.fromComponent("$out" + InstNode.name(node), out_comp, ctor_node);
139+
// Create the output record element, using the instance created above as both parent and type.
140+
out_comp := Component.TYPED_COMPONENT(node, Type.COMPLEX(node, ComplexType.RECORD(node)),
141+
Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()),
142+
NFComponent.OUTPUT_ATTR, NONE(), Absyn.dummyInfo);
143+
out_rec := InstNode.fromComponent("$out" + InstNode.name(node), out_comp, node);
140144

141-
// Make a record constructor class and update the node with it.
145+
// Make a record constructor class and create a node for the constructor.
142146
ctor_cls := Class.makeRecordConstructor(inputs, locals, out_rec);
143-
ctor_node := InstNode.updateClass(ctor_cls, ctor_node);
147+
ctor_node := InstNode.replaceClass(ctor_cls, node);
144148

145-
// Add the constructor to the function cache.
149+
// Create the constructor function and add it to the function cache.
150+
attr := DAE.FUNCTION_ATTRIBUTES_DEFAULT;
151+
collected := Pointer.create(false);
146152
InstNode.cacheAddFunc(node, Function.FUNCTION(path, ctor_node, inputs,
147153
{out_rec}, locals, {}, Type.UNKNOWN(), attr, collected, Pointer.create(0)), false);
148154
end instDefaultConstructor;
@@ -177,7 +183,7 @@ algorithm
177183
Component.isConst(comp) and Component.hasBinding(comp) then
178184
locals := n :: locals;
179185
else
180-
n := InstNode.replaceComponent(Component.makeInput(comp), n);
186+
n := InstNode.updateComponent(Component.makeInput(comp), n);
181187
inputs := n :: inputs;
182188
end if;
183189
end for;

0 commit comments

Comments
 (0)