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 {
48094809 BoundFn,
48104810 ArgTuple,
48114811 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 };
48124988};
48134989 {#code_end#}
48144990 {#header_close#}
......@@ -5226,7 +5402,6 @@ pub const Os = enum {
52265402 rtems,
52275403 nacl,
52285404 cnk,
5229 bitrig,
52305405 aix,
52315406 cuda,
52325407 nvcl,
......@@ -5237,10 +5412,12 @@ pub const Os = enum {
52375412 watchos,
52385413 mesa3d,
52395414 contiki,
5415 amdpal,
52405416 zen,
52415417};
52425418
52435419pub const Arch = enum {
5420 armv8_3a,
52445421 armv8_2a,
52455422 armv8_1a,
52465423 armv8,
......@@ -5260,9 +5437,29 @@ pub const Arch = enum {
52605437 armv5,
52615438 armv5te,
52625439 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,
52645460 aarch64,
52655461 aarch64_be,
5462 arc,
52665463 avr,
52675464 bpfel,
52685465 bpfeb,
......@@ -5315,6 +5512,7 @@ pub const Arch = enum {
53155512pub const Environ = enum {
53165513 unknown,
53175514 gnu,
5515 gnuabin32,
53185516 gnuabi64,
53195517 gnueabi,
53205518 gnueabihf,
......@@ -5332,6 +5530,7 @@ pub const Environ = enum {
53325530 amdopencl,
53335531 coreclr,
53345532 opencl,
5533 simulator,
53355534};
53365535
53375536pub const ObjectFormat = enum {
......@@ -5358,10 +5557,23 @@ pub const AtomicOrder = enum {
53585557 SeqCst,
53595558};
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
53615572pub const Mode = enum {
53625573 Debug,
53635574 ReleaseSafe,
53645575 ReleaseFast,
5576 ReleaseSmall,
53655577};
53665578
53675579pub const TypeId = enum {
......@@ -5380,7 +5592,7 @@ pub const TypeId = enum {
53805592 NullLiteral,
53815593 Nullable,
53825594 ErrorUnion,
5383 Error,
5595 ErrorSet,
53845596 Enum,
53855597 Union,
53865598 Fn,
......@@ -5389,6 +5601,176 @@ pub const TypeId = enum {
53895601 BoundFn,
53905602 ArgTuple,
53915603 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 };
53925774};
53935775
53945776pub const FloatMode = enum {
......@@ -5402,7 +5784,7 @@ pub const Endian = enum {
54025784};
54035785
54045786pub const endian = Endian.Little;
5405pub const is_test = false;
5787pub const is_test = true;
54065788pub const os = Os.linux;
54075789pub const arch = Arch.x86_64;
54085790pub const environ = Environ.gnu;
......@@ -5410,6 +5792,7 @@ pub const object_format = ObjectFormat.elf;
54105792pub const mode = Mode.Debug;
54115793pub const link_libc = false;
54125794pub const have_error_return_tracing = true;
5795pub const __zig_test_fn_slice = {}; // overwritten later
54135796 {#code_end#}
54145797 {#see_also|Build Mode#}
54155798 {#header_close#}
test/cases/type_info.zig+2-2
......@@ -70,7 +70,7 @@ test "type info: error set, error union info" {
7070 assert(TypeId(error_set_info) == TypeId.ErrorSet);
7171 assert(error_set_info.ErrorSet.errors.len == 3);
7272 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
7575 const error_union_info = @typeInfo(TestErrorSet!usize);
7676 assert(TypeId(error_union_info) == TypeId.ErrorUnion);
......@@ -103,7 +103,7 @@ test "type info: union info" {
103103 assert(typeinfo_info.Union.fields.len == 25);
104104 assert(typeinfo_info.Union.fields[4].enum_field != null);
105105 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));
107107 assert(typeinfo_info.Union.defs.len == 20);
108108
109109 const TestNoTagUnion = union {