| ... | ... | @@ -5,7 +5,7 @@ const assert = std.debug.assert; |
| 5 | 5 | const BuiltinFn = std.zig.BuiltinFn; |
| 6 | 6 | |
| 7 | 7 | ast: *const Ast, |
| 8 | | transformations: *std.array_list.Managed(Transformation), |
| 8 | transformations: *std.ArrayList(Transformation), |
| 9 | 9 | unreferenced_globals: std.array_hash_map.String(Ast.Node.Index), |
| 10 | 10 | in_scope_names: std.array_hash_map.String(u32), |
| 11 | 11 | replace_names: std.array_hash_map.String(u32), |
| ... | ... | @@ -53,15 +53,16 @@ pub const Error = error{OutOfMemory}; |
| 53 | 53 | /// The result will be priority shuffled. |
| 54 | 54 | pub fn findTransformations( |
| 55 | 55 | arena: std.mem.Allocator, |
| 56 | gpa: std.mem.Allocator, |
| 56 | 57 | ast: *const Ast, |
| 57 | | transformations: *std.array_list.Managed(Transformation), |
| 58 | transformations: *std.ArrayList(Transformation), |
| 58 | 59 | ) !void { |
| 59 | 60 | transformations.clearRetainingCapacity(); |
| 60 | 61 | |
| 61 | 62 | var walk: Walk = .{ |
| 62 | 63 | .ast = ast, |
| 63 | 64 | .transformations = transformations, |
| 64 | | .gpa = transformations.allocator, |
| 65 | .gpa = gpa, |
| 65 | 66 | .arena = arena, |
| 66 | 67 | .unreferenced_globals = .empty, |
| 67 | 68 | .in_scope_names = .empty, |
| ... | ... | @@ -76,7 +77,7 @@ pub fn findTransformations( |
| 76 | 77 | try walkMembers(&walk, walk.ast.rootDecls()); |
| 77 | 78 | |
| 78 | 79 | const unreferenced_globals = walk.unreferenced_globals.values(); |
| 79 | | try transformations.ensureUnusedCapacity(unreferenced_globals.len); |
| 80 | try transformations.ensureUnusedCapacity(gpa, unreferenced_globals.len); |
| 80 | 81 | for (unreferenced_globals) |node| { |
| 81 | 82 | transformations.appendAssumeCapacity(.{ .delete_node = node }); |
| 82 | 83 | } |
| ... | ... | @@ -148,7 +149,7 @@ fn walkMember(w: *Walk, decl: Ast.Node.Index) Error!void { |
| 148 | 149 | try walkExpression(w, fn_proto); |
| 149 | 150 | if (!isFnBodyGutted(ast, body_node)) { |
| 150 | 151 | w.replace_names.clearRetainingCapacity(); |
| 151 | | try w.transformations.append(.{ .gut_function = decl }); |
| 152 | try w.transformations.append(w.gpa, .{ .gut_function = decl }); |
| 152 | 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 | 168 | => try walkGlobalVarDecl(w, decl, ast.fullVarDecl(decl).?), |
| 168 | 169 | |
| 169 | 170 | .test_decl => { |
| 170 | | try w.transformations.append(.{ .delete_node = decl }); |
| 171 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 171 | 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 | 176 | .container_field_align, |
| 176 | 177 | .container_field, |
| 177 | 178 | => { |
| 178 | | try w.transformations.append(.{ .delete_node = decl }); |
| 179 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 179 | 180 | try walkContainerField(w, ast.fullContainerField(decl).?); |
| 180 | 181 | }, |
| 181 | 182 | |
| 182 | 183 | .@"comptime" => { |
| 183 | | try w.transformations.append(.{ .delete_node = decl }); |
| 184 | try w.transformations.append(w.gpa, .{ .delete_node = decl }); |
| 184 | 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 | 537 | |
| 537 | 538 | if (var_decl.ast.init_node.unwrap()) |init_node| { |
| 538 | 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 | 542 | try walkExpression(w, init_node); |
| 542 | 543 | } |
| ... | ... | @@ -563,7 +564,7 @@ fn walkLocalVarDecl(w: *Walk, var_decl: Ast.full.VarDecl) Error!void { |
| 563 | 564 | |
| 564 | 565 | if (var_decl.ast.init_node.unwrap()) |init_node| { |
| 565 | 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 | 569 | try walkExpression(w, init_node); |
| 569 | 570 | } |
| ... | ... | @@ -600,7 +601,7 @@ fn walkBlock( |
| 600 | 601 | if (var_decl.ast.init_node != .none and |
| 601 | 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 | 605 | .var_decl_node = stmt, |
| 605 | 606 | .references = .empty, |
| 606 | 607 | } }); |
| ... | ... | @@ -618,7 +619,7 @@ fn walkBlock( |
| 618 | 619 | .discard_identifier => {}, |
| 619 | 620 | // definitely try to remove `_ = undefined;` though. |
| 620 | 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 | 625 | try walkExpression(w, stmt); |
| ... | ... | @@ -720,7 +721,7 @@ fn walkBuiltinCall( |
| 720 | 721 | if (std.mem.endsWith(u8, token_bytes, ".zig\"")) { |
| 721 | 722 | const imported_string = std.zig.string_literal.parseAlloc(w.arena, token_bytes) catch |
| 722 | 723 | unreachable; |
| 723 | | try w.transformations.append(.{ .inline_imported_file = .{ |
| 724 | try w.transformations.append(w.gpa, .{ .inline_imported_file = .{ |
| 724 | 725 | .builtin_call_node = call_node, |
| 725 | 726 | .imported_string = imported_string, |
| 726 | 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 | 793 | if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and |
| 793 | 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 | 797 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr }); |
| 797 | 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 | 800 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr }); |
| 800 | 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 | 803 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 803 | 804 | .to_replace = node_index, |
| 804 | 805 | .replacement = while_node.ast.then_expr, |
| 805 | 806 | } }); |
| 806 | 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 | 809 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 809 | 810 | .to_replace = node_index, |
| 810 | 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 | 842 | if (!isTrueIdent(w.ast, if_node.ast.cond_expr) and |
| 842 | 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 | 846 | w.transformations.appendAssumeCapacity(.{ .replace_with_true = if_node.ast.cond_expr }); |
| 846 | 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 | 849 | w.transformations.appendAssumeCapacity(.{ .replace_with_false = if_node.ast.cond_expr }); |
| 849 | 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 | 852 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 852 | 853 | .to_replace = node_index, |
| 853 | 854 | .replacement = if_node.ast.then_expr, |
| 854 | 855 | } }); |
| 855 | 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 | 858 | w.transformations.appendAssumeCapacity(.{ .replace_node = .{ |
| 858 | 859 | .to_replace = node_index, |
| 859 | 860 | .replacement = if_node.ast.else_expr.unwrap().?, |