Skip to content

Commit 028caa0

Browse files
committed
Improved (and simplified) for loop code generation
See commit comment thread: 2c64707
1 parent 9f215c3 commit 028caa0

17 files changed

+53
-76
lines changed

regression-tests/test-results/mixed-bounds-safety-with-assert-2.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
3131
std::vector<int> v {1, 2, 3, 4, 5};
3232
add_42_to_subrange(v, 1, 3);
3333

34-
{ auto const& cpp2_range = v; for ( auto const& i : cpp2_range )
35-
std::cout << i << "\n";}
36-
#line 8 "mixed-bounds-safety-with-assert-2.cpp2"
34+
for ( auto const& i : v )
35+
std::cout << i << "\n";
3736
}
3837

3938
auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> void
@@ -42,12 +41,11 @@ auto add_42_to_subrange(auto& rng, cpp2::in<int> start, cpp2::in<int> end) -> vo
4241
cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), "");
4342

4443
auto count {0};
45-
{ auto&& cpp2_range = rng; for (
44+
for (
4645

47-
auto& i : cpp2_range ) { do
46+
auto& i : rng ) { do
4847
if ([_0 = start, _1 = count, _2 = end]{ return cpp2::cmp_less_eq(_0,_1) && cpp2::cmp_less_eq(_1,_2); }()) {
4948
i += 42;
50-
} while (false); ++count; }}
51-
#line 22 "mixed-bounds-safety-with-assert-2.cpp2"
49+
} while (false); ++count; }
5250
}
5351

regression-tests/test-results/mixed-bounds-safety-with-assert.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ auto print_subrange(auto const& rng, cpp2::in<int> start, cpp2::in<int> end) ->
3838
cpp2::Bounds.expects(cpp2::cmp_less_eq(end,CPP2_UFCS_0(ssize, rng)), "");
3939

4040
auto count {0};
41-
{ auto const& cpp2_range = rng; for (
41+
for (
4242

43-
auto const& i : cpp2_range ) { do
43+
auto const& i : rng ) { do
4444
if (cpp2::cmp_less_eq(start,count) && cpp2::cmp_less_eq(count,end)) {
4545
std::cout << i << "\n";
46-
} while (false); ++count; }}
47-
#line 18 "mixed-bounds-safety-with-assert.cpp2"
46+
} while (false); ++count; }
4847
}
4948

regression-tests/test-results/mixed-fixed-type-aliases.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ auto test(auto const& x) -> void{
4444
cpp2::u16 z {42};
4545
test(std::move(z));
4646

47-
{ auto const& cpp2_range = args; for ( auto const& arg : cpp2_range )
48-
std::cout << arg << "\n";}
49-
#line 24 "mixed-fixed-type-aliases.cpp2"
47+
for ( auto const& arg : args )
48+
std::cout << arg << "\n";
5049
}
5150

regression-tests/test-results/mixed-function-expression-and-std-for-each.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@
4343
std::move(callback)
4444
);
4545

46-
{ auto const& cpp2_range = view; for ( auto const& str : cpp2_range ) {
46+
for ( auto const& str : view ) {
4747
std::cout << str << "\n";
48-
}}
49-
#line 30 "mixed-function-expression-and-std-for-each.cpp2"
48+
}
5049
}
5150

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each-with-capture.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
auto callback {[](auto& x) -> void { x += "-ish"; }};
3737
std::ranges::for_each(view, std::move(callback));
3838

39-
{ auto const& cpp2_range = view; for ( auto const& str : cpp2_range )
40-
std::cout << str << "\n";}
41-
#line 22 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2"
39+
for ( auto const& str : view )
40+
std::cout << str << "\n";
4241
}
4342

regression-tests/test-results/mixed-function-expression-and-std-ranges-for-each.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
auto callback {[](auto& x) -> void { x += "-ish"; }};
3636
std::ranges::for_each(view, std::move(callback));
3737

38-
{ auto const& cpp2_range = view; for ( auto const& str : cpp2_range )
39-
std::cout << str << "\n";}
40-
#line 21 "mixed-function-expression-and-std-ranges-for-each.cpp2"
38+
for ( auto const& str : view )
39+
std::cout << str << "\n";
4140
}
4241

regression-tests/test-results/mixed-function-expression-with-pointer-capture.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
auto callback {[](auto& x) -> void { x += "-ish"; }};
3838
std::ranges::for_each(view, std::move(callback));
3939

40-
{ auto const& cpp2_range = view; for ( auto const& str : cpp2_range )
41-
std::cout << str << "\n";}
42-
#line 23 "mixed-function-expression-with-pointer-capture.cpp2"
40+
for ( auto const& str : view )
41+
std::cout << str << "\n";
4342
}
4443

