Skip to content

Commit da294f8

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Ported state machine for checking lookup restrictions from old new inst to new inst. - Many other lookup improvements. - Fixed cloning of instances so that modifiers don't stick to classes. - Other minor instantiation fixes.
1 parent 974e2ad commit da294f8

File tree

10 files changed

+808
-146
lines changed

10 files changed

+808
-146
lines changed

Compiler/FrontEnd/Absyn.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4318,6 +4318,16 @@ algorithm
43184318
end match;
43194319
end crefFirstIdent;
43204320

4321+
public function crefSecondIdent
4322+
input ComponentRef cref;
4323+
output Ident ident;
4324+
algorithm
4325+
ident := match cref
4326+
case CREF_QUAL() then crefFirstIdent(cref.componentRef);
4327+
case CREF_FULLYQUALIFIED() then crefSecondIdent(cref.componentRef);
4328+
end match;
4329+
end crefSecondIdent;
4330+
43214331
public function crefFirstCref
43224332
"Returns the first part of a cref."
43234333
input ComponentRef inCref;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ algorithm
144144

145145
else
146146
algorithm
147-
assert(true, "flattenComponent got unknown component");
147+
assert(false, "flattenComponent got unknown component");
148148
then
149149
fail();
150150

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ algorithm
9898

9999
// Update the instantiated class inside the top component.
100100
top_comp := Component.setClassInstance(cls, top_comp);
101-
top_comp_node := ComponentNode.replaceComponent(top_comp, top_comp_node);
101+
// Then update the component inside the top component node, which makes sure
102+
// that this change is propagated to any component which uses this node as
103+
// their parent (since the component in the node is inside an array).
104+
top_comp_node := ComponentNode.updateComponent(top_comp, top_comp_node);
102105

103106
cls := Typing.typeClass(cls, top_comp_node);
104107
dae := NFFlatten.flattenClass(cls);
@@ -362,9 +365,9 @@ algorithm
362365
// Add component ID:s to the scope.
363366
idx := 1;
364367
for c in components loop
365-
// // TODO: Handle components with the same name.
366-
// // TODO: If duplicate components should be handled here, also
367-
// // remove them from the list of components.
368+
// TODO: Handle components with the same name.
369+
// TODO: If duplicate components should be handled here, also
370+
// remove them from the list of components.
368371
scope := ClassTree.add(scope, ComponentNode.name(c),
369372
ClassTree.Entry.COMPONENT(idx), ClassTree.addConflictReplace);
370373

@@ -384,7 +387,7 @@ algorithm
384387

385388
else
386389
algorithm
387-
Error.addInternalError("NFInst.expandClass2 got unknown class.", Absyn.dummyInfo);
390+
assert(false, getInstanceName() + " got unknown class");
388391
then
389392
fail();
390393

@@ -464,7 +467,7 @@ algorithm
464467

465468
else
466469
algorithm
467-
Error.addInternalError("NFInst.expandExtends got unknown element!", Absyn.dummyInfo);
470+
assert(false, getInstanceName() + " got unknown element");
468471
then
469472
fail();
470473

@@ -585,6 +588,8 @@ algorithm
585588

586589
end match;
587590
end for;
591+
592+
instance.elements := elements;
588593
then
589594
();
590595

@@ -618,8 +623,7 @@ algorithm
618623
// Any other component shouldn't show up here.
619624
else
620625
algorithm
621-
Error.addInternalError("NFInst.addInheritedComponentRefs got unknown component.",
622-
ComponentNode.info(cn));
626+
assert(false, getInstanceName() + " got unknown component.");
623627
then
624628
fail();
625629
end match;
@@ -659,32 +663,30 @@ function instClass
659663
input Modifier modifier;
660664
input ComponentNode parent;
661665
protected
662-
Instance i, i_mod;
666+
Instance i;
663667
array<ComponentNode> components;
664-
ClassTree.Tree scope;
665668
Modifier type_mod, mod;
666669
list<Modifier> type_mods, inst_type_mods;
667670
Binding binding;
668671
InstNode n, cur_scope;
669672
list<ComponentNode> ext_nodes;
670-
Component c;
673+
String name;
671674
algorithm
672-
i := InstNode.instance(node);
673-
674-
() := match i
675+
() := match InstNode.instance(node)
675676
// A normal class.
676-
case Instance.EXPANDED_CLASS(elements = scope)
677+
case Instance.EXPANDED_CLASS()
677678
algorithm
678679
// Clone the instance node, since each component needs a unique instance.
679680
node := InstNode.clone(node);
681+
i := InstNode.instance(node);
682+
Instance.EXPANDED_CLASS(modifier = mod, extendsNodes = ext_nodes) := i;
680683

