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

Commit 526262a

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Fix default values in record constructors.
- Fix conversion of bindings for DAE.Var, since record constructors uses them for default values. - Don't generate type variables when converting types of scope crefs, to avoid loops when converting complex types. Belonging to [master]: - #2468 - OpenModelica/OpenModelica-testsuite#962
1 parent c2593f2 commit 526262a

File tree

8 files changed

+136
-66
lines changed

8 files changed

+136
-66
lines changed

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ uniontype Binding
8383

8484
record FLAT_BINDING
8585
Expression bindingExp;
86+
Variability variability;
8687
end FLAT_BINDING;
8788

8889
record CEVAL_BINDING
@@ -245,7 +246,15 @@ public
245246
input Binding binding;
246247
output Variability var;
247248
algorithm
248-
TYPED_BINDING(variability = var) := binding;
249+
var := match binding
250+
case TYPED_BINDING() then binding.variability;
251+
case FLAT_BINDING() then binding.variability;
252+
else
253+
algorithm
254+
Error.assertion(false, getInstanceName() + " got unknown binding", sourceInfo());
255+
then
256+
fail();
257+
end match;
249258
end variability;
250259

251260
function getInfo
@@ -411,21 +420,46 @@ public
411420
algorithm
412421
outBinding := match binding
413422
case UNBOUND() then DAE.UNBOUND();
414-
case TYPED_BINDING()
415-
then DAE.EQBOUND(
416-
Expression.toDAE(binding.bindingExp),
417-
NONE(),
418-
Variability.variabilityToDAEConst(binding.variability),
419-
DAE.BINDING_FROM_DEFAULT_VALUE() // TODO: revise this.
420-
);
421-
423+
case TYPED_BINDING() then makeDAEBinding(binding.bindingExp, binding.variability);
424+
case FLAT_BINDING() then makeDAEBinding(binding.bindingExp, binding.variability);
425+
case CEVAL_BINDING() then DAE.UNBOUND();
422426
else
423427
algorithm
424-
Error.assertion(false, getInstanceName() + " got untyped binding.", sourceInfo());
428+
Error.assertion(false, getInstanceName() + " got untyped binding", sourceInfo());
425429
then
426430
fail();
427431
end match;
428432
end toDAE;
433+
434+
function makeDAEBinding
435+
input Expression exp;
436+
input Variability var;
437+
output DAE.Binding binding;
438+
algorithm
439+
binding := DAE.EQBOUND(
440+
Expression.toDAE(exp),
441+
NONE(),
442+
Variability.variabilityToDAEConst(var),
443+
DAE.BINDING_FROM_DEFAULT_VALUE() // TODO: revise this.
444+
);
445+
end makeDAEBinding;
446+
447+
function toDAEExp
448+
input Binding binding;
449+
output Option<DAE.Exp> bindingExp;
450+
algorithm
451+
bindingExp := match binding
452+
case UNBOUND() then NONE();
453+
case TYPED_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
454+
case FLAT_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
455+
case CEVAL_BINDING() then NONE();
456+
else
457+
algorithm
458+
Error.assertion(false, getInstanceName() + " got untyped binding", sourceInfo());
459+
then
460+
fail();
461+
end match;
462+
end toDAEExp;
429463
end Binding;
430464

431465
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ public
575575
dcref := match cref
576576
local
577577
Type ty;
578+
DAE.Type dty;
578579

