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) {
56845684 pub const FnArg = struct {
56855685 is_generic: bool,
56865686 is_noalias: bool,
5687 arg_type: type,
5687 arg_type: ?type,
56885688 };
56895689
56905690 pub const Fn = struct {
56915691 calling_convention: CallingConvention,
56925692 is_generic: bool,
56935693 is_var_args: bool,
5694 return_type: type,
5695 async_allocator_type: type,
5694 return_type: ?type,
5695 async_allocator_type: ?type,
56965696 args: []FnArg,
56975697 };
56985698
56995699 pub const Promise = struct {
5700 child: type,
5700 child: ?type,
57015701 };
57025702
57035703 pub const Definition = struct {
src/codegen.cpp+5-5
......@@ -6627,7 +6627,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
66276627 "\n"
66286628 " pub const Union = struct {\n"
66296629 " layout: ContainerLayout,\n"
6630 " tag_type: type,\n"
6630 " tag_type: ?type,\n"
66316631 " fields: []UnionField,\n"
66326632 " defs: []Definition,\n"
66336633 " };\n"
......@@ -6644,20 +6644,20 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
66446644 " pub const FnArg = struct {\n"
66456645 " is_generic: bool,\n"
66466646 " is_noalias: bool,\n"
6647 " arg_type: type,\n"
6647 " arg_type: ?type,\n"
66486648 " };\n"
66496649 "\n"
66506650 " pub const Fn = struct {\n"
66516651 " calling_convention: CallingConvention,\n"
66526652 " is_generic: bool,\n"
66536653 " is_var_args: bool,\n"
6654 " return_type: type,\n"
6655 " async_allocator_type: type,\n"
6654 " return_type: ?type,\n"
6655 " async_allocator_type: ?type,\n"
66566656 " args: []FnArg,\n"
66576657 " };\n"
66586658 "\n"
66596659 " pub const Promise = struct {\n"
6660 " child: type,\n"
6660 " child: ?type,\n"
66616661 " };\n"
66626662 "\n"
66636663 " 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
1672216722 ConstExprValue *fields = create_const_vals(1);
1672316723 result->data.x_struct.fields = fields;
1672416724
16725 // @TODO ?type instead of using @typeOf(undefined) when we have no type.
16726 // child: type
16725 // child: ?type
1672716726 ensure_field_index(result->type, "child", 0);
1672816727 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
1673116730 if (type_entry->data.promise.result_type == nullptr)
16732 fields[0].data.x_type = ira->codegen->builtin_types.entry_undef;
16733 else
16734 fields[0].data.x_type = type_entry->data.promise.result_type;
16731 fields[0].data.x_optional = nullptr;
16732 else {
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
1673616740 break;
1673716741 }
......@@ -16872,19 +16876,23 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1687216876 fields[0].special = ConstValSpecialStatic;
1687316877 fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
1687416878 bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);
16875 // tag_type: type
16879 // tag_type: ?type
1687616880 ensure_field_index(result->type, "tag_type", 1);
1687716881 fields[1].special = ConstValSpecialStatic;
16878 fields[1].type = ira->codegen->builtin_types.entry_type;
16879 // @TODO ?type instead of using @typeOf(undefined) when we have no type.
16882 fields[1].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
16883
1688016884 AstNode *union_decl_node = type_entry->data.unionation.decl_node;
1688116885 if (union_decl_node->data.container_decl.auto_enum ||
1688216886 union_decl_node->data.container_decl.init_arg_expr != nullptr)
1688316887 {
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;
1688516893 }
1688616894 else
16887 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
16895 fields[1].data.x_optional = nullptr;
1688816896 // fields: []TypeInfo.UnionField
1688916897 ensure_field_index(result->type, "fields", 2);
1689016898
......@@ -16913,7 +16921,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1691316921 inner_fields[1].special = ConstValSpecialStatic;
1691416922 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) {
1691716925 inner_fields[1].data.x_optional = nullptr;
1691816926 } else {
1691916927 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
1702217030 ConstExprValue *fields = create_const_vals(6);
1702317031 result->data.x_struct.fields = fields;
1702417032
17025 // @TODO Fix type = undefined with ?type
17026
1702717033 // calling_convention: TypeInfo.CallingConvention
1702817034 ensure_field_index(result->type, "calling_convention", 0);
1702917035 fields[0].special = ConstValSpecialStatic;
......@@ -17041,22 +17047,32 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1704117047 fields[2].special = ConstValSpecialStatic;
1704217048 fields[2].type = ira->codegen->builtin_types.entry_bool;
1704317049 fields[2].data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;
17044 // return_type: type
17050 // return_type: ?type
1704517051 ensure_field_index(result->type, "return_type", 3);
1704617052 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);
1704817054 if (type_entry->data.fn.fn_type_id.return_type == nullptr)
17049 fields[3].data.x_type = ira->codegen->builtin_types.entry_undef;
17050 else
17051 fields[3].data.x_type = type_entry->data.fn.fn_type_id.return_type;
17055 fields[3].data.x_optional = nullptr;
17056 else {
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 }
1705217063 // async_allocator_type: type
1705317064 ensure_field_index(result->type, "async_allocator_type", 4);
1705417065 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);
1705617067 if (type_entry->data.fn.fn_type_id.async_allocator_type == nullptr)
17057 fields[4].data.x_type = ira->codegen->builtin_types.entry_undef;
17058 else
17059 fields[4].data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;
17068 fields[4].data.x_optional = nullptr;
17069 else {
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 }
1706017076 // args: []TypeInfo.FnArg
1706117077 TypeTableEntry *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg");
1706217078 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
1709017106 inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
1709117107 inner_fields[1].data.x_bool = fn_param_info->is_noalias;
1709217108 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
1709517111 if (arg_is_generic)
17096 inner_fields[2].data.x_type = ira->codegen->builtin_types.entry_undef;
17097 else
17098 inner_fields[2].data.x_type = fn_param_info->type;
17112 inner_fields[2].data.x_optional = nullptr;
17113 else {
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
1710017121 fn_arg_val->data.x_struct.fields = inner_fields;
1710117122 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" {
107107fn testPromise() void {
108108 const null_promise_info = @typeInfo(promise);
109109 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
112112 const promise_info = @typeInfo(promise->usize);
113113 assert(TypeId(promise_info) == TypeId.Promise);
114 assert(promise_info.Promise.child == usize);
114 assert(promise_info.Promise.child.? == usize);
115115}
116116
117117test "type info: error set, error union info" {
......@@ -165,7 +165,7 @@ fn testUnion() void {
165165 const typeinfo_info = @typeInfo(TypeInfo);
166166 assert(TypeId(typeinfo_info) == TypeId.Union);
167167 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
168 assert(typeinfo_info.Union.tag_type == TypeId);
168 assert(typeinfo_info.Union.tag_type.? == TypeId);
169169 assert(typeinfo_info.Union.fields.len == 25);
170170 assert(typeinfo_info.Union.fields[4].enum_field != null);
171171 assert(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
......@@ -179,7 +179,7 @@ fn testUnion() void {
179179
180180 const notag_union_info = @typeInfo(TestNoTagUnion);
181181 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);
183183 assert(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto);
184184 assert(notag_union_info.Union.fields.len == 2);
185185 assert(notag_union_info.Union.fields[0].enum_field == null);
......@@ -191,7 +191,7 @@ fn testUnion() void {
191191
192192 const extern_union_info = @typeInfo(TestExternUnion);
193193 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);
195195 assert(extern_union_info.Union.fields[0].enum_field == null);
196196 assert(extern_union_info.Union.fields[0].field_type == *c_void);
197197}
......@@ -238,13 +238,13 @@ fn testFunction() void {
238238 assert(fn_info.Fn.is_generic);
239239 assert(fn_info.Fn.args.len == 2);
240240 assert(fn_info.Fn.is_var_args);
241 assert(fn_info.Fn.return_type == @typeOf(undefined));
242 assert(fn_info.Fn.async_allocator_type == @typeOf(undefined));
241 assert(fn_info.Fn.return_type == null);
242 assert(fn_info.Fn.async_allocator_type == null);
243243
244244 const test_instance: TestStruct = undefined;
245245 const bound_fn_info = @typeInfo(@typeOf(test_instance.foo));
246246 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);
248248}
249249
250250fn foo(comptime a: usize, b: bool, args: ...) usize {