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

Commit 6ca8924

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improvements.
- Simplified the type checking of bindings, and made it work for binding on array types too. - Added check that non-type classes shouldn't have dimensions. - Fixed the check for 'each' being used correctly, and made it into a varning instead of an error. - Fixed the typing of crefs so that it doesn't cause typing cycles. Belonging to [master]: - #2370 - OpenModelica/OpenModelica-testsuite#923
1 parent d36f68c commit 6ca8924

19 files changed

+258
-139
lines changed

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,31 @@ protected
4343
import Binding = NFBinding;
4444

4545
public
46-
type Origin = enumeration(COMPONENT, EXTENDS, CLASS);
47-
48-
record UNBOUND end UNBOUND;
46+
record UNBOUND
47+
Option<NFBindingOrigin> origin;
48+
end UNBOUND;
4949

5050
record RAW_BINDING
5151
Absyn.Exp bindingExp;
5252
InstNode scope;
5353
BindingOrigin origin;
54+
Boolean isEach;
5455
end RAW_BINDING;
5556

5657
record UNTYPED_BINDING
5758
Expression bindingExp;
5859
Boolean isProcessing;
5960
InstNode scope;
6061
BindingOrigin origin;
62+
Boolean isEach;
6163
end UNTYPED_BINDING;
6264

6365
record TYPED_BINDING
6466
Expression bindingExp;
6567
Type bindingType;
6668
Variability variability;
6769
BindingOrigin origin;
70+
Boolean isEach;
6871
end TYPED_BINDING;
6972

7073
record FLAT_BINDING
@@ -86,9 +89,9 @@ public
8689
Absyn.Exp exp;
8790

8891
case SOME(exp)
89-
then RAW_BINDING(exp, scope, BindingOrigin.create(eachPrefix, level, ty, info));
92+
then RAW_BINDING(exp, scope, BindingOrigin.create(level, ty, info), eachPrefix);
9093

91-
else UNBOUND();
94+
else UNBOUND(if eachPrefix then SOME(BindingOrigin.create(level, ty, info)) else NONE());
9295
end match;
9396
end fromAbsyn;
9497

@@ -178,10 +181,11 @@ public
178181
output SourceInfo info;
179182
algorithm
180183
info := match binding
181-
case UNBOUND() then Absyn.dummyInfo;
184+
case UNBOUND(origin = SOME(BindingOrigin.ORIGIN(info = info))) then info;
182185
case RAW_BINDING() then binding.origin.info;
183186
case UNTYPED_BINDING() then binding.origin.info;
184187
case TYPED_BINDING() then binding.origin.info;
188+
else Absyn.dummyInfo;
185189
end match;
186190
end getInfo;
187191

@@ -190,12 +194,32 @@ public
190194
output BindingOrigin origin;
191195
algorithm
192196
origin := match binding
197+
case UNBOUND(origin = SOME(origin)) then origin;
193198
case RAW_BINDING() then binding.origin;
194199
case UNTYPED_BINDING() then binding.origin;
195200
case TYPED_BINDING() then binding.origin;
196201
end match;
197202
end getOrigin;
198203

204+
function setOrigin
205+
input BindingOrigin origin;
206+
input output Binding binding;
207+
algorithm
208+
() := match binding
209+
case RAW_BINDING() algorithm binding.origin := origin; then ();
210+
case UNTYPED_BINDING() algorithm binding.origin := origin; then ();
211+
case TYPED_BINDING() algorithm binding.origin := origin; then ();
212+
else ();
213+
end match;
214+
end setOrigin;
215+
216+
function setOriginNode
217+
input InstNode node;
218+
input output Binding binding;
219+
algorithm
220+
binding := setOrigin(BindingOrigin.setNode(node, getOrigin(binding)), binding);
221+
end setOriginNode;
222+
199223
function getType
200224
input Binding binding;
201225
output Type ty;
@@ -208,9 +232,10 @@ public
208232
output Boolean isEach;
209233
algorithm
210234
isEach := match binding
211-
case RAW_BINDING() then BindingOrigin.isEach(binding.origin);
212-
case UNTYPED_BINDING() then BindingOrigin.isEach(binding.origin);
213-
case TYPED_BINDING() then BindingOrigin.isEach(binding.origin);
235+
case UNBOUND(origin = SOME(_)) then true;
236+
case RAW_BINDING() then binding.isEach;
237+
case UNTYPED_BINDING() then binding.isEach;
238+
case TYPED_BINDING() then binding.isEach;
214239
else false;
215240
end match;
216241
end isEach;