681684
// Apply the modifier to the instance.
682-
mod := Modifier.merge(modifier, i.modifier);
683-
i_mod := applyModifier(modifier, i,
685+
mod := Modifier.merge(modifier, mod);
686+
i := applyModifier(modifier, i,
684687
ModifierScope.CLASS_SCOPE(InstNode.name(node)));
685688

686689
// Instantiate all the extends nodes first.
687-
Instance.EXPANDED_CLASS(extendsNodes = ext_nodes) := i_mod;
688690
for ext in ext_nodes loop
689691
Component.EXTENDS_NODE(node = n) := ComponentNode.component(ext);
690692
// No modifier, the modifier on the extends clause has already been
@@ -694,25 +696,28 @@ algorithm
694696

695697
// Instantiate all local components. This will skip inherited
696698
// components, since those have already been instantiated.
697-
components := Array.map(Instance.components(i_mod),
699+
components := Array.map(Instance.components(i),
698700
function instComponent(parent = parent, scope = node));
699701

700702
// Update the instance node with the new instance.
701-
i_mod := Instance.INSTANCED_CLASS(scope, components,
702-
i.equations, i.initialEquations, i.algorithms, i.initialAlgorithms);
703-
node := InstNode.setInstance(i_mod, node);
703+
i := Instance.instExpandedClass(components, i);
704+
node := InstNode.setInstance(i, node);
704705
then
705706
();
706707

707708
// A builtin type.
708709
case Instance.PARTIAL_BUILTIN()
709710
algorithm
711+
// Clone the instance node, since each component needs a unique instance.
710712
node := InstNode.clone(node);
711-
inst_type_mods := {};
713+
i := InstNode.instance(node);
714+
Instance.PARTIAL_BUILTIN(name = name, modifier = mod) := i;
715+
712716
// Merge any outer modifiers on the class with the class' own modifier.
713-
type_mod := Modifier.merge(modifier, i.modifier);
717+
type_mod := Modifier.merge(modifier, mod);
714718

715719
// If the modifier isn't empty, instantiate it.
720+
inst_type_mods := {};
716721
if not Modifier.isEmpty(type_mod) then
717722
type_mods := Modifier.toList(type_mod);
718723
cur_scope := InstNode.parentScope(node);
@@ -734,8 +739,8 @@ algorithm
734739
end for;
735740
end if;
736741

737-
i_mod := Instance.INSTANCED_BUILTIN(i.name, inst_type_mods);
738-
node := InstNode.setInstance(i_mod, node);
742+
i := Instance.INSTANCED_BUILTIN(name, inst_type_mods);
743+
node := InstNode.setInstance(i, node);
739744
then
740745
();
741746

@@ -772,7 +777,6 @@ algorithm
772777
case Component.COMPONENT_DEF(definition = comp as SCode.COMPONENT())
773778
algorithm
774779
name := ComponentNode.name(node);
775-
node := ComponentNode.clone(node);
776780
node := ComponentNode.setOrphanParent(parent, node);
777781

778782
// Merge the modifier from the component.
@@ -1140,8 +1144,7 @@ algorithm
11401144

11411145
else
11421146
algorithm
1143-
Error.addInternalError("NFInst.instEEquation: Unknown equation",
1144-
SCode.getEEquationInfo(scodeEq));
1147+
assert(false, getInstanceName() + " got unknown equation");
11451148
then
11461149
fail();
11471150

@@ -1269,8 +1272,7 @@ algorithm
12691272

12701273
else
12711274
algorithm
1272-
Error.addInternalError("NFInst.instStatement: Unknown statement",
1273-
SCode.getStatementInfo(scodeStmt));
1275+
assert(false, getInstanceName() + " got unknown statement");
12741276
then
12751277
fail();
12761278

Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ uniontype InstNode
228228

229229
case INST_NODE()
230230
algorithm
231-
//i := arrayCreate(1, Instance.clone(node.instance[1]));
232-
i := arrayCopy(node.instance);
231+
i := arrayCreate(1, Instance.clone(node.instance[1]));
232+
//i := arrayCopy(node.instance);
233233
then
234234
INST_NODE(node.name, node.definition, i, node.parentScope, node.nodeType);
235235

Compiler/NFFrontEnd/NFInstance.mo

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ uniontype Instance
134134
instance := EXPANDED_CLASS(classes, {}, listArray({}), Modifier.NOMOD(), {}, {}, {}, {});
135135
end initExpandedClass;
136136

137+
function instExpandedClass
138+
input array<ComponentNode> components;
139+
input Instance expandedClass;
140+
output Instance instancedClass;
141+
algorithm
142+
instancedClass := match expandedClass
143+
case EXPANDED_CLASS()
144+
then INSTANCED_CLASS(expandedClass.elements, components,
145+
expandedClass.equations, expandedClass.initialEquations,
146+
expandedClass.algorithms, expandedClass.initialAlgorithms);
147+
end match;
148+
end instExpandedClass;
149+
137150
function components
138151
input Instance instance;
139152
output array<ComponentNode> components;
@@ -288,8 +301,7 @@ uniontype Instance
288301

289302
else
290303
algorithm
291-
Error.addInternalError("NFInstance.setModifier got unmodifiable instance!\n",
292-
Absyn.dummyInfo);
304+
assert(false, getInstanceName() + " got unmodifiable instance");
293305
then
294306
fail();
295307

@@ -315,23 +327,23 @@ uniontype Instance
315327
local
316328
ClassTree.Tree tree;
317329

318-
case PARTIAL_CLASS()
319-
algorithm
320-
instance.classes := ClassTree.map(instance.classes, cloneEntry);
321-
then
322-
();
330+
//case PARTIAL_CLASS()
331+
// algorithm
332+
// instance.classes := ClassTree.map(instance.classes, cloneEntry);
333+
// then
334+
// ();
323335

324336
case EXPANDED_CLASS()
325337
algorithm
326-
instance.elements := ClassTree.map(instance.elements, cloneEntry);
327-
Array.map(instance.components, ComponentNode.clone);
338+
//instance.elements := ClassTree.map(instance.elements, cloneEntry);
339+
instance.components := Array.map(instance.components, ComponentNode.clone);
328340
then
329341
();
330342

331343
case INSTANCED_CLASS()
332344
algorithm
333-
instance.elements := ClassTree.map(instance.elements, cloneEntry);
334-
Array.map(instance.components, ComponentNode.clone);
345+
//instance.elements := ClassTree.map(instance.elements, cloneEntry);
346+
instance.components := Array.map(instance.components, ComponentNode.clone);
335347
then
336348
();
337349

0 commit comments

Comments
 (0)