|
74 | 74 | // inside_if_expr bool |
75 | 75 | ternary_names map[string]string |
76 | 76 | ternary_level_names map[string][]string |
77 | | - stmt_path_pos []int |
| 77 | + stmt_path_pos []int // positions of each statement start, for inserting C statements before the current statement |
| 78 | + skip_stmt_pos bool // for handling if expressions + autofree (since both prepend C statements) |
78 | 79 | right_is_opt bool |
79 | 80 | autofree bool |
80 | 81 | indent int |
@@ -725,9 +726,12 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { |
725 | 726 | for i, stmt in stmts { |
726 | 727 | if i == stmts.len - 1 && tmp_var != '' { |
727 | 728 | // Handle if expressions, set the value of the last expression to the temp var. |
| 729 | + g.stmt_path_pos << g.out.len |
| 730 | + g.skip_stmt_pos = true |
728 | 731 | g.writeln('$tmp_var = /* if expr set */') |
729 | 732 | } |
730 | 733 | g.stmt(stmt) |
| 734 | + g.skip_stmt_pos = false |
731 | 735 | if g.inside_ternary > 0 && i < stmts.len - 1 { |
732 | 736 | g.write(',') |
733 | 737 | } |
@@ -761,7 +765,9 @@ fn (mut g Gen) write_v_source_line_info(pos token.Position) { |
761 | 765 | } |
762 | 766 |
|
763 | 767 | fn (mut g Gen) stmt(node ast.Stmt) { |
764 | | - g.stmt_path_pos << g.out.len |
| 768 | + if !g.skip_stmt_pos { |
| 769 | + g.stmt_path_pos << g.out.len |
| 770 | + } |
765 | 771 | defer { |
766 | 772 | // If we have temporary string exprs to free after this statement, do it. e.g.: |
767 | 773 | // `foo('a' + 'b')` => `tmp := 'a' + 'b'; foo(tmp); string_free(&tmp);` |
@@ -1047,7 +1053,9 @@ fn (mut g Gen) stmt(node ast.Stmt) { |
1047 | 1053 | g.writeln('// TypeDecl') |
1048 | 1054 | } |
1049 | 1055 | } |
1050 | | - g.stmt_path_pos.delete_last() |
| 1056 | + if !g.skip_stmt_pos { // && g.stmt_path_pos.len > 0 { |
| 1057 | + g.stmt_path_pos.delete_last() |
| 1058 | + } |
1051 | 1059 | } |
1052 | 1060 |
|
1053 | 1061 | fn (mut g Gen) write_defer_stmts() { |
|
0 commit comments