authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-07 19:10:45-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-07 19:10:45-04:00
log688ff2830d82ea36a9f022ecb7cf4c2bf2e4c586
tree311f07ddad8db40e1e9ba9a29b36891b10c28acb
parent31aefa6a2179dfae752020195fb193c6333bae7e

langref: automatic update of builtin.zig

now the docs can't get out of date for this See #367

5 files changed, 51 insertions(+), 428 deletions(-)

doc/docgen.zig+18
...@@ -300,6 +300,7 @@ const Link = struct {...@@ -300,6 +300,7 @@ const Link = struct {
300const Node = union(enum) {300const Node = union(enum) {
301 Content: []const u8,301 Content: []const u8,
302 Nav,302 Nav,
303 Builtin,
303 HeaderOpen: HeaderOpen,304 HeaderOpen: HeaderOpen,
304 SeeAlso: []const SeeAlsoItem,305 SeeAlso: []const SeeAlsoItem,
305 Code: Code,306 Code: Code,
...@@ -356,6 +357,9 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {...@@ -356,6 +357,9 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
356 _ = try eatToken(tokenizer, Token.Id.BracketClose);357 _ = try eatToken(tokenizer, Token.Id.BracketClose);
357358
358 try nodes.append(Node.Nav);359 try nodes.append(Node.Nav);
360 } else if (mem.eql(u8, tag_name, "builtin")) {
361 _ = try eatToken(tokenizer, Token.Id.BracketClose);
362 try nodes.append(Node.Builtin);
359 } else if (mem.eql(u8, tag_name, "header_open")) {363 } else if (mem.eql(u8, tag_name, "header_open")) {
360 _ = try eatToken(tokenizer, Token.Id.Separator);364 _ = try eatToken(tokenizer, Token.Id.Separator);
361 const content_token = try eatToken(tokenizer, Token.Id.TagContent);365 const content_token = try eatToken(tokenizer, Token.Id.TagContent);
...@@ -690,6 +694,9 @@ fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {...@@ -690,6 +694,9 @@ fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
690694
691fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {695fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
692 var code_progress_index: usize = 0;696 var code_progress_index: usize = 0;
697
698 const builtin_code = try escapeHtml(allocator, try getBuiltinCode(allocator, zig_exe));
699
693 for (toc.nodes) |node| {700 for (toc.nodes) |node| {
694 switch (node) {701 switch (node) {
695 Node.Content => |data| {702 Node.Content => |data| {
...@@ -704,6 +711,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var...@@ -704,6 +711,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
704 Node.Nav => {711 Node.Nav => {
705 try out.write(toc.toc);712 try out.write(toc.toc);
706 },713 },
714 Node.Builtin => {
715 try out.print("<pre><code class=\"zig\">{}</code></pre>", builtin_code);
716 },
707 Node.HeaderOpen => |info| {717 Node.HeaderOpen => |info| {
708 try out.print("<h{} id=\"{}\">{}</h{}>\n", info.n, info.url, info.name, info.n);718 try out.print("<h{} id=\"{}\">{}</h{}>\n", info.n, info.url, info.name, info.n);
709 },719 },
...@@ -1060,3 +1070,11 @@ fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex...@@ -1060,3 +1070,11 @@ fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex
1060 }1070 }
1061 return result;1071 return result;
1062}1072}
1073
1074fn getBuiltinCode(allocator: *mem.Allocator, zig_exe: []const u8) ![]const u8 {
1075 const result = try exec(allocator, []const []const u8{
1076 zig_exe,
1077 "builtin",
1078 });
1079 return result.stdout;
1080}
doc/langref.html.in+2-421
...@@ -5474,425 +5474,7 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';...@@ -5474,425 +5474,7 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
5474 <p>5474 <p>
5475 Example of what is imported with <code>@import("builtin")</code>:5475 Example of what is imported with <code>@import("builtin")</code>:
5476 </p>5476 </p>
5477 {#code_begin|syntax#}5477 {#builtin#}
5478pub const StackTrace = struct {
5479 index: usize,
5480 instruction_addresses: []usize,
5481};
5482
5483pub const Os = enum {
5484 freestanding,
5485 ananas,
5486 cloudabi,
5487 dragonfly,
5488 freebsd,
5489 fuchsia,
5490 ios,
5491 kfreebsd,
5492 linux,
5493 lv2,
5494 macosx,
5495 netbsd,
5496 openbsd,
5497 solaris,
5498 windows,
5499 haiku,
5500 minix,
5501 rtems,
5502 nacl,
5503 cnk,
5504 aix,
5505 cuda,
5506 nvcl,
5507 amdhsa,
5508 ps4,
5509 elfiamcu,
5510 tvos,
5511 watchos,
5512 mesa3d,
5513 contiki,
5514 amdpal,
5515 zen,
5516};
5517
5518pub const Arch = enum {
5519 armv8_3a,
5520 armv8_2a,
5521 armv8_1a,
5522 armv8,
5523 armv8r,
5524 armv8m_baseline,
5525 armv8m_mainline,
5526 armv7,
5527 armv7em,
5528 armv7m,
5529 armv7s,
5530 armv7k,
5531 armv7ve,
5532 armv6,
5533 armv6m,
5534 armv6k,
5535 armv6t2,
5536 armv5,
5537 armv5te,
5538 armv4t,
5539 armebv8_3a,
5540 armebv8_2a,
5541 armebv8_1a,
5542 armebv8,
5543 armebv8r,
5544 armebv8m_baseline,
5545 armebv8m_mainline,
5546 armebv7,
5547 armebv7em,
5548 armebv7m,
5549 armebv7s,
5550 armebv7k,
5551 armebv7ve,
5552 armebv6,
5553 armebv6m,
5554 armebv6k,
5555 armebv6t2,
5556 armebv5,
5557 armebv5te,
5558 armebv4t,
5559 aarch64,
5560 aarch64_be,
5561 arc,
5562 avr,
5563 bpfel,
5564 bpfeb,
5565 hexagon,
5566 mips,
5567 mipsel,
5568 mips64,
5569 mips64el,
5570 msp430,
5571 nios2,
5572 powerpc,
5573 powerpc64,
5574 powerpc64le,
5575 r600,
5576 amdgcn,
5577 riscv32,
5578 riscv64,
5579 sparc,
5580 sparcv9,
5581 sparcel,
5582 s390x,
5583 tce,
5584 tcele,
5585 thumb,
5586 thumbeb,
5587 i386,
5588 x86_64,
5589 xcore,
5590 nvptx,
5591 nvptx64,
5592 le32,
5593 le64,
5594 amdil,
5595 amdil64,
5596 hsail,
5597 hsail64,
5598 spir,
5599 spir64,
5600 kalimbav3,
5601 kalimbav4,
5602 kalimbav5,
5603 shave,
5604 lanai,
5605 wasm32,
5606 wasm64,
5607 renderscript32,
5608 renderscript64,
5609};
5610
5611pub const Environ = enum {
5612 unknown,
5613 gnu,
5614 gnuabin32,
5615 gnuabi64,
5616 gnueabi,
5617 gnueabihf,
5618 gnux32,
5619 code16,
5620 eabi,
5621 eabihf,
5622 android,
5623 musl,
5624 musleabi,
5625 musleabihf,
5626 msvc,
5627 itanium,
5628 cygnus,
5629 amdopencl,
5630 coreclr,
5631 opencl,
5632 simulator,
5633};
5634
5635pub const ObjectFormat = enum {
5636 unknown,
5637 coff,
5638 elf,
5639 macho,
5640 wasm,
5641};
5642
5643pub const GlobalLinkage = enum {
5644 Internal,
5645 Strong,
5646 Weak,
5647 LinkOnce,
5648};
5649
5650pub const AtomicOrder = enum {
5651 Unordered,
5652 Monotonic,
5653 Acquire,
5654 Release,
5655 AcqRel,
5656 SeqCst,
5657};
5658
5659pub const AtomicRmwOp = enum {
5660 Xchg,
5661 Add,
5662 Sub,
5663 And,
5664 Nand,
5665 Or,
5666 Xor,
5667 Max,
5668 Min,
5669};
5670
5671pub const Mode = enum {
5672 Debug,
5673 ReleaseSafe,
5674 ReleaseFast,
5675 ReleaseSmall,
5676};
5677
5678pub const TypeId = enum {
5679 Type,
5680 Void,
5681 Bool,
5682 NoReturn,
5683 Int,
5684 Float,
5685 Pointer,
5686 Array,
5687 Struct,
5688 ComptimeFloat,
5689 ComptimeInt,
5690 Undefined,
5691 Null,
5692 Nullable,
5693 ErrorUnion,
5694 ErrorSet,
5695 Enum,
5696 Union,
5697 Fn,
5698 Namespace,
5699 Block,
5700 BoundFn,
5701 ArgTuple,
5702 Opaque,
5703 Promise,
5704};
5705
5706pub const TypeInfo = union(TypeId) {
5707 Type: void,
5708 Void: void,
5709 Bool: void,
5710 NoReturn: void,
5711 Int: Int,
5712 Float: Float,
5713 Pointer: Pointer,
5714 Array: Array,
5715 Struct: Struct,
5716 ComptimeFloat: void,
5717 ComptimeInt: void,
5718 Undefined: void,
5719 Null: void,
5720 Nullable: Nullable,
5721 ErrorUnion: ErrorUnion,
5722 ErrorSet: ErrorSet,
5723 Enum: Enum,
5724 Union: Union,
5725 Fn: Fn,
5726 Namespace: void,
5727 Block: void,
5728 BoundFn: Fn,
5729 ArgTuple: void,
5730 Opaque: void,
5731 Promise: Promise,
5732
5733
5734 pub const Int = struct {
5735 is_signed: bool,
5736 bits: u8,
5737 };
5738
5739 pub const Float = struct {
5740 bits: u8,
5741 };
5742
5743 pub const Pointer = struct {
5744 is_const: bool,
5745 is_volatile: bool,
5746 alignment: u32,
5747 child: type,
5748 };
5749
5750 pub const Array = struct {
5751 len: usize,
5752 child: type,
5753 };
5754
5755 pub const ContainerLayout = enum {
5756 Auto,
5757 Extern,
5758 Packed,
5759 };
5760
5761 pub const StructField = struct {
5762 name: []const u8,
5763 offset: ?usize,
5764 field_type: type,
5765 };
5766
5767 pub const Struct = struct {
5768 layout: ContainerLayout,
5769 fields: []StructField,
5770 defs: []Definition,
5771 };
5772
5773 pub const Nullable = struct {
5774 child: type,
5775 };
5776
5777 pub const ErrorUnion = struct {
5778 error_set: type,
5779 payload: type,
5780 };
5781
5782 pub const Error = struct {
5783 name: []const u8,
5784 value: usize,
5785 };
5786
5787 pub const ErrorSet = struct {
5788 errors: []Error,
5789 };
5790
5791 pub const EnumField = struct {
5792 name: []const u8,
5793 value: usize,
5794 };
5795
5796 pub const Enum = struct {
5797 layout: ContainerLayout,
5798 tag_type: type,
5799 fields: []EnumField,
5800 defs: []Definition,
5801 };
5802
5803 pub const UnionField = struct {
5804 name: []const u8,
5805 enum_field: ?EnumField,
5806 field_type: type,
5807 };
5808
5809 pub const Union = struct {
5810 layout: ContainerLayout,
5811 tag_type: type,
5812 fields: []UnionField,
5813 defs: []Definition,
5814 };
5815
5816 pub const CallingConvention = enum {
5817 Unspecified,
5818 C,
5819 Cold,
5820 Naked,
5821 Stdcall,
5822 Async,
5823 };
5824
5825 pub const FnArg = struct {
5826 is_generic: bool,
5827 is_noalias: bool,
5828 arg_type: type,
5829 };
5830
5831 pub const Fn = struct {
5832 calling_convention: CallingConvention,
5833 is_generic: bool,
5834 is_var_args: bool,
5835 return_type: type,
5836 async_allocator_type: type,
5837 args: []FnArg,
5838 };
5839
5840 pub const Promise = struct {
5841 child: type,
5842 };
5843
5844 pub const Definition = struct {
5845 name: []const u8,
5846 is_pub: bool,
5847 data: Data,
5848
5849 pub const Data = union(enum) {
5850 Type: type,
5851 Var: type,
5852 Fn: FnDef,
5853
5854 pub const FnDef = struct {
5855 fn_type: type,
5856 inline_type: Inline,
5857 calling_convention: CallingConvention,
5858 is_var_args: bool,
5859 is_extern: bool,
5860 is_export: bool,
5861 lib_name: ?[]const u8,
5862 return_type: type,
5863 arg_names: [][] const u8,
5864
5865 pub const Inline = enum {
5866 Auto,
5867 Always,
5868 Never,
5869 };
5870 };
5871 };
5872 };
5873};
5874
5875pub const FloatMode = enum {
5876 Optimized,
5877 Strict,
5878};
5879
5880pub const Endian = enum {
5881 Big,
5882 Little,
5883};
5884
5885pub const endian = Endian.Little;
5886pub const is_test = true;
5887pub const os = Os.linux;
5888pub const arch = Arch.x86_64;
5889pub const environ = Environ.gnu;
5890pub const object_format = ObjectFormat.elf;
5891pub const mode = Mode.Debug;
5892pub const link_libc = false;
5893pub const have_error_return_tracing = true;
5894pub const __zig_test_fn_slice = {}; // overwritten later
5895 {#code_end#}
5896 {#see_also|Build Mode#}5478 {#see_also|Build Mode#}
5897 {#header_close#}5479 {#header_close#}
5898 {#header_open|Root Source File#}5480 {#header_open|Root Source File#}
...@@ -6053,8 +5635,7 @@ pub fn build(b: *Builder) void {...@@ -6053,8 +5635,7 @@ pub fn build(b: *Builder) void {
6053 b.default_step.dependOn(&exe.step);5635 b.default_step.dependOn(&exe.step);
6054}5636}
6055 {#code_end#}5637 {#code_end#}
6056 {#header_close#}5638 <p class="file">terminal</p>
6057 {#header_open|Terminal#}
6058 <pre><code class="shell">$ zig build5639 <pre><code class="shell">$ zig build
6059$ ./test5640$ ./test
6060all your base are belong to us</code></pre>5641all your base are belong to us</code></pre>
src/codegen.cpp+14-7
...@@ -6335,13 +6335,7 @@ static const char *build_mode_to_str(BuildMode build_mode) {...@@ -6335,13 +6335,7 @@ static const char *build_mode_to_str(BuildMode build_mode) {
6335 zig_unreachable();6335 zig_unreachable();
6336}6336}
63376337
6338static void define_builtin_compile_vars(CodeGen *g) {6338Buf *codegen_generate_builtin_source(CodeGen *g) {
6339 if (g->std_package == nullptr)
6340 return;
6341
6342 const char *builtin_zig_basename = "builtin.zig";
6343 Buf *builtin_zig_path = buf_alloc();
6344 os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
6345 Buf *contents = buf_alloc();6339 Buf *contents = buf_alloc();
63466340
6347 // Modifications to this struct must be coordinated with code that does anything with6341 // Modifications to this struct must be coordinated with code that does anything with
...@@ -6707,6 +6701,19 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6707,6 +6701,19 @@ static void define_builtin_compile_vars(CodeGen *g) {
67076701
6708 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");6702 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
67096703
6704
6705 return contents;
6706}
6707
6708static void define_builtin_compile_vars(CodeGen *g) {
6709 if (g->std_package == nullptr)
6710 return;
6711
6712 const char *builtin_zig_basename = "builtin.zig";
6713 Buf *builtin_zig_path = buf_alloc();
6714 os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
6715
6716 Buf *contents = codegen_generate_builtin_source(g);
6710 ensure_cache_dir(g);6717 ensure_cache_dir(g);
6711 os_write_file(builtin_zig_path, contents);6718 os_write_file(builtin_zig_path, contents);
67126719
src/codegen.hpp+2
...@@ -59,5 +59,7 @@ void codegen_add_object(CodeGen *g, Buf *object_path);...@@ -59,5 +59,7 @@ void codegen_add_object(CodeGen *g, Buf *object_path);
5959
60void codegen_translate_c(CodeGen *g, Buf *path);60void codegen_translate_c(CodeGen *g, Buf *path);
6161
62Buf *codegen_generate_builtin_source(CodeGen *g);
63
6264
63#endif65#endif
src/main.cpp+15
...@@ -23,6 +23,7 @@ static int usage(const char *arg0) {...@@ -23,6 +23,7 @@ static int usage(const char *arg0) {
23 " build-exe [source] create executable from source or object files\n"23 " build-exe [source] create executable from source or object files\n"
24 " build-lib [source] create library from source or object files\n"24 " build-lib [source] create library from source or object files\n"
25 " build-obj [source] create object from source or assembly\n"25 " build-obj [source] create object from source or assembly\n"
26 " builtin show the source code of that @import(\"builtin\")\n"
26 " run [source] create executable and run immediately\n"27 " run [source] create executable and run immediately\n"
27 " translate-c [source] convert c code to zig code\n"28 " translate-c [source] convert c code to zig code\n"
28 " targets list available compilation targets\n"29 " targets list available compilation targets\n"
...@@ -214,6 +215,7 @@ static Buf *resolve_zig_lib_dir(void) {...@@ -214,6 +215,7 @@ static Buf *resolve_zig_lib_dir(void) {
214enum Cmd {215enum Cmd {
215 CmdInvalid,216 CmdInvalid,
216 CmdBuild,217 CmdBuild,
218 CmdBuiltin,
217 CmdRun,219 CmdRun,
218 CmdTest,220 CmdTest,
219 CmdVersion,221 CmdVersion,
...@@ -664,6 +666,8 @@ int main(int argc, char **argv) {...@@ -664,6 +666,8 @@ int main(int argc, char **argv) {
664 out_type = OutTypeExe;666 out_type = OutTypeExe;
665 } else if (strcmp(arg, "targets") == 0) {667 } else if (strcmp(arg, "targets") == 0) {
666 cmd = CmdTargets;668 cmd = CmdTargets;
669 } else if (strcmp(arg, "builtin") == 0) {
670 cmd = CmdBuiltin;
667 } else {671 } else {
668 fprintf(stderr, "Unrecognized command: %s\n", arg);672 fprintf(stderr, "Unrecognized command: %s\n", arg);
669 return usage(arg0);673 return usage(arg0);
...@@ -681,6 +685,7 @@ int main(int argc, char **argv) {...@@ -681,6 +685,7 @@ int main(int argc, char **argv) {
681 return usage(arg0);685 return usage(arg0);
682 }686 }
683 break;687 break;
688 case CmdBuiltin:
684 case CmdVersion:689 case CmdVersion:
685 case CmdZen:690 case CmdZen:
686 case CmdTargets:691 case CmdTargets:
...@@ -727,6 +732,16 @@ int main(int argc, char **argv) {...@@ -727,6 +732,16 @@ int main(int argc, char **argv) {
727 }732 }
728733
729 switch (cmd) {734 switch (cmd) {
735 case CmdBuiltin: {
736 Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
737 CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf);
738 Buf *builtin_source = codegen_generate_builtin_source(g);
739 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
740 fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));
741 return EXIT_FAILURE;
742 }
743 return EXIT_SUCCESS;
744 }
730 case CmdRun:745 case CmdRun:
731 case CmdBuild:746 case CmdBuild:
732 case CmdTranslateC:747 case CmdTranslateC: