authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-22 14:08:21+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-23 09:44:36+02:00
log923b07bac797d66fe6d393cf823402c742250f48
treeaf9e4ffa14e1548c826ad85b6e092c4fed7c411d
parentf8154905e7798aa0e884aa55e9336531f8c2daba

stage2: add parameters to fn typeInfo


2 files changed, 63 insertions(+), 6 deletions(-)

src/Sema.zig+62-4
...@@ -9886,7 +9886,65 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9886,7 +9886,65 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9886 }),9886 }),
9887 ),9887 ),
9888 .Fn => {9888 .Fn => {
9889 // TODO: look into memoizing this result.
9889 const info = ty.fnInfo();9890 const info = ty.fnInfo();
9891 var params_anon_decl = try block.startAnonDecl(src);
9892 defer params_anon_decl.deinit();
9893
9894 const param_vals = try params_anon_decl.arena().alloc(Value, info.param_types.len);
9895 for (param_vals) |*param_val, i| {
9896 const param_ty = info.param_types[i];
9897 const is_generic = param_ty.tag() == .generic_poison;
9898 const param_ty_val = if (is_generic)
9899 Value.@"null"
9900 else
9901 try Value.Tag.opt_payload.create(
9902 params_anon_decl.arena(),
9903 try Value.Tag.ty.create(params_anon_decl.arena(), param_ty),
9904 );
9905
9906 const param_fields = try params_anon_decl.arena().create([3]Value);
9907 param_fields.* = .{
9908 // is_generic: bool,
9909 Value.makeBool(is_generic),
9910 // is_noalias: bool,
9911 Value.@"false", // TODO
9912 // arg_type: ?type,
9913 param_ty_val,
9914 };
9915 param_val.* = try Value.Tag.@"struct".create(params_anon_decl.arena(), param_fields);
9916 }
9917
9918 const args_val = v: {
9919 const fn_info_decl = (try sema.namespaceLookup(
9920 block,
9921 src,
9922 type_info_ty.getNamespace().?,
9923 "Fn",
9924 )).?;
9925 try sema.mod.declareDeclDependency(sema.owner_decl, fn_info_decl);
9926 try sema.ensureDeclAnalyzed(fn_info_decl);
9927 const param_info_decl = (try sema.namespaceLookup(
9928 block,
9929 src,
9930 fn_info_decl.val.castTag(.ty).?.data.getNamespace().?,
9931 "Param",
9932 )).?;
9933 try sema.mod.declareDeclDependency(sema.owner_decl, param_info_decl);
9934 try sema.ensureDeclAnalyzed(param_info_decl);
9935 const new_decl = try params_anon_decl.finish(
9936 try Type.Tag.array.create(params_anon_decl.arena(), .{
9937 .len = param_vals.len,
9938 .elem_type = param_info_decl.ty,
9939 }),
9940 try Value.Tag.array.create(
9941 params_anon_decl.arena(),
9942 param_vals,
9943 ),
9944 );
9945 break :v try Value.Tag.decl_ref.create(sema.arena, new_decl);
9946 };
9947
9890 const field_values = try sema.arena.alloc(Value, 6);9948 const field_values = try sema.arena.alloc(Value, 6);
9891 // calling_convention: CallingConvention,9949 // calling_convention: CallingConvention,
9892 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.cc));9950 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.cc));
...@@ -9897,9 +9955,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9897,9 +9955,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9897 // is_var_args: bool,9955 // is_var_args: bool,
9898 field_values[3] = Value.makeBool(info.is_var_args);9956 field_values[3] = Value.makeBool(info.is_var_args);
9899 // return_type: ?type,9957 // return_type: ?type,
9900 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());9958 field_values[4] = try Value.Tag.ty.create(sema.arena, info.return_type);
9901 // args: []const FnArg,9959 // args: []const Fn.Param,
9902 field_values[5] = Value.@"null"; // TODO9960 field_values[5] = args_val;
99039961
9904 return sema.addConstant(9962 return sema.addConstant(
9905 type_info_ty,9963 type_info_ty,
...@@ -10102,7 +10160,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -10102,7 +10160,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
10102 }),10160 }),
10103 try Value.Tag.array.create(10161 try Value.Tag.array.create(
10104 fields_anon_decl.arena(),10162 fields_anon_decl.arena(),
10105 try fields_anon_decl.arena().dupe(Value, enum_field_vals),10163 enum_field_vals,
10106 ),10164 ),
10107 );10165 );
10108 break :v try Value.Tag.decl_ref.create(sema.arena, new_decl);10166 break :v try Value.Tag.decl_ref.create(sema.arena, new_decl);
test/behavior/type_info.zig+1-2
...@@ -323,8 +323,6 @@ fn testOpaque() !void {...@@ -323,8 +323,6 @@ fn testOpaque() !void {
323}323}
324324
325test "type info: function type info" {325test "type info: function type info" {
326 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
327
328 // wasm doesn't support align attributes on functions326 // wasm doesn't support align attributes on functions
329 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;327 if (builtin.target.cpu.arch == .wasm32 or builtin.target.cpu.arch == .wasm64) return error.SkipZigTest;
330 try testFunction();328 try testFunction();
...@@ -343,6 +341,7 @@ fn testFunction() !void {...@@ -343,6 +341,7 @@ fn testFunction() !void {
343 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));341 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));
344 try expect(fn_aligned_info.Fn.alignment == 4);342 try expect(fn_aligned_info.Fn.alignment == 4);
345343
344 if (builtin.zig_backend != .stage1) return; // no bound fn in stage2
346 const test_instance: TestPackedStruct = undefined;345 const test_instance: TestPackedStruct = undefined;
347 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));346 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
348 try expect(bound_fn_info == .BoundFn);347 try expect(bound_fn_info == .BoundFn);