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

Commit 55548c1

Browse files
committed
Improved prefixing in NFInst.
1 parent 7efbde0 commit 55548c1

File tree

7 files changed

+241
-139
lines changed

7 files changed

+241
-139
lines changed

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ algorithm
100100
elements := flattenComponent(c, prefix, elements);
101101
end for;
102102

103-
elements := flattenEquations(instance.equations, prefix, elements);
104-
elements := flattenInitialEquations(instance.initialEquations, prefix, elements);
105-
elements := flattenAlgorithms(instance.algorithms, prefix, elements);
106-
elements := flattenInitialAlgorithms(instance.initialAlgorithms, prefix, elements);
103+
elements := flattenEquations(instance.equations, elements);
104+
elements := flattenInitialEquations(instance.initialEquations, elements);
105+
elements := flattenAlgorithms(instance.algorithms, elements);
106+
elements := flattenInitialAlgorithms(instance.initialAlgorithms, elements);
107107
then
108108
();
109109

@@ -242,7 +242,7 @@ algorithm
242242
attributes = attr as Component.Attributes.ATTRIBUTES())
243243
algorithm
244244
cref := Prefix.toCref(prefix);
245-
binding_exp := flattenBinding(component.binding, prefix);
245+
binding_exp := flattenBinding(component.binding);
246246

