| author | |
| committer | |
| log | 42ade6a11443d50bec1131283899e1a9d77c01d6 |
| tree | a6c7dee0b50a9a05c338e17002e9c0ba66486dcc |
| parent | 259f407160f84a1bf33c1aa7bd10318213f52ddf |
| parent | ab3b614a335ffac9eac4f824ee18fba262ad988e |
| signature |
Replace param_names and anytype_args fields inside of Fn with functions9 files changed, 64 insertions(+), 43 deletions(-)
src/Module.zig+35-22| ... | ... | @@ -1464,20 +1464,8 @@ pub const Fn = struct { |
| 1464 | 1464 | /// These never have .generic_poison for the Type |
| 1465 | 1465 | /// because the Type is needed to pass to `Type.eql` and for inserting comptime arguments |
| 1466 | 1466 | /// into the inst_map when analyzing the body of a generic function instantiation. |
| 1467 | /// Instead, the is_anytype knowledge is communicated via `anytype_args`. | |
| 1467 | /// Instead, the is_anytype knowledge is communicated via `isAnytypeParam`. | |
| 1468 | 1468 | comptime_args: ?[*]TypedValue, |
| 1469 | /// When comptime_args is null, this is undefined. Otherwise, this flags each | |
| 1470 | /// parameter and tells whether it is anytype. | |
| 1471 | /// TODO apply the same enhancement for param_names below to this field. | |
| 1472 | anytype_args: [*]bool, | |
| 1473 | ||
| 1474 | /// Prefer to use `getParamName` to access this because of the future improvement | |
| 1475 | /// we want to do mentioned in the TODO below. | |
| 1476 | /// Stored in gpa. | |
| 1477 | /// TODO: change param ZIR instructions to be embedded inside the function | |
| 1478 | /// ZIR instruction instead of before it, so that `zir_body_inst` can be used to | |
| 1479 | /// determine param names rather than redundantly storing them here. | |
| 1480 | param_names: []const [:0]const u8, | |
| 1481 | 1469 | |
| 1482 | 1470 | /// Precomputed hash for monomorphed_funcs. |
| 1483 | 1471 | /// This is important because it may be accessed when resizing monomorphed_funcs |
| ... | ... | @@ -1590,18 +1578,43 @@ pub const Fn = struct { |
| 1590 | 1578 | gpa.destroy(node); |
| 1591 | 1579 | it = next; |
| 1592 | 1580 | } |
| 1581 | } | |
| 1593 | 1582 | |
| 1594 | for (func.param_names) |param_name| { | |
| 1595 | gpa.free(param_name); | |
| 1596 | } | |
| 1597 | gpa.free(func.param_names); | |
| 1583 | pub fn isAnytypeParam(func: Fn, mod: *Module, index: u32) bool { | |
| 1584 | const file = mod.declPtr(func.owner_decl).getFileScope(); | |
| 1585 | ||
| 1586 | const tags = file.zir.instructions.items(.tag); | |
| 1587 | ||
| 1588 | const param_body = file.zir.getParamBody(func.zir_body_inst); | |
| 1589 | const param = param_body[index]; | |
| 1590 | ||
| 1591 | return switch (tags[param]) { | |
| 1592 | .param, .param_comptime => false, | |
| 1593 | .param_anytype, .param_anytype_comptime => true, | |
| 1594 | else => unreachable, | |
| 1595 | }; | |
| 1598 | 1596 | } |
| 1599 | 1597 | |
| 1600 | pub fn getParamName(func: Fn, index: u32) [:0]const u8 { | |
| 1601 | // TODO rework ZIR of parameters so that this function looks up | |
| 1602 | // param names in ZIR instead of redundantly saving them into Fn. | |
| 1603 | // const zir = func.owner_decl.getFileScope().zir; | |
| 1604 | return func.param_names[index]; | |
| 1598 | pub fn getParamName(func: Fn, mod: *Module, index: u32) [:0]const u8 { | |
| 1599 | const file = mod.declPtr(func.owner_decl).getFileScope(); | |
| 1600 | ||
| 1601 | const tags = file.zir.instructions.items(.tag); | |
| 1602 | const data = file.zir.instructions.items(.data); | |
| 1603 | ||
| 1604 | const param_body = file.zir.getParamBody(func.zir_body_inst); | |
| 1605 | const param = param_body[index]; | |
| 1606 | ||
| 1607 | return switch (tags[param]) { | |
| 1608 | .param, .param_comptime => blk: { | |
| 1609 | const extra = file.zir.extraData(Zir.Inst.Param, data[param].pl_tok.payload_index); | |
| 1610 | break :blk file.zir.nullTerminatedString(extra.data.name); | |
| 1611 | }, | |
| 1612 | .param_anytype, .param_anytype_comptime => blk: { | |
| 1613 | const param_data = data[param].str_tok; | |
| 1614 | break :blk param_data.get(file.zir); | |
| 1615 | }, | |
| 1616 | else => unreachable, | |
| 1617 | }; | |
| 1605 | 1618 | } |
| 1606 | 1619 | |
| 1607 | 1620 | pub fn hasInferredErrorSet(func: Fn, mod: *Module) bool { |
src/Sema.zig+2-15| ... | ... | @@ -5506,7 +5506,7 @@ const GenericCallAdapter = struct { |
| 5506 | 5506 | const this_is_comptime = this_arg.val.tag() != .generic_poison; |
| 5507 | 5507 | const other_is_comptime = other_arg.val.tag() != .generic_poison; |
| 5508 | 5508 | const this_is_anytype = this_arg.ty.tag() != .generic_poison; |
| 5509 | const other_is_anytype = other_key.anytype_args[i]; | |
| 5509 | const other_is_anytype = other_key.isAnytypeParam(ctx.module, @intCast(u32, i)); | |
| 5510 | 5510 | |
| 5511 | 5511 | if (other_is_anytype != this_is_anytype) return false; |
| 5512 | 5512 | if (other_is_comptime != this_is_comptime) return false; |
| ... | ... | @@ -6386,12 +6386,9 @@ fn instantiateGenericCall( |
| 6386 | 6386 | errdefer new_func.deinit(gpa); |
| 6387 | 6387 | assert(new_func == new_module_func); |
| 6388 | 6388 | |
| 6389 | const anytype_args = try new_decl_arena_allocator.alloc(bool, func_ty_info.param_types.len); | |
| 6390 | new_func.anytype_args = anytype_args.ptr; | |
| 6391 | 6389 | arg_i = 0; |
| 6392 | 6390 | for (fn_info.param_body) |inst| { |
| 6393 | 6391 | var is_comptime = false; |
| 6394 | var is_anytype = false; | |
| 6395 | 6392 | switch (zir_tags[inst]) { |
| 6396 | 6393 | .param => { |
| 6397 | 6394 | is_comptime = func_ty_info.paramIsComptime(arg_i); |
| ... | ... | @@ -6400,11 +6397,9 @@ fn instantiateGenericCall( |
| 6400 | 6397 | is_comptime = true; |
| 6401 | 6398 | }, |
| 6402 | 6399 | .param_anytype => { |
| 6403 | is_anytype = true; | |
| 6404 | 6400 | is_comptime = func_ty_info.paramIsComptime(arg_i); |
| 6405 | 6401 | }, |
| 6406 | 6402 | .param_anytype_comptime => { |
| 6407 | is_anytype = true; | |
| 6408 | 6403 | is_comptime = true; |
| 6409 | 6404 | }, |
| 6410 | 6405 | else => continue, |
| ... | ... | @@ -6412,10 +6407,9 @@ fn instantiateGenericCall( |
| 6412 | 6407 | |
| 6413 | 6408 | // We populate the Type here regardless because it is needed by |
| 6414 | 6409 | // `GenericCallAdapter.eql` as well as function body analysis. |
| 6415 | // Whether it is anytype is communicated by `anytype_args`. | |
| 6410 | // Whether it is anytype is communicated by `isAnytypeParam`. | |
| 6416 | 6411 | const arg = child_sema.inst_map.get(inst).?; |
| 6417 | 6412 | const copied_arg_ty = try child_sema.typeOf(arg).copy(new_decl_arena_allocator); |
| 6418 | anytype_args[arg_i] = is_anytype; | |
| 6419 | 6413 | |
| 6420 | 6414 | if (try sema.typeRequiresComptime(block, .unneeded, copied_arg_ty)) { |
| 6421 | 6415 | is_comptime = true; |
| ... | ... | @@ -7760,11 +7754,6 @@ fn funcCommon( |
| 7760 | 7754 | break :blk if (sema.comptime_args.len == 0) null else sema.comptime_args.ptr; |
| 7761 | 7755 | } else null; |
| 7762 | 7756 | |
| 7763 | const param_names = try sema.gpa.alloc([:0]const u8, block.params.items.len); | |
| 7764 | for (param_names) |*param_name, i| { | |
| 7765 | param_name.* = try sema.gpa.dupeZ(u8, block.params.items[i].name); | |
| 7766 | } | |
| 7767 | ||
| 7768 | 7757 | const hash = new_func.hash; |
| 7769 | 7758 | const fn_payload = try sema.arena.create(Value.Payload.Function); |
| 7770 | 7759 | new_func.* = .{ |
| ... | ... | @@ -7772,13 +7761,11 @@ fn funcCommon( |
| 7772 | 7761 | .zir_body_inst = func_inst, |
| 7773 | 7762 | .owner_decl = sema.owner_decl_index, |
| 7774 | 7763 | .comptime_args = comptime_args, |
| 7775 | .anytype_args = undefined, | |
| 7776 | 7764 | .hash = hash, |
| 7777 | 7765 | .lbrace_line = src_locs.lbrace_line, |
| 7778 | 7766 | .rbrace_line = src_locs.rbrace_line, |
| 7779 | 7767 | .lbrace_column = @truncate(u16, src_locs.columns), |
| 7780 | 7768 | .rbrace_column = @truncate(u16, src_locs.columns >> 16), |
| 7781 | .param_names = param_names, | |
| 7782 | 7769 | .branch_quota = default_branch_quota, |
| 7783 | 7770 | .is_noinline = is_noinline, |
| 7784 | 7771 | }; |
src/Zir.zig+21| ... | ... | @@ -3915,6 +3915,27 @@ pub const FnInfo = struct { |
| 3915 | 3915 | total_params_len: u32, |
| 3916 | 3916 | }; |
| 3917 | 3917 | |
| 3918 | pub fn getParamBody(zir: Zir, fn_inst: Inst.Index) []const u32 { | |
| 3919 | const tags = zir.instructions.items(.tag); | |
| 3920 | const datas = zir.instructions.items(.data); | |
| 3921 | const inst_data = datas[fn_inst].pl_node; | |
| 3922 | ||
| 3923 | const param_block_index = switch (tags[fn_inst]) { | |
| 3924 | .func, .func_inferred => blk: { | |
| 3925 | const extra = zir.extraData(Inst.Func, inst_data.payload_index); | |
| 3926 | break :blk extra.data.param_block; | |
| 3927 | }, | |
| 3928 | .func_fancy => blk: { | |
| 3929 | const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index); | |
| 3930 | break :blk extra.data.param_block; | |
| 3931 | }, | |
| 3932 | else => unreachable, | |
| 3933 | }; | |
| 3934 | ||
| 3935 | const param_block = zir.extraData(Inst.Block, datas[param_block_index].pl_node.payload_index); | |
| 3936 | return zir.extra[param_block.end..][0..param_block.data.body_len]; | |
| 3937 | } | |
| 3938 | ||
| 3918 | 3939 | pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo { |
| 3919 | 3940 | const tags = zir.instructions.items(.tag); |
| 3920 | 3941 | const datas = zir.instructions.items(.data); |
src/arch/arm/CodeGen.zig+1-1| ... | ... | @@ -3372,7 +3372,7 @@ fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, arg_index: u32, stack_byte_c |
| 3372 | 3372 | |
| 3373 | 3373 | const mcv = self.args[arg_index]; |
| 3374 | 3374 | const ty = self.air.instructions.items(.data)[inst].ty; |
| 3375 | const name = self.mod_fn.getParamName(arg_index); | |
| 3375 | const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); | |
| 3376 | 3376 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 3377 | 3377 | |
| 3378 | 3378 | switch (mcv) { |
src/arch/riscv64/CodeGen.zig+1-1| ... | ... | @@ -1619,7 +1619,7 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1619 | 1619 | |
| 1620 | 1620 | fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void { |
| 1621 | 1621 | const ty = self.air.instructions.items(.data)[inst].ty; |
| 1622 | const name = self.mod_fn.getParamName(arg_index); | |
| 1622 | const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); | |
| 1623 | 1623 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 1624 | 1624 | |
| 1625 | 1625 | switch (mcv) { |
src/arch/sparc64/CodeGen.zig+1-1| ... | ... | @@ -2959,7 +2959,7 @@ fn finishAir(self: *Self, inst: Air.Inst.Index, result: MCValue, operands: [Live |
| 2959 | 2959 | |
| 2960 | 2960 | fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void { |
| 2961 | 2961 | const ty = self.air.instructions.items(.data)[inst].ty; |
| 2962 | const name = self.mod_fn.getParamName(arg_index); | |
| 2962 | const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); | |
| 2963 | 2963 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 2964 | 2964 | |
| 2965 | 2965 | switch (mcv) { |
src/arch/wasm/CodeGen.zig+1-1| ... | ... | @@ -1991,7 +1991,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 1991 | 1991 | switch (self.debug_output) { |
| 1992 | 1992 | .dwarf => |dwarf| { |
| 1993 | 1993 | // TODO: Get the original arg index rather than wasm arg index |
| 1994 | const name = self.mod_fn.getParamName(arg_index); | |
| 1994 | const name = self.mod_fn.getParamName(self.bin_file.base.options.module.?, arg_index); | |
| 1995 | 1995 | const leb_size = link.File.Wasm.getULEB128Size(arg.local); |
| 1996 | 1996 | const dbg_info = &dwarf.dbg_info; |
| 1997 | 1997 | try dbg_info.ensureUnusedCapacity(3 + leb_size + 5 + name.len + 1); |
src/arch/x86_64/CodeGen.zig+1-1| ... | ... | @@ -3789,7 +3789,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 3789 | 3789 | |
| 3790 | 3790 | const ty = self.air.typeOfIndex(inst); |
| 3791 | 3791 | const mcv = self.args[arg_index]; |
| 3792 | const name = self.mod_fn.getParamName(arg_index); | |
| 3792 | const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); | |
| 3793 | 3793 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 3794 | 3794 | |
| 3795 | 3795 | if (self.liveness.isUnused(inst)) |
src/codegen/llvm.zig+1-1| ... | ... | @@ -7313,7 +7313,7 @@ pub const FuncGen = struct { |
| 7313 | 7313 | const lbrace_col = func.lbrace_column + 1; |
| 7314 | 7314 | const di_local_var = dib.createParameterVariable( |
| 7315 | 7315 | self.di_scope.?, |
| 7316 | func.getParamName(src_index).ptr, // TODO test 0 bit args | |
| 7316 | func.getParamName(self.dg.module, src_index).ptr, // TODO test 0 bit args | |
| 7317 | 7317 | self.di_file.?, |
| 7318 | 7318 | lbrace_line, |
| 7319 | 7319 | try self.dg.object.lowerDebugType(inst_ty, .full), |