From cd8070f94f6865959949dbcec6e0e32cd88bb544 Mon Sep 17 00:00:00 2001 From: antlilja Date: Sat, 30 Jul 2022 11:39:49 +0200 Subject: Removed param_names from Fn inside Module.zig Removed the copy of param_names inside of Fn and changed to implementation of getParamName to fetch to parameter name from the ZIR. The signature of getParamName was also changed to take an additional *Module argument. --- src/Module.zig | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 4ac2775515..b80e00ad59 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1471,14 +1471,6 @@ pub const Fn = struct { /// TODO apply the same enhancement for param_names below to this field. anytype_args: [*]bool, - /// Prefer to use `getParamName` to access this because of the future improvement - /// we want to do mentioned in the TODO below. - /// Stored in gpa. - /// TODO: change param ZIR instructions to be embedded inside the function - /// ZIR instruction instead of before it, so that `zir_body_inst` can be used to - /// determine param names rather than redundantly storing them here. - param_names: []const [:0]const u8, - /// Precomputed hash for monomorphed_funcs. /// This is important because it may be accessed when resizing monomorphed_funcs /// while this Fn has already been added to the set, but does not have the @@ -1590,18 +1582,28 @@ pub const Fn = struct { gpa.destroy(node); it = next; } - - for (func.param_names) |param_name| { - gpa.free(param_name); - } - gpa.free(func.param_names); } - pub fn getParamName(func: Fn, index: u32) [:0]const u8 { - // TODO rework ZIR of parameters so that this function looks up - // param names in ZIR instead of redundantly saving them into Fn. - // const zir = func.owner_decl.getFileScope().zir; - return func.param_names[index]; + pub fn getParamName(func: Fn, mod: *Module, index: u32) [:0]const u8 { + const file = mod.declPtr(func.owner_decl).getFileScope(); + + const tags = file.zir.instructions.items(.tag); + const data = file.zir.instructions.items(.data); + + const param_body = file.zir.getParamBody(func.zir_body_inst); + const param = param_body[index]; + + return switch (tags[param]) { + .param, .param_comptime => blk: { + const extra = file.zir.extraData(Zir.Inst.Param, data[param].pl_tok.payload_index); + break :blk file.zir.nullTerminatedString(extra.data.name); + }, + .param_anytype, .param_anytype_comptime => blk: { + const param_data = data[param].str_tok; + break :blk param_data.get(file.zir); + }, + else => unreachable, + }; } pub fn hasInferredErrorSet(func: Fn, mod: *Module) bool { -- cgit v1.2.3 From ab3b614a335ffac9eac4f824ee18fba262ad988e Mon Sep 17 00:00:00 2001 From: antlilja Date: Sat, 30 Jul 2022 18:43:15 +0200 Subject: Removed anytype_args field from Fn anytype_args field was replaced with isAnytypeParam function. --- src/Module.zig | 21 ++++++++++++++++----- src/Sema.zig | 11 ++--------- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index b80e00ad59..625af7ca07 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1464,12 +1464,8 @@ pub const Fn = struct { /// These never have .generic_poison for the Type /// because the Type is needed to pass to `Type.eql` and for inserting comptime arguments /// into the inst_map when analyzing the body of a generic function instantiation. - /// Instead, the is_anytype knowledge is communicated via `anytype_args`. + /// Instead, the is_anytype knowledge is communicated via `isAnytypeParam`. comptime_args: ?[*]TypedValue, - /// When comptime_args is null, this is undefined. Otherwise, this flags each - /// parameter and tells whether it is anytype. - /// TODO apply the same enhancement for param_names below to this field. - anytype_args: [*]bool, /// Precomputed hash for monomorphed_funcs. /// This is important because it may be accessed when resizing monomorphed_funcs @@ -1584,6 +1580,21 @@ pub const Fn = struct { } } + pub fn isAnytypeParam(func: Fn, mod: *Module, index: u32) bool { + const file = mod.declPtr(func.owner_decl).getFileScope(); + + const tags = file.zir.instructions.items(.tag); + + const param_body = file.zir.getParamBody(func.zir_body_inst); + const param = param_body[index]; + + return switch (tags[param]) { + .param, .param_comptime => false, + .param_anytype, .param_anytype_comptime => true, + else => unreachable, + }; + } + pub fn getParamName(func: Fn, mod: *Module, index: u32) [:0]const u8 { const file = mod.declPtr(func.owner_decl).getFileScope(); diff --git a/src/Sema.zig b/src/Sema.zig index 07e81dc5be..1629b7711a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -5498,7 +5498,7 @@ const GenericCallAdapter = struct { const this_is_comptime = this_arg.val.tag() != .generic_poison; const other_is_comptime = other_arg.val.tag() != .generic_poison; const this_is_anytype = this_arg.ty.tag() != .generic_poison; - const other_is_anytype = other_key.anytype_args[i]; + const other_is_anytype = other_key.isAnytypeParam(ctx.module, @intCast(u32, i)); if (other_is_anytype != this_is_anytype) return false; if (other_is_comptime != this_is_comptime) return false; @@ -6379,12 +6379,9 @@ fn instantiateGenericCall( errdefer new_func.deinit(gpa); assert(new_func == new_module_func); - const anytype_args = try new_decl_arena_allocator.alloc(bool, func_ty_info.param_types.len); - new_func.anytype_args = anytype_args.ptr; arg_i = 0; for (fn_info.param_body) |inst| { var is_comptime = false; - var is_anytype = false; switch (zir_tags[inst]) { .param => { is_comptime = func_ty_info.paramIsComptime(arg_i); @@ -6393,11 +6390,9 @@ fn instantiateGenericCall( is_comptime = true; }, .param_anytype => { - is_anytype = true; is_comptime = func_ty_info.paramIsComptime(arg_i); }, .param_anytype_comptime => { - is_anytype = true; is_comptime = true; }, else => continue, @@ -6405,10 +6400,9 @@ fn instantiateGenericCall( // We populate the Type here regardless because it is needed by // `GenericCallAdapter.eql` as well as function body analysis. - // Whether it is anytype is communicated by `anytype_args`. + // Whether it is anytype is communicated by `isAnytypeParam`. const arg = child_sema.inst_map.get(inst).?; const copied_arg_ty = try child_sema.typeOf(arg).copy(new_decl_arena_allocator); - anytype_args[arg_i] = is_anytype; if (try sema.typeRequiresComptime(block, .unneeded, copied_arg_ty)) { is_comptime = true; @@ -7760,7 +7754,6 @@ fn funcCommon( .zir_body_inst = func_inst, .owner_decl = sema.owner_decl_index, .comptime_args = comptime_args, - .anytype_args = undefined, .hash = hash, .lbrace_line = src_locs.lbrace_line, .rbrace_line = src_locs.rbrace_line, -- cgit v1.2.3