Compiler/NFFrontEnd/NFBindingOrigin.mo

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
encapsulated uniontype NFBindingOrigin
3333
import NFModifier.ModifierScope;
34+
import NFInstNode.InstNode;
3435

3536
protected
3637
import BindingOrigin = NFBindingOrigin;
@@ -41,29 +42,24 @@ public
4142
record ORIGIN
4243
Integer level;
4344
ElementType ty;
45+
Option<InstNode> node;
4446
SourceInfo info;
4547
end ORIGIN;
4648

4749
function create
48-
input Boolean eachPrefix;
4950
input Integer level;
5051
input ElementType ty;
5152
input SourceInfo info;
5253
output BindingOrigin origin;
5354
algorithm
54-
origin := ORIGIN(if eachPrefix then -level else level, ty, info);
55+
origin := ORIGIN(level, ty, NONE(), info);
5556
end create;
5657

5758
function level
5859
input BindingOrigin origin;
5960
output Integer level = origin.level;
6061
end level;
6162

62-
function isEach
63-
input BindingOrigin origin;
64-
output Boolean isEach = origin.level < 0;
65-
end isEach;
66-
6763
function info
6864
input BindingOrigin origin;
6965
output SourceInfo info = origin.info;
@@ -78,5 +74,12 @@ public
7874
fromClass := ty == ElementType.CLASS;
7975
end isFromClass;
8076

77+
function setNode
78+
input InstNode node;
79+
input output BindingOrigin origin;
80+
algorithm
81+
origin.node := SOME(node);
82+
end setNode;
83+
8184
annotation(__OpenModelica_Interface="frontend");
8285
end NFBindingOrigin;

Compiler/NFFrontEnd/NFBuiltin.mo

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public
4444
import Absyn;
4545
import SCode;
4646
import Binding = NFBinding;
47-
import BindingOrigin = NFBindingOrigin;
47+
import NFBindingOrigin;
4848
import NFClass.Class;
4949
import NFClassTree.ClassTree;
5050
import NFComponent.Component;
@@ -175,12 +175,11 @@ constant InstNode TIME =
175175
Pointer.createImmutable(Component.TYPED_COMPONENT(
176176
REAL_NODE,
177177
Type.REAL(),
178-
Binding.UNBOUND(),
179-
Binding.UNBOUND(),
178+
Binding.UNBOUND(NONE()),
179+
Binding.UNBOUND(NONE()),
180180
NFComponent.INPUT_ATTR,
181181
NONE(),
182182
Absyn.dummyInfo)),
183-
0,
184183
InstNode.EMPTY_NODE());
185184

186185
constant ComponentRef TIME_CREF = ComponentRef.CREF(TIME, {}, Type.REAL(), Origin.CREF, ComponentRef.EMPTY());

