authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-06-19 17:45:19+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-06-19 17:45:19+03:00
log13923132363b26b9982951fd79f01dbab0046e81
treee9da44e2040b005285ccbdefea9c68b458e0db0b
parent1ca90b585692c9611c64412844d2f3a7b3e11340

@typeInfo now uses optional types instead of @typeOf(undefined)


4 files changed, 65 insertions(+), 44 deletions(-)

doc/langref.html.in+4-4
...@@ -5684,20 +5684,20 @@ pub const TypeInfo = union(TypeId) {...@@ -5684,20 +5684,20 @@ pub const TypeInfo = union(TypeId) {
5684 pub const FnArg = struct {5684 pub const FnArg = struct {
5685 is_generic: bool,5685 is_generic: bool,
5686 is_noalias: bool,5686 is_noalias: bool,
5687 arg_type: type,5687 arg_type: ?type,
5688 };5688 };
56895689
5690 pub const Fn = struct {5690 pub const Fn = struct {
5691 calling_convention: CallingConvention,5691 calling_convention: CallingConvention,
5692 is_generic: bool,5692 is_generic: bool,
5693 is_var_args: bool,5693 is_var_args: bool,
5694 return_type: type,5694 return_type: ?type,
5695 async_allocator_type: type,5695 async_allocator_type: ?type,
5696 args: []FnArg,5696 args: []FnArg,
5697 };5697 };
56985698
5699 pub const Promise = struct {5699 pub const Promise = struct {
5700 child: type,5700 child: ?type,
5701 };5701 };
57025702
5703 pub const Definition = struct {5703 pub const Definition = struct {
src/codegen.cpp+5-5
...@@ -6627,7 +6627,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -6627,7 +6627,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
6627 "\n"6627 "\n"
6628 " pub const Union = struct {\n"6628 " pub const Union = struct {\n"
6629 " layout: ContainerLayout,\n"6629 " layout: ContainerLayout,\n"
6630 " tag_type: type,\n"6630 " tag_type: ?type,\n"
6631 " fields: []UnionField,\n"6631 " fields: []UnionField,\n"
6632 " defs: []Definition,\n"6632 " defs: []Definition,\n"
6633 " };\n"6633 " };\n"
...@@ -6644,20 +6644,20 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -6644,20 +6644,20 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
6644 " pub const FnArg = struct {\n"6644 " pub const FnArg = struct {\n"
6645 " is_generic: bool,\n"6645 " is_generic: bool,\n"
6646 " is_noalias: bool,\n"6646 " is_noalias: bool,\n"
6647 " arg_type: type,\n"6647 " arg_type: ?type,\n"
6648 " };\n"6648 " };\n"
6649 "\n"6649 "\n"
6650 " pub const Fn = struct {\n"6650 " pub const Fn = struct {\n"
6651 " calling_convention: CallingConvention,\n"6651 " calling_convention: CallingConvention,\n"
6652 " is_generic: bool,\n"6652 " is_generic: bool,\n"
6653 " is_var_args: bool,\n"6653 " is_var_args: bool,\n"
6654 " return_type: type,\n"6654 " return_type: ?type,\n"
6655 " async_allocator_type: type,\n"6655 " async_allocator_type: ?type,\n"
6656 " args: []FnArg,\n"6656 " args: []FnArg,\n"
6657 " };\n"6657 " };\n"
6658 "\n"6658 "\n"
6659 " pub const Promise = struct {\n"6659 " pub const Promise = struct {\n"
6660 " child: type,\n"6660 " child: ?type,\n"
6661 " };\n"6661 " };\n"
6662 "\n"6662 "\n"
6663 " pub const Definition = struct {\n"6663 " pub const Definition = struct {\n"
src/ir.cpp+48-27
...@@ -16722,16 +16722,20 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16722,16 +16722,20 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16722 ConstExprValue *fields = create_const_vals(1);16722 ConstExprValue *fields = create_const_vals(1);
16723 result->data.x_struct.fields = fields;16723 result->data.x_struct.fields = fields;
1672416724
16725 // @TODO ?type instead of using @typeOf(undefined) when we have no type.16725 // child: ?type
16726 // child: type
16727 ensure_field_index(result->type, "child", 0);16726 ensure_field_index(result->type, "child", 0);
16728 fields[0].special = ConstValSpecialStatic;16727 fields[0].special = ConstValSpecialStatic;
16729 fields[0].type = ira->codegen->builtin_types.entry_type;16728 fields[0].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
1673016729
16731 if (type_entry->data.promise.result_type == nullptr)16730 if (type_entry->data.promise.result_type == nullptr)
16732 fields[0].data.x_type = ira->codegen->builtin_types.entry_undef;16731 fields[0].data.x_optional = nullptr;
16733 else16732 else {
16734 fields[0].data.x_type = type_entry->data.promise.result_type;16733 ConstExprValue *child_type = create_const_vals(1);
16734 child_type->special = ConstValSpecialStatic;
16735 child_type->type = ira->codegen->builtin_types.entry_type;
16736 child_type->data.x_type = type_entry->data.promise.result_type;
16737 fields[0].data.x_optional = child_type;
16738 }
1673516739
16736 break;16740 break;
16737 }16741 }
...@@ -16872,19 +16876,23 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16872,19 +16876,23 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16872 fields[0].special = ConstValSpecialStatic;16876 fields[0].special = ConstValSpecialStatic;
16873 fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");16877 fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
16874 bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);16878 bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);
16875 // tag_type: type16879 // tag_type: ?type
16876 ensure_field_index(result->type, "tag_type", 1);16880 ensure_field_index(result->type, "tag_type", 1);
16877 fields[1].special = ConstValSpecialStatic;16881 fields[1].special = ConstValSpecialStatic;
16878 fields[1].type = ira->codegen->builtin_types.entry_type;16882 fields[1].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
16879 // @TODO ?type instead of using @typeOf(undefined) when we have no type.16883
16880 AstNode *union_decl_node = type_entry->data.unionation.decl_node;16884 AstNode *union_decl_node = type_entry->data.unionation.decl_node;
16881 if (union_decl_node->data.container_decl.auto_enum ||16885 if (union_decl_node->data.container_decl.auto_enum ||
16882 union_decl_node->data.container_decl.init_arg_expr != nullptr)16886 union_decl_node->data.container_decl.init_arg_expr != nullptr)
16883 {16887 {
16884 fields[1].data.x_type = type_entry->data.unionation.tag_type;16888 ConstExprValue *tag_type = create_const_vals(1);
16889 tag_type->special = ConstValSpecialStatic;
16890 tag_type->type = ira->codegen->builtin_types.entry_type;
16891 tag_type->data.x_type = type_entry->data.unionation.tag_type;
16892 fields[1].data.x_optional = tag_type;
16885 }16893 }
16886 else16894 else
16887 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;16895 fields[1].data.x_optional = nullptr;
16888 // fields: []TypeInfo.UnionField16896 // fields: []TypeInfo.UnionField
16889 ensure_field_index(result->type, "fields", 2);16897 ensure_field_index(result->type, "fields", 2);
1689016898
...@@ -16913,7 +16921,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16913,7 +16921,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16913 inner_fields[1].special = ConstValSpecialStatic;16921 inner_fields[1].special = ConstValSpecialStatic;
16914 inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);16922 inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);
1691516923
16916 if (fields[1].data.x_type == ira->codegen->builtin_types.entry_undef) {16924 if (fields[1].data.x_optional == nullptr) {
16917 inner_fields[1].data.x_optional = nullptr;16925 inner_fields[1].data.x_optional = nullptr;
16918 } else {16926 } else {
16919 inner_fields[1].data.x_optional = create_const_vals(1);16927 inner_fields[1].data.x_optional = create_const_vals(1);
...@@ -17022,8 +17030,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -17022,8 +17030,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
17022 ConstExprValue *fields = create_const_vals(6);17030 ConstExprValue *fields = create_const_vals(6);
17023 result->data.x_struct.fields = fields;17031 result->data.x_struct.fields = fields;
1702417032
17025 // @TODO Fix type = undefined with ?type
17026
17027 // calling_convention: TypeInfo.CallingConvention17033 // calling_convention: TypeInfo.CallingConvention
17028 ensure_field_index(result->type, "calling_convention", 0);17034 ensure_field_index(result->type, "calling_convention", 0);
17029 fields[0].special = ConstValSpecialStatic;17035 fields[0].special = ConstValSpecialStatic;
...@@ -17041,22 +17047,32 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -17041,22 +17047,32 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
17041 fields[2].special = ConstValSpecialStatic;17047 fields[2].special = ConstValSpecialStatic;
17042 fields[2].type = ira->codegen->builtin_types.entry_bool;17048 fields[2].type = ira->codegen->builtin_types.entry_bool;
17043 fields[2].data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;17049 fields[2].data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;
17044 // return_type: type17050 // return_type: ?type
17045 ensure_field_index(result->type, "return_type", 3);17051 ensure_field_index(result->type, "return_type", 3);
17046 fields[3].special = ConstValSpecialStatic;17052 fields[3].special = ConstValSpecialStatic;
17047 fields[3].type = ira->codegen->builtin_types.entry_type;17053 fields[3].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
17048 if (type_entry->data.fn.fn_type_id.return_type == nullptr)17054 if (type_entry->data.fn.fn_type_id.return_type == nullptr)
17049 fields[3].data.x_type = ira->codegen->builtin_types.entry_undef;17055 fields[3].data.x_optional = nullptr;
17050 else17056 else {
17051 fields[3].data.x_type = type_entry->data.fn.fn_type_id.return_type;17057 ConstExprValue *return_type = create_const_vals(1);
17058 return_type->special = ConstValSpecialStatic;
17059 return_type->type = ira->codegen->builtin_types.entry_type;
17060 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
17061 fields[3].data.x_optional = return_type;
17062 }
17052 // async_allocator_type: type17063 // async_allocator_type: type
17053 ensure_field_index(result->type, "async_allocator_type", 4);17064 ensure_field_index(result->type, "async_allocator_type", 4);
17054 fields[4].special = ConstValSpecialStatic;17065 fields[4].special = ConstValSpecialStatic;
17055 fields[4].type = ira->codegen->builtin_types.entry_type;17066 fields[4].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
17056 if (type_entry->data.fn.fn_type_id.async_allocator_type == nullptr)17067 if (type_entry->data.fn.fn_type_id.async_allocator_type == nullptr)
17057 fields[4].data.x_type = ira->codegen->builtin_types.entry_undef;17068 fields[4].data.x_optional = nullptr;
17058 else17069 else {
17059 fields[4].data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;17070 ConstExprValue *async_alloc_type = create_const_vals(1);
17071 async_alloc_type->special = ConstValSpecialStatic;
17072 async_alloc_type->type = ira->codegen->builtin_types.entry_type;
17073 async_alloc_type->data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;
17074 fields[4].data.x_optional = async_alloc_type;
17075 }
17060 // args: []TypeInfo.FnArg17076 // args: []TypeInfo.FnArg
17061 TypeTableEntry *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg");17077 TypeTableEntry *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg");
17062 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -17078 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
...@@ -17090,12 +17106,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -17090,12 +17106,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
17090 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;17106 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
17091 inner_fields[1].data.x_bool = fn_param_info->is_noalias;17107 inner_fields[1].data.x_bool = fn_param_info->is_noalias;
17092 inner_fields[2].special = ConstValSpecialStatic;17108 inner_fields[2].special = ConstValSpecialStatic;
17093 inner_fields[2].type = ira->codegen->builtin_types.entry_type;17109 inner_fields[2].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
1709417110
17095 if (arg_is_generic)17111 if (arg_is_generic)
17096 inner_fields[2].data.x_type = ira->codegen->builtin_types.entry_undef;17112 inner_fields[2].data.x_optional = nullptr;
17097 else17113 else {
17098 inner_fields[2].data.x_type = fn_param_info->type;17114 ConstExprValue *arg_type = create_const_vals(1);
17115 arg_type->special = ConstValSpecialStatic;
17116 arg_type->type = ira->codegen->builtin_types.entry_type;
17117 arg_type->data.x_type = fn_param_info->type;
17118 inner_fields[2].data.x_optional = arg_type;
17119 }
1709917120
17100 fn_arg_val->data.x_struct.fields = inner_fields;17121 fn_arg_val->data.x_struct.fields = inner_fields;
17101 fn_arg_val->data.x_struct.parent.id = ConstParentIdArray;17122 fn_arg_val->data.x_struct.parent.id = ConstParentIdArray;
test/cases/type_info.zig+8-8
...@@ -107,11 +107,11 @@ test "type info: promise info" {...@@ -107,11 +107,11 @@ test "type info: promise info" {
107fn testPromise() void {107fn testPromise() void {
108 const null_promise_info = @typeInfo(promise);108 const null_promise_info = @typeInfo(promise);
109 assert(TypeId(null_promise_info) == TypeId.Promise);109 assert(TypeId(null_promise_info) == TypeId.Promise);
110 assert(null_promise_info.Promise.child == @typeOf(undefined));110 assert(null_promise_info.Promise.child == null);
111111
112 const promise_info = @typeInfo(promise->usize);112 const promise_info = @typeInfo(promise->usize);
113 assert(TypeId(promise_info) == TypeId.Promise);113 assert(TypeId(promise_info) == TypeId.Promise);
114 assert(promise_info.Promise.child == usize);114 assert(promise_info.Promise.child.? == usize);
115}115}
116116
117test "type info: error set, error union info" {117test "type info: error set, error union info" {
...@@ -165,7 +165,7 @@ fn testUnion() void {...@@ -165,7 +165,7 @@ fn testUnion() void {
165 const typeinfo_info = @typeInfo(TypeInfo);165 const typeinfo_info = @typeInfo(TypeInfo);
166 assert(TypeId(typeinfo_info) == TypeId.Union);166 assert(TypeId(typeinfo_info) == TypeId.Union);
167 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);167 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
168 assert(typeinfo_info.Union.tag_type == TypeId);168 assert(typeinfo_info.Union.tag_type.? == TypeId);
169 assert(typeinfo_info.Union.fields.len == 25);169 assert(typeinfo_info.Union.fields.len == 25);
170 assert(typeinfo_info.Union.fields[4].enum_field != null);170 assert(typeinfo_info.Union.fields[4].enum_field != null);
171 assert(typeinfo_info.Union.fields[4].enum_field.?.value == 4);171 assert(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
...@@ -179,7 +179,7 @@ fn testUnion() void {...@@ -179,7 +179,7 @@ fn testUnion() void {
179179
180 const notag_union_info = @typeInfo(TestNoTagUnion);180 const notag_union_info = @typeInfo(TestNoTagUnion);
181 assert(TypeId(notag_union_info) == TypeId.Union);181 assert(TypeId(notag_union_info) == TypeId.Union);
182 assert(notag_union_info.Union.tag_type == @typeOf(undefined));182 assert(notag_union_info.Union.tag_type == null);
183 assert(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto);183 assert(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto);
184 assert(notag_union_info.Union.fields.len == 2);184 assert(notag_union_info.Union.fields.len == 2);
185 assert(notag_union_info.Union.fields[0].enum_field == null);185 assert(notag_union_info.Union.fields[0].enum_field == null);
...@@ -191,7 +191,7 @@ fn testUnion() void {...@@ -191,7 +191,7 @@ fn testUnion() void {
191191
192 const extern_union_info = @typeInfo(TestExternUnion);192 const extern_union_info = @typeInfo(TestExternUnion);
193 assert(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern);193 assert(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern);
194 assert(extern_union_info.Union.tag_type == @typeOf(undefined));194 assert(extern_union_info.Union.tag_type == null);
195 assert(extern_union_info.Union.fields[0].enum_field == null);195 assert(extern_union_info.Union.fields[0].enum_field == null);
196 assert(extern_union_info.Union.fields[0].field_type == *c_void);196 assert(extern_union_info.Union.fields[0].field_type == *c_void);
197}197}
...@@ -238,13 +238,13 @@ fn testFunction() void {...@@ -238,13 +238,13 @@ fn testFunction() void {
238 assert(fn_info.Fn.is_generic);238 assert(fn_info.Fn.is_generic);
239 assert(fn_info.Fn.args.len == 2);239 assert(fn_info.Fn.args.len == 2);
240 assert(fn_info.Fn.is_var_args);240 assert(fn_info.Fn.is_var_args);
241 assert(fn_info.Fn.return_type == @typeOf(undefined));241 assert(fn_info.Fn.return_type == null);
242 assert(fn_info.Fn.async_allocator_type == @typeOf(undefined));242 assert(fn_info.Fn.async_allocator_type == null);
243243
244 const test_instance: TestStruct = undefined;244 const test_instance: TestStruct = undefined;
245 const bound_fn_info = @typeInfo(@typeOf(test_instance.foo));245 const bound_fn_info = @typeInfo(@typeOf(test_instance.foo));
246 assert(TypeId(bound_fn_info) == TypeId.BoundFn);246 assert(TypeId(bound_fn_info) == TypeId.BoundFn);
247 assert(bound_fn_info.BoundFn.args[0].arg_type == *const TestStruct);247 assert(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
248}248}
249249
250fn foo(comptime a: usize, b: bool, args: ...) usize {250fn foo(comptime a: usize, b: bool, args: ...) usize {