Skip to content

Commit d84fc85

Browse files
authored
Improve record constructor creation. (#717)
- Make sure we do not remove bindings from derived record declarations. This happens only for the old FrontEnd right now. - Improve processing a bit. Instead of checking twice in the list of already visited records, create the unique name for the constructor and then check only once.
1 parent 6c78ddb commit d84fc85

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

OMCompiler/Compiler/FrontEnd/Inst.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ function varIsModifiedInDerivedMod
10221022
output Boolean b;
10231023
algorithm
10241024
b := match inSubmod
1025+
case SCode.NAMEMOD(mod=SCode.REDECL()) then false;
10251026
case SCode.NAMEMOD() then stringEqual(inSubmod.ident, inName);
10261027
end match;
10271028
end varIsModifiedInDerivedMod;
@@ -3786,7 +3787,7 @@ algorithm
37863787
DAE.Binding bind;
37873788
DAE.Prefix pref;
37883789

3789-
case (bind as DAE.EQBOUND(), pref as DAE.PREFIX(compPre=DAE.PRE())) algorithm
3790+
case (bind as DAE.EQBOUND(), pref as DAE.PREFIX(compPre=DAE.PRE())) algorithm
37903791
bind.exp := PrefixUtil.removeCompPrefixFromExps(bind.exp, pref.compPre);
37913792
then
37923793
bind;

OMCompiler/Compiler/SimCode/SimCodeFunctionUtil.mo

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,38 +1608,32 @@ algorithm
16081608
list<SimCodeFunction.Variable> vars;
16091609
Integer varnum;
16101610
SimCodeFunction.RecordDeclaration recDecl;
1611+
Boolean is_default;
16111612

16121613
case (DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(path), varLst = varlst), accRecDecls, rt)
16131614
algorithm
16141615
name := AbsynUtil.pathStringUnquoteReplaceDot(path, "_");
16151616
rt_1 := rt;
16161617

1617-
if not listMember(name, rt_1) then
1618-
rt_1 := name :: rt_1;
1619-
(accRecDecls, rt_1) := elaborateNestedRecordDeclarations(varlst, accRecDecls, rt_1);
1618+
(sname, is_default) := checkBindingsandGetConstructorName(name, varlst);
1619+
// is_default := stringEqual(sname,name);
16201620

1621-
varlst := List.map(varlst, simCodeVarRemoveBindFromOutside);
1622-
vars := List.map(varlst, typesVar);
1623-
// vars := List.map(vars, simCodeVarRemoveBindFromOutside);
1624-
recDecl := SimCodeFunction.RECORD_DECL_FULL(name, NONE(), path, vars);
1625-
accRecDecls := List.appendElt(recDecl, accRecDecls);
1626-
end if;
1621+
if not listMember(sname, rt_1) then
1622+
rt_1 := sname :: rt_1;
1623+
1624+
if is_default then
1625+
(accRecDecls, rt_1) := elaborateNestedRecordDeclarations(varlst, accRecDecls, rt_1);
16271626

1628-
sname := name;
1629-
varnum := 1;
1630-
for var in varlst loop
1631-
if var.bind_from_outside then
1632-
sname := sname + "_" + intString(varnum);
1627+
vars := List.map(varlst, typesVar);
1628+
recDecl := SimCodeFunction.RECORD_DECL_FULL(sname, NONE(), path, vars);
1629+
else
1630+
vars := List.map(varlst, typesVar);
1631+
recDecl := SimCodeFunction.RECORD_DECL_ADD_CONSTRCTOR(sname, name, vars);
16331632
end if;
1634-
varnum := intAdd(varnum,1);
1635-
end for;
16361633

1637-
if not listMember(sname, rt_1) then
1638-
rt_1 := sname :: rt_1;
1639-
vars := List.map(varlst, typesVar);
1640-
recDecl := SimCodeFunction.RECORD_DECL_ADD_CONSTRCTOR(sname, name, vars);
16411634
accRecDecls := List.appendElt(recDecl, accRecDecls);
16421635
end if;
1636+
16431637
then (accRecDecls, rt_1);
16441638

16451639
case (DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_)), accRecDecls, rt)
@@ -1715,18 +1709,38 @@ algorithm
17151709
end match;
17161710
end typesVar;
17171711

1718-
protected function simCodeVarRemoveBindFromOutside
1719-
input output DAE.Var var;
1712+
protected function checkBindingsandGetConstructorName
1713+
input String rec_name;
1714+
input list<DAE.Var> vars;
1715+
output String ctor_name;
1716+
output Boolean is_default;
1717+
protected
1718+
Integer varnum;
17201719
algorithm
1721-
_ := match (var)
1722-
case DAE.TYPES_VAR(binding = DAE.EQBOUND(source=DAE.BINDING_FROM_DERIVED_RECORD_DECL()))
1723-
then ();
1724-
case DAE.TYPES_VAR()
1725-
algorithm
1726-
var.bind_from_outside := false;
1727-
then ();
1720+
is_default := true;
1721+
1722+
ctor_name := rec_name;
1723+
varnum := 1;
1724+
1725+
for var in vars loop
1726+
if var.bind_from_outside and not isBindingFromDerivedRecordDeclaration(var.binding) then
1727+
is_default := false;
1728+
ctor_name := ctor_name + "_" + intString(varnum);
1729+
end if;
1730+
1731+
varnum := intAdd(varnum,1);
1732+
end for;
1733+
end checkBindingsandGetConstructorName;
1734+
1735+
protected function isBindingFromDerivedRecordDeclaration
1736+
input DAE.Binding bind;
1737+
output Boolean b;
1738+
algorithm
1739+
b := match bind
1740+
case DAE.EQBOUND(source=DAE.BINDING_FROM_DERIVED_RECORD_DECL()) then true;
1741+
else false;
17281742
end match;
1729-
end simCodeVarRemoveBindFromOutside;
1743+
end isBindingFromDerivedRecordDeclaration;
17301744

17311745
protected function checkSourceAndGetBindingExp
17321746
input DAE.Binding inBinding;

0 commit comments

Comments
 (0)