Compiler/NFFrontEnd/NFBuiltinFuncs.mo

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,43 @@ constant SCode.Element DUMMY_ELEMENT = SCode.CLASS(
7575

7676
// Default Integer parameter.
7777
constant Component INT_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
78-
Type.INTEGER(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
78+
Type.INTEGER(), Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
7979

8080
constant InstNode INT_PARAM = InstNode.COMPONENT_NODE("i",
8181
Visibility.PUBLIC,
82-
Pointer.createImmutable(INT_COMPONENT), 0, InstNode.EMPTY_NODE());
82+
Pointer.createImmutable(INT_COMPONENT), InstNode.EMPTY_NODE());
8383

8484
// Default Real parameter.
8585
constant Component REAL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
86-
Type.REAL(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
86+
Type.REAL(), Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
8787

8888
constant InstNode REAL_PARAM = InstNode.COMPONENT_NODE("r",
8989
Visibility.PUBLIC,
90-
Pointer.createImmutable(REAL_COMPONENT), 0, InstNode.EMPTY_NODE());
90+
Pointer.createImmutable(REAL_COMPONENT), InstNode.EMPTY_NODE());
9191

9292
// Default Boolean parameter.
9393
constant Component BOOL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
94-
Type.BOOLEAN(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
94+
Type.BOOLEAN(), Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
9595

9696
constant InstNode BOOL_PARAM = InstNode.COMPONENT_NODE("b",
9797
Visibility.PUBLIC,
98-
Pointer.createImmutable(BOOL_COMPONENT), 0, InstNode.EMPTY_NODE());
98+
Pointer.createImmutable(BOOL_COMPONENT), InstNode.EMPTY_NODE());
9999

100100
// Default String parameter.
101101
constant Component STRING_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
102-
Type.STRING(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
102+
Type.STRING(), Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
103103

104104
constant InstNode STRING_PARAM = InstNode.COMPONENT_NODE("s",
105105
Visibility.PUBLIC,
106-
Pointer.createImmutable(STRING_COMPONENT), 0, InstNode.EMPTY_NODE());
106+
Pointer.createImmutable(STRING_COMPONENT), InstNode.EMPTY_NODE());
107107

108108
// Default enumeration(:) parameter.
109109
constant Component ENUM_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
110-
Type.ENUMERATION_ANY(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
110+
Type.ENUMERATION_ANY(), Binding.UNBOUND(NONE()), Binding.UNBOUND(NONE()), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);
111111

112112
constant InstNode ENUM_PARAM = InstNode.COMPONENT_NODE("e",
113113
Visibility.PUBLIC,
114-
Pointer.createImmutable(ENUM_COMPONENT), 0, InstNode.EMPTY_NODE());
114+
Pointer.createImmutable(ENUM_COMPONENT), InstNode.EMPTY_NODE());
115115

