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

Commit 9b5f0d6

Browse files
perostOpenModelica-Hudson
authored andcommitted
NFInst improvements.
- Improved the type checking of bindings to handle binding dimensions. - Improved deduction of array dimensions to handle binding dimensions. - Improved flattening of array bindings. - Implemented better detection of typenames during name lookup. - Fixed prefixing of inherited redeclared elements. - Implemented better support for enumeration ranges.
1 parent f482eaf commit 9b5f0d6

File tree

12 files changed

+133
-82
lines changed

12 files changed

+133
-82
lines changed

Compiler/FrontEnd/InstVar.mo

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -811,23 +811,6 @@ algorithm
811811
Absyn.Path path;
812812
list<DAE.Var> vars;
813813

814-
/*
815-
case (cache,env,ih,store,ci_state,mod,pre,n,cl,attr,pf,dims,idxs,inst_dims,impl,comment,info,graph,csets)
816-
equation
817-
true = SCode.isPartial(cl);
818-
819-
//Do not flatten because it is a function
820-
dims_1 = InstUtil.instDimExpLst(dims, impl);
821-
822-
(cache,env_1,ih,ci_state,vars) = Inst.partialInstClassIn(cache, env, ih, mod, pre, ci_state, cl, SCode.PUBLIC(), inst_dims, 0);
823-
dae = DAE.emptyDae;
824-
(cache, path) = Inst.makeFullyQualified(cache, env, Absyn.IDENT(n));
825-
ty = DAE.T_COMPLEX(ci_state, vars, NONE(), {path});
826-
ty = InstUtil.makeArrayType(dims, ty);
827-
then
828-
(cache,env_1,ih,store,dae,csets,ty,graph);*/
829-
830-
831814
// Rules for instantation of function variables (e.g. input and output
832815

833816
// Function variables with modifiers (outputs or local/protected variables)
@@ -1035,7 +1018,8 @@ algorithm
10351018
case (_,_,_,_,_,DAE.NOMOD(),_,n,_,_,_,
10361019
((DAE.DIM_UNKNOWN()) :: _),_,_,_,_,info,_,_)
10371020
equation
1038-
Error.addSourceMessage(Error.FAILURE_TO_DEDUCE_DIMS_NO_MOD,{n},info);
1021+
Error.addSourceMessage(Error.FAILURE_TO_DEDUCE_DIMS_NO_MOD,
1022+
{String(listLength(inSubscripts) + 1), n},info);
10391023
then
10401024
fail();
10411025

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,30 @@ public
4747
record RAW_BINDING
4848
Absyn.Exp bindingExp;
4949
InstNode scope;
50-
Integer propagatedDims;
50+
Integer propagatedLevels;
5151
SourceInfo info;
5252
end RAW_BINDING;
5353

5454
record UNTYPED_BINDING
5555
Expression bindingExp;
5656
Boolean isProcessing;
5757
InstNode scope;
58-
Integer propagatedDims;
58+
Integer propagatedLevels;
5959
SourceInfo info;
6060
end UNTYPED_BINDING;
6161

6262
record TYPED_BINDING
6363
Expression bindingExp;
6464
Type bindingType;
6565
DAE.Const variability;
66-
Integer propagatedDims;
66+
Integer propagatedLevels;
6767
SourceInfo info;
6868
end TYPED_BINDING;
6969

7070
public
7171
function fromAbsyn
7272
input Option<Absyn.Exp> bindingExp;
7373
input SCode.Each eachPrefix;
74-
input Integer dimensions;
7574
input InstNode scope;
7675
input SourceInfo info;
7776
output Binding binding;
@@ -83,7 +82,7 @@ public
8382

8483
case SOME(exp)
8584
algorithm
86-
pd := if SCode.eachBool(eachPrefix) then -1 else dimensions;
85+
pd := if SCode.eachBool(eachPrefix) then -1 else 0;
8786
then
8887
RAW_BINDING(exp, scope, pd, info);
8988