regression-tests/test-results/mixed-function-expression-with-repeated-capture.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
auto callback {[](auto& x) -> void { x += "-ish"; }};
3737
std::ranges::for_each(view, std::move(callback));
3838

39-
{ auto const& cpp2_range = view; for ( auto const& str : cpp2_range )
40-
std::cout << str << "\n";}
41-
#line 22 "mixed-function-expression-with-repeated-capture.cpp2"
39+
for ( auto const& str : view )
40+
std::cout << str << "\n";
4241
}
4342

regression-tests/test-results/mixed-intro-for-with-counter-include-last.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
{
2828
std::vector<int> v {1, 2, 3, 4, 5};
2929
auto counter {42};
30-
{ auto const& cpp2_range = v; for ( auto const& i : cpp2_range ) { do {
30+
for ( auto const& i : v ) { do {
3131
std::cout << i << " " << counter << "\n";
32-
} while (false); counter *= 2; }}
33-
#line 9 "mixed-intro-for-with-counter-include-last.cpp2"
32+
} while (false); counter *= 2; }
3433
}
3534

regression-tests/test-results/pure2-break-continue.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -253,87 +253,83 @@ auto for_continue_inner() -> void
253253
{
254254
std::vector vi {0, 1, 2};
255255
auto counter {0};
256-
{ auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { do {
256+
for ( auto const& i : vi ) { do {
257257
std::vector vj {0, 1, 2};
258-
{ auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) {
258+
for ( auto const& j : vj ) {
259259
#line 166 "pure2-break-continue.cpp2"
260260
{
261261
std::cout << i << j << " ";
262262
if (j==1) {
263263
goto CONTINUE_166_9;
264264
}
265265
std::cout << "inner ";
266-
} CPP2_CONTINUE_BREAK(166_9) }}
266+
} CPP2_CONTINUE_BREAK(166_9) }
267267

268268
#line 174 "pure2-break-continue.cpp2"
269269
std::cout << "outer ";
270-
} while (false); ++counter; }}
271-
#line 176 "pure2-break-continue.cpp2"
270+
} while (false); ++counter; }
272271
}
273272

274273
auto for_continue_outer() -> void
275274
{
276275
std::vector vi {0, 1, 2};
277276
auto counter {0};
278-
{ auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) {
277+
for ( auto const& i : vi ) {
279278
#line 182 "pure2-break-continue.cpp2"
280279
{ do {
281280
std::vector vj {0, 1, 2};
282-
{ auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) {
281+
for ( auto const& j : vj ) {
283282
std::cout << i << j << " ";
284283
if (j==1) {
285284
goto CONTINUE_182_5;
286285
}
287286
std::cout << "inner ";
288-
}}
287+
}
289288

290-
#line 192 "pure2-break-continue.cpp2"
291289
std::cout << "outer ";
292-
} while (false); ++counter; } CPP2_CONTINUE_BREAK(182_5) }}
290+
} while (false); ++counter; } CPP2_CONTINUE_BREAK(182_5) }
293291
#line 194 "pure2-break-continue.cpp2"
294292
}
295293

296294
auto for_break_inner() -> void
297295
{
298296
std::vector vi {0, 1, 2};
299297
auto counter {0};
300-
{ auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) { do {
298+
for ( auto const& i : vi ) { do {
301299
std::vector vj {0, 1, 2};
302-
{ auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) {
300+
for ( auto const& j : vj ) {
303301
#line 202 "pure2-break-continue.cpp2"
304302
{
305303
std::cout << i << j << " ";
306304
if (j==1) {
307305
goto BREAK_202_9;
308306
}
309307
std::cout << "inner ";
310-
} CPP2_CONTINUE_BREAK(202_9) }}
308+
} CPP2_CONTINUE_BREAK(202_9) }
311309

312310
#line 210 "pure2-break-continue.cpp2"
313311
std::cout << "outer ";
314-
} while (false); ++counter; }}
315-
#line 212 "pure2-break-continue.cpp2"
312+
} while (false); ++counter; }
316313
}
317314