247247
var := DAE.VAR(
248248
cref,
@@ -270,7 +270,6 @@ end flattenScalar;
270270

271271
function flattenBinding
272272
input Binding binding;
273-
input Prefix prefix;
274273
output Option<DAE.Exp> bindingExp;
275274
algorithm
276275
bindingExp := match binding
@@ -285,10 +284,11 @@ algorithm
285284
case Binding.TYPED_BINDING()
286285
algorithm
287286
// TODO: Implement this in a saner way.
288-
subs := List.lastN(List.flatten(Prefix.allSubscripts(prefix)),
289-
binding.propagatedDims);
290-
then
291-
SOME(Expression.subscriptExp(binding.bindingExp, subs));
287+
//subs := List.lastN(List.flatten(Prefix.allSubscripts(prefix)),
288+
// binding.propagatedDims);
289+
//then
290+
// SOME(Expression.subscriptExp(binding.bindingExp, subs));
291+
then SOME(binding.bindingExp);
292292

293293
else
294294
algorithm
@@ -302,72 +302,57 @@ end flattenBinding;
302302

303303
function flattenEquation
304304
input Equation eq;
305-
input Prefix prefix;
306305
input output list<DAE.Element> elements = {};
307306
algorithm
308307
elements := match eq
309308
local
310309
DAE.Exp lhs, rhs;
311310

312311
case Equation.EQUALITY()
313-
algorithm
314-
//lhs := Prefix.prefixExp(eq.lhs, prefix);
315-
lhs := eq.lhs;
316-
//rhs := Prefix.prefixExp(eq.rhs, prefix);
317-
rhs := eq.rhs;
318-
then
319-
DAE.EQUATION(lhs, rhs, DAE.emptyElementSource) :: elements;
312+
then DAE.EQUATION(eq.lhs, eq.rhs, DAE.emptyElementSource) :: elements;
320313

321314
case Equation.IF()
322-
then flattenIfEquation(eq.branches, false, prefix) :: elements;
315+
then flattenIfEquation(eq.branches, false) :: elements;
323316

324317
else elements;
325318
end match;
326319
end flattenEquation;
327320

328321
function flattenEquations
329322
input list<Equation> equations;
330-
input Prefix prefix;
331323
input output list<DAE.Element> elements = {};
332324
algorithm
333-
elements := List.fold1(equations, flattenEquation, prefix, elements);
325+
elements := List.fold(equations, flattenEquation, elements);
334326
end flattenEquations;
335327

336328
function flattenInitialEquation
337329
input Equation eq;
338-
input Prefix prefix;
339330
input output list<DAE.Element> elements;
340331
algorithm
341332
elements := match eq
342333
local
343334
DAE.Exp lhs, rhs;
344335

345336
case Equation.EQUALITY()
346-
algorithm
347-
lhs := Prefix.prefixExp(eq.lhs, prefix);
348-
rhs := Prefix.prefixExp(eq.rhs, prefix);
349-
then
350-
DAE.INITIALEQUATION(lhs, rhs, DAE.emptyElementSource) :: elements;
337+
then DAE.INITIALEQUATION(eq.lhs, eq.rhs, DAE.emptyElementSource) :: elements;
351338

352339
case Equation.IF()
353-
then flattenIfEquation(eq.branches, true, prefix) :: elements;
340+
then flattenIfEquation(eq.branches, true) :: elements;
354341

355342
else elements;
356343
end match;
357344
end flattenInitialEquation;
358345

359346
function flattenInitialEquations
360347
input list<Equation> equations;
361-
input Prefix prefix;
362348
input output list<DAE.Element> elements = {};
363349
algorithm
364-
elements := List.fold1(equations, flattenInitialEquation, prefix, elements);
350+
elements := List.fold(equations, flattenInitialEquation, elements);
365351
end flattenInitialEquations;
366352

367353
function flattenIfEquation
368354
input list<tuple<DAE.Exp, list<Equation>>> ifBranches;
369355
input Boolean isInitial;
370-
input Prefix prefix;
371356
output DAE.Element ifEquation;
372357
protected
373358
list<DAE.Exp> conditions = {};
@@ -376,7 +361,7 @@ protected
376361
algorithm
377362
for b in ifBranches loop
378363
conditions := Util.tuple21(b) :: conditions;
379-
branches := flattenEquations(Util.tuple22(b), prefix) :: branches;
364+
branches := flattenEquations(Util.tuple22(b)) :: branches;
380365
end for;
381366

382367
// Transform the last branch to an else-branch if its condition is true.
@@ -402,34 +387,30 @@ end flattenIfEquation;
402387

403388
function flattenAlgorithm
404389
input list<Statement> algSection;
405-
input Prefix prefix;
406390
input output list<DAE.Element> elements;
407391
algorithm
408392

409393
end flattenAlgorithm;
410394

411395
function flattenAlgorithms
412396
input list<list<Statement>> algorithms;
413-
input Prefix prefix;
414397
input output list<DAE.Element> elements = {};
415398
algorithm
416-
elements := List.fold1(algorithms, flattenAlgorithm, prefix, elements);
399+
elements := List.fold(algorithms, flattenAlgorithm, elements);
417400
end flattenAlgorithms;
418401

419402
function flattenInitialAlgorithm
420403
input list<Statement> algSection;
421-
input Prefix prefix;
422404
input output list<DAE.Element> elements;
423405
algorithm
424406

425407
end flattenInitialAlgorithm;
426408

427409
function flattenInitialAlgorithms
428410
input list<list<Statement>> algorithms;
429-
input Prefix prefix;
430411
input output list<DAE.Element> elements = {};
431412
algorithm
432-
elements := List.fold1(algorithms, flattenInitialAlgorithm, prefix, elements);
413+
elements := List.fold(algorithms, flattenInitialAlgorithm, elements);
433414
end flattenInitialAlgorithms;
434415

435416
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import NFInstance.ClassTree;
4848
import NFInstance.Instance;
4949
import NFInstanceTree.InstanceTree;
5050
import NFInstNode.InstNode;
51+
import NFInstNode.InstParent;
5152
import NFMod.Modifier;
5253
import NFMod.ModifierScope;
5354
import NFEquation.Equation;
@@ -76,7 +77,7 @@ algorithm
7677
inst_tree := makeTree(program);
7778

7879
(cls, inst_tree) := Lookup.lookupClassName(classPath, inst_tree, Absyn.dummyInfo);
79-
(cls, inst_tree) := instantiate(cls, Modifier.NOMOD(), inst_tree);
80+
(cls, inst_tree) := instantiate(cls, Modifier.NOMOD(), InstParent.NO_PARENT(), inst_tree);
8081

8182
// TODO: Why is this a separate phase? Could be done in instComponent?
8283
(cls, inst_tree) := instBindings(cls, inst_tree);
@@ -92,19 +93,21 @@ end instClassInProgram;
9293
function instantiate
9394
input output InstNode node;
9495
input Modifier modifier;
96+
input InstParent parent;
9597
input output InstanceTree tree;
9698
algorithm
9799
(node, tree) := partialInstClass(node, tree);
98-
(node, tree) := expandClass(node, tree);
100+
(node, tree) := expandClass(node, parent, tree);
99101
(node, tree) := instClass(node, modifier, tree);
100102
end instantiate;
101103

102104
function expand
103105
input output InstNode node;
106+
input InstParent parent;
104107
input output InstanceTree tree;
105108
algorithm
106109
(node, tree) := partialInstClass(node, tree);
107-
(node, tree) := expandClass(node, tree);
110+
(node, tree) := expandClass(node, parent, tree);
108111
end expand;
109112

110113
function makeTree
@@ -148,7 +151,7 @@ algorithm
148151
case SCode.CLASS()
149152
algorithm
150153
idx := idx + 1;
151-
node := InstNode.new(e.name, e, idx, scope_id);
154+
node := InstNode.new(e.name, e, idx, scope_id, InstParent.NO_PARENT());
152155
il := node :: il;
153156
scope := addElementIdToScope(e.name, ClassTree.Entry.CLASS(idx), e.info, scope);
154157
then
@@ -236,7 +239,7 @@ algorithm
236239
end match;
237240
end for;
238241

239-
tree := InstanceTree.setCurrentScope(tree, scope_idx);
242+
tree := InstanceTree.setCurrentScopeIndex(tree, scope_idx);
240243
end addImportsToScope;
241244

242245
function partialInstClass
@@ -250,7 +253,7 @@ algorithm
250253

251254
case InstNode.INST_NODE(instance = Instance.NOT_INSTANTIATED(), definition = SOME(def))
252255
algorithm
253-
tree := InstanceTree.setCurrentScope(tree, InstNode.index(node));
256+
tree := InstanceTree.setCurrentScope(tree, node);
254257
(instance, tree) := partialInstClass2(def, tree);
255258
node.instance := instance;
256259
tree := InstanceTree.updateNode(node, tree);
@@ -283,6 +286,7 @@ end partialInstClass2;
283286

284287
function expandClass
285288
input output InstNode node;
289+
input InstParent parent;
286290
input output InstanceTree tree;
287291
algorithm
288292
_ := match node
@@ -291,19 +295,20 @@ algorithm
291295

292296
case InstNode.INST_NODE(instance = Instance.PARTIAL_CLASS(), definition = SOME(def))
293297
algorithm
294-
(node, tree) := expandClass2(node, def, tree);
298+
(node, tree) := expandClass2(node, def, parent, tree);
295299
then
296300
();
297301

298302
else ();
299303
end match;
300304

301-
tree := InstanceTree.setCurrentScope(tree, InstNode.index(node));
305+
tree := InstanceTree.setCurrentScope(tree, node);
302306
end expandClass;
303307

304308
function expandClass2
305309
input InstNode node;
306310
input SCode.Element definition;
311+
input InstParent parent;
307312
output InstNode expandedNode = node;
308313
input output InstanceTree tree;
309314
algorithm
@@ -333,7 +338,7 @@ algorithm
333338
def.classDef := SCode.PARTS({ext}, {}, {}, {}, {}, {}, {}, NONE());
334339
i := Instance.PARTIAL_CLASS(ClassTree.new(), {ext});
335340
inode := InstNode.setInstance(node, i);
336-
(expandedNode, tree) := expandClass2(inode, def, tree);
341+
(expandedNode, tree) := expandClass2(inode, def, parent, tree);
337342
then
338343
();
339344
// case SCode.CLASS(classDef = SCode.DERIVED(typeSpec = ty, modifications = der_mod))
@@ -344,7 +349,7 @@ algorithm
344349
//
345350
// // Derived classes have no scope of their own, use the parent scope.
346351
// scope_id := InstNode.parent(expandedNode);
347-
// tree := InstanceTree.setCurrentScope(tree, scope_id);
352+
// tree := InstanceTree.setCurrentScopeIndex(tree, scope_id);
348353
//
349354
// // Lookup and expand the derived class.
350355
// Absyn.TPATH(path = ty_path) := ty;
@@ -372,13 +377,14 @@ algorithm
372377
algorithm
373378
Instance.PARTIAL_CLASS(classes = scope, elements = elements) :=
374379
InstNode.instance(expandedNode);
375-
tree := InstanceTree.setCurrentScope(tree, InstNode.index(expandedNode));
380+
tree := InstanceTree.setCurrentScope(tree, expandedNode);
376381
i := Instance.initExpandedClass(scope);
377382
expandedNode := InstNode.setInstance(expandedNode, i);
378383
tree := InstanceTree.updateNode(expandedNode, tree);
379384

380385
// Expand all extends clauses.
381-
(components, scope, tree) := expandExtends(elements, definition.name, scope, tree);
386+
(components, scope, tree) := expandExtends(elements, definition.name,
387+
scope, parent, tree);
382388

383389
// Add component ids to the scope.
384390
idx := 1;
@@ -402,6 +408,7 @@ algorithm
402408

403409
// Update the instance in the tree.
404410
expandedNode := InstNode.setInstance(expandedNode, i);
411+
expandedNode := InstNode.setInstParent(expandedNode, parent);
405412
tree := InstanceTree.updateNode(expandedNode, tree);
406413
then
407414
();
@@ -421,6 +428,7 @@ function expandExtends
421428
input String className;
422429
output list<Component> components = {};
423430
input output ClassTree.Tree scope;
431+
input InstParent parent;
424432
input output InstanceTree tree;
425433
protected
426434
InstNode ext_node;
@@ -440,7 +448,7 @@ algorithm
440448
ext_node := InstNode.setIndex(ext_node, InstanceTree.instanceCount(tree) + 1);
441449
ext_node := InstNode.rename(ext_node, className);
442450
tree := InstanceTree.addInstances({ext_node}, tree);
443-
(ext_node, tree) := expand(ext_node, tree);
451+
(ext_node, tree) := expand(ext_node, parent, tree);
444452

445453
// Initialize the modifier from the extends clause.
446454
mod_scope := ModifierScope.EXTENDS_SCOPE(e.baseClassPath);
@@ -597,7 +605,7 @@ algorithm
597605
case InstNode.INST_NODE(instance = i as Instance.EXPANDED_CLASS(elements = scope))
598606
algorithm
599607
comp_count := arrayLength(i.components);
600-
tree := InstanceTree.setCurrentScope(tree, InstNode.index(node));
608+
tree := InstanceTree.setCurrentScope(tree, node);
601609

602610
if comp_count > 0 then
603611
components := Dangerous.arrayCreateNoInit(comp_count,
@@ -679,17 +687,18 @@ algorithm
679687
case Component.COMPONENT_DEF(definition = comp as SCode.COMPONENT())
680688
algorithm
681689
scope := InstanceTree.currentScopeIndex(tree);
682-
tree := InstanceTree.setCurrentScope(tree, component.scope);
690+
tree := InstanceTree.setCurrentScopeIndex(tree, component.scope);
683691
comp_mod := Modifier.create(comp.modifications, comp.name,
684692
ModifierScope.COMPONENT_SCOPE(comp.name), component.scope);
685693
comp_mod := Modifier.merge(component.modifier, comp_mod);
686694
comp_mod := Modifier.propagate(comp_mod, listLength(comp.attributes.arrayDims));
687-
(cls, tree) := instTypeSpec(comp.typeSpec, comp_mod, comp.info, tree);
695+
(cls, tree) := instTypeSpec(comp.typeSpec, comp_mod,
696+
InstParent.COMPONENT(component), comp.info, tree);
688697
(dims, tree) := instDimensions(comp.attributes.arrayDims, tree);
689698
Modifier.checkEach(comp_mod, listEmpty(dims), comp.name);
690699
attr := instComponentAttributes(comp.attributes, comp.prefixes);
691700
binding := Modifier.binding(comp_mod);
692-
tree := InstanceTree.setCurrentScope(tree, scope);
701+
tree := InstanceTree.setCurrentScopeIndex(tree, scope);
693702
then
694703
(Component.UNTYPED_COMPONENT(comp.name, cls, listArray(dims), binding, attr, comp.info), tree);
695704

@@ -724,6 +733,7 @@ end instComponentAttributes;
724733
function instTypeSpec
725734
input Absyn.TypeSpec typeSpec;
726735
input Modifier modifier;
736+
input InstParent parent;
727737
input SourceInfo info;
728738
output InstNode node;
729739
input output InstanceTree tree;
@@ -732,7 +742,7 @@ algorithm
732742
case Absyn.TPATH()
733743
algorithm
734744
(node, tree) := Lookup.lookupClassName(typeSpec.path, tree, info);
735-
(node, tree) := instantiate(node, modifier, tree);
745+
(node, tree) := instantiate(node, modifier, parent, tree);
736746
then
737747
(node, tree);
738748

0 commit comments

Comments
 (0)