@@ -175,9 +174,9 @@ public
175174
output Boolean isEach;
176175
algorithm
177176
isEach := match binding
178-
case RAW_BINDING() then binding.propagatedDims == -1;
179-
case UNTYPED_BINDING() then binding.propagatedDims == -1;
180-
case TYPED_BINDING() then binding.propagatedDims == -1;
177+
case RAW_BINDING() then binding.propagatedLevels == -1;
178+
case UNTYPED_BINDING() then binding.propagatedLevels == -1;
179+
case TYPED_BINDING() then binding.propagatedLevels == -1;
181180
else false;
182181
end match;
183182
end isEach;

Compiler/NFFrontEnd/NFClassTree.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public
475475
// Make a new extends array, and recursively instantiate the extends nodes.
476476
exts := arrayCopy(exts);
477477
for i in 1:arrayLength(exts) loop
478-
(node, _, cls_count, comp_count) := instantiate(exts[i]);
478+
(node, _, cls_count, comp_count) := instantiate(exts[i], instance);
479479
exts[i] := node;
480480
// Add the inherited elements to the class/component counts.
481481
classCount := cls_count + classCount;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,24 @@ function flattenFunctionParams
314314
protected
315315
InstNode node;
316316
Component comp;
317+
ComponentRef prefix;
318+
Type ty;
317319
algorithm
318320
for i in arrayLength(components):-1:1 loop
319321
node := components[i];
320322
comp := InstNode.component(node);
323+
ty := Component.getType(comp);
324+
prefix := ComponentRef.fromNode(node, ty, {});
321325

322326
(elements, funcs) :=
323-
flattenFunctionParam(comp, InstNode.name(node), elements, funcs);
327+
flattenFunctionParam(comp, InstNode.name(node), prefix, elements, funcs);
324328
end for;
325329
end flattenFunctionParams;
326330

327331
function flattenFunctionParam
328332
input Component component;
329333
input String name;
334+
input ComponentRef prefix;
330335
input output list<DAE.Element> elements;
331336
input output DAE.FunctionTree funcs;
332337
algorithm
@@ -350,7 +355,7 @@ algorithm
350355
cref := DAE.CREF_IDENT(name, ty, {});
351356
attr := component.attributes;
352357
(binding_exp, funcs) :=
353-
flattenBinding(component.binding, ComponentRef.EMPTY(), funcs);
358+
flattenBinding(component.binding, prefix, funcs);
354359

355360
var_attr := match i
356361
case Class.INSTANCED_BUILTIN()
@@ -416,10 +421,10 @@ algorithm
416421

417422
case Binding.TYPED_BINDING()
418423
algorithm
419-
if binding.propagatedDims <= 0 then
424+
if binding.propagatedLevels < 0 then
420425
e := binding.bindingExp;
421426
else
422-
subs := List.lastN(List.flatten(ComponentRef.allSubscripts(prefix)), binding.propagatedDims);
427+
subs := List.flatten(List.lastN(ComponentRef.allSubscripts(prefix), binding.propagatedLevels + 1));
423428
e := Expression.subscript(binding.bindingExp, subs);
424429
end if;
425430

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,14 @@ algorithm
806806

807807
dims := list(Dimension.RAW_DIM(d) for d in def.attributes.arrayDims);
808808
Modifier.checkEach(comp_mod, listEmpty(dims), InstNode.name(node));
809-
comp_mod := Modifier.propagate(comp_mod, listLength(dims));
809+
binding := Modifier.binding(comp_mod);
810+
comp_mod := Modifier.propagate(comp_mod);
810811

811812
// Instantiate the type of the component.
812813
cls := instTypeSpec(def.typeSpec, comp_mod, scope, node, def.info);
813814

814815
// Instantiate attributes and create the untyped components.
815816
attr := instComponentAttributes(def.attributes, def.prefixes);
816-
binding := Modifier.binding(comp_mod);
817817
inst_comp := Component.UNTYPED_COMPONENT(cls, listArray(dims), binding,
818818
attr, SCode.isElementRedeclare(def), def.info);
819819
InstNode.updateComponent(inst_comp, node);
@@ -909,7 +909,7 @@ algorithm
909909
algorithm
910910
// Instantiate expressions in the extends nodes.
911911
sections := ClassTree.foldExtends(cls_tree,
912-
function instExpressions(scope = node), sections);
912+
function instExpressions(scope = scope), sections);
913913

