authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-05-01 13:42:20+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-05-01 13:42:20+03:00
log7d239414f7ed5abad8cdd8b1262590d8f50dd56a
tree2d54fe5628ab70601f7d4c86e80716fb7c14e879
parent1b6e97355d58fbd8aa961d02c3e75c9ca34ed1b9

Fixed type info test, added documentation.


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

doc/langref.html.in+387-4
...@@ -4809,6 +4809,182 @@ pub const TypeId = enum {...@@ -4809,6 +4809,182 @@ pub const TypeId = enum {
4809 BoundFn,4809 BoundFn,
4810 ArgTuple,4810 ArgTuple,
4811 Opaque,4811 Opaque,
4812};
4813 {#code_end#}
4814 {#header_close#}
4815 {#header_open|@typeInfo#}
4816 <pre><code class="zig">@typeInfo(comptime T: type) -&gt; @import("builtin").TypeInfo</code></pre>
4817 <p>
4818 Returns information on the type. Returns a value of the following union:
4819 </p>
4820 {#code_begin|syntax#}
4821pub const TypeInfo = union(TypeId) {
4822 Type: void,
4823 Void: void,
4824 Bool: void,
4825 NoReturn: void,
4826 Int: Int,
4827 Float: Float,
4828 Pointer: Pointer,
4829 Array: Array,
4830 Struct: Struct,
4831 FloatLiteral: void,
4832 IntLiteral: void,
4833 UndefinedLiteral: void,
4834 NullLiteral: void,
4835 Nullable: Nullable,
4836 ErrorUnion: ErrorUnion,
4837 ErrorSet: ErrorSet,
4838 Enum: Enum,
4839 Union: Union,
4840 Fn: Fn,
4841 Namespace: void,
4842 Block: void,
4843 BoundFn: Fn,
4844 ArgTuple: void,
4845 Opaque: void,
4846 Promise: Promise,
4847
4848
4849 pub const Int = struct {
4850 is_signed: bool,
4851 bits: u8,
4852 };
4853
4854 pub const Float = struct {
4855 bits: u8,
4856 };
4857
4858 pub const Pointer = struct {
4859 is_const: bool,
4860 is_volatile: bool,
4861 alignment: u32,
4862 child: type,
4863 };
4864
4865 pub const Array = struct {
4866 len: usize,
4867 child: type,
4868 };
4869
4870 pub const ContainerLayout = enum {
4871 Auto,
4872 Extern,
4873 Packed,
4874 };
4875
4876 pub const StructField = struct {
4877 name: []const u8,
4878 offset: ?usize,
4879 field_type: type,
4880 };
4881
4882 pub const Struct = struct {
4883 layout: ContainerLayout,
4884 fields: []StructField,
4885 defs: []Definition,
4886 };
4887
4888 pub const Nullable = struct {
4889 child: type,
4890 };
4891
4892 pub const ErrorUnion = struct {
4893 error_set: type,
4894 payload: type,
4895 };
4896
4897 pub const Error = struct {
4898 name: []const u8,
4899 value: usize,
4900 };
4901
4902 pub const ErrorSet = struct {
4903 errors: []Error,
4904 };
4905
4906 pub const EnumField = struct {
4907 name: []const u8,
4908 value: usize,
4909 };
4910
4911 pub const Enum = struct {
4912 layout: ContainerLayout,
4913 tag_type: type,
4914 fields: []EnumField,
4915 defs: []Definition,
4916 };
4917
4918 pub const UnionField = struct {
4919 name: []const u8,
4920 enum_field: ?EnumField,
4921 field_type: type,
4922 };
4923
4924 pub const Union = struct {
4925 layout: ContainerLayout,
4926 tag_type: type,
4927 fields: []UnionField,
4928 defs: []Definition,
4929 };
4930
4931 pub const CallingConvention = enum {
4932 Unspecified,
4933 C,
4934 Cold,
4935 Naked,
4936 Stdcall,
4937 Async,
4938 };
4939
4940 pub const FnArg = struct {
4941 is_generic: bool,
4942 is_noalias: bool,
4943 arg_type: type,
4944 };
4945
4946 pub const Fn = struct {
4947 calling_convention: CallingConvention,
4948 is_generic: bool,
4949 is_var_args: bool,
4950 return_type: type,
4951 async_allocator_type: type,
4952 args: []FnArg,
4953 };
4954
4955 pub const Promise = struct {
4956 child: type,
4957 };
4958
4959 pub const Definition = struct {
4960 name: []const u8,
4961 is_pub: bool,
4962 data: Data,
4963
4964 pub const Data = union(enum) {
4965 Type: type,
4966 Var: type,
4967 Fn: FnDef,
4968
4969 pub const FnDef = struct {
4970 fn_type: type,
4971 inline_type: Inline,
4972 calling_convention: CallingConvention,
4973 is_var_args: bool,
4974 is_extern: bool,
4975 is_export: bool,
4976 lib_name: ?[]const u8,
4977 return_type: type,
4978 arg_names: [][] const u8,
4979
4980 pub const Inline = enum {
4981 Auto,
4982 Always,
4983 Never,
4984 };
4985 };
4986 };
4987 };
4812};4988};
4813 {#code_end#}4989 {#code_end#}
4814 {#header_close#}4990 {#header_close#}
...@@ -5226,7 +5402,6 @@ pub const Os = enum {...@@ -5226,7 +5402,6 @@ pub const Os = enum {
5226 rtems,5402 rtems,
5227 nacl,5403 nacl,
5228 cnk,5404 cnk,
5229 bitrig,
5230 aix,5405 aix,
5231 cuda,5406 cuda,
5232 nvcl,5407 nvcl,
...@@ -5237,10 +5412,12 @@ pub const Os = enum {...@@ -5237,10 +5412,12 @@ pub const Os = enum {
5237 watchos,5412 watchos,
5238 mesa3d,5413 mesa3d,
5239 contiki,5414 contiki,
5415 amdpal,
5240 zen,5416 zen,
5241};5417};
52425418
5243pub const Arch = enum {5419pub const Arch = enum {
5420 armv8_3a,
5244 armv8_2a,5421 armv8_2a,
5245 armv8_1a,5422 armv8_1a,
5246 armv8,5423 armv8,
...@@ -5260,9 +5437,29 @@ pub const Arch = enum {...@@ -5260,9 +5437,29 @@ pub const Arch = enum {
5260 armv5,5437 armv5,
5261 armv5te,5438 armv5te,
5262 armv4t,5439 armv4t,
5263 armeb,5440 armebv8_3a,
5441 armebv8_2a,
5442 armebv8_1a,
5443 armebv8,
5444 armebv8r,
5445 armebv8m_baseline,
5446 armebv8m_mainline,
5447 armebv7,
5448 armebv7em,
5449 armebv7m,
5450 armebv7s,
5451 armebv7k,
5452 armebv7ve,
5453 armebv6,
5454 armebv6m,
5455 armebv6k,
5456 armebv6t2,
5457 armebv5,
5458 armebv5te,
5459 armebv4t,
5264 aarch64,5460 aarch64,
5265 aarch64_be,5461 aarch64_be,
5462 arc,
5266 avr,5463 avr,
5267 bpfel,5464 bpfel,
5268 bpfeb,5465 bpfeb,
...@@ -5315,6 +5512,7 @@ pub const Arch = enum {...@@ -5315,6 +5512,7 @@ pub const Arch = enum {
5315pub const Environ = enum {5512pub const Environ = enum {
5316 unknown,5513 unknown,
5317 gnu,5514 gnu,
5515 gnuabin32,
5318 gnuabi64,5516 gnuabi64,
5319 gnueabi,5517 gnueabi,
5320 gnueabihf,5518 gnueabihf,
...@@ -5332,6 +5530,7 @@ pub const Environ = enum {...@@ -5332,6 +5530,7 @@ pub const Environ = enum {
5332 amdopencl,5530 amdopencl,
5333 coreclr,5531 coreclr,
5334 opencl,5532 opencl,
5533 simulator,
5335};5534};
53365535
5337pub const ObjectFormat = enum {5536pub const ObjectFormat = enum {
...@@ -5358,10 +5557,23 @@ pub const AtomicOrder = enum {...@@ -5358,10 +5557,23 @@ pub const AtomicOrder = enum {
5358 SeqCst,5557 SeqCst,
5359};5558};
53605559
5560pub const AtomicRmwOp = enum {
5561 Xchg,
5562 Add,
5563 Sub,
5564 And,
5565 Nand,
5566 Or,
5567 Xor,
5568 Max,
5569 Min,
5570};
5571
5361pub const Mode = enum {5572pub const Mode = enum {
5362 Debug,5573 Debug,
5363 ReleaseSafe,5574 ReleaseSafe,
5364 ReleaseFast,5575 ReleaseFast,
5576 ReleaseSmall,
5365};5577};
53665578
5367pub const TypeId = enum {5579pub const TypeId = enum {
...@@ -5380,7 +5592,7 @@ pub const TypeId = enum {...@@ -5380,7 +5592,7 @@ pub const TypeId = enum {
5380 NullLiteral,5592 NullLiteral,
5381 Nullable,5593 Nullable,
5382 ErrorUnion,5594 ErrorUnion,
5383 Error,5595 ErrorSet,
5384 Enum,5596 Enum,
5385 Union,5597 Union,
5386 Fn,5598 Fn,
...@@ -5389,6 +5601,176 @@ pub const TypeId = enum {...@@ -5389,6 +5601,176 @@ pub const TypeId = enum {
5389 BoundFn,5601 BoundFn,
5390 ArgTuple,5602 ArgTuple,
5391 Opaque,5603 Opaque,
5604 Promise,
5605};
5606
5607pub const TypeInfo = union(TypeId) {
5608 Type: void,
5609 Void: void,
5610 Bool: void,
5611 NoReturn: void,
5612 Int: Int,
5613 Float: Float,
5614 Pointer: Pointer,
5615 Array: Array,
5616 Struct: Struct,
5617 FloatLiteral: void,
5618 IntLiteral: void,
5619 UndefinedLiteral: void,
5620 NullLiteral: void,
5621 Nullable: Nullable,
5622 ErrorUnion: ErrorUnion,
5623 ErrorSet: ErrorSet,
5624 Enum: Enum,
5625 Union: Union,
5626 Fn: Fn,
5627 Namespace: void,
5628 Block: void,
5629 BoundFn: Fn,
5630 ArgTuple: void,
5631 Opaque: void,
5632 Promise: Promise,
5633
5634
5635 pub const Int = struct {
5636 is_signed: bool,
5637 bits: u8,
5638 };
5639
5640 pub const Float = struct {
5641 bits: u8,
5642 };
5643
5644 pub const Pointer = struct {
5645 is_const: bool,
5646 is_volatile: bool,
5647 alignment: u32,
5648 child: type,
5649 };
5650
5651 pub const Array = struct {
5652 len: usize,
5653 child: type,
5654 };
5655
5656 pub const ContainerLayout = enum {
5657 Auto,
5658 Extern,
5659 Packed,
5660 };
5661
5662 pub const StructField = struct {
5663 name: []const u8,
5664 offset: ?usize,
5665 field_type: type,
5666 };
5667
5668 pub const Struct = struct {
5669 layout: ContainerLayout,
5670 fields: []StructField,
5671 defs: []Definition,
5672 };
5673
5674 pub const Nullable = struct {
5675 child: type,
5676 };
5677
5678 pub const ErrorUnion = struct {
5679 error_set: type,
5680 payload: type,
5681 };
5682
5683 pub const Error = struct {
5684 name: []const u8,
5685 value: usize,
5686 };
5687
5688 pub const ErrorSet = struct {
5689 errors: []Error,
5690 };
5691
5692 pub const EnumField = struct {
5693 name: []const u8,
5694 value: usize,
5695 };
5696
5697 pub const Enum = struct {
5698 layout: ContainerLayout,
5699 tag_type: type,
5700 fields: []EnumField,
5701 defs: []Definition,
5702 };
5703
5704 pub const UnionField = struct {
5705 name: []const u8,
5706 enum_field: ?EnumField,
5707 field_type: type,
5708 };
5709
5710 pub const Union = struct {
5711 layout: ContainerLayout,
5712 tag_type: type,
5713 fields: []UnionField,
5714 defs: []Definition,
5715 };
5716
5717 pub const CallingConvention = enum {
5718 Unspecified,
5719 C,
5720 Cold,
5721 Naked,
5722 Stdcall,
5723 Async,
5724 };
5725
5726 pub const FnArg = struct {
5727 is_generic: bool,
5728 is_noalias: bool,
5729 arg_type: type,
5730 };
5731
5732 pub const Fn = struct {
5733 calling_convention: CallingConvention,
5734 is_generic: bool,
5735 is_var_args: bool,
5736 return_type: type,
5737 async_allocator_type: type,
5738 args: []FnArg,
5739 };
5740
5741 pub const Promise = struct {
5742 child: type,
5743 };
5744
5745 pub const Definition = struct {
5746 name: []const u8,
5747 is_pub: bool,
5748 data: Data,
5749
5750 pub const Data = union(enum) {
5751 Type: type,
5752 Var: type,
5753 Fn: FnDef,
5754
5755 pub const FnDef = struct {
5756 fn_type: type,
5757 inline_type: Inline,
5758 calling_convention: CallingConvention,
5759 is_var_args: bool,
5760 is_extern: bool,
5761 is_export: bool,
5762 lib_name: ?[]const u8,
5763 return_type: type,
5764 arg_names: [][] const u8,
5765
5766 pub const Inline = enum {
5767 Auto,
5768 Always,
5769 Never,
5770 };
5771 };
5772 };
5773 };
5392};5774};
53935775
5394pub const FloatMode = enum {5776pub const FloatMode = enum {
...@@ -5402,7 +5784,7 @@ pub const Endian = enum {...@@ -5402,7 +5784,7 @@ pub const Endian = enum {
5402};5784};
54035785
5404pub const endian = Endian.Little;5786pub const endian = Endian.Little;
5405pub const is_test = false;5787pub const is_test = true;
5406pub const os = Os.linux;5788pub const os = Os.linux;
5407pub const arch = Arch.x86_64;5789pub const arch = Arch.x86_64;
5408pub const environ = Environ.gnu;5790pub const environ = Environ.gnu;
...@@ -5410,6 +5792,7 @@ pub const object_format = ObjectFormat.elf;...@@ -5410,6 +5792,7 @@ pub const object_format = ObjectFormat.elf;
5410pub const mode = Mode.Debug;5792pub const mode = Mode.Debug;
5411pub const link_libc = false;5793pub const link_libc = false;
5412pub const have_error_return_tracing = true;5794pub const have_error_return_tracing = true;
5795pub const __zig_test_fn_slice = {}; // overwritten later
5413 {#code_end#}5796 {#code_end#}
5414 {#see_also|Build Mode#}5797 {#see_also|Build Mode#}
5415 {#header_close#}5798 {#header_close#}
test/cases/type_info.zig+2-2
...@@ -70,7 +70,7 @@ test "type info: error set, error union info" {...@@ -70,7 +70,7 @@ test "type info: error set, error union info" {
70 assert(TypeId(error_set_info) == TypeId.ErrorSet);70 assert(TypeId(error_set_info) == TypeId.ErrorSet);
71 assert(error_set_info.ErrorSet.errors.len == 3);71 assert(error_set_info.ErrorSet.errors.len == 3);
72 assert(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First"));72 assert(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First"));
73 assert(error_set_info.ErrorSet.errors[2].value == 3);73 assert(error_set_info.ErrorSet.errors[2].value == usize(TestErrorSet.Third));
7474
75 const error_union_info = @typeInfo(TestErrorSet!usize);75 const error_union_info = @typeInfo(TestErrorSet!usize);
76 assert(TypeId(error_union_info) == TypeId.ErrorUnion);76 assert(TypeId(error_union_info) == TypeId.ErrorUnion);
...@@ -103,7 +103,7 @@ test "type info: union info" {...@@ -103,7 +103,7 @@ test "type info: union info" {
103 assert(typeinfo_info.Union.fields.len == 25);103 assert(typeinfo_info.Union.fields.len == 25);
104 assert(typeinfo_info.Union.fields[4].enum_field != null);104 assert(typeinfo_info.Union.fields[4].enum_field != null);
105 assert((??typeinfo_info.Union.fields[4].enum_field).value == 4);105 assert((??typeinfo_info.Union.fields[4].enum_field).value == 4);
106 assert(typeinfo_info.Union.fields[4].field_type == @typeOf(u8_info.Int));106 assert(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));
107 assert(typeinfo_info.Union.defs.len == 20);107 assert(typeinfo_info.Union.defs.len == 20);
108108
109 const TestNoTagUnion = union {109 const TestNoTagUnion = union {