authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-16 13:31:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-16 13:31:53-07:00
log94672dfb1941289eb65fdeab2e1dcc39ca70c3b7
treea28abab9a820840a1cbe8ebdaffa301020e5765e
parent92a09eb1e4d5914e5ea81c55c5feb322fcd90e7b

stage2: fully resolve fn types after analyzing its body


2 files changed, 28 insertions(+), 2 deletions(-)

src/Module.zig+15-2
...@@ -4739,7 +4739,8 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem...@@ -4739,7 +4739,8 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem
4739 // map the comptime parameters to constant values and only emit arg AIR instructions4739 // map the comptime parameters to constant values and only emit arg AIR instructions
4740 // for the runtime ones.4740 // for the runtime ones.
4741 const fn_ty = decl.ty;4741 const fn_ty = decl.ty;
4742 const runtime_params_len = @intCast(u32, fn_ty.fnParamLen());4742 const fn_ty_info = fn_ty.fnInfo();
4743 const runtime_params_len = @intCast(u32, fn_ty_info.param_types.len);
4743 try inner_block.instructions.ensureTotalCapacityPrecise(gpa, runtime_params_len);4744 try inner_block.instructions.ensureTotalCapacityPrecise(gpa, runtime_params_len);
4744 try sema.air_instructions.ensureUnusedCapacity(gpa, fn_info.total_params_len * 2); // * 2 for the `addType`4745 try sema.air_instructions.ensureUnusedCapacity(gpa, fn_info.total_params_len * 2); // * 2 for the `addType`
4745 try sema.inst_map.ensureUnusedCapacity(gpa, fn_info.total_params_len);4746 try sema.inst_map.ensureUnusedCapacity(gpa, fn_info.total_params_len);
...@@ -4771,7 +4772,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem...@@ -4771,7 +4772,7 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem
4771 continue;4772 continue;
4772 }4773 }
4773 }4774 }
4774 const param_type = fn_ty.fnParamType(runtime_param_index);4775 const param_type = fn_ty_info.param_types[runtime_param_index];
4775 const opt_opv = sema.typeHasOnePossibleValue(&inner_block, param.src, param_type) catch |err| switch (err) {4776 const opt_opv = sema.typeHasOnePossibleValue(&inner_block, param.src, param_type) catch |err| switch (err) {
4776 error.NeededSourceLocation => unreachable,4777 error.NeededSourceLocation => unreachable,
4777 error.GenericPoison => unreachable,4778 error.GenericPoison => unreachable,
...@@ -4822,6 +4823,18 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem...@@ -4822,6 +4823,18 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem
4822 func.state = .success;4823 func.state = .success;
4823 log.debug("set {s} to success", .{decl.name});4824 log.debug("set {s} to success", .{decl.name});
48244825
4826 // Finally we must resolve the return type and parameter types so that backends
4827 // have full access to type information.
4828 const src: LazySrcLoc = .{ .node_offset = 0 };
4829 sema.resolveFnTypes(&inner_block, src, fn_ty_info) catch |err| switch (err) {
4830 error.NeededSourceLocation => unreachable,
4831 error.GenericPoison => unreachable,
4832 error.ComptimeReturn => unreachable,
4833 error.ComptimeBreak => unreachable,
4834 error.AnalysisFail => {},
4835 else => |e| return e,
4836 };
4837
4825 return Air{4838 return Air{
4826 .instructions = sema.air_instructions.toOwnedSlice(),4839 .instructions = sema.air_instructions.toOwnedSlice(),
4827 .extra = sema.air_extra.toOwnedSlice(gpa),4840 .extra = sema.air_extra.toOwnedSlice(gpa),
src/Sema.zig+13
...@@ -20006,6 +20006,19 @@ fn resolvePeerTypes(...@@ -20006,6 +20006,19 @@ fn resolvePeerTypes(
20006 return chosen_ty;20006 return chosen_ty;
20007}20007}
2000820008
20009pub fn resolveFnTypes(
20010 sema: *Sema,
20011 block: *Block,
20012 src: LazySrcLoc,
20013 fn_info: Type.Payload.Function.Data,
20014) CompileError!void {
20015 try sema.resolveTypeFully(block, src, fn_info.return_type);
20016
20017 for (fn_info.param_types) |param_ty| {
20018 try sema.resolveTypeFully(block, src, param_ty);
20019 }
20020}
20021
20009fn resolveTypeLayout(20022fn resolveTypeLayout(
20010 sema: *Sema,20023 sema: *Sema,
20011 block: *Block,20024 block: *Block,