318315
auto for_break_outer() -> void
319316
{
320317
std::vector vi {0, 1, 2};
321318
auto counter {0};
322-
{ auto const& cpp2_range = vi; for ( auto const& i : cpp2_range ) {
319+
for ( auto const& i : vi ) {
323320
#line 218 "pure2-break-continue.cpp2"
324321
{ do {
325322
std::vector vj {0, 1, 2};
326-
{ auto const& cpp2_range = vj; for ( auto const& j : cpp2_range ) {
323+
for ( auto const& j : vj ) {
327324
std::cout << i << j << " ";
328325
if (j==1) {
329326
goto BREAK_218_5;
330327
}
331328
std::cout << "inner ";
332-
}}
329+
}
333330

334-
#line 228 "pure2-break-continue.cpp2"
335331
std::cout << "outer ";
336-
} while (false); ++counter; } CPP2_CONTINUE_BREAK(218_5) }}
332+
} while (false); ++counter; } CPP2_CONTINUE_BREAK(218_5) }
337333
#line 230 "pure2-break-continue.cpp2"
338334
}
339335

regression-tests/test-results/pure2-intro-example-hello-2022.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ auto println(auto const& x, auto const& len) -> void;
3030
"hello", "2022"};
3131
std::span view {vec};
3232

33-
{ auto&& cpp2_range = view; for ( auto& str : cpp2_range ) {
33+
for ( auto& str : view ) {
3434
auto len {decorate(str)};
3535
println(str, len);
36-
}}
37-
#line 10 "pure2-intro-example-hello-2022.cpp2"
36+
}
3837
}
3938

4039
[[nodiscard]] auto decorate(auto& thing) -> int{

regression-tests/test-results/pure2-intro-example-three-loops.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ auto decorate_and_print(auto& thing) -> void{
5252
} while ( cpp2::cmp_greater(*cpp2::assert_not_null(i),1) && [&]{ --*cpp2::assert_not_null(i) ; return true; }() );
5353

5454
std::cout << "\n";
55-
{ auto&& cpp2_range = words; for ( auto& word : cpp2_range )
56-
decorate_and_print(word);}
55+
for ( auto& word : words )
56+
decorate_and_print(word);
5757

58-
#line 28 "pure2-intro-example-three-loops.cpp2"
5958
print(std::string{"end of program"});
6059
}
6160

regression-tests/test-results/pure2-statement-scope-parameters.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ auto const& i = local_int;
2828

2929
// 'in' (read-only) statement scope variable
3030
#line 6 "pure2-statement-scope-parameters.cpp2"
31-
{ auto const& cpp2_range = args; for ( auto const& arg : cpp2_range ) {
31+
for ( auto const& arg : args ) {
3232
std::cout << i << "\n"; // prints 42
33-
}}
33+
}
3434
}
3535
{
3636
auto& i = local_int;

regression-tests/test-results/pure2-type-and-namespace-aliases.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ auto myfunc() -> void{
6161

6262
auto const& v2 = std::move(v);
6363

64-
{ auto const& cpp2_range = v2; for ( auto const& s : cpp2_range )
65-
std::cout << cpp2::to_string(s) + "\n";}
66-
#line 24 "pure2-type-and-namespace-aliases.cpp2"
64+
for ( auto const& s : v2 )
65+
std::cout << cpp2::to_string(s) + "\n";
6766
}
6867

6968
auto main() -> int{

regression-tests/test-results/version

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11

2-
cppfront compiler v0.2.1 Build 8514:1707
2+
cppfront compiler v0.2.1 Build 8514:1724
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0
66
No commercial use
77
No forks/derivatives
8+
FAQ: Why? Because this is a personal experiment at this time
89

910
Absolutely no warranty - try at your own risk

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8514:1707"
1+
"8514:1724"

source/cppfront.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,16 +2129,11 @@ class cppfront
21292129
// but some major compilers seem to have random troubles with that;
21302130
// the workaround to avoid their bugs for now is to emit a { } block
21312131
// around the Cpp1 range-for and make the scope variable a normal local
2132-
if (n.for_with_in) {
2133-
printer.print_cpp2("{ auto const& cpp2_range = ", n.position());
2134-
}
2135-
else {
2136-
printer.print_cpp2("{ auto&& cpp2_range = ", n.position());
2137-
}
2138-
emit(*n.range);
2139-
printer.print_cpp2("; for ( ", n.position());
2132+
printer.print_cpp2("for ( ", n.position());
21402133
emit(*n.parameter);
2141-
printer.print_cpp2(" : cpp2_range ) ", n.position());
2134+
printer.print_cpp2(" : ", n.position());
2135+
emit(*n.range);
2136+
printer.print_cpp2(" ) ", n.position());
21422137
if (!labelname.empty()) {
21432138
printer.print_extra("{");
21442139
}
@@ -2164,8 +2159,6 @@ class cppfront
21642159
printer.print_extra(" CPP2_CONTINUE_BREAK("+labelname+") }");
21652160
}
21662161

2167-
printer.print_extra("}");
2168-
21692162
in_non_rvalue_context.pop_back();
21702163
}
21712164

0 commit comments

Comments
 (0)