Skip to content

Commit 688ff28

Browse files
committedJun 7, 2018
langref: automatic update of builtin.zig
now the docs can't get out of date for this See #367
·
0.15.20.3.0
1 parent 31aefa6 commit 688ff28

File tree

5 files changed

+51
-428
lines changed

5 files changed

+51
-428
lines changed
 

‎doc/docgen.zig‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ const Link = struct {
300300
const Node = union(enum) {
301301
Content: []const u8,
302302
Nav,
303+
Builtin,
303304
HeaderOpen: HeaderOpen,
304305
SeeAlso: []const SeeAlsoItem,
305306
Code: Code,
@@ -356,6 +357,9 @@ fn genToc(allocator: *mem.Allocator, tokenizer: *Tokenizer) !Toc {
356357
_ = try eatToken(tokenizer, Token.Id.BracketClose);
357358

358359
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);
359363
} else if (mem.eql(u8, tag_name, "header_open")) {
360364
_ = try eatToken(tokenizer, Token.Id.Separator);
361365
const content_token = try eatToken(tokenizer, Token.Id.TagContent);
@@ -690,6 +694,9 @@ fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {
690694

691695
fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void {
692696
var code_progress_index: usize = 0;
697+
698+
const builtin_code = try escapeHtml(allocator, try getBuiltinCode(allocator, zig_exe));
699+
693700
for (toc.nodes) |node| {
694701
switch (node) {
695702
Node.Content => |data| {
@@ -704,6 +711,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
704711
Node.Nav => {
705712
try out.write(toc.toc);
706713
},
714+
Node.Builtin => {
715+
try out.print("<pre><code class=\"zig\">{}</code></pre>", builtin_code);
716+
},
707717
Node.HeaderOpen => |info| {
708718
try out.print("<h{} id=\"{}\">{}</h{}>\n", info.n, info.url, info.name, info.n);
709719
},
@@ -1060,3 +1070,11 @@ fn exec(allocator: *mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex
10601070
}
10611071
return result;
10621072
}
1073+
1074+
fn 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‎

Lines changed: 2 additions & 421 deletions
Original file line numberDiff line numberDiff line change
@@ -5474,425 +5474,7 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';
54745474
<p>
54755475
Example of what is imported with <code>@import("builtin")</code>:
54765476
</p>
5477-
{#code_begin|syntax#}
5478-
pub const StackTrace = struct {
5479-
index: usize,
5480-
instruction_addresses: []usize,
5481-
};
5482-
5483-
pub 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-
5518-
pub 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-
5611-
pub 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-
5635-
pub const ObjectFormat = enum {
5636-
unknown,
5637-
coff,
5638-
elf,
5639-
macho,
5640-
wasm,
5641-
};
5642-
5643-
pub const GlobalLinkage = enum {
5644-
Internal,
5645-
Strong,
5646-
Weak,
5647-
LinkOnce,
5648-
};
5649-
5650-
pub const AtomicOrder = enum {
5651-
Unordered,
5652-
Monotonic,
5653-
Acquire,
5654-
Release,
5655-
AcqRel,
5656-
SeqCst,
5657-
};
5658-
5659-
pub const AtomicRmwOp = enum {
5660-
Xchg,
5661-
Add,
5662-
Sub,
5663-
And,
5664-
Nand,
5665-
Or,
5666-
Xor,
5667-
Max,
5668-
Min,
5669-
};
5670-
5671-
pub const Mode = enum {
5672-
Debug,
5673-
ReleaseSafe,
5674-
ReleaseFast,
5675-
ReleaseSmall,
5676-
};
5677-
5678-
pub 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-
5706-
pub 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-
5875-
pub const FloatMode = enum {
5876-
Optimized,
5877-
Strict,
5878-
};
5879-
5880-
pub const Endian = enum {
5881-
Big,
5882-
Little,
5883-
};
5884-
5885-
pub const endian = Endian.Little;
5886-
pub const is_test = true;
5887-
pub const os = Os.linux;
5888-
pub const arch = Arch.x86_64;
5889-
pub const environ = Environ.gnu;
5890-
pub const object_format = ObjectFormat.elf;
5891-
pub const mode = Mode.Debug;
5892-
pub const link_libc = false;
5893-
pub const have_error_return_tracing = true;
5894-
pub const __zig_test_fn_slice = {}; // overwritten later
5895-
{#code_end#}
5477+
{#builtin#}
58965478
{#see_also|Build Mode#}
58975479
{#header_close#}
58985480
{#header_open|Root Source File#}
@@ -6053,8 +5635,7 @@ pub fn build(b: *Builder) void {
60535635
b.default_step.dependOn(&exe.step);
60545636
}
60555637
{#code_end#}
6056-
{#header_close#}
6057-
{#header_open|Terminal#}
5638+
<p class="file">terminal</p>
60585639
<pre><code class="shell">$ zig build
60595640
$ ./test
60605641
all your base are belong to us</code></pre>

‎src/codegen.cpp‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6335,13 +6335,7 @@ static const char *build_mode_to_str(BuildMode build_mode) {
63356335
zig_unreachable();
63366336
}
63376337

6338-
static void define_builtin_compile_vars(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);
6338+
Buf *codegen_generate_builtin_source(CodeGen *g) {
63456339
Buf *contents = buf_alloc();
63466340

63476341
// 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) {
67076701

67086702
buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
67096703

6704+
6705+
return contents;
6706+
}
6707+
6708+
static 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);
67106717
ensure_cache_dir(g);
67116718
os_write_file(builtin_zig_path, contents);
67126719

‎src/codegen.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ void codegen_add_object(CodeGen *g, Buf *object_path);
5959

6060
void codegen_translate_c(CodeGen *g, Buf *path);
6161

62+
Buf *codegen_generate_builtin_source(CodeGen *g);
63+
6264

6365
#endif

‎src/main.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static int usage(const char *arg0) {
2323
" build-exe [source] create executable from source or object files\n"
2424
" build-lib [source] create library from source or object files\n"
2525
" build-obj [source] create object from source or assembly\n"
26+
" builtin show the source code of that @import(\"builtin\")\n"
2627
" run [source] create executable and run immediately\n"
2728
" translate-c [source] convert c code to zig code\n"
2829
" targets list available compilation targets\n"
@@ -214,6 +215,7 @@ static Buf *resolve_zig_lib_dir(void) {
214215
enum Cmd {
215216
CmdInvalid,
216217
CmdBuild,
218+
CmdBuiltin,
217219
CmdRun,
218220
CmdTest,
219221
CmdVersion,
@@ -664,6 +666,8 @@ int main(int argc, char **argv) {
664666
out_type = OutTypeExe;
665667
} else if (strcmp(arg, "targets") == 0) {
666668
cmd = CmdTargets;
669+
} else if (strcmp(arg, "builtin") == 0) {
670+
cmd = CmdBuiltin;
667671
} else {
668672
fprintf(stderr, "Unrecognized command: %s\n", arg);
669673
return usage(arg0);
@@ -681,6 +685,7 @@ int main(int argc, char **argv) {
681685
return usage(arg0);
682686
}
683687
break;
688+
case CmdBuiltin:
684689
case CmdVersion:
685690
case CmdZen:
686691
case CmdTargets:
@@ -727,6 +732,16 @@ int main(int argc, char **argv) {
727732
}
728733

729734
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+
}
730745
case CmdRun:
731746
case CmdBuild:
732747
case CmdTranslateC:

0 commit comments

Comments
 (0)
Please sign in to comment.