| ... | ... | @@ -54,8 +54,10 @@ fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Va |
| 54 | 54 | fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { |
| 55 | 55 | const tv = decl.typed_value.most_recent.typed_value; |
| 56 | 56 | try renderType(ctx, writer, tv.ty.fnReturnType()); |
| 57 | | const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name)); |
| 58 | | defer ctx.file.base.allocator.free(name); |
| 57 | // Use the child allocator directly, as we know the name can be freed before |
| 58 | // the rest of the arena. |
| 59 | const name = try map(ctx.arena.child_allocator, mem.spanZ(decl.name)); |
| 60 | defer ctx.arena.child_allocator.free(name); |
| 59 | 61 | try writer.print(" {}(", .{name}); |
| 60 | 62 | var param_len = tv.ty.fnParamLen(); |
| 61 | 63 | if (param_len == 0) |
| ... | ... | @@ -102,22 +104,18 @@ fn genArray(file: *C, decl: *Decl) !void { |
| 102 | 104 | const Context = struct { |
| 103 | 105 | file: *C, |
| 104 | 106 | decl: *Decl, |
| 105 | | inst_map: std.AutoHashMap(*Inst, []u8), |
| 107 | inst_map: *std.AutoHashMap(*Inst, []u8), |
| 108 | arena: *std.heap.ArenaAllocator, |
| 106 | 109 | argdex: usize = 0, |
| 107 | 110 | unnamed_index: usize = 0, |
| 108 | 111 | |
| 109 | 112 | fn name(self: *Context) ![]u8 { |
| 110 | | const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index}); |
| 113 | const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index}); |
| 111 | 114 | self.unnamed_index += 1; |
| 112 | 115 | return val; |
| 113 | 116 | } |
| 114 | 117 | |
| 115 | 118 | fn deinit(self: *Context) void { |
| 116 | | var it = self.inst_map.iterator(); |
| 117 | | while (it.next()) |kv| { |
| 118 | | self.file.base.allocator.free(kv.value); |
| 119 | | } |
| 120 | | self.inst_map.deinit(); |
| 121 | 119 | self.* = undefined; |
| 122 | 120 | } |
| 123 | 121 | }; |
| ... | ... | @@ -126,10 +124,15 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 126 | 124 | const writer = file.main.writer(); |
| 127 | 125 | const tv = decl.typed_value.most_recent.typed_value; |
| 128 | 126 | |
| 127 | var arena = std.heap.ArenaAllocator.init(file.base.allocator); |
| 128 | defer arena.deinit(); |
| 129 | var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator); |
| 130 | defer inst_map.deinit(); |
| 129 | 131 | var ctx = Context{ |
| 130 | 132 | .file = file, |
| 131 | 133 | .decl = decl, |
| 132 | | .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator), |
| 134 | .arena = &arena, |
| 135 | .inst_map = &inst_map, |
| 133 | 136 | }; |
| 134 | 137 | defer ctx.deinit(); |
| 135 | 138 | |
| ... | ... | @@ -163,7 +166,7 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 163 | 166 | } |
| 164 | 167 | |
| 165 | 168 | fn genArg(ctx: *Context) !?[]u8 { |
| 166 | | const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex}); |
| 169 | const name = try std.fmt.allocPrint(&ctx.arena.allocator, "arg{}", .{ctx.argdex}); |
| 167 | 170 | ctx.argdex += 1; |
| 168 | 171 | return name; |
| 169 | 172 | } |