116116
// Integer(e)
117117
constant array<NFInstNode.CachedData> EMPTY_NODE_CACHE = listArrayLiteral({

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,8 @@ uniontype Call
668668
// Create a range binding on which we will iterate to vectorize.
669669
ty := Type.ARRAY(Type.INTEGER(), {dim});
670670
exp := Expression.RANGE(ty, Expression.INTEGER(1), NONE(), Expression.INTEGER(Dimension.size(dim)));
671-
origin := BindingOrigin.create(false, 0, NFBindingOrigin.ElementType.COMPONENT, info);
672-
bind := Binding.TYPED_BINDING(exp, ty, Variability.CONSTANT, origin);
671+
origin := BindingOrigin.create(0, NFBindingOrigin.ElementType.COMPONENT, info);
672+
bind := Binding.TYPED_BINDING(exp, ty, Variability.CONSTANT, origin, false);
673673

674674
// Add an iterator to the call scope.
675675
(iter_scope, iter) := Inst.addIteratorToScope("$i" + intString(i), bind, iter_scope, Type.INTEGER());

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,10 @@ uniontype Class
418418
output Type ty;
419419
algorithm
420420
ty := match cls
421-
case DERIVED_CLASS() then getType(InstNode.getClass(cls.baseClass), clsNode);
421+
case DERIVED_CLASS()
422+
then Type.liftArrayLeftList(
423+
getType(InstNode.getClass(cls.baseClass), clsNode),
424+
cls.dims);
422425
case INSTANCED_CLASS() then cls.ty;
423426
case PARTIAL_BUILTIN() then cls.ty;
424427
case INSTANCED_BUILTIN()

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,19 @@ public
490490
output DAE.ComponentRef dcref;
491491
algorithm
492492
dcref := match cref
493+
local
494+
Type ty;
495+
493496
case EMPTY() then accumCref;
494497
case CREF()
495498
algorithm
496-
dcref := DAE.ComponentRef.CREF_QUAL(InstNode.name(cref.node), Type.toDAE(cref.ty),
499+
// If the type is unknown here it's likely because the cref part is
500+
// from a scope prefix, which the typing doesn't bother typing since
501+
// that introduces cycles in the typing. We could patch the crefs
502+
// after the typing, but the new frontend doesn't use these types anyway.
503+
// So instead we just fetch the type of the node if the type is unknown.
504+
ty := if Type.isUnknown(cref.ty) then InstNode.getType(cref.node) else cref.ty;
505+
dcref := DAE.ComponentRef.CREF_QUAL(InstNode.name(cref.node), Type.toDAE(ty),
497506
list(Subscript.toDAE(s) for s in cref.subscripts), accumCref);
498507
then
499508
toDAE_impl(cref.restCref, dcref);
@@ -566,7 +575,7 @@ public
566575
list<Dimension> dims;
567576
list<list<Subscript>> subs;
568577

569-
case CREF()
578+
case CREF(ty = Type.ARRAY())
570579
algorithm
571580
dims := Type.arrayDims(cref.ty);
572581
subs := Subscript.expandList(cref.subscripts, dims);

Compiler/NFFrontEnd/NFExpressionIterator.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public
109109
algorithm
110110
iterator := match binding
111111
case Binding.TYPED_BINDING()
112-
then if BindingOrigin.isEach(binding.origin) or BindingOrigin.isFromClass(binding.origin) then
112+
then if Binding.isEach(binding) or BindingOrigin.isFromClass(binding.origin) then
113113
EACH_ITERATOR(binding.bindingExp) else fromExp(binding.bindingExp);
114114

115115
case Binding.FLAT_BINDING()

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,10 @@ algorithm
295295
eq := Equation.ARRAY_EQUALITY(Expression.CREF(ty, name), Binding.getTypedExp(binding), ty,
296296
ElementSource.createElementSource(info));
297297
sections := Sections.prependEquation(eq, sections);
298-
binding := Binding.UNBOUND();
298+
binding := Binding.UNBOUND(NONE());
299299
end if;
300300

301-
ty_attrs := list(flattenTypeAttribute(m, prefix, node) for m in typeAttrs);
301+
ty_attrs := list(flattenTypeAttribute(m, name, node) for m in typeAttrs);
302302
vars := Variable.VARIABLE(name, ty, binding, visibility, comp_attr, ty_attrs, cmt, info) :: vars;
303303
end flattenSimpleComponent;
304304

@@ -364,7 +364,7 @@ algorithm
364364
eq := Equation.EQUALITY(Expression.CREF(ty, name), binding_exp, ty,
365365
ElementSource.createElementSource(InstNode.info(node)));
366366
sections := Sections.prependEquation(eq, sections);
367-
opt_binding := SOME(Binding.UNBOUND());
367+
opt_binding := SOME(Binding.UNBOUND(NONE()));
368368
else
369369
opt_binding := SOME(flattenBinding(binding, prefix, node));
370370
end if;
@@ -427,8 +427,8 @@ algorithm
427427
algorithm
428428
binding_level := BindingOrigin.level(binding.origin);
429429

430-
if binding_level > 0 then
431-
subs := List.flatten(ComponentRef.subscriptsN(prefix, InstNode.level(component) - binding_level));
430+
if not binding.isEach and binding_level > 0 then
431+
subs := List.flatten(ComponentRef.subscriptsN(prefix, binding_level));
432432
binding.bindingExp := Expression.applySubscripts(subs, binding.bindingExp);
433433
end if;
434434
then

0 commit comments

Comments
 (0)