914914
// Instantiate expressions in the local components.
915915
ClassTree.applyLocalComponents(cls_tree,
@@ -1036,7 +1036,7 @@ algorithm
10361036
algorithm
10371037
bind_exp := instExp(binding.bindingExp, binding.scope, binding.info, allowTypename);
10381038
then
1039-
Binding.UNTYPED_BINDING(bind_exp, false, binding.scope, binding.propagatedDims, binding.info);
1039+
Binding.UNTYPED_BINDING(bind_exp, false, binding.scope, binding.propagatedLevels, binding.info);
10401040

10411041
else binding;
10421042
end match;
@@ -1385,7 +1385,7 @@ algorithm
13851385

13861386
case SCode.EEquation.EQ_FOR(info = info)
13871387
algorithm
1388-
binding := Binding.fromAbsyn(scodeEq.range, SCode.NOT_EACH(), 0, scope, info);
1388+
binding := Binding.fromAbsyn(scodeEq.range, SCode.NOT_EACH(), scope, info);
13891389
binding := instBinding(binding, allowTypename = true);
13901390

13911391
(for_scope, iter) := addIteratorToScope(scodeEq.index, binding, info, scope);
@@ -1513,7 +1513,7 @@ algorithm
15131513

15141514
case SCode.Statement.ALG_FOR(info = info)
15151515
algorithm
1516-
binding := Binding.fromAbsyn(scodeStmt.range, SCode.NOT_EACH(), 0, scope, info);
1516+
binding := Binding.fromAbsyn(scodeStmt.range, SCode.NOT_EACH(), scope, info);
15171517
binding := instBinding(binding, allowTypename = true);
15181518

15191519
(for_scope, iter) := addIteratorToScope(scodeStmt.index, binding, info, scope);

Compiler/NFFrontEnd/NFLookup.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected
107107
Type ty;
108108
algorithm
109109
if InstNode.isClass(component) then
110-
ty := Class.getType(InstNode.getClass(component));
110+
ty := Class.getType(InstNode.getClass(Inst.expand(component)));
111111

112112
state := match ty
113113
case Type.ENUMERATION() then LookupState.STATE_COMP();

Compiler/NFFrontEnd/NFMod.mo

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public
152152

153153
case SCode.MOD()
154154
algorithm
155-
binding := Binding.fromAbsyn(mod.binding, mod.eachPrefix, 0, scope, mod.info);
155+
binding := Binding.fromAbsyn(mod.binding, mod.eachPrefix, scope, mod.info);
156156
submod_lst := list((m.ident, createSubMod(m, modScope, scope)) for m in mod.subModLst);
157157
submod_table := ModTable.fromList(submod_lst,
158158
function mergeLocal(scope = modScope, prefix = {}));
@@ -326,34 +326,39 @@ public
326326
checking, so that it matches the binding. To do this we need to now how many
327327
dimensions the binding has been propagated through. In this case it's been
328328
propagated from B.a to A.x, and since B.a has one dimension we should add
329-
that dimension to A.x to make it match the binding. The number of dimensions
330-
that a binding is propagated through is therefore saved in a binding. A
331-
binding can also have the 'each' prefix, meaning that the binding should be
332-
applied as it is. In that case we set the dimension counter to -1 and don't
333-
increment it when the binding is propagated.
329+
that dimension to A.x to make it match the binding. The number of levels a
330+
binding is propagated through is therefore saved in each binding. A binding
331+
can also have the 'each' prefix, meaning that the binding should be applied
332+
as it is. In that case we set the level counter to -1 and don't increment
333+
it when the binding is propagated.
334334

335335
This function simply goes through a modifier recursively and increments the
336-
dimension counter by the number of dimensions that an element has."
336+
level counter by 1 for all bindings in the modifier, but will not traverse
337+
into submodifiers that have the 'each' prefix."
337338
input output Modifier modifier;
338-
input Integer dimensions;
339339
algorithm
340-
if dimensions == 0 then
341-
return;
342-
end if;
343-
344340
_ := match modifier
345341
case MODIFIER()
346342
algorithm
347-
modifier.binding := propagateBinding(modifier.binding, dimensions);
348-
modifier.subModifiers := ModTable.map(modifier.subModifiers,
349-
function propagateSubMod(dimensions = dimensions));
343+
modifier.binding := propagateBinding(modifier.binding);
344+
modifier.subModifiers := ModTable.map(modifier.subModifiers, propagateSubMod);
350345
then
351346
();
352347

353348
else ();
354349
end match;
355350
end propagate;
356351

352+
function isEach
353+
input Modifier mod;
354+
output Boolean isEach;
355+
algorithm
356+
isEach := match mod
357+
case MODIFIER(eachPrefix = SCode.EACH()) then true;
358+
else false;
359+
end match;
360+
end isEach;
361+
357362
function checkEach
358363
input Modifier mod;
359364
input Boolean isScalar;
@@ -489,23 +494,19 @@ protected
489494
function propagateSubMod
490495
input String name;
491496
input output Modifier modifier;
492-
input Integer dimensions;
493497
algorithm
494-
modifier := propagate(modifier, dimensions);
498+
if not isEach(modifier) then
499+
modifier := propagate(modifier);
500+
end if;
495501
end propagateSubMod;
496502

497503
function propagateBinding
498504
input output Binding binding;
499-
input Integer dimensions;
500505
algorithm
501506
_ := match binding
502-
// Special case for the each prefix, don't do anything.
503-
case Binding.RAW_BINDING(propagatedDims = -1) then ();
504-
505-
// A normal binding, increment with the dimension count.
506507
case Binding.RAW_BINDING()
507508
algorithm
508-
binding.propagatedDims := binding.propagatedDims + dimensions;
509+
binding.propagatedLevels := binding.propagatedLevels + 1;
509510
then
510511
();
511512

Compiler/NFFrontEnd/NFRangeIterator.mo

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public
6363
Integer istart, istep, istop;
6464
Real rstart, rstep, rstop;
6565
Type ty;
66+
list<String> literals;
67+
Absyn.Path path;
68+
list<Expression> values;
6669

6770
case Expression.ARRAY() then ARRAY_RANGE(exp.elements);
6871

@@ -86,6 +89,28 @@ public
8689
stop = Expression.REAL(rstop))
8790
then REAL_RANGE(rstart, 1.0, rstop);
8891

