| ... | @@ -5,7 +5,7 @@ const assert = std.debug.assert; | ... | @@ -5,7 +5,7 @@ const assert = std.debug.assert; |
| 5 | const BuiltinFn = std.zig.BuiltinFn; | 5 | const BuiltinFn = std.zig.BuiltinFn; |
| 6 | | 6 | |
| 7 | ast: *const Ast, | 7 | ast: *const Ast, |
| 8 | transformations: *std.array_list.Managed(Transformation), | 8 | transformations: *std.ArrayList(Transformation), |
| 9 | unreferenced_globals: std.array_hash_map.String(Ast.Node.Index), | 9 | unreferenced_globals: std.array_hash_map.String(Ast.Node.Index), |
| 10 | in_scope_names: std.array_hash_map.String(u32), | 10 | in_scope_names: std.array_hash_map.String(u32), |
| 11 | replace_names: std.array_hash_map.String(u32), | 11 | replace_names: std.array_hash_map.String(u32), |
| ... | @@ -53,15 +53,16 @@ pub const Error = error{OutOfMemory}; | ... | @@ -53,15 +53,16 @@ pub const Error = error{OutOfMemory}; |
| 53 | /// The result will be priority shuffled. | 53 | /// The result will be priority shuffled. |
| 54 | pub fn findTransformations( | 54 | pub fn findTransformations( |
| 55 | arena: std.mem.Allocator, | 55 | arena: std.mem.Allocator, |
| | 56 | gpa: std.mem.Allocator, |
| 56 | ast: *const Ast, | 57 | ast: *const Ast, |
| 57 | transformations: *std.array_list.Managed(Transformation), | 58 | transformations: *std.ArrayList(Transformation), |
| 58 | ) !void { | 59 | ) !void { |
| 59 | transformations.clearRetainingCapacity(); | 60 | transformations.clearRetainingCapacity(); |
| 60 | | 61 | |
| 61 | var walk: Walk = .{ | 62 | var walk: Walk = .{ |
| 62 | .ast = ast, | 63 | .ast = ast, |
| 63 | .transformations = transformations, | 64 | .transformations = transformations, |
| 64 | .gpa = transformations.allocator, | 65 | .gpa = gpa, |
| 65 | .arena = arena, | 66 | .arena = arena, |
| 66 | .unreferenced_globals = .empty, | 67 | .unreferenced_globals = .empty, |
| 67 | .in_scope_names = .empty, | 68 | .in_scope_names = .empty, |
| ... | @@ -76,7 +77,7 @@ pub fn findTransformations( | ... | @@ -76,7 +77,7 @@ pub fn findTransformations( |
| 76 | try walkMembers(&walk, walk.ast.rootDecls()); | 77 | try walkMembers(&walk, walk.ast.rootDecls()); |
| 77 | | 78 | |
| 78 | const unreferenced_globals = walk.unreferenced_globals.values(); | 79 | const unreferenced_globals = walk.unreferenced_globals.values(); |
| 79 | try transformations.ensureUnusedCapacity(unreferenced_globals.len); | 80 | try transformations.ensureUnusedCapacity(gpa, unreferenced_globals.len); |
| 80 | for (unreferenced_globals) |node| { | 81 | for (unreferenced_globals) |node| { |
| 81 | transformations.appendAssumeCapacity(.{ .delete_node = node }); | 82 | transformations.appendAssumeCapacity(.{ .delete_node = node }); |
| 82 | } | 83 | } |
| ... | @@ -148,7 +149,7 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { | ... | @@ -148,7 +149,7 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { |
| 148 | try walkExpression(w, fn_proto); | 149 | try walkExpression(w, fn_proto); |
| 149 | if (!isFnBodyGutted(ast, body_node)) { | 150 | if (!isFnBodyGutted(ast, body_node)) { |
| 150 | w.replace_names.clearRetainingCapacity(); | 151 | w.replace_names.clearRetainingCapacity(); |
| 151 | try w.transformations.append(.{ .gut_function = decl }); | 152 | try w.transformations.append(w.gpa, .{ .gut_function = decl }); |
| 152 | try walkExpression(w, body_node); | 153 | try walkExpression(w, body_node); |
| 153 | } | 154 | } |
| 154 | }, | 155 | }, |
| ... | @@ -167,7 +168,7 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { | ... | @@ -167,7 +168,7 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { |
| 167 | => try walkGlobalVarDecl(w, decl, ast.fullVarDecl(decl).?), | 168 | => try walkGlobalVarDecl(w, decl, ast.fullVarDecl(decl).?), |
| 168 | | 169 | |
| 169 | .test_decl => { | 170 | .test_decl => { |
| 170 | try w.transformations.append(.{ .delete_node = decl }); | 171 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 171 | try walkExpression(w, ast.nodeData(decl).opt_token_and_node[1]); | 172 | try walkExpression(w, ast.nodeData(decl).opt_token_and_node[1]); |
| 172 | }, | 173 | }, |
| 173 | | 174 | |
| ... | @@ -175,12 +176,12 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { | ... | @@ -175,12 +176,12 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { |
| 175 | .container_field_align, | 176 | .container_field_align, |
| 176 | .container_field, | 177 | .container_field, |
| 177 | => { | 178 | => { |
| 178 | try w.transformations.append(.{ .delete_node = decl }); | 179 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 179 | try walkContainerField(w, ast.fullContainerField(decl).?); | 180 | try walkContainerField(w, ast.fullContainerField(decl).?); |
| 180 | }, | 181 | }, |
| 181 | | 182 | |
| 182 | .@"comptime" => { | 183 | .@"comptime" => { |
| 183 | try w.transformations.append(.{ .delete_node = decl }); | 184 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 184 | try walkExpression(w, decl); | 185 | try walkExpression(w, decl); |
| 185 | }, | 186 | }, |
| 186 | | 187 | |
| ... | @@ -536,7 +537,7 @@ fn walkGlobalVarDecl(w: *Walk, decl_node: Ast.Node.Index, var_decl: Ast.full.Var | ... | @@ -536,7 +537,7 @@ fn walkGlobalVarDecl(w: *Walk, decl_node: Ast.Node.Index, var_decl: Ast.full.Var |
| 536 | | 537 | |
| 537 | if (var_decl.ast.init_node.unwrap()) |init_node| { | 538 | if (var_decl.ast.init_node.unwrap()) |init_node| { |
| 538 | if (!isUndefinedIdent(w.ast, init_node)) { | 539 | if (!isUndefinedIdent(w.ast, init_node)) { |
| 539 | try w.transformations.append(.{ .replace_with_undef = init_node }); | 540 | try w.transformations.append(w.gpa, .{ .replace_with_undef = init_node }); |
| 540 | } | 541 | } |
| 541 | try walkExpression(w, init_node); | 542 | try walkExpression(w, init_node); |
| 542 | } | 543 | } |
| ... | @@ -563,7 +564,7 @@ fn walkLocalVarDecl(w: *Walk, var_decl: Ast.full.VarDecl) Error!void { | ... | @@ -563,7 +564,7 @@ fn walkLocalVarDecl(w: *Walk, var_decl: Ast.full.VarDecl) Error!void { |
| 563 | | 564 | |
| 564 | if (var_decl.ast.init_node.unwrap()) |init_node| { | 565 | if (var_decl.ast.init_node.unwrap()) |init_node| { |
| 565 | if (!isUndefinedIdent(w.ast, init_node)) { | 566 | if (!isUndefinedIdent(w.ast, init_node)) { |
| 566 | try w.transformations.append(.{ .replace_with_undef = init_node }); | 567 | try w.transformations.append(w.gpa, .{ .replace_with_undef = init_node }); |
| 567 | } | 568 | } |
| 568 | try walkExpression(w, init_node); | 569 | try walkExpression(w, init_node); |
| 569 | } | 570 | } |
| ... | @@ -600,7 +601,7 @@ fn walkBlock( | ... | @@ -600,7 +601,7 @@ fn walkBlock( |
| 600 | if (var_decl.ast.init_node != .none and | 601 | if (var_decl.ast.init_node != .none and |
| 601 | isUndefinedIdent(w.ast, var_decl.ast.init_node.unwrap().?)) | 602 | isUndefinedIdent(w.ast, var_decl.ast.init_node.unwrap().?)) |
| 602 | { | 603 | { |
| 603 | try w.transformations.append(.{ .delete_var_decl = .{ | 604 | try w.transformations.append(w.gpa, .{ .delete_var_decl = .{ |
| 604 | .var_decl_node = stmt, | 605 | .var_decl_node = stmt, |
| 605 | .references = .empty, | 606 | .references = .empty, |
| 606 | } }); | 607 | } }); |
| ... | @@ -618,7 +619,7 @@ fn walkBlock( | ... | @@ -618,7 +619,7 @@ fn walkBlock( |
| 618 | .discard_identifier => {}, | 619 | .discard_identifier => {}, |
| 619 | // definitely try to remove `_ = undefined;` though. | 620 | // definitely try to remove `_ = undefined;` though. |
| 620 | .discard_undefined, .trap_call, .other => { | 621 | .discard_undefined, .trap_call, .other => { |
| 621 | try w.transformations.append(.{ .delete_node = stmt }); | 622 | try w.transformations.append(w.gpa, .{ .delete_node = stmt }); |
| 622 | }, | 623 | }, |
| 623 | } | 624 | } |
| 624 | try walkExpression(w, stmt); | 625 | try walkExpression(w, stmt); |
| ... | @@ -720,7 +721,7 @@ fn walkBuiltinCall( | ... | @@ -720,7 +721,7 @@ fn walkBuiltinCall( |
| 720 | if (std.mem.endsWith(u8, token_bytes, ".zig\"")) { | 721 | if (std.mem.endsWith(u8, token_bytes, ".zig\"")) { |
| 721 | const imported_string = std.zig.string_literal.parseAlloc(w.arena, token_bytes) catch | 722 | const imported_string = std.zig.string_literal.parseAlloc(w.arena, token_bytes) catch |
| 722 | unreachable; | 723 | unreachable; |
| 723 | try w.transformations.append(.{ .inline_imported_file = .{ | 724 | try w.transformations.append(w.gpa, .{ .inline_imported_file = .{ |
| 724 | .builtin_call_node = call_node, | 725 | .builtin_call_node = call_node, |
| 725 | .imported_string = imported_string, | 726 | .imported_string = imported_string, |
| 726 | .in_scope_names = try std.array_hash_map.String(void).init( | 727 | .in_scope_names = try std.array_hash_map.String(void).init( |
| ... | @@ -792,19 +793,19 @@ fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) E | ... | @@ -792,19 +793,19 @@ fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) E |
| 792 | if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and | 793 | if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and |
| 793 | (while_node.ast.else_expr == .none or isEmptyBlock(w.ast, while_node.ast.else_expr.unwrap().?))) | 794 | (while_node.ast.else_expr == .none or isEmptyBlock(w.ast, while_node.ast.else_expr.unwrap().?))) |
| 794 | { | 795 | { |
| 795 | try w.transformations.ensureUnusedCapacity(1); | 796 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 796 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); | 797 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); |
| 797 | } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) { | 798 | } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) { |
| 798 | try w.transformations.ensureUnusedCapacity(1); | 799 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 799 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); | 800 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); |
| 800 | } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) { | 801 | } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) { |
| 801 | try w.transformations.ensureUnusedCapacity(1); | 802 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 802 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ | 803 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 803 | .to_replace = node_index, | 804 | .to_replace = node_index, |
| 804 | .replacement = while_node.ast.then_expr, | 805 | .replacement = while_node.ast.then_expr, |
| 805 | } }); | 806 | } }); |
| 806 | } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) { | 807 | } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) { |
| 807 | try w.transformations.ensureUnusedCapacity(1); | 808 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 808 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ | 809 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 809 | .to_replace = node_index, | 810 | .to_replace = node_index, |
| 810 | .replacement = while_node.ast.else_expr.unwrap().?, | 811 | .replacement = while_node.ast.else_expr.unwrap().?, |
| ... | @@ -841,19 +842,19 @@ fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void | ... | @@ -841,19 +842,19 @@ fn walkIf(w: *Walk, node_index: Ast.Node.Index, if_node: Ast.full.If) Error!void |
| 841 | if (!isTrueIdent(w.ast, if_node.ast.cond_expr) and | 842 | if (!isTrueIdent(w.ast, if_node.ast.cond_expr) and |
| 842 | (if_node.ast.else_expr == .none or isEmptyBlock(w.ast, if_node.ast.else_expr.unwrap().?))) | 843 | (if_node.ast.else_expr == .none or isEmptyBlock(w.ast, if_node.ast.else_expr.unwrap().?))) |
| 843 | { | 844 | { |
| 844 | try w.transformations.ensureUnusedCapacity(1); | 845 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 845 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = if_node.ast.cond_expr }); | 846 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = if_node.ast.cond_expr }); |
| 846 | } else if (!isFalseIdent(w.ast, if_node.ast.cond_expr) and isEmptyBlock(w.ast, if_node.ast.then_expr)) { | 847 | } else if (!isFalseIdent(w.ast, if_node.ast.cond_expr) and isEmptyBlock(w.ast, if_node.ast.then_expr)) { |
| 847 | try w.transformations.ensureUnusedCapacity(1); | 848 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 848 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = if_node.ast.cond_expr }); | 849 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = if_node.ast.cond_expr }); |
| 849 | } else if (isTrueIdent(w.ast, if_node.ast.cond_expr)) { | 850 | } else if (isTrueIdent(w.ast, if_node.ast.cond_expr)) { |
| 850 | try w.transformations.ensureUnusedCapacity(1); | 851 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 851 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ | 852 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 852 | .to_replace = node_index, | 853 | .to_replace = node_index, |
| 853 | .replacement = if_node.ast.then_expr, | 854 | .replacement = if_node.ast.then_expr, |
| 854 | } }); | 855 | } }); |
| 855 | } else if (isFalseIdent(w.ast, if_node.ast.cond_expr)) { | 856 | } else if (isFalseIdent(w.ast, if_node.ast.cond_expr)) { |
| 856 | try w.transformations.ensureUnusedCapacity(1); | 857 | try w.transformations.ensureUnusedCapacity(w.gpa, 1); |
| 857 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ | 858 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 858 | .to_replace = node_index, | 859 | .to_replace = node_index, |
| 859 | .replacement = if_node.ast.else_expr.unwrap().?, | 860 | .replacement = if_node.ast.else_expr.unwrap().?, |