| ... | @@ -128,14 +128,29 @@ const Scope = struct { | ... | @@ -128,14 +128,29 @@ const Scope = struct { |
| 128 | | 128 | |
| 129 | /// Given the desired name, return a name that does not shadow anything from outer scopes. | 129 | /// Given the desired name, return a name that does not shadow anything from outer scopes. |
| 130 | /// Inserts the returned name into the scope. | 130 | /// Inserts the returned name into the scope. |
| | 131 | /// The name will not be visible to callers of getAlias. |
| | 132 | fn reserveMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { |
| | 133 | return scope.createMangledName(c, name, true); |
| | 134 | } |
| | 135 | |
| | 136 | /// Same as reserveMangledName, but enables the alias immediately. |
| 131 | fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { | 137 | fn makeMangledName(scope: *Block, c: *Context, name: []const u8) ![]const u8 { |
| | 138 | return scope.createMangledName(c, name, false); |
| | 139 | } |
| | 140 | |
| | 141 | fn createMangledName(scope: *Block, c: *Context, name: []const u8, reservation: bool) ![]const u8 { |
| 132 | const name_copy = try c.arena.dupe(u8, name); | 142 | const name_copy = try c.arena.dupe(u8, name); |
| 133 | var proposed_name = name_copy; | 143 | var proposed_name = name_copy; |
| 134 | while (scope.contains(proposed_name)) { | 144 | while (scope.contains(proposed_name)) { |
| 135 | scope.mangle_count += 1; | 145 | scope.mangle_count += 1; |
| 136 | proposed_name = try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ name, scope.mangle_count }); | 146 | proposed_name = try std.fmt.allocPrint(c.arena, "{s}_{d}", .{ name, scope.mangle_count }); |
| 137 | } | 147 | } |
| 138 | try scope.variables.append(.{ .name = name_copy, .alias = proposed_name }); | 148 | const new_mangle = try scope.variables.addOne(); |
| | 149 | if (reservation) { |
| | 150 | new_mangle.* = .{ .name = name_copy, .alias = name_copy }; |
| | 151 | } else { |
| | 152 | new_mangle.* = .{ .name = name_copy, .alias = proposed_name }; |
| | 153 | } |
| 139 | return proposed_name; | 154 | return proposed_name; |
| 140 | } | 155 | } |
| 141 | | 156 | |
| ... | @@ -3806,8 +3821,8 @@ fn transCreatePreCrement( | ... | @@ -3806,8 +3821,8 @@ fn transCreatePreCrement( |
| 3806 | // zig: }) | 3821 | // zig: }) |
| 3807 | var block_scope = try Scope.Block.init(c, scope, true); | 3822 | var block_scope = try Scope.Block.init(c, scope, true); |
| 3808 | defer block_scope.deinit(); | 3823 | defer block_scope.deinit(); |
| 3809 | const ref = try block_scope.makeMangledName(c, "ref"); | | |
| 3810 | | 3824 | |
| | 3825 | const ref = try block_scope.reserveMangledName(c, "ref"); |
| 3811 | const expr = try transExpr(c, &block_scope.base, op_expr, .used); | 3826 | const expr = try transExpr(c, &block_scope.base, op_expr, .used); |
| 3812 | const addr_of = try Tag.address_of.create(c.arena, expr); | 3827 | const addr_of = try Tag.address_of.create(c.arena, expr); |
| 3813 | const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = ref, .init = addr_of }); | 3828 | const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = ref, .init = addr_of }); |
| ... | @@ -3853,7 +3868,8 @@ fn transCreatePostCrement( | ... | @@ -3853,7 +3868,8 @@ fn transCreatePostCrement( |
| 3853 | // zig: }) | 3868 | // zig: }) |
| 3854 | var block_scope = try Scope.Block.init(c, scope, true); | 3869 | var block_scope = try Scope.Block.init(c, scope, true); |
| 3855 | defer block_scope.deinit(); | 3870 | defer block_scope.deinit(); |
| 3856 | const ref = try block_scope.makeMangledName(c, "ref"); | 3871 | const ref = try block_scope.reserveMangledName(c, "ref"); |
| | 3872 | const tmp = try block_scope.reserveMangledName(c, "tmp"); |
| 3857 | | 3873 | |
| 3858 | const expr = try transExpr(c, &block_scope.base, op_expr, .used); | 3874 | const expr = try transExpr(c, &block_scope.base, op_expr, .used); |
| 3859 | const addr_of = try Tag.address_of.create(c.arena, expr); | 3875 | const addr_of = try Tag.address_of.create(c.arena, expr); |
| ... | @@ -3863,7 +3879,6 @@ fn transCreatePostCrement( | ... | @@ -3863,7 +3879,6 @@ fn transCreatePostCrement( |
| 3863 | const lhs_node = try Tag.identifier.create(c.arena, ref); | 3879 | const lhs_node = try Tag.identifier.create(c.arena, ref); |
| 3864 | const ref_node = try Tag.deref.create(c.arena, lhs_node); | 3880 | const ref_node = try Tag.deref.create(c.arena, lhs_node); |
| 3865 | | 3881 | |
| 3866 | const tmp = try block_scope.makeMangledName(c, "tmp"); | | |
| 3867 | const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = ref_node }); | 3882 | const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = ref_node }); |
| 3868 | try block_scope.statements.append(tmp_decl); | 3883 | try block_scope.statements.append(tmp_decl); |
| 3869 | | 3884 | |
| ... | @@ -3968,7 +3983,7 @@ fn transCreateCompoundAssign( | ... | @@ -3968,7 +3983,7 @@ fn transCreateCompoundAssign( |
| 3968 | // zig: }) | 3983 | // zig: }) |
| 3969 | var block_scope = try Scope.Block.init(c, scope, true); | 3984 | var block_scope = try Scope.Block.init(c, scope, true); |
| 3970 | defer block_scope.deinit(); | 3985 | defer block_scope.deinit(); |
| 3971 | const ref = try block_scope.makeMangledName(c, "ref"); | 3986 | const ref = try block_scope.reserveMangledName(c, "ref"); |
| 3972 | | 3987 | |
| 3973 | const expr = try transExpr(c, &block_scope.base, lhs, .used); | 3988 | const expr = try transExpr(c, &block_scope.base, lhs, .used); |
| 3974 | const addr_of = try Tag.address_of.create(c.arena, expr); | 3989 | const addr_of = try Tag.address_of.create(c.arena, expr); |
| ... | @@ -4098,9 +4113,9 @@ fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang | ... | @@ -4098,9 +4113,9 @@ fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang |
| 4098 | var block_scope = try Scope.Block.init(c, scope, true); | 4113 | var block_scope = try Scope.Block.init(c, scope, true); |
| 4099 | defer block_scope.deinit(); | 4114 | defer block_scope.deinit(); |
| 4100 | | 4115 | |
| 4101 | const mangled_name = try block_scope.makeMangledName(c, "cond_temp"); | 4116 | const cond_temp = try block_scope.reserveMangledName(c, "cond_temp"); |
| 4102 | const init_node = try transExpr(c, &block_scope.base, cond_expr, .used); | 4117 | const init_node = try transExpr(c, &block_scope.base, cond_expr, .used); |
| 4103 | const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = mangled_name, .init = init_node }); | 4118 | const ref_decl = try Tag.var_simple.create(c.arena, .{ .name = cond_temp, .init = init_node }); |
| 4104 | try block_scope.statements.append(ref_decl); | 4119 | try block_scope.statements.append(ref_decl); |
| 4105 | | 4120 | |
| 4106 | var cond_scope = Scope.Condition{ | 4121 | var cond_scope = Scope.Condition{ |
| ... | @@ -4111,7 +4126,7 @@ fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang | ... | @@ -4111,7 +4126,7 @@ fn transBinaryConditionalOperator(c: *Context, scope: *Scope, stmt: *const clang |
| 4111 | }; | 4126 | }; |
| 4112 | defer cond_scope.deinit(); | 4127 | defer cond_scope.deinit(); |
| 4113 | | 4128 | |
| 4114 | const cond_ident = try Tag.identifier.create(c.arena, mangled_name); | 4129 | const cond_ident = try Tag.identifier.create(c.arena, cond_temp); |
| 4115 | const ty = getExprQualType(c, cond_expr).getTypePtr(); | 4130 | const ty = getExprQualType(c, cond_expr).getTypePtr(); |
| 4116 | const cond_node = try finishBoolExpr(c, &cond_scope.base, cond_expr.getBeginLoc(), ty, cond_ident, .used); | 4131 | const cond_node = try finishBoolExpr(c, &cond_scope.base, cond_expr.getBeginLoc(), ty, cond_ident, .used); |
| 4117 | var then_body = cond_ident; | 4132 | var then_body = cond_ident; |
| ... | @@ -4552,11 +4567,12 @@ fn transCreateNodeAssign( | ... | @@ -4552,11 +4567,12 @@ fn transCreateNodeAssign( |
| 4552 | var block_scope = try Scope.Block.init(c, scope, true); | 4567 | var block_scope = try Scope.Block.init(c, scope, true); |
| 4553 | defer block_scope.deinit(); | 4568 | defer block_scope.deinit(); |
| 4554 | | 4569 | |
| 4555 | const tmp = try block_scope.makeMangledName(c, "tmp"); | 4570 | const tmp = try block_scope.reserveMangledName(c, "tmp"); |
| 4556 | var rhs_node = try transExpr(c, &block_scope.base, rhs, .used); | 4571 | var rhs_node = try transExpr(c, &block_scope.base, rhs, .used); |
| 4557 | if (!exprIsBooleanType(lhs) and isBoolRes(rhs_node)) { | 4572 | if (!exprIsBooleanType(lhs) and isBoolRes(rhs_node)) { |
| 4558 | rhs_node = try Tag.bool_to_int.create(c.arena, rhs_node); | 4573 | rhs_node = try Tag.bool_to_int.create(c.arena, rhs_node); |
| 4559 | } | 4574 | } |
| | 4575 | |
| 4560 | const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = rhs_node }); | 4576 | const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = rhs_node }); |
| 4561 | try block_scope.statements.append(tmp_decl); | 4577 | try block_scope.statements.append(tmp_decl); |
| 4562 | | 4578 | |