579580
case EMPTY() then accumCref;
580581
case CREF()
@@ -585,7 +586,8 @@ public
585586
// after the typing, but the new frontend doesn't use these types anyway.
586587
// So instead we just fetch the type of the node if the type is unknown.
587588
ty := if Type.isUnknown(cref.ty) then InstNode.getType(cref.node) else cref.ty;
588-
dcref := DAE.ComponentRef.CREF_QUAL(InstNode.name(cref.node), Type.toDAE(ty),
589+
dty := Type.toDAE(ty, makeTypeVars = false);
590+
dcref := DAE.ComponentRef.CREF_QUAL(InstNode.name(cref.node), dty,
589591
list(Subscript.toDAE(s) for s in cref.subscripts), accumCref);
590592
then
591593
toDAE_impl(cref.restCref, dcref);

Compiler/NFFrontEnd/NFConvertDAE.mo

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected
110110
Option<DAE.VariableAttributes> var_attr;
111111
Option<DAE.Exp> binding_exp;
112112
algorithm
113-
binding_exp := convertBinding(var.binding);
113+
binding_exp := Binding.toDAEExp(var.binding);
114114
var_attr := convertVarAttributes(var.typeAttributes, var.ty, var.attributes);
115115
daeVar := makeDAEVar(var.name, var.ty, binding_exp, var.attributes,
116116
var.visibility, var_attr, var.comment, useLocalDir, false, var.info);
@@ -190,17 +190,6 @@ algorithm
190190
end match;
191191
end getComponentDirection;
192192

193-
function convertBinding
194-
input Binding binding;
195-
output Option<DAE.Exp> bindingExp;
196-
algorithm
197-
bindingExp := match binding
198-
case Binding.UNBOUND() then NONE();
199-
case Binding.TYPED_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
200-
case Binding.FLAT_BINDING() then SOME(Expression.toDAE(binding.bindingExp));
201-
end match;
202-
end convertBinding;
203-
204193
function convertVarAttributes
205194
input list<tuple<String, Binding>> attrs;
206195
input Type ty;
@@ -981,7 +970,7 @@ algorithm
981970
case Component.TYPED_COMPONENT(ty = ty, info = info)
982971
algorithm
983972
cref := ComponentRef.fromNode(node, ty);
984-
binding := convertBinding(comp.binding);
973+
binding := Binding.toDAEExp(comp.binding);
985974
cls := InstNode.getClass(comp.classInst);
986975
ty_attr := list((Modifier.name(m), Modifier.binding(m)) for m in Class.getTypeAttributes(cls));
987976
attr := comp.attributes;
@@ -1061,44 +1050,38 @@ algorithm
10611050
end convertExternalDeclOutput;
10621051

10631052
public
1064-
function makeTypesVars
1053+
function makeTypeVars
10651054
input InstNode complexCls;
10661055
output list<DAE.Var> typeVars;
10671056
protected
10681057
Component comp;
10691058
DAE.Var type_var;
10701059
algorithm
1071-
typeVars := {};
1072-
1073-
() := match cls as InstNode.getClass(complexCls)
1074-
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE()) algorithm
1075-
1076-
for c in ClassTree.getComponents(cls.elements) loop
1077-
if InstNode.isOnlyOuter(c) or InstNode.isEmpty(c) then
1078-
continue;
1079-
end if;
1080-
comp := InstNode.component(InstNode.resolveOuter(c));
1081-
1082-
type_var := DAE.TYPES_VAR(InstNode.name(c)
1083-
, Component.Attributes.toDAE(Component.getAttributes(comp))
1084-
, Type.toDAE(Component.getType(comp))
1085-
// Fix recursive bindings and update this
1086-
// , Binding.toDAE(Component.getBinding(comp))
1087-
, DAE.UNBOUND()
1088-
, NONE()
1089-
);
1090-
typeVars := type_var::typeVars;
1091-
end for;
1092-
1093-
typeVars := listReverse(typeVars);
1094-
then ();
1095-
1096-
else ();
1060+
typeVars := match cls as InstNode.getClass(complexCls)
1061+
case Class.INSTANCED_CLASS(elements = ClassTree.FLAT_TREE())
1062+
then list(makeTypeVar(c) for c guard not (InstNode.isOnlyOuter(c) or InstNode.isEmpty(c))
1063+
in ClassTree.getComponents(cls.elements));
10971064

1065+
else {};
10981066
end match;
1067+
end makeTypeVars;
10991068

1100-
end makeTypesVars;
1101-
1069+
function makeTypeVar
1070+
input InstNode component;
1071+
output DAE.Var typeVar;
1072+
protected
1073+
Component comp;
1074+
algorithm
1075+
comp := InstNode.component(InstNode.resolveOuter(component));
1076+
1077+
typeVar := DAE.TYPES_VAR(
1078+
InstNode.name(component),
1079+
Component.Attributes.toDAE(Component.getAttributes(comp)),
1080+
Type.toDAE(Component.getType(comp)),
1081+
Binding.toDAE(Component.getBinding(comp)),
1082+
NONE()
1083+
);
1084+
end makeTypeVar;
11021085

