Commit 688ff2830d

Andrew Kelley <superjoe30@gmail.com>
2018-06-08 01:10:45
langref: automatic update of builtin.zig
now the docs can't get out of date for this See #367
1 parent 31aefa6
doc/docgen.zig
@@ -300,6 +300,7 @@ const Link = struct {
 const Node = union(enum) {
     Content: []const u8,
     Nav,
+    Builtin,
     HeaderOpen: HeaderOpen,
     SeeAlso: []const SeeAlsoItem,
     Code: Code,
@@ -356,6 +357,9 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
                     _ = try eatToken(tokenizer, Token.Id.BracketClose);
 
                     try nodes.append(Node.Nav);
+                } else if (mem.eql(u8, tag_name, "builtin")) {
+                    _ = try eatToken(tokenizer, Token.Id.BracketClose);
+                    try nodes.append(Node.Builtin);
                 } else if (mem.eql(u8, tag_name, "header_open")) {
                     _ = try eatToken(tokenizer, Token.Id.Separator);
                     const content_token = try eatToken(tokenizer, Token.Id.TagContent);
@@ -690,6 +694,9 @@ fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
 
 fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
     var code_progress_index: usize = 0;
+
+    const builtin_code = try escapeHtml(allocator, try getBuiltinCode(allocator, zig_exe));
+
     for (toc.nodes) |node| {
         switch (node) {
             Node.Content => |data| {
@@ -704,6 +711,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
             Node.Nav => {
                 try out.write(toc.toc);
             },
+            Node.Builtin => {
+                try out.print("<pre><code class=\"zig\">{}</code></pre>", builtin_code);
+            },
             Node.HeaderOpen => |info| {
                 try out.print("<h{} id=\"{}\">{}</h{}>\n", info.n, info.url, info.name, info.n);
             },
@@ -1060,3 +1070,11 @@ fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex
     }
     return result;
 }
+
+fn getBuiltinCode(allocator: *mem.Allocator, zig_exe: []const u8) ![]const u8 {
+    const result = try exec(allocator, []const []const u8{
+        zig_exe,
+        "builtin",
+    });
+    return result.stdout;
+}
doc/langref.html.in
@@ -5474,425 +5474,7 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
       <p>
       Example of what is imported with <code>@import("builtin")</code>:
       </p>
-      {#code_begin|syntax#}
-pub const StackTrace = struct {
-    index: usize,
-    instruction_addresses: []usize,
-};
-
-pub const Os = enum {
-    freestanding,
-    ananas,
-    cloudabi,
-    dragonfly,
-    freebsd,
-    fuchsia,
-    ios,
-    kfreebsd,
-    linux,
-    lv2,
-    macosx,
-    netbsd,
-    openbsd,
-    solaris,
-    windows,
-    haiku,
-    minix,
-    rtems,
-    nacl,
-    cnk,
-    aix,
-    cuda,
-    nvcl,
-    amdhsa,
-    ps4,
-    elfiamcu,
-    tvos,
-    watchos,
-    mesa3d,
-    contiki,
-    amdpal,
-    zen,
-};
-
-pub const Arch = enum {
-    armv8_3a,
-    armv8_2a,
-    armv8_1a,
-    armv8,
-    armv8r,
-    armv8m_baseline,
-    armv8m_mainline,
-    armv7,
-    armv7em,
-    armv7m,
-    armv7s,
-    armv7k,
-    armv7ve,
-    armv6,
-    armv6m,
-    armv6k,
-    armv6t2,
-    armv5,
-    armv5te,
-    armv4t,
-    armebv8_3a,
-    armebv8_2a,
-    armebv8_1a,
-    armebv8,
-    armebv8r,
-    armebv8m_baseline,
-    armebv8m_mainline,
-    armebv7,
-    armebv7em,
-    armebv7m,
-    armebv7s,
-    armebv7k,
-    armebv7ve,
-    armebv6,
-    armebv6m,
-    armebv6k,
-    armebv6t2,
-    armebv5,
-    armebv5te,
-    armebv4t,
-    aarch64,
-    aarch64_be,
-    arc,
-    avr,
-    bpfel,
-    bpfeb,
-    hexagon,
-    mips,
-    mipsel,
-    mips64,
-    mips64el,
-    msp430,
-    nios2,
-    powerpc,
-    powerpc64,
-    powerpc64le,
-    r600,
-    amdgcn,
-    riscv32,
-    riscv64,
-    sparc,
-    sparcv9,
-    sparcel,
-    s390x,
-    tce,
-    tcele,
-    thumb,
-    thumbeb,
-    i386,
-    x86_64,
-    xcore,
-    nvptx,
-    nvptx64,
-    le32,
-    le64,
-    amdil,
-    amdil64,
-    hsail,
-    hsail64,
-    spir,
-    spir64,
-    kalimbav3,
-    kalimbav4,
-    kalimbav5,
-    shave,
-    lanai,
-    wasm32,
-    wasm64,
-    renderscript32,
-    renderscript64,
-};
-
-pub const Environ = enum {
-    unknown,
-    gnu,
-    gnuabin32,
-    gnuabi64,
-    gnueabi,
-    gnueabihf,
-    gnux32,
-    code16,
-    eabi,
-    eabihf,
-    android,
-    musl,
-    musleabi,
-    musleabihf,
-    msvc,
-    itanium,
-    cygnus,
-    amdopencl,
-    coreclr,
-    opencl,
-    simulator,
-};
-
-pub const ObjectFormat = enum {
-    unknown,
-    coff,
-    elf,
-    macho,
-    wasm,
-};
-
-pub const GlobalLinkage = enum {
-    Internal,
-    Strong,
-    Weak,
-    LinkOnce,
-};
-
-pub const AtomicOrder = enum {
-    Unordered,
-    Monotonic,
-    Acquire,
-    Release,
-    AcqRel,
-    SeqCst,
-};
-
-pub const AtomicRmwOp = enum {
-    Xchg,
-    Add,
-    Sub,
-    And,
-    Nand,
-    Or,
-    Xor,
-    Max,
-    Min,
-};
-
-pub const Mode = enum {
-    Debug,
-    ReleaseSafe,
-    ReleaseFast,
-    ReleaseSmall,
-};
-
-pub const TypeId = enum {
-    Type,
-    Void,
-    Bool,
-    NoReturn,
-    Int,
-    Float,
-    Pointer,
-    Array,
-    Struct,
-    ComptimeFloat,
-    ComptimeInt,
-    Undefined,
-    Null,
-    Nullable,
-    ErrorUnion,
-    ErrorSet,
-    Enum,
-    Union,
-    Fn,
-    Namespace,
-    Block,
-    BoundFn,
-    ArgTuple,
-    Opaque,
-    Promise,
-};
-
-pub const TypeInfo = union(TypeId) {
-    Type: void,
-    Void: void,
-    Bool: void,
-    NoReturn: void,
-    Int: Int,
-    Float: Float,
-    Pointer: Pointer,
-    Array: Array,
-    Struct: Struct,
-    ComptimeFloat: void,
-    ComptimeInt: void,
-    Undefined: void,
-    Null: void,
-    Nullable: Nullable,
-    ErrorUnion: ErrorUnion,
-    ErrorSet: ErrorSet,
-    Enum: Enum,
-    Union: Union,
-    Fn: Fn,
-    Namespace: void,
-    Block: void,
-    BoundFn: Fn,
-    ArgTuple: void,
-    Opaque: void,
-    Promise: Promise,
-
-
-    pub const Int = struct {
-        is_signed: bool,
-        bits: u8,
-    };
-
-    pub const Float = struct {
-        bits: u8,
-    };
-
-    pub const Pointer = struct {
-        is_const: bool,
-        is_volatile: bool,
-        alignment: u32,
-        child: type,
-    };
-
-    pub const Array = struct {
-        len: usize,
-        child: type,
-    };
-
-    pub const ContainerLayout = enum {
-        Auto,
-        Extern,
-        Packed,
-    };
-
-    pub const StructField = struct {
-        name: []const u8,
-        offset: ?usize,
-        field_type: type,
-    };
-
-    pub const Struct = struct {
-        layout: ContainerLayout,
-        fields: []StructField,
-        defs: []Definition,
-    };
-
-    pub const Nullable = struct {
-        child: type,
-    };
-
-    pub const ErrorUnion = struct {
-        error_set: type,
-        payload: type,
-    };
-
-    pub const Error = struct {
-        name: []const u8,
-        value: usize,
-    };
-
-    pub const ErrorSet = struct {
-        errors: []Error,
-    };
-
-    pub const EnumField = struct {
-        name: []const u8,
-        value: usize,
-    };
-
-    pub const Enum = struct {
-        layout: ContainerLayout,
-        tag_type: type,
-        fields: []EnumField,
-        defs: []Definition,
-    };
-
-    pub const UnionField = struct {
-        name: []const u8,
-        enum_field: ?EnumField,
-        field_type: type,
-    };
-
-    pub const Union = struct {
-        layout: ContainerLayout,
-        tag_type: type,
-        fields: []UnionField,
-        defs: []Definition,
-    };
-
-    pub const CallingConvention = enum {
-        Unspecified,
-        C,
-        Cold,
-        Naked,
-        Stdcall,
-        Async,
-    };
-
-    pub const FnArg = struct {
-        is_generic: bool,
-        is_noalias: bool,
-        arg_type: type,
-    };
-
-    pub const Fn = struct {
-        calling_convention: CallingConvention,
-        is_generic: bool,
-        is_var_args: bool,
-        return_type: type,
-        async_allocator_type: type,
-        args: []FnArg,
-    };
-
-    pub const Promise = struct {
-        child: type,
-    };
-
-    pub const Definition = struct {
-        name: []const u8,
-        is_pub: bool,
-        data: Data,
-
-        pub const Data = union(enum) {
-            Type: type,
-            Var: type,
-            Fn: FnDef,
-
-            pub const FnDef = struct {
-                fn_type: type,
-                inline_type: Inline,
-                calling_convention: CallingConvention,
-                is_var_args: bool,
-                is_extern: bool,
-                is_export: bool,
-                lib_name: ?[]const u8,
-                return_type: type,
-                arg_names: [][] const u8,
-
-                pub const Inline = enum {
-                    Auto,
-                    Always,
-                    Never,
-                };
-            };
-        };
-    };
-};
-
-pub const FloatMode = enum {
-    Optimized,
-    Strict,
-};
-
-pub const Endian = enum {
-    Big,
-    Little,
-};
-
-pub const endian = Endian.Little;
-pub const is_test = true;
-pub const os = Os.linux;
-pub const arch = Arch.x86_64;
-pub const environ = Environ.gnu;
-pub const object_format = ObjectFormat.elf;
-pub const mode = Mode.Debug;
-pub const link_libc = false;
-pub const have_error_return_tracing = true;
-pub const __zig_test_fn_slice = {}; // overwritten later
-      {#code_end#}
+      {#builtin#}
       {#see_also|Build Mode#}
       {#header_close#}
       {#header_open|Root Source File#}
@@ -6053,8 +5635,7 @@ pub fn build(b: *Builder) void {
     b.default_step.dependOn(&exe.step);
 }
       {#code_end#}
-      {#header_close#}
-      {#header_open|Terminal#}
+      <p class="file">terminal</p>
       <pre><code class="shell">$ zig build
 $ ./test
 all your base are belong to us</code></pre>
src/codegen.cpp
@@ -6335,13 +6335,7 @@ static const char *build_mode_to_str(BuildMode build_mode) {
     zig_unreachable();
 }
 
-static void define_builtin_compile_vars(CodeGen *g) {
-    if (g->std_package == nullptr)
-        return;
-
-    const char *builtin_zig_basename = "builtin.zig";
-    Buf *builtin_zig_path = buf_alloc();
-    os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
+Buf *codegen_generate_builtin_source(CodeGen *g) {
     Buf *contents = buf_alloc();
 
     // 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) {
 
     buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
 
+
+    return contents;
+}
+
+static void define_builtin_compile_vars(CodeGen *g) {
+    if (g->std_package == nullptr)
+        return;
+
+    const char *builtin_zig_basename = "builtin.zig";
+    Buf *builtin_zig_path = buf_alloc();
+    os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
+
+    Buf *contents = codegen_generate_builtin_source(g);
     ensure_cache_dir(g);
     os_write_file(builtin_zig_path, contents);
 
src/codegen.hpp
@@ -59,5 +59,7 @@ void codegen_add_object(CodeGen *g, Buf *object_path);
 
 void codegen_translate_c(CodeGen *g, Buf *path);
 
+Buf *codegen_generate_builtin_source(CodeGen *g);
+
 
 #endif
src/main.cpp
@@ -23,6 +23,7 @@ static int usage(const char *arg0) {
         "  build-exe [source]           create executable from source or object files\n"
         "  build-lib [source]           create library from source or object files\n"
         "  build-obj [source]           create object from source or assembly\n"
+        "  builtin                      show the source code of that @import(\"builtin\")\n"
         "  run [source]                 create executable and run immediately\n"
         "  translate-c [source]         convert c code to zig code\n"
         "  targets                      list available compilation targets\n"
@@ -214,6 +215,7 @@ static Buf *resolve_zig_lib_dir(void) {
 enum Cmd {
     CmdInvalid,
     CmdBuild,
+    CmdBuiltin,
     CmdRun,
     CmdTest,
     CmdVersion,
@@ -664,6 +666,8 @@ int main(int argc, char **argv) {
                 out_type = OutTypeExe;
             } else if (strcmp(arg, "targets") == 0) {
                 cmd = CmdTargets;
+            } else if (strcmp(arg, "builtin") == 0) {
+                cmd = CmdBuiltin;
             } else {
                 fprintf(stderr, "Unrecognized command: %s\n", arg);
                 return usage(arg0);
@@ -681,6 +685,7 @@ int main(int argc, char **argv) {
                         return usage(arg0);
                     }
                     break;
+                case CmdBuiltin:
                 case CmdVersion:
                 case CmdZen:
                 case CmdTargets:
@@ -727,6 +732,16 @@ int main(int argc, char **argv) {
     }
 
     switch (cmd) {
+    case CmdBuiltin: {
+        Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
+        CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf);
+        Buf *builtin_source = codegen_generate_builtin_source(g);
+        if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
+            fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));
+            return EXIT_FAILURE;
+        }
+        return EXIT_SUCCESS;
+    }
     case CmdRun:
     case CmdBuild:
     case CmdTranslateC: