Skip to content

Commit 485a2c2

Browse files
committed
Extend multivarinit to inline var type inference
var i, z := 4; var s1, s2 := 'hello'; Expression is evaluated once into a temp, then assigned to each variable. Type promotion applied uniformly to all variables.
1 parent 92ff3d0 commit 485a2c2

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

compiler/pstatmnt.pas

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,9 @@ ((p.resultdef.typ=recorddef) or
19051905
end
19061906
else if current_scanner.token = _ASSIGNMENT then
19071907
begin
1908-
{ Type inference: var x := expr }
1909-
if sc.count > 1 then
1908+
{ Type inference: var x := expr or var x, y := expr }
1909+
if (sc.count > 1) and
1910+
not(m_multi_var_init in current_settings.modeswitches) then
19101911
Message(parser_e_initialized_only_one_var);
19111912
consume(_ASSIGNMENT);
19121913
vs := tabstractnormalvarsym(sc[0]);
@@ -1949,13 +1950,30 @@ ((p.resultdef.typ=recorddef) or
19491950
if not(nf_explicit in initexpr.flags) and is_integer(hdef) and
19501951
(torddef(hdef).ordtype in [s8bit,u8bit,s16bit,u16bit]) then
19511952
hdef := s32inttype;
1952-
vs.vardef := hdef;
1953-
vs.varstate := vs_initialised;
1954-
if vs.typ = staticvarsym then
1955-
cnodeutils.insertbssdata(tstaticvarsym(vs));
1956-
result := cassignmentnode.create(
1957-
cloadnode.create(vs, vs.owner),
1958-
initexpr);
1953+
for i := 0 to sc.count - 1 do
1954+
begin
1955+
tabstractnormalvarsym(sc[i]).vardef := hdef;
1956+
tabstractnormalvarsym(sc[i]).varstate := vs_initialised;
1957+
if tsym(sc[i]).typ = staticvarsym then
1958+
cnodeutils.insertbssdata(tstaticvarsym(sc[i]));
1959+
end;
1960+
if sc.count = 1 then
1961+
result := cassignmentnode.create(
1962+
cloadnode.create(vs, vs.owner),
1963+
initexpr)
1964+
else
1965+
begin
1966+
result := internalstatements(statements);
1967+
tempnode := ctempcreatenode.create(hdef, hdef.size, tt_persistent, true);
1968+
addstatement(statements, tempnode);
1969+
addstatement(statements, cassignmentnode.create(
1970+
ctemprefnode.create(tempnode), initexpr));
1971+
for i := 0 to sc.count - 1 do
1972+
addstatement(statements, cassignmentnode.create(
1973+
cloadnode.create(tsym(sc[i]), tsym(sc[i]).owner),
1974+
ctemprefnode.create(tempnode)));
1975+
addstatement(statements, ctempdeletenode.create(tempnode));
1976+
end;
19591977
end;
19601978
end
19611979
else

0 commit comments

Comments
 (0)