11031086
annotation(__OpenModelica_Interface="frontend");
11041087
end NFConvertDAE;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,13 @@ function getRecordBindings
344344
protected
345345
Expression binding_exp;
346346
list<Expression> expl;
347+
Variability var;
347348
algorithm
348349
binding_exp := Binding.getTypedExp(binding);
350+
var := Binding.variability(binding);
349351

350352
recordBindings := match binding_exp
351-
case Expression.RECORD() then list(Binding.FLAT_BINDING(e) for e in binding_exp.elements);
353+
case Expression.RECORD() then list(Binding.FLAT_BINDING(e, var) for e in binding_exp.elements);
352354
else
353355
algorithm
354356
Error.assertion(false, getInstanceName() + " got non-record binding " +

Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,32 +1242,76 @@ uniontype InstNode
12421242
end match;
12431243
end setModifier;
12441244

1245-
function toDAEType
1245+
function toPartialDAEType
1246+
"Returns the DAE type for a class, without the list of variables filled in."
12461247
input InstNode clsNode;
12471248
output DAE.Type outType;
12481249
algorithm
12491250
outType := match clsNode
12501251
local
12511252
Class cls;
1253+
ClassInf.State state;
1254+
1255+
case CLASS_NODE()
1256+
algorithm
1257+
cls := Pointer.access(clsNode.cls);
1258+
then
1259+
match cls
1260+
case Class.DAE_TYPE() then stripDAETypeVars(cls.ty);
1261+
1262+
else
1263+
algorithm
1264+
state := Restriction.toDAE(Class.restriction(cls), scopePath(clsNode));
1265+
then
1266+
DAE.Type.T_COMPLEX(state, {}, NONE());
1267+
1268+
end match;
1269+
end match;
1270+
end toPartialDAEType;
1271+
1272+
function stripDAETypeVars
1273+
input output DAE.Type ty;
1274+
algorithm
1275+
() := match ty
1276+
case DAE.Type.T_COMPLEX()
1277+
algorithm
1278+
ty.varLst := {};
1279+
then
1280+
();
1281+
1282+
else ();
1283+
end match;
1284+
end stripDAETypeVars;
1285+
1286+
function toFullDAEType
1287+
"Returns the DAE type for a class, with the list of variables filled in."
1288+
input InstNode clsNode;
1289+
output DAE.Type outType;
1290+
algorithm
1291+
outType := match clsNode
1292+
local
1293+
Class cls;
1294+
list<DAE.Var> vars;
1295+
ClassInf.State state;
12521296

12531297
case CLASS_NODE()
12541298
algorithm
12551299
cls := Pointer.access(clsNode.cls);
12561300
then
12571301
match cls
12581302
case Class.DAE_TYPE() then cls.ty;
1303+
12591304
else
12601305
algorithm
1261-
outType := DAE.Type.T_COMPLEX(
1262-
Restriction.toDAE(Class.restriction(cls), scopePath(clsNode)),
1263-
ConvertDAE.makeTypesVars(clsNode),
1264-
NONE());
1306+
state := Restriction.toDAE(Class.restriction(cls), scopePath(clsNode));
1307+
vars := ConvertDAE.makeTypeVars(clsNode);
1308+
outType := DAE.Type.T_COMPLEX(state, vars, NONE());
12651309
Pointer.update(clsNode.cls, Class.DAE_TYPE(outType));
12661310
then
12671311
outType;
12681312
end match;
12691313
end match;
1270-
end toDAEType;
1314+
end toFullDAEType;
12711315

12721316
function isBuiltin
12731317
input InstNode node;

Compiler/NFFrontEnd/NFScalarize.mo

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import MetaModelica.Dangerous.arrayCreateNoInit;
4848
import Variable = NFVariable;
4949
import NFComponent.Component;
5050
import NFPrefixes.Visibility;
51+
import NFPrefixes.Variability;
5152
import List;
5253
import ElementSource;
5354
import DAE;
@@ -96,6 +97,7 @@ protected
9697
Variable v;
9798
list<String> ty_attr_names;
9899
array<ExpressionIterator> ty_attr_iters;
100+
Variability bind_var;
99101
algorithm
100102
if Type.isArray(var.ty) then
101103
Variable.VARIABLE(name, ty, binding, vis, attr, ty_attr, cmt, info) := var;
@@ -110,10 +112,11 @@ algorithm
110112

111113
if Binding.isBound(binding) then
112114
binding_iter := ExpressionIterator.fromExp(Binding.getTypedExp(binding));
115+
bind_var := Binding.variability(binding);
113116

114117
for cr in crefs loop
115118
(binding_iter, exp) := ExpressionIterator.next(binding_iter);
116-
binding := Binding.FLAT_BINDING(exp);
119+
binding := Binding.FLAT_BINDING(exp, bind_var);
117120
ty_attr := nextTypeAttributes(ty_attr_names, ty_attr_iters);
118121
vars := Variable.VARIABLE(cr, ty, binding, vis, attr, ty_attr, cmt, info) :: vars;
119122
end for;
@@ -162,7 +165,7 @@ algorithm
162165
(iter, exp) := ExpressionIterator.next(iters[i]);
163166
arrayUpdate(iters, i, iter);
164167
i := i + 1;
165-
attrs := (name, Binding.FLAT_BINDING(exp)) :: attrs;
168+
attrs := (name, Binding.FLAT_BINDING(exp, Variability.PARAMETER)) :: attrs;
166169
end for;
167170
end nextTypeAttributes;
168171

Compiler/NFFrontEnd/NFType.mo

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ public
628628

629629
function toDAE
630630
input Type ty;
631+
input Boolean makeTypeVars = true;
631632
output DAE.Type daeTy;
632633
algorithm
633634
daeTy := match ty
@@ -638,15 +639,16 @@ public
638639
case Type.ENUMERATION() then DAE.T_ENUMERATION(NONE(), ty.typePath, ty.literals, {}, {});
639640
case Type.CLOCK() then DAE.T_CLOCK_DEFAULT;
640641
case Type.ARRAY()
641-
then DAE.T_ARRAY(toDAE(ty.elementType),
642+
then DAE.T_ARRAY(toDAE(ty.elementType, makeTypeVars),
642643
list(Dimension.toDAE(d) for d in ty.dimensions));
643644
case Type.TUPLE()
644645
then DAE.T_TUPLE(list(toDAE(t) for t in ty.types), ty.names);
645646
case Type.FUNCTION()
646-
then DAE.T_FUNCTION({} /*TODO:FIXME*/, toDAE(ty.resultType), ty.attributes, Absyn.IDENT("TODO:FIXME"));
647+
then DAE.T_FUNCTION({} /*TODO:FIXME*/, toDAE(ty.resultType, makeTypeVars), ty.attributes, Absyn.IDENT("TODO:FIXME"));
647648
case Type.NORETCALL() then DAE.T_NORETCALL_DEFAULT;
648649
case Type.UNKNOWN() then DAE.T_UNKNOWN_DEFAULT;
649-
case Type.COMPLEX() then InstNode.toDAEType(ty.cls);
650+
case Type.COMPLEX()
651+
then if makeTypeVars then InstNode.toFullDAEType(ty.cls) else InstNode.toPartialDAEType(ty.cls);
650652
case Type.POLYMORPHIC() then DAE.T_METAPOLYMORPHIC(ty.name);
651653
case Type.ANY() then DAE.T_ANYTYPE(NONE());
652654
else

Compiler/NFFrontEnd/NFTyping.mo

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

831831
exp := Ceval.evalExp(exp, Ceval.EvalTarget.CONDITION(info));
832832
then
833-
Binding.FLAT_BINDING(exp);
833+
Binding.FLAT_BINDING(exp, var);
834834

835835
end match;
836836
end typeComponentCondition;

0 commit comments

Comments
 (0)