92+
case Expression.RANGE(start = Expression.ENUM_LITERAL(ty = ty, index = istart),
93+
step = NONE(),
94+
stop = Expression.ENUM_LITERAL(index = istop))
95+
algorithm
96+
Type.ENUMERATION(typePath = path, literals = literals) := ty;
97+
values := {};
98+
99+
if istart <= istop then
100+
for i in 2:istart loop
101+
literals := listRest(literals);
102+
end for;
103+
104+
for i in istart:istop loop
105+
values := Expression.ENUM_LITERAL(ty, listHead(literals), i) :: values;
106+
literals := listRest(literals);
107+
end for;
108+
109+
values := listReverse(values);
110+
end if;
111+
then
112+
ARRAY_RANGE(values);
113+
89114
else
90115
algorithm
91116
assert(false, getInstanceName() + " got unknown range");

Compiler/NFFrontEnd/NFType.mo

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ public
137137
end if;
138138
end unliftArray;
139139

140+
function unliftArrayN
141+
input Integer N;
142+
input output Type ty;
143+
protected
144+
Type el_ty;
145+
list<Dimension> dims;
146+
algorithm
147+
ARRAY(el_ty, dims) := ty;
148+
149+
for i in 1:N loop
150+
dims := listRest(dims);
151+
end for;
152+
153+
if listEmpty(dims) then
154+
ty := el_ty;
155+
else
156+
ty := ARRAY(el_ty, dims);
157+
end if;
158+
end unliftArrayN;
159+
140160
function isInteger
141161
input Type ty;
142162
output Boolean isInteger;
@@ -321,20 +341,10 @@ public
321341
dims := match ty
322342
case ARRAY() then ty.dimensions;
323343
case FUNCTION() then arrayDims(ty.resultType);
344+
else {};
324345
end match;
325346
end arrayDims;
326347

327-
function getTypeDims
328-
input Type ty;
329-
output list<Dimension> dims;
330-
algorithm
331-
try
332-
dims := arrayDims(ty);
333-
else
334-
dims := {};
335-
end try;
336-
end getTypeDims;
337-
338348
function nthDimension
339349
input Type ty;
340350
input Integer index;

0 commit comments

Comments
 (0)