authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-09-23 11:49:44-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-10-01 17:59:42-06:00
log97ab720d84ac6af5174ed93f371fedfa2331445d
tree45873c843a24f1709e3c5eb401fd6d711e593307
parent0228887b943dd7e70f7c5cc56e3993769342abf2
signaturelock-open Commit is signed but in an unrecognized format.

stage1: Add alignment to TypeInfo.Fn


3 files changed, 26 insertions(+), 16 deletions(-)

lib/std/builtin.zig+1
...@@ -343,6 +343,7 @@ pub const TypeInfo = union(enum) {...@@ -343,6 +343,7 @@ pub const TypeInfo = union(enum) {
343 /// therefore must be kept in sync with the compiler implementation.343 /// therefore must be kept in sync with the compiler implementation.
344 pub const Fn = struct {344 pub const Fn = struct {
345 calling_convention: CallingConvention,345 calling_convention: CallingConvention,
346 alignment: u29,
346 is_generic: bool,347 is_generic: bool,
347 is_var_args: bool,348 is_var_args: bool,
348 return_type: ?type,349 return_type: ?type,
src/stage1/ir.cpp+20-15
...@@ -25564,7 +25564,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25564,7 +25564,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25564 result->special = ConstValSpecialStatic;25564 result->special = ConstValSpecialStatic;
25565 result->type = ir_type_info_get_type(ira, "Fn", nullptr);25565 result->type = ir_type_info_get_type(ira, "Fn", nullptr);
2556625566
25567 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 5);25567 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 6);
25568 result->data.x_struct.fields = fields;25568 result->data.x_struct.fields = fields;
2556925569
25570 // calling_convention: TypeInfo.CallingConvention25570 // calling_convention: TypeInfo.CallingConvention
...@@ -25572,30 +25572,35 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25572,30 +25572,35 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25572 fields[0]->special = ConstValSpecialStatic;25572 fields[0]->special = ConstValSpecialStatic;
25573 fields[0]->type = get_builtin_type(ira->codegen, "CallingConvention");25573 fields[0]->type = get_builtin_type(ira->codegen, "CallingConvention");
25574 bigint_init_unsigned(&fields[0]->data.x_enum_tag, type_entry->data.fn.fn_type_id.cc);25574 bigint_init_unsigned(&fields[0]->data.x_enum_tag, type_entry->data.fn.fn_type_id.cc);
25575 // alignment: u29
25576 ensure_field_index(result->type, "alignment", 1);
25577 fields[1]->special = ConstValSpecialStatic;
25578 fields[1]->type = ira->codegen->builtin_types.entry_u29;
25579 bigint_init_unsigned(&fields[1]->data.x_bigint, type_entry->data.fn.fn_type_id.alignment);
25575 // is_generic: bool25580 // is_generic: bool
25576 ensure_field_index(result->type, "is_generic", 1);25581 ensure_field_index(result->type, "is_generic", 2);
25577 bool is_generic = type_entry->data.fn.is_generic;25582 bool is_generic = type_entry->data.fn.is_generic;
25578 fields[1]->special = ConstValSpecialStatic;
25579 fields[1]->type = ira->codegen->builtin_types.entry_bool;
25580 fields[1]->data.x_bool = is_generic;
25581 // is_varargs: bool
25582 ensure_field_index(result->type, "is_var_args", 2);
25583 bool is_varargs = type_entry->data.fn.fn_type_id.is_var_args;
25584 fields[2]->special = ConstValSpecialStatic;25583 fields[2]->special = ConstValSpecialStatic;
25585 fields[2]->type = ira->codegen->builtin_types.entry_bool;25584 fields[2]->type = ira->codegen->builtin_types.entry_bool;
25586 fields[2]->data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;25585 fields[2]->data.x_bool = is_generic;
25587 // return_type: ?type25586 // is_varargs: bool
25588 ensure_field_index(result->type, "return_type", 3);25587 ensure_field_index(result->type, "is_var_args", 3);
25588 bool is_varargs = type_entry->data.fn.fn_type_id.is_var_args;
25589 fields[3]->special = ConstValSpecialStatic;25589 fields[3]->special = ConstValSpecialStatic;
25590 fields[3]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_type);25590 fields[3]->type = ira->codegen->builtin_types.entry_bool;
25591 fields[3]->data.x_bool = is_varargs;
25592 // return_type: ?type
25593 ensure_field_index(result->type, "return_type", 4);
25594 fields[4]->special = ConstValSpecialStatic;
25595 fields[4]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_type);
25591 if (type_entry->data.fn.fn_type_id.return_type == nullptr)25596 if (type_entry->data.fn.fn_type_id.return_type == nullptr)
25592 fields[3]->data.x_optional = nullptr;25597 fields[4]->data.x_optional = nullptr;
25593 else {25598 else {
25594 ZigValue *return_type = ira->codegen->pass1_arena->create<ZigValue>();25599 ZigValue *return_type = ira->codegen->pass1_arena->create<ZigValue>();
25595 return_type->special = ConstValSpecialStatic;25600 return_type->special = ConstValSpecialStatic;
25596 return_type->type = ira->codegen->builtin_types.entry_type;25601 return_type->type = ira->codegen->builtin_types.entry_type;
25597 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;25602 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
25598 fields[3]->data.x_optional = return_type;25603 fields[4]->data.x_optional = return_type;
25599 }25604 }
25600 // args: []TypeInfo.FnArg25605 // args: []TypeInfo.FnArg
25601 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);25606 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
...@@ -25611,7 +25616,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25611,7 +25616,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25611 fn_arg_array->data.x_array.special = ConstArraySpecialNone;25616 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
25612 fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);25617 fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
2561325618
25614 init_const_slice(ira->codegen, fields[4], fn_arg_array, 0, fn_arg_count, false);25619 init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false);
2561525620
25616 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {25621 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
25617 FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];25622 FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];
test/stage1/behavior/type_info.zig+5-1
...@@ -273,11 +273,14 @@ test "type info: function type info" {...@@ -273,11 +273,14 @@ test "type info: function type info" {
273fn testFunction() void {273fn testFunction() void {
274 const fn_info = @typeInfo(@TypeOf(foo));274 const fn_info = @typeInfo(@TypeOf(foo));
275 expect(fn_info == .Fn);275 expect(fn_info == .Fn);
276 expect(fn_info.Fn.alignment == 0);
276 expect(fn_info.Fn.calling_convention == .C);277 expect(fn_info.Fn.calling_convention == .C);
277 expect(!fn_info.Fn.is_generic);278 expect(!fn_info.Fn.is_generic);
278 expect(fn_info.Fn.args.len == 2);279 expect(fn_info.Fn.args.len == 2);
279 expect(fn_info.Fn.is_var_args);280 expect(fn_info.Fn.is_var_args);
280 expect(fn_info.Fn.return_type.? == usize);281 expect(fn_info.Fn.return_type.? == usize);
282 const fn_aligned_info = @typeInfo(@TypeOf(fooAligned));
283 expect(fn_aligned_info.Fn.alignment == 4);
281284
282 const test_instance: TestStruct = undefined;285 const test_instance: TestStruct = undefined;
283 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));286 const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo));
...@@ -285,7 +288,8 @@ fn testFunction() void {...@@ -285,7 +288,8 @@ fn testFunction() void {
285 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);288 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
286}289}
287290
288extern fn foo(a: usize, b: bool, ...) usize;291extern fn foo(a: usize, b: bool, ...) callconv(.C) usize;
292extern fn fooAligned(a: usize, b: bool, ...) align(4) callconv(.C) usize;
289293
290test "typeInfo with comptime parameter in struct fn def" {294test "typeInfo with comptime parameter in struct fn def" {
291 const S = struct {295 const S = struct {