authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-18 13:30:25-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-05-18 13:30:25-04:00
log83a78094788038fcf487e5947945a1f5900d564a
treea8639a11bea5a2fc2b9c5aebf30011f7675f37d7
parent942d384831196acf24868c32ef84409b05441960
parentc38b165db4a16ba6a5c6d13537177db656fc4033
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1019 from zig-lang/pointer-reform

Pointer Reform - change prefix deref syntax to postfix deref syntax

117 files changed, 6037 insertions(+), 4268 deletions(-)

build.zig+34-25
......@@ -16,7 +16,7 @@ pub fn build(b: &Builder) !void {
1616 var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
1717
1818 const rel_zig_exe = try os.path.relative(b.allocator, b.build_root, b.zig_exe);
19 var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8 {
19 var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8{
2020 docgen_exe.getOutputPath(),
2121 rel_zig_exe,
2222 "doc/langref.html.in",
......@@ -30,7 +30,10 @@ pub fn build(b: &Builder) !void {
3030 const test_step = b.step("test", "Run all the tests");
3131
3232 // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
33 const build_info = try b.exec([][]const u8{b.zig_exe, "BUILD_INFO"});
33 const build_info = try b.exec([][]const u8{
34 b.zig_exe,
35 "BUILD_INFO",
36 });
3437 var index: usize = 0;
3538 const cmake_binary_dir = nextValue(&index, build_info);
3639 const cxx_compiler = nextValue(&index, build_info);
......@@ -67,7 +70,10 @@ pub fn build(b: &Builder) !void {
6770 dependOnLib(exe, llvm);
6871
6972 if (exe.target.getOs() == builtin.Os.linux) {
70 const libstdcxx_path_padded = try b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
73 const libstdcxx_path_padded = try b.exec([][]const u8{
74 cxx_compiler,
75 "-print-file-name=libstdc++.a",
76 });
7177 const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
7278 if (mem.eql(u8, libstdcxx_path, "libstdc++.a")) {
7379 warn(
......@@ -111,17 +117,11 @@ pub fn build(b: &Builder) !void {
111117
112118 test_step.dependOn(docs_step);
113119
114 test_step.dependOn(tests.addPkgTests(b, test_filter,
115 "test/behavior.zig", "behavior", "Run the behavior tests",
116 with_lldb));
120 test_step.dependOn(tests.addPkgTests(b, test_filter, "test/behavior.zig", "behavior", "Run the behavior tests", with_lldb));
117121
118 test_step.dependOn(tests.addPkgTests(b, test_filter,
119 "std/index.zig", "std", "Run the standard library tests",
120 with_lldb));
122 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/index.zig", "std", "Run the standard library tests", with_lldb));
121123
122 test_step.dependOn(tests.addPkgTests(b, test_filter,
123 "std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests",
124 with_lldb));
124 test_step.dependOn(tests.addPkgTests(b, test_filter, "std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests", with_lldb));
125125
126126 test_step.dependOn(tests.addCompareOutputTests(b, test_filter));
127127 test_step.dependOn(tests.addBuildExampleTests(b, test_filter));
......@@ -149,8 +149,7 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) vo
149149
150150fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) void {
151151 const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
152 lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
153 b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
152 lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp", b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
154153}
155154
156155const LibraryDep = struct {
......@@ -161,11 +160,21 @@ const LibraryDep = struct {
161160};
162161
163162fn findLLVM(b: &Builder, llvm_config_exe: []const u8) !LibraryDep {
164 const libs_output = try b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"});
165 const includes_output = try b.exec([][]const u8{llvm_config_exe, "--includedir"});
166 const libdir_output = try b.exec([][]const u8{llvm_config_exe, "--libdir"});
163 const libs_output = try b.exec([][]const u8{
164 llvm_config_exe,
165 "--libs",
166 "--system-libs",
167 });
168 const includes_output = try b.exec([][]const u8{
169 llvm_config_exe,
170 "--includedir",
171 });
172 const libdir_output = try b.exec([][]const u8{
173 llvm_config_exe,
174 "--libdir",
175 });
167176
168 var result = LibraryDep {
177 var result = LibraryDep{
169178 .libs = ArrayList([]const u8).init(b.allocator),
170179 .system_libs = ArrayList([]const u8).init(b.allocator),
171180 .includes = ArrayList([]const u8).init(b.allocator),
......@@ -227,17 +236,17 @@ pub fn installCHeaders(b: &Builder, c_header_files: []const u8) void {
227236}
228237
229238fn nextValue(index: &usize, build_info: []const u8) []const u8 {
230 const start = *index;
231 while (true) : (*index += 1) {
232 switch (build_info[*index]) {
239 const start = index.*;
240 while (true) : (index.* += 1) {
241 switch (build_info[index.*]) {
233242 '\n' => {
234 const result = build_info[start..*index];
235 *index += 1;
243 const result = build_info[start..index.*];
244 index.* += 1;
236245 return result;
237246 },
238247 '\r' => {
239 const result = build_info[start..*index];
240 *index += 2;
248 const result = build_info[start..index.*];
249 index.* += 2;
241250 return result;
242251 },
243252 else => continue,
doc/langref.html.in+34-32
......@@ -1232,7 +1232,7 @@ mem.eql(u8, pattern, "ababab")</code></pre>
12321232 </td>
12331233 </tr>
12341234 <tr>
1235 <td><pre><code class="zig">*a<code></pre></td>
1235 <td><pre><code class="zig">a.*<code></pre></td>
12361236 <td>
12371237 <ul>
12381238 <li>{#link|Pointers#}</li>
......@@ -1244,7 +1244,7 @@ mem.eql(u8, pattern, "ababab")</code></pre>
12441244 <td>
12451245 <pre><code class="zig">const x: u32 = 1234;
12461246const ptr = &amp;x;
1247*x == 1234</code></pre>
1247x.* == 1234</code></pre>
12481248 </td>
12491249 </tr>
12501250 <tr>
......@@ -1258,7 +1258,7 @@ const ptr = &amp;x;
12581258 <td>
12591259 <pre><code class="zig">const x: u32 = 1234;
12601260const ptr = &amp;x;
1261*x == 1234</code></pre>
1261x.* == 1234</code></pre>
12621262 </td>
12631263 </tr>
12641264 </table>
......@@ -1267,8 +1267,8 @@ const ptr = &amp;x;
12671267 {#header_open|Precedence#}
12681268 <pre><code>x() x[] x.y
12691269a!b
1270!x -x -%x ~x *x &amp;x ?x ??x
1271x{}
1270!x -x -%x ~x &amp;x ?x ??x
1271x{} x.*
12721272! * / % ** *%
12731273+ - ++ +% -%
12741274&lt;&lt; &gt;&gt;
......@@ -1316,7 +1316,7 @@ var some_integers: [100]i32 = undefined;
13161316
13171317test "modify an array" {
13181318 for (some_integers) |*item, i| {
1319 *item = i32(i);
1319 item.* = i32(i);
13201320 }
13211321 assert(some_integers[10] == 10);
13221322 assert(some_integers[99] == 99);
......@@ -1357,7 +1357,7 @@ comptime {
13571357var fancy_array = init: {
13581358 var initial_value: [10]Point = undefined;
13591359 for (initial_value) |*pt, i| {
1360 *pt = Point {
1360 pt.* = Point {
13611361 .x = i32(i),
13621362 .y = i32(i) * 2,
13631363 };
......@@ -1400,7 +1400,7 @@ test "address of syntax" {
14001400 const x_ptr = &x;
14011401
14021402 // Deference a pointer:
1403 assert(*x_ptr == 1234);
1403 assert(x_ptr.* == 1234);
14041404
14051405 // When you get the address of a const variable, you get a const pointer.
14061406 assert(@typeOf(x_ptr) == &const i32);
......@@ -1409,8 +1409,8 @@ test "address of syntax" {
14091409 var y: i32 = 5678;
14101410 const y_ptr = &y;
14111411 assert(@typeOf(y_ptr) == &i32);
1412 *y_ptr += 1;
1413 assert(*y_ptr == 5679);
1412 y_ptr.* += 1;
1413 assert(y_ptr.* == 5679);
14141414}
14151415
14161416test "pointer array access" {
......@@ -1448,9 +1448,9 @@ comptime {
14481448 // @ptrCast.
14491449 var x: i32 = 1;
14501450 const ptr = &x;
1451 *ptr += 1;
1451 ptr.* += 1;
14521452 x += 1;
1453 assert(*ptr == 3);
1453 assert(ptr.* == 3);
14541454}
14551455
14561456test "@ptrToInt and @intToPtr" {
......@@ -1492,7 +1492,7 @@ test "nullable pointers" {
14921492 var x: i32 = 1;
14931493 ptr = &x;
14941494
1495 assert(*??ptr == 1);
1495 assert((??ptr).* == 1);
14961496
14971497 // Nullable pointers are the same size as normal pointers, because pointer
14981498 // value 0 is used as the null value.
......@@ -1505,7 +1505,7 @@ test "pointer casting" {
15051505 // conversions are not possible.
15061506 const bytes align(@alignOf(u32)) = []u8{0x12, 0x12, 0x12, 0x12};
15071507 const u32_ptr = @ptrCast(&const u32, &bytes[0]);
1508 assert(*u32_ptr == 0x12121212);
1508 assert(u32_ptr.* == 0x12121212);
15091509
15101510 // Even this example is contrived - there are better ways to do the above than
15111511 // pointer casting. For example, using a slice narrowing cast:
......@@ -1610,7 +1610,7 @@ fn foo(bytes: []u8) u32 {
16101610 <code>u8</code> can alias any memory.
16111611 </p>
16121612 <p>As an example, this code produces undefined behavior:</p>
1613 <pre><code class="zig">*@ptrCast(&amp;u32, f32(12.34))</code></pre>
1613 <pre><code class="zig">@ptrCast(&amp;u32, f32(12.34)).*</code></pre>
16141614 <p>Instead, use {#link|@bitCast#}:
16151615 <pre><code class="zig">@bitCast(u32, f32(12.34))</code></pre>
16161616 <p>As an added benefit, the <code>@bitcast</code> version works at compile-time.</p>
......@@ -2040,7 +2040,7 @@ const Variant = union(enum) {
20402040 Bool: bool,
20412041
20422042 fn truthy(self: &const Variant) bool {
2043 return switch (*self) {
2043 return switch (self.*) {
20442044 Variant.Int => |x_int| x_int != 0,
20452045 Variant.Bool => |x_bool| x_bool,
20462046 };
......@@ -2151,7 +2151,7 @@ test "switch enum" {
21512151
21522152 // A reference to the matched value can be obtained using `*` syntax.
21532153 Item.C => |*item| blk: {
2154 (*item).x += 1;
2154 item.*.x += 1;
21552155 break :blk 6;
21562156 },
21572157
......@@ -2374,7 +2374,7 @@ test "for reference" {
23742374 // Iterate over the slice by reference by
23752375 // specifying that the capture value is a pointer.
23762376 for (items) |*value| {
2377 *value += 1;
2377 value.* += 1;
23782378 }
23792379
23802380 assert(items[0] == 4);
......@@ -2483,7 +2483,7 @@ test "if nullable" {
24832483 // Access the value by reference using a pointer capture.
24842484 var c: ?u32 = 3;
24852485 if (c) |*value| {
2486 *value = 2;
2486 value.* = 2;
24872487 }
24882488
24892489 if (c) |value| {
......@@ -2524,7 +2524,7 @@ test "if error union" {
25242524 // Access the value by reference using a pointer capture.
25252525 var c: error!u32 = 3;
25262526 if (c) |*value| {
2527 *value = 9;
2527 value.* = 9;
25282528 } else |err| {
25292529 unreachable;
25302530 }
......@@ -3872,7 +3872,7 @@ pub fn main() void {
38723872 {#header_open|@addWithOverflow#}
38733873 <pre><code class="zig">@addWithOverflow(comptime T: type, a: T, b: T, result: &T) -&gt; bool</code></pre>
38743874 <p>
3875 Performs <code>*result = a + b</code>. If overflow or underflow occurs,
3875 Performs <code>result.* = a + b</code>. If overflow or underflow occurs,
38763876 stores the overflowed bits in <code>result</code> and returns <code>true</code>.
38773877 If no overflow or underflow occurs, returns <code>false</code>.
38783878 </p>
......@@ -4073,9 +4073,9 @@ comptime {
40734073 </p>
40744074 {#code_begin|syntax#}
40754075fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T {
4076 const old_value = *ptr;
4076 const old_value = ptr.*;
40774077 if (old_value == expected_value) {
4078 *ptr = new_value;
4078 ptr.* = new_value;
40794079 return null;
40804080 } else {
40814081 return old_value;
......@@ -4100,9 +4100,9 @@ fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_v
41004100 </p>
41014101 {#code_begin|syntax#}
41024102fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T {
4103 const old_value = *ptr;
4103 const old_value = ptr.*;
41044104 if (old_value == expected_value and usuallyTrueButSometimesFalse()) {
4105 *ptr = new_value;
4105 ptr.* = new_value;
41064106 return null;
41074107 } else {
41084108 return old_value;
......@@ -4447,7 +4447,7 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);</code></pre>
44474447 This function is a low level intrinsic with no safety mechanisms. Most
44484448 code should not use this function, instead using something like this:
44494449 </p>
4450 <pre><code class="zig">for (dest[0...byte_count]) |*b| *b = c;</code></pre>
4450 <pre><code class="zig">for (dest[0...byte_count]) |*b| b.* = c;</code></pre>
44514451 <p>
44524452 The optimizer is intelligent enough to turn the above snippet into a memset.
44534453 </p>
......@@ -4480,7 +4480,7 @@ mem.set(u8, dest, c);</code></pre>
44804480 {#header_open|@mulWithOverflow#}
44814481 <pre><code class="zig">@mulWithOverflow(comptime T: type, a: T, b: T, result: &T) -&gt; bool</code></pre>
44824482 <p>
4483 Performs <code>*result = a * b</code>. If overflow or underflow occurs,
4483 Performs <code>result.* = a * b</code>. If overflow or underflow occurs,
44844484 stores the overflowed bits in <code>result</code> and returns <code>true</code>.
44854485 If no overflow or underflow occurs, returns <code>false</code>.
44864486 </p>
......@@ -4514,7 +4514,7 @@ fn targetFunction(x: i32) usize {
45144514
45154515 var local_variable: i32 = 42;
45164516 const ptr = &local_variable;
4517 *ptr += 1;
4517 ptr.* += 1;
45184518
45194519 assert(local_variable == 43);
45204520 return @ptrToInt(ptr);
......@@ -4746,7 +4746,7 @@ pub const FloatMode = enum {
47464746 {#header_open|@shlWithOverflow#}
47474747 <pre><code class="zig">@shlWithOverflow(comptime T: type, a: T, shift_amt: Log2T, result: &T) -&gt; bool</code></pre>
47484748 <p>
4749 Performs <code>*result = a &lt;&lt; b</code>. If overflow or underflow occurs,
4749 Performs <code>result.* = a &lt;&lt; b</code>. If overflow or underflow occurs,
47504750 stores the overflowed bits in <code>result</code> and returns <code>true</code>.
47514751 If no overflow or underflow occurs, returns <code>false</code>.
47524752 </p>
......@@ -4790,7 +4790,7 @@ pub const FloatMode = enum {
47904790 {#header_open|@subWithOverflow#}
47914791 <pre><code class="zig">@subWithOverflow(comptime T: type, a: T, b: T, result: &T) -&gt; bool</code></pre>
47924792 <p>
4793 Performs <code>*result = a - b</code>. If overflow or underflow occurs,
4793 Performs <code>result.* = a - b</code>. If overflow or underflow occurs,
47944794 stores the overflowed bits in <code>result</code> and returns <code>true</code>.
47954795 If no overflow or underflow occurs, returns <code>false</code>.
47964796 </p>
......@@ -6382,10 +6382,12 @@ MultiplyOperator = "||" | "*" | "/" | "%" | "**" | "*%"
63826382
63836383PrefixOpExpression = PrefixOp TypeExpr | SuffixOpExpression
63846384
6385SuffixOpExpression = ("async" option("&lt;" SuffixOpExpression "&gt;") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
6385SuffixOpExpression = ("async" option("&lt;" SuffixOpExpression "&gt;") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression | PtrDerefExpression)
63866386
63876387FieldAccessExpression = "." Symbol
63886388
6389PtrDerefExpression = ".*"
6390
63896391FnCallExpression = "(" list(Expression, ",") ")"
63906392
63916393ArrayAccessExpression = "[" Expression "]"
......@@ -6398,7 +6400,7 @@ ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",")
63986400
63996401StructLiteralField = "." Symbol "=" Expression
64006402
6401PrefixOp = "!" | "-" | "~" | "*" | ("&amp;" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
6403PrefixOp = "!" | "-" | "~" | ("*" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
64026404
64036405PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl | PromiseType
64046406
src-self-hosted/arg.zig+41-33
......@@ -30,24 +30,22 @@ fn argInAllowedSet(maybe_set: ?[]const []const u8, arg: []const u8) bool {
3030}
3131
3232// Modifies the current argument index during iteration
33fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required: usize,
34 allowed_set: ?[]const []const u8, index: &usize) !FlagArg {
35
33fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required: usize, allowed_set: ?[]const []const u8, index: &usize) !FlagArg {
3634 switch (required) {
37 0 => return FlagArg { .None = undefined }, // TODO: Required to force non-tag but value?
35 0 => return FlagArg{ .None = undefined }, // TODO: Required to force non-tag but value?
3836 1 => {
39 if (*index + 1 >= args.len) {
37 if (index.* + 1 >= args.len) {
4038 return error.MissingFlagArguments;
4139 }
4240
43 *index += 1;
44 const arg = args[*index];
41 index.* += 1;
42 const arg = args[index.*];
4543
4644 if (!argInAllowedSet(allowed_set, arg)) {
4745 return error.ArgumentNotInAllowedSet;
4846 }
4947
50 return FlagArg { .Single = arg };
48 return FlagArg{ .Single = arg };
5149 },
5250 else => |needed| {
5351 var extra = ArrayList([]const u8).init(allocator);
......@@ -55,12 +53,12 @@ fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required:
5553
5654 var j: usize = 0;
5755 while (j < needed) : (j += 1) {
58 if (*index + 1 >= args.len) {
56 if (index.* + 1 >= args.len) {
5957 return error.MissingFlagArguments;
6058 }
6159
62 *index += 1;
63 const arg = args[*index];
60 index.* += 1;
61 const arg = args[index.*];
6462
6563 if (!argInAllowedSet(allowed_set, arg)) {
6664 return error.ArgumentNotInAllowedSet;
......@@ -69,7 +67,7 @@ fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required:
6967 try extra.append(arg);
7068 }
7169
72 return FlagArg { .Many = extra };
70 return FlagArg{ .Many = extra };
7371 },
7472 }
7573}
......@@ -82,7 +80,7 @@ pub const Args = struct {
8280 positionals: ArrayList([]const u8),
8381
8482 pub fn parse(allocator: &Allocator, comptime spec: []const Flag, args: []const []const u8) !Args {
85 var parsed = Args {
83 var parsed = Args{
8684 .flags = HashMapFlags.init(allocator),
8785 .positionals = ArrayList([]const u8).init(allocator),
8886 };
......@@ -116,11 +114,7 @@ pub const Args = struct {
116114 };
117115
118116 if (flag.mergable) {
119 var prev =
120 if (parsed.flags.get(flag_name_trimmed)) |entry|
121 entry.value.Many
122 else
123 ArrayList([]const u8).init(allocator);
117 var prev = if (parsed.flags.get(flag_name_trimmed)) |entry| entry.value.Many else ArrayList([]const u8).init(allocator);
124118
125119 // MergeN creation disallows 0 length flag entry (doesn't make sense)
126120 switch (flag_args) {
......@@ -129,7 +123,7 @@ pub const Args = struct {
129123 FlagArg.Many => |inner| try prev.appendSlice(inner.toSliceConst()),
130124 }
131125
132 _ = try parsed.flags.put(flag_name_trimmed, FlagArg { .Many = prev });
126 _ = try parsed.flags.put(flag_name_trimmed, FlagArg{ .Many = prev });
133127 } else {
134128 _ = try parsed.flags.put(flag_name_trimmed, flag_args);
135129 }
......@@ -163,7 +157,9 @@ pub const Args = struct {
163157 pub fn single(self: &Args, name: []const u8) ?[]const u8 {
164158 if (self.flags.get(name)) |entry| {
165159 switch (entry.value) {
166 FlagArg.Single => |inner| { return inner; },
160 FlagArg.Single => |inner| {
161 return inner;
162 },
167163 else => @panic("attempted to retrieve flag with wrong type"),
168164 }
169165 } else {
......@@ -175,7 +171,9 @@ pub const Args = struct {
175171 pub fn many(self: &Args, name: []const u8) ?[]const []const u8 {
176172 if (self.flags.get(name)) |entry| {
177173 switch (entry.value) {
178 FlagArg.Many => |inner| { return inner.toSliceConst(); },
174 FlagArg.Many => |inner| {
175 return inner.toSliceConst();
176 },
179177 else => @panic("attempted to retrieve flag with wrong type"),
180178 }
181179 } else {
......@@ -207,7 +205,7 @@ pub const Flag = struct {
207205 }
208206
209207 pub fn ArgN(comptime name: []const u8, comptime n: usize) Flag {
210 return Flag {
208 return Flag{
211209 .name = name,
212210 .required = n,
213211 .mergable = false,
......@@ -220,7 +218,7 @@ pub const Flag = struct {
220218 @compileError("n must be greater than 0");
221219 }
222220
223 return Flag {
221 return Flag{
224222 .name = name,
225223 .required = n,
226224 .mergable = true,
......@@ -229,7 +227,7 @@ pub const Flag = struct {
229227 }
230228
231229 pub fn Option(comptime name: []const u8, comptime set: []const []const u8) Flag {
232 return Flag {
230 return Flag{
233231 .name = name,
234232 .required = 1,
235233 .mergable = false,
......@@ -239,26 +237,36 @@ pub const Flag = struct {
239237};
240238
241239test "parse arguments" {
242 const spec1 = comptime []const Flag {
240 const spec1 = comptime []const Flag{
243241 Flag.Bool("--help"),
244242 Flag.Bool("--init"),
245243 Flag.Arg1("--build-file"),
246 Flag.Option("--color", []const []const u8 { "on", "off", "auto" }),
244 Flag.Option("--color", []const []const u8{
245 "on",
246 "off",
247 "auto",
248 }),
247249 Flag.ArgN("--pkg-begin", 2),
248250 Flag.ArgMergeN("--object", 1),
249251 Flag.ArgN("--library", 1),
250252 };
251253
252 const cliargs = []const []const u8 {
254 const cliargs = []const []const u8{
253255 "build",
254256 "--help",
255257 "pos1",
256 "--build-file", "build.zig",
257 "--object", "obj1",
258 "--object", "obj2",
259 "--library", "lib1",
260 "--library", "lib2",
261 "--color", "on",
258 "--build-file",
259 "build.zig",
260 "--object",
261 "obj1",
262 "--object",
263 "obj2",
264 "--library",
265 "lib1",
266 "--library",
267 "lib2",
268 "--color",
269 "on",
262270 "pos2",
263271 };
264272
src-self-hosted/module.zig+9-9
......@@ -96,6 +96,7 @@ pub const Module = struct {
9696 pub const LinkLib = struct {
9797 name: []const u8,
9898 path: ?[]const u8,
99
99100 /// the list of symbols we depend on from this lib
100101 symbols: ArrayList([]u8),
101102 provided_explicitly: bool,
......@@ -130,9 +131,7 @@ pub const Module = struct {
130131 }
131132 };
132133
133 pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target,
134 kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !&Module
135 {
134 pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !&Module {
136135 var name_buffer = try Buffer.init(allocator, name);
137136 errdefer name_buffer.deinit();
138137
......@@ -148,14 +147,14 @@ pub const Module = struct {
148147 const module_ptr = try allocator.create(Module);
149148 errdefer allocator.destroy(module_ptr);
150149
151 *module_ptr = Module {
150 module_ptr.* = Module{
152151 .allocator = allocator,
153152 .name = name_buffer,
154153 .root_src_path = root_src_path,
155154 .module = module,
156155 .context = context,
157156 .builder = builder,
158 .target = *target,
157 .target = target.*,
159158 .kind = kind,
160159 .build_mode = build_mode,
161160 .zig_lib_dir = zig_lib_dir,
......@@ -221,8 +220,10 @@ pub const Module = struct {
221220
222221 pub fn build(self: &Module) !void {
223222 if (self.llvm_argv.len != 0) {
224 var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator,
225 [][]const []const u8 { [][]const u8{"zig (LLVM option parsing)"}, self.llvm_argv, });
223 var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator, [][]const []const u8{
224 [][]const u8{"zig (LLVM option parsing)"},
225 self.llvm_argv,
226 });
226227 defer c_compatible_args.deinit();
227228 c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr);
228229 }
......@@ -261,7 +262,6 @@ pub const Module = struct {
261262
262263 warn("====llvm ir:====\n");
263264 self.dump();
264
265265 }
266266
267267 pub fn link(self: &Module, out_file: ?[]const u8) !void {
......@@ -285,7 +285,7 @@ pub const Module = struct {
285285 }
286286
287287 const link_lib = try self.allocator.create(LinkLib);
288 *link_lib = LinkLib {
288 link_lib.* = LinkLib{
289289 .name = name,
290290 .path = null,
291291 .provided_explicitly = provided_explicitly,
src-self-hosted/target.zig+4-3
......@@ -12,7 +12,7 @@ pub const Target = union(enum) {
1212 Cross: CrossTarget,
1313
1414 pub fn oFileExt(self: &const Target) []const u8 {
15 const environ = switch (*self) {
15 const environ = switch (self.*) {
1616 Target.Native => builtin.environ,
1717 Target.Cross => |t| t.environ,
1818 };
......@@ -30,7 +30,7 @@ pub const Target = union(enum) {
3030 }
3131
3232 pub fn getOs(self: &const Target) builtin.Os {
33 return switch (*self) {
33 return switch (self.*) {
3434 Target.Native => builtin.os,
3535 Target.Cross => |t| t.os,
3636 };
......@@ -38,7 +38,8 @@ pub const Target = union(enum) {
3838
3939 pub fn isDarwin(self: &const Target) bool {
4040 return switch (self.getOs()) {
41 builtin.Os.ios, builtin.Os.macosx => true,
41 builtin.Os.ios,
42 builtin.Os.macosx => true,
4243 else => false,
4344 };
4445 }
src/all_types.hpp+6-1
......@@ -379,6 +379,7 @@ enum NodeType {
379379 NodeTypeArrayAccessExpr,
380380 NodeTypeSliceExpr,
381381 NodeTypeFieldAccessExpr,
382 NodeTypePtrDeref,
382383 NodeTypeUse,
383384 NodeTypeBoolLiteral,
384385 NodeTypeNullLiteral,
......@@ -603,13 +604,16 @@ struct AstNodeFieldAccessExpr {
603604 Buf *field_name;
604605};
605606
607struct AstNodePtrDerefExpr {
608 AstNode *target;
609};
610
606611enum PrefixOp {
607612 PrefixOpInvalid,
608613 PrefixOpBoolNot,
609614 PrefixOpBinNot,
610615 PrefixOpNegation,
611616 PrefixOpNegationWrap,
612 PrefixOpDereference,
613617 PrefixOpMaybe,
614618 PrefixOpUnwrapMaybe,
615619};
......@@ -911,6 +915,7 @@ struct AstNode {
911915 AstNodeCompTime comptime_expr;
912916 AstNodeAsmExpr asm_expr;
913917 AstNodeFieldAccessExpr field_access_expr;
918 AstNodePtrDerefExpr ptr_deref_expr;
914919 AstNodeContainerDecl container_decl;
915920 AstNodeStructField struct_field;
916921 AstNodeStringLiteral string_literal;
src/analyze.cpp+1
......@@ -3281,6 +3281,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
32813281 case NodeTypeUnreachable:
32823282 case NodeTypeAsmExpr:
32833283 case NodeTypeFieldAccessExpr:
3284 case NodeTypePtrDeref:
32843285 case NodeTypeStructField:
32853286 case NodeTypeContainerInitExpr:
32863287 case NodeTypeStructValueField:
src/ast_render.cpp+9-1
......@@ -66,7 +66,6 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
6666 case PrefixOpNegationWrap: return "-%";
6767 case PrefixOpBoolNot: return "!";
6868 case PrefixOpBinNot: return "~";
69 case PrefixOpDereference: return "*";
7069 case PrefixOpMaybe: return "?";
7170 case PrefixOpUnwrapMaybe: return "??";
7271 }
......@@ -222,6 +221,8 @@ static const char *node_type_str(NodeType node_type) {
222221 return "AsmExpr";
223222 case NodeTypeFieldAccessExpr:
224223 return "FieldAccessExpr";
224 case NodeTypePtrDeref:
225 return "PtrDerefExpr";
225226 case NodeTypeContainerDecl:
226227 return "ContainerDecl";
227228 case NodeTypeStructField:
......@@ -696,6 +697,13 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
696697 print_symbol(ar, rhs);
697698 break;
698699 }
700 case NodeTypePtrDeref:
701 {
702 AstNode *lhs = node->data.ptr_deref_expr.target;
703 render_node_ungrouped(ar, lhs);
704 fprintf(ar->f, ".*");
705 break;
706 }
699707 case NodeTypeUndefinedLiteral:
700708 fprintf(ar->f, "undefined");
701709 break;
src/ir.cpp+10-4
......@@ -4609,8 +4609,14 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
46094609}
46104610
46114611static IrInstruction *ir_gen_prefix_op_id_lval(IrBuilder *irb, Scope *scope, AstNode *node, IrUnOp op_id, LVal lval) {
4612 assert(node->type == NodeTypePrefixOpExpr);
4613 AstNode *expr_node = node->data.prefix_op_expr.primary_expr;
4612 AstNode *expr_node;
4613 if (node->type == NodeTypePrefixOpExpr) {
4614 expr_node = node->data.prefix_op_expr.primary_expr;
4615 } else if (node->type == NodeTypePtrDeref) {
4616 expr_node = node->data.ptr_deref_expr.target;
4617 } else {
4618 zig_unreachable();
4619 }
46144620
46154621 IrInstruction *value = ir_gen_node_extra(irb, expr_node, scope, lval);
46164622 if (value == irb->codegen->invalid_instruction)
......@@ -4751,8 +4757,6 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
47514757 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegation), lval);
47524758 case PrefixOpNegationWrap:
47534759 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap), lval);
4754 case PrefixOpDereference:
4755 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpDereference, lval);
47564760 case PrefixOpMaybe:
47574761 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpMaybe), lval);
47584762 case PrefixOpUnwrapMaybe:
......@@ -6588,6 +6592,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
65886592
65896593 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
65906594 }
6595 case NodeTypePtrDeref:
6596 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpDereference, lval);
65916597 case NodeTypeThisLiteral:
65926598 return ir_lval_wrap(irb, scope, ir_gen_this_literal(irb, scope, node), lval);
65936599 case NodeTypeBoolLiteral:
src/parser.cpp+25-18
......@@ -1046,11 +1046,12 @@ static AstNode *ast_parse_fn_proto_partial(ParseContext *pc, size_t *token_index
10461046}
10471047
10481048/*
1049SuffixOpExpression = ("async" option("<" SuffixOpExpression ">") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
1049SuffixOpExpression = ("async" option("<" SuffixOpExpression ">") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | PtrDerefExpression | SliceExpression)
10501050FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
10511051ArrayAccessExpression : token(LBracket) Expression token(RBracket)
10521052SliceExpression = "[" Expression ".." option(Expression) "]"
10531053FieldAccessExpression : token(Dot) token(Symbol)
1054PtrDerefExpression = ".*"
10541055StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression
10551056*/
10561057static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
......@@ -1131,13 +1132,27 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
11311132 } else if (first_token->id == TokenIdDot) {
11321133 *token_index += 1;
11331134
1134 Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol);
1135 Token *token = &pc->tokens->at(*token_index);
1136
1137 if (token->id == TokenIdSymbol) {
1138 *token_index += 1;
11351139
1136 AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token);
1137 node->data.field_access_expr.struct_expr = primary_expr;
1138 node->data.field_access_expr.field_name = token_buf(name_token);
1140 AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token);
1141 node->data.field_access_expr.struct_expr = primary_expr;
1142 node->data.field_access_expr.field_name = token_buf(token);
1143
1144 primary_expr = node;
1145 } else if (token->id == TokenIdStar) {
1146 *token_index += 1;
1147
1148 AstNode *node = ast_create_node(pc, NodeTypePtrDeref, first_token);
1149 node->data.ptr_deref_expr.target = primary_expr;
1150
1151 primary_expr = node;
1152 } else {
1153 ast_invalid_token_error(pc, token);
1154 }
11391155
1140 primary_expr = node;
11411156 } else {
11421157 return primary_expr;
11431158 }
......@@ -1150,10 +1165,8 @@ static PrefixOp tok_to_prefix_op(Token *token) {
11501165 case TokenIdDash: return PrefixOpNegation;
11511166 case TokenIdMinusPercent: return PrefixOpNegationWrap;
11521167 case TokenIdTilde: return PrefixOpBinNot;
1153 case TokenIdStar: return PrefixOpDereference;
11541168 case TokenIdMaybe: return PrefixOpMaybe;
11551169 case TokenIdDoubleQuestion: return PrefixOpUnwrapMaybe;
1156 case TokenIdStarStar: return PrefixOpDereference;
11571170 default: return PrefixOpInvalid;
11581171 }
11591172}
......@@ -1199,7 +1212,7 @@ static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) {
11991212
12001213/*
12011214PrefixOpExpression = PrefixOp ErrorSetExpr | SuffixOpExpression
1202PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
1215PrefixOp = "!" | "-" | "~" | ("*" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
12031216*/
12041217static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
12051218 Token *token = &pc->tokens->at(*token_index);
......@@ -1222,15 +1235,6 @@ static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index,
12221235
12231236 AstNode *node = ast_create_node(pc, NodeTypePrefixOpExpr, token);
12241237 AstNode *parent_node = node;
1225 if (token->id == TokenIdStarStar) {
1226 // pretend that we got 2 star tokens
1227
1228 parent_node = ast_create_node(pc, NodeTypePrefixOpExpr, token);
1229 parent_node->data.prefix_op_expr.primary_expr = node;
1230 parent_node->data.prefix_op_expr.prefix_op = PrefixOpDereference;
1231
1232 node->column += 1;
1233 }
12341238
12351239 AstNode *prefix_op_expr = ast_parse_error_set_expr(pc, token_index, true);
12361240 node->data.prefix_op_expr.primary_expr = prefix_op_expr;
......@@ -3012,6 +3016,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30123016 case NodeTypeFieldAccessExpr:
30133017 visit_field(&node->data.field_access_expr.struct_expr, visit, context);
30143018 break;
3019 case NodeTypePtrDeref:
3020 visit_field(&node->data.ptr_deref_expr.target, visit, context);
3021 break;
30153022 case NodeTypeUse:
30163023 visit_field(&node->data.use.expr, visit, context);
30173024 break;
src/translate_c.cpp+53-30
......@@ -247,6 +247,12 @@ static AstNode *trans_create_node_field_access_str(Context *c, AstNode *containe
247247 return trans_create_node_field_access(c, container, buf_create_from_str(field_name));
248248}
249249
250static AstNode *trans_create_node_ptr_deref(Context *c, AstNode *child_node) {
251 AstNode *node = trans_create_node(c, NodeTypePtrDeref);
252 node->data.ptr_deref_expr.target = child_node;
253 return node;
254}
255
250256static AstNode *trans_create_node_prefix_op(Context *c, PrefixOp op, AstNode *child_node) {
251257 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
252258 node->data.prefix_op_expr.prefix_op = op;
......@@ -1412,8 +1418,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14121418 AstNode *operation_type_cast = trans_c_cast(c, rhs_location,
14131419 stmt->getComputationLHSType(),
14141420 stmt->getLHS()->getType(),
1415 trans_create_node_prefix_op(c, PrefixOpDereference,
1416 trans_create_node_symbol(c, tmp_var_name)));
1421 trans_create_node_ptr_deref(c, trans_create_node_symbol(c, tmp_var_name)));
14171422
14181423 // result_type(... >> u5(rhs))
14191424 AstNode *result_type_cast = trans_c_cast(c, rhs_location,
......@@ -1426,7 +1431,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14261431
14271432 // *_ref = ...
14281433 AstNode *assign_statement = trans_create_node_bin_op(c,
1429 trans_create_node_prefix_op(c, PrefixOpDereference,
1434 trans_create_node_ptr_deref(c,
14301435 trans_create_node_symbol(c, tmp_var_name)),
14311436 BinOpTypeAssign, result_type_cast);
14321437
......@@ -1436,7 +1441,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14361441 // break :x *_ref
14371442 child_scope->node->data.block.statements.append(
14381443 trans_create_node_break(c, label_name,
1439 trans_create_node_prefix_op(c, PrefixOpDereference,
1444 trans_create_node_ptr_deref(c,
14401445 trans_create_node_symbol(c, tmp_var_name))));
14411446 }
14421447
......@@ -1483,11 +1488,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
14831488 if (rhs == nullptr) return nullptr;
14841489
14851490 AstNode *assign_statement = trans_create_node_bin_op(c,
1486 trans_create_node_prefix_op(c, PrefixOpDereference,
1491 trans_create_node_ptr_deref(c,
14871492 trans_create_node_symbol(c, tmp_var_name)),
14881493 BinOpTypeAssign,
14891494 trans_create_node_bin_op(c,
1490 trans_create_node_prefix_op(c, PrefixOpDereference,
1495 trans_create_node_ptr_deref(c,
14911496 trans_create_node_symbol(c, tmp_var_name)),
14921497 bin_op,
14931498 rhs));
......@@ -1496,7 +1501,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
14961501 // break :x *_ref
14971502 child_scope->node->data.block.statements.append(
14981503 trans_create_node_break(c, label_name,
1499 trans_create_node_prefix_op(c, PrefixOpDereference,
1504 trans_create_node_ptr_deref(c,
15001505 trans_create_node_symbol(c, tmp_var_name))));
15011506
15021507 return child_scope->node;
......@@ -1817,13 +1822,13 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
18171822 // const _tmp = *_ref;
18181823 Buf* tmp_var_name = buf_create_from_str("_tmp");
18191824 AstNode *tmp_var_decl = trans_create_node_var_decl_local(c, true, tmp_var_name, nullptr,
1820 trans_create_node_prefix_op(c, PrefixOpDereference,
1825 trans_create_node_ptr_deref(c,
18211826 trans_create_node_symbol(c, ref_var_name)));
18221827 child_scope->node->data.block.statements.append(tmp_var_decl);
18231828
18241829 // *_ref += 1;
18251830 AstNode *assign_statement = trans_create_node_bin_op(c,
1826 trans_create_node_prefix_op(c, PrefixOpDereference,
1831 trans_create_node_ptr_deref(c,
18271832 trans_create_node_symbol(c, ref_var_name)),
18281833 assign_op,
18291834 trans_create_node_unsigned(c, 1));
......@@ -1871,14 +1876,14 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
18711876
18721877 // *_ref += 1;
18731878 AstNode *assign_statement = trans_create_node_bin_op(c,
1874 trans_create_node_prefix_op(c, PrefixOpDereference,
1879 trans_create_node_ptr_deref(c,
18751880 trans_create_node_symbol(c, ref_var_name)),
18761881 assign_op,
18771882 trans_create_node_unsigned(c, 1));
18781883 child_scope->node->data.block.statements.append(assign_statement);
18791884
18801885 // break :x *_ref
1881 AstNode *deref_expr = trans_create_node_prefix_op(c, PrefixOpDereference,
1886 AstNode *deref_expr = trans_create_node_ptr_deref(c,
18821887 trans_create_node_symbol(c, ref_var_name));
18831888 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr));
18841889
......@@ -1923,7 +1928,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
19231928 if (is_fn_ptr)
19241929 return value_node;
19251930 AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node);
1926 return trans_create_node_prefix_op(c, PrefixOpDereference, unwrapped);
1931 return trans_create_node_ptr_deref(c, unwrapped);
19271932 }
19281933 case UO_Plus:
19291934 emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_Plus");
......@@ -4443,27 +4448,45 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t
44434448 }
44444449}
44454450
4446static PrefixOp ctok_to_prefix_op(CTok *token) {
4447 switch (token->id) {
4448 case CTokIdBang: return PrefixOpBoolNot;
4449 case CTokIdMinus: return PrefixOpNegation;
4450 case CTokIdTilde: return PrefixOpBinNot;
4451 case CTokIdAsterisk: return PrefixOpDereference;
4452 default: return PrefixOpInvalid;
4453 }
4454}
44554451static AstNode *parse_ctok_prefix_op_expr(Context *c, CTokenize *ctok, size_t *tok_i) {
44564452 CTok *op_tok = &ctok->tokens.at(*tok_i);
4457 PrefixOp prefix_op = ctok_to_prefix_op(op_tok);
4458 if (prefix_op == PrefixOpInvalid) {
4459 return parse_ctok_suffix_op_expr(c, ctok, tok_i);
4460 }
4461 *tok_i += 1;
44624453
4463 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4464 if (prefix_op_expr == nullptr)
4465 return nullptr;
4466 return trans_create_node_prefix_op(c, prefix_op, prefix_op_expr);
4454 switch (op_tok->id) {
4455 case CTokIdBang:
4456 {
4457 *tok_i += 1;
4458 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4459 if (prefix_op_expr == nullptr)
4460 return nullptr;
4461 return trans_create_node_prefix_op(c, PrefixOpBoolNot, prefix_op_expr);
4462 }
4463 case CTokIdMinus:
4464 {
4465 *tok_i += 1;
4466 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4467 if (prefix_op_expr == nullptr)
4468 return nullptr;
4469 return trans_create_node_prefix_op(c, PrefixOpNegation, prefix_op_expr);
4470 }
4471 case CTokIdTilde:
4472 {
4473 *tok_i += 1;
4474 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4475 if (prefix_op_expr == nullptr)
4476 return nullptr;
4477 return trans_create_node_prefix_op(c, PrefixOpBinNot, prefix_op_expr);
4478 }
4479 case CTokIdAsterisk:
4480 {
4481 *tok_i += 1;
4482 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4483 if (prefix_op_expr == nullptr)
4484 return nullptr;
4485 return trans_create_node_ptr_deref(c, prefix_op_expr);
4486 }
4487 default:
4488 return parse_ctok_suffix_op_expr(c, ctok, tok_i);
4489 }
44674490}
44684491
44694492static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *char_ptr) {
std/array_list.zig+33-21
......@@ -8,7 +8,7 @@ pub fn ArrayList(comptime T: type) type {
88 return AlignedArrayList(T, @alignOf(T));
99}
1010
11pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
11pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
1212 return struct {
1313 const Self = this;
1414
......@@ -21,7 +21,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
2121
2222 /// Deinitialize with `deinit` or use `toOwnedSlice`.
2323 pub fn init(allocator: &Allocator) Self {
24 return Self {
24 return Self{
2525 .items = []align(A) T{},
2626 .len = 0,
2727 .allocator = allocator,
......@@ -52,7 +52,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
5252 /// allocated with `allocator`.
5353 /// Deinitialize with `deinit` or use `toOwnedSlice`.
5454 pub fn fromOwnedSlice(allocator: &Allocator, slice: []align(A) T) Self {
55 return Self {
55 return Self{
5656 .items = slice,
5757 .len = slice.len,
5858 .allocator = allocator,
......@@ -63,7 +63,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
6363 pub fn toOwnedSlice(self: &Self) []align(A) T {
6464 const allocator = self.allocator;
6565 const result = allocator.alignedShrink(T, A, self.items, self.len);
66 *self = init(allocator);
66 self.* = init(allocator);
6767 return result;
6868 }
6969
......@@ -71,21 +71,21 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
7171 try l.ensureCapacity(l.len + 1);
7272 l.len += 1;
7373
74 mem.copy(T, l.items[n+1..l.len], l.items[n..l.len-1]);
75 l.items[n] = *item;
74 mem.copy(T, l.items[n + 1..l.len], l.items[n..l.len - 1]);
75 l.items[n] = item.*;
7676 }
7777
7878 pub fn insertSlice(l: &Self, n: usize, items: []align(A) const T) !void {
7979 try l.ensureCapacity(l.len + items.len);
8080 l.len += items.len;
8181
82 mem.copy(T, l.items[n+items.len..l.len], l.items[n..l.len-items.len]);
83 mem.copy(T, l.items[n..n+items.len], items);
82 mem.copy(T, l.items[n + items.len..l.len], l.items[n..l.len - items.len]);
83 mem.copy(T, l.items[n..n + items.len], items);
8484 }
8585
8686 pub fn append(l: &Self, item: &const T) !void {
8787 const new_item_ptr = try l.addOne();
88 *new_item_ptr = *item;
88 new_item_ptr.* = item.*;
8989 }
9090
9191 pub fn appendSlice(l: &Self, items: []align(A) const T) !void {
......@@ -128,8 +128,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type{
128128 }
129129
130130 pub fn popOrNull(self: &Self) ?T {
131 if (self.len == 0)
132 return null;
131 if (self.len == 0) return null;
133132 return self.pop();
134133 }
135134
......@@ -160,13 +159,19 @@ test "basic ArrayList test" {
160159 var list = ArrayList(i32).init(debug.global_allocator);
161160 defer list.deinit();
162161
163 {var i: usize = 0; while (i < 10) : (i += 1) {
164 list.append(i32(i + 1)) catch unreachable;
165 }}
162 {
163 var i: usize = 0;
164 while (i < 10) : (i += 1) {
165 list.append(i32(i + 1)) catch unreachable;
166 }
167 }
166168
167 {var i: usize = 0; while (i < 10) : (i += 1) {
168 assert(list.items[i] == i32(i + 1));
169 }}
169 {
170 var i: usize = 0;
171 while (i < 10) : (i += 1) {
172 assert(list.items[i] == i32(i + 1));
173 }
174 }
170175
171176 for (list.toSlice()) |v, i| {
172177 assert(v == i32(i + 1));
......@@ -179,14 +184,18 @@ test "basic ArrayList test" {
179184 assert(list.pop() == 10);
180185 assert(list.len == 9);
181186
182 list.appendSlice([]const i32 { 1, 2, 3 }) catch unreachable;
187 list.appendSlice([]const i32{
188 1,
189 2,
190 3,
191 }) catch unreachable;
183192 assert(list.len == 12);
184193 assert(list.pop() == 3);
185194 assert(list.pop() == 2);
186195 assert(list.pop() == 1);
187196 assert(list.len == 9);
188197
189 list.appendSlice([]const i32 {}) catch unreachable;
198 list.appendSlice([]const i32{}) catch unreachable;
190199 assert(list.len == 9);
191200}
192201
......@@ -228,12 +237,15 @@ test "insert ArrayList test" {
228237 assert(list.items[0] == 5);
229238 assert(list.items[1] == 1);
230239
231 try list.insertSlice(1, []const i32 { 9, 8 });
240 try list.insertSlice(1, []const i32{
241 9,
242 8,
243 });
232244 assert(list.items[0] == 5);
233245 assert(list.items[1] == 9);
234246 assert(list.items[2] == 8);
235247
236 const items = []const i32 { 1 };
248 const items = []const i32{1};
237249 try list.insertSlice(0, items[0..0]);
238250 assert(list.items[0] == 5);
239251}
std/atomic/queue.zig+7-5
......@@ -70,7 +70,7 @@ test "std.atomic.queue" {
7070
7171 var queue: Queue(i32) = undefined;
7272 queue.init();
73 var context = Context {
73 var context = Context{
7474 .allocator = a,
7575 .queue = &queue,
7676 .put_sum = 0,
......@@ -81,16 +81,18 @@ test "std.atomic.queue" {
8181
8282 var putters: [put_thread_count]&std.os.Thread = undefined;
8383 for (putters) |*t| {
84 *t = try std.os.spawnThread(&context, startPuts);
84 t.* = try std.os.spawnThread(&context, startPuts);
8585 }
8686 var getters: [put_thread_count]&std.os.Thread = undefined;
8787 for (getters) |*t| {
88 *t = try std.os.spawnThread(&context, startGets);
88 t.* = try std.os.spawnThread(&context, startGets);
8989 }
9090
91 for (putters) |t| t.wait();
91 for (putters) |t|
92 t.wait();
9293 _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
93 for (getters) |t| t.wait();
94 for (getters) |t|
95 t.wait();
9496
9597 std.debug.assert(context.put_sum == context.get_sum);
9698 std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
std/atomic/stack.zig+8-8
......@@ -14,9 +14,7 @@ pub fn Stack(comptime T: type) type {
1414 };
1515
1616 pub fn init() Self {
17 return Self {
18 .root = null,
19 };
17 return Self{ .root = null };
2018 }
2119
2220 /// push operation, but only if you are the first item in the stack. if you did not succeed in
......@@ -75,7 +73,7 @@ test "std.atomic.stack" {
7573 var a = &fixed_buffer_allocator.allocator;
7674
7775 var stack = Stack(i32).init();
78 var context = Context {
76 var context = Context{
7977 .allocator = a,
8078 .stack = &stack,
8179 .put_sum = 0,
......@@ -86,16 +84,18 @@ test "std.atomic.stack" {
8684
8785 var putters: [put_thread_count]&std.os.Thread = undefined;
8886 for (putters) |*t| {
89 *t = try std.os.spawnThread(&context, startPuts);
87 t.* = try std.os.spawnThread(&context, startPuts);
9088 }
9189 var getters: [put_thread_count]&std.os.Thread = undefined;
9290 for (getters) |*t| {
93 *t = try std.os.spawnThread(&context, startGets);
91 t.* = try std.os.spawnThread(&context, startGets);
9492 }
9593
96 for (putters) |t| t.wait();
94 for (putters) |t|
95 t.wait();
9796 _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
98 for (getters) |t| t.wait();
97 for (getters) |t|
98 t.wait();
9999
100100 std.debug.assert(context.put_sum == context.get_sum);
101101 std.debug.assert(context.get_count == puts_per_thread * put_thread_count);
std/buffer.zig+3-8
......@@ -31,9 +31,7 @@ pub const Buffer = struct {
3131 /// * ::replaceContentsBuffer
3232 /// * ::resize
3333 pub fn initNull(allocator: &Allocator) Buffer {
34 return Buffer {
35 .list = ArrayList(u8).init(allocator),
36 };
34 return Buffer{ .list = ArrayList(u8).init(allocator) };
3735 }
3836
3937 /// Must deinitialize with deinit.
......@@ -45,9 +43,7 @@ pub const Buffer = struct {
4543 /// allocated with `allocator`.
4644 /// Must deinitialize with deinit.
4745 pub fn fromOwnedSlice(allocator: &Allocator, slice: []u8) Buffer {
48 var self = Buffer {
49 .list = ArrayList(u8).fromOwnedSlice(allocator, slice),
50 };
46 var self = Buffer{ .list = ArrayList(u8).fromOwnedSlice(allocator, slice) };
5147 self.list.append(0);
5248 return self;
5349 }
......@@ -57,11 +53,10 @@ pub const Buffer = struct {
5753 pub fn toOwnedSlice(self: &Buffer) []u8 {
5854 const allocator = self.list.allocator;
5955 const result = allocator.shrink(u8, self.list.items, self.len());
60 *self = initNull(allocator);
56 self.* = initNull(allocator);
6157 return result;
6258 }
6359
64
6560 pub fn deinit(self: &Buffer) void {
6661 self.list.deinit();
6762 }
std/build.zig+86-121
......@@ -82,10 +82,8 @@ pub const Builder = struct {
8282 description: []const u8,
8383 };
8484
85 pub fn init(allocator: &Allocator, zig_exe: []const u8, build_root: []const u8,
86 cache_root: []const u8) Builder
87 {
88 var self = Builder {
85 pub fn init(allocator: &Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder {
86 var self = Builder{
8987 .zig_exe = zig_exe,
9088 .build_root = build_root,
9189 .cache_root = os.path.relative(allocator, build_root, cache_root) catch unreachable,
......@@ -112,12 +110,12 @@ pub const Builder = struct {
112110 .lib_dir = undefined,
113111 .exe_dir = undefined,
114112 .installed_files = ArrayList([]const u8).init(allocator),
115 .uninstall_tls = TopLevelStep {
113 .uninstall_tls = TopLevelStep{
116114 .step = Step.init("uninstall", allocator, makeUninstall),
117115 .description = "Remove build artifacts from prefix path",
118116 },
119117 .have_uninstall_step = false,
120 .install_tls = TopLevelStep {
118 .install_tls = TopLevelStep{
121119 .step = Step.initNoOp("install", allocator),
122120 .description = "Copy build artifacts to prefix path",
123121 },
......@@ -151,9 +149,7 @@ pub const Builder = struct {
151149 return LibExeObjStep.createObject(self, name, root_src);
152150 }
153151
154 pub fn addSharedLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8,
155 ver: &const Version) &LibExeObjStep
156 {
152 pub fn addSharedLibrary(self: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
157153 return LibExeObjStep.createSharedLibrary(self, name, root_src, ver);
158154 }
159155
......@@ -163,7 +159,7 @@ pub const Builder = struct {
163159
164160 pub fn addTest(self: &Builder, root_src: []const u8) &TestStep {
165161 const test_step = self.allocator.create(TestStep) catch unreachable;
166 *test_step = TestStep.init(self, root_src);
162 test_step.* = TestStep.init(self, root_src);
167163 return test_step;
168164 }
169165
......@@ -190,33 +186,31 @@ pub const Builder = struct {
190186 }
191187
192188 /// ::argv is copied.
193 pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
194 argv: []const []const u8) &CommandStep
195 {
189 pub fn addCommand(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
196190 return CommandStep.create(self, cwd, env_map, argv);
197191 }
198192
199193 pub fn addWriteFile(self: &Builder, file_path: []const u8, data: []const u8) &WriteFileStep {
200194 const write_file_step = self.allocator.create(WriteFileStep) catch unreachable;
201 *write_file_step = WriteFileStep.init(self, file_path, data);
195 write_file_step.* = WriteFileStep.init(self, file_path, data);
202196 return write_file_step;
203197 }
204198
205199 pub fn addLog(self: &Builder, comptime format: []const u8, args: ...) &LogStep {
206200 const data = self.fmt(format, args);
207201 const log_step = self.allocator.create(LogStep) catch unreachable;
208 *log_step = LogStep.init(self, data);
202 log_step.* = LogStep.init(self, data);
209203 return log_step;
210204 }
211205
212206 pub fn addRemoveDirTree(self: &Builder, dir_path: []const u8) &RemoveDirStep {
213207 const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable;
214 *remove_dir_step = RemoveDirStep.init(self, dir_path);
208 remove_dir_step.* = RemoveDirStep.init(self, dir_path);
215209 return remove_dir_step;
216210 }
217211
218212 pub fn version(self: &const Builder, major: u32, minor: u32, patch: u32) Version {
219 return Version {
213 return Version{
220214 .major = major,
221215 .minor = minor,
222216 .patch = patch,
......@@ -254,8 +248,7 @@ pub const Builder = struct {
254248 }
255249
256250 pub fn getInstallStep(self: &Builder) &Step {
257 if (self.have_install_step)
258 return &self.install_tls.step;
251 if (self.have_install_step) return &self.install_tls.step;
259252
260253 self.top_level_steps.append(&self.install_tls) catch unreachable;
261254 self.have_install_step = true;
......@@ -263,8 +256,7 @@ pub const Builder = struct {
263256 }
264257
265258 pub fn getUninstallStep(self: &Builder) &Step {
266 if (self.have_uninstall_step)
267 return &self.uninstall_tls.step;
259 if (self.have_uninstall_step) return &self.uninstall_tls.step;
268260
269261 self.top_level_steps.append(&self.uninstall_tls) catch unreachable;
270262 self.have_uninstall_step = true;
......@@ -360,7 +352,7 @@ pub const Builder = struct {
360352
361353 pub fn option(self: &Builder, comptime T: type, name: []const u8, description: []const u8) ?T {
362354 const type_id = comptime typeToEnum(T);
363 const available_option = AvailableOption {
355 const available_option = AvailableOption{
364356 .name = name,
365357 .type_id = type_id,
366358 .description = description,
......@@ -413,7 +405,7 @@ pub const Builder = struct {
413405
414406 pub fn step(self: &Builder, name: []const u8, description: []const u8) &Step {
415407 const step_info = self.allocator.create(TopLevelStep) catch unreachable;
416 *step_info = TopLevelStep {
408 step_info.* = TopLevelStep{
417409 .step = Step.initNoOp(name, self.allocator),
418410 .description = description,
419411 };
......@@ -446,9 +438,9 @@ pub const Builder = struct {
446438 }
447439
448440 pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) bool {
449 if (self.user_input_options.put(name, UserInputOption {
441 if (self.user_input_options.put(name, UserInputOption{
450442 .name = name,
451 .value = UserValue { .Scalar = value },
443 .value = UserValue{ .Scalar = value },
452444 .used = false,
453445 }) catch unreachable) |*prev_value| {
454446 // option already exists
......@@ -458,18 +450,18 @@ pub const Builder = struct {
458450 var list = ArrayList([]const u8).init(self.allocator);
459451 list.append(s) catch unreachable;
460452 list.append(value) catch unreachable;
461 _ = self.user_input_options.put(name, UserInputOption {
453 _ = self.user_input_options.put(name, UserInputOption{
462454 .name = name,
463 .value = UserValue { .List = list },
455 .value = UserValue{ .List = list },
464456 .used = false,
465457 }) catch unreachable;
466458 },
467459 UserValue.List => |*list| {
468460 // append to the list
469461 list.append(value) catch unreachable;
470 _ = self.user_input_options.put(name, UserInputOption {
462 _ = self.user_input_options.put(name, UserInputOption{
471463 .name = name,
472 .value = UserValue { .List = *list },
464 .value = UserValue{ .List = list.* },
473465 .used = false,
474466 }) catch unreachable;
475467 },
......@@ -483,9 +475,9 @@ pub const Builder = struct {
483475 }
484476
485477 pub fn addUserInputFlag(self: &Builder, name: []const u8) bool {
486 if (self.user_input_options.put(name, UserInputOption {
478 if (self.user_input_options.put(name, UserInputOption{
487479 .name = name,
488 .value = UserValue {.Flag = {} },
480 .value = UserValue{ .Flag = {} },
489481 .used = false,
490482 }) catch unreachable) |*prev_value| {
491483 switch (prev_value.value) {
......@@ -556,9 +548,7 @@ pub const Builder = struct {
556548 warn("\n");
557549 }
558550
559 fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
560 argv: []const []const u8) !void
561 {
551 fn spawnChildEnvMap(self: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) !void {
562552 if (self.verbose) {
563553 printCmd(cwd, argv);
564554 }
......@@ -617,7 +607,7 @@ pub const Builder = struct {
617607 self.pushInstalledFile(full_dest_path);
618608
619609 const install_step = self.allocator.create(InstallFileStep) catch unreachable;
620 *install_step = InstallFileStep.init(self, src_path, full_dest_path);
610 install_step.* = InstallFileStep.init(self, src_path, full_dest_path);
621611 return install_step;
622612 }
623613
......@@ -659,25 +649,23 @@ pub const Builder = struct {
659649 if (builtin.environ == builtin.Environ.msvc) {
660650 return "cl.exe";
661651 } else {
662 return os.getEnvVarOwned(self.allocator, "CC") catch |err|
652 return os.getEnvVarOwned(self.allocator, "CC") catch |err|
663653 if (err == error.EnvironmentVariableNotFound)
664654 ([]const u8)("cc")
665655 else
666 debug.panic("Unable to get environment variable: {}", err)
667 ;
656 debug.panic("Unable to get environment variable: {}", err);
668657 }
669658 }
670659
671660 pub fn findProgram(self: &Builder, names: []const []const u8, paths: []const []const u8) ![]const u8 {
672661 // TODO report error for ambiguous situations
673 const exe_extension = (Target { .Native = {}}).exeFileExt();
662 const exe_extension = (Target{ .Native = {} }).exeFileExt();
674663 for (self.search_prefixes.toSliceConst()) |search_prefix| {
675664 for (names) |name| {
676665 if (os.path.isAbsolute(name)) {
677666 return name;
678667 }
679 const full_path = try os.path.join(self.allocator, search_prefix, "bin",
680 self.fmt("{}{}", name, exe_extension));
668 const full_path = try os.path.join(self.allocator, search_prefix, "bin", self.fmt("{}{}", name, exe_extension));
681669 if (os.path.real(self.allocator, full_path)) |real_path| {
682670 return real_path;
683671 } else |_| {
......@@ -761,7 +749,7 @@ pub const Target = union(enum) {
761749 Cross: CrossTarget,
762750
763751 pub fn oFileExt(self: &const Target) []const u8 {
764 const environ = switch (*self) {
752 const environ = switch (self.*) {
765753 Target.Native => builtin.environ,
766754 Target.Cross => |t| t.environ,
767755 };
......@@ -786,7 +774,7 @@ pub const Target = union(enum) {
786774 }
787775
788776 pub fn getOs(self: &const Target) builtin.Os {
789 return switch (*self) {
777 return switch (self.*) {
790778 Target.Native => builtin.os,
791779 Target.Cross => |t| t.os,
792780 };
......@@ -794,7 +782,8 @@ pub const Target = union(enum) {
794782
795783 pub fn isDarwin(self: &const Target) bool {
796784 return switch (self.getOs()) {
797 builtin.Os.ios, builtin.Os.macosx => true,
785 builtin.Os.ios,
786 builtin.Os.macosx => true,
798787 else => false,
799788 };
800789 }
......@@ -860,61 +849,57 @@ pub const LibExeObjStep = struct {
860849 Obj,
861850 };
862851
863 pub fn createSharedLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8,
864 ver: &const Version) &LibExeObjStep
865 {
852 pub fn createSharedLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8, ver: &const Version) &LibExeObjStep {
866853 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
867 *self = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
854 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver);
868855 return self;
869856 }
870857
871858 pub fn createCSharedLibrary(builder: &Builder, name: []const u8, version: &const Version) &LibExeObjStep {
872859 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
873 *self = initC(builder, name, Kind.Lib, version, false);
860 self.* = initC(builder, name, Kind.Lib, version, false);
874861 return self;
875862 }
876863
877864 pub fn createStaticLibrary(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
878865 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
879 *self = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
866 self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0));
880867 return self;
881868 }
882869
883870 pub fn createCStaticLibrary(builder: &Builder, name: []const u8) &LibExeObjStep {
884871 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
885 *self = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
872 self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true);
886873 return self;
887874 }
888875
889876 pub fn createObject(builder: &Builder, name: []const u8, root_src: []const u8) &LibExeObjStep {
890877 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
891 *self = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
878 self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0));
892879 return self;
893880 }
894881
895882 pub fn createCObject(builder: &Builder, name: []const u8, src: []const u8) &LibExeObjStep {
896883 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
897 *self = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
884 self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false);
898885 self.object_src = src;
899886 return self;
900887 }
901888
902889 pub fn createExecutable(builder: &Builder, name: []const u8, root_src: ?[]const u8) &LibExeObjStep {
903890 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
904 *self = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
891 self.* = initExtraArgs(builder, name, root_src, Kind.Exe, false, builder.version(0, 0, 0));
905892 return self;
906893 }
907894
908895 pub fn createCExecutable(builder: &Builder, name: []const u8) &LibExeObjStep {
909896 const self = builder.allocator.create(LibExeObjStep) catch unreachable;
910 *self = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
897 self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false);
911898 return self;
912899 }
913900
914 fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind,
915 static: bool, ver: &const Version) LibExeObjStep
916 {
917 var self = LibExeObjStep {
901 fn initExtraArgs(builder: &Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: &const Version) LibExeObjStep {
902 var self = LibExeObjStep{
918903 .strip = false,
919904 .builder = builder,
920905 .verbose_link = false,
......@@ -930,7 +915,7 @@ pub const LibExeObjStep = struct {
930915 .step = Step.init(name, builder.allocator, make),
931916 .output_path = null,
932917 .output_h_path = null,
933 .version = *ver,
918 .version = ver.*,
934919 .out_filename = undefined,
935920 .out_h_filename = builder.fmt("{}.h", name),
936921 .major_only_filename = undefined,
......@@ -953,11 +938,11 @@ pub const LibExeObjStep = struct {
953938 }
954939
955940 fn initC(builder: &Builder, name: []const u8, kind: Kind, version: &const Version, static: bool) LibExeObjStep {
956 var self = LibExeObjStep {
941 var self = LibExeObjStep{
957942 .builder = builder,
958943 .name = name,
959944 .kind = kind,
960 .version = *version,
945 .version = version.*,
961946 .static = static,
962947 .target = Target.Native,
963948 .cflags = ArrayList([]const u8).init(builder.allocator),
......@@ -1005,9 +990,9 @@ pub const LibExeObjStep = struct {
1005990 self.out_filename = self.builder.fmt("lib{}.a", self.name);
1006991 } else {
1007992 switch (self.target.getOs()) {
1008 builtin.Os.ios, builtin.Os.macosx => {
1009 self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib",
1010 self.name, self.version.major, self.version.minor, self.version.patch);
993 builtin.Os.ios,
994 builtin.Os.macosx => {
995 self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch);
1011996 self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major);
1012997 self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name);
1013998 },
......@@ -1015,8 +1000,7 @@ pub const LibExeObjStep = struct {
10151000 self.out_filename = self.builder.fmt("{}.dll", self.name);
10161001 },
10171002 else => {
1018 self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}",
1019 self.name, self.version.major, self.version.minor, self.version.patch);
1003 self.out_filename = self.builder.fmt("lib{}.so.{d}.{d}.{d}", self.name, self.version.major, self.version.minor, self.version.patch);
10201004 self.major_only_filename = self.builder.fmt("lib{}.so.{d}", self.name, self.version.major);
10211005 self.name_only_filename = self.builder.fmt("lib{}.so", self.name);
10221006 },
......@@ -1026,16 +1010,12 @@ pub const LibExeObjStep = struct {
10261010 }
10271011 }
10281012
1029 pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,
1030 target_environ: builtin.Environ) void
1031 {
1032 self.target = Target {
1033 .Cross = CrossTarget {
1034 .arch = target_arch,
1035 .os = target_os,
1036 .environ = target_environ,
1037 }
1038 };
1013 pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
1014 self.target = Target{ .Cross = CrossTarget{
1015 .arch = target_arch,
1016 .os = target_os,
1017 .environ = target_environ,
1018 } };
10391019 self.computeOutFileNames();
10401020 }
10411021
......@@ -1159,7 +1139,7 @@ pub const LibExeObjStep = struct {
11591139 pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
11601140 assert(self.is_zig);
11611141
1162 self.packages.append(Pkg {
1142 self.packages.append(Pkg{
11631143 .name = name,
11641144 .path = pkg_index_path,
11651145 }) catch unreachable;
......@@ -1343,8 +1323,7 @@ pub const LibExeObjStep = struct {
13431323 try builder.spawnChild(zig_args.toSliceConst());
13441324
13451325 if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) {
1346 try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename,
1347 self.name_only_filename);
1326 try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename);
13481327 }
13491328 }
13501329
......@@ -1373,7 +1352,8 @@ pub const LibExeObjStep = struct {
13731352 args.append("ssp-buffer-size=4") catch unreachable;
13741353 }
13751354 },
1376 builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => {
1355 builtin.Mode.ReleaseFast,
1356 builtin.Mode.ReleaseSmall => {
13771357 args.append("-O2") catch unreachable;
13781358 args.append("-fno-stack-protector") catch unreachable;
13791359 },
......@@ -1505,8 +1485,7 @@ pub const LibExeObjStep = struct {
15051485 }
15061486
15071487 if (!is_darwin) {
1508 const rpath_arg = builder.fmt("-Wl,-rpath,{}",
1509 os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
1488 const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
15101489 defer builder.allocator.free(rpath_arg);
15111490 cc_args.append(rpath_arg) catch unreachable;
15121491
......@@ -1535,8 +1514,7 @@ pub const LibExeObjStep = struct {
15351514 try builder.spawnChild(cc_args.toSliceConst());
15361515
15371516 if (self.target.wantSharedLibSymLinks()) {
1538 try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename,
1539 self.name_only_filename);
1517 try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, self.name_only_filename);
15401518 }
15411519 }
15421520 },
......@@ -1581,8 +1559,7 @@ pub const LibExeObjStep = struct {
15811559 cc_args.append("-o") catch unreachable;
15821560 cc_args.append(output_path) catch unreachable;
15831561
1584 const rpath_arg = builder.fmt("-Wl,-rpath,{}",
1585 os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
1562 const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable);
15861563 defer builder.allocator.free(rpath_arg);
15871564 cc_args.append(rpath_arg) catch unreachable;
15881565
......@@ -1635,7 +1612,7 @@ pub const TestStep = struct {
16351612
16361613 pub fn init(builder: &Builder, root_src: []const u8) TestStep {
16371614 const step_name = builder.fmt("test {}", root_src);
1638 return TestStep {
1615 return TestStep{
16391616 .step = Step.init(step_name, builder.allocator, make),
16401617 .builder = builder,
16411618 .root_src = root_src,
......@@ -1644,7 +1621,7 @@ pub const TestStep = struct {
16441621 .name_prefix = "",
16451622 .filter = null,
16461623 .link_libs = BufSet.init(builder.allocator),
1647 .target = Target { .Native = {} },
1624 .target = Target{ .Native = {} },
16481625 .exec_cmd_args = null,
16491626 .include_dirs = ArrayList([]const u8).init(builder.allocator),
16501627 };
......@@ -1674,16 +1651,12 @@ pub const TestStep = struct {
16741651 self.filter = text;
16751652 }
16761653
1677 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,
1678 target_environ: builtin.Environ) void
1679 {
1680 self.target = Target {
1681 .Cross = CrossTarget {
1682 .arch = target_arch,
1683 .os = target_os,
1684 .environ = target_environ,
1685 }
1686 };
1654 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
1655 self.target = Target{ .Cross = CrossTarget{
1656 .arch = target_arch,
1657 .os = target_os,
1658 .environ = target_environ,
1659 } };
16871660 }
16881661
16891662 pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) void {
......@@ -1789,11 +1762,9 @@ pub const CommandStep = struct {
17891762 env_map: &const BufMap,
17901763
17911764 /// ::argv is copied.
1792 pub fn create(builder: &Builder, cwd: ?[]const u8, env_map: &const BufMap,
1793 argv: []const []const u8) &CommandStep
1794 {
1765 pub fn create(builder: &Builder, cwd: ?[]const u8, env_map: &const BufMap, argv: []const []const u8) &CommandStep {
17951766 const self = builder.allocator.create(CommandStep) catch unreachable;
1796 *self = CommandStep {
1767 self.* = CommandStep{
17971768 .builder = builder,
17981769 .step = Step.init(argv[0], builder.allocator, make),
17991770 .argv = builder.allocator.alloc([]u8, argv.len) catch unreachable,
......@@ -1828,7 +1799,7 @@ const InstallArtifactStep = struct {
18281799 LibExeObjStep.Kind.Exe => builder.exe_dir,
18291800 LibExeObjStep.Kind.Lib => builder.lib_dir,
18301801 };
1831 *self = Self {
1802 self.* = Self{
18321803 .builder = builder,
18331804 .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make),
18341805 .artifact = artifact,
......@@ -1837,10 +1808,8 @@ const InstallArtifactStep = struct {
18371808 self.step.dependOn(&artifact.step);
18381809 builder.pushInstalledFile(self.dest_file);
18391810 if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
1840 builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir,
1841 artifact.major_only_filename) catch unreachable);
1842 builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir,
1843 artifact.name_only_filename) catch unreachable);
1811 builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir, artifact.major_only_filename) catch unreachable);
1812 builder.pushInstalledFile(os.path.join(builder.allocator, builder.lib_dir, artifact.name_only_filename) catch unreachable);
18441813 }
18451814 return self;
18461815 }
......@@ -1859,8 +1828,7 @@ const InstallArtifactStep = struct {
18591828 };
18601829 try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode);
18611830 if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) {
1862 try doAtomicSymLinks(builder.allocator, self.dest_file,
1863 self.artifact.major_only_filename, self.artifact.name_only_filename);
1831 try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename);
18641832 }
18651833 }
18661834};
......@@ -1872,7 +1840,7 @@ pub const InstallFileStep = struct {
18721840 dest_path: []const u8,
18731841
18741842 pub fn init(builder: &Builder, src_path: []const u8, dest_path: []const u8) InstallFileStep {
1875 return InstallFileStep {
1843 return InstallFileStep{
18761844 .builder = builder,
18771845 .step = Step.init(builder.fmt("install {}", src_path), builder.allocator, make),
18781846 .src_path = src_path,
......@@ -1893,7 +1861,7 @@ pub const WriteFileStep = struct {
18931861 data: []const u8,
18941862
18951863 pub fn init(builder: &Builder, file_path: []const u8, data: []const u8) WriteFileStep {
1896 return WriteFileStep {
1864 return WriteFileStep{
18971865 .builder = builder,
18981866 .step = Step.init(builder.fmt("writefile {}", file_path), builder.allocator, make),
18991867 .file_path = file_path,
......@@ -1922,7 +1890,7 @@ pub const LogStep = struct {
19221890 data: []const u8,
19231891
19241892 pub fn init(builder: &Builder, data: []const u8) LogStep {
1925 return LogStep {
1893 return LogStep{
19261894 .builder = builder,
19271895 .step = Step.init(builder.fmt("log {}", data), builder.allocator, make),
19281896 .data = data,
......@@ -1941,7 +1909,7 @@ pub const RemoveDirStep = struct {
19411909 dir_path: []const u8,
19421910
19431911 pub fn init(builder: &Builder, dir_path: []const u8) RemoveDirStep {
1944 return RemoveDirStep {
1912 return RemoveDirStep{
19451913 .builder = builder,
19461914 .step = Step.init(builder.fmt("RemoveDir {}", dir_path), builder.allocator, make),
19471915 .dir_path = dir_path,
......@@ -1966,8 +1934,8 @@ pub const Step = struct {
19661934 loop_flag: bool,
19671935 done_flag: bool,
19681936
1969 pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step)error!void) Step {
1970 return Step {
1937 pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn(&Step) error!void) Step {
1938 return Step{
19711939 .name = name,
19721940 .makeFn = makeFn,
19731941 .dependencies = ArrayList(&Step).init(allocator),
......@@ -1980,8 +1948,7 @@ pub const Step = struct {
19801948 }
19811949
19821950 pub fn make(self: &Step) !void {
1983 if (self.done_flag)
1984 return;
1951 if (self.done_flag) return;
19851952
19861953 try self.makeFn(self);
19871954 self.done_flag = true;
......@@ -1994,9 +1961,7 @@ pub const Step = struct {
19941961 fn makeNoOp(self: &Step) error!void {}
19951962};
19961963
1997fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8,
1998 filename_name_only: []const u8) !void
1999{
1964fn doAtomicSymLinks(allocator: &Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {
20001965 const out_dir = os.path.dirname(output_path);
20011966 const out_basename = os.path.basename(output_path);
20021967 // sym link for libfoo.so.1 to libfoo.so.1.2.3
std/crypto/blake2.zig+470-241
......@@ -6,11 +6,23 @@ const builtin = @import("builtin");
66const htest = @import("test.zig");
77
88const RoundParam = struct {
9 a: usize, b: usize, c: usize, d: usize, x: usize, y: usize,
9 a: usize,
10 b: usize,
11 c: usize,
12 d: usize,
13 x: usize,
14 y: usize,
1015};
1116
1217fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
13 return RoundParam { .a = a, .b = b, .c = c, .d = d, .x = x, .y = y, };
18 return RoundParam{
19 .a = a,
20 .b = b,
21 .c = c,
22 .d = d,
23 .x = x,
24 .y = y,
25 };
1426}
1527
1628/////////////////////
......@@ -19,145 +31,153 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, x: usize, y: usize) RoundParam {
1931pub const Blake2s224 = Blake2s(224);
2032pub const Blake2s256 = Blake2s(256);
2133
22fn Blake2s(comptime out_len: usize) type { return struct {
23 const Self = this;
24 const block_size = 64;
25 const digest_size = out_len / 8;
34fn Blake2s(comptime out_len: usize) type {
35 return struct {
36 const Self = this;
37 const block_size = 64;
38 const digest_size = out_len / 8;
39
40 const iv = [8]u32{
41 0x6A09E667,
42 0xBB67AE85,
43 0x3C6EF372,
44 0xA54FF53A,
45 0x510E527F,
46 0x9B05688C,
47 0x1F83D9AB,
48 0x5BE0CD19,
49 };
2650
27 const iv = [8]u32 {
28 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
29 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
30 };
51 const sigma = [10][16]u8{
52 []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
53 []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
54 []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
55 []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
56 []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
57 []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
58 []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
59 []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
60 []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
61 []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
62 };
3163
32 const sigma = [10][16]u8 {
33 []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
34 []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
35 []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
36 []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
37 []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
38 []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
39 []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
40 []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
41 []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
42 []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
43 };
64 h: [8]u32,
65 t: u64,
66 // Streaming cache
67 buf: [64]u8,
68 buf_len: u8,
4469
45 h: [8]u32,
46 t: u64,
47 // Streaming cache
48 buf: [64]u8,
49 buf_len: u8,
50
51 pub fn init() Self {
52 debug.assert(8 <= out_len and out_len <= 512);
53
54 var s: Self = undefined;
55 s.reset();
56 return s;
57 }
58
59 pub fn reset(d: &Self) void {
60 mem.copy(u32, d.h[0..], iv[0..]);
61
62 // No key plus default parameters
63 d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
64 d.t = 0;
65 d.buf_len = 0;
66 }
67
68 pub fn hash(b: []const u8, out: []u8) void {
69 var d = Self.init();
70 d.update(b);
71 d.final(out);
72 }
73
74 pub fn update(d: &Self, b: []const u8) void {
75 var off: usize = 0;
76
77 // Partial buffer exists from previous update. Copy into buffer then hash.
78 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
79 off += 64 - d.buf_len;
80 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
81 d.t += 64;
82 d.round(d.buf[0..], false);
83 d.buf_len = 0;
70 pub fn init() Self {
71 debug.assert(8 <= out_len and out_len <= 512);
72
73 var s: Self = undefined;
74 s.reset();
75 return s;
8476 }
8577
86 // Full middle blocks.
87 while (off + 64 <= b.len) : (off += 64) {
88 d.t += 64;
89 d.round(b[off..off + 64], false);
78 pub fn reset(d: &Self) void {
79 mem.copy(u32, d.h[0..], iv[0..]);
80
81 // No key plus default parameters
82 d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
83 d.t = 0;
84 d.buf_len = 0;
9085 }
9186
92 // Copy any remainder for next pass.
93 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
94 d.buf_len += u8(b[off..].len);
95 }
87 pub fn hash(b: []const u8, out: []u8) void {
88 var d = Self.init();
89 d.update(b);
90 d.final(out);
91 }
9692
97 pub fn final(d: &Self, out: []u8) void {
98 debug.assert(out.len >= out_len / 8);
93 pub fn update(d: &Self, b: []const u8) void {
94 var off: usize = 0;
9995
100 mem.set(u8, d.buf[d.buf_len..], 0);
101 d.t += d.buf_len;
102 d.round(d.buf[0..], true);
96 // Partial buffer exists from previous update. Copy into buffer then hash.
97 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
98 off += 64 - d.buf_len;
99 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
100 d.t += 64;
101 d.round(d.buf[0..], false);
102 d.buf_len = 0;
103 }
103104
104 const rr = d.h[0 .. out_len / 32];
105 // Full middle blocks.
106 while (off + 64 <= b.len) : (off += 64) {
107 d.t += 64;
108 d.round(b[off..off + 64], false);
109 }
105110
106 for (rr) |s, j| {
107 mem.writeInt(out[4*j .. 4*j + 4], s, builtin.Endian.Little);
111 // Copy any remainder for next pass.
112 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
113 d.buf_len += u8(b[off..].len);
108114 }
109 }
110115
111 fn round(d: &Self, b: []const u8, last: bool) void {
112 debug.assert(b.len == 64);
116 pub fn final(d: &Self, out: []u8) void {
117 debug.assert(out.len >= out_len / 8);
113118
114 var m: [16]u32 = undefined;
115 var v: [16]u32 = undefined;
119 mem.set(u8, d.buf[d.buf_len..], 0);
120 d.t += d.buf_len;
121 d.round(d.buf[0..], true);
116122
117 for (m) |*r, i| {
118 *r = mem.readIntLE(u32, b[4*i .. 4*i + 4]);
119 }
123 const rr = d.h[0..out_len / 32];
120124
121 var k: usize = 0;
122 while (k < 8) : (k += 1) {
123 v[k] = d.h[k];
124 v[k+8] = iv[k];
125 for (rr) |s, j| {
126 mem.writeInt(out[4 * j..4 * j + 4], s, builtin.Endian.Little);
127 }
125128 }
126129
127 v[12] ^= @truncate(u32, d.t);
128 v[13] ^= u32(d.t >> 32);
129 if (last) v[14] = ~v[14];
130
131 const rounds = comptime []RoundParam {
132 Rp(0, 4, 8, 12, 0, 1),
133 Rp(1, 5, 9, 13, 2, 3),
134 Rp(2, 6, 10, 14, 4, 5),
135 Rp(3, 7, 11, 15, 6, 7),
136 Rp(0, 5, 10, 15, 8, 9),
137 Rp(1, 6, 11, 12, 10, 11),
138 Rp(2, 7, 8, 13, 12, 13),
139 Rp(3, 4, 9, 14, 14, 15),
140 };
130 fn round(d: &Self, b: []const u8, last: bool) void {
131 debug.assert(b.len == 64);
141132
142 comptime var j: usize = 0;
143 inline while (j < 10) : (j += 1) {
144 inline for (rounds) |r| {
145 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
146 v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(16));
147 v[r.c] = v[r.c] +% v[r.d];
148 v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(12));
149 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
150 v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(8));
151 v[r.c] = v[r.c] +% v[r.d];
152 v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(7));
133 var m: [16]u32 = undefined;
134 var v: [16]u32 = undefined;
135
136 for (m) |*r, i| {
137 r.* = mem.readIntLE(u32, b[4 * i..4 * i + 4]);
153138 }
154 }
155139
156 for (d.h) |*r, i| {
157 *r ^= v[i] ^ v[i + 8];
140 var k: usize = 0;
141 while (k < 8) : (k += 1) {
142 v[k] = d.h[k];
143 v[k + 8] = iv[k];
144 }
145
146 v[12] ^= @truncate(u32, d.t);
147 v[13] ^= u32(d.t >> 32);
148 if (last) v[14] = ~v[14];
149
150 const rounds = comptime []RoundParam{
151 Rp(0, 4, 8, 12, 0, 1),
152 Rp(1, 5, 9, 13, 2, 3),
153 Rp(2, 6, 10, 14, 4, 5),
154 Rp(3, 7, 11, 15, 6, 7),
155 Rp(0, 5, 10, 15, 8, 9),
156 Rp(1, 6, 11, 12, 10, 11),
157 Rp(2, 7, 8, 13, 12, 13),
158 Rp(3, 4, 9, 14, 14, 15),
159 };
160
161 comptime var j: usize = 0;
162 inline while (j < 10) : (j += 1) {
163 inline for (rounds) |r| {
164 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
165 v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(16));
166 v[r.c] = v[r.c] +% v[r.d];
167 v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(12));
168 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
169 v[r.d] = math.rotr(u32, v[r.d] ^ v[r.a], usize(8));
170 v[r.c] = v[r.c] +% v[r.d];
171 v[r.b] = math.rotr(u32, v[r.b] ^ v[r.c], usize(7));
172 }
173 }
174
175 for (d.h) |*r, i| {
176 r.* ^= v[i] ^ v[i + 8];
177 }
158178 }
159 }
160};}
179 };
180}
161181
162182test "blake2s224 single" {
163183 const h1 = "1fa1291e65248b37b3433475b2a0dd63d54a11ecc4e3e034e7bc1ef4";
......@@ -230,7 +250,7 @@ test "blake2s256 streaming" {
230250}
231251
232252test "blake2s256 aligned final" {
233 var block = []u8 {0} ** Blake2s256.block_size;
253 var block = []u8{0} ** Blake2s256.block_size;
234254 var out: [Blake2s256.digest_size]u8 = undefined;
235255
236256 var h = Blake2s256.init();
......@@ -238,154 +258,363 @@ test "blake2s256 aligned final" {
238258 h.final(out[0..]);
239259}
240260
241
242261/////////////////////
243262// Blake2b
244263
245264pub const Blake2b384 = Blake2b(384);
246265pub const Blake2b512 = Blake2b(512);
247266
248fn Blake2b(comptime out_len: usize) type { return struct {
249 const Self = this;
250 const block_size = 128;
251 const digest_size = out_len / 8;
267fn Blake2b(comptime out_len: usize) type {
268 return struct {
269 const Self = this;
270 const block_size = 128;
271 const digest_size = out_len / 8;
272
273 const iv = [8]u64{
274 0x6a09e667f3bcc908,
275 0xbb67ae8584caa73b,
276 0x3c6ef372fe94f82b,
277 0xa54ff53a5f1d36f1,
278 0x510e527fade682d1,
279 0x9b05688c2b3e6c1f,
280 0x1f83d9abfb41bd6b,
281 0x5be0cd19137e2179,
282 };
252283
253 const iv = [8]u64 {
254 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
255 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
256 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
257 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
258 };
284 const sigma = [12][16]u8{
285 []const u8{
286 0,
287 1,
288 2,
289 3,
290 4,
291 5,
292 6,
293 7,
294 8,
295 9,
296 10,
297 11,
298 12,
299 13,
300 14,
301 15,
302 },
303 []const u8{
304 14,
305 10,
306 4,
307 8,
308 9,
309 15,
310 13,
311 6,
312 1,
313 12,
314 0,
315 2,
316 11,
317 7,
318 5,
319 3,
320 },
321 []const u8{
322 11,
323 8,
324 12,
325 0,
326 5,
327 2,
328 15,
329 13,
330 10,
331 14,
332 3,
333 6,
334 7,
335 1,
336 9,
337 4,
338 },
339 []const u8{
340 7,
341 9,
342 3,
343 1,
344 13,
345 12,
346 11,
347 14,
348 2,
349 6,
350 5,
351 10,
352 4,
353 0,
354 15,
355 8,
356 },
357 []const u8{
358 9,
359 0,
360 5,
361 7,
362 2,
363 4,
364 10,
365 15,
366 14,
367 1,
368 11,
369 12,
370 6,
371 8,
372 3,
373 13,
374 },
375 []const u8{
376 2,
377 12,
378 6,
379 10,
380 0,
381 11,
382 8,
383 3,
384 4,
385 13,
386 7,
387 5,
388 15,
389 14,
390 1,
391 9,
392 },
393 []const u8{
394 12,
395 5,
396 1,
397 15,
398 14,
399 13,
400 4,
401 10,
402 0,
403 7,
404 6,
405 3,
406 9,
407 2,
408 8,
409 11,
410 },
411 []const u8{
412 13,
413 11,
414 7,
415 14,
416 12,
417 1,
418 3,
419 9,
420 5,
421 0,
422 15,
423 4,
424 8,
425 6,
426 2,
427 10,
428 },
429 []const u8{
430 6,
431 15,
432 14,
433 9,
434 11,
435 3,
436 0,
437 8,
438 12,
439 2,
440 13,
441 7,
442 1,
443 4,
444 10,
445 5,
446 },
447 []const u8{
448 10,
449 2,
450 8,
451 4,
452 7,
453 6,
454 1,
455 5,
456 15,
457 11,
458 9,
459 14,
460 3,
461 12,
462 13,
463 0,
464 },
465 []const u8{
466 0,
467 1,
468 2,
469 3,
470 4,
471 5,
472 6,
473 7,
474 8,
475 9,
476 10,
477 11,
478 12,
479 13,
480 14,
481 15,
482 },
483 []const u8{
484 14,
485 10,
486 4,
487 8,
488 9,
489 15,
490 13,
491 6,
492 1,
493 12,
494 0,
495 2,
496 11,
497 7,
498 5,
499 3,
500 },
501 };
259502
260 const sigma = [12][16]u8 {
261 []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
262 []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
263 []const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
264 []const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
265 []const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
266 []const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
267 []const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
268 []const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
269 []const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
270 []const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
271 []const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
272 []const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
273 };
503 h: [8]u64,
504 t: u128,
505 // Streaming cache
506 buf: [128]u8,
507 buf_len: u8,
274508
275 h: [8]u64,
276 t: u128,
277 // Streaming cache
278 buf: [128]u8,
279 buf_len: u8,
280
281 pub fn init() Self {
282 debug.assert(8 <= out_len and out_len <= 512);
283
284 var s: Self = undefined;
285 s.reset();
286 return s;
287 }
288
289 pub fn reset(d: &Self) void {
290 mem.copy(u64, d.h[0..], iv[0..]);
291
292 // No key plus default parameters
293 d.h[0] ^= 0x01010000 ^ (out_len >> 3);
294 d.t = 0;
295 d.buf_len = 0;
296 }
297
298 pub fn hash(b: []const u8, out: []u8) void {
299 var d = Self.init();
300 d.update(b);
301 d.final(out);
302 }
303
304 pub fn update(d: &Self, b: []const u8) void {
305 var off: usize = 0;
306
307 // Partial buffer exists from previous update. Copy into buffer then hash.
308 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
309 off += 128 - d.buf_len;
310 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
311 d.t += 128;
312 d.round(d.buf[0..], false);
509 pub fn init() Self {
510 debug.assert(8 <= out_len and out_len <= 512);
511
512 var s: Self = undefined;
513 s.reset();
514 return s;
515 }
516
517 pub fn reset(d: &Self) void {
518 mem.copy(u64, d.h[0..], iv[0..]);
519
520 // No key plus default parameters
521 d.h[0] ^= 0x01010000 ^ (out_len >> 3);
522 d.t = 0;
313523 d.buf_len = 0;
314524 }
315525
316 // Full middle blocks.
317 while (off + 128 <= b.len) : (off += 128) {
318 d.t += 128;
319 d.round(b[off..off + 128], false);
526 pub fn hash(b: []const u8, out: []u8) void {
527 var d = Self.init();
528 d.update(b);
529 d.final(out);
320530 }
321531
322 // Copy any remainder for next pass.
323 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
324 d.buf_len += u8(b[off..].len);
325 }
532 pub fn update(d: &Self, b: []const u8) void {
533 var off: usize = 0;
326534
327 pub fn final(d: &Self, out: []u8) void {
328 mem.set(u8, d.buf[d.buf_len..], 0);
329 d.t += d.buf_len;
330 d.round(d.buf[0..], true);
535 // Partial buffer exists from previous update. Copy into buffer then hash.
536 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
537 off += 128 - d.buf_len;
538 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
539 d.t += 128;
540 d.round(d.buf[0..], false);
541 d.buf_len = 0;
542 }
331543
332 const rr = d.h[0 .. out_len / 64];
544 // Full middle blocks.
545 while (off + 128 <= b.len) : (off += 128) {
546 d.t += 128;
547 d.round(b[off..off + 128], false);
548 }
333549
334 for (rr) |s, j| {
335 mem.writeInt(out[8*j .. 8*j + 8], s, builtin.Endian.Little);
550 // Copy any remainder for next pass.
551 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
552 d.buf_len += u8(b[off..].len);
336553 }
337 }
338554
339 fn round(d: &Self, b: []const u8, last: bool) void {
340 debug.assert(b.len == 128);
555 pub fn final(d: &Self, out: []u8) void {
556 mem.set(u8, d.buf[d.buf_len..], 0);
557 d.t += d.buf_len;
558 d.round(d.buf[0..], true);
341559
342 var m: [16]u64 = undefined;
343 var v: [16]u64 = undefined;
560 const rr = d.h[0..out_len / 64];
344561
345 for (m) |*r, i| {
346 *r = mem.readIntLE(u64, b[8*i .. 8*i + 8]);
562 for (rr) |s, j| {
563 mem.writeInt(out[8 * j..8 * j + 8], s, builtin.Endian.Little);
564 }
347565 }
348566
349 var k: usize = 0;
350 while (k < 8) : (k += 1) {
351 v[k] = d.h[k];
352 v[k+8] = iv[k];
353 }
567 fn round(d: &Self, b: []const u8, last: bool) void {
568 debug.assert(b.len == 128);
354569
355 v[12] ^= @truncate(u64, d.t);
356 v[13] ^= u64(d.t >> 64);
357 if (last) v[14] = ~v[14];
358
359 const rounds = comptime []RoundParam {
360 Rp(0, 4, 8, 12, 0, 1),
361 Rp(1, 5, 9, 13, 2, 3),
362 Rp(2, 6, 10, 14, 4, 5),
363 Rp(3, 7, 11, 15, 6, 7),
364 Rp(0, 5, 10, 15, 8, 9),
365 Rp(1, 6, 11, 12, 10, 11),
366 Rp(2, 7, 8, 13, 12, 13),
367 Rp(3, 4, 9, 14, 14, 15),
368 };
570 var m: [16]u64 = undefined;
571 var v: [16]u64 = undefined;
369572
370 comptime var j: usize = 0;
371 inline while (j < 12) : (j += 1) {
372 inline for (rounds) |r| {
373 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
374 v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(32));
375 v[r.c] = v[r.c] +% v[r.d];
376 v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(24));
377 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
378 v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(16));
379 v[r.c] = v[r.c] +% v[r.d];
380 v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(63));
573 for (m) |*r, i| {
574 r.* = mem.readIntLE(u64, b[8 * i..8 * i + 8]);
575 }
576
577 var k: usize = 0;
578 while (k < 8) : (k += 1) {
579 v[k] = d.h[k];
580 v[k + 8] = iv[k];
381581 }
382 }
383582
384 for (d.h) |*r, i| {
385 *r ^= v[i] ^ v[i + 8];
583 v[12] ^= @truncate(u64, d.t);
584 v[13] ^= u64(d.t >> 64);
585 if (last) v[14] = ~v[14];
586
587 const rounds = comptime []RoundParam{
588 Rp(0, 4, 8, 12, 0, 1),
589 Rp(1, 5, 9, 13, 2, 3),
590 Rp(2, 6, 10, 14, 4, 5),
591 Rp(3, 7, 11, 15, 6, 7),
592 Rp(0, 5, 10, 15, 8, 9),
593 Rp(1, 6, 11, 12, 10, 11),
594 Rp(2, 7, 8, 13, 12, 13),
595 Rp(3, 4, 9, 14, 14, 15),
596 };
597
598 comptime var j: usize = 0;
599 inline while (j < 12) : (j += 1) {
600 inline for (rounds) |r| {
601 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.x]];
602 v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(32));
603 v[r.c] = v[r.c] +% v[r.d];
604 v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(24));
605 v[r.a] = v[r.a] +% v[r.b] +% m[sigma[j][r.y]];
606 v[r.d] = math.rotr(u64, v[r.d] ^ v[r.a], usize(16));
607 v[r.c] = v[r.c] +% v[r.d];
608 v[r.b] = math.rotr(u64, v[r.b] ^ v[r.c], usize(63));
609 }
610 }
611
612 for (d.h) |*r, i| {
613 r.* ^= v[i] ^ v[i + 8];
614 }
386615 }
387 }
388};}
616 };
617}
389618
390619test "blake2b384 single" {
391620 const h1 = "b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100";
......@@ -458,7 +687,7 @@ test "blake2b512 streaming" {
458687}
459688
460689test "blake2b512 aligned final" {
461 var block = []u8 {0} ** Blake2b512.block_size;
690 var block = []u8{0} ** Blake2b512.block_size;
462691 var out: [Blake2b512.digest_size]u8 = undefined;
463692
464693 var h = Blake2b512.init();
std/crypto/hmac.zig+2-2
......@@ -29,12 +29,12 @@ pub fn Hmac(comptime H: type) type {
2929
3030 var o_key_pad: [H.block_size]u8 = undefined;
3131 for (o_key_pad) |*b, i| {
32 *b = scratch[i] ^ 0x5c;
32 b.* = scratch[i] ^ 0x5c;
3333 }
3434
3535 var i_key_pad: [H.block_size]u8 = undefined;
3636 for (i_key_pad) |*b, i| {
37 *b = scratch[i] ^ 0x36;
37 b.* = scratch[i] ^ 0x36;
3838 }
3939
4040 // HMAC(k, m) = H(o_key_pad | H(i_key_pad | message)) where | is concatenation
std/crypto/sha3.zig+180-101
......@@ -10,148 +10,228 @@ pub const Sha3_256 = Keccak(256, 0x06);
1010pub const Sha3_384 = Keccak(384, 0x06);
1111pub const Sha3_512 = Keccak(512, 0x06);
1212
13fn Keccak(comptime bits: usize, comptime delim: u8) type { return struct {
14 const Self = this;
15 const block_size = 200;
16 const digest_size = bits / 8;
17
18 s: [200]u8,
19 offset: usize,
20 rate: usize,
21
22 pub fn init() Self {
23 var d: Self = undefined;
24 d.reset();
25 return d;
26 }
13fn Keccak(comptime bits: usize, comptime delim: u8) type {
14 return struct {
15 const Self = this;
16 const block_size = 200;
17 const digest_size = bits / 8;
18
19 s: [200]u8,
20 offset: usize,
21 rate: usize,
22
23 pub fn init() Self {
24 var d: Self = undefined;
25 d.reset();
26 return d;
27 }
2728
28 pub fn reset(d: &Self) void {
29 mem.set(u8, d.s[0..], 0);
30 d.offset = 0;
31 d.rate = 200 - (bits / 4);
32 }
29 pub fn reset(d: &Self) void {
30 mem.set(u8, d.s[0..], 0);
31 d.offset = 0;
32 d.rate = 200 - (bits / 4);
33 }
3334
34 pub fn hash(b: []const u8, out: []u8) void {
35 var d = Self.init();
36 d.update(b);
37 d.final(out);
38 }
35 pub fn hash(b: []const u8, out: []u8) void {
36 var d = Self.init();
37 d.update(b);
38 d.final(out);
39 }
3940
40 pub fn update(d: &Self, b: []const u8) void {
41 var ip: usize = 0;
42 var len = b.len;
43 var rate = d.rate - d.offset;
44 var offset = d.offset;
41 pub fn update(d: &Self, b: []const u8) void {
42 var ip: usize = 0;
43 var len = b.len;
44 var rate = d.rate - d.offset;
45 var offset = d.offset;
4546
46 // absorb
47 while (len >= rate) {
48 for (d.s[offset .. offset + rate]) |*r, i|
49 *r ^= b[ip..][i];
47 // absorb
48 while (len >= rate) {
49 for (d.s[offset..offset + rate]) |*r, i|
50 r.* ^= b[ip..][i];
5051
51 keccak_f(1600, d.s[0..]);
52 keccak_f(1600, d.s[0..]);
5253
53 ip += rate;
54 len -= rate;
55 rate = d.rate;
56 offset = 0;
57 }
54 ip += rate;
55 len -= rate;
56 rate = d.rate;
57 offset = 0;
58 }
5859
59 for (d.s[offset .. offset + len]) |*r, i|
60 *r ^= b[ip..][i];
60 for (d.s[offset..offset + len]) |*r, i|
61 r.* ^= b[ip..][i];
6162
62 d.offset = offset + len;
63 }
63 d.offset = offset + len;
64 }
6465
65 pub fn final(d: &Self, out: []u8) void {
66 // padding
67 d.s[d.offset] ^= delim;
68 d.s[d.rate - 1] ^= 0x80;
66 pub fn final(d: &Self, out: []u8) void {
67 // padding
68 d.s[d.offset] ^= delim;
69 d.s[d.rate - 1] ^= 0x80;
6970
70 keccak_f(1600, d.s[0..]);
71 keccak_f(1600, d.s[0..]);
7172
72 // squeeze
73 var op: usize = 0;
74 var len: usize = bits / 8;
73 // squeeze
74 var op: usize = 0;
75 var len: usize = bits / 8;
7576
76 while (len >= d.rate) {
77 mem.copy(u8, out[op..], d.s[0..d.rate]);
78 keccak_f(1600, d.s[0..]);
79 op += d.rate;
80 len -= d.rate;
77 while (len >= d.rate) {
78 mem.copy(u8, out[op..], d.s[0..d.rate]);
79 keccak_f(1600, d.s[0..]);
80 op += d.rate;
81 len -= d.rate;
82 }
83
84 mem.copy(u8, out[op..], d.s[0..len]);
8185 }
86 };
87}
8288
83 mem.copy(u8, out[op..], d.s[0..len]);
84 }
85};}
86
87const RC = []const u64 {
88 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
89 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
90 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
91 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
92 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
93 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
89const RC = []const u64{
90 0x0000000000000001,
91 0x0000000000008082,
92 0x800000000000808a,
93 0x8000000080008000,
94 0x000000000000808b,
95 0x0000000080000001,
96 0x8000000080008081,
97 0x8000000000008009,
98 0x000000000000008a,
99 0x0000000000000088,
100 0x0000000080008009,
101 0x000000008000000a,
102 0x000000008000808b,
103 0x800000000000008b,
104 0x8000000000008089,
105 0x8000000000008003,
106 0x8000000000008002,
107 0x8000000000000080,
108 0x000000000000800a,
109 0x800000008000000a,
110 0x8000000080008081,
111 0x8000000000008080,
112 0x0000000080000001,
113 0x8000000080008008,
94114};
95115
96const ROTC = []const usize {
97 1, 3, 6, 10, 15, 21, 28, 36,
98 45, 55, 2, 14, 27, 41, 56, 8,
99 25, 43, 62, 18, 39, 61, 20, 44
116const ROTC = []const usize{
117 1,
118 3,
119 6,
120 10,
121 15,
122 21,
123 28,
124 36,
125 45,
126 55,
127 2,
128 14,
129 27,
130 41,
131 56,
132 8,
133 25,
134 43,
135 62,
136 18,
137 39,
138 61,
139 20,
140 44,
100141};
101142
102const PIL = []const usize {
103 10, 7, 11, 17, 18, 3, 5, 16,
104 8, 21, 24, 4, 15, 23, 19, 13,
105 12, 2, 20, 14, 22, 9, 6, 1
143const PIL = []const usize{
144 10,
145 7,
146 11,
147 17,
148 18,
149 3,
150 5,
151 16,
152 8,
153 21,
154 24,
155 4,
156 15,
157 23,
158 19,
159 13,
160 12,
161 2,
162 20,
163 14,
164 22,
165 9,
166 6,
167 1,
106168};
107169
108const M5 = []const usize {
109 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
170const M5 = []const usize{
171 0,
172 1,
173 2,
174 3,
175 4,
176 0,
177 1,
178 2,
179 3,
180 4,
110181};
111182
112183fn keccak_f(comptime F: usize, d: []u8) void {
113184 debug.assert(d.len == F / 8);
114185
115186 const B = F / 25;
116 const no_rounds = comptime x: { break :x 12 + 2 * math.log2(B); };
187 const no_rounds = comptime x: {
188 break :x 12 + 2 * math.log2(B);
189 };
117190
118 var s = []const u64 {0} ** 25;
119 var t = []const u64 {0} ** 1;
120 var c = []const u64 {0} ** 5;
191 var s = []const u64{0} ** 25;
192 var t = []const u64{0} ** 1;
193 var c = []const u64{0} ** 5;
121194
122195 for (s) |*r, i| {
123 *r = mem.readIntLE(u64, d[8*i .. 8*i + 8]);
196 r.* = mem.readIntLE(u64, d[8 * i..8 * i + 8]);
124197 }
125198
126199 comptime var x: usize = 0;
127200 comptime var y: usize = 0;
128201 for (RC[0..no_rounds]) |round| {
129202 // theta
130 x = 0; inline while (x < 5) : (x += 1) {
131 c[x] = s[x] ^ s[x+5] ^ s[x+10] ^ s[x+15] ^ s[x+20];
203 x = 0;
204 inline while (x < 5) : (x += 1) {
205 c[x] = s[x] ^ s[x + 5] ^ s[x + 10] ^ s[x + 15] ^ s[x + 20];
132206 }
133 x = 0; inline while (x < 5) : (x += 1) {
134 t[0] = c[M5[x+4]] ^ math.rotl(u64, c[M5[x+1]], usize(1));
135 y = 0; inline while (y < 5) : (y += 1) {
136 s[x + y*5] ^= t[0];
207 x = 0;
208 inline while (x < 5) : (x += 1) {
209 t[0] = c[M5[x + 4]] ^ math.rotl(u64, c[M5[x + 1]], usize(1));
210 y = 0;
211 inline while (y < 5) : (y += 1) {
212 s[x + y * 5] ^= t[0];
137213 }
138214 }
139215
140216 // rho+pi
141217 t[0] = s[1];
142 x = 0; inline while (x < 24) : (x += 1) {
218 x = 0;
219 inline while (x < 24) : (x += 1) {
143220 c[0] = s[PIL[x]];
144221 s[PIL[x]] = math.rotl(u64, t[0], ROTC[x]);
145222 t[0] = c[0];
146223 }
147224
148225 // chi
149 y = 0; inline while (y < 5) : (y += 1) {
150 x = 0; inline while (x < 5) : (x += 1) {
151 c[x] = s[x + y*5];
226 y = 0;
227 inline while (y < 5) : (y += 1) {
228 x = 0;
229 inline while (x < 5) : (x += 1) {
230 c[x] = s[x + y * 5];
152231 }
153 x = 0; inline while (x < 5) : (x += 1) {
154 s[x + y*5] = c[x] ^ (~c[M5[x+1]] & c[M5[x+2]]);
232 x = 0;
233 inline while (x < 5) : (x += 1) {
234 s[x + y * 5] = c[x] ^ (~c[M5[x + 1]] & c[M5[x + 2]]);
155235 }
156236 }
157237
......@@ -160,11 +240,10 @@ fn keccak_f(comptime F: usize, d: []u8) void {
160240 }
161241
162242 for (s) |r, i| {
163 mem.writeInt(d[8*i .. 8*i + 8], r, builtin.Endian.Little);
243 mem.writeInt(d[8 * i..8 * i + 8], r, builtin.Endian.Little);
164244 }
165245}
166246
167
168247test "sha3-224 single" {
169248 htest.assertEqualHash(Sha3_224, "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7", "");
170249 htest.assertEqualHash(Sha3_224, "e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf", "abc");
......@@ -192,7 +271,7 @@ test "sha3-224 streaming" {
192271}
193272
194273test "sha3-256 single" {
195 htest.assertEqualHash(Sha3_256, "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a" , "");
274 htest.assertEqualHash(Sha3_256, "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a", "");
196275 htest.assertEqualHash(Sha3_256, "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", "abc");
197276 htest.assertEqualHash(Sha3_256, "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
198277}
......@@ -218,7 +297,7 @@ test "sha3-256 streaming" {
218297}
219298
220299test "sha3-256 aligned final" {
221 var block = []u8 {0} ** Sha3_256.block_size;
300 var block = []u8{0} ** Sha3_256.block_size;
222301 var out: [Sha3_256.digest_size]u8 = undefined;
223302
224303 var h = Sha3_256.init();
......@@ -228,7 +307,7 @@ test "sha3-256 aligned final" {
228307
229308test "sha3-384 single" {
230309 const h1 = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004";
231 htest.assertEqualHash(Sha3_384, h1 , "");
310 htest.assertEqualHash(Sha3_384, h1, "");
232311 const h2 = "ec01498288516fc926459f58e2c6ad8df9b473cb0fc08c2596da7cf0e49be4b298d88cea927ac7f539f1edf228376d25";
233312 htest.assertEqualHash(Sha3_384, h2, "abc");
234313 const h3 = "79407d3b5916b59c3e30b09822974791c313fb9ecc849e406f23592d04f625dc8c709b98b43b3852b337216179aa7fc7";
......@@ -259,7 +338,7 @@ test "sha3-384 streaming" {
259338
260339test "sha3-512 single" {
261340 const h1 = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26";
262 htest.assertEqualHash(Sha3_512, h1 , "");
341 htest.assertEqualHash(Sha3_512, h1, "");
263342 const h2 = "b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0";
264343 htest.assertEqualHash(Sha3_512, h2, "abc");
265344 const h3 = "afebb2ef542e6579c50cad06d2e578f9f8dd6881d7dc824d26360feebf18a4fa73e3261122948efcfd492e74e82e2189ed0fb440d187f382270cb455f21dd185";
......@@ -289,7 +368,7 @@ test "sha3-512 streaming" {
289368}
290369
291370test "sha3-512 aligned final" {
292 var block = []u8 {0} ** Sha3_512.block_size;
371 var block = []u8{0} ** Sha3_512.block_size;
293372 var out: [Sha3_512.digest_size]u8 = undefined;
294373
295374 var h = Sha3_512.init();
std/crypto/test.zig+1-2
......@@ -14,9 +14,8 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu
1414pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {
1515 var expected_bytes: [expected.len / 2]u8 = undefined;
1616 for (expected_bytes) |*r, i| {
17 *r = fmt.parseInt(u8, expected[2*i .. 2*i+2], 16) catch unreachable;
17 r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;
1818 }
1919
2020 debug.assert(mem.eql(u8, expected_bytes, input));
2121}
22
std/debug/index.zig+98-135
......@@ -104,9 +104,7 @@ pub fn panic(comptime format: []const u8, args: ...) noreturn {
104104
105105var panicking: u8 = 0; // TODO make this a bool
106106
107pub fn panicExtra(trace: ?&const builtin.StackTrace, first_trace_addr: ?usize,
108 comptime format: []const u8, args: ...) noreturn
109{
107pub fn panicExtra(trace: ?&const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: ...) noreturn {
110108 @setCold(true);
111109
112110 if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
......@@ -132,9 +130,7 @@ const WHITE = "\x1b[37;1m";
132130const DIM = "\x1b[2m";
133131const RESET = "\x1b[0m";
134132
135pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var, allocator: &mem.Allocator,
136 debug_info: &ElfStackTrace, tty_color: bool) !void
137{
133pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool) !void {
138134 var frame_index: usize = undefined;
139135 var frames_left: usize = undefined;
140136 if (stack_trace.index < stack_trace.instruction_addresses.len) {
......@@ -154,9 +150,7 @@ pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var,
154150 }
155151}
156152
157pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator,
158 debug_info: &ElfStackTrace, tty_color: bool, start_addr: ?usize) !void
159{
153pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool, start_addr: ?usize) !void {
160154 const AddressState = union(enum) {
161155 NotLookingForStartAddress,
162156 LookingForStartAddress: usize,
......@@ -166,14 +160,14 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator,
166160 // else AddressState.NotLookingForStartAddress;
167161 var addr_state: AddressState = undefined;
168162 if (start_addr) |addr| {
169 addr_state = AddressState { .LookingForStartAddress = addr };
163 addr_state = AddressState{ .LookingForStartAddress = addr };
170164 } else {
171165 addr_state = AddressState.NotLookingForStartAddress;
172166 }
173167
174168 var fp = @ptrToInt(@frameAddress());
175 while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
176 const return_address = *@intToPtr(&const usize, fp + @sizeOf(usize));
169 while (fp != 0) : (fp = @intToPtr(&const usize, fp).*) {
170 const return_address = @intToPtr(&const usize, fp + @sizeOf(usize)).*;
177171
178172 switch (addr_state) {
179173 AddressState.NotLookingForStartAddress => {},
......@@ -200,32 +194,32 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
200194 // in practice because the compiler dumps everything in a single
201195 // object file. Future improvement: use external dSYM data when
202196 // available.
203 const unknown = macho.Symbol { .name = "???", .address = address };
197 const unknown = macho.Symbol{
198 .name = "???",
199 .address = address,
200 };
204201 const symbol = debug_info.symbol_table.search(address) ?? &unknown;
205 try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++
206 DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n",
207 symbol.name, address);
202 try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address);
208203 },
209204 else => {
210205 const compile_unit = findCompileUnit(debug_info, address) catch {
211 try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n",
212 address);
206 try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", address);
213207 return;
214208 };
215209 const compile_unit_name = try compile_unit.die.getAttrString(debug_info, DW.AT_name);
216210 if (getLineNumberInfo(debug_info, compile_unit, address - 1)) |line_info| {
217211 defer line_info.deinit();
218 try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++
219 DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n",
220 line_info.file_name, line_info.line, line_info.column,
221 address, compile_unit_name);
212 try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, address, compile_unit_name);
222213 if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) {
223214 if (line_info.column == 0) {
224215 try out_stream.write("\n");
225216 } else {
226 {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) {
227 try out_stream.writeByte(' ');
228 }}
217 {
218 var col_i: usize = 1;
219 while (col_i < line_info.column) : (col_i += 1) {
220 try out_stream.writeByte(' ');
221 }
222 }
229223 try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
230224 }
231225 } else |err| switch (err) {
......@@ -233,7 +227,8 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
233227 else => return err,
234228 }
235229 } else |err| switch (err) {
236 error.MissingDebugInfo, error.InvalidDebugInfo => {
230 error.MissingDebugInfo,
231 error.InvalidDebugInfo => {
237232 try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name);
238233 },
239234 else => return err,
......@@ -247,7 +242,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
247242 builtin.ObjectFormat.elf => {
248243 const st = try allocator.create(ElfStackTrace);
249244 errdefer allocator.destroy(st);
250 *st = ElfStackTrace {
245 st.* = ElfStackTrace{
251246 .self_exe_file = undefined,
252247 .elf = undefined,
253248 .debug_info = undefined,
......@@ -279,9 +274,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
279274 const st = try allocator.create(ElfStackTrace);
280275 errdefer allocator.destroy(st);
281276
282 *st = ElfStackTrace {
283 .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)),
284 };
277 st.* = ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) };
285278
286279 return st;
287280 },
......@@ -325,8 +318,7 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: var, line_info: &con
325318 }
326319 }
327320
328 if (amt_read < buf.len)
329 return error.EndOfFile;
321 if (amt_read < buf.len) return error.EndOfFile;
330322 }
331323}
332324
......@@ -418,10 +410,8 @@ const Constant = struct {
418410 signed: bool,
419411
420412 fn asUnsignedLe(self: &const Constant) !u64 {
421 if (self.payload.len > @sizeOf(u64))
422 return error.InvalidDebugInfo;
423 if (self.signed)
424 return error.InvalidDebugInfo;
413 if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
414 if (self.signed) return error.InvalidDebugInfo;
425415 return mem.readInt(self.payload, u64, builtin.Endian.Little);
426416 }
427417};
......@@ -438,15 +428,14 @@ const Die = struct {
438428
439429 fn getAttr(self: &const Die, id: u64) ?&const FormValue {
440430 for (self.attrs.toSliceConst()) |*attr| {
441 if (attr.id == id)
442 return &attr.value;
431 if (attr.id == id) return &attr.value;
443432 }
444433 return null;
445434 }
446435
447436 fn getAttrAddr(self: &const Die, id: u64) !u64 {
448437 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
449 return switch (*form_value) {
438 return switch (form_value.*) {
450439 FormValue.Address => |value| value,
451440 else => error.InvalidDebugInfo,
452441 };
......@@ -454,7 +443,7 @@ const Die = struct {
454443
455444 fn getAttrSecOffset(self: &const Die, id: u64) !u64 {
456445 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
457 return switch (*form_value) {
446 return switch (form_value.*) {
458447 FormValue.Const => |value| value.asUnsignedLe(),
459448 FormValue.SecOffset => |value| value,
460449 else => error.InvalidDebugInfo,
......@@ -463,7 +452,7 @@ const Die = struct {
463452
464453 fn getAttrUnsignedLe(self: &const Die, id: u64) !u64 {
465454 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
466 return switch (*form_value) {
455 return switch (form_value.*) {
467456 FormValue.Const => |value| value.asUnsignedLe(),
468457 else => error.InvalidDebugInfo,
469458 };
......@@ -471,7 +460,7 @@ const Die = struct {
471460
472461 fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) ![]u8 {
473462 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
474 return switch (*form_value) {
463 return switch (form_value.*) {
475464 FormValue.String => |value| value,
476465 FormValue.StrPtr => |offset| getString(st, offset),
477466 else => error.InvalidDebugInfo,
......@@ -518,10 +507,8 @@ const LineNumberProgram = struct {
518507 prev_basic_block: bool,
519508 prev_end_sequence: bool,
520509
521 pub fn init(is_stmt: bool, include_dirs: []const []const u8,
522 file_entries: &ArrayList(FileEntry), target_address: usize) LineNumberProgram
523 {
524 return LineNumberProgram {
510 pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: &ArrayList(FileEntry), target_address: usize) LineNumberProgram {
511 return LineNumberProgram{
525512 .address = 0,
526513 .file = 1,
527514 .line = 1,
......@@ -548,14 +535,16 @@ const LineNumberProgram = struct {
548535 return error.MissingDebugInfo;
549536 } else if (self.prev_file - 1 >= self.file_entries.len) {
550537 return error.InvalidDebugInfo;
551 } else &self.file_entries.items[self.prev_file - 1];
538 } else
539 &self.file_entries.items[self.prev_file - 1];
552540
553541 const dir_name = if (file_entry.dir_index >= self.include_dirs.len) {
554542 return error.InvalidDebugInfo;
555 } else self.include_dirs[file_entry.dir_index];
543 } else
544 self.include_dirs[file_entry.dir_index];
556545 const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
557546 errdefer self.file_entries.allocator.free(file_name);
558 return LineInfo {
547 return LineInfo{
559548 .line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
560549 .column = self.prev_column,
561550 .file_name = file_name,
......@@ -578,8 +567,7 @@ fn readStringRaw(allocator: &mem.Allocator, in_stream: var) ![]u8 {
578567 var buf = ArrayList(u8).init(allocator);
579568 while (true) {
580569 const byte = try in_stream.readByte();
581 if (byte == 0)
582 break;
570 if (byte == 0) break;
583571 try buf.append(byte);
584572 }
585573 return buf.toSlice();
......@@ -600,7 +588,7 @@ fn readAllocBytes(allocator: &mem.Allocator, in_stream: var, size: usize) ![]u8
600588
601589fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
602590 const buf = try readAllocBytes(allocator, in_stream, size);
603 return FormValue { .Block = buf };
591 return FormValue{ .Block = buf };
604592}
605593
606594fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
......@@ -609,26 +597,23 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !
609597}
610598
611599fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {
612 return FormValue { .Const = Constant {
600 return FormValue{ .Const = Constant{
613601 .signed = signed,
614602 .payload = try readAllocBytes(allocator, in_stream, size),
615 }};
603 } };
616604}
617605
618606fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 {
619 return if (is_64) try in_stream.readIntLe(u64)
620 else u64(try in_stream.readIntLe(u32)) ;
607 return if (is_64) try in_stream.readIntLe(u64) else u64(try in_stream.readIntLe(u32));
621608}
622609
623610fn parseFormValueTargetAddrSize(in_stream: var) !u64 {
624 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32))
625 else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64)
626 else unreachable;
611 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable;
627612}
628613
629614fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
630615 const buf = try readAllocBytes(allocator, in_stream, size);
631 return FormValue { .Ref = buf };
616 return FormValue{ .Ref = buf };
632617}
633618
634619fn parseFormValueRef(allocator: &mem.Allocator, in_stream: var, comptime T: type) !FormValue {
......@@ -646,11 +631,9 @@ const ParseFormValueError = error {
646631 OutOfMemory,
647632};
648633
649fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64: bool)
650 ParseFormValueError!FormValue
651{
634fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64: bool) ParseFormValueError!FormValue {
652635 return switch (form_id) {
653 DW.FORM_addr => FormValue { .Address = try parseFormValueTargetAddrSize(in_stream) },
636 DW.FORM_addr => FormValue{ .Address = try parseFormValueTargetAddrSize(in_stream) },
654637 DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1),
655638 DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2),
656639 DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4),
......@@ -662,7 +645,8 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
662645 DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2),
663646 DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4),
664647 DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8),
665 DW.FORM_udata, DW.FORM_sdata => {
648 DW.FORM_udata,
649 DW.FORM_sdata => {
666650 const block_len = try readULeb128(in_stream);
667651 const signed = form_id == DW.FORM_sdata;
668652 return parseFormValueConstant(allocator, in_stream, signed, block_len);
......@@ -670,11 +654,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
670654 DW.FORM_exprloc => {
671655 const size = try readULeb128(in_stream);
672656 const buf = try readAllocBytes(allocator, in_stream, size);
673 return FormValue { .ExprLoc = buf };
657 return FormValue{ .ExprLoc = buf };
674658 },
675 DW.FORM_flag => FormValue { .Flag = (try in_stream.readByte()) != 0 },
676 DW.FORM_flag_present => FormValue { .Flag = true },
677 DW.FORM_sec_offset => FormValue { .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
659 DW.FORM_flag => FormValue{ .Flag = (try in_stream.readByte()) != 0 },
660 DW.FORM_flag_present => FormValue{ .Flag = true },
661 DW.FORM_sec_offset => FormValue{ .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
678662
679663 DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8),
680664 DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16),
......@@ -685,11 +669,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
685669 return parseFormValueRefLen(allocator, in_stream, ref_len);
686670 },
687671
688 DW.FORM_ref_addr => FormValue { .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
689 DW.FORM_ref_sig8 => FormValue { .RefSig8 = try in_stream.readIntLe(u64) },
672 DW.FORM_ref_addr => FormValue{ .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
673 DW.FORM_ref_sig8 => FormValue{ .RefSig8 = try in_stream.readIntLe(u64) },
690674
691 DW.FORM_string => FormValue { .String = try readStringRaw(allocator, in_stream) },
692 DW.FORM_strp => FormValue { .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
675 DW.FORM_string => FormValue{ .String = try readStringRaw(allocator, in_stream) },
676 DW.FORM_strp => FormValue{ .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
693677 DW.FORM_indirect => {
694678 const child_form_id = try readULeb128(in_stream);
695679 return parseFormValue(allocator, in_stream, child_form_id, is_64);
......@@ -705,9 +689,8 @@ fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
705689 var result = AbbrevTable.init(st.allocator());
706690 while (true) {
707691 const abbrev_code = try readULeb128(in_stream);
708 if (abbrev_code == 0)
709 return result;
710 try result.append(AbbrevTableEntry {
692 if (abbrev_code == 0) return result;
693 try result.append(AbbrevTableEntry{
711694 .abbrev_code = abbrev_code,
712695 .tag_id = try readULeb128(in_stream),
713696 .has_children = (try in_stream.readByte()) == DW.CHILDREN_yes,
......@@ -718,9 +701,8 @@ fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
718701 while (true) {
719702 const attr_id = try readULeb128(in_stream);
720703 const form_id = try readULeb128(in_stream);
721 if (attr_id == 0 and form_id == 0)
722 break;
723 try attrs.append(AbbrevAttr {
704 if (attr_id == 0 and form_id == 0) break;
705 try attrs.append(AbbrevAttr{
724706 .attr_id = attr_id,
725707 .form_id = form_id,
726708 });
......@@ -737,7 +719,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
737719 }
738720 }
739721 try st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset);
740 try st.abbrev_table_list.append(AbbrevTableHeader {
722 try st.abbrev_table_list.append(AbbrevTableHeader{
741723 .offset = abbrev_offset,
742724 .table = try parseAbbrevTable(st),
743725 });
......@@ -746,8 +728,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
746728
747729fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) ?&const AbbrevTableEntry {
748730 for (abbrev_table.toSliceConst()) |*table_entry| {
749 if (table_entry.abbrev_code == abbrev_code)
750 return table_entry;
731 if (table_entry.abbrev_code == abbrev_code) return table_entry;
751732 }
752733 return null;
753734}
......@@ -759,14 +740,14 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) !
759740 const abbrev_code = try readULeb128(in_stream);
760741 const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo;
761742
762 var result = Die {
743 var result = Die{
763744 .tag_id = table_entry.tag_id,
764745 .has_children = table_entry.has_children,
765746 .attrs = ArrayList(Die.Attr).init(st.allocator()),
766747 };
767748 try result.attrs.resize(table_entry.attrs.len);
768749 for (table_entry.attrs.toSliceConst()) |attr, i| {
769 result.attrs.items[i] = Die.Attr {
750 result.attrs.items[i] = Die.Attr{
770751 .id = attr.attr_id,
771752 .value = try parseFormValue(st.allocator(), in_stream, attr.form_id, is_64),
772753 };
......@@ -790,8 +771,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
790771
791772 var is_64: bool = undefined;
792773 const unit_length = try readInitialLength(@typeOf(in_stream.readFn).ReturnType.ErrorSet, in_stream, &is_64);
793 if (unit_length == 0)
794 return error.MissingDebugInfo;
774 if (unit_length == 0) return error.MissingDebugInfo;
795775 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
796776
797777 if (compile_unit.index != this_index) {
......@@ -803,8 +783,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
803783 // TODO support 3 and 5
804784 if (version != 2 and version != 4) return error.InvalidDebugInfo;
805785
806 const prologue_length = if (is_64) try in_stream.readInt(st.elf.endian, u64)
807 else try in_stream.readInt(st.elf.endian, u32);
786 const prologue_length = if (is_64) try in_stream.readInt(st.elf.endian, u64) else try in_stream.readInt(st.elf.endian, u32);
808787 const prog_start_offset = (try in_file.getPos()) + prologue_length;
809788
810789 const minimum_instruction_length = try in_stream.readByte();
......@@ -819,38 +798,37 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
819798 const line_base = try in_stream.readByteSigned();
820799
821800 const line_range = try in_stream.readByte();
822 if (line_range == 0)
823 return error.InvalidDebugInfo;
801 if (line_range == 0) return error.InvalidDebugInfo;
824802
825803 const opcode_base = try in_stream.readByte();
826804
827805 const standard_opcode_lengths = try st.allocator().alloc(u8, opcode_base - 1);
828806
829 {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) {
830 standard_opcode_lengths[i] = try in_stream.readByte();
831 }}
807 {
808 var i: usize = 0;
809 while (i < opcode_base - 1) : (i += 1) {
810 standard_opcode_lengths[i] = try in_stream.readByte();
811 }
812 }
832813
833814 var include_directories = ArrayList([]u8).init(st.allocator());
834815 try include_directories.append(compile_unit_cwd);
835816 while (true) {
836817 const dir = try st.readString();
837 if (dir.len == 0)
838 break;
818 if (dir.len == 0) break;
839819 try include_directories.append(dir);
840820 }
841821
842822 var file_entries = ArrayList(FileEntry).init(st.allocator());
843 var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(),
844 &file_entries, target_address);
823 var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address);
845824
846825 while (true) {
847826 const file_name = try st.readString();
848 if (file_name.len == 0)
849 break;
827 if (file_name.len == 0) break;
850828 const dir_index = try readULeb128(in_stream);
851829 const mtime = try readULeb128(in_stream);
852830 const len_bytes = try readULeb128(in_stream);
853 try file_entries.append(FileEntry {
831 try file_entries.append(FileEntry{
854832 .file_name = file_name,
855833 .dir_index = dir_index,
856834 .mtime = mtime,
......@@ -866,8 +844,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
866844 var sub_op: u8 = undefined; // TODO move this to the correct scope and fix the compiler crash
867845 if (opcode == DW.LNS_extended_op) {
868846 const op_size = try readULeb128(in_stream);
869 if (op_size < 1)
870 return error.InvalidDebugInfo;
847 if (op_size < 1) return error.InvalidDebugInfo;
871848 sub_op = try in_stream.readByte();
872849 switch (sub_op) {
873850 DW.LNE_end_sequence => {
......@@ -884,7 +861,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
884861 const dir_index = try readULeb128(in_stream);
885862 const mtime = try readULeb128(in_stream);
886863 const len_bytes = try readULeb128(in_stream);
887 try file_entries.append(FileEntry {
864 try file_entries.append(FileEntry{
888865 .file_name = file_name,
889866 .dir_index = dir_index,
890867 .mtime = mtime,
......@@ -941,11 +918,9 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
941918 const arg = try in_stream.readInt(st.elf.endian, u16);
942919 prog.address += arg;
943920 },
944 DW.LNS_set_prologue_end => {
945 },
921 DW.LNS_set_prologue_end => {},
946922 else => {
947 if (opcode - 1 >= standard_opcode_lengths.len)
948 return error.InvalidDebugInfo;
923 if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo;
949924 const len_bytes = standard_opcode_lengths[opcode - 1];
950925 try in_file.seekForward(len_bytes);
951926 },
......@@ -972,16 +947,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
972947
973948 var is_64: bool = undefined;
974949 const unit_length = try readInitialLength(@typeOf(in_stream.readFn).ReturnType.ErrorSet, in_stream, &is_64);
975 if (unit_length == 0)
976 return;
950 if (unit_length == 0) return;
977951 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
978952
979953 const version = try in_stream.readInt(st.elf.endian, u16);
980954 if (version < 2 or version > 5) return error.InvalidDebugInfo;
981955
982 const debug_abbrev_offset =
983 if (is_64) try in_stream.readInt(st.elf.endian, u64)
984 else try in_stream.readInt(st.elf.endian, u32);
956 const debug_abbrev_offset = if (is_64) try in_stream.readInt(st.elf.endian, u64) else try in_stream.readInt(st.elf.endian, u32);
985957
986958 const address_size = try in_stream.readByte();
987959 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
......@@ -992,15 +964,14 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
992964 try st.self_exe_file.seekTo(compile_unit_pos);
993965
994966 const compile_unit_die = try st.allocator().create(Die);
995 *compile_unit_die = try parseDie(st, abbrev_table, is_64);
967 compile_unit_die.* = try parseDie(st, abbrev_table, is_64);
996968
997 if (compile_unit_die.tag_id != DW.TAG_compile_unit)
998 return error.InvalidDebugInfo;
969 if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;
999970
1000971 const pc_range = x: {
1001972 if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
1002973 if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
1003 const pc_end = switch (*high_pc_value) {
974 const pc_end = switch (high_pc_value.*) {
1004975 FormValue.Address => |value| value,
1005976 FormValue.Const => |value| b: {
1006977 const offset = try value.asUnsignedLe();
......@@ -1008,7 +979,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
1008979 },
1009980 else => return error.InvalidDebugInfo,
1010981 };
1011 break :x PcRange {
982 break :x PcRange{
1012983 .start = low_pc,
1013984 .end = pc_end,
1014985 };
......@@ -1016,13 +987,12 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
1016987 break :x null;
1017988 }
1018989 } else |err| {
1019 if (err != error.MissingDebugInfo)
1020 return err;
990 if (err != error.MissingDebugInfo) return err;
1021991 break :x null;
1022992 }
1023993 };
1024994
1025 try st.compile_unit_list.append(CompileUnit {
995 try st.compile_unit_list.append(CompileUnit{
1026996 .version = version,
1027997 .is_64 = is_64,
1028998 .pc_range = pc_range,
......@@ -1040,8 +1010,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10401010 const in_stream = &in_file_stream.stream;
10411011 for (st.compile_unit_list.toSlice()) |*compile_unit| {
10421012 if (compile_unit.pc_range) |range| {
1043 if (target_address >= range.start and target_address < range.end)
1044 return compile_unit;
1013 if (target_address >= range.start and target_address < range.end) return compile_unit;
10451014 }
10461015 if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| {
10471016 var base_address: usize = 0;
......@@ -1063,8 +1032,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10631032 }
10641033 }
10651034 } else |err| {
1066 if (err != error.MissingDebugInfo)
1067 return err;
1035 if (err != error.MissingDebugInfo) return err;
10681036 continue;
10691037 }
10701038 }
......@@ -1073,8 +1041,8 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10731041
10741042fn readInitialLength(comptime E: type, in_stream: &io.InStream(E), is_64: &bool) !u64 {
10751043 const first_32_bits = try in_stream.readIntLe(u32);
1076 *is_64 = (first_32_bits == 0xffffffff);
1077 if (*is_64) {
1044 is_64.* = (first_32_bits == 0xffffffff);
1045 if (is_64.*) {
10781046 return in_stream.readIntLe(u64);
10791047 } else {
10801048 if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo;
......@@ -1091,13 +1059,11 @@ fn readULeb128(in_stream: var) !u64 {
10911059
10921060 var operand: u64 = undefined;
10931061
1094 if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand))
1095 return error.InvalidDebugInfo;
1062 if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
10961063
10971064 result |= operand;
10981065
1099 if ((byte & 0b10000000) == 0)
1100 return result;
1066 if ((byte & 0b10000000) == 0) return result;
11011067
11021068 shift += 7;
11031069 }
......@@ -1112,15 +1078,13 @@ fn readILeb128(in_stream: var) !i64 {
11121078
11131079 var operand: i64 = undefined;
11141080
1115 if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand))
1116 return error.InvalidDebugInfo;
1081 if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
11171082
11181083 result |= operand;
11191084 shift += 7;
11201085
11211086 if ((byte & 0b10000000) == 0) {
1122 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0)
1123 result |= -(i64(1) << u6(shift));
1087 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << u6(shift));
11241088 return result;
11251089 }
11261090 }
......@@ -1131,7 +1095,6 @@ pub const global_allocator = &global_fixed_allocator.allocator;
11311095var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator_mem[0..]);
11321096var global_allocator_mem: [100 * 1024]u8 = undefined;
11331097
1134
11351098// TODO make thread safe
11361099var debug_info_allocator: ?&mem.Allocator = null;
11371100var debug_info_direct_allocator: std.heap.DirectAllocator = undefined;
std/event.zig+20-33
......@@ -6,7 +6,7 @@ const mem = std.mem;
66const posix = std.os.posix;
77
88pub const TcpServer = struct {
9 handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File) void,
9 handleRequestFn: async<&mem.Allocator> fn(&TcpServer, &const std.net.Address, &const std.os.File) void,
1010
1111 loop: &Loop,
1212 sockfd: i32,
......@@ -18,13 +18,11 @@ pub const TcpServer = struct {
1818 const PromiseNode = std.LinkedList(promise).Node;
1919
2020 pub fn init(loop: &Loop) !TcpServer {
21 const sockfd = try std.os.posixSocket(posix.AF_INET,
22 posix.SOCK_STREAM|posix.SOCK_CLOEXEC|posix.SOCK_NONBLOCK,
23 posix.PROTO_tcp);
21 const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
2422 errdefer std.os.close(sockfd);
2523
2624 // TODO can't initialize handler coroutine here because we need well defined copy elision
27 return TcpServer {
25 return TcpServer{
2826 .loop = loop,
2927 .sockfd = sockfd,
3028 .accept_coro = null,
......@@ -34,9 +32,7 @@ pub const TcpServer = struct {
3432 };
3533 }
3634
37 pub fn listen(self: &TcpServer, address: &const std.net.Address,
38 handleRequestFn: async<&mem.Allocator> fn (&TcpServer, &const std.net.Address, &const std.os.File)void) !void
39 {
35 pub fn listen(self: &TcpServer, address: &const std.net.Address, handleRequestFn: async<&mem.Allocator> fn(&TcpServer, &const std.net.Address, &const std.os.File) void) !void {
4036 self.handleRequestFn = handleRequestFn;
4137
4238 try std.os.posixBind(self.sockfd, &address.os_addr);
......@@ -48,7 +44,6 @@ pub const TcpServer = struct {
4844
4945 try self.loop.addFd(self.sockfd, ??self.accept_coro);
5046 errdefer self.loop.removeFd(self.sockfd);
51
5247 }
5348
5449 pub fn deinit(self: &TcpServer) void {
......@@ -60,9 +55,7 @@ pub const TcpServer = struct {
6055 pub async fn handler(self: &TcpServer) void {
6156 while (true) {
6257 var accepted_addr: std.net.Address = undefined;
63 if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr,
64 posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd|
65 {
58 if (std.os.posixAccept(self.sockfd, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
6659 var socket = std.os.File.openHandle(accepted_fd);
6760 _ = async<self.loop.allocator> self.handleRequestFn(self, accepted_addr, socket) catch |err| switch (err) {
6861 error.OutOfMemory => {
......@@ -110,7 +103,7 @@ pub const Loop = struct {
110103
111104 fn init(allocator: &mem.Allocator) !Loop {
112105 const epollfd = try std.os.linuxEpollCreate(std.os.linux.EPOLL_CLOEXEC);
113 return Loop {
106 return Loop{
114107 .keep_running = true,
115108 .allocator = allocator,
116109 .epollfd = epollfd,
......@@ -118,11 +111,9 @@ pub const Loop = struct {
118111 }
119112
120113 pub fn addFd(self: &Loop, fd: i32, prom: promise) !void {
121 var ev = std.os.linux.epoll_event {
122 .events = std.os.linux.EPOLLIN|std.os.linux.EPOLLOUT|std.os.linux.EPOLLET,
123 .data = std.os.linux.epoll_data {
124 .ptr = @ptrToInt(prom),
125 },
114 var ev = std.os.linux.epoll_event{
115 .events = std.os.linux.EPOLLIN | std.os.linux.EPOLLOUT | std.os.linux.EPOLLET,
116 .data = std.os.linux.epoll_data{ .ptr = @ptrToInt(prom) },
126117 };
127118 try std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_ADD, fd, &ev);
128119 }
......@@ -157,9 +148,9 @@ pub const Loop = struct {
157148};
158149
159150pub async fn connect(loop: &Loop, _address: &const std.net.Address) !std.os.File {
160 var address = *_address; // TODO https://github.com/zig-lang/zig/issues/733
151 var address = _address.*; // TODO https://github.com/zig-lang/zig/issues/733
161152
162 const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM|posix.SOCK_CLOEXEC|posix.SOCK_NONBLOCK, posix.PROTO_tcp);
153 const sockfd = try std.os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
163154 errdefer std.os.close(sockfd);
164155
165156 try std.os.posixConnectAsync(sockfd, &address.os_addr);
......@@ -179,11 +170,9 @@ test "listen on a port, send bytes, receive bytes" {
179170
180171 const Self = this;
181172
182 async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address,
183 _socket: &const std.os.File) void
184 {
173 async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {
185174 const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
186 var socket = *_socket; // TODO https://github.com/zig-lang/zig/issues/733
175 var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
187176 defer socket.close();
188177 const next_handler = async errorableHandler(self, _addr, socket) catch |err| switch (err) {
189178 error.OutOfMemory => @panic("unable to handle connection: out of memory"),
......@@ -191,14 +180,14 @@ test "listen on a port, send bytes, receive bytes" {
191180 (await next_handler) catch |err| {
192181 std.debug.panic("unable to handle connection: {}\n", err);
193182 };
194 suspend |p| { cancel p; }
183 suspend |p| {
184 cancel p;
185 }
195186 }
196187
197 async fn errorableHandler(self: &Self, _addr: &const std.net.Address,
198 _socket: &const std.os.File) !void
199 {
200 const addr = *_addr; // TODO https://github.com/zig-lang/zig/issues/733
201 var socket = *_socket; // TODO https://github.com/zig-lang/zig/issues/733
188 async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {
189 const addr = _addr.*; // TODO https://github.com/zig-lang/zig/issues/733
190 var socket = _socket.*; // TODO https://github.com/zig-lang/zig/issues/733
202191
203192 var adapter = std.io.FileOutStream.init(&socket);
204193 var stream = &adapter.stream;
......@@ -210,9 +199,7 @@ test "listen on a port, send bytes, receive bytes" {
210199 const addr = std.net.Address.initIp4(ip4addr, 0);
211200
212201 var loop = try Loop.init(std.debug.global_allocator);
213 var server = MyServer {
214 .tcp_server = try TcpServer.init(&loop),
215 };
202 var server = MyServer{ .tcp_server = try TcpServer.init(&loop) };
216203 defer server.tcp_server.deinit();
217204 try server.tcp_server.listen(addr, MyServer.handler);
218205
std/fmt/errol/index.zig+22-32
......@@ -86,7 +86,7 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {
8686 const data = enum3_data[i];
8787 const digits = buffer[1..data.str.len + 1];
8888 mem.copy(u8, digits, data.str);
89 return FloatDecimal {
89 return FloatDecimal{
9090 .digits = digits,
9191 .exp = data.exp,
9292 };
......@@ -105,7 +105,6 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
105105 return errolFixed(val, buffer);
106106 }
107107
108
109108 // normalize the midpoint
110109
111110 const e = math.frexp(val).exponent;
......@@ -137,11 +136,11 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
137136 }
138137
139138 // compute boundaries
140 var high = HP {
139 var high = HP{
141140 .val = mid.val,
142141 .off = mid.off + (fpnext(val) - val) * lten * ten / 2.0,
143142 };
144 var low = HP {
143 var low = HP{
145144 .val = mid.val,
146145 .off = mid.off + (fpprev(val) - val) * lten * ten / 2.0,
147146 };
......@@ -171,15 +170,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
171170 var buf_index: usize = 1;
172171 while (true) {
173172 var hdig = u8(math.floor(high.val));
174 if ((high.val == f64(hdig)) and (high.off < 0))
175 hdig -= 1;
173 if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
176174
177175 var ldig = u8(math.floor(low.val));
178 if ((low.val == f64(ldig)) and (low.off < 0))
179 ldig -= 1;
176 if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
180177
181 if (ldig != hdig)
182 break;
178 if (ldig != hdig) break;
183179
184180 buffer[buf_index] = hdig + '0';
185181 buf_index += 1;
......@@ -191,13 +187,12 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
191187
192188 const tmp = (high.val + low.val) / 2.0;
193189 var mdig = u8(math.floor(tmp + 0.5));
194 if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0)
195 mdig -= 1;
190 if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
196191
197192 buffer[buf_index] = mdig + '0';
198193 buf_index += 1;
199194
200 return FloatDecimal {
195 return FloatDecimal{
201196 .digits = buffer[1..buf_index],
202197 .exp = exp,
203198 };
......@@ -235,7 +230,7 @@ fn hpProd(in: &const HP, val: f64) HP {
235230 const p = in.val * val;
236231 const e = ((hi * hi2 - p) + lo * hi2 + hi * lo2) + lo * lo2;
237232
238 return HP {
233 return HP{
239234 .val = p,
240235 .off = in.off * val + e,
241236 };
......@@ -246,8 +241,8 @@ fn hpProd(in: &const HP, val: f64) HP {
246241/// @hi: The high bits.
247242/// @lo: The low bits.
248243fn split(val: f64, hi: &f64, lo: &f64) void {
249 *hi = gethi(val);
250 *lo = val - *hi;
244 hi.* = gethi(val);
245 lo.* = val - hi.*;
251246}
252247
253248fn gethi(in: f64) f64 {
......@@ -301,7 +296,6 @@ fn hpMul10(hp: &HP) void {
301296 hpNormalize(hp);
302297}
303298
304
305299/// Integer conversion algorithm, guaranteed correct, optimal, and best.
306300/// @val: The val.
307301/// @buf: The output buffer.
......@@ -343,8 +337,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
343337 }
344338 const m64 = @truncate(u64, @divTrunc(mid, x));
345339
346 if (lf != hf)
347 mi += 19;
340 if (lf != hf) mi += 19;
348341
349342 var buf_index = u64toa(m64, buffer) - 1;
350343
......@@ -354,7 +347,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
354347 buf_index += 1;
355348 }
356349
357 return FloatDecimal {
350 return FloatDecimal{
358351 .digits = buffer[0..buf_index],
359352 .exp = i32(buf_index) + mi,
360353 };
......@@ -396,25 +389,24 @@ fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
396389 buffer[j] = u8(mdig + '0');
397390 j += 1;
398391
399 if(hdig != ldig or j > 50)
400 break;
392 if (hdig != ldig or j > 50) break;
401393 }
402394
403395 if (mid > 0.5) {
404 buffer[j-1] += 1;
405 } else if ((mid == 0.5) and (buffer[j-1] & 0x1) != 0) {
406 buffer[j-1] += 1;
396 buffer[j - 1] += 1;
397 } else if ((mid == 0.5) and (buffer[j - 1] & 0x1) != 0) {
398 buffer[j - 1] += 1;
407399 }
408400 } else {
409 while (buffer[j-1] == '0') {
410 buffer[j-1] = 0;
401 while (buffer[j - 1] == '0') {
402 buffer[j - 1] = 0;
411403 j -= 1;
412404 }
413405 }
414406
415407 buffer[j] = 0;
416408
417 return FloatDecimal {
409 return FloatDecimal{
418410 .digits = buffer[0..j],
419411 .exp = exp,
420412 };
......@@ -587,7 +579,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
587579 buffer[buf_index] = c_digits_lut[d8 + 1];
588580 buf_index += 1;
589581 } else {
590 const a = u32(value / kTen16); // 1 to 1844
582 const a = u32(value / kTen16); // 1 to 1844
591583 value %= kTen16;
592584
593585 if (a < 10) {
......@@ -686,7 +678,6 @@ fn fpeint(from: f64) u128 {
686678 return u128(1) << @truncate(u7, (bits >> 52) -% 1023);
687679}
688680
689
690681/// Given two different integers with the same length in terms of the number
691682/// of decimal digits, index the digits from the right-most position starting
692683/// from zero, find the first index where the digits in the two integers
......@@ -713,7 +704,6 @@ fn mismatch10(a: u64, b: u64) i32 {
713704 a_copy /= 10;
714705 b_copy /= 10;
715706
716 if (a_copy == b_copy)
717 return i;
707 if (a_copy == b_copy) return i;
718708 }
719709}
std/fmt/index.zig+37-51
......@@ -11,9 +11,7 @@ const max_int_digits = 65;
1111/// Renders fmt string with args, calling output with slices of bytes.
1212/// If `output` returns an error, the error is returned from `format` and
1313/// `output` is not called again.
14pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void,
15 comptime fmt: []const u8, args: ...) Errors!void
16{
14pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void, comptime fmt: []const u8, args: ...) Errors!void {
1715 const State = enum {
1816 Start,
1917 OpenBrace,
......@@ -270,7 +268,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
270268 }
271269}
272270
273pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
271pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
274272 const T = @typeOf(value);
275273 switch (@typeId(T)) {
276274 builtin.TypeId.Int => {
......@@ -305,7 +303,7 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@
305303 },
306304 builtin.TypeId.Pointer => {
307305 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
308 return output(context, (*value)[0..]);
306 return output(context, (value.*)[0..]);
309307 } else {
310308 return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
311309 }
......@@ -319,13 +317,11 @@ pub fn formatValue(value: var, context: var, comptime Errors: type, output: fn(@
319317 }
320318}
321319
322pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
320pub fn formatAsciiChar(c: u8, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
323321 return output(context, (&c)[0..1]);
324322}
325323
326pub fn formatBuf(buf: []const u8, width: usize,
327 context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
328{
324pub fn formatBuf(buf: []const u8, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
329325 try output(context, buf);
330326
331327 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
......@@ -338,7 +334,7 @@ pub fn formatBuf(buf: []const u8, width: usize,
338334// Print a float in scientific notation to the specified precision. Null uses full precision.
339335// It should be the case that every full precision, printed value can be re-parsed back to the
340336// same type unambiguously.
341pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
337pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
342338 var x = f64(value);
343339
344340 // Errol doesn't handle these special cases.
......@@ -387,7 +383,7 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
387383 var printed: usize = 0;
388384 if (float_decimal.digits.len > 1) {
389385 const num_digits = math.min(float_decimal.digits.len, precision + 1);
390 try output(context, float_decimal.digits[1 .. num_digits]);
386 try output(context, float_decimal.digits[1..num_digits]);
391387 printed += num_digits - 1;
392388 }
393389
......@@ -399,12 +395,9 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
399395 try output(context, float_decimal.digits[0..1]);
400396 try output(context, ".");
401397 if (float_decimal.digits.len > 1) {
402 const num_digits = if (@typeOf(value) == f32)
403 math.min(usize(9), float_decimal.digits.len)
404 else
405 float_decimal.digits.len;
398 const num_digits = if (@typeOf(value) == f32) math.min(usize(9), float_decimal.digits.len) else float_decimal.digits.len;
406399
407 try output(context, float_decimal.digits[1 .. num_digits]);
400 try output(context, float_decimal.digits[1..num_digits]);
408401 } else {
409402 try output(context, "0");
410403 }
......@@ -430,7 +423,7 @@ pub fn formatFloatScientific(value: var, maybe_precision: ?usize, context: var,
430423
431424// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
432425// By default floats are printed at full precision (no rounding).
433pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void {
426pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
434427 var x = f64(value);
435428
436429 // Errol doesn't handle these special cases.
......@@ -480,14 +473,14 @@ pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, com
480473
481474 if (num_digits_whole > 0) {
482475 // We may have to zero pad, for instance 1e4 requires zero padding.
483 try output(context, float_decimal.digits[0 .. num_digits_whole_no_pad]);
476 try output(context, float_decimal.digits[0..num_digits_whole_no_pad]);
484477
485478 var i = num_digits_whole_no_pad;
486479 while (i < num_digits_whole) : (i += 1) {
487480 try output(context, "0");
488481 }
489482 } else {
490 try output(context , "0");
483 try output(context, "0");
491484 }
492485
493486 // {.0} special case doesn't want a trailing '.'
......@@ -519,10 +512,10 @@ pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, com
519512 // Remaining fractional portion, zero-padding if insufficient.
520513 debug.assert(precision >= printed);
521514 if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) {
522 try output(context, float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
515 try output(context, float_decimal.digits[num_digits_whole_no_pad..num_digits_whole_no_pad + precision - printed]);
523516 return;
524517 } else {
525 try output(context, float_decimal.digits[num_digits_whole_no_pad ..]);
518 try output(context, float_decimal.digits[num_digits_whole_no_pad..]);
526519 printed += float_decimal.digits.len - num_digits_whole_no_pad;
527520
528521 while (printed < precision) : (printed += 1) {
......@@ -538,14 +531,14 @@ pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, com
538531
539532 if (num_digits_whole > 0) {
540533 // We may have to zero pad, for instance 1e4 requires zero padding.
541 try output(context, float_decimal.digits[0 .. num_digits_whole_no_pad]);
534 try output(context, float_decimal.digits[0..num_digits_whole_no_pad]);
542535
543536 var i = num_digits_whole_no_pad;
544537 while (i < num_digits_whole) : (i += 1) {
545538 try output(context, "0");
546539 }
547540 } else {
548 try output(context , "0");
541 try output(context, "0");
549542 }
550543
551544 // Omit `.` if no fractional portion
......@@ -565,7 +558,7 @@ pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, com
565558 }
566559 }
567560
568 try output(context, float_decimal.digits[num_digits_whole_no_pad ..]);
561 try output(context, float_decimal.digits[num_digits_whole_no_pad..]);
569562 }
570563}
571564
......@@ -609,9 +602,7 @@ pub fn formatInt(value: var, base: u8, uppercase: bool, width: usize,
609602 }
610603}
611604
612fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize,
613 context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
614{
605fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
615606 const uint = @IntType(false, @typeOf(value).bit_count);
616607 if (value < 0) {
617608 const minus_sign: u8 = '-';
......@@ -630,9 +621,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize,
630621 }
631622}
632623
633fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
634 context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
635{
624fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void) Errors!void {
636625 // max_int_digits accounts for the minus sign. when printing an unsigned
637626 // number we don't need to do that.
638627 var buf: [max_int_digits - 1]u8 = undefined;
......@@ -644,8 +633,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
644633 index -= 1;
645634 buf[index] = digitToChar(u8(digit), uppercase);
646635 a /= base;
647 if (a == 0)
648 break;
636 if (a == 0) break;
649637 }
650638
651639 const digits_buf = buf[index..];
......@@ -657,8 +645,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
657645 while (true) {
658646 try output(context, (&zero_byte)[0..1]);
659647 leftover_padding -= 1;
660 if (leftover_padding == 0)
661 break;
648 if (leftover_padding == 0) break;
662649 }
663650 mem.set(u8, buf[0..index], '0');
664651 return output(context, buf);
......@@ -670,7 +657,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize,
670657}
671658
672659pub fn formatIntBuf(out_buf: []u8, value: var, base: u8, uppercase: bool, width: usize) usize {
673 var context = FormatIntBuf {
660 var context = FormatIntBuf{
674661 .out_buf = out_buf,
675662 .index = 0,
676663 };
......@@ -687,10 +674,8 @@ fn formatIntCallback(context: &FormatIntBuf, bytes: []const u8) (error{}!void) {
687674}
688675
689676pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T {
690 if (!T.is_signed)
691 return parseUnsigned(T, buf, radix);
692 if (buf.len == 0)
693 return T(0);
677 if (!T.is_signed) return parseUnsigned(T, buf, radix);
678 if (buf.len == 0) return T(0);
694679 if (buf[0] == '-') {
695680 return math.negate(try parseUnsigned(T, buf[1..], radix));
696681 } else if (buf[0] == '+') {
......@@ -710,9 +695,10 @@ test "fmt.parseInt" {
710695 assert(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
711696}
712697
713const ParseUnsignedError = error {
698const ParseUnsignedError = error{
714699 /// The result cannot fit in the type specified
715700 Overflow,
701
716702 /// The input had a byte that was not a digit
717703 InvalidCharacter,
718704};
......@@ -737,8 +723,7 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
737723 else => return error.InvalidCharacter,
738724 };
739725
740 if (value >= radix)
741 return error.InvalidCharacter;
726 if (value >= radix) return error.InvalidCharacter;
742727
743728 return value;
744729}
......@@ -762,20 +747,21 @@ fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) !void {
762747}
763748
764749pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) ![]u8 {
765 var context = BufPrintContext { .remaining = buf, };
750 var context = BufPrintContext{ .remaining = buf };
766751 try format(&context, error{BufferTooSmall}, bufPrintWrite, fmt, args);
767752 return buf[0..buf.len - context.remaining.len];
768753}
769754
770755pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) ![]u8 {
771756 var size: usize = 0;
772 format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
757 format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {
758 };
773759 const buf = try allocator.alloc(u8, size);
774760 return bufPrint(buf, fmt, args);
775761}
776762
777763fn countSize(size: &usize, bytes: []const u8) (error{}!void) {
778 *size += bytes.len;
764 size.* += bytes.len;
779765}
780766
781767test "buf print int" {
......@@ -843,9 +829,7 @@ test "fmt.format" {
843829 unused: u8,
844830 };
845831 var buf1: [32]u8 = undefined;
846 const value = Struct {
847 .unused = 42,
848 };
832 const value = Struct{ .unused = 42 };
849833 const result = try bufPrint(buf1[0..], "pointer: {}\n", &value);
850834 assert(mem.startsWith(u8, result, "pointer: Struct@"));
851835 }
......@@ -1072,7 +1056,7 @@ fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void
10721056
10731057pub fn trim(buf: []const u8) []const u8 {
10741058 var start: usize = 0;
1075 while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) { }
1059 while (start < buf.len and isWhiteSpace(buf[start])) : (start += 1) {}
10761060
10771061 var end: usize = buf.len;
10781062 while (true) {
......@@ -1084,7 +1068,6 @@ pub fn trim(buf: []const u8) []const u8 {
10841068 }
10851069 }
10861070 break;
1087
10881071 }
10891072 return buf[start..end];
10901073}
......@@ -1099,7 +1082,10 @@ test "fmt.trim" {
10991082
11001083pub fn isWhiteSpace(byte: u8) bool {
11011084 return switch (byte) {
1102 ' ', '\t', '\n', '\r' => true,
1085 ' ',
1086 '\t',
1087 '\n',
1088 '\r' => true,
11031089 else => false,
11041090 };
11051091}
std/hash/crc.zig+16-16
......@@ -9,9 +9,9 @@ const std = @import("../index.zig");
99const debug = std.debug;
1010
1111pub const Polynomial = struct {
12 const IEEE = 0xedb88320;
12 const IEEE = 0xedb88320;
1313 const Castagnoli = 0x82f63b78;
14 const Koopman = 0xeb31d82e;
14 const Koopman = 0xeb31d82e;
1515};
1616
1717// IEEE is by far the most common CRC and so is aliased by default.
......@@ -27,20 +27,22 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
2727
2828 for (tables[0]) |*e, i| {
2929 var crc = u32(i);
30 var j: usize = 0; while (j < 8) : (j += 1) {
30 var j: usize = 0;
31 while (j < 8) : (j += 1) {
3132 if (crc & 1 == 1) {
3233 crc = (crc >> 1) ^ poly;
3334 } else {
3435 crc = (crc >> 1);
3536 }
3637 }
37 *e = crc;
38 e.* = crc;
3839 }
3940
4041 var i: usize = 0;
4142 while (i < 256) : (i += 1) {
4243 var crc = tables[0][i];
43 var j: usize = 1; while (j < 8) : (j += 1) {
44 var j: usize = 1;
45 while (j < 8) : (j += 1) {
4446 const index = @truncate(u8, crc);
4547 crc = tables[0][index] ^ (crc >> 8);
4648 tables[j][i] = crc;
......@@ -53,22 +55,21 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
5355 crc: u32,
5456
5557 pub fn init() Self {
56 return Self {
57 .crc = 0xffffffff,
58 };
58 return Self{ .crc = 0xffffffff };
5959 }
6060
6161 pub fn update(self: &Self, input: []const u8) void {
6262 var i: usize = 0;
6363 while (i + 8 <= input.len) : (i += 8) {
64 const p = input[i..i+8];
64 const p = input[i..i + 8];
6565
6666 // Unrolling this way gives ~50Mb/s increase
67 self.crc ^= (u32(p[0]) << 0);
68 self.crc ^= (u32(p[1]) << 8);
67 self.crc ^= (u32(p[0]) << 0);
68 self.crc ^= (u32(p[1]) << 8);
6969 self.crc ^= (u32(p[2]) << 16);
7070 self.crc ^= (u32(p[3]) << 24);
7171
72
7273 self.crc =
7374 lookup_tables[0][p[7]] ^
7475 lookup_tables[1][p[6]] ^
......@@ -123,14 +124,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
123124
124125 for (table) |*e, i| {
125126 var crc = u32(i * 16);
126 var j: usize = 0; while (j < 8) : (j += 1) {
127 var j: usize = 0;
128 while (j < 8) : (j += 1) {
127129 if (crc & 1 == 1) {
128130 crc = (crc >> 1) ^ poly;
129131 } else {
130132 crc = (crc >> 1);
131133 }
132134 }
133 *e = crc;
135 e.* = crc;
134136 }
135137
136138 break :block table;
......@@ -139,9 +141,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
139141 crc: u32,
140142
141143 pub fn init() Self {
142 return Self {
143 .crc = 0xffffffff,
144 };
144 return Self{ .crc = 0xffffffff };
145145 }
146146
147147 pub fn update(self: &Self, input: []const u8) void {
std/hash_map.zig+57-45
......@@ -9,10 +9,7 @@ const builtin = @import("builtin");
99const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
1010const debug_u32 = if (want_modification_safety) u32 else void;
1111
12pub fn HashMap(comptime K: type, comptime V: type,
13 comptime hash: fn(key: K)u32,
14 comptime eql: fn(a: K, b: K)bool) type
15{
12pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K) u32, comptime eql: fn(a: K, b: K) bool) type {
1613 return struct {
1714 entries: []Entry,
1815 size: usize,
......@@ -65,7 +62,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
6562 };
6663
6764 pub fn init(allocator: &Allocator) Self {
68 return Self {
65 return Self{
6966 .entries = []Entry{},
7067 .allocator = allocator,
7168 .size = 0,
......@@ -129,34 +126,36 @@ pub fn HashMap(comptime K: type, comptime V: type,
129126 if (hm.entries.len == 0) return null;
130127 hm.incrementModificationCount();
131128 const start_index = hm.keyToIndex(key);
132 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
133 const index = (start_index + roll_over) % hm.entries.len;
134 var entry = &hm.entries[index];
135
136 if (!entry.used)
137 return null;
138
139 if (!eql(entry.key, key)) continue;
140
141 while (roll_over < hm.entries.len) : (roll_over += 1) {
142 const next_index = (start_index + roll_over + 1) % hm.entries.len;
143 const next_entry = &hm.entries[next_index];
144 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
145 entry.used = false;
146 hm.size -= 1;
147 return entry;
129 {
130 var roll_over: usize = 0;
131 while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
132 const index = (start_index + roll_over) % hm.entries.len;
133 var entry = &hm.entries[index];
134
135 if (!entry.used) return null;
136
137 if (!eql(entry.key, key)) continue;
138
139 while (roll_over < hm.entries.len) : (roll_over += 1) {
140 const next_index = (start_index + roll_over + 1) % hm.entries.len;
141 const next_entry = &hm.entries[next_index];
142 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
143 entry.used = false;
144 hm.size -= 1;
145 return entry;
146 }
147 entry.* = next_entry.*;
148 entry.distance_from_start_index -= 1;
149 entry = next_entry;
148150 }
149 *entry = *next_entry;
150 entry.distance_from_start_index -= 1;
151 entry = next_entry;
151 unreachable; // shifting everything in the table
152152 }
153 unreachable; // shifting everything in the table
154 }}
153 }
155154 return null;
156155 }
157156
158157 pub fn iterator(hm: &const Self) Iterator {
159 return Iterator {
158 return Iterator{
160159 .hm = hm,
161160 .count = 0,
162161 .index = 0,
......@@ -182,21 +181,23 @@ pub fn HashMap(comptime K: type, comptime V: type,
182181 /// Returns the value that was already there.
183182 fn internalPut(hm: &Self, orig_key: K, orig_value: &const V) ?V {
184183 var key = orig_key;
185 var value = *orig_value;
184 var value = orig_value.*;
186185 const start_index = hm.keyToIndex(key);
187186 var roll_over: usize = 0;
188187 var distance_from_start_index: usize = 0;
189 while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1;}) {
188 while (roll_over < hm.entries.len) : ({
189 roll_over += 1;
190 distance_from_start_index += 1;
191 }) {
190192 const index = (start_index + roll_over) % hm.entries.len;
191193 const entry = &hm.entries[index];
192194
193195 if (entry.used and !eql(entry.key, key)) {
194196 if (entry.distance_from_start_index < distance_from_start_index) {
195197 // robin hood to the rescue
196 const tmp = *entry;
197 hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index,
198 distance_from_start_index);
199 *entry = Entry {
198 const tmp = entry.*;
199 hm.max_distance_from_start_index = math.max(hm.max_distance_from_start_index, distance_from_start_index);
200 entry.* = Entry{
200201 .used = true,
201202 .distance_from_start_index = distance_from_start_index,
202203 .key = key,
......@@ -219,7 +220,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
219220 }
220221
221222 hm.max_distance_from_start_index = math.max(distance_from_start_index, hm.max_distance_from_start_index);
222 *entry = Entry {
223 entry.* = Entry{
223224 .used = true,
224225 .distance_from_start_index = distance_from_start_index,
225226 .key = key,
......@@ -232,13 +233,16 @@ pub fn HashMap(comptime K: type, comptime V: type,
232233
233234 fn internalGet(hm: &const Self, key: K) ?&Entry {
234235 const start_index = hm.keyToIndex(key);
235 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
236 const index = (start_index + roll_over) % hm.entries.len;
237 const entry = &hm.entries[index];
238
239 if (!entry.used) return null;
240 if (eql(entry.key, key)) return entry;
241 }}
236 {
237 var roll_over: usize = 0;
238 while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
239 const index = (start_index + roll_over) % hm.entries.len;
240 const entry = &hm.entries[index];
241
242 if (!entry.used) return null;
243 if (eql(entry.key, key)) return entry;
244 }
245 }
242246 return null;
243247 }
244248
......@@ -282,11 +286,19 @@ test "iterator hash map" {
282286 assert((reset_map.put(2, 22) catch unreachable) == null);
283287 assert((reset_map.put(3, 33) catch unreachable) == null);
284288
285 var keys = []i32 { 1, 2, 3 };
286 var values = []i32 { 11, 22, 33 };
289 var keys = []i32{
290 1,
291 2,
292 3,
293 };
294 var values = []i32{
295 11,
296 22,
297 33,
298 };
287299
288300 var it = reset_map.iterator();
289 var count : usize = 0;
301 var count: usize = 0;
290302 while (it.next()) |next| {
291303 assert(next.key == keys[count]);
292304 assert(next.value == values[count]);
......@@ -305,7 +317,7 @@ test "iterator hash map" {
305317 }
306318
307319 it.reset();
308 var entry = ?? it.next();
320 var entry = ??it.next();
309321 assert(entry.key == keys[0]);
310322 assert(entry.value == values[0]);
311323}
std/heap.zig+52-54
......@@ -10,7 +10,7 @@ const c = std.c;
1010const Allocator = mem.Allocator;
1111
1212pub const c_allocator = &c_allocator_state;
13var c_allocator_state = Allocator {
13var c_allocator_state = Allocator{
1414 .allocFn = cAlloc,
1515 .reallocFn = cRealloc,
1616 .freeFn = cFree,
......@@ -18,10 +18,7 @@ var c_allocator_state = Allocator {
1818
1919fn cAlloc(self: &Allocator, n: usize, alignment: u29) ![]u8 {
2020 assert(alignment <= @alignOf(c_longdouble));
21 return if (c.malloc(n)) |buf|
22 @ptrCast(&u8, buf)[0..n]
23 else
24 error.OutOfMemory;
21 return if (c.malloc(n)) |buf| @ptrCast(&u8, buf)[0..n] else error.OutOfMemory;
2522}
2623
2724fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
......@@ -48,8 +45,8 @@ pub const DirectAllocator = struct {
4845 const HeapHandle = if (builtin.os == Os.windows) os.windows.HANDLE else void;
4946
5047 pub fn init() DirectAllocator {
51 return DirectAllocator {
52 .allocator = Allocator {
48 return DirectAllocator{
49 .allocator = Allocator{
5350 .allocFn = alloc,
5451 .reallocFn = realloc,
5552 .freeFn = free,
......@@ -71,39 +68,39 @@ pub const DirectAllocator = struct {
7168 const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
7269
7370 switch (builtin.os) {
74 Os.linux, Os.macosx, Os.ios => {
71 Os.linux,
72 Os.macosx,
73 Os.ios => {
7574 const p = os.posix;
76 const alloc_size = if(alignment <= os.page_size) n else n + alignment;
77 const addr = p.mmap(null, alloc_size, p.PROT_READ|p.PROT_WRITE,
78 p.MAP_PRIVATE|p.MAP_ANONYMOUS, -1, 0);
79 if(addr == p.MAP_FAILED) return error.OutOfMemory;
80
81 if(alloc_size == n) return @intToPtr(&u8, addr)[0..n];
82
75 const alloc_size = if (alignment <= os.page_size) n else n + alignment;
76 const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0);
77 if (addr == p.MAP_FAILED) return error.OutOfMemory;
78
79 if (alloc_size == n) return @intToPtr(&u8, addr)[0..n];
80
8381 var aligned_addr = addr & ~usize(alignment - 1);
8482 aligned_addr += alignment;
85
83
8684 //We can unmap the unused portions of our mmap, but we must only
8785 // pass munmap bytes that exist outside our allocated pages or it
8886 // will happily eat us too
89
87
9088 //Since alignment > page_size, we are by definition on a page boundry
9189 const unused_start = addr;
9290 const unused_len = aligned_addr - 1 - unused_start;
9391
9492 var err = p.munmap(unused_start, unused_len);
9593 debug.assert(p.getErrno(err) == 0);
96
94
9795 //It is impossible that there is an unoccupied page at the top of our
9896 // mmap.
99
97
10098 return @intToPtr(&u8, aligned_addr)[0..n];
10199 },
102100 Os.windows => {
103101 const amt = n + alignment + @sizeOf(usize);
104102 const heap_handle = self.heap_handle ?? blk: {
105 const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0)
106 ?? return error.OutOfMemory;
103 const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) ?? return error.OutOfMemory;
107104 self.heap_handle = hh;
108105 break :blk hh;
109106 };
......@@ -113,7 +110,7 @@ pub const DirectAllocator = struct {
113110 const march_forward_bytes = if (rem == 0) 0 else (alignment - rem);
114111 const adjusted_addr = root_addr + march_forward_bytes;
115112 const record_addr = adjusted_addr + n;
116 *@intToPtr(&align(1) usize, record_addr) = root_addr;
113 @intToPtr(&align(1) usize, record_addr).* = root_addr;
117114 return @intToPtr(&u8, adjusted_addr)[0..n];
118115 },
119116 else => @compileError("Unsupported OS"),
......@@ -124,7 +121,9 @@ pub const DirectAllocator = struct {
124121 const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
125122
126123 switch (builtin.os) {
127 Os.linux, Os.macosx, Os.ios => {
124 Os.linux,
125 Os.macosx,
126 Os.ios => {
128127 if (new_size <= old_mem.len) {
129128 const base_addr = @ptrToInt(old_mem.ptr);
130129 const old_addr_end = base_addr + old_mem.len;
......@@ -144,13 +143,13 @@ pub const DirectAllocator = struct {
144143 Os.windows => {
145144 const old_adjusted_addr = @ptrToInt(old_mem.ptr);
146145 const old_record_addr = old_adjusted_addr + old_mem.len;
147 const root_addr = *@intToPtr(&align(1) usize, old_record_addr);
146 const root_addr = @intToPtr(&align(1) usize, old_record_addr).*;
148147 const old_ptr = @intToPtr(os.windows.LPVOID, root_addr);
149148 const amt = new_size + alignment + @sizeOf(usize);
150149 const new_ptr = os.windows.HeapReAlloc(??self.heap_handle, 0, old_ptr, amt) ?? blk: {
151150 if (new_size > old_mem.len) return error.OutOfMemory;
152151 const new_record_addr = old_record_addr - new_size + old_mem.len;
153 *@intToPtr(&align(1) usize, new_record_addr) = root_addr;
152 @intToPtr(&align(1) usize, new_record_addr).* = root_addr;
154153 return old_mem[0..new_size];
155154 };
156155 const offset = old_adjusted_addr - root_addr;
......@@ -158,7 +157,7 @@ pub const DirectAllocator = struct {
158157 const new_adjusted_addr = new_root_addr + offset;
159158 assert(new_adjusted_addr % alignment == 0);
160159 const new_record_addr = new_adjusted_addr + new_size;
161 *@intToPtr(&align(1) usize, new_record_addr) = new_root_addr;
160 @intToPtr(&align(1) usize, new_record_addr).* = new_root_addr;
162161 return @intToPtr(&u8, new_adjusted_addr)[0..new_size];
163162 },
164163 else => @compileError("Unsupported OS"),
......@@ -169,12 +168,14 @@ pub const DirectAllocator = struct {
169168 const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
170169
171170 switch (builtin.os) {
172 Os.linux, Os.macosx, Os.ios => {
171 Os.linux,
172 Os.macosx,
173 Os.ios => {
173174 _ = os.posix.munmap(@ptrToInt(bytes.ptr), bytes.len);
174175 },
175176 Os.windows => {
176177 const record_addr = @ptrToInt(bytes.ptr) + bytes.len;
177 const root_addr = *@intToPtr(&align(1) usize, record_addr);
178 const root_addr = @intToPtr(&align(1) usize, record_addr).*;
178179 const ptr = @intToPtr(os.windows.LPVOID, root_addr);
179180 _ = os.windows.HeapFree(??self.heap_handle, 0, ptr);
180181 },
......@@ -195,8 +196,8 @@ pub const ArenaAllocator = struct {
195196 const BufNode = std.LinkedList([]u8).Node;
196197
197198 pub fn init(child_allocator: &Allocator) ArenaAllocator {
198 return ArenaAllocator {
199 .allocator = Allocator {
199 return ArenaAllocator{
200 .allocator = Allocator{
200201 .allocFn = alloc,
201202 .reallocFn = realloc,
202203 .freeFn = free,
......@@ -228,7 +229,7 @@ pub const ArenaAllocator = struct {
228229 const buf = try self.child_allocator.alignedAlloc(u8, @alignOf(BufNode), len);
229230 const buf_node_slice = ([]BufNode)(buf[0..@sizeOf(BufNode)]);
230231 const buf_node = &buf_node_slice[0];
231 *buf_node = BufNode {
232 buf_node.* = BufNode{
232233 .data = buf,
233234 .prev = null,
234235 .next = null,
......@@ -253,7 +254,7 @@ pub const ArenaAllocator = struct {
253254 cur_node = try self.createNode(cur_buf.len, n + alignment);
254255 continue;
255256 }
256 const result = cur_buf[adjusted_index .. new_end_index];
257 const result = cur_buf[adjusted_index..new_end_index];
257258 self.end_index = new_end_index;
258259 return result;
259260 }
......@@ -269,7 +270,7 @@ pub const ArenaAllocator = struct {
269270 }
270271 }
271272
272 fn free(allocator: &Allocator, bytes: []u8) void { }
273 fn free(allocator: &Allocator, bytes: []u8) void {}
273274};
274275
275276pub const FixedBufferAllocator = struct {
......@@ -278,8 +279,8 @@ pub const FixedBufferAllocator = struct {
278279 buffer: []u8,
279280
280281 pub fn init(buffer: []u8) FixedBufferAllocator {
281 return FixedBufferAllocator {
282 .allocator = Allocator {
282 return FixedBufferAllocator{
283 .allocator = Allocator{
283284 .allocFn = alloc,
284285 .reallocFn = realloc,
285286 .freeFn = free,
......@@ -299,7 +300,7 @@ pub const FixedBufferAllocator = struct {
299300 if (new_end_index > self.buffer.len) {
300301 return error.OutOfMemory;
301302 }
302 const result = self.buffer[adjusted_index .. new_end_index];
303 const result = self.buffer[adjusted_index..new_end_index];
303304 self.end_index = new_end_index;
304305
305306 return result;
......@@ -315,7 +316,7 @@ pub const FixedBufferAllocator = struct {
315316 }
316317 }
317318
318 fn free(allocator: &Allocator, bytes: []u8) void { }
319 fn free(allocator: &Allocator, bytes: []u8) void {}
319320};
320321
321322/// lock free
......@@ -325,8 +326,8 @@ pub const ThreadSafeFixedBufferAllocator = struct {
325326 buffer: []u8,
326327
327328 pub fn init(buffer: []u8) ThreadSafeFixedBufferAllocator {
328 return ThreadSafeFixedBufferAllocator {
329 .allocator = Allocator {
329 return ThreadSafeFixedBufferAllocator{
330 .allocator = Allocator{
330331 .allocFn = alloc,
331332 .reallocFn = realloc,
332333 .freeFn = free,
......@@ -348,8 +349,7 @@ pub const ThreadSafeFixedBufferAllocator = struct {
348349 if (new_end_index > self.buffer.len) {
349350 return error.OutOfMemory;
350351 }
351 end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index,
352 builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) ?? return self.buffer[adjusted_index .. new_end_index];
352 end_index = @cmpxchgWeak(usize, &self.end_index, end_index, new_end_index, builtin.AtomicOrder.SeqCst, builtin.AtomicOrder.SeqCst) ?? return self.buffer[adjusted_index..new_end_index];
353353 }
354354 }
355355
......@@ -363,11 +363,9 @@ pub const ThreadSafeFixedBufferAllocator = struct {
363363 }
364364 }
365365
366 fn free(allocator: &Allocator, bytes: []u8) void { }
366 fn free(allocator: &Allocator, bytes: []u8) void {}
367367};
368368
369
370
371369test "c_allocator" {
372370 if (builtin.link_libc) {
373371 var slice = c_allocator.alloc(u8, 50) catch return;
......@@ -415,8 +413,8 @@ fn testAllocator(allocator: &mem.Allocator) !void {
415413 var slice = try allocator.alloc(&i32, 100);
416414
417415 for (slice) |*item, i| {
418 *item = try allocator.create(i32);
419 **item = i32(i);
416 item.* = try allocator.create(i32);
417 item.*.* = i32(i);
420418 }
421419
422420 for (slice) |item, i| {
......@@ -434,26 +432,26 @@ fn testAllocator(allocator: &mem.Allocator) !void {
434432fn testAllocatorLargeAlignment(allocator: &mem.Allocator) mem.Allocator.Error!void {
435433 //Maybe a platform's page_size is actually the same as or
436434 // very near usize?
437 if(os.page_size << 2 > @maxValue(usize)) return;
438
435 if (os.page_size << 2 > @maxValue(usize)) return;
436
439437 const USizeShift = @IntType(false, std.math.log2(usize.bit_count));
440438 const large_align = u29(os.page_size << 2);
441
439
442440 var align_mask: usize = undefined;
443441 _ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(large_align)), &align_mask);
444
442
445443 var slice = try allocator.allocFn(allocator, 500, large_align);
446444 debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
447
445
448446 slice = try allocator.reallocFn(allocator, slice, 100, large_align);
449447 debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
450
448
451449 slice = try allocator.reallocFn(allocator, slice, 5000, large_align);
452450 debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
453
451
454452 slice = try allocator.reallocFn(allocator, slice, 10, large_align);
455453 debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
456
454
457455 slice = try allocator.reallocFn(allocator, slice, 20000, large_align);
458456 debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
459457
std/io.zig+18-47
......@@ -18,32 +18,17 @@ const is_windows = builtin.os == builtin.Os.windows;
1818const GetStdIoErrs = os.WindowsGetStdHandleErrs;
1919
2020pub fn getStdErr() GetStdIoErrs!File {
21 const handle = if (is_windows)
22 try os.windowsGetStdHandle(os.windows.STD_ERROR_HANDLE)
23 else if (is_posix)
24 os.posix.STDERR_FILENO
25 else
26 unreachable;
21 const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_ERROR_HANDLE) else if (is_posix) os.posix.STDERR_FILENO else unreachable;
2722 return File.openHandle(handle);
2823}
2924
3025pub fn getStdOut() GetStdIoErrs!File {
31 const handle = if (is_windows)
32 try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE)
33 else if (is_posix)
34 os.posix.STDOUT_FILENO
35 else
36 unreachable;
26 const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_OUTPUT_HANDLE) else if (is_posix) os.posix.STDOUT_FILENO else unreachable;
3727 return File.openHandle(handle);
3828}
3929
4030pub fn getStdIn() GetStdIoErrs!File {
41 const handle = if (is_windows)
42 try os.windowsGetStdHandle(os.windows.STD_INPUT_HANDLE)
43 else if (is_posix)
44 os.posix.STDIN_FILENO
45 else
46 unreachable;
31 const handle = if (is_windows) try os.windowsGetStdHandle(os.windows.STD_INPUT_HANDLE) else if (is_posix) os.posix.STDIN_FILENO else unreachable;
4732 return File.openHandle(handle);
4833}
4934
......@@ -56,11 +41,9 @@ pub const FileInStream = struct {
5641 pub const Stream = InStream(Error);
5742
5843 pub fn init(file: &File) FileInStream {
59 return FileInStream {
44 return FileInStream{
6045 .file = file,
61 .stream = Stream {
62 .readFn = readFn,
63 },
46 .stream = Stream{ .readFn = readFn },
6447 };
6548 }
6649
......@@ -79,11 +62,9 @@ pub const FileOutStream = struct {
7962 pub const Stream = OutStream(Error);
8063
8164 pub fn init(file: &File) FileOutStream {
82 return FileOutStream {
65 return FileOutStream{
8366 .file = file,
84 .stream = Stream {
85 .writeFn = writeFn,
86 },
67 .stream = Stream{ .writeFn = writeFn },
8768 };
8869 }
8970
......@@ -121,8 +102,7 @@ pub fn InStream(comptime ReadError: type) type {
121102 }
122103
123104 const new_buf_size = math.min(max_size, actual_buf_len + os.page_size);
124 if (new_buf_size == actual_buf_len)
125 return error.StreamTooLong;
105 if (new_buf_size == actual_buf_len) return error.StreamTooLong;
126106 try buffer.resize(new_buf_size);
127107 }
128108 }
......@@ -165,9 +145,7 @@ pub fn InStream(comptime ReadError: type) type {
165145 /// memory would be greater than `max_size`, returns `error.StreamTooLong`.
166146 /// Caller owns returned memory.
167147 /// If this function returns an error, the contents from the stream read so far are lost.
168 pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator,
169 delimiter: u8, max_size: usize) ![]u8
170 {
148 pub fn readUntilDelimiterAlloc(self: &Self, allocator: &mem.Allocator, delimiter: u8, max_size: usize) ![]u8 {
171149 var buf = Buffer.initNull(allocator);
172150 defer buf.deinit();
173151
......@@ -283,7 +261,7 @@ pub fn BufferedInStream(comptime Error: type) type {
283261pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type) type {
284262 return struct {
285263 const Self = this;
286 const Stream = InStream(Error);
264 const Stream = InStream(Error);
287265
288266 pub stream: Stream,
289267
......@@ -294,7 +272,7 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
294272 end_index: usize,
295273
296274 pub fn init(unbuffered_in_stream: &Stream) Self {
297 return Self {
275 return Self{
298276 .unbuffered_in_stream = unbuffered_in_stream,
299277 .buffer = undefined,
300278
......@@ -305,9 +283,7 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize, comptime Error: type)
305283 .start_index = buffer_size,
306284 .end_index = buffer_size,
307285
308 .stream = Stream {
309 .readFn = readFn,
310 },
286 .stream = Stream{ .readFn = readFn },
311287 };
312288 }
313289
......@@ -368,13 +344,11 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize, comptime OutStreamEr
368344 index: usize,
369345
370346 pub fn init(unbuffered_out_stream: &Stream) Self {
371 return Self {
347 return Self{
372348 .unbuffered_out_stream = unbuffered_out_stream,
373349 .buffer = undefined,
374350 .index = 0,
375 .stream = Stream {
376 .writeFn = writeFn,
377 },
351 .stream = Stream{ .writeFn = writeFn },
378352 };
379353 }
380354
......@@ -416,11 +390,9 @@ pub const BufferOutStream = struct {
416390 pub const Stream = OutStream(Error);
417391
418392 pub fn init(buffer: &Buffer) BufferOutStream {
419 return BufferOutStream {
393 return BufferOutStream{
420394 .buffer = buffer,
421 .stream = Stream {
422 .writeFn = writeFn,
423 },
395 .stream = Stream{ .writeFn = writeFn },
424396 };
425397 }
426398
......@@ -430,7 +402,6 @@ pub const BufferOutStream = struct {
430402 }
431403};
432404
433
434405pub const BufferedAtomicFile = struct {
435406 atomic_file: os.AtomicFile,
436407 file_stream: FileOutStream,
......@@ -441,7 +412,7 @@ pub const BufferedAtomicFile = struct {
441412 var self = try allocator.create(BufferedAtomicFile);
442413 errdefer allocator.destroy(self);
443414
444 *self = BufferedAtomicFile {
415 self.* = BufferedAtomicFile{
445416 .atomic_file = undefined,
446417 .file_stream = undefined,
447418 .buffered_stream = undefined,
......@@ -489,7 +460,7 @@ pub fn readLine(buf: []u8) !usize {
489460 '\r' => {
490461 // trash the following \n
491462 _ = stream.readByte() catch return error.EndOfFile;
492 return index;
463 return index;
493464 },
494465 '\n' => return index,
495466 else => {
std/json.zig+124-83
......@@ -35,7 +35,7 @@ pub const Token = struct {
3535 };
3636
3737 pub fn init(id: Id, count: usize, offset: u1) Token {
38 return Token {
38 return Token{
3939 .id = id,
4040 .offset = offset,
4141 .string_has_escape = false,
......@@ -45,7 +45,7 @@ pub const Token = struct {
4545 }
4646
4747 pub fn initString(count: usize, has_unicode_escape: bool) Token {
48 return Token {
48 return Token{
4949 .id = Id.String,
5050 .offset = 0,
5151 .string_has_escape = has_unicode_escape,
......@@ -55,7 +55,7 @@ pub const Token = struct {
5555 }
5656
5757 pub fn initNumber(count: usize, number_is_integer: bool) Token {
58 return Token {
58 return Token{
5959 .id = Id.Number,
6060 .offset = 0,
6161 .string_has_escape = false,
......@@ -66,7 +66,7 @@ pub const Token = struct {
6666
6767 // A marker token is a zero-length
6868 pub fn initMarker(id: Id) Token {
69 return Token {
69 return Token{
7070 .id = id,
7171 .offset = 0,
7272 .string_has_escape = false,
......@@ -77,7 +77,7 @@ pub const Token = struct {
7777
7878 // Slice into the underlying input string.
7979 pub fn slice(self: &const Token, input: []const u8, i: usize) []const u8 {
80 return input[i + self.offset - self.count .. i + self.offset];
80 return input[i + self.offset - self.count..i + self.offset];
8181 }
8282};
8383
......@@ -105,8 +105,8 @@ const StreamingJsonParser = struct {
105105 stack: u256,
106106 stack_used: u8,
107107
108 const object_bit = 0;
109 const array_bit = 1;
108 const object_bit = 0;
109 const array_bit = 1;
110110 const max_stack_size = @maxValue(u8);
111111
112112 pub fn init() StreamingJsonParser {
......@@ -120,7 +120,7 @@ const StreamingJsonParser = struct {
120120 p.count = 0;
121121 // Set before ever read in main transition function
122122 p.after_string_state = undefined;
123 p.after_value_state = State.ValueEnd; // handle end of values normally
123 p.after_value_state = State.ValueEnd; // handle end of values normally
124124 p.stack = 0;
125125 p.stack_used = 0;
126126 p.complete = false;
......@@ -181,7 +181,7 @@ const StreamingJsonParser = struct {
181181 }
182182 };
183183
184 pub const Error = error {
184 pub const Error = error{
185185 InvalidTopLevel,
186186 TooManyNestedItems,
187187 TooManyClosingItems,
......@@ -206,8 +206,8 @@ const StreamingJsonParser = struct {
206206 //
207207 // There is currently no error recovery on a bad stream.
208208 pub fn feed(p: &StreamingJsonParser, c: u8, token1: &?Token, token2: &?Token) Error!void {
209 *token1 = null;
210 *token2 = null;
209 token1.* = null;
210 token2.* = null;
211211 p.count += 1;
212212
213213 // unlikely
......@@ -228,7 +228,7 @@ const StreamingJsonParser = struct {
228228 p.state = State.ValueBegin;
229229 p.after_string_state = State.ObjectSeparator;
230230
231 *token = Token.initMarker(Token.Id.ObjectBegin);
231 token.* = Token.initMarker(Token.Id.ObjectBegin);
232232 },
233233 '[' => {
234234 p.stack <<= 1;
......@@ -238,7 +238,7 @@ const StreamingJsonParser = struct {
238238 p.state = State.ValueBegin;
239239 p.after_string_state = State.ValueEnd;
240240
241 *token = Token.initMarker(Token.Id.ArrayBegin);
241 token.* = Token.initMarker(Token.Id.ArrayBegin);
242242 },
243243 '-' => {
244244 p.number_is_integer = true;
......@@ -281,7 +281,10 @@ const StreamingJsonParser = struct {
281281 p.after_value_state = State.TopLevelEnd;
282282 p.count = 0;
283283 },
284 0x09, 0x0A, 0x0D, 0x20 => {
284 0x09,
285 0x0A,
286 0x0D,
287 0x20 => {
285288 // whitespace
286289 },
287290 else => {
......@@ -290,7 +293,10 @@ const StreamingJsonParser = struct {
290293 },
291294
292295 State.TopLevelEnd => switch (c) {
293 0x09, 0x0A, 0x0D, 0x20 => {
296 0x09,
297 0x0A,
298 0x0D,
299 0x20 => {
294300 // whitespace
295301 },
296302 else => {
......@@ -324,7 +330,7 @@ const StreamingJsonParser = struct {
324330 else => {},
325331 }
326332
327 *token = Token.initMarker(Token.Id.ObjectEnd);
333 token.* = Token.initMarker(Token.Id.ObjectEnd);
328334 },
329335 ']' => {
330336 if (p.stack & 1 != array_bit) {
......@@ -348,7 +354,7 @@ const StreamingJsonParser = struct {
348354 else => {},
349355 }
350356
351 *token = Token.initMarker(Token.Id.ArrayEnd);
357 token.* = Token.initMarker(Token.Id.ArrayEnd);
352358 },
353359 '{' => {
354360 if (p.stack_used == max_stack_size) {
......@@ -362,7 +368,7 @@ const StreamingJsonParser = struct {
362368 p.state = State.ValueBegin;
363369 p.after_string_state = State.ObjectSeparator;
364370
365 *token = Token.initMarker(Token.Id.ObjectBegin);
371 token.* = Token.initMarker(Token.Id.ObjectBegin);
366372 },
367373 '[' => {
368374 if (p.stack_used == max_stack_size) {
......@@ -376,7 +382,7 @@ const StreamingJsonParser = struct {
376382 p.state = State.ValueBegin;
377383 p.after_string_state = State.ValueEnd;
378384
379 *token = Token.initMarker(Token.Id.ArrayBegin);
385 token.* = Token.initMarker(Token.Id.ArrayBegin);
380386 },
381387 '-' => {
382388 p.state = State.Number;
......@@ -406,7 +412,10 @@ const StreamingJsonParser = struct {
406412 p.state = State.NullLiteral1;
407413 p.count = 0;
408414 },
409 0x09, 0x0A, 0x0D, 0x20 => {
415 0x09,
416 0x0A,
417 0x0D,
418 0x20 => {
410419 // whitespace
411420 },
412421 else => {
......@@ -428,7 +437,7 @@ const StreamingJsonParser = struct {
428437 p.state = State.ValueBegin;
429438 p.after_string_state = State.ObjectSeparator;
430439
431 *token = Token.initMarker(Token.Id.ObjectBegin);
440 token.* = Token.initMarker(Token.Id.ObjectBegin);
432441 },
433442 '[' => {
434443 if (p.stack_used == max_stack_size) {
......@@ -442,7 +451,7 @@ const StreamingJsonParser = struct {
442451 p.state = State.ValueBegin;
443452 p.after_string_state = State.ValueEnd;
444453
445 *token = Token.initMarker(Token.Id.ArrayBegin);
454 token.* = Token.initMarker(Token.Id.ArrayBegin);
446455 },
447456 '-' => {
448457 p.state = State.Number;
......@@ -472,7 +481,10 @@ const StreamingJsonParser = struct {
472481 p.state = State.NullLiteral1;
473482 p.count = 0;
474483 },
475 0x09, 0x0A, 0x0D, 0x20 => {
484 0x09,
485 0x0A,
486 0x0D,
487 0x20 => {
476488 // whitespace
477489 },
478490 else => {
......@@ -501,7 +513,7 @@ const StreamingJsonParser = struct {
501513 p.state = State.TopLevelEnd;
502514 }
503515
504 *token = Token.initMarker(Token.Id.ArrayEnd);
516 token.* = Token.initMarker(Token.Id.ArrayEnd);
505517 },
506518 '}' => {
507519 if (p.stack_used == 0) {
......@@ -519,9 +531,12 @@ const StreamingJsonParser = struct {
519531 p.state = State.TopLevelEnd;
520532 }
521533
522 *token = Token.initMarker(Token.Id.ObjectEnd);
534 token.* = Token.initMarker(Token.Id.ObjectEnd);
523535 },
524 0x09, 0x0A, 0x0D, 0x20 => {
536 0x09,
537 0x0A,
538 0x0D,
539 0x20 => {
525540 // whitespace
526541 },
527542 else => {
......@@ -534,7 +549,10 @@ const StreamingJsonParser = struct {
534549 p.state = State.ValueBegin;
535550 p.after_string_state = State.ValueEnd;
536551 },
537 0x09, 0x0A, 0x0D, 0x20 => {
552 0x09,
553 0x0A,
554 0x0D,
555 0x20 => {
538556 // whitespace
539557 },
540558 else => {
......@@ -553,12 +571,15 @@ const StreamingJsonParser = struct {
553571 p.complete = true;
554572 }
555573
556 *token = Token.initString(p.count - 1, p.string_has_escape);
574 token.* = Token.initString(p.count - 1, p.string_has_escape);
557575 },
558576 '\\' => {
559577 p.state = State.StringEscapeCharacter;
560578 },
561 0x20, 0x21, 0x23 ... 0x5B, 0x5D ... 0x7F => {
579 0x20,
580 0x21,
581 0x23 ... 0x5B,
582 0x5D ... 0x7F => {
562583 // non-control ascii
563584 },
564585 0xC0 ... 0xDF => {
......@@ -599,7 +620,14 @@ const StreamingJsonParser = struct {
599620 // The current JSONTestSuite tests rely on both of this behaviour being present
600621 // however, so we default to the status quo where both are accepted until this
601622 // is further clarified.
602 '"', '\\', '/', 'b', 'f', 'n', 'r', 't' => {
623 '"',
624 '\\',
625 '/',
626 'b',
627 'f',
628 'n',
629 'r',
630 't' => {
603631 p.string_has_escape = true;
604632 p.state = State.String;
605633 },
......@@ -613,28 +641,36 @@ const StreamingJsonParser = struct {
613641 },
614642
615643 State.StringEscapeHexUnicode4 => switch (c) {
616 '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
644 '0' ... '9',
645 'A' ... 'F',
646 'a' ... 'f' => {
617647 p.state = State.StringEscapeHexUnicode3;
618648 },
619649 else => return error.InvalidUnicodeHexSymbol,
620650 },
621651
622652 State.StringEscapeHexUnicode3 => switch (c) {
623 '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
653 '0' ... '9',
654 'A' ... 'F',
655 'a' ... 'f' => {
624656 p.state = State.StringEscapeHexUnicode2;
625657 },
626658 else => return error.InvalidUnicodeHexSymbol,
627659 },
628660
629661 State.StringEscapeHexUnicode2 => switch (c) {
630 '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
662 '0' ... '9',
663 'A' ... 'F',
664 'a' ... 'f' => {
631665 p.state = State.StringEscapeHexUnicode1;
632666 },
633667 else => return error.InvalidUnicodeHexSymbol,
634668 },
635669
636670 State.StringEscapeHexUnicode1 => switch (c) {
637 '0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
671 '0' ... '9',
672 'A' ... 'F',
673 'a' ... 'f' => {
638674 p.state = State.String;
639675 },
640676 else => return error.InvalidUnicodeHexSymbol,
......@@ -662,13 +698,14 @@ const StreamingJsonParser = struct {
662698 p.number_is_integer = false;
663699 p.state = State.NumberFractionalRequired;
664700 },
665 'e', 'E' => {
701 'e',
702 'E' => {
666703 p.number_is_integer = false;
667704 p.state = State.NumberExponent;
668705 },
669706 else => {
670707 p.state = p.after_value_state;
671 *token = Token.initNumber(p.count, p.number_is_integer);
708 token.* = Token.initNumber(p.count, p.number_is_integer);
672709 return true;
673710 },
674711 }
......@@ -681,7 +718,8 @@ const StreamingJsonParser = struct {
681718 p.number_is_integer = false;
682719 p.state = State.NumberFractionalRequired;
683720 },
684 'e', 'E' => {
721 'e',
722 'E' => {
685723 p.number_is_integer = false;
686724 p.state = State.NumberExponent;
687725 },
......@@ -690,7 +728,7 @@ const StreamingJsonParser = struct {
690728 },
691729 else => {
692730 p.state = p.after_value_state;
693 *token = Token.initNumber(p.count, p.number_is_integer);
731 token.* = Token.initNumber(p.count, p.number_is_integer);
694732 return true;
695733 },
696734 }
......@@ -714,13 +752,14 @@ const StreamingJsonParser = struct {
714752 '0' ... '9' => {
715753 // another digit
716754 },
717 'e', 'E' => {
755 'e',
756 'E' => {
718757 p.number_is_integer = false;
719758 p.state = State.NumberExponent;
720759 },
721760 else => {
722761 p.state = p.after_value_state;
723 *token = Token.initNumber(p.count, p.number_is_integer);
762 token.* = Token.initNumber(p.count, p.number_is_integer);
724763 return true;
725764 },
726765 }
......@@ -729,20 +768,22 @@ const StreamingJsonParser = struct {
729768 State.NumberMaybeExponent => {
730769 p.complete = p.after_value_state == State.TopLevelEnd;
731770 switch (c) {
732 'e', 'E' => {
771 'e',
772 'E' => {
733773 p.number_is_integer = false;
734774 p.state = State.NumberExponent;
735775 },
736776 else => {
737777 p.state = p.after_value_state;
738 *token = Token.initNumber(p.count, p.number_is_integer);
778 token.* = Token.initNumber(p.count, p.number_is_integer);
739779 return true;
740780 },
741781 }
742782 },
743783
744784 State.NumberExponent => switch (c) {
745 '-', '+', => {
785 '-',
786 '+' => {
746787 p.complete = false;
747788 p.state = State.NumberExponentDigitsRequired;
748789 },
......@@ -773,7 +814,7 @@ const StreamingJsonParser = struct {
773814 },
774815 else => {
775816 p.state = p.after_value_state;
776 *token = Token.initNumber(p.count, p.number_is_integer);
817 token.* = Token.initNumber(p.count, p.number_is_integer);
777818 return true;
778819 },
779820 }
......@@ -793,7 +834,7 @@ const StreamingJsonParser = struct {
793834 'e' => {
794835 p.state = p.after_value_state;
795836 p.complete = p.state == State.TopLevelEnd;
796 *token = Token.init(Token.Id.True, p.count + 1, 1);
837 token.* = Token.init(Token.Id.True, p.count + 1, 1);
797838 },
798839 else => {
799840 return error.InvalidLiteral;
......@@ -819,7 +860,7 @@ const StreamingJsonParser = struct {
819860 'e' => {
820861 p.state = p.after_value_state;
821862 p.complete = p.state == State.TopLevelEnd;
822 *token = Token.init(Token.Id.False, p.count + 1, 1);
863 token.* = Token.init(Token.Id.False, p.count + 1, 1);
823864 },
824865 else => {
825866 return error.InvalidLiteral;
......@@ -840,7 +881,7 @@ const StreamingJsonParser = struct {
840881 'l' => {
841882 p.state = p.after_value_state;
842883 p.complete = p.state == State.TopLevelEnd;
843 *token = Token.init(Token.Id.Null, p.count + 1, 1);
884 token.* = Token.init(Token.Id.Null, p.count + 1, 1);
844885 },
845886 else => {
846887 return error.InvalidLiteral;
......@@ -895,7 +936,7 @@ pub const Value = union(enum) {
895936 Object: ObjectMap,
896937
897938 pub fn dump(self: &const Value) void {
898 switch (*self) {
939 switch (self.*) {
899940 Value.Null => {
900941 std.debug.warn("null");
901942 },
......@@ -950,7 +991,7 @@ pub const Value = union(enum) {
950991 }
951992
952993 fn dumpIndentLevel(self: &const Value, indent: usize, level: usize) void {
953 switch (*self) {
994 switch (self.*) {
954995 Value.Null => {
955996 std.debug.warn("null");
956997 },
......@@ -1027,7 +1068,7 @@ const JsonParser = struct {
10271068 };
10281069
10291070 pub fn init(allocator: &Allocator, copy_strings: bool) JsonParser {
1030 return JsonParser {
1071 return JsonParser{
10311072 .allocator = allocator,
10321073 .state = State.Simple,
10331074 .copy_strings = copy_strings,
......@@ -1082,7 +1123,7 @@ const JsonParser = struct {
10821123
10831124 std.debug.assert(p.stack.len == 1);
10841125
1085 return ValueTree {
1126 return ValueTree{
10861127 .arena = arena,
10871128 .root = p.stack.at(0),
10881129 };
......@@ -1115,11 +1156,11 @@ const JsonParser = struct {
11151156
11161157 switch (token.id) {
11171158 Token.Id.ObjectBegin => {
1118 try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
1159 try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
11191160 p.state = State.ObjectKey;
11201161 },
11211162 Token.Id.ArrayBegin => {
1122 try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
1163 try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
11231164 p.state = State.ArrayValue;
11241165 },
11251166 Token.Id.String => {
......@@ -1133,12 +1174,12 @@ const JsonParser = struct {
11331174 p.state = State.ObjectKey;
11341175 },
11351176 Token.Id.True => {
1136 _ = try object.put(key, Value { .Bool = true });
1177 _ = try object.put(key, Value{ .Bool = true });
11371178 _ = p.stack.pop();
11381179 p.state = State.ObjectKey;
11391180 },
11401181 Token.Id.False => {
1141 _ = try object.put(key, Value { .Bool = false });
1182 _ = try object.put(key, Value{ .Bool = false });
11421183 _ = p.stack.pop();
11431184 p.state = State.ObjectKey;
11441185 },
......@@ -1165,11 +1206,11 @@ const JsonParser = struct {
11651206 try p.pushToParent(value);
11661207 },
11671208 Token.Id.ObjectBegin => {
1168 try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
1209 try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
11691210 p.state = State.ObjectKey;
11701211 },
11711212 Token.Id.ArrayBegin => {
1172 try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
1213 try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
11731214 p.state = State.ArrayValue;
11741215 },
11751216 Token.Id.String => {
......@@ -1179,10 +1220,10 @@ const JsonParser = struct {
11791220 try array.append(try p.parseNumber(token, input, i));
11801221 },
11811222 Token.Id.True => {
1182 try array.append(Value { .Bool = true });
1223 try array.append(Value{ .Bool = true });
11831224 },
11841225 Token.Id.False => {
1185 try array.append(Value { .Bool = false });
1226 try array.append(Value{ .Bool = false });
11861227 },
11871228 Token.Id.Null => {
11881229 try array.append(Value.Null);
......@@ -1194,11 +1235,11 @@ const JsonParser = struct {
11941235 },
11951236 State.Simple => switch (token.id) {
11961237 Token.Id.ObjectBegin => {
1197 try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
1238 try p.stack.append(Value{ .Object = ObjectMap.init(allocator) });
11981239 p.state = State.ObjectKey;
11991240 },
12001241 Token.Id.ArrayBegin => {
1201 try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
1242 try p.stack.append(Value{ .Array = ArrayList(Value).init(allocator) });
12021243 p.state = State.ArrayValue;
12031244 },
12041245 Token.Id.String => {
......@@ -1208,15 +1249,16 @@ const JsonParser = struct {
12081249 try p.stack.append(try p.parseNumber(token, input, i));
12091250 },
12101251 Token.Id.True => {
1211 try p.stack.append(Value { .Bool = true });
1252 try p.stack.append(Value{ .Bool = true });
12121253 },
12131254 Token.Id.False => {
1214 try p.stack.append(Value { .Bool = false });
1255 try p.stack.append(Value{ .Bool = false });
12151256 },
12161257 Token.Id.Null => {
12171258 try p.stack.append(Value.Null);
12181259 },
1219 Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
1260 Token.Id.ObjectEnd,
1261 Token.Id.ArrayEnd => {
12201262 unreachable;
12211263 },
12221264 },
......@@ -1248,15 +1290,14 @@ const JsonParser = struct {
12481290 // TODO: We don't strictly have to copy values which do not contain any escape
12491291 // characters if flagged with the option.
12501292 const slice = token.slice(input, i);
1251 return Value { .String = try mem.dupe(p.allocator, u8, slice) };
1293 return Value{ .String = try mem.dupe(p.allocator, u8, slice) };
12521294 }
12531295
12541296 fn parseNumber(p: &JsonParser, token: &const Token, input: []const u8, i: usize) !Value {
12551297 return if (token.number_is_integer)
1256 Value { .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
1298 Value{ .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
12571299 else
1258 @panic("TODO: fmt.parseFloat not yet implemented")
1259 ;
1300 @panic("TODO: fmt.parseFloat not yet implemented");
12601301 }
12611302};
12621303
......@@ -1267,21 +1308,21 @@ test "json parser dynamic" {
12671308 defer p.deinit();
12681309
12691310 const s =
1270 \\{
1271 \\ "Image": {
1272 \\ "Width": 800,
1273 \\ "Height": 600,
1274 \\ "Title": "View from 15th Floor",
1275 \\ "Thumbnail": {
1276 \\ "Url": "http://www.example.com/image/481989943",
1277 \\ "Height": 125,
1278 \\ "Width": 100
1279 \\ },
1280 \\ "Animated" : false,
1281 \\ "IDs": [116, 943, 234, 38793]
1282 \\ }
1283 \\}
1284 ;
1311 \\{
1312 \\ "Image": {
1313 \\ "Width": 800,
1314 \\ "Height": 600,
1315 \\ "Title": "View from 15th Floor",
1316 \\ "Thumbnail": {
1317 \\ "Url": "http://www.example.com/image/481989943",
1318 \\ "Height": 125,
1319 \\ "Width": 100
1320 \\ },
1321 \\ "Animated" : false,
1322 \\ "IDs": [116, 943, 234, 38793]
1323 \\ }
1324 \\}
1325 ;
12851326
12861327 var tree = try p.parse(s);
12871328 defer tree.deinit();
std/linked_list.zig+55-40
......@@ -26,10 +26,10 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
2626 data: T,
2727
2828 pub fn init(value: &const T) Node {
29 return Node {
29 return Node{
3030 .prev = null,
3131 .next = null,
32 .data = *value,
32 .data = value.*,
3333 };
3434 }
3535
......@@ -45,18 +45,18 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
4545 };
4646
4747 first: ?&Node,
48 last: ?&Node,
49 len: usize,
48 last: ?&Node,
49 len: usize,
5050
5151 /// Initialize a linked list.
5252 ///
5353 /// Returns:
5454 /// An empty linked list.
5555 pub fn init() Self {
56 return Self {
56 return Self{
5757 .first = null,
58 .last = null,
59 .len = 0,
58 .last = null,
59 .len = 0,
6060 };
6161 }
6262
......@@ -131,7 +131,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
131131 } else {
132132 // Empty list.
133133 list.first = new_node;
134 list.last = new_node;
134 list.last = new_node;
135135 new_node.prev = null;
136136 new_node.next = null;
137137
......@@ -217,7 +217,7 @@ fn BaseLinkedList(comptime T: type, comptime ParentType: type, comptime field_na
217217 pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) !&Node {
218218 comptime assert(!isIntrusive());
219219 var node = try list.allocateNode(allocator);
220 *node = Node.init(data);
220 node.* = Node.init(data);
221221 return node;
222222 }
223223 };
......@@ -227,11 +227,11 @@ test "basic linked list test" {
227227 const allocator = debug.global_allocator;
228228 var list = LinkedList(u32).init();
229229
230 var one = try list.createNode(1, allocator);
231 var two = try list.createNode(2, allocator);
230 var one = try list.createNode(1, allocator);
231 var two = try list.createNode(2, allocator);
232232 var three = try list.createNode(3, allocator);
233 var four = try list.createNode(4, allocator);
234 var five = try list.createNode(5, allocator);
233 var four = try list.createNode(4, allocator);
234 var five = try list.createNode(5, allocator);
235235 defer {
236236 list.destroyNode(one, allocator);
237237 list.destroyNode(two, allocator);
......@@ -240,11 +240,11 @@ test "basic linked list test" {
240240 list.destroyNode(five, allocator);
241241 }
242242
243 list.append(two); // {2}
244 list.append(five); // {2, 5}
245 list.prepend(one); // {1, 2, 5}
246 list.insertBefore(five, four); // {1, 2, 4, 5}
247 list.insertAfter(two, three); // {1, 2, 3, 4, 5}
243 list.append(two); // {2}
244 list.append(five); // {2, 5}
245 list.prepend(one); // {1, 2, 5}
246 list.insertBefore(five, four); // {1, 2, 4, 5}
247 list.insertAfter(two, three); // {1, 2, 3, 4, 5}
248248
249249 // Traverse forwards.
250250 {
......@@ -266,13 +266,13 @@ test "basic linked list test" {
266266 }
267267 }
268268
269 var first = list.popFirst(); // {2, 3, 4, 5}
270 var last = list.pop(); // {2, 3, 4}
271 list.remove(three); // {2, 4}
269 var first = list.popFirst(); // {2, 3, 4, 5}
270 var last = list.pop(); // {2, 3, 4}
271 list.remove(three); // {2, 4}
272272
273 assert ((??list.first).data == 2);
274 assert ((??list.last ).data == 4);
275 assert (list.len == 2);
273 assert((??list.first).data == 2);
274 assert((??list.last).data == 4);
275 assert(list.len == 2);
276276}
277277
278278const ElementList = IntrusiveLinkedList(Element, "link");
......@@ -285,17 +285,32 @@ test "basic intrusive linked list test" {
285285 const allocator = debug.global_allocator;
286286 var list = ElementList.init();
287287
288 var one = Element { .value = 1, .link = ElementList.Node.initIntrusive() };
289 var two = Element { .value = 2, .link = ElementList.Node.initIntrusive() };
290 var three = Element { .value = 3, .link = ElementList.Node.initIntrusive() };
291 var four = Element { .value = 4, .link = ElementList.Node.initIntrusive() };
292 var five = Element { .value = 5, .link = ElementList.Node.initIntrusive() };
288 var one = Element{
289 .value = 1,
290 .link = ElementList.Node.initIntrusive(),
291 };
292 var two = Element{
293 .value = 2,
294 .link = ElementList.Node.initIntrusive(),
295 };
296 var three = Element{
297 .value = 3,
298 .link = ElementList.Node.initIntrusive(),
299 };
300 var four = Element{
301 .value = 4,
302 .link = ElementList.Node.initIntrusive(),
303 };
304 var five = Element{
305 .value = 5,
306 .link = ElementList.Node.initIntrusive(),
307 };
293308
294 list.append(&two.link); // {2}
295 list.append(&five.link); // {2, 5}
296 list.prepend(&one.link); // {1, 2, 5}
297 list.insertBefore(&five.link, &four.link); // {1, 2, 4, 5}
298 list.insertAfter(&two.link, &three.link); // {1, 2, 3, 4, 5}
309 list.append(&two.link); // {2}
310 list.append(&five.link); // {2, 5}
311 list.prepend(&one.link); // {1, 2, 5}
312 list.insertBefore(&five.link, &four.link); // {1, 2, 4, 5}
313 list.insertAfter(&two.link, &three.link); // {1, 2, 3, 4, 5}
299314
300315 // Traverse forwards.
301316 {
......@@ -317,11 +332,11 @@ test "basic intrusive linked list test" {
317332 }
318333 }
319334
320 var first = list.popFirst(); // {2, 3, 4, 5}
321 var last = list.pop(); // {2, 3, 4}
322 list.remove(&three.link); // {2, 4}
335 var first = list.popFirst(); // {2, 3, 4, 5}
336 var last = list.pop(); // {2, 3, 4}
337 list.remove(&three.link); // {2, 4}
323338
324 assert ((??list.first).toData().value == 2);
325 assert ((??list.last ).toData().value == 4);
326 assert (list.len == 2);
339 assert((??list.first).toData().value == 2);
340 assert((??list.last).toData().value == 4);
341 assert(list.len == 2);
327342}
std/math/acos.zig+7-7
......@@ -16,7 +16,7 @@ pub fn acos(x: var) @typeOf(x) {
1616}
1717
1818fn r32(z: f32) f32 {
19 const pS0 = 1.6666586697e-01;
19 const pS0 = 1.6666586697e-01;
2020 const pS1 = -4.2743422091e-02;
2121 const pS2 = -8.6563630030e-03;
2222 const qS1 = -7.0662963390e-01;
......@@ -74,16 +74,16 @@ fn acos32(x: f32) f32 {
7474}
7575
7676fn r64(z: f64) f64 {
77 const pS0: f64 = 1.66666666666666657415e-01;
77 const pS0: f64 = 1.66666666666666657415e-01;
7878 const pS1: f64 = -3.25565818622400915405e-01;
79 const pS2: f64 = 2.01212532134862925881e-01;
79 const pS2: f64 = 2.01212532134862925881e-01;
8080 const pS3: f64 = -4.00555345006794114027e-02;
81 const pS4: f64 = 7.91534994289814532176e-04;
82 const pS5: f64 = 3.47933107596021167570e-05;
81 const pS4: f64 = 7.91534994289814532176e-04;
82 const pS5: f64 = 3.47933107596021167570e-05;
8383 const qS1: f64 = -2.40339491173441421878e+00;
84 const qS2: f64 = 2.02094576023350569471e+00;
84 const qS2: f64 = 2.02094576023350569471e+00;
8585 const qS3: f64 = -6.88283971605453293030e-01;
86 const qS4: f64 = 7.70381505559019352791e-02;
86 const qS4: f64 = 7.70381505559019352791e-02;
8787
8888 const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
8989 const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
std/math/asin.zig+9-9
......@@ -17,7 +17,7 @@ pub fn asin(x: var) @typeOf(x) {
1717}
1818
1919fn r32(z: f32) f32 {
20 const pS0 = 1.6666586697e-01;
20 const pS0 = 1.6666586697e-01;
2121 const pS1 = -4.2743422091e-02;
2222 const pS2 = -8.6563630030e-03;
2323 const qS1 = -7.0662963390e-01;
......@@ -37,9 +37,9 @@ fn asin32(x: f32) f32 {
3737 if (ix >= 0x3F800000) {
3838 // |x| >= 1
3939 if (ix == 0x3F800000) {
40 return x * pio2 + 0x1.0p-120; // asin(+-1) = +-pi/2 with inexact
40 return x * pio2 + 0x1.0p-120; // asin(+-1) = +-pi/2 with inexact
4141 } else {
42 return math.nan(f32); // asin(|x| > 1) is nan
42 return math.nan(f32); // asin(|x| > 1) is nan
4343 }
4444 }
4545
......@@ -66,16 +66,16 @@ fn asin32(x: f32) f32 {
6666}
6767
6868fn r64(z: f64) f64 {
69 const pS0: f64 = 1.66666666666666657415e-01;
69 const pS0: f64 = 1.66666666666666657415e-01;
7070 const pS1: f64 = -3.25565818622400915405e-01;
71 const pS2: f64 = 2.01212532134862925881e-01;
71 const pS2: f64 = 2.01212532134862925881e-01;
7272 const pS3: f64 = -4.00555345006794114027e-02;
73 const pS4: f64 = 7.91534994289814532176e-04;
74 const pS5: f64 = 3.47933107596021167570e-05;
73 const pS4: f64 = 7.91534994289814532176e-04;
74 const pS5: f64 = 3.47933107596021167570e-05;
7575 const qS1: f64 = -2.40339491173441421878e+00;
76 const qS2: f64 = 2.02094576023350569471e+00;
76 const qS2: f64 = 2.02094576023350569471e+00;
7777 const qS3: f64 = -6.88283971605453293030e-01;
78 const qS4: f64 = 7.70381505559019352791e-02;
78 const qS4: f64 = 7.70381505559019352791e-02;
7979
8080 const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
8181 const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
std/math/atan2.zig+34-32
......@@ -31,7 +31,7 @@ pub fn atan2(comptime T: type, x: T, y: T) T {
3131}
3232
3333fn atan2_32(y: f32, x: f32) f32 {
34 const pi: f32 = 3.1415927410e+00;
34 const pi: f32 = 3.1415927410e+00;
3535 const pi_lo: f32 = -8.7422776573e-08;
3636
3737 if (math.isNan(x) or math.isNan(y)) {
......@@ -53,9 +53,10 @@ fn atan2_32(y: f32, x: f32) f32 {
5353
5454 if (iy == 0) {
5555 switch (m) {
56 0, 1 => return y, // atan(+-0, +...)
57 2 => return pi, // atan(+0, -...)
58 3 => return -pi, // atan(-0, -...)
56 0,
57 1 => return y, // atan(+-0, +...)
58 2 => return pi, // atan(+0, -...)
59 3 => return -pi, // atan(-0, -...)
5960 else => unreachable,
6061 }
6162 }
......@@ -71,18 +72,18 @@ fn atan2_32(y: f32, x: f32) f32 {
7172 if (ix == 0x7F800000) {
7273 if (iy == 0x7F800000) {
7374 switch (m) {
74 0 => return pi / 4, // atan(+inf, +inf)
75 1 => return -pi / 4, // atan(-inf, +inf)
76 2 => return 3*pi / 4, // atan(+inf, -inf)
77 3 => return -3*pi / 4, // atan(-inf, -inf)
75 0 => return pi / 4, // atan(+inf, +inf)
76 1 => return -pi / 4, // atan(-inf, +inf)
77 2 => return 3 * pi / 4, // atan(+inf, -inf)
78 3 => return -3 * pi / 4, // atan(-inf, -inf)
7879 else => unreachable,
7980 }
8081 } else {
8182 switch (m) {
82 0 => return 0.0, // atan(+..., +inf)
83 1 => return -0.0, // atan(-..., +inf)
84 2 => return pi, // atan(+..., -inf)
85 3 => return -pi, // atan(-...f, -inf)
83 0 => return 0.0, // atan(+..., +inf)
84 1 => return -0.0, // atan(-..., +inf)
85 2 => return pi, // atan(+..., -inf)
86 3 => return -pi, // atan(-...f, -inf)
8687 else => unreachable,
8788 }
8889 }
......@@ -107,16 +108,16 @@ fn atan2_32(y: f32, x: f32) f32 {
107108 };
108109
109110 switch (m) {
110 0 => return z, // atan(+, +)
111 1 => return -z, // atan(-, +)
112 2 => return pi - (z - pi_lo), // atan(+, -)
113 3 => return (z - pi_lo) - pi, // atan(-, -)
111 0 => return z, // atan(+, +)
112 1 => return -z, // atan(-, +)
113 2 => return pi - (z - pi_lo), // atan(+, -)
114 3 => return (z - pi_lo) - pi, // atan(-, -)
114115 else => unreachable,
115116 }
116117}
117118
118119fn atan2_64(y: f64, x: f64) f64 {
119 const pi: f64 = 3.1415926535897931160E+00;
120 const pi: f64 = 3.1415926535897931160E+00;
120121 const pi_lo: f64 = 1.2246467991473531772E-16;
121122
122123 if (math.isNan(x) or math.isNan(y)) {
......@@ -143,9 +144,10 @@ fn atan2_64(y: f64, x: f64) f64 {
143144
144145 if (iy | ly == 0) {
145146 switch (m) {
146 0, 1 => return y, // atan(+-0, +...)
147 2 => return pi, // atan(+0, -...)
148 3 => return -pi, // atan(-0, -...)
147 0,
148 1 => return y, // atan(+-0, +...)
149 2 => return pi, // atan(+0, -...)
150 3 => return -pi, // atan(-0, -...)
149151 else => unreachable,
150152 }
151153 }
......@@ -161,18 +163,18 @@ fn atan2_64(y: f64, x: f64) f64 {
161163 if (ix == 0x7FF00000) {
162164 if (iy == 0x7FF00000) {
163165 switch (m) {
164 0 => return pi / 4, // atan(+inf, +inf)
165 1 => return -pi / 4, // atan(-inf, +inf)
166 2 => return 3*pi / 4, // atan(+inf, -inf)
167 3 => return -3*pi / 4, // atan(-inf, -inf)
166 0 => return pi / 4, // atan(+inf, +inf)
167 1 => return -pi / 4, // atan(-inf, +inf)
168 2 => return 3 * pi / 4, // atan(+inf, -inf)
169 3 => return -3 * pi / 4, // atan(-inf, -inf)
168170 else => unreachable,
169171 }
170172 } else {
171173 switch (m) {
172 0 => return 0.0, // atan(+..., +inf)
173 1 => return -0.0, // atan(-..., +inf)
174 2 => return pi, // atan(+..., -inf)
175 3 => return -pi, // atan(-...f, -inf)
174 0 => return 0.0, // atan(+..., +inf)
175 1 => return -0.0, // atan(-..., +inf)
176 2 => return pi, // atan(+..., -inf)
177 3 => return -pi, // atan(-...f, -inf)
176178 else => unreachable,
177179 }
178180 }
......@@ -197,10 +199,10 @@ fn atan2_64(y: f64, x: f64) f64 {
197199 };
198200
199201 switch (m) {
200 0 => return z, // atan(+, +)
201 1 => return -z, // atan(-, +)
202 2 => return pi - (z - pi_lo), // atan(+, -)
203 3 => return (z - pi_lo) - pi, // atan(-, -)
202 0 => return z, // atan(+, +)
203 1 => return -z, // atan(-, +)
204 2 => return pi - (z - pi_lo), // atan(+, -)
205 3 => return (z - pi_lo) - pi, // atan(-, -)
204206 else => unreachable,
205207 }
206208}
std/math/cbrt.zig+5-5
......@@ -58,15 +58,15 @@ fn cbrt32(x: f32) f32 {
5858}
5959
6060fn cbrt64(x: f64) f64 {
61 const B1: u32 = 715094163; // (1023 - 1023 / 3 - 0.03306235651 * 2^20
62 const B2: u32 = 696219795; // (1023 - 1023 / 3 - 54 / 3 - 0.03306235651 * 2^20
61 const B1: u32 = 715094163; // (1023 - 1023 / 3 - 0.03306235651 * 2^20
62 const B2: u32 = 696219795; // (1023 - 1023 / 3 - 54 / 3 - 0.03306235651 * 2^20
6363
6464 // |1 / cbrt(x) - p(x)| < 2^(23.5)
65 const P0: f64 = 1.87595182427177009643;
65 const P0: f64 = 1.87595182427177009643;
6666 const P1: f64 = -1.88497979543377169875;
67 const P2: f64 = 1.621429720105354466140;
67 const P2: f64 = 1.621429720105354466140;
6868 const P3: f64 = -0.758397934778766047437;
69 const P4: f64 = 0.145996192886612446982;
69 const P4: f64 = 0.145996192886612446982;
7070
7171 var u = @bitCast(u64, x);
7272 var hx = u32(u >> 32) & 0x7FFFFFFF;
std/math/ceil.zig+2-2
......@@ -56,7 +56,7 @@ fn ceil64(x: f64) f64 {
5656 const e = (u >> 52) & 0x7FF;
5757 var y: f64 = undefined;
5858
59 if (e >= 0x3FF+52 or x == 0) {
59 if (e >= 0x3FF + 52 or x == 0) {
6060 return x;
6161 }
6262
......@@ -68,7 +68,7 @@ fn ceil64(x: f64) f64 {
6868 y = x + math.f64_toint - math.f64_toint - x;
6969 }
7070
71 if (e <= 0x3FF-1) {
71 if (e <= 0x3FF - 1) {
7272 math.forceEval(y);
7373 if (u >> 63 != 0) {
7474 return -0.0;
std/math/complex/exp.zig+11-17
......@@ -19,8 +19,8 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
1919fn exp32(z: &const Complex(f32)) Complex(f32) {
2020 @setFloatMode(this, @import("builtin").FloatMode.Strict);
2121
22 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
23 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
22 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
23 const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2
2424
2525 const x = z.re;
2626 const y = z.im;
......@@ -41,12 +41,10 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
4141 // cexp(finite|nan +- i inf|nan) = nan + i nan
4242 if ((hx & 0x7fffffff) != 0x7f800000) {
4343 return Complex(f32).new(y - y, y - y);
44 }
45 // cexp(-inf +- i inf|nan) = 0 + i0
44 } // cexp(-inf +- i inf|nan) = 0 + i0
4645 else if (hx & 0x80000000 != 0) {
4746 return Complex(f32).new(0, 0);
48 }
49 // cexp(+inf +- i inf|nan) = inf + i nan
47 } // cexp(+inf +- i inf|nan) = inf + i nan
5048 else {
5149 return Complex(f32).new(x, y - y);
5250 }
......@@ -55,8 +53,7 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
5553 // 88.7 <= x <= 192 so must scale
5654 if (hx >= exp_overflow and hx <= cexp_overflow) {
5755 return ldexp_cexp(z, 0);
58 }
59 // - x < exp_overflow => exp(x) won't overflow (common)
56 } // - x < exp_overflow => exp(x) won't overflow (common)
6057 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
6158 // - x = +-inf
6259 // - x = nan
......@@ -67,8 +64,8 @@ fn exp32(z: &const Complex(f32)) Complex(f32) {
6764}
6865
6966fn exp64(z: &const Complex(f64)) Complex(f64) {
70 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
71 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
67 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
68 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
7269
7370 const x = z.re;
7471 const y = z.im;
......@@ -95,12 +92,10 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
9592 // cexp(finite|nan +- i inf|nan) = nan + i nan
9693 if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) {
9794 return Complex(f64).new(y - y, y - y);
98 }
99 // cexp(-inf +- i inf|nan) = 0 + i0
95 } // cexp(-inf +- i inf|nan) = 0 + i0
10096 else if (hx & 0x80000000 != 0) {
10197 return Complex(f64).new(0, 0);
102 }
103 // cexp(+inf +- i inf|nan) = inf + i nan
98 } // cexp(+inf +- i inf|nan) = inf + i nan
10499 else {
105100 return Complex(f64).new(x, y - y);
106101 }
......@@ -109,9 +104,8 @@ fn exp64(z: &const Complex(f64)) Complex(f64) {
109104 // 709.7 <= x <= 1454.3 so must scale
110105 if (hx >= exp_overflow and hx <= cexp_overflow) {
111106 const r = ldexp_cexp(z, 0);
112 return *r;
113 }
114 // - x < exp_overflow => exp(x) won't overflow (common)
107 return r.*;
108 } // - x < exp_overflow => exp(x) won't overflow (common)
115109 // - x > cexp_overflow, so exp(x) * s overflows for s > 0
116110 // - x = +-inf
117111 // - x = nan
std/math/complex/ldexp.zig+7-10
......@@ -15,12 +15,12 @@ pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
1515}
1616
1717fn frexp_exp32(x: f32, expt: &i32) f32 {
18 const k = 235; // reduction constant
19 const kln2 = 162.88958740; // k * ln2
18 const k = 235; // reduction constant
19 const kln2 = 162.88958740; // k * ln2
2020
2121 const exp_x = math.exp(x - kln2);
2222 const hx = @bitCast(u32, exp_x);
23 *expt = i32(hx >> 23) - (0x7f + 127) + k;
23 expt.* = i32(hx >> 23) - (0x7f + 127) + k;
2424 return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
2525}
2626
......@@ -35,15 +35,12 @@ fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) {
3535 const half_expt2 = exptf - half_expt1;
3636 const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
3737
38 return Complex(f32).new(
39 math.cos(z.im) * exp_x * scale1 * scale2,
40 math.sin(z.im) * exp_x * scale1 * scale2,
41 );
38 return Complex(f32).new(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
4239}
4340
4441fn frexp_exp64(x: f64, expt: &i32) f64 {
45 const k = 1799; // reduction constant
46 const kln2 = 1246.97177782734161156; // k * ln2
42 const k = 1799; // reduction constant
43 const kln2 = 1246.97177782734161156; // k * ln2
4744
4845 const exp_x = math.exp(x - kln2);
4946
......@@ -51,7 +48,7 @@ fn frexp_exp64(x: f64, expt: &i32) f64 {
5148 const hx = u32(fx >> 32);
5249 const lx = @truncate(u32, fx);
5350
54 *expt = i32(hx >> 20) - (0x3ff + 1023) + k;
51 expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
5552
5653 const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
5754 return @bitCast(f64, (u64(high_word) << 32) | lx);
std/math/cos.zig+6-6
......@@ -18,20 +18,20 @@ pub fn cos(x: var) @typeOf(x) {
1818}
1919
2020// sin polynomial coefficients
21const S0 = 1.58962301576546568060E-10;
21const S0 = 1.58962301576546568060E-10;
2222const S1 = -2.50507477628578072866E-8;
23const S2 = 2.75573136213857245213E-6;
23const S2 = 2.75573136213857245213E-6;
2424const S3 = -1.98412698295895385996E-4;
25const S4 = 8.33333333332211858878E-3;
25const S4 = 8.33333333332211858878E-3;
2626const S5 = -1.66666666666666307295E-1;
2727
2828// cos polynomial coeffiecients
2929const C0 = -1.13585365213876817300E-11;
30const C1 = 2.08757008419747316778E-9;
30const C1 = 2.08757008419747316778E-9;
3131const C2 = -2.75573141792967388112E-7;
32const C3 = 2.48015872888517045348E-5;
32const C3 = 2.48015872888517045348E-5;
3333const C4 = -1.38888888888730564116E-3;
34const C5 = 4.16666666666665929218E-2;
34const C5 = 4.16666666666665929218E-2;
3535
3636// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
3737//
std/math/floor.zig+2-2
......@@ -57,7 +57,7 @@ fn floor64(x: f64) f64 {
5757 const e = (u >> 52) & 0x7FF;
5858 var y: f64 = undefined;
5959
60 if (e >= 0x3FF+52 or x == 0) {
60 if (e >= 0x3FF + 52 or x == 0) {
6161 return x;
6262 }
6363
......@@ -69,7 +69,7 @@ fn floor64(x: f64) f64 {
6969 y = x + math.f64_toint - math.f64_toint - x;
7070 }
7171
72 if (e <= 0x3FF-1) {
72 if (e <= 0x3FF - 1) {
7373 math.forceEval(y);
7474 if (u >> 63 != 0) {
7575 return -1.0;
std/math/fma.zig+5-2
......@@ -5,7 +5,7 @@ const assert = std.debug.assert;
55pub fn fma(comptime T: type, x: T, y: T, z: T) T {
66 return switch (T) {
77 f32 => fma32(x, y, z),
8 f64 => fma64(x, y ,z),
8 f64 => fma64(x, y, z),
99 else => @compileError("fma not implemented for " ++ @typeName(T)),
1010 };
1111}
......@@ -71,7 +71,10 @@ fn fma64(x: f64, y: f64, z: f64) f64 {
7171 }
7272}
7373
74const dd = struct { hi: f64, lo: f64, };
74const dd = struct {
75 hi: f64,
76 lo: f64,
77};
7578
7679fn dd_add(a: f64, b: f64) dd {
7780 var ret: dd = undefined;
std/math/hypot.zig+4-4
......@@ -39,11 +39,11 @@ fn hypot32(x: f32, y: f32) f32 {
3939 }
4040
4141 var z: f32 = 1.0;
42 if (ux >= (0x7F+60) << 23) {
42 if (ux >= (0x7F + 60) << 23) {
4343 z = 0x1.0p90;
4444 xx *= 0x1.0p-90;
4545 yy *= 0x1.0p-90;
46 } else if (uy < (0x7F-60) << 23) {
46 } else if (uy < (0x7F - 60) << 23) {
4747 z = 0x1.0p-90;
4848 xx *= 0x1.0p-90;
4949 yy *= 0x1.0p-90;
......@@ -57,8 +57,8 @@ fn sq(hi: &f64, lo: &f64, x: f64) void {
5757 const xc = x * split;
5858 const xh = x - xc + xc;
5959 const xl = x - xh;
60 *hi = x * x;
61 *lo = xh * xh - *hi + 2 * xh * xl + xl * xl;
60 hi.* = x * x;
61 lo.* = xh * xh - hi.* + 2 * xh * xl + xl * xl;
6262}
6363
6464fn hypot64(x: f64, y: f64) f64 {
std/math/index.zig+29-46
......@@ -47,12 +47,12 @@ pub fn forceEval(value: var) void {
4747 f32 => {
4848 var x: f32 = undefined;
4949 const p = @ptrCast(&volatile f32, &x);
50 *p = x;
50 p.* = x;
5151 },
5252 f64 => {
5353 var x: f64 = undefined;
5454 const p = @ptrCast(&volatile f64, &x);
55 *p = x;
55 p.* = x;
5656 },
5757 else => {
5858 @compileError("forceEval not implemented for " ++ @typeName(T));
......@@ -179,7 +179,6 @@ test "math" {
179179 _ = @import("complex/index.zig");
180180}
181181
182
183182pub fn min(x: var, y: var) @typeOf(x + y) {
184183 return if (x < y) x else y;
185184}
......@@ -280,10 +279,10 @@ pub fn rotr(comptime T: type, x: T, r: var) T {
280279}
281280
282281test "math.rotr" {
283 assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
284 assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
285 assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
286 assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
282 assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
283 assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
284 assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
285 assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
287286 assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010);
288287}
289288
......@@ -299,14 +298,13 @@ pub fn rotl(comptime T: type, x: T, r: var) T {
299298}
300299
301300test "math.rotl" {
302 assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
303 assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
304 assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
305 assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
301 assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
302 assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
303 assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
304 assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
306305 assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
307306}
308307
309
310308pub fn Log2Int(comptime T: type) type {
311309 return @IntType(false, log2(T.bit_count));
312310}
......@@ -323,14 +321,14 @@ fn testOverflow() void {
323321 assert((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000);
324322}
325323
326
327324pub fn absInt(x: var) !@typeOf(x) {
328325 const T = @typeOf(x);
329326 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
330327 comptime assert(T.is_signed); // must pass a signed integer to absInt
331 if (x == @minValue(@typeOf(x)))
328
329 if (x == @minValue(@typeOf(x))) {
332330 return error.Overflow;
333 {
331 } else {
334332 @setRuntimeSafety(false);
335333 return if (x < 0) -x else x;
336334 }
......@@ -349,10 +347,8 @@ pub const absFloat = @import("fabs.zig").fabs;
349347
350348pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
351349 @setRuntimeSafety(false);
352 if (denominator == 0)
353 return error.DivisionByZero;
354 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
355 return error.Overflow;
350 if (denominator == 0) return error.DivisionByZero;
351 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
356352 return @divTrunc(numerator, denominator);
357353}
358354
......@@ -372,10 +368,8 @@ fn testDivTrunc() void {
372368
373369pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
374370 @setRuntimeSafety(false);
375 if (denominator == 0)
376 return error.DivisionByZero;
377 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
378 return error.Overflow;
371 if (denominator == 0) return error.DivisionByZero;
372 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
379373 return @divFloor(numerator, denominator);
380374}
381375
......@@ -395,13 +389,10 @@ fn testDivFloor() void {
395389
396390pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
397391 @setRuntimeSafety(false);
398 if (denominator == 0)
399 return error.DivisionByZero;
400 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
401 return error.Overflow;
392 if (denominator == 0) return error.DivisionByZero;
393 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
402394 const result = @divTrunc(numerator, denominator);
403 if (result * denominator != numerator)
404 return error.UnexpectedRemainder;
395 if (result * denominator != numerator) return error.UnexpectedRemainder;
405396 return result;
406397}
407398
......@@ -423,10 +414,8 @@ fn testDivExact() void {
423414
424415pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
425416 @setRuntimeSafety(false);
426 if (denominator == 0)
427 return error.DivisionByZero;
428 if (denominator < 0)
429 return error.NegativeDenominator;
417 if (denominator == 0) return error.DivisionByZero;
418 if (denominator < 0) return error.NegativeDenominator;
430419 return @mod(numerator, denominator);
431420}
432421
......@@ -448,10 +437,8 @@ fn testMod() void {
448437
449438pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
450439 @setRuntimeSafety(false);
451 if (denominator == 0)
452 return error.DivisionByZero;
453 if (denominator < 0)
454 return error.NegativeDenominator;
440 if (denominator == 0) return error.DivisionByZero;
441 if (denominator < 0) return error.NegativeDenominator;
455442 return @rem(numerator, denominator);
456443}
457444
......@@ -475,8 +462,7 @@ fn testRem() void {
475462/// Result is an unsigned integer.
476463pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
477464 const uint = @IntType(false, @typeOf(x).bit_count);
478 if (x >= 0)
479 return uint(x);
465 if (x >= 0) return uint(x);
480466
481467 return uint(-(x + 1)) + 1;
482468}
......@@ -495,15 +481,12 @@ test "math.absCast" {
495481/// Returns the negation of the integer parameter.
496482/// Result is a signed integer.
497483pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
498 if (@typeOf(x).is_signed)
499 return negate(x);
484 if (@typeOf(x).is_signed) return negate(x);
500485
501486 const int = @IntType(true, @typeOf(x).bit_count);
502 if (x > -@minValue(int))
503 return error.Overflow;
487 if (x > -@minValue(int)) return error.Overflow;
504488
505 if (x == -@minValue(int))
506 return @minValue(int);
489 if (x == -@minValue(int)) return @minValue(int);
507490
508491 return -int(x);
509492}
......@@ -546,7 +529,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
546529 var x = value;
547530
548531 comptime var i = 1;
549 inline while(T.bit_count > i) : (i *= 2) {
532 inline while (T.bit_count > i) : (i *= 2) {
550533 x |= (x >> i);
551534 }
552535
std/math/ln.zig+2-4
......@@ -120,11 +120,9 @@ pub fn ln_64(x_: f64) f64 {
120120 k -= 54;
121121 x *= 0x1.0p54;
122122 hx = u32(@bitCast(u64, ix) >> 32);
123 }
124 else if (hx >= 0x7FF00000) {
123 } else if (hx >= 0x7FF00000) {
125124 return x;
126 }
127 else if (hx == 0x3FF00000 and ix << 32 == 0) {
125 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
128126 return 0;
129127 }
130128
std/math/log10.zig+8-10
......@@ -35,10 +35,10 @@ pub fn log10(x: var) @typeOf(x) {
3535}
3636
3737pub fn log10_32(x_: f32) f32 {
38 const ivln10hi: f32 = 4.3432617188e-01;
39 const ivln10lo: f32 = -3.1689971365e-05;
40 const log10_2hi: f32 = 3.0102920532e-01;
41 const log10_2lo: f32 = 7.9034151668e-07;
38 const ivln10hi: f32 = 4.3432617188e-01;
39 const ivln10lo: f32 = -3.1689971365e-05;
40 const log10_2hi: f32 = 3.0102920532e-01;
41 const log10_2lo: f32 = 7.9034151668e-07;
4242 const Lg1: f32 = 0xaaaaaa.0p-24;
4343 const Lg2: f32 = 0xccce13.0p-25;
4444 const Lg3: f32 = 0x91e9ee.0p-25;
......@@ -95,8 +95,8 @@ pub fn log10_32(x_: f32) f32 {
9595}
9696
9797pub fn log10_64(x_: f64) f64 {
98 const ivln10hi: f64 = 4.34294481878168880939e-01;
99 const ivln10lo: f64 = 2.50829467116452752298e-11;
98 const ivln10hi: f64 = 4.34294481878168880939e-01;
99 const ivln10lo: f64 = 2.50829467116452752298e-11;
100100 const log10_2hi: f64 = 3.01029995663611771306e-01;
101101 const log10_2lo: f64 = 3.69423907715893078616e-13;
102102 const Lg1: f64 = 6.666666666666735130e-01;
......@@ -126,11 +126,9 @@ pub fn log10_64(x_: f64) f64 {
126126 k -= 54;
127127 x *= 0x1.0p54;
128128 hx = u32(@bitCast(u64, x) >> 32);
129 }
130 else if (hx >= 0x7FF00000) {
129 } else if (hx >= 0x7FF00000) {
131130 return x;
132 }
133 else if (hx == 0x3FF00000 and ix << 32 == 0) {
131 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
134132 return 0;
135133 }
136134
std/math/log2.zig+5-2
......@@ -27,7 +27,10 @@ pub fn log2(x: var) @typeOf(x) {
2727 TypeId.IntLiteral => comptime {
2828 var result = 0;
2929 var x_shifted = x;
30 while (b: {x_shifted >>= 1; break :b x_shifted != 0;}) : (result += 1) {}
30 while (b: {
31 x_shifted >>= 1;
32 break :b x_shifted != 0;
33 }) : (result += 1) {}
3134 return result;
3235 },
3336 TypeId.Int => {
......@@ -38,7 +41,7 @@ pub fn log2(x: var) @typeOf(x) {
3841}
3942
4043pub fn log2_32(x_: f32) f32 {
41 const ivln2hi: f32 = 1.4428710938e+00;
44 const ivln2hi: f32 = 1.4428710938e+00;
4245 const ivln2lo: f32 = -1.7605285393e-04;
4346 const Lg1: f32 = 0xaaaaaa.0p-24;
4447 const Lg2: f32 = 0xccce13.0p-25;
std/math/round.zig+4-4
......@@ -24,13 +24,13 @@ fn round32(x_: f32) f32 {
2424 const e = (u >> 23) & 0xFF;
2525 var y: f32 = undefined;
2626
27 if (e >= 0x7F+23) {
27 if (e >= 0x7F + 23) {
2828 return x;
2929 }
3030 if (u >> 31 != 0) {
3131 x = -x;
3232 }
33 if (e < 0x7F-1) {
33 if (e < 0x7F - 1) {
3434 math.forceEval(x + math.f32_toint);
3535 return 0 * @bitCast(f32, u);
3636 }
......@@ -61,13 +61,13 @@ fn round64(x_: f64) f64 {
6161 const e = (u >> 52) & 0x7FF;
6262 var y: f64 = undefined;
6363
64 if (e >= 0x3FF+52) {
64 if (e >= 0x3FF + 52) {
6565 return x;
6666 }
6767 if (u >> 63 != 0) {
6868 x = -x;
6969 }
70 if (e < 0x3ff-1) {
70 if (e < 0x3ff - 1) {
7171 math.forceEval(x + math.f64_toint);
7272 return 0 * @bitCast(f64, u);
7373 }
std/math/sin.zig+6-6
......@@ -19,20 +19,20 @@ pub fn sin(x: var) @typeOf(x) {
1919}
2020
2121// sin polynomial coefficients
22const S0 = 1.58962301576546568060E-10;
22const S0 = 1.58962301576546568060E-10;
2323const S1 = -2.50507477628578072866E-8;
24const S2 = 2.75573136213857245213E-6;
24const S2 = 2.75573136213857245213E-6;
2525const S3 = -1.98412698295895385996E-4;
26const S4 = 8.33333333332211858878E-3;
26const S4 = 8.33333333332211858878E-3;
2727const S5 = -1.66666666666666307295E-1;
2828
2929// cos polynomial coeffiecients
3030const C0 = -1.13585365213876817300E-11;
31const C1 = 2.08757008419747316778E-9;
31const C1 = 2.08757008419747316778E-9;
3232const C2 = -2.75573141792967388112E-7;
33const C3 = 2.48015872888517045348E-5;
33const C3 = 2.48015872888517045348E-5;
3434const C4 = -1.38888888888730564116E-3;
35const C5 = 4.16666666666665929218E-2;
35const C5 = 4.16666666666665929218E-2;
3636
3737// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
3838//
std/math/tan.zig+3-3
......@@ -19,12 +19,12 @@ pub fn tan(x: var) @typeOf(x) {
1919}
2020
2121const Tp0 = -1.30936939181383777646E4;
22const Tp1 = 1.15351664838587416140E6;
22const Tp1 = 1.15351664838587416140E6;
2323const Tp2 = -1.79565251976484877988E7;
2424
25const Tq1 = 1.36812963470692954678E4;
25const Tq1 = 1.36812963470692954678E4;
2626const Tq2 = -1.32089234440210967447E6;
27const Tq3 = 2.50083801823357915839E7;
27const Tq3 = 2.50083801823357915839E7;
2828const Tq4 = -5.38695755929454629881E7;
2929
3030// NOTE: This is taken from the go stdlib. The musl implementation is much more complex.
std/mem.zig+124-73
......@@ -6,14 +6,14 @@ const builtin = @import("builtin");
66const mem = this;
77
88pub const Allocator = struct {
9 const Error = error {OutOfMemory};
9 const Error = error{OutOfMemory};
1010
1111 /// Allocate byte_count bytes and return them in a slice, with the
1212 /// slice's pointer aligned at least to alignment bytes.
1313 /// The returned newly allocated memory is undefined.
1414 /// `alignment` is guaranteed to be >= 1
1515 /// `alignment` is guaranteed to be a power of 2
16 allocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) Error![]u8,
16 allocFn: fn(self: &Allocator, byte_count: usize, alignment: u29) Error![]u8,
1717
1818 /// If `new_byte_count > old_mem.len`:
1919 /// * `old_mem.len` is the same as what was returned from allocFn or reallocFn.
......@@ -26,10 +26,10 @@ pub const Allocator = struct {
2626 /// The returned newly allocated memory is undefined.
2727 /// `alignment` is guaranteed to be >= 1
2828 /// `alignment` is guaranteed to be a power of 2
29 reallocFn: fn (self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
29 reallocFn: fn(self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
3030
3131 /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
32 freeFn: fn (self: &Allocator, old_mem: []u8) void,
32 freeFn: fn(self: &Allocator, old_mem: []u8) void,
3333
3434 fn create(self: &Allocator, comptime T: type) !&T {
3535 if (@sizeOf(T) == 0) return &{};
......@@ -47,7 +47,7 @@ pub const Allocator = struct {
4747 if (@sizeOf(T) == 0) return &{};
4848 const slice = try self.alloc(T, 1);
4949 const ptr = &slice[0];
50 *ptr = *init;
50 ptr.* = init.*;
5151 return ptr;
5252 }
5353
......@@ -59,9 +59,7 @@ pub const Allocator = struct {
5959 return self.alignedAlloc(T, @alignOf(T), n);
6060 }
6161
62 fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29,
63 n: usize) ![]align(alignment) T
64 {
62 fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
6563 if (n == 0) {
6664 return (&align(alignment) T)(undefined)[0..0];
6765 }
......@@ -70,7 +68,7 @@ pub const Allocator = struct {
7068 assert(byte_slice.len == byte_count);
7169 // This loop gets optimized out in ReleaseFast mode
7270 for (byte_slice) |*byte| {
73 *byte = undefined;
71 byte.* = undefined;
7472 }
7573 return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
7674 }
......@@ -79,9 +77,7 @@ pub const Allocator = struct {
7977 return self.alignedRealloc(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
8078 }
8179
82 fn alignedRealloc(self: &Allocator, comptime T: type, comptime alignment: u29,
83 old_mem: []align(alignment) T, n: usize) ![]align(alignment) T
84 {
80 fn alignedRealloc(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
8581 if (old_mem.len == 0) {
8682 return self.alloc(T, n);
8783 }
......@@ -97,7 +93,7 @@ pub const Allocator = struct {
9793 if (n > old_mem.len) {
9894 // This loop gets optimized out in ReleaseFast mode
9995 for (byte_slice[old_byte_slice.len..]) |*byte| {
100 *byte = undefined;
96 byte.* = undefined;
10197 }
10298 }
10399 return ([]T)(@alignCast(alignment, byte_slice));
......@@ -110,9 +106,7 @@ pub const Allocator = struct {
110106 return self.alignedShrink(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
111107 }
112108
113 fn alignedShrink(self: &Allocator, comptime T: type, comptime alignment: u29,
114 old_mem: []align(alignment) T, n: usize) []align(alignment) T
115 {
109 fn alignedShrink(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
116110 if (n == 0) {
117111 self.free(old_mem);
118112 return old_mem[0..0];
......@@ -131,8 +125,7 @@ pub const Allocator = struct {
131125
132126 fn free(self: &Allocator, memory: var) void {
133127 const bytes = ([]const u8)(memory);
134 if (bytes.len == 0)
135 return;
128 if (bytes.len == 0) return;
136129 const non_const_ptr = @intToPtr(&u8, @ptrToInt(bytes.ptr));
137130 self.freeFn(self, non_const_ptr[0..bytes.len]);
138131 }
......@@ -146,11 +139,13 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
146139 // this and automatically omit safety checks for loops
147140 @setRuntimeSafety(false);
148141 assert(dest.len >= source.len);
149 for (source) |s, i| dest[i] = s;
142 for (source) |s, i|
143 dest[i] = s;
150144}
151145
152146pub fn set(comptime T: type, dest: []T, value: T) void {
153 for (dest) |*d| *d = value;
147 for (dest) |*d|
148 d.* = value;
154149}
155150
156151/// Returns true if lhs < rhs, false otherwise
......@@ -229,8 +224,7 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
229224 var i: usize = slice.len;
230225 while (i != 0) {
231226 i -= 1;
232 if (slice[i] == value)
233 return i;
227 if (slice[i] == value) return i;
234228 }
235229 return null;
236230}
......@@ -238,8 +232,7 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
238232pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
239233 var i: usize = start_index;
240234 while (i < slice.len) : (i += 1) {
241 if (slice[i] == value)
242 return i;
235 if (slice[i] == value) return i;
243236 }
244237 return null;
245238}
......@@ -253,8 +246,7 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
253246 while (i != 0) {
254247 i -= 1;
255248 for (values) |value| {
256 if (slice[i] == value)
257 return i;
249 if (slice[i] == value) return i;
258250 }
259251 }
260252 return null;
......@@ -264,8 +256,7 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val
264256 var i: usize = start_index;
265257 while (i < slice.len) : (i += 1) {
266258 for (values) |value| {
267 if (slice[i] == value)
268 return i;
259 if (slice[i] == value) return i;
269260 }
270261 }
271262 return null;
......@@ -279,28 +270,23 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
279270/// To start looking at a different index, slice the haystack first.
280271/// TODO is there even a better algorithm for this?
281272pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
282 if (needle.len > haystack.len)
283 return null;
273 if (needle.len > haystack.len) return null;
284274
285275 var i: usize = haystack.len - needle.len;
286276 while (true) : (i -= 1) {
287 if (mem.eql(T, haystack[i..i+needle.len], needle))
288 return i;
289 if (i == 0)
290 return null;
277 if (mem.eql(T, haystack[i..i + needle.len], needle)) return i;
278 if (i == 0) return null;
291279 }
292280}
293281
294282// TODO boyer-moore algorithm
295283pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
296 if (needle.len > haystack.len)
297 return null;
284 if (needle.len > haystack.len) return null;
298285
299286 var i: usize = start_index;
300287 const end = haystack.len - needle.len;
301288 while (i <= end) : (i += 1) {
302 if (eql(T, haystack[i .. i + needle.len], needle))
303 return i;
289 if (eql(T, haystack[i..i + needle.len], needle)) return i;
304290 }
305291 return null;
306292}
......@@ -355,9 +341,12 @@ pub fn readIntBE(comptime T: type, bytes: []const u8) T {
355341 }
356342 assert(bytes.len == @sizeOf(T));
357343 var result: T = 0;
358 {comptime var i = 0; inline while (i < @sizeOf(T)) : (i += 1) {
359 result = (result << 8) | T(bytes[i]);
360 }}
344 {
345 comptime var i = 0;
346 inline while (i < @sizeOf(T)) : (i += 1) {
347 result = (result << 8) | T(bytes[i]);
348 }
349 }
361350 return result;
362351}
363352
......@@ -369,9 +358,12 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) T {
369358 }
370359 assert(bytes.len == @sizeOf(T));
371360 var result: T = 0;
372 {comptime var i = 0; inline while (i < @sizeOf(T)) : (i += 1) {
373 result |= T(bytes[i]) << i * 8;
374 }}
361 {
362 comptime var i = 0;
363 inline while (i < @sizeOf(T)) : (i += 1) {
364 result |= T(bytes[i]) << i * 8;
365 }
366 }
375367 return result;
376368}
377369
......@@ -393,7 +385,7 @@ pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) void {
393385 },
394386 builtin.Endian.Little => {
395387 for (buf) |*b| {
396 *b = @truncate(u8, bits);
388 b.* = @truncate(u8, bits);
397389 bits >>= 8;
398390 }
399391 },
......@@ -401,7 +393,6 @@ pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) void {
401393 assert(bits == 0);
402394}
403395
404
405396pub fn hash_slice_u8(k: []const u8) u32 {
406397 // FNV 32-bit hash
407398 var h: u32 = 2166136261;
......@@ -420,7 +411,7 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) bool {
420411/// split(" abc def ghi ", " ")
421412/// Will return slices for "abc", "def", "ghi", null, in that order.
422413pub fn split(buffer: []const u8, split_bytes: []const u8) SplitIterator {
423 return SplitIterator {
414 return SplitIterator{
424415 .index = 0,
425416 .buffer = buffer,
426417 .split_bytes = split_bytes,
......@@ -436,7 +427,7 @@ test "mem.split" {
436427}
437428
438429pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
439 return if (needle.len > haystack.len) false else eql(T, haystack[0 .. needle.len], needle);
430 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
440431}
441432
442433test "mem.startsWith" {
......@@ -445,10 +436,9 @@ test "mem.startsWith" {
445436}
446437
447438pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
448 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);
439 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len..], needle);
449440}
450441
451
452442test "mem.endsWith" {
453443 assert(endsWith(u8, "Needle in haystack", "haystack"));
454444 assert(!endsWith(u8, "Bob", "Bo"));
......@@ -542,29 +532,47 @@ test "testReadInt" {
542532}
543533fn testReadIntImpl() void {
544534 {
545 const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };
546 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
547 assert(readIntBE(u32, bytes) == 0x12345678);
548 assert(readIntBE(i32, bytes) == 0x12345678);
535 const bytes = []u8{
536 0x12,
537 0x34,
538 0x56,
539 0x78,
540 };
541 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
542 assert(readIntBE(u32, bytes) == 0x12345678);
543 assert(readIntBE(i32, bytes) == 0x12345678);
549544 assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
550 assert(readIntLE(u32, bytes) == 0x78563412);
551 assert(readIntLE(i32, bytes) == 0x78563412);
545 assert(readIntLE(u32, bytes) == 0x78563412);
546 assert(readIntLE(i32, bytes) == 0x78563412);
552547 }
553548 {
554 const buf = []u8{0x00, 0x00, 0x12, 0x34};
549 const buf = []u8{
550 0x00,
551 0x00,
552 0x12,
553 0x34,
554 };
555555 const answer = readInt(buf, u64, builtin.Endian.Big);
556556 assert(answer == 0x00001234);
557557 }
558558 {
559 const buf = []u8{0x12, 0x34, 0x00, 0x00};
559 const buf = []u8{
560 0x12,
561 0x34,
562 0x00,
563 0x00,
564 };
560565 const answer = readInt(buf, u64, builtin.Endian.Little);
561566 assert(answer == 0x00003412);
562567 }
563568 {
564 const bytes = []u8{0xff, 0xfe};
565 assert(readIntBE(u16, bytes) == 0xfffe);
569 const bytes = []u8{
570 0xff,
571 0xfe,
572 };
573 assert(readIntBE(u16, bytes) == 0xfffe);
566574 assert(readIntBE(i16, bytes) == -0x0002);
567 assert(readIntLE(u16, bytes) == 0xfeff);
575 assert(readIntLE(u16, bytes) == 0xfeff);
568576 assert(readIntLE(i16, bytes) == -0x0101);
569577 }
570578}
......@@ -577,19 +585,38 @@ fn testWriteIntImpl() void {
577585 var bytes: [4]u8 = undefined;
578586
579587 writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
580 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
588 assert(eql(u8, bytes, []u8{
589 0x12,
590 0x34,
591 0x56,
592 0x78,
593 }));
581594
582595 writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
583 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
596 assert(eql(u8, bytes, []u8{
597 0x12,
598 0x34,
599 0x56,
600 0x78,
601 }));
584602
585603 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
586 assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
604 assert(eql(u8, bytes, []u8{
605 0x00,
606 0x00,
607 0x12,
608 0x34,
609 }));
587610
588611 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
589 assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
612 assert(eql(u8, bytes, []u8{
613 0x34,
614 0x12,
615 0x00,
616 0x00,
617 }));
590618}
591619
592
593620pub fn min(comptime T: type, slice: []const T) T {
594621 var best = slice[0];
595622 for (slice[1..]) |item| {
......@@ -615,9 +642,9 @@ test "mem.max" {
615642}
616643
617644pub fn swap(comptime T: type, a: &T, b: &T) void {
618 const tmp = *a;
619 *a = *b;
620 *b = tmp;
645 const tmp = a.*;
646 a.* = b.*;
647 b.* = tmp;
621648}
622649
623650/// In-place order reversal of a slice
......@@ -630,10 +657,22 @@ pub fn reverse(comptime T: type, items: []T) void {
630657}
631658
632659test "std.mem.reverse" {
633 var arr = []i32{ 5, 3, 1, 2, 4 };
660 var arr = []i32{
661 5,
662 3,
663 1,
664 2,
665 4,
666 };
634667 reverse(i32, arr[0..]);
635668
636 assert(eql(i32, arr, []i32{ 4, 2, 1, 3, 5 }));
669 assert(eql(i32, arr, []i32{
670 4,
671 2,
672 1,
673 3,
674 5,
675 }));
637676}
638677
639678/// In-place rotation of the values in an array ([0 1 2 3] becomes [1 2 3 0] if we rotate by 1)
......@@ -645,10 +684,22 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
645684}
646685
647686test "std.mem.rotate" {
648 var arr = []i32{ 5, 3, 1, 2, 4 };
687 var arr = []i32{
688 5,
689 3,
690 1,
691 2,
692 4,
693 };
649694 rotate(i32, arr[0..], 2);
650695
651 assert(eql(i32, arr, []i32{ 1, 2, 4, 5, 3 }));
696 assert(eql(i32, arr, []i32{
697 1,
698 2,
699 4,
700 5,
701 3,
702 }));
652703}
653704
654705// TODO: When https://github.com/zig-lang/zig/issues/649 is solved these can be done by
std/net.zig+16-24
......@@ -19,37 +19,29 @@ pub const Address = struct {
1919 os_addr: OsAddress,
2020
2121 pub fn initIp4(ip4: u32, port: u16) Address {
22 return Address {
23 .os_addr = posix.sockaddr {
24 .in = posix.sockaddr_in {
25 .family = posix.AF_INET,
26 .port = std.mem.endianSwapIfLe(u16, port),
27 .addr = ip4,
28 .zero = []u8{0} ** 8,
29 },
30 },
31 };
22 return Address{ .os_addr = posix.sockaddr{ .in = posix.sockaddr_in{
23 .family = posix.AF_INET,
24 .port = std.mem.endianSwapIfLe(u16, port),
25 .addr = ip4,
26 .zero = []u8{0} ** 8,
27 } } };
3228 }
3329
3430 pub fn initIp6(ip6: &const Ip6Addr, port: u16) Address {
35 return Address {
31 return Address{
3632 .family = posix.AF_INET6,
37 .os_addr = posix.sockaddr {
38 .in6 = posix.sockaddr_in6 {
39 .family = posix.AF_INET6,
40 .port = std.mem.endianSwapIfLe(u16, port),
41 .flowinfo = 0,
42 .addr = ip6.addr,
43 .scope_id = ip6.scope_id,
44 },
45 },
33 .os_addr = posix.sockaddr{ .in6 = posix.sockaddr_in6{
34 .family = posix.AF_INET6,
35 .port = std.mem.endianSwapIfLe(u16, port),
36 .flowinfo = 0,
37 .addr = ip6.addr,
38 .scope_id = ip6.scope_id,
39 } },
4640 };
4741 }
4842
4943 pub fn initPosix(addr: &const posix.sockaddr) Address {
50 return Address {
51 .os_addr = *addr,
52 };
44 return Address{ .os_addr = addr.* };
5345 }
5446
5547 pub fn format(self: &const Address, out_stream: var) !void {
......@@ -98,7 +90,7 @@ pub fn parseIp4(buf: []const u8) !u32 {
9890 }
9991 } else {
10092 return error.InvalidCharacter;
101 }
93 }
10294 }
10395 if (index == 3 and saw_any_digits) {
10496 out_ptr[index] = x;
std/os/child_process.zig+101-91
......@@ -49,7 +49,7 @@ pub const ChildProcess = struct {
4949 err_pipe: if (is_windows) void else [2]i32,
5050 llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
5151
52 pub const SpawnError = error {
52 pub const SpawnError = error{
5353 ProcessFdQuotaExceeded,
5454 Unexpected,
5555 NotDir,
......@@ -88,7 +88,7 @@ pub const ChildProcess = struct {
8888 const child = try allocator.create(ChildProcess);
8989 errdefer allocator.destroy(child);
9090
91 *child = ChildProcess {
91 child.* = ChildProcess{
9292 .allocator = allocator,
9393 .argv = argv,
9494 .pid = undefined,
......@@ -99,8 +99,10 @@ pub const ChildProcess = struct {
9999 .term = null,
100100 .env_map = null,
101101 .cwd = null,
102 .uid = if (is_windows) {} else null,
103 .gid = if (is_windows) {} else null,
102 .uid = if (is_windows) {} else
103 null,
104 .gid = if (is_windows) {} else
105 null,
104106 .stdin = null,
105107 .stdout = null,
106108 .stderr = null,
......@@ -193,9 +195,7 @@ pub const ChildProcess = struct {
193195
194196 /// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
195197 /// If it succeeds, the caller owns result.stdout and result.stderr memory.
196 pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8,
197 env_map: ?&const BufMap, max_output_size: usize) !ExecResult
198 {
198 pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8, env_map: ?&const BufMap, max_output_size: usize) !ExecResult {
199199 const child = try ChildProcess.init(argv, allocator);
200200 defer child.deinit();
201201
......@@ -218,7 +218,7 @@ pub const ChildProcess = struct {
218218 try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
219219 try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size);
220220
221 return ExecResult {
221 return ExecResult{
222222 .term = try child.wait(),
223223 .stdout = stdout.toOwnedSlice(),
224224 .stderr = stderr.toOwnedSlice(),
......@@ -255,9 +255,9 @@ pub const ChildProcess = struct {
255255 self.term = (SpawnError!Term)(x: {
256256 var exit_code: windows.DWORD = undefined;
257257 if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
258 break :x Term { .Unknown = 0 };
258 break :x Term{ .Unknown = 0 };
259259 } else {
260 break :x Term { .Exited = @bitCast(i32, exit_code)};
260 break :x Term{ .Exited = @bitCast(i32, exit_code) };
261261 }
262262 });
263263
......@@ -288,9 +288,18 @@ pub const ChildProcess = struct {
288288 }
289289
290290 fn cleanupStreams(self: &ChildProcess) void {
291 if (self.stdin) |*stdin| { stdin.close(); self.stdin = null; }
292 if (self.stdout) |*stdout| { stdout.close(); self.stdout = null; }
293 if (self.stderr) |*stderr| { stderr.close(); self.stderr = null; }
291 if (self.stdin) |*stdin| {
292 stdin.close();
293 self.stdin = null;
294 }
295 if (self.stdout) |*stdout| {
296 stdout.close();
297 self.stdout = null;
298 }
299 if (self.stderr) |*stderr| {
300 stderr.close();
301 self.stderr = null;
302 }
294303 }
295304
296305 fn cleanupAfterWait(self: &ChildProcess, status: i32) !Term {
......@@ -317,25 +326,30 @@ pub const ChildProcess = struct {
317326
318327 fn statusToTerm(status: i32) Term {
319328 return if (posix.WIFEXITED(status))
320 Term { .Exited = posix.WEXITSTATUS(status) }
329 Term{ .Exited = posix.WEXITSTATUS(status) }
321330 else if (posix.WIFSIGNALED(status))
322 Term { .Signal = posix.WTERMSIG(status) }
331 Term{ .Signal = posix.WTERMSIG(status) }
323332 else if (posix.WIFSTOPPED(status))
324 Term { .Stopped = posix.WSTOPSIG(status) }
333 Term{ .Stopped = posix.WSTOPSIG(status) }
325334 else
326 Term { .Unknown = status }
327 ;
335 Term{ .Unknown = status };
328336 }
329337
330338 fn spawnPosix(self: &ChildProcess) !void {
331339 const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
332 errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
340 errdefer if (self.stdin_behavior == StdIo.Pipe) {
341 destroyPipe(stdin_pipe);
342 };
333343
334344 const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined;
335 errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
345 errdefer if (self.stdout_behavior == StdIo.Pipe) {
346 destroyPipe(stdout_pipe);
347 };
336348
337349 const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined;
338 errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
350 errdefer if (self.stderr_behavior == StdIo.Pipe) {
351 destroyPipe(stderr_pipe);
352 };
339353
340354 const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
341355 const dev_null_fd = if (any_ignore) blk: {
......@@ -346,7 +360,9 @@ pub const ChildProcess = struct {
346360 } else blk: {
347361 break :blk undefined;
348362 };
349 defer { if (any_ignore) os.close(dev_null_fd); }
363 defer {
364 if (any_ignore) os.close(dev_null_fd);
365 }
350366
351367 var env_map_owned: BufMap = undefined;
352368 var we_own_env_map: bool = undefined;
......@@ -358,7 +374,9 @@ pub const ChildProcess = struct {
358374 env_map_owned = try os.getEnvMap(self.allocator);
359375 break :x &env_map_owned;
360376 };
361 defer { if (we_own_env_map) env_map_owned.deinit(); }
377 defer {
378 if (we_own_env_map) env_map_owned.deinit();
379 }
362380
363381 // This pipe is used to communicate errors between the time of fork
364382 // and execve from the child process to the parent process.
......@@ -369,23 +387,21 @@ pub const ChildProcess = struct {
369387 const pid_err = posix.getErrno(pid_result);
370388 if (pid_err > 0) {
371389 return switch (pid_err) {
372 posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources,
390 posix.EAGAIN,
391 posix.ENOMEM,
392 posix.ENOSYS => error.SystemResources,
373393 else => os.unexpectedErrorPosix(pid_err),
374394 };
375395 }
376396 if (pid_result == 0) {
377397 // we are the child
378398
379 setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch
380 |err| forkChildErrReport(err_pipe[1], err);
381 setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch
382 |err| forkChildErrReport(err_pipe[1], err);
383 setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch
384 |err| forkChildErrReport(err_pipe[1], err);
399 setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
400 setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
401 setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
385402
386403 if (self.cwd) |cwd| {
387 os.changeCurDir(self.allocator, cwd) catch
388 |err| forkChildErrReport(err_pipe[1], err);
404 os.changeCurDir(self.allocator, cwd) catch |err| forkChildErrReport(err_pipe[1], err);
389405 }
390406
391407 if (self.gid) |gid| {
......@@ -396,8 +412,7 @@ pub const ChildProcess = struct {
396412 os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
397413 }
398414
399 os.posixExecve(self.argv, env_map, self.allocator) catch
400 |err| forkChildErrReport(err_pipe[1], err);
415 os.posixExecve(self.argv, env_map, self.allocator) catch |err| forkChildErrReport(err_pipe[1], err);
401416 }
402417
403418 // we are the parent
......@@ -423,37 +438,41 @@ pub const ChildProcess = struct {
423438 self.llnode = LinkedList(&ChildProcess).Node.init(self);
424439 self.term = null;
425440
426 if (self.stdin_behavior == StdIo.Pipe) { os.close(stdin_pipe[0]); }
427 if (self.stdout_behavior == StdIo.Pipe) { os.close(stdout_pipe[1]); }
428 if (self.stderr_behavior == StdIo.Pipe) { os.close(stderr_pipe[1]); }
441 if (self.stdin_behavior == StdIo.Pipe) {
442 os.close(stdin_pipe[0]);
443 }
444 if (self.stdout_behavior == StdIo.Pipe) {
445 os.close(stdout_pipe[1]);
446 }
447 if (self.stderr_behavior == StdIo.Pipe) {
448 os.close(stderr_pipe[1]);
449 }
429450 }
430451
431452 fn spawnWindows(self: &ChildProcess) !void {
432 const saAttr = windows.SECURITY_ATTRIBUTES {
453 const saAttr = windows.SECURITY_ATTRIBUTES{
433454 .nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
434455 .bInheritHandle = windows.TRUE,
435456 .lpSecurityDescriptor = null,
436457 };
437458
438 const any_ignore = (self.stdin_behavior == StdIo.Ignore or
439 self.stdout_behavior == StdIo.Ignore or
440 self.stderr_behavior == StdIo.Ignore);
459 const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
441460
442461 const nul_handle = if (any_ignore) blk: {
443462 const nul_file_path = "NUL";
444463 var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined;
445464 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
446 break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ,
447 windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
465 break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
448466 } else blk: {
449467 break :blk undefined;
450468 };
451 defer { if (any_ignore) os.close(nul_handle); }
469 defer {
470 if (any_ignore) os.close(nul_handle);
471 }
452472 if (any_ignore) {
453473 try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0);
454474 }
455475
456
457476 var g_hChildStd_IN_Rd: ?windows.HANDLE = null;
458477 var g_hChildStd_IN_Wr: ?windows.HANDLE = null;
459478 switch (self.stdin_behavior) {
......@@ -470,7 +489,9 @@ pub const ChildProcess = struct {
470489 g_hChildStd_IN_Rd = null;
471490 },
472491 }
473 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
492 errdefer if (self.stdin_behavior == StdIo.Pipe) {
493 windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr);
494 };
474495
475496 var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
476497 var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
......@@ -488,7 +509,9 @@ pub const ChildProcess = struct {
488509 g_hChildStd_OUT_Wr = null;
489510 },
490511 }
491 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
512 errdefer if (self.stdin_behavior == StdIo.Pipe) {
513 windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr);
514 };
492515
493516 var g_hChildStd_ERR_Rd: ?windows.HANDLE = null;
494517 var g_hChildStd_ERR_Wr: ?windows.HANDLE = null;
......@@ -506,12 +529,14 @@ pub const ChildProcess = struct {
506529 g_hChildStd_ERR_Wr = null;
507530 },
508531 }
509 errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
532 errdefer if (self.stdin_behavior == StdIo.Pipe) {
533 windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr);
534 };
510535
511536 const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv);
512537 defer self.allocator.free(cmd_line);
513538
514 var siStartInfo = windows.STARTUPINFOA {
539 var siStartInfo = windows.STARTUPINFOA{
515540 .cb = @sizeOf(windows.STARTUPINFOA),
516541 .hStdError = g_hChildStd_ERR_Wr,
517542 .hStdOutput = g_hChildStd_OUT_Wr,
......@@ -534,19 +559,11 @@ pub const ChildProcess = struct {
534559 };
535560 var piProcInfo: windows.PROCESS_INFORMATION = undefined;
536561
537 const cwd_slice = if (self.cwd) |cwd|
538 try cstr.addNullByte(self.allocator, cwd)
539 else
540 null
541 ;
562 const cwd_slice = if (self.cwd) |cwd| try cstr.addNullByte(self.allocator, cwd) else null;
542563 defer if (cwd_slice) |cwd| self.allocator.free(cwd);
543564 const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null;
544565
545 const maybe_envp_buf = if (self.env_map) |env_map|
546 try os.createWindowsEnvBlock(self.allocator, env_map)
547 else
548 null
549 ;
566 const maybe_envp_buf = if (self.env_map) |env_map| try os.createWindowsEnvBlock(self.allocator, env_map) else null;
550567 defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf);
551568 const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null;
552569
......@@ -563,11 +580,8 @@ pub const ChildProcess = struct {
563580 };
564581 defer self.allocator.free(app_name);
565582
566 windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
567 &siStartInfo, &piProcInfo) catch |no_path_err|
568 {
569 if (no_path_err != error.FileNotFound)
570 return no_path_err;
583 windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| {
584 if (no_path_err != error.FileNotFound) return no_path_err;
571585
572586 const PATH = try os.getEnvVarOwned(self.allocator, "PATH");
573587 defer self.allocator.free(PATH);
......@@ -577,9 +591,7 @@ pub const ChildProcess = struct {
577591 const joined_path = try os.path.join(self.allocator, search_path, app_name);
578592 defer self.allocator.free(joined_path);
579593
580 if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
581 &siStartInfo, &piProcInfo)) |_|
582 {
594 if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo)) |_| {
583595 break;
584596 } else |err| if (err == error.FileNotFound) {
585597 continue;
......@@ -609,9 +621,15 @@ pub const ChildProcess = struct {
609621 self.thread_handle = piProcInfo.hThread;
610622 self.term = null;
611623
612 if (self.stdin_behavior == StdIo.Pipe) { os.close(??g_hChildStd_IN_Rd); }
613 if (self.stderr_behavior == StdIo.Pipe) { os.close(??g_hChildStd_ERR_Wr); }
614 if (self.stdout_behavior == StdIo.Pipe) { os.close(??g_hChildStd_OUT_Wr); }
624 if (self.stdin_behavior == StdIo.Pipe) {
625 os.close(??g_hChildStd_IN_Rd);
626 }
627 if (self.stderr_behavior == StdIo.Pipe) {
628 os.close(??g_hChildStd_ERR_Wr);
629 }
630 if (self.stdout_behavior == StdIo.Pipe) {
631 os.close(??g_hChildStd_OUT_Wr);
632 }
615633 }
616634
617635 fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void {
......@@ -622,18 +640,14 @@ pub const ChildProcess = struct {
622640 StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno),
623641 }
624642 }
625
626643};
627644
628fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8,
629 lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void
630{
631 if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0,
632 @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0)
633 {
645fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8, lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void {
646 if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
634647 const err = windows.GetLastError();
635648 return switch (err) {
636 windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
649 windows.ERROR.FILE_NOT_FOUND,
650 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
637651 windows.ERROR.INVALID_PARAMETER => unreachable,
638652 windows.ERROR.INVALID_NAME => error.InvalidName,
639653 else => os.unexpectedErrorWindows(err),
......@@ -641,9 +655,6 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
641655 }
642656}
643657
644
645
646
647658/// Caller must dealloc.
648659/// Guarantees a null byte at result[result.len].
649660fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) ![]u8 {
......@@ -653,8 +664,7 @@ fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8)
653664 var buf_stream = &io.BufferOutStream.init(&buf).stream;
654665
655666 for (argv) |arg, arg_i| {
656 if (arg_i != 0)
657 try buf.appendByte(' ');
667 if (arg_i != 0) try buf.appendByte(' ');
658668 if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
659669 try buf.append(arg);
660670 continue;
......@@ -688,7 +698,6 @@ fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) void {
688698 if (wr) |h| os.close(h);
689699}
690700
691
692701// TODO: workaround for bug where the `const` from `&const` is dropped when the type is
693702// a namespace field lookup
694703const SECURITY_ATTRIBUTES = windows.SECURITY_ATTRIBUTES;
......@@ -717,8 +726,8 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S
717726 try windowsMakePipe(&rd_h, &wr_h, sattr);
718727 errdefer windowsDestroyPipe(rd_h, wr_h);
719728 try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
720 *rd = rd_h;
721 *wr = wr_h;
729 rd.* = rd_h;
730 wr.* = wr_h;
722731}
723732
724733fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
......@@ -727,8 +736,8 @@ fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const
727736 try windowsMakePipe(&rd_h, &wr_h, sattr);
728737 errdefer windowsDestroyPipe(rd_h, wr_h);
729738 try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0);
730 *rd = rd_h;
731 *wr = wr_h;
739 rd.* = rd_h;
740 wr.* = wr_h;
732741}
733742
734743fn makePipe() ![2]i32 {
......@@ -736,7 +745,8 @@ fn makePipe() ![2]i32 {
736745 const err = posix.getErrno(posix.pipe(&fds));
737746 if (err > 0) {
738747 return switch (err) {
739 posix.EMFILE, posix.ENFILE => error.SystemResources,
748 posix.EMFILE,
749 posix.ENFILE => error.SystemResources,
740750 else => os.unexpectedErrorPosix(err),
741751 };
742752 }
......@@ -744,8 +754,8 @@ fn makePipe() ![2]i32 {
744754}
745755
746756fn destroyPipe(pipe: &const [2]i32) void {
747 os.close((*pipe)[0]);
748 os.close((*pipe)[1]);
757 os.close((pipe.*)[0]);
758 os.close((pipe.*)[1]);
749759}
750760
751761// Child of fork calls this to report an error to the fork parent.
std/os/darwin.zig+182-100
......@@ -10,33 +10,56 @@ pub const STDIN_FILENO = 0;
1010pub const STDOUT_FILENO = 1;
1111pub const STDERR_FILENO = 2;
1212
13pub const PROT_NONE = 0x00; /// [MC2] no permissions
14pub const PROT_READ = 0x01; /// [MC2] pages can be read
15pub const PROT_WRITE = 0x02; /// [MC2] pages can be written
16pub const PROT_EXEC = 0x04; /// [MC2] pages can be executed
17
18pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
19pub const MAP_FILE = 0x0000; /// map from file (default)
20pub const MAP_FIXED = 0x0010; /// interpret addr exactly
21pub const MAP_HASSEMAPHORE = 0x0200; /// region may contain semaphores
22pub const MAP_PRIVATE = 0x0002; /// changes are private
23pub const MAP_SHARED = 0x0001; /// share changes
24pub const MAP_NOCACHE = 0x0400; /// don't cache pages for this mapping
25pub const MAP_NORESERVE = 0x0040; /// don't reserve needed swap area
13/// [MC2] no permissions
14pub const PROT_NONE = 0x00;
15/// [MC2] pages can be read
16pub const PROT_READ = 0x01;
17/// [MC2] pages can be written
18pub const PROT_WRITE = 0x02;
19/// [MC2] pages can be executed
20pub const PROT_EXEC = 0x04;
21
22/// allocated from memory, swap space
23pub const MAP_ANONYMOUS = 0x1000;
24/// map from file (default)
25pub const MAP_FILE = 0x0000;
26/// interpret addr exactly
27pub const MAP_FIXED = 0x0010;
28/// region may contain semaphores
29pub const MAP_HASSEMAPHORE = 0x0200;
30/// changes are private
31pub const MAP_PRIVATE = 0x0002;
32/// share changes
33pub const MAP_SHARED = 0x0001;
34/// don't cache pages for this mapping
35pub const MAP_NOCACHE = 0x0400;
36/// don't reserve needed swap area
37pub const MAP_NORESERVE = 0x0040;
2638pub const MAP_FAILED = @maxValue(usize);
2739
28pub const WNOHANG = 0x00000001; /// [XSI] no hang in wait/no child to reap
29pub const WUNTRACED = 0x00000002; /// [XSI] notify on stop, untraced child
30
31pub const SA_ONSTACK = 0x0001; /// take signal on signal stack
32pub const SA_RESTART = 0x0002; /// restart system on signal return
33pub const SA_RESETHAND = 0x0004; /// reset to SIG_DFL when taking signal
34pub const SA_NOCLDSTOP = 0x0008; /// do not generate SIGCHLD on child stop
35pub const SA_NODEFER = 0x0010; /// don't mask the signal we're delivering
36pub const SA_NOCLDWAIT = 0x0020; /// don't keep zombies around
37pub const SA_SIGINFO = 0x0040; /// signal handler with SA_SIGINFO args
38pub const SA_USERTRAMP = 0x0100; /// do not bounce off kernel's sigtramp
39pub const SA_64REGSET = 0x0200; /// signal handler with SA_SIGINFO args with 64bit regs information
40/// [XSI] no hang in wait/no child to reap
41pub const WNOHANG = 0x00000001;
42/// [XSI] notify on stop, untraced child
43pub const WUNTRACED = 0x00000002;
44
45/// take signal on signal stack
46pub const SA_ONSTACK = 0x0001;
47/// restart system on signal return
48pub const SA_RESTART = 0x0002;
49/// reset to SIG_DFL when taking signal
50pub const SA_RESETHAND = 0x0004;
51/// do not generate SIGCHLD on child stop
52pub const SA_NOCLDSTOP = 0x0008;
53/// don't mask the signal we're delivering
54pub const SA_NODEFER = 0x0010;
55/// don't keep zombies around
56pub const SA_NOCLDWAIT = 0x0020;
57/// signal handler with SA_SIGINFO args
58pub const SA_SIGINFO = 0x0040;
59/// do not bounce off kernel's sigtramp
60pub const SA_USERTRAMP = 0x0100;
61/// signal handler with SA_SIGINFO args with 64bit regs information
62pub const SA_64REGSET = 0x0200;
4063
4164pub const O_LARGEFILE = 0x0000;
4265pub const O_PATH = 0x0000;
......@@ -46,20 +69,34 @@ pub const X_OK = 1;
4669pub const W_OK = 2;
4770pub const R_OK = 4;
4871
49pub const O_RDONLY = 0x0000; /// open for reading only
50pub const O_WRONLY = 0x0001; /// open for writing only
51pub const O_RDWR = 0x0002; /// open for reading and writing
52pub const O_NONBLOCK = 0x0004; /// do not block on open or for data to become available
53pub const O_APPEND = 0x0008; /// append on each write
54pub const O_CREAT = 0x0200; /// create file if it does not exist
55pub const O_TRUNC = 0x0400; /// truncate size to 0
56pub const O_EXCL = 0x0800; /// error if O_CREAT and the file exists
57pub const O_SHLOCK = 0x0010; /// atomically obtain a shared lock
58pub const O_EXLOCK = 0x0020; /// atomically obtain an exclusive lock
59pub const O_NOFOLLOW = 0x0100; /// do not follow symlinks
60pub const O_SYMLINK = 0x200000; /// allow open of symlinks
61pub const O_EVTONLY = 0x8000; /// descriptor requested for event notifications only
62pub const O_CLOEXEC = 0x1000000; /// mark as close-on-exec
72/// open for reading only
73pub const O_RDONLY = 0x0000;
74/// open for writing only
75pub const O_WRONLY = 0x0001;
76/// open for reading and writing
77pub const O_RDWR = 0x0002;
78/// do not block on open or for data to become available
79pub const O_NONBLOCK = 0x0004;
80/// append on each write
81pub const O_APPEND = 0x0008;
82/// create file if it does not exist
83pub const O_CREAT = 0x0200;
84/// truncate size to 0
85pub const O_TRUNC = 0x0400;
86/// error if O_CREAT and the file exists
87pub const O_EXCL = 0x0800;
88/// atomically obtain a shared lock
89pub const O_SHLOCK = 0x0010;
90/// atomically obtain an exclusive lock
91pub const O_EXLOCK = 0x0020;
92/// do not follow symlinks
93pub const O_NOFOLLOW = 0x0100;
94/// allow open of symlinks
95pub const O_SYMLINK = 0x200000;
96/// descriptor requested for event notifications only
97pub const O_EVTONLY = 0x8000;
98/// mark as close-on-exec
99pub const O_CLOEXEC = 0x1000000;
63100
64101pub const O_ACCMODE = 3;
65102pub const O_ALERT = 536870912;
......@@ -87,52 +124,102 @@ pub const DT_LNK = 10;
87124pub const DT_SOCK = 12;
88125pub const DT_WHT = 14;
89126
90pub const SIG_BLOCK = 1; /// block specified signal set
91pub const SIG_UNBLOCK = 2; /// unblock specified signal set
92pub const SIG_SETMASK = 3; /// set specified signal set
93
94pub const SIGHUP = 1; /// hangup
95pub const SIGINT = 2; /// interrupt
96pub const SIGQUIT = 3; /// quit
97pub const SIGILL = 4; /// illegal instruction (not reset when caught)
98pub const SIGTRAP = 5; /// trace trap (not reset when caught)
99pub const SIGABRT = 6; /// abort()
100pub const SIGPOLL = 7; /// pollable event ([XSR] generated, not supported)
101pub const SIGIOT = SIGABRT; /// compatibility
102pub const SIGEMT = 7; /// EMT instruction
103pub const SIGFPE = 8; /// floating point exception
104pub const SIGKILL = 9; /// kill (cannot be caught or ignored)
105pub const SIGBUS = 10; /// bus error
106pub const SIGSEGV = 11; /// segmentation violation
107pub const SIGSYS = 12; /// bad argument to system call
108pub const SIGPIPE = 13; /// write on a pipe with no one to read it
109pub const SIGALRM = 14; /// alarm clock
110pub const SIGTERM = 15; /// software termination signal from kill
111pub const SIGURG = 16; /// urgent condition on IO channel
112pub const SIGSTOP = 17; /// sendable stop signal not from tty
113pub const SIGTSTP = 18; /// stop signal from tty
114pub const SIGCONT = 19; /// continue a stopped process
115pub const SIGCHLD = 20; /// to parent on child stop or exit
116pub const SIGTTIN = 21; /// to readers pgrp upon background tty read
117pub const SIGTTOU = 22; /// like TTIN for output if (tp->t_local&LTOSTOP)
118pub const SIGIO = 23; /// input/output possible signal
119pub const SIGXCPU = 24; /// exceeded CPU time limit
120pub const SIGXFSZ = 25; /// exceeded file size limit
121pub const SIGVTALRM = 26; /// virtual time alarm
122pub const SIGPROF = 27; /// profiling time alarm
123pub const SIGWINCH = 28; /// window size changes
124pub const SIGINFO = 29; /// information request
125pub const SIGUSR1 = 30; /// user defined signal 1
126pub const SIGUSR2 = 31; /// user defined signal 2
127
128fn wstatus(x: i32) i32 { return x & 0o177; }
127/// block specified signal set
128pub const SIG_BLOCK = 1;
129/// unblock specified signal set
130pub const SIG_UNBLOCK = 2;
131/// set specified signal set
132pub const SIG_SETMASK = 3;
133
134/// hangup
135pub const SIGHUP = 1;
136/// interrupt
137pub const SIGINT = 2;
138/// quit
139pub const SIGQUIT = 3;
140/// illegal instruction (not reset when caught)
141pub const SIGILL = 4;
142/// trace trap (not reset when caught)
143pub const SIGTRAP = 5;
144/// abort()
145pub const SIGABRT = 6;
146/// pollable event ([XSR] generated, not supported)
147pub const SIGPOLL = 7;
148/// compatibility
149pub const SIGIOT = SIGABRT;
150/// EMT instruction
151pub const SIGEMT = 7;
152/// floating point exception
153pub const SIGFPE = 8;
154/// kill (cannot be caught or ignored)
155pub const SIGKILL = 9;
156/// bus error
157pub const SIGBUS = 10;
158/// segmentation violation
159pub const SIGSEGV = 11;
160/// bad argument to system call
161pub const SIGSYS = 12;
162/// write on a pipe with no one to read it
163pub const SIGPIPE = 13;
164/// alarm clock
165pub const SIGALRM = 14;
166/// software termination signal from kill
167pub const SIGTERM = 15;
168/// urgent condition on IO channel
169pub const SIGURG = 16;
170/// sendable stop signal not from tty
171pub const SIGSTOP = 17;
172/// stop signal from tty
173pub const SIGTSTP = 18;
174/// continue a stopped process
175pub const SIGCONT = 19;
176/// to parent on child stop or exit
177pub const SIGCHLD = 20;
178/// to readers pgrp upon background tty read
179pub const SIGTTIN = 21;
180/// like TTIN for output if (tp->t_local&LTOSTOP)
181pub const SIGTTOU = 22;
182/// input/output possible signal
183pub const SIGIO = 23;
184/// exceeded CPU time limit
185pub const SIGXCPU = 24;
186/// exceeded file size limit
187pub const SIGXFSZ = 25;
188/// virtual time alarm
189pub const SIGVTALRM = 26;
190/// profiling time alarm
191pub const SIGPROF = 27;
192/// window size changes
193pub const SIGWINCH = 28;
194/// information request
195pub const SIGINFO = 29;
196/// user defined signal 1
197pub const SIGUSR1 = 30;
198/// user defined signal 2
199pub const SIGUSR2 = 31;
200
201fn wstatus(x: i32) i32 {
202 return x & 0o177;
203}
129204const wstopped = 0o177;
130pub fn WEXITSTATUS(x: i32) i32 { return x >> 8; }
131pub fn WTERMSIG(x: i32) i32 { return wstatus(x); }
132pub fn WSTOPSIG(x: i32) i32 { return x >> 8; }
133pub fn WIFEXITED(x: i32) bool { return wstatus(x) == 0; }
134pub fn WIFSTOPPED(x: i32) bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; }
135pub fn WIFSIGNALED(x: i32) bool { return wstatus(x) != wstopped and wstatus(x) != 0; }
205pub fn WEXITSTATUS(x: i32) i32 {
206 return x >> 8;
207}
208pub fn WTERMSIG(x: i32) i32 {
209 return wstatus(x);
210}
211pub fn WSTOPSIG(x: i32) i32 {
212 return x >> 8;
213}
214pub fn WIFEXITED(x: i32) bool {
215 return wstatus(x) == 0;
216}
217pub fn WIFSTOPPED(x: i32) bool {
218 return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13;
219}
220pub fn WIFSIGNALED(x: i32) bool {
221 return wstatus(x) != wstopped and wstatus(x) != 0;
222}
136223
137224/// Get the errno from a syscall return value, or 0 for no error.
138225pub fn getErrno(r: usize) usize {
......@@ -184,11 +271,8 @@ pub fn write(fd: i32, buf: &const u8, nbyte: usize) usize {
184271 return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte));
185272}
186273
187pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32,
188 offset: isize) usize
189{
190 const ptr_result = c.mmap(@ptrCast(&c_void, address), length,
191 @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
274pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
275 const ptr_result = c.mmap(@ptrCast(&c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
192276 const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
193277 return errnoWrap(isize_result);
194278}
......@@ -202,7 +286,7 @@ pub fn unlink(path: &const u8) usize {
202286}
203287
204288pub fn getcwd(buf: &u8, size: usize) usize {
205 return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(*c._errno())) else 0;
289 return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
206290}
207291
208292pub fn waitpid(pid: i32, status: &i32, options: u32) usize {
......@@ -223,7 +307,6 @@ pub fn pipe(fds: &[2]i32) usize {
223307 return errnoWrap(c.pipe(@ptrCast(&c_int, fds)));
224308}
225309
226
227310pub fn getdirentries64(fd: i32, buf_ptr: &u8, buf_len: usize, basep: &i64) usize {
228311 return errnoWrap(@bitCast(isize, c.__getdirentries64(fd, buf_ptr, buf_len, basep)));
229312}
......@@ -269,7 +352,7 @@ pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
269352}
270353
271354pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) usize {
272 return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(*c._errno())) else 0;
355 return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(c._errno().*)) else 0;
273356}
274357
275358pub fn setreuid(ruid: u32, euid: u32) usize {
......@@ -287,8 +370,8 @@ pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&s
287370pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
288371 assert(sig != SIGKILL);
289372 assert(sig != SIGSTOP);
290 var cact = c.Sigaction {
291 .handler = @ptrCast(extern fn(c_int)void, act.handler),
373 var cact = c.Sigaction{
374 .handler = @ptrCast(extern fn(c_int) void, act.handler),
292375 .sa_flags = @bitCast(c_int, act.flags),
293376 .sa_mask = act.mask,
294377 };
......@@ -298,8 +381,8 @@ pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigacti
298381 return result;
299382 }
300383 if (oact) |old| {
301 *old = Sigaction {
302 .handler = @ptrCast(extern fn(i32)void, coact.handler),
384 old.* = Sigaction{
385 .handler = @ptrCast(extern fn(i32) void, coact.handler),
303386 .flags = @bitCast(u32, coact.sa_flags),
304387 .mask = coact.sa_mask,
305388 };
......@@ -319,23 +402,22 @@ pub const sockaddr = c.sockaddr;
319402
320403/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
321404pub const Sigaction = struct {
322 handler: extern fn(i32)void,
405 handler: extern fn(i32) void,
323406 mask: sigset_t,
324407 flags: u32,
325408};
326409
327410pub fn sigaddset(set: &sigset_t, signo: u5) void {
328 *set |= u32(1) << (signo - 1);
411 set.* |= u32(1) << (signo - 1);
329412}
330413
331414/// Takes the return value from a syscall and formats it back in the way
332415/// that the kernel represents it to libc. Errno was a mistake, let's make
333416/// it go away forever.
334417fn errnoWrap(value: isize) usize {
335 return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value);
418 return @bitCast(usize, if (value == -1) -isize(c._errno().*) else value);
336419}
337420
338
339421pub const timezone = c.timezone;
340422pub const timeval = c.timeval;
341423pub const mach_timebase_info_data = c.mach_timebase_info_data;
std/os/index.zig+38-42
......@@ -137,7 +137,7 @@ pub fn getRandomBytes(buf: []u8) !void {
137137 }
138138 },
139139 Os.zen => {
140 const randomness = []u8 {
140 const randomness = []u8{
141141 42,
142142 1,
143143 7,
......@@ -265,7 +265,7 @@ pub fn posixRead(fd: i32, buf: []u8) !void {
265265 }
266266}
267267
268pub const PosixWriteError = error {
268pub const PosixWriteError = error{
269269 WouldBlock,
270270 FileClosed,
271271 DestinationAddressRequired,
......@@ -310,7 +310,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) !void {
310310 }
311311}
312312
313pub const PosixOpenError = error {
313pub const PosixOpenError = error{
314314 OutOfMemory,
315315 AccessDenied,
316316 FileTooBig,
......@@ -477,7 +477,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, allocator:
477477 return posixExecveErrnoToErr(err);
478478}
479479
480pub const PosixExecveError = error {
480pub const PosixExecveError = error{
481481 SystemResources,
482482 AccessDenied,
483483 InvalidExe,
......@@ -512,7 +512,7 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
512512 };
513513}
514514
515pub var linux_aux_raw = []usize {0} ** 38;
515pub var linux_aux_raw = []usize{0} ** 38;
516516pub var posix_environ_raw: []&u8 = undefined;
517517
518518/// Caller must free result when done.
......@@ -667,7 +667,7 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con
667667 }
668668}
669669
670pub const WindowsSymLinkError = error {
670pub const WindowsSymLinkError = error{
671671 OutOfMemory,
672672 Unexpected,
673673};
......@@ -686,7 +686,7 @@ pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path
686686 }
687687}
688688
689pub const PosixSymLinkError = error {
689pub const PosixSymLinkError = error{
690690 OutOfMemory,
691691 AccessDenied,
692692 DiskQuota,
......@@ -895,7 +895,7 @@ pub const AtomicFile = struct {
895895 else => return err,
896896 };
897897
898 return AtomicFile {
898 return AtomicFile{
899899 .allocator = allocator,
900900 .file = file,
901901 .tmp_path = tmp_path,
......@@ -1087,7 +1087,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) !void {
10871087/// removes it. If it cannot be removed because it is a non-empty directory,
10881088/// this function recursively removes its entries and then tries again.
10891089/// TODO non-recursive implementation
1090const DeleteTreeError = error {
1090const DeleteTreeError = error{
10911091 OutOfMemory,
10921092 AccessDenied,
10931093 FileTooBig,
......@@ -1217,7 +1217,7 @@ pub const Dir = struct {
12171217 Os.ios => 0,
12181218 else => {},
12191219 };
1220 return Dir {
1220 return Dir{
12211221 .allocator = allocator,
12221222 .fd = fd,
12231223 .darwin_seek = darwin_seek_init,
......@@ -1294,7 +1294,7 @@ pub const Dir = struct {
12941294 posix.DT_WHT => Entry.Kind.Whiteout,
12951295 else => Entry.Kind.Unknown,
12961296 };
1297 return Entry {
1297 return Entry{
12981298 .name = name,
12991299 .kind = entry_kind,
13001300 };
......@@ -1355,7 +1355,7 @@ pub const Dir = struct {
13551355 posix.DT_SOCK => Entry.Kind.UnixDomainSocket,
13561356 else => Entry.Kind.Unknown,
13571357 };
1358 return Entry {
1358 return Entry{
13591359 .name = name,
13601360 .kind = entry_kind,
13611361 };
......@@ -1465,7 +1465,7 @@ pub fn posix_setregid(rgid: u32, egid: u32) !void {
14651465 };
14661466}
14671467
1468pub const WindowsGetStdHandleErrs = error {
1468pub const WindowsGetStdHandleErrs = error{
14691469 NoStdHandles,
14701470 Unexpected,
14711471};
......@@ -1489,7 +1489,7 @@ pub const ArgIteratorPosix = struct {
14891489 count: usize,
14901490
14911491 pub fn init() ArgIteratorPosix {
1492 return ArgIteratorPosix {
1492 return ArgIteratorPosix{
14931493 .index = 0,
14941494 .count = raw.len,
14951495 };
......@@ -1522,16 +1522,14 @@ pub const ArgIteratorWindows = struct {
15221522 quote_count: usize,
15231523 seen_quote_count: usize,
15241524
1525 pub const NextError = error {
1526 OutOfMemory,
1527 };
1525 pub const NextError = error{OutOfMemory};
15281526
15291527 pub fn init() ArgIteratorWindows {
15301528 return initWithCmdLine(windows.GetCommandLineA());
15311529 }
15321530
15331531 pub fn initWithCmdLine(cmd_line: &const u8) ArgIteratorWindows {
1534 return ArgIteratorWindows {
1532 return ArgIteratorWindows{
15351533 .index = 0,
15361534 .cmd_line = cmd_line,
15371535 .in_quote = false,
......@@ -1676,9 +1674,7 @@ pub const ArgIterator = struct {
16761674 inner: InnerType,
16771675
16781676 pub fn init() ArgIterator {
1679 return ArgIterator {
1680 .inner = InnerType.init(),
1681 };
1677 return ArgIterator{ .inner = InnerType.init() };
16821678 }
16831679
16841680 pub const NextError = ArgIteratorWindows.NextError;
......@@ -1757,33 +1753,33 @@ pub fn argsFree(allocator: &mem.Allocator, args_alloc: []const []u8) void {
17571753}
17581754
17591755test "windows arg parsing" {
1760 testWindowsCmdLine(c"a b\tc d", [][]const u8 {
1756 testWindowsCmdLine(c"a b\tc d", [][]const u8{
17611757 "a",
17621758 "b",
17631759 "c",
17641760 "d",
17651761 });
1766 testWindowsCmdLine(c"\"abc\" d e", [][]const u8 {
1762 testWindowsCmdLine(c"\"abc\" d e", [][]const u8{
17671763 "abc",
17681764 "d",
17691765 "e",
17701766 });
1771 testWindowsCmdLine(c"a\\\\\\b d\"e f\"g h", [][]const u8 {
1767 testWindowsCmdLine(c"a\\\\\\b d\"e f\"g h", [][]const u8{
17721768 "a\\\\\\b",
17731769 "de fg",
17741770 "h",
17751771 });
1776 testWindowsCmdLine(c"a\\\\\\\"b c d", [][]const u8 {
1772 testWindowsCmdLine(c"a\\\\\\\"b c d", [][]const u8{
17771773 "a\\\"b",
17781774 "c",
17791775 "d",
17801776 });
1781 testWindowsCmdLine(c"a\\\\\\\\\"b c\" d e", [][]const u8 {
1777 testWindowsCmdLine(c"a\\\\\\\\\"b c\" d e", [][]const u8{
17821778 "a\\\\b c",
17831779 "d",
17841780 "e",
17851781 });
1786 testWindowsCmdLine(c"a b\tc \"d f", [][]const u8 {
1782 testWindowsCmdLine(c"a b\tc \"d f", [][]const u8{
17871783 "a",
17881784 "b",
17891785 "c",
......@@ -1791,7 +1787,7 @@ test "windows arg parsing" {
17911787 "f",
17921788 });
17931789
1794 testWindowsCmdLine(c"\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", [][]const u8 {
1790 testWindowsCmdLine(c"\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", [][]const u8{
17951791 ".\\..\\zig-cache\\build",
17961792 "bin\\zig.exe",
17971793 ".\\..",
......@@ -1811,7 +1807,7 @@ fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const
18111807
18121808// TODO make this a build variable that you can set
18131809const unexpected_error_tracing = false;
1814const UnexpectedError = error {
1810const UnexpectedError = error{
18151811 /// The Operating System returned an undocumented error code.
18161812 Unexpected,
18171813};
......@@ -1950,7 +1946,7 @@ pub fn isTty(handle: FileHandle) bool {
19501946 }
19511947}
19521948
1953pub const PosixSocketError = error {
1949pub const PosixSocketError = error{
19541950 /// Permission to create a socket of the specified type and/or
19551951 /// pro‐tocol is denied.
19561952 PermissionDenied,
......@@ -1992,7 +1988,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
19921988 }
19931989}
19941990
1995pub const PosixBindError = error {
1991pub const PosixBindError = error{
19961992 /// The address is protected, and the user is not the superuser.
19971993 /// For UNIX domain sockets: Search permission is denied on a component
19981994 /// of the path prefix.
......@@ -2065,7 +2061,7 @@ pub fn posixBind(fd: i32, addr: &const posix.sockaddr) PosixBindError!void {
20652061 }
20662062}
20672063
2068const PosixListenError = error {
2064const PosixListenError = error{
20692065 /// Another socket is already listening on the same port.
20702066 /// For Internet domain sockets, the socket referred to by sockfd had not previously
20712067 /// been bound to an address and, upon attempting to bind it to an ephemeral port, it
......@@ -2098,7 +2094,7 @@ pub fn posixListen(sockfd: i32, backlog: u32) PosixListenError!void {
20982094 }
20992095}
21002096
2101pub const PosixAcceptError = error {
2097pub const PosixAcceptError = error{
21022098 /// The socket is marked nonblocking and no connections are present to be accepted.
21032099 WouldBlock,
21042100
......@@ -2165,7 +2161,7 @@ pub fn posixAccept(fd: i32, addr: &posix.sockaddr, flags: u32) PosixAcceptError!
21652161 }
21662162}
21672163
2168pub const LinuxEpollCreateError = error {
2164pub const LinuxEpollCreateError = error{
21692165 /// Invalid value specified in flags.
21702166 InvalidSyscall,
21712167
......@@ -2198,7 +2194,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
21982194 }
21992195}
22002196
2201pub const LinuxEpollCtlError = error {
2197pub const LinuxEpollCtlError = error{
22022198 /// epfd or fd is not a valid file descriptor.
22032199 InvalidFileDescriptor,
22042200
......@@ -2271,7 +2267,7 @@ pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usiz
22712267 }
22722268}
22732269
2274pub const PosixGetSockNameError = error {
2270pub const PosixGetSockNameError = error{
22752271 /// Insufficient resources were available in the system to perform the operation.
22762272 SystemResources,
22772273
......@@ -2295,7 +2291,7 @@ pub fn posixGetSockName(sockfd: i32) PosixGetSockNameError!posix.sockaddr {
22952291 }
22962292}
22972293
2298pub const PosixConnectError = error {
2294pub const PosixConnectError = error{
22992295 /// For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket
23002296 /// file, or search permission is denied for one of the directories in the path prefix.
23012297 /// or
......@@ -2485,7 +2481,7 @@ pub const Thread = struct {
24852481 }
24862482};
24872483
2488pub const SpawnThreadError = error {
2484pub const SpawnThreadError = error{
24892485 /// A system-imposed limit on the number of threads was encountered.
24902486 /// There are a number of limits that may trigger this error:
24912487 /// * the RLIMIT_NPROC soft resource limit (set via setrlimit(2)),
......@@ -2533,7 +2529,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25332529 if (@sizeOf(Context) == 0) {
25342530 return startFn({});
25352531 } else {
2536 return startFn(*@ptrCast(&Context, @alignCast(@alignOf(Context), arg)));
2532 return startFn(@ptrCast(&Context, @alignCast(@alignOf(Context), arg)).*);
25372533 }
25382534 }
25392535 };
......@@ -2563,7 +2559,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25632559 if (@sizeOf(Context) == 0) {
25642560 return startFn({});
25652561 } else {
2566 return startFn(*@intToPtr(&const Context, ctx_addr));
2562 return startFn(@intToPtr(&const Context, ctx_addr).*);
25672563 }
25682564 }
25692565 extern fn posixThreadMain(ctx: ?&c_void) ?&c_void {
......@@ -2571,7 +2567,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25712567 _ = startFn({});
25722568 return null;
25732569 } else {
2574 _ = startFn(*@ptrCast(&const Context, @alignCast(@alignOf(Context), ctx)));
2570 _ = startFn(@ptrCast(&const Context, @alignCast(@alignOf(Context), ctx)).*);
25752571 return null;
25762572 }
25772573 }
......@@ -2591,7 +2587,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25912587 stack_end -= stack_end % @alignOf(Context);
25922588 assert(stack_end >= stack_addr);
25932589 const context_ptr = @alignCast(@alignOf(Context), @intToPtr(&Context, stack_end));
2594 *context_ptr = context;
2590 context_ptr.* = context;
25952591 arg = stack_end;
25962592 }
25972593
std/os/linux/index.zig+189-190
......@@ -30,96 +30,95 @@ pub const FUTEX_PRIVATE_FLAG = 128;
3030
3131pub const FUTEX_CLOCK_REALTIME = 256;
3232
33
34pub const PROT_NONE = 0;
35pub const PROT_READ = 1;
36pub const PROT_WRITE = 2;
37pub const PROT_EXEC = 4;
33pub const PROT_NONE = 0;
34pub const PROT_READ = 1;
35pub const PROT_WRITE = 2;
36pub const PROT_EXEC = 4;
3837pub const PROT_GROWSDOWN = 0x01000000;
39pub const PROT_GROWSUP = 0x02000000;
40
41pub const MAP_FAILED = @maxValue(usize);
42pub const MAP_SHARED = 0x01;
43pub const MAP_PRIVATE = 0x02;
44pub const MAP_TYPE = 0x0f;
45pub const MAP_FIXED = 0x10;
46pub const MAP_ANONYMOUS = 0x20;
47pub const MAP_NORESERVE = 0x4000;
48pub const MAP_GROWSDOWN = 0x0100;
49pub const MAP_DENYWRITE = 0x0800;
38pub const PROT_GROWSUP = 0x02000000;
39
40pub const MAP_FAILED = @maxValue(usize);
41pub const MAP_SHARED = 0x01;
42pub const MAP_PRIVATE = 0x02;
43pub const MAP_TYPE = 0x0f;
44pub const MAP_FIXED = 0x10;
45pub const MAP_ANONYMOUS = 0x20;
46pub const MAP_NORESERVE = 0x4000;
47pub const MAP_GROWSDOWN = 0x0100;
48pub const MAP_DENYWRITE = 0x0800;
5049pub const MAP_EXECUTABLE = 0x1000;
51pub const MAP_LOCKED = 0x2000;
52pub const MAP_POPULATE = 0x8000;
53pub const MAP_NONBLOCK = 0x10000;
54pub const MAP_STACK = 0x20000;
55pub const MAP_HUGETLB = 0x40000;
56pub const MAP_FILE = 0;
50pub const MAP_LOCKED = 0x2000;
51pub const MAP_POPULATE = 0x8000;
52pub const MAP_NONBLOCK = 0x10000;
53pub const MAP_STACK = 0x20000;
54pub const MAP_HUGETLB = 0x40000;
55pub const MAP_FILE = 0;
5756
5857pub const F_OK = 0;
5958pub const X_OK = 1;
6059pub const W_OK = 2;
6160pub const R_OK = 4;
6261
63pub const WNOHANG = 1;
64pub const WUNTRACED = 2;
65pub const WSTOPPED = 2;
66pub const WEXITED = 4;
62pub const WNOHANG = 1;
63pub const WUNTRACED = 2;
64pub const WSTOPPED = 2;
65pub const WEXITED = 4;
6766pub const WCONTINUED = 8;
68pub const WNOWAIT = 0x1000000;
69
70pub const SA_NOCLDSTOP = 1;
71pub const SA_NOCLDWAIT = 2;
72pub const SA_SIGINFO = 4;
73pub const SA_ONSTACK = 0x08000000;
74pub const SA_RESTART = 0x10000000;
75pub const SA_NODEFER = 0x40000000;
76pub const SA_RESETHAND = 0x80000000;
77pub const SA_RESTORER = 0x04000000;
78
79pub const SIGHUP = 1;
80pub const SIGINT = 2;
81pub const SIGQUIT = 3;
82pub const SIGILL = 4;
83pub const SIGTRAP = 5;
84pub const SIGABRT = 6;
85pub const SIGIOT = SIGABRT;
86pub const SIGBUS = 7;
87pub const SIGFPE = 8;
88pub const SIGKILL = 9;
89pub const SIGUSR1 = 10;
90pub const SIGSEGV = 11;
91pub const SIGUSR2 = 12;
92pub const SIGPIPE = 13;
93pub const SIGALRM = 14;
94pub const SIGTERM = 15;
67pub const WNOWAIT = 0x1000000;
68
69pub const SA_NOCLDSTOP = 1;
70pub const SA_NOCLDWAIT = 2;
71pub const SA_SIGINFO = 4;
72pub const SA_ONSTACK = 0x08000000;
73pub const SA_RESTART = 0x10000000;
74pub const SA_NODEFER = 0x40000000;
75pub const SA_RESETHAND = 0x80000000;
76pub const SA_RESTORER = 0x04000000;
77
78pub const SIGHUP = 1;
79pub const SIGINT = 2;
80pub const SIGQUIT = 3;
81pub const SIGILL = 4;
82pub const SIGTRAP = 5;
83pub const SIGABRT = 6;
84pub const SIGIOT = SIGABRT;
85pub const SIGBUS = 7;
86pub const SIGFPE = 8;
87pub const SIGKILL = 9;
88pub const SIGUSR1 = 10;
89pub const SIGSEGV = 11;
90pub const SIGUSR2 = 12;
91pub const SIGPIPE = 13;
92pub const SIGALRM = 14;
93pub const SIGTERM = 15;
9594pub const SIGSTKFLT = 16;
96pub const SIGCHLD = 17;
97pub const SIGCONT = 18;
98pub const SIGSTOP = 19;
99pub const SIGTSTP = 20;
100pub const SIGTTIN = 21;
101pub const SIGTTOU = 22;
102pub const SIGURG = 23;
103pub const SIGXCPU = 24;
104pub const SIGXFSZ = 25;
95pub const SIGCHLD = 17;
96pub const SIGCONT = 18;
97pub const SIGSTOP = 19;
98pub const SIGTSTP = 20;
99pub const SIGTTIN = 21;
100pub const SIGTTOU = 22;
101pub const SIGURG = 23;
102pub const SIGXCPU = 24;
103pub const SIGXFSZ = 25;
105104pub const SIGVTALRM = 26;
106pub const SIGPROF = 27;
107pub const SIGWINCH = 28;
108pub const SIGIO = 29;
109pub const SIGPOLL = 29;
110pub const SIGPWR = 30;
111pub const SIGSYS = 31;
105pub const SIGPROF = 27;
106pub const SIGWINCH = 28;
107pub const SIGIO = 29;
108pub const SIGPOLL = 29;
109pub const SIGPWR = 30;
110pub const SIGSYS = 31;
112111pub const SIGUNUSED = SIGSYS;
113112
114113pub const O_RDONLY = 0o0;
115114pub const O_WRONLY = 0o1;
116pub const O_RDWR = 0o2;
115pub const O_RDWR = 0o2;
117116
118117pub const SEEK_SET = 0;
119118pub const SEEK_CUR = 1;
120119pub const SEEK_END = 2;
121120
122pub const SIG_BLOCK = 0;
121pub const SIG_BLOCK = 0;
123122pub const SIG_UNBLOCK = 1;
124123pub const SIG_SETMASK = 2;
125124
......@@ -408,7 +407,6 @@ pub const DT_LNK = 10;
408407pub const DT_SOCK = 12;
409408pub const DT_WHT = 14;
410409
411
412410pub const TCGETS = 0x5401;
413411pub const TCSETS = 0x5402;
414412pub const TCSETSW = 0x5403;
......@@ -539,23 +537,23 @@ pub const MS_BIND = 4096;
539537pub const MS_MOVE = 8192;
540538pub const MS_REC = 16384;
541539pub const MS_SILENT = 32768;
542pub const MS_POSIXACL = (1<<16);
543pub const MS_UNBINDABLE = (1<<17);
544pub const MS_PRIVATE = (1<<18);
545pub const MS_SLAVE = (1<<19);
546pub const MS_SHARED = (1<<20);
547pub const MS_RELATIME = (1<<21);
548pub const MS_KERNMOUNT = (1<<22);
549pub const MS_I_VERSION = (1<<23);
550pub const MS_STRICTATIME = (1<<24);
551pub const MS_LAZYTIME = (1<<25);
552pub const MS_NOREMOTELOCK = (1<<27);
553pub const MS_NOSEC = (1<<28);
554pub const MS_BORN = (1<<29);
555pub const MS_ACTIVE = (1<<30);
556pub const MS_NOUSER = (1<<31);
557
558pub const MS_RMT_MASK = (MS_RDONLY|MS_SYNCHRONOUS|MS_MANDLOCK|MS_I_VERSION|MS_LAZYTIME);
540pub const MS_POSIXACL = (1 << 16);
541pub const MS_UNBINDABLE = (1 << 17);
542pub const MS_PRIVATE = (1 << 18);
543pub const MS_SLAVE = (1 << 19);
544pub const MS_SHARED = (1 << 20);
545pub const MS_RELATIME = (1 << 21);
546pub const MS_KERNMOUNT = (1 << 22);
547pub const MS_I_VERSION = (1 << 23);
548pub const MS_STRICTATIME = (1 << 24);
549pub const MS_LAZYTIME = (1 << 25);
550pub const MS_NOREMOTELOCK = (1 << 27);
551pub const MS_NOSEC = (1 << 28);
552pub const MS_BORN = (1 << 29);
553pub const MS_ACTIVE = (1 << 30);
554pub const MS_NOUSER = (1 << 31);
555
556pub const MS_RMT_MASK = (MS_RDONLY | MS_SYNCHRONOUS | MS_MANDLOCK | MS_I_VERSION | MS_LAZYTIME);
559557
560558pub const MS_MGC_VAL = 0xc0ed0000;
561559pub const MS_MGC_MSK = 0xffff0000;
......@@ -565,7 +563,6 @@ pub const MNT_DETACH = 2;
565563pub const MNT_EXPIRE = 4;
566564pub const UMOUNT_NOFOLLOW = 8;
567565
568
569566pub const S_IFMT = 0o170000;
570567
571568pub const S_IFDIR = 0o040000;
......@@ -626,15 +623,30 @@ pub const TFD_CLOEXEC = O_CLOEXEC;
626623pub const TFD_TIMER_ABSTIME = 1;
627624pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1);
628625
629fn unsigned(s: i32) u32 { return @bitCast(u32, s); }
630fn signed(s: u32) i32 { return @bitCast(i32, s); }
631pub fn WEXITSTATUS(s: i32) i32 { return signed((unsigned(s) & 0xff00) >> 8); }
632pub fn WTERMSIG(s: i32) i32 { return signed(unsigned(s) & 0x7f); }
633pub fn WSTOPSIG(s: i32) i32 { return WEXITSTATUS(s); }
634pub fn WIFEXITED(s: i32) bool { return WTERMSIG(s) == 0; }
635pub fn WIFSTOPPED(s: i32) bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; }
636pub fn WIFSIGNALED(s: i32) bool { return (unsigned(s)&0xffff)-%1 < 0xff; }
637
626fn unsigned(s: i32) u32 {
627 return @bitCast(u32, s);
628}
629fn signed(s: u32) i32 {
630 return @bitCast(i32, s);
631}
632pub fn WEXITSTATUS(s: i32) i32 {
633 return signed((unsigned(s) & 0xff00) >> 8);
634}
635pub fn WTERMSIG(s: i32) i32 {
636 return signed(unsigned(s) & 0x7f);
637}
638pub fn WSTOPSIG(s: i32) i32 {
639 return WEXITSTATUS(s);
640}
641pub fn WIFEXITED(s: i32) bool {
642 return WTERMSIG(s) == 0;
643}
644pub fn WIFSTOPPED(s: i32) bool {
645 return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
646}
647pub fn WIFSIGNALED(s: i32) bool {
648 return (unsigned(s) & 0xffff) -% 1 < 0xff;
649}
638650
639651pub const winsize = extern struct {
640652 ws_row: u16,
......@@ -707,8 +719,7 @@ pub fn umount2(special: &const u8, flags: u32) usize {
707719}
708720
709721pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
710 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd),
711 @bitCast(usize, offset));
722 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), @bitCast(usize, offset));
712723}
713724
714725pub fn munmap(address: usize, length: usize) usize {
......@@ -812,7 +823,8 @@ pub fn clock_gettime(clk_id: i32, tp: &timespec) usize {
812823 if (@ptrToInt(f) != 0) {
813824 const rc = f(clk_id, tp);
814825 switch (rc) {
815 0, @bitCast(usize, isize(-EINVAL)) => return rc,
826 0,
827 @bitCast(usize, isize(-EINVAL)) => return rc,
816828 else => {},
817829 }
818830 }
......@@ -823,8 +835,7 @@ var vdso_clock_gettime = init_vdso_clock_gettime;
823835extern fn init_vdso_clock_gettime(clk: i32, ts: &timespec) usize {
824836 const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM);
825837 var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr);
826 _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f,
827 builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic);
838 _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f, builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic);
828839 if (@ptrToInt(f) == 0) return @bitCast(usize, isize(-ENOSYS));
829840 return f(clk, ts);
830841}
......@@ -918,18 +929,18 @@ pub fn getpid() i32 {
918929}
919930
920931pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) usize {
921 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8);
932 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
922933}
923934
924935pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) usize {
925936 assert(sig >= 1);
926937 assert(sig != SIGKILL);
927938 assert(sig != SIGSTOP);
928 var ksa = k_sigaction {
939 var ksa = k_sigaction{
929940 .handler = act.handler,
930941 .flags = act.flags | SA_RESTORER,
931942 .mask = undefined,
932 .restorer = @ptrCast(extern fn()void, restore_rt),
943 .restorer = @ptrCast(extern fn() void, restore_rt),
933944 };
934945 var ksa_old: k_sigaction = undefined;
935946 @memcpy(@ptrCast(&u8, &ksa.mask), @ptrCast(&const u8, &act.mask), 8);
......@@ -952,22 +963,22 @@ const all_mask = []usize{@maxValue(usize)};
952963const app_mask = []usize{0xfffffffc7fffffff};
953964
954965const k_sigaction = extern struct {
955 handler: extern fn(i32)void,
966 handler: extern fn(i32) void,
956967 flags: usize,
957 restorer: extern fn()void,
968 restorer: extern fn() void,
958969 mask: [2]u32,
959970};
960971
961972/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
962973pub const Sigaction = struct {
963 handler: extern fn(i32)void,
974 handler: extern fn(i32) void,
964975 mask: sigset_t,
965976 flags: u32,
966977};
967978
968pub const SIG_ERR = @intToPtr(extern fn(i32)void, @maxValue(usize));
969pub const SIG_DFL = @intToPtr(extern fn(i32)void, 0);
970pub const SIG_IGN = @intToPtr(extern fn(i32)void, 1);
979pub const SIG_ERR = @intToPtr(extern fn(i32) void, @maxValue(usize));
980pub const SIG_DFL = @intToPtr(extern fn(i32) void, 0);
981pub const SIG_IGN = @intToPtr(extern fn(i32) void, 1);
971982pub const empty_sigset = []usize{0} ** sigset_t.len;
972983
973984pub fn raise(sig: i32) usize {
......@@ -980,25 +991,25 @@ pub fn raise(sig: i32) usize {
980991}
981992
982993fn blockAllSignals(set: &sigset_t) void {
983 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG/8);
994 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8);
984995}
985996
986997fn blockAppSignals(set: &sigset_t) void {
987 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG/8);
998 _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8);
988999}
9891000
9901001fn restoreSignals(set: &sigset_t) void {
991 _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG/8);
1002 _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8);
9921003}
9931004
9941005pub fn sigaddset(set: &sigset_t, sig: u6) void {
9951006 const s = sig - 1;
996 (*set)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
1007 (set.*)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
9971008}
9981009
9991010pub fn sigismember(set: &const sigset_t, sig: u6) bool {
10001011 const s = sig - 1;
1001 return ((*set)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
1012 return ((set.*)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
10021013}
10031014
10041015pub const in_port_t = u16;
......@@ -1062,9 +1073,7 @@ pub fn recvmsg(fd: i32, msg: &msghdr, flags: u32) usize {
10621073 return syscall3(SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
10631074}
10641075
1065pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32,
1066 noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize
1067{
1076pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) usize {
10681077 return syscall6(SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
10691078}
10701079
......@@ -1132,25 +1141,16 @@ pub fn fgetxattr(fd: usize, name: &const u8, value: &void, size: usize) usize {
11321141 return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size);
11331142}
11341143
1135pub fn setxattr(path: &const u8, name: &const u8, value: &const void,
1136 size: usize, flags: usize) usize {
1137
1138 return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value),
1139 size, flags);
1144pub fn setxattr(path: &const u8, name: &const u8, value: &const void, size: usize, flags: usize) usize {
1145 return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
11401146}
11411147
1142pub fn lsetxattr(path: &const u8, name: &const u8, value: &const void,
1143 size: usize, flags: usize) usize {
1144
1145 return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value),
1146 size, flags);
1148pub fn lsetxattr(path: &const u8, name: &const u8, value: &const void, size: usize, flags: usize) usize {
1149 return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
11471150}
11481151
1149pub fn fsetxattr(fd: usize, name: &const u8, value: &const void,
1150 size: usize, flags: usize) usize {
1151
1152 return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value),
1153 size, flags);
1152pub fn fsetxattr(fd: usize, name: &const u8, value: &const void, size: usize, flags: usize) usize {
1153 return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags);
11541154}
11551155
11561156pub fn removexattr(path: &const u8, name: &const u8) usize {
......@@ -1199,7 +1199,7 @@ pub fn timerfd_create(clockid: i32, flags: u32) usize {
11991199
12001200pub const itimerspec = extern struct {
12011201 it_interval: timespec,
1202 it_value: timespec
1202 it_value: timespec,
12031203};
12041204
12051205pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) usize {
......@@ -1211,30 +1211,30 @@ pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_va
12111211}
12121212
12131213pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
1214pub const _LINUX_CAPABILITY_U32S_1 = 1;
1214pub const _LINUX_CAPABILITY_U32S_1 = 1;
12151215
12161216pub const _LINUX_CAPABILITY_VERSION_2 = 0x20071026;
1217pub const _LINUX_CAPABILITY_U32S_2 = 2;
1217pub const _LINUX_CAPABILITY_U32S_2 = 2;
12181218
12191219pub const _LINUX_CAPABILITY_VERSION_3 = 0x20080522;
1220pub const _LINUX_CAPABILITY_U32S_3 = 2;
1220pub const _LINUX_CAPABILITY_U32S_3 = 2;
12211221
1222pub const VFS_CAP_REVISION_MASK = 0xFF000000;
1223pub const VFS_CAP_REVISION_SHIFT = 24;
1224pub const VFS_CAP_FLAGS_MASK = ~VFS_CAP_REVISION_MASK;
1222pub const VFS_CAP_REVISION_MASK = 0xFF000000;
1223pub const VFS_CAP_REVISION_SHIFT = 24;
1224pub const VFS_CAP_FLAGS_MASK = ~VFS_CAP_REVISION_MASK;
12251225pub const VFS_CAP_FLAGS_EFFECTIVE = 0x000001;
12261226
12271227pub const VFS_CAP_REVISION_1 = 0x01000000;
1228pub const VFS_CAP_U32_1 = 1;
1229pub const XATTR_CAPS_SZ_1 = @sizeOf(u32)*(1 + 2*VFS_CAP_U32_1);
1228pub const VFS_CAP_U32_1 = 1;
1229pub const XATTR_CAPS_SZ_1 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_1);
12301230
12311231pub const VFS_CAP_REVISION_2 = 0x02000000;
1232pub const VFS_CAP_U32_2 = 2;
1233pub const XATTR_CAPS_SZ_2 = @sizeOf(u32)*(1 + 2*VFS_CAP_U32_2);
1232pub const VFS_CAP_U32_2 = 2;
1233pub const XATTR_CAPS_SZ_2 = @sizeOf(u32) * (1 + 2 * VFS_CAP_U32_2);
12341234
1235pub const XATTR_CAPS_SZ = XATTR_CAPS_SZ_2;
1236pub const VFS_CAP_U32 = VFS_CAP_U32_2;
1237pub const VFS_CAP_REVISION = VFS_CAP_REVISION_2;
1235pub const XATTR_CAPS_SZ = XATTR_CAPS_SZ_2;
1236pub const VFS_CAP_U32 = VFS_CAP_U32_2;
1237pub const VFS_CAP_REVISION = VFS_CAP_REVISION_2;
12381238
12391239pub const vfs_cap_data = extern struct {
12401240 //all of these are mandated as little endian
......@@ -1245,49 +1245,48 @@ pub const vfs_cap_data = extern struct {
12451245 };
12461246
12471247 magic_etc: u32,
1248 data: [VFS_CAP_U32]Data,
1248 data: [VFS_CAP_U32]Data,
12491249};
12501250
1251
1252pub const CAP_CHOWN = 0;
1253pub const CAP_DAC_OVERRIDE = 1;
1254pub const CAP_DAC_READ_SEARCH = 2;
1255pub const CAP_FOWNER = 3;
1256pub const CAP_FSETID = 4;
1257pub const CAP_KILL = 5;
1258pub const CAP_SETGID = 6;
1259pub const CAP_SETUID = 7;
1260pub const CAP_SETPCAP = 8;
1261pub const CAP_LINUX_IMMUTABLE = 9;
1262pub const CAP_NET_BIND_SERVICE = 10;
1263pub const CAP_NET_BROADCAST = 11;
1264pub const CAP_NET_ADMIN = 12;
1265pub const CAP_NET_RAW = 13;
1266pub const CAP_IPC_LOCK = 14;
1267pub const CAP_IPC_OWNER = 15;
1268pub const CAP_SYS_MODULE = 16;
1269pub const CAP_SYS_RAWIO = 17;
1270pub const CAP_SYS_CHROOT = 18;
1271pub const CAP_SYS_PTRACE = 19;
1272pub const CAP_SYS_PACCT = 20;
1273pub const CAP_SYS_ADMIN = 21;
1274pub const CAP_SYS_BOOT = 22;
1275pub const CAP_SYS_NICE = 23;
1276pub const CAP_SYS_RESOURCE = 24;
1277pub const CAP_SYS_TIME = 25;
1278pub const CAP_SYS_TTY_CONFIG = 26;
1279pub const CAP_MKNOD = 27;
1280pub const CAP_LEASE = 28;
1281pub const CAP_AUDIT_WRITE = 29;
1282pub const CAP_AUDIT_CONTROL = 30;
1283pub const CAP_SETFCAP = 31;
1284pub const CAP_MAC_OVERRIDE = 32;
1285pub const CAP_MAC_ADMIN = 33;
1286pub const CAP_SYSLOG = 34;
1287pub const CAP_WAKE_ALARM = 35;
1288pub const CAP_BLOCK_SUSPEND = 36;
1289pub const CAP_AUDIT_READ = 37;
1290pub const CAP_LAST_CAP = CAP_AUDIT_READ;
1251pub const CAP_CHOWN = 0;
1252pub const CAP_DAC_OVERRIDE = 1;
1253pub const CAP_DAC_READ_SEARCH = 2;
1254pub const CAP_FOWNER = 3;
1255pub const CAP_FSETID = 4;
1256pub const CAP_KILL = 5;
1257pub const CAP_SETGID = 6;
1258pub const CAP_SETUID = 7;
1259pub const CAP_SETPCAP = 8;
1260pub const CAP_LINUX_IMMUTABLE = 9;
1261pub const CAP_NET_BIND_SERVICE = 10;
1262pub const CAP_NET_BROADCAST = 11;
1263pub const CAP_NET_ADMIN = 12;
1264pub const CAP_NET_RAW = 13;
1265pub const CAP_IPC_LOCK = 14;
1266pub const CAP_IPC_OWNER = 15;
1267pub const CAP_SYS_MODULE = 16;
1268pub const CAP_SYS_RAWIO = 17;
1269pub const CAP_SYS_CHROOT = 18;
1270pub const CAP_SYS_PTRACE = 19;
1271pub const CAP_SYS_PACCT = 20;
1272pub const CAP_SYS_ADMIN = 21;
1273pub const CAP_SYS_BOOT = 22;
1274pub const CAP_SYS_NICE = 23;
1275pub const CAP_SYS_RESOURCE = 24;
1276pub const CAP_SYS_TIME = 25;
1277pub const CAP_SYS_TTY_CONFIG = 26;
1278pub const CAP_MKNOD = 27;
1279pub const CAP_LEASE = 28;
1280pub const CAP_AUDIT_WRITE = 29;
1281pub const CAP_AUDIT_CONTROL = 30;
1282pub const CAP_SETFCAP = 31;
1283pub const CAP_MAC_OVERRIDE = 32;
1284pub const CAP_MAC_ADMIN = 33;
1285pub const CAP_SYSLOG = 34;
1286pub const CAP_WAKE_ALARM = 35;
1287pub const CAP_BLOCK_SUSPEND = 36;
1288pub const CAP_AUDIT_READ = 37;
1289pub const CAP_LAST_CAP = CAP_AUDIT_READ;
12911290
12921291pub fn cap_valid(u8: x) bool {
12931292 return x >= 0 and x <= CAP_LAST_CAP;
std/segmented_list.zig+30-24
......@@ -95,7 +95,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
9595
9696 /// Deinitialize with `deinit`
9797 pub fn init(allocator: &Allocator) Self {
98 return Self {
98 return Self{
9999 .allocator = allocator,
100100 .len = 0,
101101 .prealloc_segment = undefined,
......@@ -106,7 +106,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
106106 pub fn deinit(self: &Self) void {
107107 self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
108108 self.allocator.free(self.dynamic_segments);
109 *self = undefined;
109 self.* = undefined;
110110 }
111111
112112 pub fn at(self: &Self, i: usize) &T {
......@@ -120,7 +120,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
120120
121121 pub fn push(self: &Self, item: &const T) !void {
122122 const new_item_ptr = try self.addOne();
123 *new_item_ptr = *item;
123 new_item_ptr.* = item.*;
124124 }
125125
126126 pub fn pushMany(self: &Self, items: []const T) !void {
......@@ -130,11 +130,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
130130 }
131131
132132 pub fn pop(self: &Self) ?T {
133 if (self.len == 0)
134 return null;
133 if (self.len == 0) return null;
135134
136135 const index = self.len - 1;
137 const result = *self.uncheckedAt(index);
136 const result = self.uncheckedAt(index).*;
138137 self.len = index;
139138 return result;
140139 }
......@@ -247,8 +246,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
247246 shelf_size: usize,
248247
249248 pub fn next(it: &Iterator) ?&T {
250 if (it.index >= it.list.len)
251 return null;
249 if (it.index >= it.list.len) return null;
252250 if (it.index < prealloc_item_count) {
253251 const ptr = &it.list.prealloc_segment[it.index];
254252 it.index += 1;
......@@ -272,12 +270,10 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
272270 }
273271
274272 pub fn prev(it: &Iterator) ?&T {
275 if (it.index == 0)
276 return null;
273 if (it.index == 0) return null;
277274
278275 it.index -= 1;
279 if (it.index < prealloc_item_count)
280 return &it.list.prealloc_segment[it.index];
276 if (it.index < prealloc_item_count) return &it.list.prealloc_segment[it.index];
281277
282278 if (it.box_index == 0) {
283279 it.shelf_index -= 1;
......@@ -309,7 +305,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
309305 };
310306
311307 pub fn iterator(self: &Self, start_index: usize) Iterator {
312 var it = Iterator {
308 var it = Iterator{
313309 .list = self,
314310 .index = undefined,
315311 .shelf_index = undefined,
......@@ -339,25 +335,31 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
339335 var list = SegmentedList(i32, prealloc).init(allocator);
340336 defer list.deinit();
341337
342 {var i: usize = 0; while (i < 100) : (i += 1) {
343 try list.push(i32(i + 1));
344 assert(list.len == i + 1);
345 }}
338 {
339 var i: usize = 0;
340 while (i < 100) : (i += 1) {
341 try list.push(i32(i + 1));
342 assert(list.len == i + 1);
343 }
344 }
346345
347 {var i: usize = 0; while (i < 100) : (i += 1) {
348 assert(*list.at(i) == i32(i + 1));
349 }}
346 {
347 var i: usize = 0;
348 while (i < 100) : (i += 1) {
349 assert(list.at(i).* == i32(i + 1));
350 }
351 }
350352
351353 {
352354 var it = list.iterator(0);
353355 var x: i32 = 0;
354356 while (it.next()) |item| {
355357 x += 1;
356 assert(*item == x);
358 assert(item.* == x);
357359 }
358360 assert(x == 100);
359361 while (it.prev()) |item| : (x -= 1) {
360 assert(*item == x);
362 assert(item.* == x);
361363 }
362364 assert(x == 0);
363365 }
......@@ -365,14 +367,18 @@ fn testSegmentedList(comptime prealloc: usize, allocator: &Allocator) !void {
365367 assert(??list.pop() == 100);
366368 assert(list.len == 99);
367369
368 try list.pushMany([]i32 { 1, 2, 3 });
370 try list.pushMany([]i32{
371 1,
372 2,
373 3,
374 });
369375 assert(list.len == 102);
370376 assert(??list.pop() == 3);
371377 assert(??list.pop() == 2);
372378 assert(??list.pop() == 1);
373379 assert(list.len == 99);
374380
375 try list.pushMany([]const i32 {});
381 try list.pushMany([]const i32{});
376382 assert(list.len == 99);
377383
378384 var i: i32 = 99;
std/sort.zig+398-164
......@@ -5,15 +5,18 @@ const math = std.math;
55const builtin = @import("builtin");
66
77/// Stable in-place sort. O(n) best case, O(pow(n, 2)) worst case. O(1) memory (no allocator required).
8pub fn insertionSort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) void {
9 {var i: usize = 1; while (i < items.len) : (i += 1) {
10 const x = items[i];
11 var j: usize = i;
12 while (j > 0 and lessThan(x, items[j - 1])) : (j -= 1) {
13 items[j] = items[j - 1];
8pub fn insertionSort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) void {
9 {
10 var i: usize = 1;
11 while (i < items.len) : (i += 1) {
12 const x = items[i];
13 var j: usize = i;
14 while (j > 0 and lessThan(x, items[j - 1])) : (j -= 1) {
15 items[j] = items[j - 1];
16 }
17 items[j] = x;
1418 }
15 items[j] = x;
16 }}
19 }
1720}
1821
1922const Range = struct {
......@@ -21,7 +24,10 @@ const Range = struct {
2124 end: usize,
2225
2326 fn init(start: usize, end: usize) Range {
24 return Range { .start = start, .end = end };
27 return Range{
28 .start = start,
29 .end = end,
30 };
2531 }
2632
2733 fn length(self: &const Range) usize {
......@@ -29,7 +35,6 @@ const Range = struct {
2935 }
3036};
3137
32
3338const Iterator = struct {
3439 size: usize,
3540 power_of_two: usize,
......@@ -42,7 +47,7 @@ const Iterator = struct {
4247 fn init(size2: usize, min_level: usize) Iterator {
4348 const power_of_two = math.floorPowerOfTwo(usize, size2);
4449 const denominator = power_of_two / min_level;
45 return Iterator {
50 return Iterator{
4651 .numerator = 0,
4752 .decimal = 0,
4853 .size = size2,
......@@ -68,7 +73,10 @@ const Iterator = struct {
6873 self.decimal += 1;
6974 }
7075
71 return Range {.start = start, .end = self.decimal};
76 return Range{
77 .start = start,
78 .end = self.decimal,
79 };
7280 }
7381
7482 fn finished(self: &Iterator) bool {
......@@ -100,7 +108,7 @@ const Pull = struct {
100108
101109/// Stable in-place sort. O(n) best case, O(n*log(n)) worst case and average case. O(1) memory (no allocator required).
102110/// Currently implemented as block sort.
103pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) void {
111pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) void {
104112 // Implementation ported from https://github.com/BonzaiThePenguin/WikiSort/blob/master/WikiSort.c
105113 var cache: [512]T = undefined;
106114
......@@ -123,7 +131,16 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
123131 // http://pages.ripco.net/~jgamble/nw.html
124132 var iterator = Iterator.init(items.len, 4);
125133 while (!iterator.finished()) {
126 var order = []u8{0, 1, 2, 3, 4, 5, 6, 7};
134 var order = []u8{
135 0,
136 1,
137 2,
138 3,
139 4,
140 5,
141 6,
142 7,
143 };
127144 const range = iterator.nextRange();
128145
129146 const sliced_items = items[range.start..];
......@@ -149,56 +166,56 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
149166 swap(T, sliced_items, lessThan, &order, 3, 5);
150167 swap(T, sliced_items, lessThan, &order, 3, 4);
151168 },
152 7 => {
153 swap(T, sliced_items, lessThan, &order, 1, 2);
154 swap(T, sliced_items, lessThan, &order, 3, 4);
155 swap(T, sliced_items, lessThan, &order, 5, 6);
156 swap(T, sliced_items, lessThan, &order, 0, 2);
157 swap(T, sliced_items, lessThan, &order, 3, 5);
158 swap(T, sliced_items, lessThan, &order, 4, 6);
159 swap(T, sliced_items, lessThan, &order, 0, 1);
160 swap(T, sliced_items, lessThan, &order, 4, 5);
161 swap(T, sliced_items, lessThan, &order, 2, 6);
162 swap(T, sliced_items, lessThan, &order, 0, 4);
163 swap(T, sliced_items, lessThan, &order, 1, 5);
164 swap(T, sliced_items, lessThan, &order, 0, 3);
165 swap(T, sliced_items, lessThan, &order, 2, 5);
166 swap(T, sliced_items, lessThan, &order, 1, 3);
167 swap(T, sliced_items, lessThan, &order, 2, 4);
168 swap(T, sliced_items, lessThan, &order, 2, 3);
169 },
170 6 => {
171 swap(T, sliced_items, lessThan, &order, 1, 2);
172 swap(T, sliced_items, lessThan, &order, 4, 5);
173 swap(T, sliced_items, lessThan, &order, 0, 2);
174 swap(T, sliced_items, lessThan, &order, 3, 5);
175 swap(T, sliced_items, lessThan, &order, 0, 1);
176 swap(T, sliced_items, lessThan, &order, 3, 4);
177 swap(T, sliced_items, lessThan, &order, 2, 5);
178 swap(T, sliced_items, lessThan, &order, 0, 3);
179 swap(T, sliced_items, lessThan, &order, 1, 4);
180 swap(T, sliced_items, lessThan, &order, 2, 4);
181 swap(T, sliced_items, lessThan, &order, 1, 3);
182 swap(T, sliced_items, lessThan, &order, 2, 3);
183 },
184 5 => {
185 swap(T, sliced_items, lessThan, &order, 0, 1);
186 swap(T, sliced_items, lessThan, &order, 3, 4);
187 swap(T, sliced_items, lessThan, &order, 2, 4);
188 swap(T, sliced_items, lessThan, &order, 2, 3);
189 swap(T, sliced_items, lessThan, &order, 1, 4);
190 swap(T, sliced_items, lessThan, &order, 0, 3);
191 swap(T, sliced_items, lessThan, &order, 0, 2);
192 swap(T, sliced_items, lessThan, &order, 1, 3);
193 swap(T, sliced_items, lessThan, &order, 1, 2);
194 },
195 4 => {
196 swap(T, sliced_items, lessThan, &order, 0, 1);
197 swap(T, sliced_items, lessThan, &order, 2, 3);
198 swap(T, sliced_items, lessThan, &order, 0, 2);
199 swap(T, sliced_items, lessThan, &order, 1, 3);
200 swap(T, sliced_items, lessThan, &order, 1, 2);
201 },
169 7 => {
170 swap(T, sliced_items, lessThan, &order, 1, 2);
171 swap(T, sliced_items, lessThan, &order, 3, 4);
172 swap(T, sliced_items, lessThan, &order, 5, 6);
173 swap(T, sliced_items, lessThan, &order, 0, 2);
174 swap(T, sliced_items, lessThan, &order, 3, 5);
175 swap(T, sliced_items, lessThan, &order, 4, 6);
176 swap(T, sliced_items, lessThan, &order, 0, 1);
177 swap(T, sliced_items, lessThan, &order, 4, 5);
178 swap(T, sliced_items, lessThan, &order, 2, 6);
179 swap(T, sliced_items, lessThan, &order, 0, 4);
180 swap(T, sliced_items, lessThan, &order, 1, 5);
181 swap(T, sliced_items, lessThan, &order, 0, 3);
182 swap(T, sliced_items, lessThan, &order, 2, 5);
183 swap(T, sliced_items, lessThan, &order, 1, 3);
184 swap(T, sliced_items, lessThan, &order, 2, 4);
185 swap(T, sliced_items, lessThan, &order, 2, 3);
186 },
187 6 => {
188 swap(T, sliced_items, lessThan, &order, 1, 2);
189 swap(T, sliced_items, lessThan, &order, 4, 5);
190 swap(T, sliced_items, lessThan, &order, 0, 2);
191 swap(T, sliced_items, lessThan, &order, 3, 5);
192 swap(T, sliced_items, lessThan, &order, 0, 1);
193 swap(T, sliced_items, lessThan, &order, 3, 4);
194 swap(T, sliced_items, lessThan, &order, 2, 5);
195 swap(T, sliced_items, lessThan, &order, 0, 3);
196 swap(T, sliced_items, lessThan, &order, 1, 4);
197 swap(T, sliced_items, lessThan, &order, 2, 4);
198 swap(T, sliced_items, lessThan, &order, 1, 3);
199 swap(T, sliced_items, lessThan, &order, 2, 3);
200 },
201 5 => {
202 swap(T, sliced_items, lessThan, &order, 0, 1);
203 swap(T, sliced_items, lessThan, &order, 3, 4);
204 swap(T, sliced_items, lessThan, &order, 2, 4);
205 swap(T, sliced_items, lessThan, &order, 2, 3);
206 swap(T, sliced_items, lessThan, &order, 1, 4);
207 swap(T, sliced_items, lessThan, &order, 0, 3);
208 swap(T, sliced_items, lessThan, &order, 0, 2);
209 swap(T, sliced_items, lessThan, &order, 1, 3);
210 swap(T, sliced_items, lessThan, &order, 1, 2);
211 },
212 4 => {
213 swap(T, sliced_items, lessThan, &order, 0, 1);
214 swap(T, sliced_items, lessThan, &order, 2, 3);
215 swap(T, sliced_items, lessThan, &order, 0, 2);
216 swap(T, sliced_items, lessThan, &order, 1, 3);
217 swap(T, sliced_items, lessThan, &order, 1, 2);
218 },
202219 else => {},
203220 }
204221 }
......@@ -273,7 +290,6 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
273290 // we merged two levels at the same time, so we're done with this level already
274291 // (iterator.nextLevel() is called again at the bottom of this outer merge loop)
275292 _ = iterator.nextLevel();
276
277293 } else {
278294 iterator.begin();
279295 while (!iterator.finished()) {
......@@ -303,7 +319,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
303319 // 8. redistribute the two internal buffers back into the items
304320
305321 var block_size: usize = math.sqrt(iterator.length());
306 var buffer_size = iterator.length()/block_size + 1;
322 var buffer_size = iterator.length() / block_size + 1;
307323
308324 // as an optimization, we really only need to pull out the internal buffers once for each level of merges
309325 // after that we can reuse the same buffers over and over, then redistribute it when we're finished with this level
......@@ -316,8 +332,18 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
316332 var start: usize = 0;
317333 var pull_index: usize = 0;
318334 var pull = []Pull{
319 Pull {.from = 0, .to = 0, .count = 0, .range = Range.init(0, 0),},
320 Pull {.from = 0, .to = 0, .count = 0, .range = Range.init(0, 0),},
335 Pull{
336 .from = 0,
337 .to = 0,
338 .count = 0,
339 .range = Range.init(0, 0),
340 },
341 Pull{
342 .from = 0,
343 .to = 0,
344 .count = 0,
345 .range = Range.init(0, 0),
346 },
321347 };
322348
323349 var buffer1 = Range.init(0, 0);
......@@ -355,7 +381,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
355381 // these values will be pulled out to the start of A
356382 last = A.start;
357383 count = 1;
358 while (count < find) : ({last = index; count += 1;}) {
384 while (count < find) : ({
385 last = index;
386 count += 1;
387 }) {
359388 index = findLastForward(T, items, items[last], Range.init(last + 1, A.end), lessThan, find - count);
360389 if (index == A.end) break;
361390 }
......@@ -363,7 +392,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
363392
364393 if (count >= buffer_size) {
365394 // keep track of the range within the items where we'll need to "pull out" these values to create the internal buffer
366 pull[pull_index] = Pull {
395 pull[pull_index] = Pull{
367396 .range = Range.init(A.start, B.end),
368397 .count = count,
369398 .from = index,
......@@ -398,7 +427,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
398427 } else if (pull_index == 0 and count > buffer1.length()) {
399428 // keep track of the largest buffer we were able to find
400429 buffer1 = Range.init(A.start, A.start + count);
401 pull[pull_index] = Pull {
430 pull[pull_index] = Pull{
402431 .range = Range.init(A.start, B.end),
403432 .count = count,
404433 .from = index,
......@@ -410,7 +439,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
410439 // these values will be pulled out to the end of B
411440 last = B.end - 1;
412441 count = 1;
413 while (count < find) : ({last = index - 1; count += 1;}) {
442 while (count < find) : ({
443 last = index - 1;
444 count += 1;
445 }) {
414446 index = findFirstBackward(T, items, items[last], Range.init(B.start, last), lessThan, find - count);
415447 if (index == B.start) break;
416448 }
......@@ -418,7 +450,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
418450
419451 if (count >= buffer_size) {
420452 // keep track of the range within the items where we'll need to "pull out" these values to create the internal buffe
421 pull[pull_index] = Pull {
453 pull[pull_index] = Pull{
422454 .range = Range.init(A.start, B.end),
423455 .count = count,
424456 .from = index,
......@@ -457,7 +489,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
457489 } else if (pull_index == 0 and count > buffer1.length()) {
458490 // keep track of the largest buffer we were able to find
459491 buffer1 = Range.init(B.end - count, B.end);
460 pull[pull_index] = Pull {
492 pull[pull_index] = Pull{
461493 .range = Range.init(A.start, B.end),
462494 .count = count,
463495 .from = index,
......@@ -496,7 +528,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
496528
497529 // adjust block_size and buffer_size based on the values we were able to pull out
498530 buffer_size = buffer1.length();
499 block_size = iterator.length()/buffer_size + 1;
531 block_size = iterator.length() / buffer_size + 1;
500532
501533 // the first buffer NEEDS to be large enough to tag each of the evenly sized A blocks,
502534 // so this was originally here to test the math for adjusting block_size above
......@@ -547,7 +579,10 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
547579 // swap the first value of each A block with the value in buffer1
548580 var indexA = buffer1.start;
549581 index = firstA.end;
550 while (index < blockA.end) : ({indexA += 1; index += block_size;}) {
582 while (index < blockA.end) : ({
583 indexA += 1;
584 index += block_size;
585 }) {
551586 mem.swap(T, &items[indexA], &items[index]);
552587 }
553588
......@@ -626,9 +661,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
626661
627662 // if there are no more A blocks remaining, this step is finished!
628663 blockA.start += block_size;
629 if (blockA.length() == 0)
630 break;
631
664 if (blockA.length() == 0) break;
632665 } else if (blockB.length() < block_size) {
633666 // move the last B block, which is unevenly sized, to before the remaining A blocks, by using a rotation
634667 // the cache is disabled here since it might contain the contents of the previous A block
......@@ -709,7 +742,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
709742}
710743
711744// merge operation without a buffer
712fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const Range, lessThan: fn(&const T,&const T)bool) void {
745fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const Range, lessThan: fn(&const T, &const T) bool) void {
713746 if (A_arg.length() == 0 or B_arg.length() == 0) return;
714747
715748 // this just repeatedly binary searches into B and rotates A into position.
......@@ -730,8 +763,8 @@ fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const
730763 // again, this is NOT a general-purpose solution – it only works well in this case!
731764 // kind of like how the O(n^2) insertion sort is used in some places
732765
733 var A = *A_arg;
734 var B = *B_arg;
766 var A = A_arg.*;
767 var B = B_arg.*;
735768
736769 while (true) {
737770 // find the first place in B where the first item in A needs to be inserted
......@@ -751,7 +784,7 @@ fn mergeInPlace(comptime T: type, items: []T, A_arg: &const Range, B_arg: &const
751784}
752785
753786// merge operation using an internal buffer
754fn mergeInternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, buffer: &const Range) void {
787fn mergeInternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, buffer: &const Range) void {
755788 // whenever we find a value to add to the final array, swap it with the value that's already in that spot
756789 // when this algorithm is finished, 'buffer' will contain its original contents, but in a different order
757790 var A_count: usize = 0;
......@@ -787,9 +820,9 @@ fn blockSwap(comptime T: type, items: []T, start1: usize, start2: usize, block_s
787820
788821// combine a linear search with a binary search to reduce the number of comparisons in situations
789822// where have some idea as to how many unique values there are and where the next value might be
790fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
823fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
791824 if (range.length() == 0) return range.start;
792 const skip = math.max(range.length()/unique, usize(1));
825 const skip = math.max(range.length() / unique, usize(1));
793826
794827 var index = range.start + skip;
795828 while (lessThan(items[index - 1], value)) : (index += skip) {
......@@ -801,9 +834,9 @@ fn findFirstForward(comptime T: type, items: []T, value: &const T, range: &const
801834 return binaryFirst(T, items, value, Range.init(index - skip, index), lessThan);
802835}
803836
804fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
837fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
805838 if (range.length() == 0) return range.start;
806 const skip = math.max(range.length()/unique, usize(1));
839 const skip = math.max(range.length() / unique, usize(1));
807840
808841 var index = range.end - skip;
809842 while (index > range.start and !lessThan(items[index - 1], value)) : (index -= skip) {
......@@ -815,9 +848,9 @@ fn findFirstBackward(comptime T: type, items: []T, value: &const T, range: &cons
815848 return binaryFirst(T, items, value, Range.init(index, index + skip), lessThan);
816849}
817850
818fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
851fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
819852 if (range.length() == 0) return range.start;
820 const skip = math.max(range.length()/unique, usize(1));
853 const skip = math.max(range.length() / unique, usize(1));
821854
822855 var index = range.start + skip;
823856 while (!lessThan(value, items[index - 1])) : (index += skip) {
......@@ -829,9 +862,9 @@ fn findLastForward(comptime T: type, items: []T, value: &const T, range: &const
829862 return binaryLast(T, items, value, Range.init(index - skip, index), lessThan);
830863}
831864
832fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool, unique: usize) usize {
865fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool, unique: usize) usize {
833866 if (range.length() == 0) return range.start;
834 const skip = math.max(range.length()/unique, usize(1));
867 const skip = math.max(range.length() / unique, usize(1));
835868
836869 var index = range.end - skip;
837870 while (index > range.start and lessThan(value, items[index - 1])) : (index -= skip) {
......@@ -843,12 +876,12 @@ fn findLastBackward(comptime T: type, items: []T, value: &const T, range: &const
843876 return binaryLast(T, items, value, Range.init(index, index + skip), lessThan);
844877}
845878
846fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool) usize {
879fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool) usize {
847880 var start = range.start;
848881 var end = range.end - 1;
849882 if (range.start >= range.end) return range.end;
850883 while (start < end) {
851 const mid = start + (end - start)/2;
884 const mid = start + (end - start) / 2;
852885 if (lessThan(items[mid], value)) {
853886 start = mid + 1;
854887 } else {
......@@ -861,12 +894,12 @@ fn binaryFirst(comptime T: type, items: []T, value: &const T, range: &const Rang
861894 return start;
862895}
863896
864fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T,&const T)bool) usize {
897fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range, lessThan: fn(&const T, &const T) bool) usize {
865898 var start = range.start;
866899 var end = range.end - 1;
867900 if (range.start >= range.end) return range.end;
868901 while (start < end) {
869 const mid = start + (end - start)/2;
902 const mid = start + (end - start) / 2;
870903 if (!lessThan(value, items[mid])) {
871904 start = mid + 1;
872905 } else {
......@@ -879,7 +912,7 @@ fn binaryLast(comptime T: type, items: []T, value: &const T, range: &const Range
879912 return start;
880913}
881914
882fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, into: []T) void {
915fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, into: []T) void {
883916 var A_index: usize = A.start;
884917 var B_index: usize = B.start;
885918 const A_last = A.end;
......@@ -909,7 +942,7 @@ fn mergeInto(comptime T: type, from: []T, A: &const Range, B: &const Range, less
909942 }
910943}
911944
912fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T,&const T)bool, cache: []T) void {
945fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range, lessThan: fn(&const T, &const T) bool, cache: []T) void {
913946 // A fits into the cache, so use that instead of the internal buffer
914947 var A_index: usize = 0;
915948 var B_index: usize = B.start;
......@@ -937,29 +970,27 @@ fn mergeExternal(comptime T: type, items: []T, A: &const Range, B: &const Range,
937970 mem.copy(T, items[insert_index..], cache[A_index..A_last]);
938971}
939972
940fn swap(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool, order: &[8]u8, x: usize, y: usize) void {
941 if (lessThan(items[y], items[x]) or
942 ((*order)[x] > (*order)[y] and !lessThan(items[x], items[y])))
943 {
973fn swap(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool, order: &[8]u8, x: usize, y: usize) void {
974 if (lessThan(items[y], items[x]) or ((order.*)[x] > (order.*)[y] and !lessThan(items[x], items[y]))) {
944975 mem.swap(T, &items[x], &items[y]);
945 mem.swap(u8, &(*order)[x], &(*order)[y]);
976 mem.swap(u8, &(order.*)[x], &(order.*)[y]);
946977 }
947978}
948979
949980fn i32asc(lhs: &const i32, rhs: &const i32) bool {
950 return *lhs < *rhs;
981 return lhs.* < rhs.*;
951982}
952983
953984fn i32desc(lhs: &const i32, rhs: &const i32) bool {
954 return *rhs < *lhs;
985 return rhs.* < lhs.*;
955986}
956987
957988fn u8asc(lhs: &const u8, rhs: &const u8) bool {
958 return *lhs < *rhs;
989 return lhs.* < rhs.*;
959990}
960991
961992fn u8desc(lhs: &const u8, rhs: &const u8) bool {
962 return *rhs < *lhs;
993 return rhs.* < lhs.*;
963994}
964995
965996test "stable sort" {
......@@ -967,44 +998,125 @@ test "stable sort" {
967998 comptime testStableSort();
968999}
9691000fn testStableSort() void {
970 var expected = []IdAndValue {
971 IdAndValue{.id = 0, .value = 0},
972 IdAndValue{.id = 1, .value = 0},
973 IdAndValue{.id = 2, .value = 0},
974 IdAndValue{.id = 0, .value = 1},
975 IdAndValue{.id = 1, .value = 1},
976 IdAndValue{.id = 2, .value = 1},
977 IdAndValue{.id = 0, .value = 2},
978 IdAndValue{.id = 1, .value = 2},
979 IdAndValue{.id = 2, .value = 2},
1001 var expected = []IdAndValue{
1002 IdAndValue{
1003 .id = 0,
1004 .value = 0,
1005 },
1006 IdAndValue{
1007 .id = 1,
1008 .value = 0,
1009 },
1010 IdAndValue{
1011 .id = 2,
1012 .value = 0,
1013 },
1014 IdAndValue{
1015 .id = 0,
1016 .value = 1,
1017 },
1018 IdAndValue{
1019 .id = 1,
1020 .value = 1,
1021 },
1022 IdAndValue{
1023 .id = 2,
1024 .value = 1,
1025 },
1026 IdAndValue{
1027 .id = 0,
1028 .value = 2,
1029 },
1030 IdAndValue{
1031 .id = 1,
1032 .value = 2,
1033 },
1034 IdAndValue{
1035 .id = 2,
1036 .value = 2,
1037 },
9801038 };
981 var cases = [][9]IdAndValue {
982 []IdAndValue {
983 IdAndValue{.id = 0, .value = 0},
984 IdAndValue{.id = 0, .value = 1},
985 IdAndValue{.id = 0, .value = 2},
986 IdAndValue{.id = 1, .value = 0},
987 IdAndValue{.id = 1, .value = 1},
988 IdAndValue{.id = 1, .value = 2},
989 IdAndValue{.id = 2, .value = 0},
990 IdAndValue{.id = 2, .value = 1},
991 IdAndValue{.id = 2, .value = 2},
1039 var cases = [][9]IdAndValue{
1040 []IdAndValue{
1041 IdAndValue{
1042 .id = 0,
1043 .value = 0,
1044 },
1045 IdAndValue{
1046 .id = 0,
1047 .value = 1,
1048 },
1049 IdAndValue{
1050 .id = 0,
1051 .value = 2,
1052 },
1053 IdAndValue{
1054 .id = 1,
1055 .value = 0,
1056 },
1057 IdAndValue{
1058 .id = 1,
1059 .value = 1,
1060 },
1061 IdAndValue{
1062 .id = 1,
1063 .value = 2,
1064 },
1065 IdAndValue{
1066 .id = 2,
1067 .value = 0,
1068 },
1069 IdAndValue{
1070 .id = 2,
1071 .value = 1,
1072 },
1073 IdAndValue{
1074 .id = 2,
1075 .value = 2,
1076 },
9921077 },
993 []IdAndValue {
994 IdAndValue{.id = 0, .value = 2},
995 IdAndValue{.id = 0, .value = 1},
996 IdAndValue{.id = 0, .value = 0},
997 IdAndValue{.id = 1, .value = 2},
998 IdAndValue{.id = 1, .value = 1},
999 IdAndValue{.id = 1, .value = 0},
1000 IdAndValue{.id = 2, .value = 2},
1001 IdAndValue{.id = 2, .value = 1},
1002 IdAndValue{.id = 2, .value = 0},
1078 []IdAndValue{
1079 IdAndValue{
1080 .id = 0,
1081 .value = 2,
1082 },
1083 IdAndValue{
1084 .id = 0,
1085 .value = 1,
1086 },
1087 IdAndValue{
1088 .id = 0,
1089 .value = 0,
1090 },
1091 IdAndValue{
1092 .id = 1,
1093 .value = 2,
1094 },
1095 IdAndValue{
1096 .id = 1,
1097 .value = 1,
1098 },
1099 IdAndValue{
1100 .id = 1,
1101 .value = 0,
1102 },
1103 IdAndValue{
1104 .id = 2,
1105 .value = 2,
1106 },
1107 IdAndValue{
1108 .id = 2,
1109 .value = 1,
1110 },
1111 IdAndValue{
1112 .id = 2,
1113 .value = 0,
1114 },
10031115 },
10041116 };
10051117 for (cases) |*case| {
1006 insertionSort(IdAndValue, (*case)[0..], cmpByValue);
1007 for (*case) |item, i| {
1118 insertionSort(IdAndValue, (case.*)[0..], cmpByValue);
1119 for (case.*) |item, i| {
10081120 assert(item.id == expected[i].id);
10091121 assert(item.value == expected[i].value);
10101122 }
......@@ -1019,13 +1131,31 @@ fn cmpByValue(a: &const IdAndValue, b: &const IdAndValue) bool {
10191131}
10201132
10211133test "std.sort" {
1022 const u8cases = [][]const []const u8 {
1023 [][]const u8{"", ""},
1024 [][]const u8{"a", "a"},
1025 [][]const u8{"az", "az"},
1026 [][]const u8{"za", "az"},
1027 [][]const u8{"asdf", "adfs"},
1028 [][]const u8{"one", "eno"},
1134 const u8cases = [][]const []const u8{
1135 [][]const u8{
1136 "",
1137 "",
1138 },
1139 [][]const u8{
1140 "a",
1141 "a",
1142 },
1143 [][]const u8{
1144 "az",
1145 "az",
1146 },
1147 [][]const u8{
1148 "za",
1149 "az",
1150 },
1151 [][]const u8{
1152 "asdf",
1153 "adfs",
1154 },
1155 [][]const u8{
1156 "one",
1157 "eno",
1158 },
10291159 };
10301160
10311161 for (u8cases) |case| {
......@@ -1036,13 +1166,59 @@ test "std.sort" {
10361166 assert(mem.eql(u8, slice, case[1]));
10371167 }
10381168
1039 const i32cases = [][]const []const i32 {
1040 [][]const i32{[]i32{}, []i32{}},
1041 [][]const i32{[]i32{1}, []i32{1}},
1042 [][]const i32{[]i32{0, 1}, []i32{0, 1}},
1043 [][]const i32{[]i32{1, 0}, []i32{0, 1}},
1044 [][]const i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}},
1045 [][]const i32{[]i32{2, 1, 3}, []i32{1, 2, 3}},
1169 const i32cases = [][]const []const i32{
1170 [][]const i32{
1171 []i32{},
1172 []i32{},
1173 },
1174 [][]const i32{
1175 []i32{1},
1176 []i32{1},
1177 },
1178 [][]const i32{
1179 []i32{
1180 0,
1181 1,
1182 },
1183 []i32{
1184 0,
1185 1,
1186 },
1187 },
1188 [][]const i32{
1189 []i32{
1190 1,
1191 0,
1192 },
1193 []i32{
1194 0,
1195 1,
1196 },
1197 },
1198 [][]const i32{
1199 []i32{
1200 1,
1201 -1,
1202 0,
1203 },
1204 []i32{
1205 -1,
1206 0,
1207 1,
1208 },
1209 },
1210 [][]const i32{
1211 []i32{
1212 2,
1213 1,
1214 3,
1215 },
1216 []i32{
1217 1,
1218 2,
1219 3,
1220 },
1221 },
10461222 };
10471223
10481224 for (i32cases) |case| {
......@@ -1055,13 +1231,59 @@ test "std.sort" {
10551231}
10561232
10571233test "std.sort descending" {
1058 const rev_cases = [][]const []const i32 {
1059 [][]const i32{[]i32{}, []i32{}},
1060 [][]const i32{[]i32{1}, []i32{1}},
1061 [][]const i32{[]i32{0, 1}, []i32{1, 0}},
1062 [][]const i32{[]i32{1, 0}, []i32{1, 0}},
1063 [][]const i32{[]i32{1, -1, 0}, []i32{1, 0, -1}},
1064 [][]const i32{[]i32{2, 1, 3}, []i32{3, 2, 1}},
1234 const rev_cases = [][]const []const i32{
1235 [][]const i32{
1236 []i32{},
1237 []i32{},
1238 },
1239 [][]const i32{
1240 []i32{1},
1241 []i32{1},
1242 },
1243 [][]const i32{
1244 []i32{
1245 0,
1246 1,
1247 },
1248 []i32{
1249 1,
1250 0,
1251 },
1252 },
1253 [][]const i32{
1254 []i32{
1255 1,
1256 0,
1257 },
1258 []i32{
1259 1,
1260 0,
1261 },
1262 },
1263 [][]const i32{
1264 []i32{
1265 1,
1266 -1,
1267 0,
1268 },
1269 []i32{
1270 1,
1271 0,
1272 -1,
1273 },
1274 },
1275 [][]const i32{
1276 []i32{
1277 2,
1278 1,
1279 3,
1280 },
1281 []i32{
1282 3,
1283 2,
1284 1,
1285 },
1286 },
10651287 };
10661288
10671289 for (rev_cases) |case| {
......@@ -1074,10 +1296,22 @@ test "std.sort descending" {
10741296}
10751297
10761298test "another sort case" {
1077 var arr = []i32{ 5, 3, 1, 2, 4 };
1299 var arr = []i32{
1300 5,
1301 3,
1302 1,
1303 2,
1304 4,
1305 };
10781306 sort(i32, arr[0..], i32asc);
10791307
1080 assert(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 }));
1308 assert(mem.eql(i32, arr, []i32{
1309 1,
1310 2,
1311 3,
1312 4,
1313 5,
1314 }));
10811315}
10821316
10831317test "sort fuzz testing" {
......@@ -1112,7 +1346,7 @@ fn fuzzTest(rng: &std.rand.Random) void {
11121346 }
11131347}
11141348
1115pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) T {
1349pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) T {
11161350 var i: usize = 0;
11171351 var smallest = items[0];
11181352 for (items[1..]) |item| {
......@@ -1123,7 +1357,7 @@ pub fn min(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const
11231357 return smallest;
11241358}
11251359
1126pub fn max(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T)bool) T {
1360pub fn max(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &const T) bool) T {
11271361 var i: usize = 0;
11281362 var biggest = items[0];
11291363 for (items[1..]) |item| {
std/special/bootstrap.zig+4-4
......@@ -27,10 +27,10 @@ extern fn zen_start() noreturn {
2727nakedcc fn _start() noreturn {
2828 switch (builtin.arch) {
2929 builtin.Arch.x86_64 => {
30 argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
30 argc_ptr = asm ("lea (%%rsp), %[argc]" : [argc] "=r" (-> &usize));
3131 },
3232 builtin.Arch.i386 => {
33 argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
33 argc_ptr = asm ("lea (%%esp), %[argc]" : [argc] "=r" (-> &usize));
3434 },
3535 else => @compileError("unsupported arch"),
3636 }
......@@ -46,7 +46,7 @@ extern fn WinMainCRTStartup() noreturn {
4646}
4747
4848fn posixCallMainAndExit() noreturn {
49 const argc = *argc_ptr;
49 const argc = argc_ptr.*;
5050 const argv = @ptrCast(&&u8, &argc_ptr[1]);
5151 const envp_nullable = @ptrCast(&?&u8, &argv[argc + 1]);
5252 var envp_count: usize = 0;
......@@ -56,7 +56,7 @@ fn posixCallMainAndExit() noreturn {
5656 const auxv = &@ptrCast(&usize, envp.ptr)[envp_count + 1];
5757 var i: usize = 0;
5858 while (auxv[i] != 0) : (i += 2) {
59 if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i+1];
59 if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i + 1];
6060 }
6161 std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size);
6262 }
std/special/compiler_rt/fixuint.zig+2-4
......@@ -36,12 +36,10 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
3636 const significand: rep_t = (aAbs & significandMask) | implicitBit;
3737
3838 // If either the value or the exponent is negative, the result is zero.
39 if (sign == -1 or exponent < 0)
40 return 0;
39 if (sign == -1 or exponent < 0) return 0;
4140
4241 // If the value is too large for the integer type, saturate.
43 if (c_uint(exponent) >= fixuint_t.bit_count)
44 return ~fixuint_t(0);
42 if (c_uint(exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
4543
4644 // If 0 <= exponent < significandBits, right shift to get the result.
4745 // Otherwise, shift left.
std/special/compiler_rt/fixunsdfdi.zig-1
......@@ -9,4 +9,3 @@ pub extern fn __fixunsdfdi(a: f64) u64 {
99test "import fixunsdfdi" {
1010 _ = @import("fixunsdfdi_test.zig");
1111}
12
std/special/compiler_rt/fixunsdfsi.zig-1
......@@ -9,4 +9,3 @@ pub extern fn __fixunsdfsi(a: f64) u32 {
99test "import fixunsdfsi" {
1010 _ = @import("fixunsdfsi_test.zig");
1111}
12
std/special/compiler_rt/fixunssfti.zig-1
......@@ -9,4 +9,3 @@ pub extern fn __fixunssfti(a: f32) u128 {
99test "import fixunssfti" {
1010 _ = @import("fixunssfti_test.zig");
1111}
12
std/special/compiler_rt/fixunstfti.zig-1
......@@ -9,4 +9,3 @@ pub extern fn __fixunstfti(a: f128) u128 {
99test "import fixunstfti" {
1010 _ = @import("fixunstfti_test.zig");
1111}
12
std/special/compiler_rt/index.zig+674-144
......@@ -92,9 +92,10 @@ pub fn setXmm0(comptime T: type, value: T) void {
9292 const aligned_value: T align(16) = value;
9393 asm volatile (
9494 \\movaps (%[ptr]), %%xmm0
95 :
96 : [ptr] "r" (&aligned_value)
97 : "xmm0");
95
96 :
97 : [ptr] "r" (&aligned_value)
98 : "xmm0");
9899}
99100
100101extern fn __udivdi3(a: u64, b: u64) u64 {
......@@ -283,26 +284,27 @@ extern fn __udivmodsi4(a: u32, b: u32, rem: &u32) u32 {
283284 @setRuntimeSafety(is_test);
284285
285286 const d = __udivsi3(a, b);
286 *rem = u32(i32(a) -% (i32(d) * i32(b)));
287 rem.* = u32(i32(a) -% (i32(d) * i32(b)));
287288 return d;
288289}
289290
290
291291extern fn __udivsi3(n: u32, d: u32) u32 {
292292 @setRuntimeSafety(is_test);
293293
294294 const n_uword_bits: c_uint = u32.bit_count;
295295 // special cases
296 if (d == 0)
297 return 0; // ?!
298 if (n == 0)
299 return 0;
296 if (d == 0) return 0; // ?!
297 if (n == 0) return 0;
300298 var sr = @bitCast(c_uint, c_int(@clz(d)) - c_int(@clz(n)));
301299 // 0 <= sr <= n_uword_bits - 1 or sr large
302 if (sr > n_uword_bits - 1) // d > r
300 if (sr > n_uword_bits - 1) {
301 // d > r
303302 return 0;
304 if (sr == n_uword_bits - 1) // d == 1
303 }
304 if (sr == n_uword_bits - 1) {
305 // d == 1
305306 return n;
307 }
306308 sr += 1;
307309 // 1 <= sr <= n_uword_bits - 1
308310 // Not a special case
......@@ -341,139 +343,667 @@ fn test_one_umoddi3(a: u64, b: u64, expected_r: u64) void {
341343}
342344
343345test "test_udivsi3" {
344 const cases = [][3]u32 {
345 []u32{0x00000000, 0x00000001, 0x00000000},
346 []u32{0x00000000, 0x00000002, 0x00000000},
347 []u32{0x00000000, 0x00000003, 0x00000000},
348 []u32{0x00000000, 0x00000010, 0x00000000},
349 []u32{0x00000000, 0x078644FA, 0x00000000},
350 []u32{0x00000000, 0x0747AE14, 0x00000000},
351 []u32{0x00000000, 0x7FFFFFFF, 0x00000000},
352 []u32{0x00000000, 0x80000000, 0x00000000},
353 []u32{0x00000000, 0xFFFFFFFD, 0x00000000},
354 []u32{0x00000000, 0xFFFFFFFE, 0x00000000},
355 []u32{0x00000000, 0xFFFFFFFF, 0x00000000},
356 []u32{0x00000001, 0x00000001, 0x00000001},
357 []u32{0x00000001, 0x00000002, 0x00000000},
358 []u32{0x00000001, 0x00000003, 0x00000000},
359 []u32{0x00000001, 0x00000010, 0x00000000},
360 []u32{0x00000001, 0x078644FA, 0x00000000},
361 []u32{0x00000001, 0x0747AE14, 0x00000000},
362 []u32{0x00000001, 0x7FFFFFFF, 0x00000000},
363 []u32{0x00000001, 0x80000000, 0x00000000},
364 []u32{0x00000001, 0xFFFFFFFD, 0x00000000},
365 []u32{0x00000001, 0xFFFFFFFE, 0x00000000},
366 []u32{0x00000001, 0xFFFFFFFF, 0x00000000},
367 []u32{0x00000002, 0x00000001, 0x00000002},
368 []u32{0x00000002, 0x00000002, 0x00000001},
369 []u32{0x00000002, 0x00000003, 0x00000000},
370 []u32{0x00000002, 0x00000010, 0x00000000},
371 []u32{0x00000002, 0x078644FA, 0x00000000},
372 []u32{0x00000002, 0x0747AE14, 0x00000000},
373 []u32{0x00000002, 0x7FFFFFFF, 0x00000000},
374 []u32{0x00000002, 0x80000000, 0x00000000},
375 []u32{0x00000002, 0xFFFFFFFD, 0x00000000},
376 []u32{0x00000002, 0xFFFFFFFE, 0x00000000},
377 []u32{0x00000002, 0xFFFFFFFF, 0x00000000},
378 []u32{0x00000003, 0x00000001, 0x00000003},
379 []u32{0x00000003, 0x00000002, 0x00000001},
380 []u32{0x00000003, 0x00000003, 0x00000001},
381 []u32{0x00000003, 0x00000010, 0x00000000},
382 []u32{0x00000003, 0x078644FA, 0x00000000},
383 []u32{0x00000003, 0x0747AE14, 0x00000000},
384 []u32{0x00000003, 0x7FFFFFFF, 0x00000000},
385 []u32{0x00000003, 0x80000000, 0x00000000},
386 []u32{0x00000003, 0xFFFFFFFD, 0x00000000},
387 []u32{0x00000003, 0xFFFFFFFE, 0x00000000},
388 []u32{0x00000003, 0xFFFFFFFF, 0x00000000},
389 []u32{0x00000010, 0x00000001, 0x00000010},
390 []u32{0x00000010, 0x00000002, 0x00000008},
391 []u32{0x00000010, 0x00000003, 0x00000005},
392 []u32{0x00000010, 0x00000010, 0x00000001},
393 []u32{0x00000010, 0x078644FA, 0x00000000},
394 []u32{0x00000010, 0x0747AE14, 0x00000000},
395 []u32{0x00000010, 0x7FFFFFFF, 0x00000000},
396 []u32{0x00000010, 0x80000000, 0x00000000},
397 []u32{0x00000010, 0xFFFFFFFD, 0x00000000},
398 []u32{0x00000010, 0xFFFFFFFE, 0x00000000},
399 []u32{0x00000010, 0xFFFFFFFF, 0x00000000},
400 []u32{0x078644FA, 0x00000001, 0x078644FA},
401 []u32{0x078644FA, 0x00000002, 0x03C3227D},
402 []u32{0x078644FA, 0x00000003, 0x028216FE},
403 []u32{0x078644FA, 0x00000010, 0x0078644F},
404 []u32{0x078644FA, 0x078644FA, 0x00000001},
405 []u32{0x078644FA, 0x0747AE14, 0x00000001},
406 []u32{0x078644FA, 0x7FFFFFFF, 0x00000000},
407 []u32{0x078644FA, 0x80000000, 0x00000000},
408 []u32{0x078644FA, 0xFFFFFFFD, 0x00000000},
409 []u32{0x078644FA, 0xFFFFFFFE, 0x00000000},
410 []u32{0x078644FA, 0xFFFFFFFF, 0x00000000},
411 []u32{0x0747AE14, 0x00000001, 0x0747AE14},
412 []u32{0x0747AE14, 0x00000002, 0x03A3D70A},
413 []u32{0x0747AE14, 0x00000003, 0x026D3A06},
414 []u32{0x0747AE14, 0x00000010, 0x00747AE1},
415 []u32{0x0747AE14, 0x078644FA, 0x00000000},
416 []u32{0x0747AE14, 0x0747AE14, 0x00000001},
417 []u32{0x0747AE14, 0x7FFFFFFF, 0x00000000},
418 []u32{0x0747AE14, 0x80000000, 0x00000000},
419 []u32{0x0747AE14, 0xFFFFFFFD, 0x00000000},
420 []u32{0x0747AE14, 0xFFFFFFFE, 0x00000000},
421 []u32{0x0747AE14, 0xFFFFFFFF, 0x00000000},
422 []u32{0x7FFFFFFF, 0x00000001, 0x7FFFFFFF},
423 []u32{0x7FFFFFFF, 0x00000002, 0x3FFFFFFF},
424 []u32{0x7FFFFFFF, 0x00000003, 0x2AAAAAAA},
425 []u32{0x7FFFFFFF, 0x00000010, 0x07FFFFFF},
426 []u32{0x7FFFFFFF, 0x078644FA, 0x00000011},
427 []u32{0x7FFFFFFF, 0x0747AE14, 0x00000011},
428 []u32{0x7FFFFFFF, 0x7FFFFFFF, 0x00000001},
429 []u32{0x7FFFFFFF, 0x80000000, 0x00000000},
430 []u32{0x7FFFFFFF, 0xFFFFFFFD, 0x00000000},
431 []u32{0x7FFFFFFF, 0xFFFFFFFE, 0x00000000},
432 []u32{0x7FFFFFFF, 0xFFFFFFFF, 0x00000000},
433 []u32{0x80000000, 0x00000001, 0x80000000},
434 []u32{0x80000000, 0x00000002, 0x40000000},
435 []u32{0x80000000, 0x00000003, 0x2AAAAAAA},
436 []u32{0x80000000, 0x00000010, 0x08000000},
437 []u32{0x80000000, 0x078644FA, 0x00000011},
438 []u32{0x80000000, 0x0747AE14, 0x00000011},
439 []u32{0x80000000, 0x7FFFFFFF, 0x00000001},
440 []u32{0x80000000, 0x80000000, 0x00000001},
441 []u32{0x80000000, 0xFFFFFFFD, 0x00000000},
442 []u32{0x80000000, 0xFFFFFFFE, 0x00000000},
443 []u32{0x80000000, 0xFFFFFFFF, 0x00000000},
444 []u32{0xFFFFFFFD, 0x00000001, 0xFFFFFFFD},
445 []u32{0xFFFFFFFD, 0x00000002, 0x7FFFFFFE},
446 []u32{0xFFFFFFFD, 0x00000003, 0x55555554},
447 []u32{0xFFFFFFFD, 0x00000010, 0x0FFFFFFF},
448 []u32{0xFFFFFFFD, 0x078644FA, 0x00000022},
449 []u32{0xFFFFFFFD, 0x0747AE14, 0x00000023},
450 []u32{0xFFFFFFFD, 0x7FFFFFFF, 0x00000001},
451 []u32{0xFFFFFFFD, 0x80000000, 0x00000001},
452 []u32{0xFFFFFFFD, 0xFFFFFFFD, 0x00000001},
453 []u32{0xFFFFFFFD, 0xFFFFFFFE, 0x00000000},
454 []u32{0xFFFFFFFD, 0xFFFFFFFF, 0x00000000},
455 []u32{0xFFFFFFFE, 0x00000001, 0xFFFFFFFE},
456 []u32{0xFFFFFFFE, 0x00000002, 0x7FFFFFFF},
457 []u32{0xFFFFFFFE, 0x00000003, 0x55555554},
458 []u32{0xFFFFFFFE, 0x00000010, 0x0FFFFFFF},
459 []u32{0xFFFFFFFE, 0x078644FA, 0x00000022},
460 []u32{0xFFFFFFFE, 0x0747AE14, 0x00000023},
461 []u32{0xFFFFFFFE, 0x7FFFFFFF, 0x00000002},
462 []u32{0xFFFFFFFE, 0x80000000, 0x00000001},
463 []u32{0xFFFFFFFE, 0xFFFFFFFD, 0x00000001},
464 []u32{0xFFFFFFFE, 0xFFFFFFFE, 0x00000001},
465 []u32{0xFFFFFFFE, 0xFFFFFFFF, 0x00000000},
466 []u32{0xFFFFFFFF, 0x00000001, 0xFFFFFFFF},
467 []u32{0xFFFFFFFF, 0x00000002, 0x7FFFFFFF},
468 []u32{0xFFFFFFFF, 0x00000003, 0x55555555},
469 []u32{0xFFFFFFFF, 0x00000010, 0x0FFFFFFF},
470 []u32{0xFFFFFFFF, 0x078644FA, 0x00000022},
471 []u32{0xFFFFFFFF, 0x0747AE14, 0x00000023},
472 []u32{0xFFFFFFFF, 0x7FFFFFFF, 0x00000002},
473 []u32{0xFFFFFFFF, 0x80000000, 0x00000001},
474 []u32{0xFFFFFFFF, 0xFFFFFFFD, 0x00000001},
475 []u32{0xFFFFFFFF, 0xFFFFFFFE, 0x00000001},
476 []u32{0xFFFFFFFF, 0xFFFFFFFF, 0x00000001},
346 const cases = [][3]u32{
347 []u32{
348 0x00000000,
349 0x00000001,
350 0x00000000,
351 },
352 []u32{
353 0x00000000,
354 0x00000002,
355 0x00000000,
356 },
357 []u32{
358 0x00000000,
359 0x00000003,
360 0x00000000,
361 },
362 []u32{
363 0x00000000,
364 0x00000010,
365 0x00000000,
366 },
367 []u32{
368 0x00000000,
369 0x078644FA,
370 0x00000000,
371 },
372 []u32{
373 0x00000000,
374 0x0747AE14,
375 0x00000000,
376 },
377 []u32{
378 0x00000000,
379 0x7FFFFFFF,
380 0x00000000,
381 },
382 []u32{
383 0x00000000,
384 0x80000000,
385 0x00000000,
386 },
387 []u32{
388 0x00000000,
389 0xFFFFFFFD,
390 0x00000000,
391 },
392 []u32{
393 0x00000000,
394 0xFFFFFFFE,
395 0x00000000,
396 },
397 []u32{
398 0x00000000,
399 0xFFFFFFFF,
400 0x00000000,
401 },
402 []u32{
403 0x00000001,
404 0x00000001,
405 0x00000001,
406 },
407 []u32{
408 0x00000001,
409 0x00000002,
410 0x00000000,
411 },
412 []u32{
413 0x00000001,
414 0x00000003,
415 0x00000000,
416 },
417 []u32{
418 0x00000001,
419 0x00000010,
420 0x00000000,
421 },
422 []u32{
423 0x00000001,
424 0x078644FA,
425 0x00000000,
426 },
427 []u32{
428 0x00000001,
429 0x0747AE14,
430 0x00000000,
431 },
432 []u32{
433 0x00000001,
434 0x7FFFFFFF,
435 0x00000000,
436 },
437 []u32{
438 0x00000001,
439 0x80000000,
440 0x00000000,
441 },
442 []u32{
443 0x00000001,
444 0xFFFFFFFD,
445 0x00000000,
446 },
447 []u32{
448 0x00000001,
449 0xFFFFFFFE,
450 0x00000000,
451 },
452 []u32{
453 0x00000001,
454 0xFFFFFFFF,
455 0x00000000,
456 },
457 []u32{
458 0x00000002,
459 0x00000001,
460 0x00000002,
461 },
462 []u32{
463 0x00000002,
464 0x00000002,
465 0x00000001,
466 },
467 []u32{
468 0x00000002,
469 0x00000003,
470 0x00000000,
471 },
472 []u32{
473 0x00000002,
474 0x00000010,
475 0x00000000,
476 },
477 []u32{
478 0x00000002,
479 0x078644FA,
480 0x00000000,
481 },
482 []u32{
483 0x00000002,
484 0x0747AE14,
485 0x00000000,
486 },
487 []u32{
488 0x00000002,
489 0x7FFFFFFF,
490 0x00000000,
491 },
492 []u32{
493 0x00000002,
494 0x80000000,
495 0x00000000,
496 },
497 []u32{
498 0x00000002,
499 0xFFFFFFFD,
500 0x00000000,
501 },
502 []u32{
503 0x00000002,
504 0xFFFFFFFE,
505 0x00000000,
506 },
507 []u32{
508 0x00000002,
509 0xFFFFFFFF,
510 0x00000000,
511 },
512 []u32{
513 0x00000003,
514 0x00000001,
515 0x00000003,
516 },
517 []u32{
518 0x00000003,
519 0x00000002,
520 0x00000001,
521 },
522 []u32{
523 0x00000003,
524 0x00000003,
525 0x00000001,
526 },
527 []u32{
528 0x00000003,
529 0x00000010,
530 0x00000000,
531 },
532 []u32{
533 0x00000003,
534 0x078644FA,
535 0x00000000,
536 },
537 []u32{
538 0x00000003,
539 0x0747AE14,
540 0x00000000,
541 },
542 []u32{
543 0x00000003,
544 0x7FFFFFFF,
545 0x00000000,
546 },
547 []u32{
548 0x00000003,
549 0x80000000,
550 0x00000000,
551 },
552 []u32{
553 0x00000003,
554 0xFFFFFFFD,
555 0x00000000,
556 },
557 []u32{
558 0x00000003,
559 0xFFFFFFFE,
560 0x00000000,
561 },
562 []u32{
563 0x00000003,
564 0xFFFFFFFF,
565 0x00000000,
566 },
567 []u32{
568 0x00000010,
569 0x00000001,
570 0x00000010,
571 },
572 []u32{
573 0x00000010,
574 0x00000002,
575 0x00000008,
576 },
577 []u32{
578 0x00000010,
579 0x00000003,
580 0x00000005,
581 },
582 []u32{
583 0x00000010,
584 0x00000010,
585 0x00000001,
586 },
587 []u32{
588 0x00000010,
589 0x078644FA,
590 0x00000000,
591 },
592 []u32{
593 0x00000010,
594 0x0747AE14,
595 0x00000000,
596 },
597 []u32{
598 0x00000010,
599 0x7FFFFFFF,
600 0x00000000,
601 },
602 []u32{
603 0x00000010,
604 0x80000000,
605 0x00000000,
606 },
607 []u32{
608 0x00000010,
609 0xFFFFFFFD,
610 0x00000000,
611 },
612 []u32{
613 0x00000010,
614 0xFFFFFFFE,
615 0x00000000,
616 },
617 []u32{
618 0x00000010,
619 0xFFFFFFFF,
620 0x00000000,
621 },
622 []u32{
623 0x078644FA,
624 0x00000001,
625 0x078644FA,
626 },
627 []u32{
628 0x078644FA,
629 0x00000002,
630 0x03C3227D,
631 },
632 []u32{
633 0x078644FA,
634 0x00000003,
635 0x028216FE,
636 },
637 []u32{
638 0x078644FA,
639 0x00000010,
640 0x0078644F,
641 },
642 []u32{
643 0x078644FA,
644 0x078644FA,
645 0x00000001,
646 },
647 []u32{
648 0x078644FA,
649 0x0747AE14,
650 0x00000001,
651 },
652 []u32{
653 0x078644FA,
654 0x7FFFFFFF,
655 0x00000000,
656 },
657 []u32{
658 0x078644FA,
659 0x80000000,
660 0x00000000,
661 },
662 []u32{
663 0x078644FA,
664 0xFFFFFFFD,
665 0x00000000,
666 },
667 []u32{
668 0x078644FA,
669 0xFFFFFFFE,
670 0x00000000,
671 },
672 []u32{
673 0x078644FA,
674 0xFFFFFFFF,
675 0x00000000,
676 },
677 []u32{
678 0x0747AE14,
679 0x00000001,
680 0x0747AE14,
681 },
682 []u32{
683 0x0747AE14,
684 0x00000002,
685 0x03A3D70A,
686 },
687 []u32{
688 0x0747AE14,
689 0x00000003,
690 0x026D3A06,
691 },
692 []u32{
693 0x0747AE14,
694 0x00000010,
695 0x00747AE1,
696 },
697 []u32{
698 0x0747AE14,
699 0x078644FA,
700 0x00000000,
701 },
702 []u32{
703 0x0747AE14,
704 0x0747AE14,
705 0x00000001,
706 },
707 []u32{
708 0x0747AE14,
709 0x7FFFFFFF,
710 0x00000000,
711 },
712 []u32{
713 0x0747AE14,
714 0x80000000,
715 0x00000000,
716 },
717 []u32{
718 0x0747AE14,
719 0xFFFFFFFD,
720 0x00000000,
721 },
722 []u32{
723 0x0747AE14,
724 0xFFFFFFFE,
725 0x00000000,
726 },
727 []u32{
728 0x0747AE14,
729 0xFFFFFFFF,
730 0x00000000,
731 },
732 []u32{
733 0x7FFFFFFF,
734 0x00000001,
735 0x7FFFFFFF,
736 },
737 []u32{
738 0x7FFFFFFF,
739 0x00000002,
740 0x3FFFFFFF,
741 },
742 []u32{
743 0x7FFFFFFF,
744 0x00000003,
745 0x2AAAAAAA,
746 },
747 []u32{
748 0x7FFFFFFF,
749 0x00000010,
750 0x07FFFFFF,
751 },
752 []u32{
753 0x7FFFFFFF,
754 0x078644FA,
755 0x00000011,
756 },
757 []u32{
758 0x7FFFFFFF,
759 0x0747AE14,
760 0x00000011,
761 },
762 []u32{
763 0x7FFFFFFF,
764 0x7FFFFFFF,
765 0x00000001,
766 },
767 []u32{
768 0x7FFFFFFF,
769 0x80000000,
770 0x00000000,
771 },
772 []u32{
773 0x7FFFFFFF,
774 0xFFFFFFFD,
775 0x00000000,
776 },
777 []u32{
778 0x7FFFFFFF,
779 0xFFFFFFFE,
780 0x00000000,
781 },
782 []u32{
783 0x7FFFFFFF,
784 0xFFFFFFFF,
785 0x00000000,
786 },
787 []u32{
788 0x80000000,
789 0x00000001,
790 0x80000000,
791 },
792 []u32{
793 0x80000000,
794 0x00000002,
795 0x40000000,
796 },
797 []u32{
798 0x80000000,
799 0x00000003,
800 0x2AAAAAAA,
801 },
802 []u32{
803 0x80000000,
804 0x00000010,
805 0x08000000,
806 },
807 []u32{
808 0x80000000,
809 0x078644FA,
810 0x00000011,
811 },
812 []u32{
813 0x80000000,
814 0x0747AE14,
815 0x00000011,
816 },
817 []u32{
818 0x80000000,
819 0x7FFFFFFF,
820 0x00000001,
821 },
822 []u32{
823 0x80000000,
824 0x80000000,
825 0x00000001,
826 },
827 []u32{
828 0x80000000,
829 0xFFFFFFFD,
830 0x00000000,
831 },
832 []u32{
833 0x80000000,
834 0xFFFFFFFE,
835 0x00000000,
836 },
837 []u32{
838 0x80000000,
839 0xFFFFFFFF,
840 0x00000000,
841 },
842 []u32{
843 0xFFFFFFFD,
844 0x00000001,
845 0xFFFFFFFD,
846 },
847 []u32{
848 0xFFFFFFFD,
849 0x00000002,
850 0x7FFFFFFE,
851 },
852 []u32{
853 0xFFFFFFFD,
854 0x00000003,
855 0x55555554,
856 },
857 []u32{
858 0xFFFFFFFD,
859 0x00000010,
860 0x0FFFFFFF,
861 },
862 []u32{
863 0xFFFFFFFD,
864 0x078644FA,
865 0x00000022,
866 },
867 []u32{
868 0xFFFFFFFD,
869 0x0747AE14,
870 0x00000023,
871 },
872 []u32{
873 0xFFFFFFFD,
874 0x7FFFFFFF,
875 0x00000001,
876 },
877 []u32{
878 0xFFFFFFFD,
879 0x80000000,
880 0x00000001,
881 },
882 []u32{
883 0xFFFFFFFD,
884 0xFFFFFFFD,
885 0x00000001,
886 },
887 []u32{
888 0xFFFFFFFD,
889 0xFFFFFFFE,
890 0x00000000,
891 },
892 []u32{
893 0xFFFFFFFD,
894 0xFFFFFFFF,
895 0x00000000,
896 },
897 []u32{
898 0xFFFFFFFE,
899 0x00000001,
900 0xFFFFFFFE,
901 },
902 []u32{
903 0xFFFFFFFE,
904 0x00000002,
905 0x7FFFFFFF,
906 },
907 []u32{
908 0xFFFFFFFE,
909 0x00000003,
910 0x55555554,
911 },
912 []u32{
913 0xFFFFFFFE,
914 0x00000010,
915 0x0FFFFFFF,
916 },
917 []u32{
918 0xFFFFFFFE,
919 0x078644FA,
920 0x00000022,
921 },
922 []u32{
923 0xFFFFFFFE,
924 0x0747AE14,
925 0x00000023,
926 },
927 []u32{
928 0xFFFFFFFE,
929 0x7FFFFFFF,
930 0x00000002,
931 },
932 []u32{
933 0xFFFFFFFE,
934 0x80000000,
935 0x00000001,
936 },
937 []u32{
938 0xFFFFFFFE,
939 0xFFFFFFFD,
940 0x00000001,
941 },
942 []u32{
943 0xFFFFFFFE,
944 0xFFFFFFFE,
945 0x00000001,
946 },
947 []u32{
948 0xFFFFFFFE,
949 0xFFFFFFFF,
950 0x00000000,
951 },
952 []u32{
953 0xFFFFFFFF,
954 0x00000001,
955 0xFFFFFFFF,
956 },
957 []u32{
958 0xFFFFFFFF,
959 0x00000002,
960 0x7FFFFFFF,
961 },
962 []u32{
963 0xFFFFFFFF,
964 0x00000003,
965 0x55555555,
966 },
967 []u32{
968 0xFFFFFFFF,
969 0x00000010,
970 0x0FFFFFFF,
971 },
972 []u32{
973 0xFFFFFFFF,
974 0x078644FA,
975 0x00000022,
976 },
977 []u32{
978 0xFFFFFFFF,
979 0x0747AE14,
980 0x00000023,
981 },
982 []u32{
983 0xFFFFFFFF,
984 0x7FFFFFFF,
985 0x00000002,
986 },
987 []u32{
988 0xFFFFFFFF,
989 0x80000000,
990 0x00000001,
991 },
992 []u32{
993 0xFFFFFFFF,
994 0xFFFFFFFD,
995 0x00000001,
996 },
997 []u32{
998 0xFFFFFFFF,
999 0xFFFFFFFE,
1000 0x00000001,
1001 },
1002 []u32{
1003 0xFFFFFFFF,
1004 0xFFFFFFFF,
1005 0x00000001,
1006 },
4771007 };
4781008
4791009 for (cases) |case| {
std/special/compiler_rt/udivmod.zig+23-20
......@@ -1,7 +1,10 @@
11const builtin = @import("builtin");
22const is_test = builtin.is_test;
33
4const low = switch (builtin.endian) { builtin.Endian.Big => 1, builtin.Endian.Little => 0 };
4const low = switch (builtin.endian) {
5 builtin.Endian.Big => 1,
6 builtin.Endian.Little => 0,
7};
58const high = 1 - low;
69
710pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) DoubleInt {
......@@ -11,8 +14,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
1114 const SignedDoubleInt = @IntType(true, DoubleInt.bit_count);
1215 const Log2SingleInt = @import("std").math.Log2Int(SingleInt);
1316
14 const n = *@ptrCast(&const [2]SingleInt, &a); // TODO issue #421
15 const d = *@ptrCast(&const [2]SingleInt, &b); // TODO issue #421
17 const n = @ptrCast(&const [2]SingleInt, &a).*; // TODO issue #421
18 const d = @ptrCast(&const [2]SingleInt, &b).*; // TODO issue #421
1619 var q: [2]SingleInt = undefined;
1720 var r: [2]SingleInt = undefined;
1821 var sr: c_uint = undefined;
......@@ -23,7 +26,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
2326 // ---
2427 // 0 X
2528 if (maybe_rem) |rem| {
26 *rem = n[low] % d[low];
29 rem.* = n[low] % d[low];
2730 }
2831 return n[low] / d[low];
2932 }
......@@ -31,7 +34,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
3134 // ---
3235 // K X
3336 if (maybe_rem) |rem| {
34 *rem = n[low];
37 rem.* = n[low];
3538 }
3639 return 0;
3740 }
......@@ -42,7 +45,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
4245 // ---
4346 // 0 0
4447 if (maybe_rem) |rem| {
45 *rem = n[high] % d[low];
48 rem.* = n[high] % d[low];
4649 }
4750 return n[high] / d[low];
4851 }
......@@ -54,7 +57,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
5457 if (maybe_rem) |rem| {
5558 r[high] = n[high] % d[high];
5659 r[low] = 0;
57 *rem = *@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]); // TODO issue #421
60 rem.* = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
5861 }
5962 return n[high] / d[high];
6063 }
......@@ -66,7 +69,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
6669 if (maybe_rem) |rem| {
6770 r[low] = n[low];
6871 r[high] = n[high] & (d[high] - 1);
69 *rem = *@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]); // TODO issue #421
72 rem.* = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
7073 }
7174 return n[high] >> Log2SingleInt(@ctz(d[high]));
7275 }
......@@ -77,7 +80,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
7780 // 0 <= sr <= SingleInt.bit_count - 2 or sr large
7881 if (sr > SingleInt.bit_count - 2) {
7982 if (maybe_rem) |rem| {
80 *rem = a;
83 rem.* = a;
8184 }
8285 return 0;
8386 }
......@@ -98,7 +101,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
98101 if ((d[low] & (d[low] - 1)) == 0) {
99102 // d is a power of 2
100103 if (maybe_rem) |rem| {
101 *rem = n[low] & (d[low] - 1);
104 rem.* = n[low] & (d[low] - 1);
102105 }
103106 if (d[low] == 1) {
104107 return a;
......@@ -106,7 +109,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
106109 sr = @ctz(d[low]);
107110 q[high] = n[high] >> Log2SingleInt(sr);
108111 q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
109 return *@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0]); // TODO issue #421
112 return @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
110113 }
111114 // K X
112115 // ---
......@@ -141,7 +144,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
141144 // 0 <= sr <= SingleInt.bit_count - 1 or sr large
142145 if (sr > SingleInt.bit_count - 1) {
143146 if (maybe_rem) |rem| {
144 *rem = a;
147 rem.* = a;
145148 }
146149 return 0;
147150 }
......@@ -170,25 +173,25 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
170173 var r_all: DoubleInt = undefined;
171174 while (sr > 0) : (sr -= 1) {
172175 // r:q = ((r:q) << 1) | carry
173 r[high] = (r[high] << 1) | (r[low] >> (SingleInt.bit_count - 1));
174 r[low] = (r[low] << 1) | (q[high] >> (SingleInt.bit_count - 1));
175 q[high] = (q[high] << 1) | (q[low] >> (SingleInt.bit_count - 1));
176 q[low] = (q[low] << 1) | carry;
176 r[high] = (r[high] << 1) | (r[low] >> (SingleInt.bit_count - 1));
177 r[low] = (r[low] << 1) | (q[high] >> (SingleInt.bit_count - 1));
178 q[high] = (q[high] << 1) | (q[low] >> (SingleInt.bit_count - 1));
179 q[low] = (q[low] << 1) | carry;
177180 // carry = 0;
178181 // if (r.all >= b)
179182 // {
180183 // r.all -= b;
181184 // carry = 1;
182185 // }
183 r_all = *@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]); // TODO issue #421
186 r_all = @ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
184187 const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
185188 carry = u32(s & 1);
186189 r_all -= b & @bitCast(DoubleInt, s);
187 r = *@ptrCast(&[2]SingleInt, &r_all); // TODO issue #421
190 r = @ptrCast(&[2]SingleInt, &r_all).*; // TODO issue #421
188191 }
189 const q_all = ((*@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0])) << 1) | carry; // TODO issue #421
192 const q_all = ((@ptrCast(&align(@alignOf(SingleInt)) DoubleInt, &q[0]).*) << 1) | carry; // TODO issue #421
190193 if (maybe_rem) |rem| {
191 *rem = r_all;
194 rem.* = r_all;
192195 }
193196 return q_all;
194197}
std/special/compiler_rt/udivmodti4.zig+1-1
......@@ -9,7 +9,7 @@ pub extern fn __udivmodti4(a: u128, b: u128, maybe_rem: ?&u128) u128 {
99
1010pub extern fn __udivmodti4_windows_x86_64(a: &const u128, b: &const u128, maybe_rem: ?&u128) void {
1111 @setRuntimeSafety(builtin.is_test);
12 compiler_rt.setXmm0(u128, udivmod(u128, *a, *b, maybe_rem));
12 compiler_rt.setXmm0(u128, udivmod(u128, a.*, b.*, maybe_rem));
1313}
1414
1515test "import udivmodti4" {
std/special/compiler_rt/umodti3.zig+1-1
......@@ -11,5 +11,5 @@ pub extern fn __umodti3(a: u128, b: u128) u128 {
1111
1212pub extern fn __umodti3_windows_x86_64(a: &const u128, b: &const u128) void {
1313 @setRuntimeSafety(builtin.is_test);
14 compiler_rt.setXmm0(u128, __umodti3(*a, *b));
14 compiler_rt.setXmm0(u128, __umodti3(a.*, b.*));
1515}
std/zig/ast.zig+34-41
......@@ -40,7 +40,7 @@ pub const Tree = struct {
4040 };
4141
4242 pub fn tokenLocationPtr(self: &Tree, start_index: usize, token: &const Token) Location {
43 var loc = Location {
43 var loc = Location{
4444 .line = 0,
4545 .column = 0,
4646 .line_start = start_index,
......@@ -71,7 +71,6 @@ pub const Tree = struct {
7171 pub fn dump(self: &Tree) void {
7272 self.root_node.base.dump(0);
7373 }
74
7574};
7675
7776pub const Error = union(enum) {
......@@ -95,7 +94,7 @@ pub const Error = union(enum) {
9594 ExpectedCommaOrEnd: ExpectedCommaOrEnd,
9695
9796 pub fn render(self: &Error, tokens: &Tree.TokenList, stream: var) !void {
98 switch (*self) {
97 switch (self.*) {
9998 // TODO https://github.com/zig-lang/zig/issues/683
10099 @TagType(Error).InvalidToken => |*x| return x.render(tokens, stream),
101100 @TagType(Error).ExpectedVarDeclOrFn => |*x| return x.render(tokens, stream),
......@@ -119,7 +118,7 @@ pub const Error = union(enum) {
119118 }
120119
121120 pub fn loc(self: &Error) TokenIndex {
122 switch (*self) {
121 switch (self.*) {
123122 // TODO https://github.com/zig-lang/zig/issues/683
124123 @TagType(Error).InvalidToken => |x| return x.token,
125124 @TagType(Error).ExpectedVarDeclOrFn => |x| return x.token,
......@@ -144,15 +143,12 @@ pub const Error = union(enum) {
144143
145144 pub const InvalidToken = SingleTokenError("Invalid token {}");
146145 pub const ExpectedVarDeclOrFn = SingleTokenError("Expected variable declaration or function, found {}");
147 pub const ExpectedAggregateKw = SingleTokenError("Expected " ++
148 @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++
149 @tagName(Token.Id.Keyword_enum) ++ ", found {}");
146 pub const ExpectedAggregateKw = SingleTokenError("Expected " ++ @tagName(Token.Id.Keyword_struct) ++ ", " ++ @tagName(Token.Id.Keyword_union) ++ ", or " ++ @tagName(Token.Id.Keyword_enum) ++ ", found {}");
150147 pub const ExpectedEqOrSemi = SingleTokenError("Expected '=' or ';', found {}");
151148 pub const ExpectedSemiOrLBrace = SingleTokenError("Expected ';' or '{{', found {}");
152149 pub const ExpectedLabelable = SingleTokenError("Expected 'while', 'for', 'inline', 'suspend', or '{{', found {}");
153150 pub const ExpectedInlinable = SingleTokenError("Expected 'while' or 'for', found {}");
154 pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++
155 @tagName(Token.Id.Identifier) ++ ", found {}");
151 pub const ExpectedAsmOutputReturnOrType = SingleTokenError("Expected '->' or " ++ @tagName(Token.Id.Identifier) ++ ", found {}");
156152 pub const ExpectedSliceOrRBracket = SingleTokenError("Expected ']' or '..', found {}");
157153 pub const ExpectedPrimaryExpr = SingleTokenError("Expected primary expression, found {}");
158154
......@@ -165,8 +161,7 @@ pub const Error = union(enum) {
165161 node: &Node,
166162
167163 pub fn render(self: &ExpectedCall, tokens: &Tree.TokenList, stream: var) !void {
168 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}",
169 @tagName(self.node.id));
164 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ ", found {}", @tagName(self.node.id));
170165 }
171166 };
172167
......@@ -174,8 +169,7 @@ pub const Error = union(enum) {
174169 node: &Node,
175170
176171 pub fn render(self: &ExpectedCallOrFnProto, tokens: &Tree.TokenList, stream: var) !void {
177 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++
178 @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
172 return stream.print("expected " ++ @tagName(@TagType(Node.SuffixOp.Op).Call) ++ " or " ++ @tagName(Node.Id.FnProto) ++ ", found {}", @tagName(self.node.id));
179173 }
180174 };
181175
......@@ -445,17 +439,17 @@ pub const Node = struct {
445439
446440 pub fn iterate(self: &Root, index: usize) ?&Node {
447441 if (index < self.decls.len) {
448 return *self.decls.at(index);
442 return self.decls.at(index).*;
449443 }
450444 return null;
451445 }
452446
453447 pub fn firstToken(self: &Root) TokenIndex {
454 return if (self.decls.len == 0) self.eof_token else (*self.decls.at(0)).firstToken();
448 return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
455449 }
456450
457451 pub fn lastToken(self: &Root) TokenIndex {
458 return if (self.decls.len == 0) self.eof_token else (*self.decls.at(self.decls.len - 1)).lastToken();
452 return if (self.decls.len == 0) self.eof_token else (self.decls.at(self.decls.len - 1).*).lastToken();
459453 }
460454 };
461455
......@@ -545,7 +539,7 @@ pub const Node = struct {
545539 pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node {
546540 var i = index;
547541
548 if (i < self.decls.len) return *self.decls.at(i);
542 if (i < self.decls.len) return self.decls.at(i).*;
549543 i -= self.decls.len;
550544
551545 return null;
......@@ -598,10 +592,10 @@ pub const Node = struct {
598592 i -= 1;
599593 },
600594 InitArg.None,
601 InitArg.Enum => { }
595 InitArg.Enum => {},
602596 }
603597
604 if (i < self.fields_and_decls.len) return *self.fields_and_decls.at(i);
598 if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i).*;
605599 i -= self.fields_and_decls.len;
606600
607601 return null;
......@@ -814,7 +808,7 @@ pub const Node = struct {
814808 i -= 1;
815809 }
816810
817 if (i < self.params.len) return *self.params.at(self.params.len - i - 1);
811 if (i < self.params.len) return self.params.at(self.params.len - i - 1).*;
818812 i -= self.params.len;
819813
820814 if (self.align_expr) |align_expr| {
......@@ -839,7 +833,6 @@ pub const Node = struct {
839833 i -= 1;
840834 }
841835
842
843836 return null;
844837 }
845838
......@@ -934,7 +927,7 @@ pub const Node = struct {
934927 pub fn iterate(self: &Block, index: usize) ?&Node {
935928 var i = index;
936929
937 if (i < self.statements.len) return *self.statements.at(i);
930 if (i < self.statements.len) return self.statements.at(i).*;
938931 i -= self.statements.len;
939932
940933 return null;
......@@ -1119,6 +1112,7 @@ pub const Node = struct {
11191112 base: Node,
11201113 switch_token: TokenIndex,
11211114 expr: &Node,
1115
11221116 /// these can be SwitchCase nodes or LineComment nodes
11231117 cases: CaseList,
11241118 rbrace: TokenIndex,
......@@ -1131,7 +1125,7 @@ pub const Node = struct {
11311125 if (i < 1) return self.expr;
11321126 i -= 1;
11331127
1134 if (i < self.cases.len) return *self.cases.at(i);
1128 if (i < self.cases.len) return self.cases.at(i).*;
11351129 i -= self.cases.len;
11361130
11371131 return null;
......@@ -1157,7 +1151,7 @@ pub const Node = struct {
11571151 pub fn iterate(self: &SwitchCase, index: usize) ?&Node {
11581152 var i = index;
11591153
1160 if (i < self.items.len) return *self.items.at(i);
1154 if (i < self.items.len) return self.items.at(i).*;
11611155 i -= self.items.len;
11621156
11631157 if (self.payload) |payload| {
......@@ -1172,7 +1166,7 @@ pub const Node = struct {
11721166 }
11731167
11741168 pub fn firstToken(self: &SwitchCase) TokenIndex {
1175 return (*self.items.at(0)).firstToken();
1169 return (self.items.at(0).*).firstToken();
11761170 }
11771171
11781172 pub fn lastToken(self: &SwitchCase) TokenIndex {
......@@ -1491,7 +1485,7 @@ pub const Node = struct {
14911485 BitNot,
14921486 BoolNot,
14931487 Cancel,
1494 Deref,
1488 PointerType,
14951489 MaybeType,
14961490 Negation,
14971491 NegationWrap,
......@@ -1533,7 +1527,6 @@ pub const Node = struct {
15331527 Op.BitNot,
15341528 Op.BoolNot,
15351529 Op.Cancel,
1536 Op.Deref,
15371530 Op.MaybeType,
15381531 Op.Negation,
15391532 Op.NegationWrap,
......@@ -1593,6 +1586,7 @@ pub const Node = struct {
15931586 Slice: Slice,
15941587 ArrayInitializer: InitList,
15951588 StructInitializer: InitList,
1589 Deref,
15961590
15971591 pub const InitList = SegmentedList(&Node, 2);
15981592
......@@ -1617,7 +1611,7 @@ pub const Node = struct {
16171611
16181612 switch (self.op) {
16191613 @TagType(Op).Call => |*call_info| {
1620 if (i < call_info.params.len) return *call_info.params.at(i);
1614 if (i < call_info.params.len) return call_info.params.at(i).*;
16211615 i -= call_info.params.len;
16221616 },
16231617 Op.ArrayAccess => |index_expr| {
......@@ -1634,11 +1628,11 @@ pub const Node = struct {
16341628 }
16351629 },
16361630 Op.ArrayInitializer => |*exprs| {
1637 if (i < exprs.len) return *exprs.at(i);
1631 if (i < exprs.len) return exprs.at(i).*;
16381632 i -= exprs.len;
16391633 },
16401634 Op.StructInitializer => |*fields| {
1641 if (i < fields.len) return *fields.at(i);
1635 if (i < fields.len) return fields.at(i).*;
16421636 i -= fields.len;
16431637 },
16441638 }
......@@ -1831,7 +1825,7 @@ pub const Node = struct {
18311825 pub fn iterate(self: &BuiltinCall, index: usize) ?&Node {
18321826 var i = index;
18331827
1834 if (i < self.params.len) return *self.params.at(i);
1828 if (i < self.params.len) return self.params.at(i).*;
18351829 i -= self.params.len;
18361830
18371831 return null;
......@@ -1874,11 +1868,11 @@ pub const Node = struct {
18741868 }
18751869
18761870 pub fn firstToken(self: &MultilineStringLiteral) TokenIndex {
1877 return *self.lines.at(0);
1871 return self.lines.at(0).*;
18781872 }
18791873
18801874 pub fn lastToken(self: &MultilineStringLiteral) TokenIndex {
1881 return *self.lines.at(self.lines.len - 1);
1875 return self.lines.at(self.lines.len - 1).*;
18821876 }
18831877 };
18841878
......@@ -1975,7 +1969,7 @@ pub const Node = struct {
19751969
19761970 const Kind = union(enum) {
19771971 Variable: &Identifier,
1978 Return: &Node
1972 Return: &Node,
19791973 };
19801974
19811975 pub fn iterate(self: &AsmOutput, index: usize) ?&Node {
......@@ -1995,7 +1989,7 @@ pub const Node = struct {
19951989 Kind.Return => |return_type| {
19961990 if (i < 1) return return_type;
19971991 i -= 1;
1998 }
1992 },
19991993 }
20001994
20011995 return null;
......@@ -2060,13 +2054,13 @@ pub const Node = struct {
20602054 pub fn iterate(self: &Asm, index: usize) ?&Node {
20612055 var i = index;
20622056
2063 if (i < self.outputs.len) return &(*self.outputs.at(index)).base;
2057 if (i < self.outputs.len) return &(self.outputs.at(index).*).base;
20642058 i -= self.outputs.len;
20652059
2066 if (i < self.inputs.len) return &(*self.inputs.at(index)).base;
2060 if (i < self.inputs.len) return &(self.inputs.at(index).*).base;
20672061 i -= self.inputs.len;
20682062
2069 if (i < self.clobbers.len) return *self.clobbers.at(index);
2063 if (i < self.clobbers.len) return self.clobbers.at(index).*;
20702064 i -= self.clobbers.len;
20712065
20722066 return null;
......@@ -2160,11 +2154,11 @@ pub const Node = struct {
21602154 }
21612155
21622156 pub fn firstToken(self: &DocComment) TokenIndex {
2163 return *self.lines.at(0);
2157 return self.lines.at(0).*;
21642158 }
21652159
21662160 pub fn lastToken(self: &DocComment) TokenIndex {
2167 return *self.lines.at(self.lines.len - 1);
2161 return self.lines.at(self.lines.len - 1).*;
21682162 }
21692163 };
21702164
......@@ -2193,4 +2187,3 @@ pub const Node = struct {
21932187 }
21942188 };
21952189};
2196
std/zig/parse.zig+930-1161
......@@ -17,15 +17,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1717 defer stack.deinit();
1818
1919 const arena = &tree_arena.allocator;
20 const root_node = try arena.construct(ast.Node.Root {
21 .base = ast.Node { .id = ast.Node.Id.Root },
20 const root_node = try arena.construct(ast.Node.Root{
21 .base = ast.Node{ .id = ast.Node.Id.Root },
2222 .decls = ast.Node.Root.DeclList.init(arena),
2323 .doc_comments = null,
2424 // initialized when we get the eof token
2525 .eof_token = undefined,
2626 });
2727
28 var tree = ast.Tree {
28 var tree = ast.Tree{
2929 .source = source,
3030 .root_node = root_node,
3131 .arena_allocator = tree_arena,
......@@ -36,9 +36,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
3636 var tokenizer = Tokenizer.init(tree.source);
3737 while (true) {
3838 const token_ptr = try tree.tokens.addOne();
39 *token_ptr = tokenizer.next();
40 if (token_ptr.id == Token.Id.Eof)
41 break;
39 token_ptr.* = tokenizer.next();
40 if (token_ptr.id == Token.Id.Eof) break;
4241 }
4342 var tok_it = tree.tokens.iterator(0);
4443
......@@ -63,33 +62,27 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
6362 Token.Id.Keyword_test => {
6463 stack.append(State.TopLevel) catch unreachable;
6564
66 const block = try arena.construct(ast.Node.Block {
67 .base = ast.Node {
68 .id = ast.Node.Id.Block,
69 },
65 const block = try arena.construct(ast.Node.Block{
66 .base = ast.Node{ .id = ast.Node.Id.Block },
7067 .label = null,
7168 .lbrace = undefined,
7269 .statements = ast.Node.Block.StatementList.init(arena),
7370 .rbrace = undefined,
7471 });
75 const test_node = try arena.construct(ast.Node.TestDecl {
76 .base = ast.Node {
77 .id = ast.Node.Id.TestDecl,
78 },
72 const test_node = try arena.construct(ast.Node.TestDecl{
73 .base = ast.Node{ .id = ast.Node.Id.TestDecl },
7974 .doc_comments = comments,
8075 .test_token = token_index,
8176 .name = undefined,
8277 .body_node = &block.base,
8378 });
8479 try root_node.decls.push(&test_node.base);
85 try stack.append(State { .Block = block });
86 try stack.append(State {
87 .ExpectTokenSave = ExpectTokenSave {
88 .id = Token.Id.LBrace,
89 .ptr = &block.rbrace,
90 }
91 });
92 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &test_node.name } });
80 try stack.append(State{ .Block = block });
81 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
82 .id = Token.Id.LBrace,
83 .ptr = &block.rbrace,
84 } });
85 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &test_node.name } });
9386 continue;
9487 },
9588 Token.Id.Eof => {
......@@ -99,29 +92,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
9992 },
10093 Token.Id.Keyword_pub => {
10194 stack.append(State.TopLevel) catch unreachable;
102 try stack.append(State {
103 .TopLevelExtern = TopLevelDeclCtx {
104 .decls = &root_node.decls,
105 .visib_token = token_index,
106 .extern_export_inline_token = null,
107 .lib_name = null,
108 .comments = comments,
109 }
110 });
95 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
96 .decls = &root_node.decls,
97 .visib_token = token_index,
98 .extern_export_inline_token = null,
99 .lib_name = null,
100 .comments = comments,
101 } });
111102 continue;
112103 },
113104 Token.Id.Keyword_comptime => {
114 const block = try arena.construct(ast.Node.Block {
115 .base = ast.Node {.id = ast.Node.Id.Block },
105 const block = try arena.construct(ast.Node.Block{
106 .base = ast.Node{ .id = ast.Node.Id.Block },
116107 .label = null,
117108 .lbrace = undefined,
118109 .statements = ast.Node.Block.StatementList.init(arena),
119110 .rbrace = undefined,
120111 });
121 const node = try arena.construct(ast.Node.Comptime {
122 .base = ast.Node {
123 .id = ast.Node.Id.Comptime,
124 },
112 const node = try arena.construct(ast.Node.Comptime{
113 .base = ast.Node{ .id = ast.Node.Id.Comptime },
125114 .comptime_token = token_index,
126115 .expr = &block.base,
127116 .doc_comments = comments,
......@@ -129,27 +118,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
129118 try root_node.decls.push(&node.base);
130119
131120 stack.append(State.TopLevel) catch unreachable;
132 try stack.append(State { .Block = block });
133 try stack.append(State {
134 .ExpectTokenSave = ExpectTokenSave {
135 .id = Token.Id.LBrace,
136 .ptr = &block.rbrace,
137 }
138 });
121 try stack.append(State{ .Block = block });
122 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
123 .id = Token.Id.LBrace,
124 .ptr = &block.rbrace,
125 } });
139126 continue;
140127 },
141128 else => {
142129 putBackToken(&tok_it, &tree);
143130 stack.append(State.TopLevel) catch unreachable;
144 try stack.append(State {
145 .TopLevelExtern = TopLevelDeclCtx {
146 .decls = &root_node.decls,
147 .visib_token = null,
148 .extern_export_inline_token = null,
149 .lib_name = null,
150 .comments = comments,
151 }
152 });
131 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
132 .decls = &root_node.decls,
133 .visib_token = null,
134 .extern_export_inline_token = null,
135 .lib_name = null,
136 .comments = comments,
137 } });
153138 continue;
154139 },
155140 }
......@@ -159,41 +144,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
159144 const token_index = token.index;
160145 const token_ptr = token.ptr;
161146 switch (token_ptr.id) {
162 Token.Id.Keyword_export, Token.Id.Keyword_inline => {
163 stack.append(State {
164 .TopLevelDecl = TopLevelDeclCtx {
165 .decls = ctx.decls,
166 .visib_token = ctx.visib_token,
167 .extern_export_inline_token = AnnotatedToken {
168 .index = token_index,
169 .ptr = token_ptr,
170 },
171 .lib_name = null,
172 .comments = ctx.comments,
147 Token.Id.Keyword_export,
148 Token.Id.Keyword_inline => {
149 stack.append(State{ .TopLevelDecl = TopLevelDeclCtx{
150 .decls = ctx.decls,
151 .visib_token = ctx.visib_token,
152 .extern_export_inline_token = AnnotatedToken{
153 .index = token_index,
154 .ptr = token_ptr,
173155 },
174 }) catch unreachable;
156 .lib_name = null,
157 .comments = ctx.comments,
158 } }) catch unreachable;
175159 continue;
176160 },
177161 Token.Id.Keyword_extern => {
178 stack.append(State {
179 .TopLevelLibname = TopLevelDeclCtx {
180 .decls = ctx.decls,
181 .visib_token = ctx.visib_token,
182 .extern_export_inline_token = AnnotatedToken {
183 .index = token_index,
184 .ptr = token_ptr,
185 },
186 .lib_name = null,
187 .comments = ctx.comments,
162 stack.append(State{ .TopLevelLibname = TopLevelDeclCtx{
163 .decls = ctx.decls,
164 .visib_token = ctx.visib_token,
165 .extern_export_inline_token = AnnotatedToken{
166 .index = token_index,
167 .ptr = token_ptr,
188168 },
189 }) catch unreachable;
169 .lib_name = null,
170 .comments = ctx.comments,
171 } }) catch unreachable;
190172 continue;
191173 },
192174 else => {
193175 putBackToken(&tok_it, &tree);
194 stack.append(State { .TopLevelDecl = ctx }) catch unreachable;
176 stack.append(State{ .TopLevelDecl = ctx }) catch unreachable;
195177 continue;
196 }
178 },
197179 }
198180 },
199181 State.TopLevelLibname => |ctx| {
......@@ -207,15 +189,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
207189 };
208190 };
209191
210 stack.append(State {
211 .TopLevelDecl = TopLevelDeclCtx {
212 .decls = ctx.decls,
213 .visib_token = ctx.visib_token,
214 .extern_export_inline_token = ctx.extern_export_inline_token,
215 .lib_name = lib_name,
216 .comments = ctx.comments,
217 },
218 }) catch unreachable;
192 stack.append(State{ .TopLevelDecl = TopLevelDeclCtx{
193 .decls = ctx.decls,
194 .visib_token = ctx.visib_token,
195 .extern_export_inline_token = ctx.extern_export_inline_token,
196 .lib_name = lib_name,
197 .comments = ctx.comments,
198 } }) catch unreachable;
219199 continue;
220200 },
221201 State.TopLevelDecl => |ctx| {
......@@ -225,14 +205,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
225205 switch (token_ptr.id) {
226206 Token.Id.Keyword_use => {
227207 if (ctx.extern_export_inline_token) |annotated_token| {
228 *(try tree.errors.addOne()) = Error {
229 .InvalidToken = Error.InvalidToken { .token = annotated_token.index },
230 };
208 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = annotated_token.index } };
231209 return tree;
232210 }
233211
234 const node = try arena.construct(ast.Node.Use {
235 .base = ast.Node {.id = ast.Node.Id.Use },
212 const node = try arena.construct(ast.Node.Use{
213 .base = ast.Node{ .id = ast.Node.Id.Use },
236214 .visib_token = ctx.visib_token,
237215 .expr = undefined,
238216 .semicolon_token = undefined,
......@@ -240,44 +218,39 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
240218 });
241219 try ctx.decls.push(&node.base);
242220
243 stack.append(State {
244 .ExpectTokenSave = ExpectTokenSave {
245 .id = Token.Id.Semicolon,
246 .ptr = &node.semicolon_token,
247 }
248 }) catch unreachable;
249 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
221 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
222 .id = Token.Id.Semicolon,
223 .ptr = &node.semicolon_token,
224 } }) catch unreachable;
225 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
250226 continue;
251227 },
252 Token.Id.Keyword_var, Token.Id.Keyword_const => {
228 Token.Id.Keyword_var,
229 Token.Id.Keyword_const => {
253230 if (ctx.extern_export_inline_token) |annotated_token| {
254231 if (annotated_token.ptr.id == Token.Id.Keyword_inline) {
255 *(try tree.errors.addOne()) = Error {
256 .InvalidToken = Error.InvalidToken { .token = annotated_token.index },
257 };
232 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = annotated_token.index } };
258233 return tree;
259234 }
260235 }
261236
262 try stack.append(State {
263 .VarDecl = VarDeclCtx {
264 .comments = ctx.comments,
265 .visib_token = ctx.visib_token,
266 .lib_name = ctx.lib_name,
267 .comptime_token = null,
268 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,
269 .mut_token = token_index,
270 .list = ctx.decls
271 }
272 });
273 continue;
274 },
275 Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc,
276 Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => {
277 const fn_proto = try arena.construct(ast.Node.FnProto {
278 .base = ast.Node {
279 .id = ast.Node.Id.FnProto,
280 },
237 try stack.append(State{ .VarDecl = VarDeclCtx{
238 .comments = ctx.comments,
239 .visib_token = ctx.visib_token,
240 .lib_name = ctx.lib_name,
241 .comptime_token = null,
242 .extern_export_token = if (ctx.extern_export_inline_token) |at| at.index else null,
243 .mut_token = token_index,
244 .list = ctx.decls,
245 } });
246 continue;
247 },
248 Token.Id.Keyword_fn,
249 Token.Id.Keyword_nakedcc,
250 Token.Id.Keyword_stdcallcc,
251 Token.Id.Keyword_async => {
252 const fn_proto = try arena.construct(ast.Node.FnProto{
253 .base = ast.Node{ .id = ast.Node.Id.FnProto },
281254 .doc_comments = ctx.comments,
282255 .visib_token = ctx.visib_token,
283256 .name_token = null,
......@@ -293,36 +266,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
293266 .align_expr = null,
294267 });
295268 try ctx.decls.push(&fn_proto.base);
296 stack.append(State { .FnDef = fn_proto }) catch unreachable;
297 try stack.append(State { .FnProto = fn_proto });
269 stack.append(State{ .FnDef = fn_proto }) catch unreachable;
270 try stack.append(State{ .FnProto = fn_proto });
298271
299272 switch (token_ptr.id) {
300 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {
273 Token.Id.Keyword_nakedcc,
274 Token.Id.Keyword_stdcallcc => {
301275 fn_proto.cc_token = token_index;
302 try stack.append(State {
303 .ExpectTokenSave = ExpectTokenSave {
304 .id = Token.Id.Keyword_fn,
305 .ptr = &fn_proto.fn_token,
306 }
307 });
276 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
277 .id = Token.Id.Keyword_fn,
278 .ptr = &fn_proto.fn_token,
279 } });
308280 continue;
309281 },
310282 Token.Id.Keyword_async => {
311 const async_node = try arena.construct(ast.Node.AsyncAttribute {
312 .base = ast.Node {.id = ast.Node.Id.AsyncAttribute },
283 const async_node = try arena.construct(ast.Node.AsyncAttribute{
284 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
313285 .async_token = token_index,
314286 .allocator_type = null,
315287 .rangle_bracket = null,
316288 });
317289 fn_proto.async_attr = async_node;
318290
319 try stack.append(State {
320 .ExpectTokenSave = ExpectTokenSave {
321 .id = Token.Id.Keyword_fn,
322 .ptr = &fn_proto.fn_token,
323 }
324 });
325 try stack.append(State { .AsyncAllocator = async_node });
291 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
292 .id = Token.Id.Keyword_fn,
293 .ptr = &fn_proto.fn_token,
294 } });
295 try stack.append(State{ .AsyncAllocator = async_node });
326296 continue;
327297 },
328298 Token.Id.Keyword_fn => {
......@@ -333,9 +303,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
333303 }
334304 },
335305 else => {
336 *(try tree.errors.addOne()) = Error {
337 .ExpectedVarDeclOrFn = Error.ExpectedVarDeclOrFn { .token = token_index },
338 };
306 ((try tree.errors.addOne())).* = Error{ .ExpectedVarDeclOrFn = Error.ExpectedVarDeclOrFn{ .token = token_index } };
339307 return tree;
340308 },
341309 }
......@@ -343,34 +311,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
343311 State.TopLevelExternOrField => |ctx| {
344312 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| {
345313 std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct);
346 const node = try arena.construct(ast.Node.StructField {
347 .base = ast.Node {
348 .id = ast.Node.Id.StructField,
349 },
314 const node = try arena.construct(ast.Node.StructField{
315 .base = ast.Node{ .id = ast.Node.Id.StructField },
350316 .doc_comments = ctx.comments,
351317 .visib_token = ctx.visib_token,
352318 .name_token = identifier,
353319 .type_expr = undefined,
354320 });
355321 const node_ptr = try ctx.container_decl.fields_and_decls.addOne();
356 *node_ptr = &node.base;
322 node_ptr.* = &node.base;
357323
358 stack.append(State { .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable;
359 try stack.append(State { .Expression = OptionalCtx { .Required = &node.type_expr } });
360 try stack.append(State { .ExpectToken = Token.Id.Colon });
324 stack.append(State{ .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable;
325 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.type_expr } });
326 try stack.append(State{ .ExpectToken = Token.Id.Colon });
361327 continue;
362328 }
363329
364330 stack.append(State{ .ContainerDecl = ctx.container_decl }) catch unreachable;
365 try stack.append(State {
366 .TopLevelExtern = TopLevelDeclCtx {
367 .decls = &ctx.container_decl.fields_and_decls,
368 .visib_token = ctx.visib_token,
369 .extern_export_inline_token = null,
370 .lib_name = null,
371 .comments = ctx.comments,
372 }
373 });
331 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
332 .decls = &ctx.container_decl.fields_and_decls,
333 .visib_token = ctx.visib_token,
334 .extern_export_inline_token = null,
335 .lib_name = null,
336 .comments = ctx.comments,
337 } });
374338 continue;
375339 },
376340
......@@ -382,7 +346,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
382346 putBackToken(&tok_it, &tree);
383347 continue;
384348 }
385 stack.append(State { .Expression = ctx }) catch unreachable;
349 stack.append(State{ .Expression = ctx }) catch unreachable;
386350 continue;
387351 },
388352
......@@ -390,8 +354,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
390354 const token = nextToken(&tok_it, &tree);
391355 const token_index = token.index;
392356 const token_ptr = token.ptr;
393 const node = try arena.construct(ast.Node.ContainerDecl {
394 .base = ast.Node {.id = ast.Node.Id.ContainerDecl },
357 const node = try arena.construct(ast.Node.ContainerDecl{
358 .base = ast.Node{ .id = ast.Node.Id.ContainerDecl },
395359 .ltoken = ctx.ltoken,
396360 .layout = ctx.layout,
397361 .kind = switch (token_ptr.id) {
......@@ -399,9 +363,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
399363 Token.Id.Keyword_union => ast.Node.ContainerDecl.Kind.Union,
400364 Token.Id.Keyword_enum => ast.Node.ContainerDecl.Kind.Enum,
401365 else => {
402 *(try tree.errors.addOne()) = Error {
403 .ExpectedAggregateKw = Error.ExpectedAggregateKw { .token = token_index },
404 };
366 ((try tree.errors.addOne())).* = Error{ .ExpectedAggregateKw = Error.ExpectedAggregateKw{ .token = token_index } };
405367 return tree;
406368 },
407369 },
......@@ -411,9 +373,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
411373 });
412374 ctx.opt_ctx.store(&node.base);
413375
414 stack.append(State { .ContainerDecl = node }) catch unreachable;
415 try stack.append(State { .ExpectToken = Token.Id.LBrace });
416 try stack.append(State { .ContainerInitArgStart = node });
376 stack.append(State{ .ContainerDecl = node }) catch unreachable;
377 try stack.append(State{ .ExpectToken = Token.Id.LBrace });
378 try stack.append(State{ .ContainerInitArgStart = node });
417379 continue;
418380 },
419381
......@@ -422,8 +384,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
422384 continue;
423385 }
424386
425 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;
426 try stack.append(State { .ContainerInitArg = container_decl });
387 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
388 try stack.append(State{ .ContainerInitArg = container_decl });
427389 continue;
428390 },
429391
......@@ -433,23 +395,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
433395 const init_arg_token_ptr = init_arg_token.ptr;
434396 switch (init_arg_token_ptr.id) {
435397 Token.Id.Keyword_enum => {
436 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg {.Enum = null};
398 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg{ .Enum = null };
437399 const lparen_tok = nextToken(&tok_it, &tree);
438400 const lparen_tok_index = lparen_tok.index;
439401 const lparen_tok_ptr = lparen_tok.ptr;
440402 if (lparen_tok_ptr.id == Token.Id.LParen) {
441 try stack.append(State { .ExpectToken = Token.Id.RParen } );
442 try stack.append(State { .Expression = OptionalCtx {
443 .RequiredNull = &container_decl.init_arg_expr.Enum,
444 } });
403 try stack.append(State{ .ExpectToken = Token.Id.RParen });
404 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &container_decl.init_arg_expr.Enum } });
445405 } else {
446406 putBackToken(&tok_it, &tree);
447407 }
448408 },
449409 else => {
450410 putBackToken(&tok_it, &tree);
451 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg { .Type = undefined };
452 stack.append(State { .Expression = OptionalCtx { .Required = &container_decl.init_arg_expr.Type } }) catch unreachable;
411 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg{ .Type = undefined };
412 stack.append(State{ .Expression = OptionalCtx{ .Required = &container_decl.init_arg_expr.Type } }) catch unreachable;
453413 },
454414 }
455415 continue;
......@@ -468,26 +428,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
468428 Token.Id.Identifier => {
469429 switch (container_decl.kind) {
470430 ast.Node.ContainerDecl.Kind.Struct => {
471 const node = try arena.construct(ast.Node.StructField {
472 .base = ast.Node {
473 .id = ast.Node.Id.StructField,
474 },
431 const node = try arena.construct(ast.Node.StructField{
432 .base = ast.Node{ .id = ast.Node.Id.StructField },
475433 .doc_comments = comments,
476434 .visib_token = null,
477435 .name_token = token_index,
478436 .type_expr = undefined,
479437 });
480438 const node_ptr = try container_decl.fields_and_decls.addOne();
481 *node_ptr = &node.base;
439 node_ptr.* = &node.base;
482440
483 try stack.append(State { .FieldListCommaOrEnd = container_decl });
484 try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.type_expr } });
485 try stack.append(State { .ExpectToken = Token.Id.Colon });
441 try stack.append(State{ .FieldListCommaOrEnd = container_decl });
442 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.type_expr } });
443 try stack.append(State{ .ExpectToken = Token.Id.Colon });
486444 continue;
487445 },
488446 ast.Node.ContainerDecl.Kind.Union => {
489 const node = try arena.construct(ast.Node.UnionTag {
490 .base = ast.Node {.id = ast.Node.Id.UnionTag },
447 const node = try arena.construct(ast.Node.UnionTag{
448 .base = ast.Node{ .id = ast.Node.Id.UnionTag },
491449 .name_token = token_index,
492450 .type_expr = null,
493451 .value_expr = null,
......@@ -495,24 +453,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
495453 });
496454 try container_decl.fields_and_decls.push(&node.base);
497455
498 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
499 try stack.append(State { .FieldInitValue = OptionalCtx { .RequiredNull = &node.value_expr } });
500 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &node.type_expr } });
501 try stack.append(State { .IfToken = Token.Id.Colon });
456 stack.append(State{ .FieldListCommaOrEnd = container_decl }) catch unreachable;
457 try stack.append(State{ .FieldInitValue = OptionalCtx{ .RequiredNull = &node.value_expr } });
458 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &node.type_expr } });
459 try stack.append(State{ .IfToken = Token.Id.Colon });
502460 continue;
503461 },
504462 ast.Node.ContainerDecl.Kind.Enum => {
505 const node = try arena.construct(ast.Node.EnumTag {
506 .base = ast.Node { .id = ast.Node.Id.EnumTag },
463 const node = try arena.construct(ast.Node.EnumTag{
464 .base = ast.Node{ .id = ast.Node.Id.EnumTag },
507465 .name_token = token_index,
508466 .value = null,
509467 .doc_comments = comments,
510468 });
511469 try container_decl.fields_and_decls.push(&node.base);
512470
513 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
514 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &node.value } });
515 try stack.append(State { .IfToken = Token.Id.Equal });
471 stack.append(State{ .FieldListCommaOrEnd = container_decl }) catch unreachable;
472 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &node.value } });
473 try stack.append(State{ .IfToken = Token.Id.Equal });
516474 continue;
517475 },
518476 }
......@@ -520,48 +478,40 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
520478 Token.Id.Keyword_pub => {
521479 switch (container_decl.kind) {
522480 ast.Node.ContainerDecl.Kind.Struct => {
523 try stack.append(State {
524 .TopLevelExternOrField = TopLevelExternOrFieldCtx {
525 .visib_token = token_index,
526 .container_decl = container_decl,
527 .comments = comments,
528 }
529 });
481 try stack.append(State{ .TopLevelExternOrField = TopLevelExternOrFieldCtx{
482 .visib_token = token_index,
483 .container_decl = container_decl,
484 .comments = comments,
485 } });
530486 continue;
531487 },
532488 else => {
533489 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
534 try stack.append(State {
535 .TopLevelExtern = TopLevelDeclCtx {
536 .decls = &container_decl.fields_and_decls,
537 .visib_token = token_index,
538 .extern_export_inline_token = null,
539 .lib_name = null,
540 .comments = comments,
541 }
542 });
490 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
491 .decls = &container_decl.fields_and_decls,
492 .visib_token = token_index,
493 .extern_export_inline_token = null,
494 .lib_name = null,
495 .comments = comments,
496 } });
543497 continue;
544 }
498 },
545499 }
546500 },
547501 Token.Id.Keyword_export => {
548502 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
549 try stack.append(State {
550 .TopLevelExtern = TopLevelDeclCtx {
551 .decls = &container_decl.fields_and_decls,
552 .visib_token = token_index,
553 .extern_export_inline_token = null,
554 .lib_name = null,
555 .comments = comments,
556 }
557 });
503 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
504 .decls = &container_decl.fields_and_decls,
505 .visib_token = token_index,
506 .extern_export_inline_token = null,
507 .lib_name = null,
508 .comments = comments,
509 } });
558510 continue;
559511 },
560512 Token.Id.RBrace => {
561513 if (comments != null) {
562 *(try tree.errors.addOne()) = Error {
563 .UnattachedDocComment = Error.UnattachedDocComment { .token = token_index },
564 };
514 ((try tree.errors.addOne())).* = Error{ .UnattachedDocComment = Error.UnattachedDocComment{ .token = token_index } };
565515 return tree;
566516 }
567517 container_decl.rbrace_token = token_index;
......@@ -570,26 +520,21 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
570520 else => {
571521 putBackToken(&tok_it, &tree);
572522 stack.append(State{ .ContainerDecl = container_decl }) catch unreachable;
573 try stack.append(State {
574 .TopLevelExtern = TopLevelDeclCtx {
575 .decls = &container_decl.fields_and_decls,
576 .visib_token = null,
577 .extern_export_inline_token = null,
578 .lib_name = null,
579 .comments = comments,
580 }
581 });
523 try stack.append(State{ .TopLevelExtern = TopLevelDeclCtx{
524 .decls = &container_decl.fields_and_decls,
525 .visib_token = null,
526 .extern_export_inline_token = null,
527 .lib_name = null,
528 .comments = comments,
529 } });
582530 continue;
583 }
531 },
584532 }
585533 },
586534
587
588535 State.VarDecl => |ctx| {
589 const var_decl = try arena.construct(ast.Node.VarDecl {
590 .base = ast.Node {
591 .id = ast.Node.Id.VarDecl,
592 },
536 const var_decl = try arena.construct(ast.Node.VarDecl{
537 .base = ast.Node{ .id = ast.Node.Id.VarDecl },
593538 .doc_comments = ctx.comments,
594539 .visib_token = ctx.visib_token,
595540 .mut_token = ctx.mut_token,
......@@ -606,27 +551,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
606551 });
607552 try ctx.list.push(&var_decl.base);
608553
609 try stack.append(State { .VarDeclAlign = var_decl });
610 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &var_decl.type_node} });
611 try stack.append(State { .IfToken = Token.Id.Colon });
612 try stack.append(State {
613 .ExpectTokenSave = ExpectTokenSave {
614 .id = Token.Id.Identifier,
615 .ptr = &var_decl.name_token,
616 }
617 });
554 try stack.append(State{ .VarDeclAlign = var_decl });
555 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &var_decl.type_node } });
556 try stack.append(State{ .IfToken = Token.Id.Colon });
557 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
558 .id = Token.Id.Identifier,
559 .ptr = &var_decl.name_token,
560 } });
618561 continue;
619562 },
620563 State.VarDeclAlign => |var_decl| {
621 try stack.append(State { .VarDeclEq = var_decl });
564 try stack.append(State{ .VarDeclEq = var_decl });
622565
623566 const next_token = nextToken(&tok_it, &tree);
624567 const next_token_index = next_token.index;
625568 const next_token_ptr = next_token.ptr;
626569 if (next_token_ptr.id == Token.Id.Keyword_align) {
627 try stack.append(State { .ExpectToken = Token.Id.RParen });
628 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.align_node} });
629 try stack.append(State { .ExpectToken = Token.Id.LParen });
570 try stack.append(State{ .ExpectToken = Token.Id.RParen });
571 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &var_decl.align_node } });
572 try stack.append(State{ .ExpectToken = Token.Id.LParen });
630573 continue;
631574 }
632575
......@@ -640,8 +583,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
640583 switch (token_ptr.id) {
641584 Token.Id.Equal => {
642585 var_decl.eq_token = token_index;
643 stack.append(State { .VarDeclSemiColon = var_decl }) catch unreachable;
644 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } });
586 stack.append(State{ .VarDeclSemiColon = var_decl }) catch unreachable;
587 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &var_decl.init_node } });
645588 continue;
646589 },
647590 Token.Id.Semicolon => {
......@@ -649,11 +592,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
649592 continue;
650593 },
651594 else => {
652 *(try tree.errors.addOne()) = Error {
653 .ExpectedEqOrSemi = Error.ExpectedEqOrSemi { .token = token_index },
654 };
595 ((try tree.errors.addOne())).* = Error{ .ExpectedEqOrSemi = Error.ExpectedEqOrSemi{ .token = token_index } };
655596 return tree;
656 }
597 },
657598 }
658599 },
659600
......@@ -661,12 +602,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
661602 const semicolon_token = nextToken(&tok_it, &tree);
662603
663604 if (semicolon_token.ptr.id != Token.Id.Semicolon) {
664 *(try tree.errors.addOne()) = Error {
665 .ExpectedToken = Error.ExpectedToken {
666 .token = semicolon_token.index,
667 .expected_id = Token.Id.Semicolon,
668 },
669 };
605 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
606 .token = semicolon_token.index,
607 .expected_id = Token.Id.Semicolon,
608 } };
670609 return tree;
671610 }
672611
......@@ -686,32 +625,30 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
686625 const token = nextToken(&tok_it, &tree);
687626 const token_index = token.index;
688627 const token_ptr = token.ptr;
689 switch(token_ptr.id) {
628 switch (token_ptr.id) {
690629 Token.Id.LBrace => {
691 const block = try arena.construct(ast.Node.Block {
692 .base = ast.Node { .id = ast.Node.Id.Block },
630 const block = try arena.construct(ast.Node.Block{
631 .base = ast.Node{ .id = ast.Node.Id.Block },
693632 .label = null,
694633 .lbrace = token_index,
695634 .statements = ast.Node.Block.StatementList.init(arena),
696635 .rbrace = undefined,
697636 });
698637 fn_proto.body_node = &block.base;
699 stack.append(State { .Block = block }) catch unreachable;
638 stack.append(State{ .Block = block }) catch unreachable;
700639 continue;
701640 },
702641 Token.Id.Semicolon => continue,
703642 else => {
704 *(try tree.errors.addOne()) = Error {
705 .ExpectedSemiOrLBrace = Error.ExpectedSemiOrLBrace { .token = token_index },
706 };
643 ((try tree.errors.addOne())).* = Error{ .ExpectedSemiOrLBrace = Error.ExpectedSemiOrLBrace{ .token = token_index } };
707644 return tree;
708645 },
709646 }
710647 },
711648 State.FnProto => |fn_proto| {
712 stack.append(State { .FnProtoAlign = fn_proto }) catch unreachable;
713 try stack.append(State { .ParamDecl = fn_proto });
714 try stack.append(State { .ExpectToken = Token.Id.LParen });
649 stack.append(State{ .FnProtoAlign = fn_proto }) catch unreachable;
650 try stack.append(State{ .ParamDecl = fn_proto });
651 try stack.append(State{ .ExpectToken = Token.Id.LParen });
715652
716653 if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |name_token| {
717654 fn_proto.name_token = name_token;
......@@ -719,12 +656,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
719656 continue;
720657 },
721658 State.FnProtoAlign => |fn_proto| {
722 stack.append(State { .FnProtoReturnType = fn_proto }) catch unreachable;
659 stack.append(State{ .FnProtoReturnType = fn_proto }) catch unreachable;
723660
724661 if (eatToken(&tok_it, &tree, Token.Id.Keyword_align)) |align_token| {
725 try stack.append(State { .ExpectToken = Token.Id.RParen });
726 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &fn_proto.align_expr } });
727 try stack.append(State { .ExpectToken = Token.Id.LParen });
662 try stack.append(State{ .ExpectToken = Token.Id.RParen });
663 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &fn_proto.align_expr } });
664 try stack.append(State{ .ExpectToken = Token.Id.LParen });
728665 }
729666 continue;
730667 },
......@@ -734,42 +671,37 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
734671 const token_ptr = token.ptr;
735672 switch (token_ptr.id) {
736673 Token.Id.Bang => {
737 fn_proto.return_type = ast.Node.FnProto.ReturnType { .InferErrorSet = undefined };
738 stack.append(State {
739 .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.InferErrorSet },
740 }) catch unreachable;
674 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .InferErrorSet = undefined };
675 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &fn_proto.return_type.InferErrorSet } }) catch unreachable;
741676 continue;
742677 },
743678 else => {
744679 // TODO: this is a special case. Remove this when #760 is fixed
745680 if (token_ptr.id == Token.Id.Keyword_error) {
746681 if ((??tok_it.peek()).id == Token.Id.LBrace) {
747 const error_type_node = try arena.construct(ast.Node.ErrorType {
748 .base = ast.Node { .id = ast.Node.Id.ErrorType },
682 const error_type_node = try arena.construct(ast.Node.ErrorType{
683 .base = ast.Node{ .id = ast.Node.Id.ErrorType },
749684 .token = token_index,
750685 });
751 fn_proto.return_type = ast.Node.FnProto.ReturnType {
752 .Explicit = &error_type_node.base,
753 };
686 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = &error_type_node.base };
754687 continue;
755688 }
756689 }
757690
758691 putBackToken(&tok_it, &tree);
759 fn_proto.return_type = ast.Node.FnProto.ReturnType { .Explicit = undefined };
760 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.Explicit }, }) catch unreachable;
692 fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = undefined };
693 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &fn_proto.return_type.Explicit } }) catch unreachable;
761694 continue;
762695 },
763696 }
764697 },
765698
766
767699 State.ParamDecl => |fn_proto| {
768700 if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| {
769701 continue;
770702 }
771 const param_decl = try arena.construct(ast.Node.ParamDecl {
772 .base = ast.Node {.id = ast.Node.Id.ParamDecl },
703 const param_decl = try arena.construct(ast.Node.ParamDecl{
704 .base = ast.Node{ .id = ast.Node.Id.ParamDecl },
773705 .comptime_token = null,
774706 .noalias_token = null,
775707 .name_token = null,
......@@ -778,14 +710,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
778710 });
779711 try fn_proto.params.push(&param_decl.base);
780712
781 stack.append(State {
782 .ParamDeclEnd = ParamDeclEndCtx {
783 .param_decl = param_decl,
784 .fn_proto = fn_proto,
785 }
786 }) catch unreachable;
787 try stack.append(State { .ParamDeclName = param_decl });
788 try stack.append(State { .ParamDeclAliasOrComptime = param_decl });
713 stack.append(State{ .ParamDeclEnd = ParamDeclEndCtx{
714 .param_decl = param_decl,
715 .fn_proto = fn_proto,
716 } }) catch unreachable;
717 try stack.append(State{ .ParamDeclName = param_decl });
718 try stack.append(State{ .ParamDeclAliasOrComptime = param_decl });
789719 continue;
790720 },
791721 State.ParamDeclAliasOrComptime => |param_decl| {
......@@ -811,21 +741,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
811741 State.ParamDeclEnd => |ctx| {
812742 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
813743 ctx.param_decl.var_args_token = ellipsis3;
814 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;
744 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
815745 continue;
816746 }
817747
818 try stack.append(State { .ParamDeclComma = ctx.fn_proto });
819 try stack.append(State {
820 .TypeExprBegin = OptionalCtx { .Required = &ctx.param_decl.type_node }
821 });
748 try stack.append(State{ .ParamDeclComma = ctx.fn_proto });
749 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &ctx.param_decl.type_node } });
822750 continue;
823751 },
824752 State.ParamDeclComma => |fn_proto| {
825753 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
826754 ExpectCommaOrEndResult.end_token => |t| {
827755 if (t == null) {
828 stack.append(State { .ParamDecl = fn_proto }) catch unreachable;
756 stack.append(State{ .ParamDecl = fn_proto }) catch unreachable;
829757 }
830758 continue;
831759 },
......@@ -838,12 +766,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
838766
839767 State.MaybeLabeledExpression => |ctx| {
840768 if (eatToken(&tok_it, &tree, Token.Id.Colon)) |_| {
841 stack.append(State {
842 .LabeledExpression = LabelCtx {
843 .label = ctx.label,
844 .opt_ctx = ctx.opt_ctx,
845 }
846 }) catch unreachable;
769 stack.append(State{ .LabeledExpression = LabelCtx{
770 .label = ctx.label,
771 .opt_ctx = ctx.opt_ctx,
772 } }) catch unreachable;
847773 continue;
848774 }
849775
......@@ -856,69 +782,59 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
856782 const token_ptr = token.ptr;
857783 switch (token_ptr.id) {
858784 Token.Id.LBrace => {
859 const block = try arena.construct(ast.Node.Block {
860 .base = ast.Node {.id = ast.Node.Id.Block},
785 const block = try arena.construct(ast.Node.Block{
786 .base = ast.Node{ .id = ast.Node.Id.Block },
861787 .label = ctx.label,
862788 .lbrace = token_index,
863789 .statements = ast.Node.Block.StatementList.init(arena),
864790 .rbrace = undefined,
865791 });
866792 ctx.opt_ctx.store(&block.base);
867 stack.append(State { .Block = block }) catch unreachable;
793 stack.append(State{ .Block = block }) catch unreachable;
868794 continue;
869795 },
870796 Token.Id.Keyword_while => {
871 stack.append(State {
872 .While = LoopCtx {
873 .label = ctx.label,
874 .inline_token = null,
875 .loop_token = token_index,
876 .opt_ctx = ctx.opt_ctx.toRequired(),
877 }
878 }) catch unreachable;
797 stack.append(State{ .While = LoopCtx{
798 .label = ctx.label,
799 .inline_token = null,
800 .loop_token = token_index,
801 .opt_ctx = ctx.opt_ctx.toRequired(),
802 } }) catch unreachable;
879803 continue;
880804 },
881805 Token.Id.Keyword_for => {
882 stack.append(State {
883 .For = LoopCtx {
884 .label = ctx.label,
885 .inline_token = null,
886 .loop_token = token_index,
887 .opt_ctx = ctx.opt_ctx.toRequired(),
888 }
889 }) catch unreachable;
806 stack.append(State{ .For = LoopCtx{
807 .label = ctx.label,
808 .inline_token = null,
809 .loop_token = token_index,
810 .opt_ctx = ctx.opt_ctx.toRequired(),
811 } }) catch unreachable;
890812 continue;
891813 },
892814 Token.Id.Keyword_suspend => {
893 const node = try arena.construct(ast.Node.Suspend {
894 .base = ast.Node {
895 .id = ast.Node.Id.Suspend,
896 },
815 const node = try arena.construct(ast.Node.Suspend{
816 .base = ast.Node{ .id = ast.Node.Id.Suspend },
897817 .label = ctx.label,
898818 .suspend_token = token_index,
899819 .payload = null,
900820 .body = null,
901821 });
902822 ctx.opt_ctx.store(&node.base);
903 stack.append(State { .SuspendBody = node }) catch unreachable;
904 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
823 stack.append(State{ .SuspendBody = node }) catch unreachable;
824 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
905825 continue;
906826 },
907827 Token.Id.Keyword_inline => {
908 stack.append(State {
909 .Inline = InlineCtx {
910 .label = ctx.label,
911 .inline_token = token_index,
912 .opt_ctx = ctx.opt_ctx.toRequired(),
913 }
914 }) catch unreachable;
828 stack.append(State{ .Inline = InlineCtx{
829 .label = ctx.label,
830 .inline_token = token_index,
831 .opt_ctx = ctx.opt_ctx.toRequired(),
832 } }) catch unreachable;
915833 continue;
916834 },
917835 else => {
918836 if (ctx.opt_ctx != OptionalCtx.Optional) {
919 *(try tree.errors.addOne()) = Error {
920 .ExpectedLabelable = Error.ExpectedLabelable { .token = token_index },
921 };
837 ((try tree.errors.addOne())).* = Error{ .ExpectedLabelable = Error.ExpectedLabelable{ .token = token_index } };
922838 return tree;
923839 }
924840
......@@ -933,32 +849,26 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
933849 const token_ptr = token.ptr;
934850 switch (token_ptr.id) {
935851 Token.Id.Keyword_while => {
936 stack.append(State {
937 .While = LoopCtx {
938 .inline_token = ctx.inline_token,
939 .label = ctx.label,
940 .loop_token = token_index,
941 .opt_ctx = ctx.opt_ctx.toRequired(),
942 }
943 }) catch unreachable;
852 stack.append(State{ .While = LoopCtx{
853 .inline_token = ctx.inline_token,
854 .label = ctx.label,
855 .loop_token = token_index,
856 .opt_ctx = ctx.opt_ctx.toRequired(),
857 } }) catch unreachable;
944858 continue;
945859 },
946860 Token.Id.Keyword_for => {
947 stack.append(State {
948 .For = LoopCtx {
949 .inline_token = ctx.inline_token,
950 .label = ctx.label,
951 .loop_token = token_index,
952 .opt_ctx = ctx.opt_ctx.toRequired(),
953 }
954 }) catch unreachable;
861 stack.append(State{ .For = LoopCtx{
862 .inline_token = ctx.inline_token,
863 .label = ctx.label,
864 .loop_token = token_index,
865 .opt_ctx = ctx.opt_ctx.toRequired(),
866 } }) catch unreachable;
955867 continue;
956868 },
957869 else => {
958870 if (ctx.opt_ctx != OptionalCtx.Optional) {
959 *(try tree.errors.addOne()) = Error {
960 .ExpectedInlinable = Error.ExpectedInlinable { .token = token_index },
961 };
871 ((try tree.errors.addOne())).* = Error{ .ExpectedInlinable = Error.ExpectedInlinable{ .token = token_index } };
962872 return tree;
963873 }
964874
......@@ -968,8 +878,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
968878 }
969879 },
970880 State.While => |ctx| {
971 const node = try arena.construct(ast.Node.While {
972 .base = ast.Node {.id = ast.Node.Id.While },
881 const node = try arena.construct(ast.Node.While{
882 .base = ast.Node{ .id = ast.Node.Id.While },
973883 .label = ctx.label,
974884 .inline_token = ctx.inline_token,
975885 .while_token = ctx.loop_token,
......@@ -980,25 +890,25 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
980890 .@"else" = null,
981891 });
982892 ctx.opt_ctx.store(&node.base);
983 stack.append(State { .Else = &node.@"else" }) catch unreachable;
984 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });
985 try stack.append(State { .WhileContinueExpr = &node.continue_expr });
986 try stack.append(State { .IfToken = Token.Id.Colon });
987 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
988 try stack.append(State { .ExpectToken = Token.Id.RParen });
989 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });
990 try stack.append(State { .ExpectToken = Token.Id.LParen });
893 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
894 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
895 try stack.append(State{ .WhileContinueExpr = &node.continue_expr });
896 try stack.append(State{ .IfToken = Token.Id.Colon });
897 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
898 try stack.append(State{ .ExpectToken = Token.Id.RParen });
899 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.condition } });
900 try stack.append(State{ .ExpectToken = Token.Id.LParen });
991901 continue;
992902 },
993903 State.WhileContinueExpr => |dest| {
994 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;
995 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = dest } });
996 try stack.append(State { .ExpectToken = Token.Id.LParen });
904 stack.append(State{ .ExpectToken = Token.Id.RParen }) catch unreachable;
905 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = dest } });
906 try stack.append(State{ .ExpectToken = Token.Id.LParen });
997907 continue;
998908 },
999909 State.For => |ctx| {
1000 const node = try arena.construct(ast.Node.For {
1001 .base = ast.Node {.id = ast.Node.Id.For },
910 const node = try arena.construct(ast.Node.For{
911 .base = ast.Node{ .id = ast.Node.Id.For },
1002912 .label = ctx.label,
1003913 .inline_token = ctx.inline_token,
1004914 .for_token = ctx.loop_token,
......@@ -1008,12 +918,12 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1008918 .@"else" = null,
1009919 });
1010920 ctx.opt_ctx.store(&node.base);
1011 stack.append(State { .Else = &node.@"else" }) catch unreachable;
1012 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });
1013 try stack.append(State { .PointerIndexPayload = OptionalCtx { .Optional = &node.payload } });
1014 try stack.append(State { .ExpectToken = Token.Id.RParen });
1015 try stack.append(State { .Expression = OptionalCtx { .Required = &node.array_expr } });
1016 try stack.append(State { .ExpectToken = Token.Id.LParen });
921 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
922 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
923 try stack.append(State{ .PointerIndexPayload = OptionalCtx{ .Optional = &node.payload } });
924 try stack.append(State{ .ExpectToken = Token.Id.RParen });
925 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.array_expr } });
926 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1017927 continue;
1018928 },
1019929 State.Else => |dest| {
......@@ -1023,16 +933,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1023933 need_index_restore = true;
1024934 }
1025935 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
1026 const node = try arena.construct(ast.Node.Else {
1027 .base = ast.Node {.id = ast.Node.Id.Else },
936 const node = try arena.construct(ast.Node.Else{
937 .base = ast.Node{ .id = ast.Node.Id.Else },
1028938 .else_token = else_token,
1029939 .payload = null,
1030940 .body = undefined,
1031941 });
1032 *dest = node;
942 dest.* = node;
1033943
1034 stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }) catch unreachable;
1035 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
944 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }) catch unreachable;
945 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
1036946 continue;
1037947 } else {
1038948 if (need_index_restore) {
......@@ -1042,7 +952,6 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1042952 }
1043953 },
1044954
1045
1046955 State.Block => |block| {
1047956 const token = nextToken(&tok_it, &tree);
1048957 const token_index = token.index;
......@@ -1054,7 +963,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1054963 },
1055964 else => {
1056965 putBackToken(&tok_it, &tree);
1057 stack.append(State { .Block = block }) catch unreachable;
966 stack.append(State{ .Block = block }) catch unreachable;
1058967
1059968 var any_comments = false;
1060969 while (try eatLineComment(arena, &tok_it, &tree)) |line_comment| {
......@@ -1063,7 +972,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1063972 }
1064973 if (any_comments) continue;
1065974
1066 try stack.append(State { .Statement = block });
975 try stack.append(State{ .Statement = block });
1067976 continue;
1068977 },
1069978 }
......@@ -1074,33 +983,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1074983 const token_ptr = token.ptr;
1075984 switch (token_ptr.id) {
1076985 Token.Id.Keyword_comptime => {
1077 stack.append(State {
1078 .ComptimeStatement = ComptimeStatementCtx {
1079 .comptime_token = token_index,
1080 .block = block,
1081 }
1082 }) catch unreachable;
1083 continue;
1084 },
1085 Token.Id.Keyword_var, Token.Id.Keyword_const => {
1086 stack.append(State {
1087 .VarDecl = VarDeclCtx {
1088 .comments = null,
1089 .visib_token = null,
1090 .comptime_token = null,
1091 .extern_export_token = null,
1092 .lib_name = null,
1093 .mut_token = token_index,
1094 .list = &block.statements,
1095 }
1096 }) catch unreachable;
986 stack.append(State{ .ComptimeStatement = ComptimeStatementCtx{
987 .comptime_token = token_index,
988 .block = block,
989 } }) catch unreachable;
1097990 continue;
1098991 },
1099 Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => {
1100 const node = try arena.construct(ast.Node.Defer {
1101 .base = ast.Node {
1102 .id = ast.Node.Id.Defer,
1103 },
992 Token.Id.Keyword_var,
993 Token.Id.Keyword_const => {
994 stack.append(State{ .VarDecl = VarDeclCtx{
995 .comments = null,
996 .visib_token = null,
997 .comptime_token = null,
998 .extern_export_token = null,
999 .lib_name = null,
1000 .mut_token = token_index,
1001 .list = &block.statements,
1002 } }) catch unreachable;
1003 continue;
1004 },
1005 Token.Id.Keyword_defer,
1006 Token.Id.Keyword_errdefer => {
1007 const node = try arena.construct(ast.Node.Defer{
1008 .base = ast.Node{ .id = ast.Node.Id.Defer },
11041009 .defer_token = token_index,
11051010 .kind = switch (token_ptr.id) {
11061011 Token.Id.Keyword_defer => ast.Node.Defer.Kind.Unconditional,
......@@ -1110,15 +1015,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
11101015 .expr = undefined,
11111016 });
11121017 const node_ptr = try block.statements.addOne();
1113 *node_ptr = &node.base;
1018 node_ptr.* = &node.base;
11141019
1115 stack.append(State { .Semicolon = node_ptr }) catch unreachable;
1116 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
1020 stack.append(State{ .Semicolon = node_ptr }) catch unreachable;
1021 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
11171022 continue;
11181023 },
11191024 Token.Id.LBrace => {
1120 const inner_block = try arena.construct(ast.Node.Block {
1121 .base = ast.Node { .id = ast.Node.Id.Block },
1025 const inner_block = try arena.construct(ast.Node.Block{
1026 .base = ast.Node{ .id = ast.Node.Id.Block },
11221027 .label = null,
11231028 .lbrace = token_index,
11241029 .statements = ast.Node.Block.StatementList.init(arena),
......@@ -1126,16 +1031,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
11261031 });
11271032 try block.statements.push(&inner_block.base);
11281033
1129 stack.append(State { .Block = inner_block }) catch unreachable;
1034 stack.append(State{ .Block = inner_block }) catch unreachable;
11301035 continue;
11311036 },
11321037 else => {
11331038 putBackToken(&tok_it, &tree);
11341039 const statement = try block.statements.addOne();
1135 try stack.append(State { .Semicolon = statement });
1136 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });
1040 try stack.append(State{ .Semicolon = statement });
1041 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });
11371042 continue;
1138 }
1043 },
11391044 }
11401045 },
11411046 State.ComptimeStatement => |ctx| {
......@@ -1143,34 +1048,33 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
11431048 const token_index = token.index;
11441049 const token_ptr = token.ptr;
11451050 switch (token_ptr.id) {
1146 Token.Id.Keyword_var, Token.Id.Keyword_const => {
1147 stack.append(State {
1148 .VarDecl = VarDeclCtx {
1149 .comments = null,
1150 .visib_token = null,
1151 .comptime_token = ctx.comptime_token,
1152 .extern_export_token = null,
1153 .lib_name = null,
1154 .mut_token = token_index,
1155 .list = &ctx.block.statements,
1156 }
1157 }) catch unreachable;
1051 Token.Id.Keyword_var,
1052 Token.Id.Keyword_const => {
1053 stack.append(State{ .VarDecl = VarDeclCtx{
1054 .comments = null,
1055 .visib_token = null,
1056 .comptime_token = ctx.comptime_token,
1057 .extern_export_token = null,
1058 .lib_name = null,
1059 .mut_token = token_index,
1060 .list = &ctx.block.statements,
1061 } }) catch unreachable;
11581062 continue;
11591063 },
11601064 else => {
11611065 putBackToken(&tok_it, &tree);
11621066 putBackToken(&tok_it, &tree);
11631067 const statement = try ctx.block.statements.addOne();
1164 try stack.append(State { .Semicolon = statement });
1165 try stack.append(State { .Expression = OptionalCtx { .Required = statement } });
1068 try stack.append(State{ .Semicolon = statement });
1069 try stack.append(State{ .Expression = OptionalCtx{ .Required = statement } });
11661070 continue;
1167 }
1071 },
11681072 }
11691073 },
11701074 State.Semicolon => |node_ptr| {
1171 const node = *node_ptr;
1075 const node = node_ptr.*;
11721076 if (node.requireSemiColon()) {
1173 stack.append(State { .ExpectToken = Token.Id.Semicolon }) catch unreachable;
1077 stack.append(State{ .ExpectToken = Token.Id.Semicolon }) catch unreachable;
11741078 continue;
11751079 }
11761080 continue;
......@@ -1185,22 +1089,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
11851089 continue;
11861090 }
11871091
1188 const node = try arena.construct(ast.Node.AsmOutput {
1189 .base = ast.Node {.id = ast.Node.Id.AsmOutput },
1092 const node = try arena.construct(ast.Node.AsmOutput{
1093 .base = ast.Node{ .id = ast.Node.Id.AsmOutput },
11901094 .symbolic_name = undefined,
11911095 .constraint = undefined,
11921096 .kind = undefined,
11931097 });
11941098 try items.push(node);
11951099
1196 stack.append(State { .AsmOutputItems = items }) catch unreachable;
1197 try stack.append(State { .IfToken = Token.Id.Comma });
1198 try stack.append(State { .ExpectToken = Token.Id.RParen });
1199 try stack.append(State { .AsmOutputReturnOrType = node });
1200 try stack.append(State { .ExpectToken = Token.Id.LParen });
1201 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } });
1202 try stack.append(State { .ExpectToken = Token.Id.RBracket });
1203 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } });
1100 stack.append(State{ .AsmOutputItems = items }) catch unreachable;
1101 try stack.append(State{ .IfToken = Token.Id.Comma });
1102 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1103 try stack.append(State{ .AsmOutputReturnOrType = node });
1104 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1105 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.constraint } });
1106 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1107 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.symbolic_name } });
12041108 continue;
12051109 },
12061110 State.AsmOutputReturnOrType => |node| {
......@@ -1209,20 +1113,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
12091113 const token_ptr = token.ptr;
12101114 switch (token_ptr.id) {
12111115 Token.Id.Identifier => {
1212 node.kind = ast.Node.AsmOutput.Kind { .Variable = try createLiteral(arena, ast.Node.Identifier, token_index) };
1116 node.kind = ast.Node.AsmOutput.Kind{ .Variable = try createLiteral(arena, ast.Node.Identifier, token_index) };
12131117 continue;
12141118 },
12151119 Token.Id.Arrow => {
1216 node.kind = ast.Node.AsmOutput.Kind { .Return = undefined };
1217 try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.kind.Return } });
1120 node.kind = ast.Node.AsmOutput.Kind{ .Return = undefined };
1121 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.kind.Return } });
12181122 continue;
12191123 },
12201124 else => {
1221 *(try tree.errors.addOne()) = Error {
1222 .ExpectedAsmOutputReturnOrType = Error.ExpectedAsmOutputReturnOrType {
1223 .token = token_index,
1224 },
1225 };
1125 ((try tree.errors.addOne())).* = Error{ .ExpectedAsmOutputReturnOrType = Error.ExpectedAsmOutputReturnOrType{ .token = token_index } };
12261126 return tree;
12271127 },
12281128 }
......@@ -1236,49 +1136,48 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
12361136 continue;
12371137 }
12381138
1239 const node = try arena.construct(ast.Node.AsmInput {
1240 .base = ast.Node {.id = ast.Node.Id.AsmInput },
1139 const node = try arena.construct(ast.Node.AsmInput{
1140 .base = ast.Node{ .id = ast.Node.Id.AsmInput },
12411141 .symbolic_name = undefined,
12421142 .constraint = undefined,
12431143 .expr = undefined,
12441144 });
12451145 try items.push(node);
12461146
1247 stack.append(State { .AsmInputItems = items }) catch unreachable;
1248 try stack.append(State { .IfToken = Token.Id.Comma });
1249 try stack.append(State { .ExpectToken = Token.Id.RParen });
1250 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
1251 try stack.append(State { .ExpectToken = Token.Id.LParen });
1252 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } });
1253 try stack.append(State { .ExpectToken = Token.Id.RBracket });
1254 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } });
1147 stack.append(State{ .AsmInputItems = items }) catch unreachable;
1148 try stack.append(State{ .IfToken = Token.Id.Comma });
1149 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1150 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
1151 try stack.append(State{ .ExpectToken = Token.Id.LParen });
1152 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.constraint } });
1153 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1154 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.symbolic_name } });
12551155 continue;
12561156 },
12571157 State.AsmClobberItems => |items| {
1258 stack.append(State { .AsmClobberItems = items }) catch unreachable;
1259 try stack.append(State { .IfToken = Token.Id.Comma });
1260 try stack.append(State { .StringLiteral = OptionalCtx { .Required = try items.addOne() } });
1158 stack.append(State{ .AsmClobberItems = items }) catch unreachable;
1159 try stack.append(State{ .IfToken = Token.Id.Comma });
1160 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = try items.addOne() } });
12611161 continue;
12621162 },
12631163
1264
12651164 State.ExprListItemOrEnd => |list_state| {
12661165 if (eatToken(&tok_it, &tree, list_state.end)) |token_index| {
1267 *list_state.ptr = token_index;
1166 (list_state.ptr).* = token_index;
12681167 continue;
12691168 }
12701169
1271 stack.append(State { .ExprListCommaOrEnd = list_state }) catch unreachable;
1272 try stack.append(State { .Expression = OptionalCtx { .Required = try list_state.list.addOne() } });
1170 stack.append(State{ .ExprListCommaOrEnd = list_state }) catch unreachable;
1171 try stack.append(State{ .Expression = OptionalCtx{ .Required = try list_state.list.addOne() } });
12731172 continue;
12741173 },
12751174 State.ExprListCommaOrEnd => |list_state| {
12761175 switch (expectCommaOrEnd(&tok_it, &tree, list_state.end)) {
12771176 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1278 *list_state.ptr = end;
1177 (list_state.ptr).* = end;
12791178 continue;
12801179 } else {
1281 stack.append(State { .ExprListItemOrEnd = list_state }) catch unreachable;
1180 stack.append(State{ .ExprListItemOrEnd = list_state }) catch unreachable;
12821181 continue;
12831182 },
12841183 ExpectCommaOrEndResult.parse_error => |e| {
......@@ -1293,44 +1192,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
12931192 }
12941193
12951194 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1296 *list_state.ptr = rbrace;
1195 (list_state.ptr).* = rbrace;
12971196 continue;
12981197 }
12991198
1300 const node = try arena.construct(ast.Node.FieldInitializer {
1301 .base = ast.Node {
1302 .id = ast.Node.Id.FieldInitializer,
1303 },
1199 const node = try arena.construct(ast.Node.FieldInitializer{
1200 .base = ast.Node{ .id = ast.Node.Id.FieldInitializer },
13041201 .period_token = undefined,
13051202 .name_token = undefined,
13061203 .expr = undefined,
13071204 });
13081205 try list_state.list.push(&node.base);
13091206
1310 stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable;
1311 try stack.append(State { .Expression = OptionalCtx{ .Required = &node.expr } });
1312 try stack.append(State { .ExpectToken = Token.Id.Equal });
1313 try stack.append(State {
1314 .ExpectTokenSave = ExpectTokenSave {
1315 .id = Token.Id.Identifier,
1316 .ptr = &node.name_token,
1317 }
1318 });
1319 try stack.append(State {
1320 .ExpectTokenSave = ExpectTokenSave {
1321 .id = Token.Id.Period,
1322 .ptr = &node.period_token,
1323 }
1324 });
1207 stack.append(State{ .FieldInitListCommaOrEnd = list_state }) catch unreachable;
1208 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
1209 try stack.append(State{ .ExpectToken = Token.Id.Equal });
1210 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1211 .id = Token.Id.Identifier,
1212 .ptr = &node.name_token,
1213 } });
1214 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1215 .id = Token.Id.Period,
1216 .ptr = &node.period_token,
1217 } });
13251218 continue;
13261219 },
13271220 State.FieldInitListCommaOrEnd => |list_state| {
13281221 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
13291222 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1330 *list_state.ptr = end;
1223 (list_state.ptr).* = end;
13311224 continue;
13321225 } else {
1333 stack.append(State { .FieldInitListItemOrEnd = list_state }) catch unreachable;
1226 stack.append(State{ .FieldInitListItemOrEnd = list_state }) catch unreachable;
13341227 continue;
13351228 },
13361229 ExpectCommaOrEndResult.parse_error => |e| {
......@@ -1345,7 +1238,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
13451238 container_decl.rbrace_token = end;
13461239 continue;
13471240 } else {
1348 try stack.append(State { .ContainerDecl = container_decl });
1241 try stack.append(State{ .ContainerDecl = container_decl });
13491242 continue;
13501243 },
13511244 ExpectCommaOrEndResult.parse_error => |e| {
......@@ -1360,23 +1253,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
13601253 }
13611254
13621255 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1363 *list_state.ptr = rbrace;
1256 (list_state.ptr).* = rbrace;
13641257 continue;
13651258 }
13661259
13671260 const node_ptr = try list_state.list.addOne();
13681261
1369 try stack.append(State { .ErrorTagListCommaOrEnd = list_state });
1370 try stack.append(State { .ErrorTag = node_ptr });
1262 try stack.append(State{ .ErrorTagListCommaOrEnd = list_state });
1263 try stack.append(State{ .ErrorTag = node_ptr });
13711264 continue;
13721265 },
13731266 State.ErrorTagListCommaOrEnd => |list_state| {
13741267 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
13751268 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1376 *list_state.ptr = end;
1269 (list_state.ptr).* = end;
13771270 continue;
13781271 } else {
1379 stack.append(State { .ErrorTagListItemOrEnd = list_state }) catch unreachable;
1272 stack.append(State{ .ErrorTagListItemOrEnd = list_state }) catch unreachable;
13801273 continue;
13811274 },
13821275 ExpectCommaOrEndResult.parse_error => |e| {
......@@ -1391,24 +1284,22 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
13911284 }
13921285
13931286 if (eatToken(&tok_it, &tree, Token.Id.RBrace)) |rbrace| {
1394 *list_state.ptr = rbrace;
1287 (list_state.ptr).* = rbrace;
13951288 continue;
13961289 }
13971290
13981291 const comments = try eatDocComments(arena, &tok_it, &tree);
1399 const node = try arena.construct(ast.Node.SwitchCase {
1400 .base = ast.Node {
1401 .id = ast.Node.Id.SwitchCase,
1402 },
1292 const node = try arena.construct(ast.Node.SwitchCase{
1293 .base = ast.Node{ .id = ast.Node.Id.SwitchCase },
14031294 .items = ast.Node.SwitchCase.ItemList.init(arena),
14041295 .payload = null,
14051296 .expr = undefined,
14061297 });
14071298 try list_state.list.push(&node.base);
1408 try stack.append(State { .SwitchCaseCommaOrEnd = list_state });
1409 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } });
1410 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
1411 try stack.append(State { .SwitchCaseFirstItem = &node.items });
1299 try stack.append(State{ .SwitchCaseCommaOrEnd = list_state });
1300 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } });
1301 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
1302 try stack.append(State{ .SwitchCaseFirstItem = &node.items });
14121303
14131304 continue;
14141305 },
......@@ -1416,10 +1307,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
14161307 State.SwitchCaseCommaOrEnd => |list_state| {
14171308 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
14181309 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1419 *list_state.ptr = end;
1310 (list_state.ptr).* = end;
14201311 continue;
14211312 } else {
1422 try stack.append(State { .SwitchCaseOrEnd = list_state });
1313 try stack.append(State{ .SwitchCaseOrEnd = list_state });
14231314 continue;
14241315 },
14251316 ExpectCommaOrEndResult.parse_error => |e| {
......@@ -1434,29 +1325,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
14341325 const token_index = token.index;
14351326 const token_ptr = token.ptr;
14361327 if (token_ptr.id == Token.Id.Keyword_else) {
1437 const else_node = try arena.construct(ast.Node.SwitchElse {
1438 .base = ast.Node{ .id = ast.Node.Id.SwitchElse},
1328 const else_node = try arena.construct(ast.Node.SwitchElse{
1329 .base = ast.Node{ .id = ast.Node.Id.SwitchElse },
14391330 .token = token_index,
14401331 });
14411332 try case_items.push(&else_node.base);
14421333
1443 try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight });
1334 try stack.append(State{ .ExpectToken = Token.Id.EqualAngleBracketRight });
14441335 continue;
14451336 } else {
14461337 putBackToken(&tok_it, &tree);
1447 try stack.append(State { .SwitchCaseItem = case_items });
1338 try stack.append(State{ .SwitchCaseItem = case_items });
14481339 continue;
14491340 }
14501341 },
14511342 State.SwitchCaseItem => |case_items| {
1452 stack.append(State { .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable;
1453 try stack.append(State { .RangeExpressionBegin = OptionalCtx { .Required = try case_items.addOne() } });
1343 stack.append(State{ .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable;
1344 try stack.append(State{ .RangeExpressionBegin = OptionalCtx{ .Required = try case_items.addOne() } });
14541345 },
14551346 State.SwitchCaseItemCommaOrEnd => |case_items| {
14561347 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.EqualAngleBracketRight)) {
14571348 ExpectCommaOrEndResult.end_token => |t| {
14581349 if (t == null) {
1459 stack.append(State { .SwitchCaseItem = case_items }) catch unreachable;
1350 stack.append(State{ .SwitchCaseItem = case_items }) catch unreachable;
14601351 }
14611352 continue;
14621353 },
......@@ -1468,10 +1359,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
14681359 continue;
14691360 },
14701361
1471
14721362 State.SuspendBody => |suspend_node| {
14731363 if (suspend_node.payload != null) {
1474 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = &suspend_node.body } });
1364 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = &suspend_node.body } });
14751365 }
14761366 continue;
14771367 },
......@@ -1481,13 +1371,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
14811371 }
14821372
14831373 async_node.rangle_bracket = TokenIndex(0);
1484 try stack.append(State {
1485 .ExpectTokenSave = ExpectTokenSave {
1486 .id = Token.Id.AngleBracketRight,
1487 .ptr = &??async_node.rangle_bracket,
1488 }
1489 });
1490 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &async_node.allocator_type } });
1374 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1375 .id = Token.Id.AngleBracketRight,
1376 .ptr = &??async_node.rangle_bracket,
1377 } });
1378 try stack.append(State{ .TypeExprBegin = OptionalCtx{ .RequiredNull = &async_node.allocator_type } });
14911379 continue;
14921380 },
14931381 State.AsyncEnd => |ctx| {
......@@ -1506,27 +1394,20 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
15061394 continue;
15071395 }
15081396
1509 *(try tree.errors.addOne()) = Error {
1510 .ExpectedCall = Error.ExpectedCall { .node = node },
1511 };
1397 ((try tree.errors.addOne())).* = Error{ .ExpectedCall = Error.ExpectedCall{ .node = node } };
15121398 return tree;
15131399 },
15141400 else => {
1515 *(try tree.errors.addOne()) = Error {
1516 .ExpectedCallOrFnProto = Error.ExpectedCallOrFnProto { .node = node },
1517 };
1401 ((try tree.errors.addOne())).* = Error{ .ExpectedCallOrFnProto = Error.ExpectedCallOrFnProto{ .node = node } };
15181402 return tree;
1519 }
1403 },
15201404 }
15211405 },
15221406
1523
15241407 State.ExternType => |ctx| {
15251408 if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| {
1526 const fn_proto = try arena.construct(ast.Node.FnProto {
1527 .base = ast.Node {
1528 .id = ast.Node.Id.FnProto,
1529 },
1409 const fn_proto = try arena.construct(ast.Node.FnProto{
1410 .base = ast.Node{ .id = ast.Node.Id.FnProto },
15301411 .doc_comments = ctx.comments,
15311412 .visib_token = null,
15321413 .name_token = null,
......@@ -1542,17 +1423,15 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
15421423 .align_expr = null,
15431424 });
15441425 ctx.opt_ctx.store(&fn_proto.base);
1545 stack.append(State { .FnProto = fn_proto }) catch unreachable;
1426 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
15461427 continue;
15471428 }
15481429
1549 stack.append(State {
1550 .ContainerKind = ContainerKindCtx {
1551 .opt_ctx = ctx.opt_ctx,
1552 .ltoken = ctx.extern_token,
1553 .layout = ast.Node.ContainerDecl.Layout.Extern,
1554 },
1555 }) catch unreachable;
1430 stack.append(State{ .ContainerKind = ContainerKindCtx{
1431 .opt_ctx = ctx.opt_ctx,
1432 .ltoken = ctx.extern_token,
1433 .layout = ast.Node.ContainerDecl.Layout.Extern,
1434 } }) catch unreachable;
15561435 continue;
15571436 },
15581437 State.SliceOrArrayAccess => |node| {
......@@ -1562,20 +1441,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
15621441 switch (token_ptr.id) {
15631442 Token.Id.Ellipsis2 => {
15641443 const start = node.op.ArrayAccess;
1565 node.op = ast.Node.SuffixOp.Op {
1566 .Slice = ast.Node.SuffixOp.Op.Slice {
1567 .start = start,
1568 .end = null,
1569 }
1570 };
1444 node.op = ast.Node.SuffixOp.Op{ .Slice = ast.Node.SuffixOp.Op.Slice{
1445 .start = start,
1446 .end = null,
1447 } };
15711448
1572 stack.append(State {
1573 .ExpectTokenSave = ExpectTokenSave {
1574 .id = Token.Id.RBracket,
1575 .ptr = &node.rtoken,
1576 }
1577 }) catch unreachable;
1578 try stack.append(State { .Expression = OptionalCtx { .Optional = &node.op.Slice.end } });
1449 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1450 .id = Token.Id.RBracket,
1451 .ptr = &node.rtoken,
1452 } }) catch unreachable;
1453 try stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.op.Slice.end } });
15791454 continue;
15801455 },
15811456 Token.Id.RBracket => {
......@@ -1583,33 +1458,29 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
15831458 continue;
15841459 },
15851460 else => {
1586 *(try tree.errors.addOne()) = Error {
1587 .ExpectedSliceOrRBracket = Error.ExpectedSliceOrRBracket { .token = token_index },
1588 };
1461 ((try tree.errors.addOne())).* = Error{ .ExpectedSliceOrRBracket = Error.ExpectedSliceOrRBracket{ .token = token_index } };
15891462 return tree;
1590 }
1463 },
15911464 }
15921465 },
15931466 State.SliceOrArrayType => |node| {
15941467 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {
1595 node.op = ast.Node.PrefixOp.Op {
1596 .SliceType = ast.Node.PrefixOp.AddrOfInfo {
1597 .align_expr = null,
1598 .bit_offset_start_token = null,
1599 .bit_offset_end_token = null,
1600 .const_token = null,
1601 .volatile_token = null,
1602 }
1603 };
1604 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
1605 try stack.append(State { .AddrOfModifiers = &node.op.SliceType });
1468 node.op = ast.Node.PrefixOp.Op{ .SliceType = ast.Node.PrefixOp.AddrOfInfo{
1469 .align_expr = null,
1470 .bit_offset_start_token = null,
1471 .bit_offset_end_token = null,
1472 .const_token = null,
1473 .volatile_token = null,
1474 } };
1475 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1476 try stack.append(State{ .AddrOfModifiers = &node.op.SliceType });
16061477 continue;
16071478 }
16081479
1609 node.op = ast.Node.PrefixOp.Op { .ArrayType = undefined };
1610 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
1611 try stack.append(State { .ExpectToken = Token.Id.RBracket });
1612 try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayType } });
1480 node.op = ast.Node.PrefixOp.Op{ .ArrayType = undefined };
1481 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1482 try stack.append(State{ .ExpectToken = Token.Id.RBracket });
1483 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.op.ArrayType } });
16131484 continue;
16141485 },
16151486 State.AddrOfModifiers => |addr_of_info| {
......@@ -1620,22 +1491,18 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
16201491 Token.Id.Keyword_align => {
16211492 stack.append(state) catch unreachable;
16221493 if (addr_of_info.align_expr != null) {
1623 *(try tree.errors.addOne()) = Error {
1624 .ExtraAlignQualifier = Error.ExtraAlignQualifier { .token = token_index },
1625 };
1494 ((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };
16261495 return tree;
16271496 }
1628 try stack.append(State { .ExpectToken = Token.Id.RParen });
1629 try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &addr_of_info.align_expr} });
1630 try stack.append(State { .ExpectToken = Token.Id.LParen });
1497 try stack.append(State{ .ExpectToken = Token.Id.RParen });
1498 try stack.append(State{ .Expression = OptionalCtx{ .RequiredNull = &addr_of_info.align_expr } });
1499 try stack.append(State{ .ExpectToken = Token.Id.LParen });
16311500 continue;
16321501 },
16331502 Token.Id.Keyword_const => {
16341503 stack.append(state) catch unreachable;
16351504 if (addr_of_info.const_token != null) {
1636 *(try tree.errors.addOne()) = Error {
1637 .ExtraConstQualifier = Error.ExtraConstQualifier { .token = token_index },
1638 };
1505 ((try tree.errors.addOne())).* = Error{ .ExtraConstQualifier = Error.ExtraConstQualifier{ .token = token_index } };
16391506 return tree;
16401507 }
16411508 addr_of_info.const_token = token_index;
......@@ -1644,9 +1511,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
16441511 Token.Id.Keyword_volatile => {
16451512 stack.append(state) catch unreachable;
16461513 if (addr_of_info.volatile_token != null) {
1647 *(try tree.errors.addOne()) = Error {
1648 .ExtraVolatileQualifier = Error.ExtraVolatileQualifier { .token = token_index },
1649 };
1514 ((try tree.errors.addOne())).* = Error{ .ExtraVolatileQualifier = Error.ExtraVolatileQualifier{ .token = token_index } };
16501515 return tree;
16511516 }
16521517 addr_of_info.volatile_token = token_index;
......@@ -1659,19 +1524,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
16591524 }
16601525 },
16611526
1662
16631527 State.Payload => |opt_ctx| {
16641528 const token = nextToken(&tok_it, &tree);
16651529 const token_index = token.index;
16661530 const token_ptr = token.ptr;
16671531 if (token_ptr.id != Token.Id.Pipe) {
16681532 if (opt_ctx != OptionalCtx.Optional) {
1669 *(try tree.errors.addOne()) = Error {
1670 .ExpectedToken = Error.ExpectedToken {
1671 .token = token_index,
1672 .expected_id = Token.Id.Pipe,
1673 },
1674 };
1533 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1534 .token = token_index,
1535 .expected_id = Token.Id.Pipe,
1536 } };
16751537 return tree;
16761538 }
16771539
......@@ -1679,21 +1541,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
16791541 continue;
16801542 }
16811543
1682 const node = try arena.construct(ast.Node.Payload {
1683 .base = ast.Node {.id = ast.Node.Id.Payload },
1544 const node = try arena.construct(ast.Node.Payload{
1545 .base = ast.Node{ .id = ast.Node.Id.Payload },
16841546 .lpipe = token_index,
16851547 .error_symbol = undefined,
1686 .rpipe = undefined
1548 .rpipe = undefined,
16871549 });
16881550 opt_ctx.store(&node.base);
16891551
1690 stack.append(State {
1691 .ExpectTokenSave = ExpectTokenSave {
1692 .id = Token.Id.Pipe,
1693 .ptr = &node.rpipe,
1694 }
1695 }) catch unreachable;
1696 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.error_symbol } });
1552 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1553 .id = Token.Id.Pipe,
1554 .ptr = &node.rpipe,
1555 } }) catch unreachable;
1556 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.error_symbol } });
16971557 continue;
16981558 },
16991559 State.PointerPayload => |opt_ctx| {
......@@ -1702,12 +1562,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
17021562 const token_ptr = token.ptr;
17031563 if (token_ptr.id != Token.Id.Pipe) {
17041564 if (opt_ctx != OptionalCtx.Optional) {
1705 *(try tree.errors.addOne()) = Error {
1706 .ExpectedToken = Error.ExpectedToken {
1707 .token = token_index,
1708 .expected_id = Token.Id.Pipe,
1709 },
1710 };
1565 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1566 .token = token_index,
1567 .expected_id = Token.Id.Pipe,
1568 } };
17111569 return tree;
17121570 }
17131571
......@@ -1715,28 +1573,24 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
17151573 continue;
17161574 }
17171575
1718 const node = try arena.construct(ast.Node.PointerPayload {
1719 .base = ast.Node {.id = ast.Node.Id.PointerPayload },
1576 const node = try arena.construct(ast.Node.PointerPayload{
1577 .base = ast.Node{ .id = ast.Node.Id.PointerPayload },
17201578 .lpipe = token_index,
17211579 .ptr_token = null,
17221580 .value_symbol = undefined,
1723 .rpipe = undefined
1581 .rpipe = undefined,
17241582 });
17251583 opt_ctx.store(&node.base);
17261584
1727 try stack.append(State {
1728 .ExpectTokenSave = ExpectTokenSave {
1729 .id = Token.Id.Pipe,
1730 .ptr = &node.rpipe,
1731 }
1732 });
1733 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });
1734 try stack.append(State {
1735 .OptionalTokenSave = OptionalTokenSave {
1736 .id = Token.Id.Asterisk,
1737 .ptr = &node.ptr_token,
1738 }
1739 });
1585 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1586 .id = Token.Id.Pipe,
1587 .ptr = &node.rpipe,
1588 } });
1589 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.value_symbol } });
1590 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
1591 .id = Token.Id.Asterisk,
1592 .ptr = &node.ptr_token,
1593 } });
17401594 continue;
17411595 },
17421596 State.PointerIndexPayload => |opt_ctx| {
......@@ -1745,12 +1599,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
17451599 const token_ptr = token.ptr;
17461600 if (token_ptr.id != Token.Id.Pipe) {
17471601 if (opt_ctx != OptionalCtx.Optional) {
1748 *(try tree.errors.addOne()) = Error {
1749 .ExpectedToken = Error.ExpectedToken {
1750 .token = token_index,
1751 .expected_id = Token.Id.Pipe,
1752 },
1753 };
1602 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
1603 .token = token_index,
1604 .expected_id = Token.Id.Pipe,
1605 } };
17541606 return tree;
17551607 }
17561608
......@@ -1758,61 +1610,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
17581610 continue;
17591611 }
17601612
1761 const node = try arena.construct(ast.Node.PointerIndexPayload {
1762 .base = ast.Node {.id = ast.Node.Id.PointerIndexPayload },
1613 const node = try arena.construct(ast.Node.PointerIndexPayload{
1614 .base = ast.Node{ .id = ast.Node.Id.PointerIndexPayload },
17631615 .lpipe = token_index,
17641616 .ptr_token = null,
17651617 .value_symbol = undefined,
17661618 .index_symbol = null,
1767 .rpipe = undefined
1619 .rpipe = undefined,
17681620 });
17691621 opt_ctx.store(&node.base);
17701622
1771 stack.append(State {
1772 .ExpectTokenSave = ExpectTokenSave {
1773 .id = Token.Id.Pipe,
1774 .ptr = &node.rpipe,
1775 }
1776 }) catch unreachable;
1777 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.index_symbol } });
1778 try stack.append(State { .IfToken = Token.Id.Comma });
1779 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } });
1780 try stack.append(State {
1781 .OptionalTokenSave = OptionalTokenSave {
1782 .id = Token.Id.Asterisk,
1783 .ptr = &node.ptr_token,
1784 }
1785 });
1623 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
1624 .id = Token.Id.Pipe,
1625 .ptr = &node.rpipe,
1626 } }) catch unreachable;
1627 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.index_symbol } });
1628 try stack.append(State{ .IfToken = Token.Id.Comma });
1629 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.value_symbol } });
1630 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
1631 .id = Token.Id.Asterisk,
1632 .ptr = &node.ptr_token,
1633 } });
17861634 continue;
17871635 },
17881636
1789
17901637 State.Expression => |opt_ctx| {
17911638 const token = nextToken(&tok_it, &tree);
17921639 const token_index = token.index;
17931640 const token_ptr = token.ptr;
17941641 switch (token_ptr.id) {
1795 Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => {
1796 const node = try arena.construct(ast.Node.ControlFlowExpression {
1797 .base = ast.Node {.id = ast.Node.Id.ControlFlowExpression },
1642 Token.Id.Keyword_return,
1643 Token.Id.Keyword_break,
1644 Token.Id.Keyword_continue => {
1645 const node = try arena.construct(ast.Node.ControlFlowExpression{
1646 .base = ast.Node{ .id = ast.Node.Id.ControlFlowExpression },
17981647 .ltoken = token_index,
17991648 .kind = undefined,
18001649 .rhs = null,
18011650 });
18021651 opt_ctx.store(&node.base);
18031652
1804 stack.append(State { .Expression = OptionalCtx { .Optional = &node.rhs } }) catch unreachable;
1653 stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.rhs } }) catch unreachable;
18051654
18061655 switch (token_ptr.id) {
18071656 Token.Id.Keyword_break => {
1808 node.kind = ast.Node.ControlFlowExpression.Kind { .Break = null };
1809 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Break } });
1810 try stack.append(State { .IfToken = Token.Id.Colon });
1657 node.kind = ast.Node.ControlFlowExpression.Kind{ .Break = null };
1658 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.kind.Break } });
1659 try stack.append(State{ .IfToken = Token.Id.Colon });
18111660 },
18121661 Token.Id.Keyword_continue => {
1813 node.kind = ast.Node.ControlFlowExpression.Kind { .Continue = null };
1814 try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Continue } });
1815 try stack.append(State { .IfToken = Token.Id.Colon });
1662 node.kind = ast.Node.ControlFlowExpression.Kind{ .Continue = null };
1663 try stack.append(State{ .Identifier = OptionalCtx{ .RequiredNull = &node.kind.Continue } });
1664 try stack.append(State{ .IfToken = Token.Id.Colon });
18161665 },
18171666 Token.Id.Keyword_return => {
18181667 node.kind = ast.Node.ControlFlowExpression.Kind.Return;
......@@ -1821,56 +1670,58 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
18211670 }
18221671 continue;
18231672 },
1824 Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => {
1825 const node = try arena.construct(ast.Node.PrefixOp {
1826 .base = ast.Node {.id = ast.Node.Id.PrefixOp },
1673 Token.Id.Keyword_try,
1674 Token.Id.Keyword_cancel,
1675 Token.Id.Keyword_resume => {
1676 const node = try arena.construct(ast.Node.PrefixOp{
1677 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
18271678 .op_token = token_index,
18281679 .op = switch (token_ptr.id) {
1829 Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{} },
1830 Token.Id.Keyword_cancel => ast.Node.PrefixOp.Op { .Cancel = void{} },
1831 Token.Id.Keyword_resume => ast.Node.PrefixOp.Op { .Resume = void{} },
1680 Token.Id.Keyword_try => ast.Node.PrefixOp.Op{ .Try = void{} },
1681 Token.Id.Keyword_cancel => ast.Node.PrefixOp.Op{ .Cancel = void{} },
1682 Token.Id.Keyword_resume => ast.Node.PrefixOp.Op{ .Resume = void{} },
18321683 else => unreachable,
18331684 },
18341685 .rhs = undefined,
18351686 });
18361687 opt_ctx.store(&node.base);
18371688
1838 stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
1689 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
18391690 continue;
18401691 },
18411692 else => {
18421693 if (!try parseBlockExpr(&stack, arena, opt_ctx, token_ptr, token_index)) {
18431694 putBackToken(&tok_it, &tree);
1844 stack.append(State { .UnwrapExpressionBegin = opt_ctx }) catch unreachable;
1695 stack.append(State{ .UnwrapExpressionBegin = opt_ctx }) catch unreachable;
18451696 }
18461697 continue;
1847 }
1698 },
18481699 }
18491700 },
18501701 State.RangeExpressionBegin => |opt_ctx| {
1851 stack.append(State { .RangeExpressionEnd = opt_ctx }) catch unreachable;
1852 try stack.append(State { .Expression = opt_ctx });
1702 stack.append(State{ .RangeExpressionEnd = opt_ctx }) catch unreachable;
1703 try stack.append(State{ .Expression = opt_ctx });
18531704 continue;
18541705 },
18551706 State.RangeExpressionEnd => |opt_ctx| {
18561707 const lhs = opt_ctx.get() ?? continue;
18571708
18581709 if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| {
1859 const node = try arena.construct(ast.Node.InfixOp {
1860 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1710 const node = try arena.construct(ast.Node.InfixOp{
1711 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
18611712 .lhs = lhs,
18621713 .op_token = ellipsis3,
18631714 .op = ast.Node.InfixOp.Op.Range,
18641715 .rhs = undefined,
18651716 });
18661717 opt_ctx.store(&node.base);
1867 stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
1718 stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
18681719 continue;
18691720 }
18701721 },
18711722 State.AssignmentExpressionBegin => |opt_ctx| {
1872 stack.append(State { .AssignmentExpressionEnd = opt_ctx }) catch unreachable;
1873 try stack.append(State { .Expression = opt_ctx });
1723 stack.append(State{ .AssignmentExpressionEnd = opt_ctx }) catch unreachable;
1724 try stack.append(State{ .Expression = opt_ctx });
18741725 continue;
18751726 },
18761727
......@@ -1881,16 +1732,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
18811732 const token_index = token.index;
18821733 const token_ptr = token.ptr;
18831734 if (tokenIdToAssignment(token_ptr.id)) |ass_id| {
1884 const node = try arena.construct(ast.Node.InfixOp {
1885 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1735 const node = try arena.construct(ast.Node.InfixOp{
1736 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
18861737 .lhs = lhs,
18871738 .op_token = token_index,
18881739 .op = ass_id,
18891740 .rhs = undefined,
18901741 });
18911742 opt_ctx.store(&node.base);
1892 stack.append(State { .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1893 try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } });
1743 stack.append(State{ .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1744 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } });
18941745 continue;
18951746 } else {
18961747 putBackToken(&tok_it, &tree);
......@@ -1899,8 +1750,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
18991750 },
19001751
19011752 State.UnwrapExpressionBegin => |opt_ctx| {
1902 stack.append(State { .UnwrapExpressionEnd = opt_ctx }) catch unreachable;
1903 try stack.append(State { .BoolOrExpressionBegin = opt_ctx });
1753 stack.append(State{ .UnwrapExpressionEnd = opt_ctx }) catch unreachable;
1754 try stack.append(State{ .BoolOrExpressionBegin = opt_ctx });
19041755 continue;
19051756 },
19061757
......@@ -1911,8 +1762,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19111762 const token_index = token.index;
19121763 const token_ptr = token.ptr;
19131764 if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| {
1914 const node = try arena.construct(ast.Node.InfixOp {
1915 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1765 const node = try arena.construct(ast.Node.InfixOp{
1766 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19161767 .lhs = lhs,
19171768 .op_token = token_index,
19181769 .op = unwrap_id,
......@@ -1920,11 +1771,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19201771 });
19211772 opt_ctx.store(&node.base);
19221773
1923 stack.append(State { .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1924 try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } });
1774 stack.append(State{ .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1775 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } });
19251776
19261777 if (node.op == ast.Node.InfixOp.Op.Catch) {
1927 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.op.Catch } });
1778 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.op.Catch } });
19281779 }
19291780 continue;
19301781 } else {
......@@ -1934,8 +1785,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19341785 },
19351786
19361787 State.BoolOrExpressionBegin => |opt_ctx| {
1937 stack.append(State { .BoolOrExpressionEnd = opt_ctx }) catch unreachable;
1938 try stack.append(State { .BoolAndExpressionBegin = opt_ctx });
1788 stack.append(State{ .BoolOrExpressionEnd = opt_ctx }) catch unreachable;
1789 try stack.append(State{ .BoolAndExpressionBegin = opt_ctx });
19391790 continue;
19401791 },
19411792
......@@ -1943,23 +1794,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19431794 const lhs = opt_ctx.get() ?? continue;
19441795
19451796 if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| {
1946 const node = try arena.construct(ast.Node.InfixOp {
1947 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1797 const node = try arena.construct(ast.Node.InfixOp{
1798 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19481799 .lhs = lhs,
19491800 .op_token = or_token,
19501801 .op = ast.Node.InfixOp.Op.BoolOr,
19511802 .rhs = undefined,
19521803 });
19531804 opt_ctx.store(&node.base);
1954 stack.append(State { .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1955 try stack.append(State { .BoolAndExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1805 stack.append(State{ .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1806 try stack.append(State{ .BoolAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
19561807 continue;
19571808 }
19581809 },
19591810
19601811 State.BoolAndExpressionBegin => |opt_ctx| {
1961 stack.append(State { .BoolAndExpressionEnd = opt_ctx }) catch unreachable;
1962 try stack.append(State { .ComparisonExpressionBegin = opt_ctx });
1812 stack.append(State{ .BoolAndExpressionEnd = opt_ctx }) catch unreachable;
1813 try stack.append(State{ .ComparisonExpressionBegin = opt_ctx });
19631814 continue;
19641815 },
19651816
......@@ -1967,23 +1818,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19671818 const lhs = opt_ctx.get() ?? continue;
19681819
19691820 if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| {
1970 const node = try arena.construct(ast.Node.InfixOp {
1971 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1821 const node = try arena.construct(ast.Node.InfixOp{
1822 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19721823 .lhs = lhs,
19731824 .op_token = and_token,
19741825 .op = ast.Node.InfixOp.Op.BoolAnd,
19751826 .rhs = undefined,
19761827 });
19771828 opt_ctx.store(&node.base);
1978 stack.append(State { .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1979 try stack.append(State { .ComparisonExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1829 stack.append(State{ .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1830 try stack.append(State{ .ComparisonExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
19801831 continue;
19811832 }
19821833 },
19831834
19841835 State.ComparisonExpressionBegin => |opt_ctx| {
1985 stack.append(State { .ComparisonExpressionEnd = opt_ctx }) catch unreachable;
1986 try stack.append(State { .BinaryOrExpressionBegin = opt_ctx });
1836 stack.append(State{ .ComparisonExpressionEnd = opt_ctx }) catch unreachable;
1837 try stack.append(State{ .BinaryOrExpressionBegin = opt_ctx });
19871838 continue;
19881839 },
19891840
......@@ -1994,16 +1845,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
19941845 const token_index = token.index;
19951846 const token_ptr = token.ptr;
19961847 if (tokenIdToComparison(token_ptr.id)) |comp_id| {
1997 const node = try arena.construct(ast.Node.InfixOp {
1998 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1848 const node = try arena.construct(ast.Node.InfixOp{
1849 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
19991850 .lhs = lhs,
20001851 .op_token = token_index,
20011852 .op = comp_id,
20021853 .rhs = undefined,
20031854 });
20041855 opt_ctx.store(&node.base);
2005 stack.append(State { .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2006 try stack.append(State { .BinaryOrExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1856 stack.append(State{ .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1857 try stack.append(State{ .BinaryOrExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
20071858 continue;
20081859 } else {
20091860 putBackToken(&tok_it, &tree);
......@@ -2012,8 +1863,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
20121863 },
20131864
20141865 State.BinaryOrExpressionBegin => |opt_ctx| {
2015 stack.append(State { .BinaryOrExpressionEnd = opt_ctx }) catch unreachable;
2016 try stack.append(State { .BinaryXorExpressionBegin = opt_ctx });
1866 stack.append(State{ .BinaryOrExpressionEnd = opt_ctx }) catch unreachable;
1867 try stack.append(State{ .BinaryXorExpressionBegin = opt_ctx });
20171868 continue;
20181869 },
20191870
......@@ -2021,23 +1872,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
20211872 const lhs = opt_ctx.get() ?? continue;
20221873
20231874 if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| {
2024 const node = try arena.construct(ast.Node.InfixOp {
2025 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1875 const node = try arena.construct(ast.Node.InfixOp{
1876 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20261877 .lhs = lhs,
20271878 .op_token = pipe,
20281879 .op = ast.Node.InfixOp.Op.BitOr,
20291880 .rhs = undefined,
20301881 });
20311882 opt_ctx.store(&node.base);
2032 stack.append(State { .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2033 try stack.append(State { .BinaryXorExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1883 stack.append(State{ .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1884 try stack.append(State{ .BinaryXorExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
20341885 continue;
20351886 }
20361887 },
20371888
20381889 State.BinaryXorExpressionBegin => |opt_ctx| {
2039 stack.append(State { .BinaryXorExpressionEnd = opt_ctx }) catch unreachable;
2040 try stack.append(State { .BinaryAndExpressionBegin = opt_ctx });
1890 stack.append(State{ .BinaryXorExpressionEnd = opt_ctx }) catch unreachable;
1891 try stack.append(State{ .BinaryAndExpressionBegin = opt_ctx });
20411892 continue;
20421893 },
20431894
......@@ -2045,23 +1896,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
20451896 const lhs = opt_ctx.get() ?? continue;
20461897
20471898 if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| {
2048 const node = try arena.construct(ast.Node.InfixOp {
2049 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1899 const node = try arena.construct(ast.Node.InfixOp{
1900 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20501901 .lhs = lhs,
20511902 .op_token = caret,
20521903 .op = ast.Node.InfixOp.Op.BitXor,
20531904 .rhs = undefined,
20541905 });
20551906 opt_ctx.store(&node.base);
2056 stack.append(State { .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2057 try stack.append(State { .BinaryAndExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1907 stack.append(State{ .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1908 try stack.append(State{ .BinaryAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
20581909 continue;
20591910 }
20601911 },
20611912
20621913 State.BinaryAndExpressionBegin => |opt_ctx| {
2063 stack.append(State { .BinaryAndExpressionEnd = opt_ctx }) catch unreachable;
2064 try stack.append(State { .BitShiftExpressionBegin = opt_ctx });
1914 stack.append(State{ .BinaryAndExpressionEnd = opt_ctx }) catch unreachable;
1915 try stack.append(State{ .BitShiftExpressionBegin = opt_ctx });
20651916 continue;
20661917 },
20671918
......@@ -2069,23 +1920,23 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
20691920 const lhs = opt_ctx.get() ?? continue;
20701921
20711922 if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| {
2072 const node = try arena.construct(ast.Node.InfixOp {
2073 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1923 const node = try arena.construct(ast.Node.InfixOp{
1924 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
20741925 .lhs = lhs,
20751926 .op_token = ampersand,
20761927 .op = ast.Node.InfixOp.Op.BitAnd,
20771928 .rhs = undefined,
20781929 });
20791930 opt_ctx.store(&node.base);
2080 stack.append(State { .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2081 try stack.append(State { .BitShiftExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1931 stack.append(State{ .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1932 try stack.append(State{ .BitShiftExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
20821933 continue;
20831934 }
20841935 },
20851936
20861937 State.BitShiftExpressionBegin => |opt_ctx| {
2087 stack.append(State { .BitShiftExpressionEnd = opt_ctx }) catch unreachable;
2088 try stack.append(State { .AdditionExpressionBegin = opt_ctx });
1938 stack.append(State{ .BitShiftExpressionEnd = opt_ctx }) catch unreachable;
1939 try stack.append(State{ .AdditionExpressionBegin = opt_ctx });
20891940 continue;
20901941 },
20911942
......@@ -2096,16 +1947,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
20961947 const token_index = token.index;
20971948 const token_ptr = token.ptr;
20981949 if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| {
2099 const node = try arena.construct(ast.Node.InfixOp {
2100 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1950 const node = try arena.construct(ast.Node.InfixOp{
1951 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21011952 .lhs = lhs,
21021953 .op_token = token_index,
21031954 .op = bitshift_id,
21041955 .rhs = undefined,
21051956 });
21061957 opt_ctx.store(&node.base);
2107 stack.append(State { .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2108 try stack.append(State { .AdditionExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1958 stack.append(State{ .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1959 try stack.append(State{ .AdditionExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
21091960 continue;
21101961 } else {
21111962 putBackToken(&tok_it, &tree);
......@@ -2114,8 +1965,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21141965 },
21151966
21161967 State.AdditionExpressionBegin => |opt_ctx| {
2117 stack.append(State { .AdditionExpressionEnd = opt_ctx }) catch unreachable;
2118 try stack.append(State { .MultiplyExpressionBegin = opt_ctx });
1968 stack.append(State{ .AdditionExpressionEnd = opt_ctx }) catch unreachable;
1969 try stack.append(State{ .MultiplyExpressionBegin = opt_ctx });
21191970 continue;
21201971 },
21211972
......@@ -2126,16 +1977,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21261977 const token_index = token.index;
21271978 const token_ptr = token.ptr;
21281979 if (tokenIdToAddition(token_ptr.id)) |add_id| {
2129 const node = try arena.construct(ast.Node.InfixOp {
2130 .base = ast.Node {.id = ast.Node.Id.InfixOp },
1980 const node = try arena.construct(ast.Node.InfixOp{
1981 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21311982 .lhs = lhs,
21321983 .op_token = token_index,
21331984 .op = add_id,
21341985 .rhs = undefined,
21351986 });
21361987 opt_ctx.store(&node.base);
2137 stack.append(State { .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2138 try stack.append(State { .MultiplyExpressionBegin = OptionalCtx { .Required = &node.rhs } });
1988 stack.append(State{ .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
1989 try stack.append(State{ .MultiplyExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
21391990 continue;
21401991 } else {
21411992 putBackToken(&tok_it, &tree);
......@@ -2144,8 +1995,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21441995 },
21451996
21461997 State.MultiplyExpressionBegin => |opt_ctx| {
2147 stack.append(State { .MultiplyExpressionEnd = opt_ctx }) catch unreachable;
2148 try stack.append(State { .CurlySuffixExpressionBegin = opt_ctx });
1998 stack.append(State{ .MultiplyExpressionEnd = opt_ctx }) catch unreachable;
1999 try stack.append(State{ .CurlySuffixExpressionBegin = opt_ctx });
21492000 continue;
21502001 },
21512002
......@@ -2156,16 +2007,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21562007 const token_index = token.index;
21572008 const token_ptr = token.ptr;
21582009 if (tokenIdToMultiply(token_ptr.id)) |mult_id| {
2159 const node = try arena.construct(ast.Node.InfixOp {
2160 .base = ast.Node {.id = ast.Node.Id.InfixOp },
2010 const node = try arena.construct(ast.Node.InfixOp{
2011 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
21612012 .lhs = lhs,
21622013 .op_token = token_index,
21632014 .op = mult_id,
21642015 .rhs = undefined,
21652016 });
21662017 opt_ctx.store(&node.base);
2167 stack.append(State { .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2168 try stack.append(State { .CurlySuffixExpressionBegin = OptionalCtx { .Required = &node.rhs } });
2018 stack.append(State{ .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2019 try stack.append(State{ .CurlySuffixExpressionBegin = OptionalCtx{ .Required = &node.rhs } });
21692020 continue;
21702021 } else {
21712022 putBackToken(&tok_it, &tree);
......@@ -2174,9 +2025,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21742025 },
21752026
21762027 State.CurlySuffixExpressionBegin => |opt_ctx| {
2177 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx }) catch unreachable;
2178 try stack.append(State { .IfToken = Token.Id.LBrace });
2179 try stack.append(State { .TypeExprBegin = opt_ctx });
2028 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx }) catch unreachable;
2029 try stack.append(State{ .IfToken = Token.Id.LBrace });
2030 try stack.append(State{ .TypeExprBegin = opt_ctx });
21802031 continue;
21812032 },
21822033
......@@ -2184,51 +2035,43 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
21842035 const lhs = opt_ctx.get() ?? continue;
21852036
21862037 if ((??tok_it.peek()).id == Token.Id.Period) {
2187 const node = try arena.construct(ast.Node.SuffixOp {
2188 .base = ast.Node { .id = ast.Node.Id.SuffixOp },
2038 const node = try arena.construct(ast.Node.SuffixOp{
2039 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
21892040 .lhs = lhs,
2190 .op = ast.Node.SuffixOp.Op {
2191 .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena),
2192 },
2041 .op = ast.Node.SuffixOp.Op{ .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
21932042 .rtoken = undefined,
21942043 });
21952044 opt_ctx.store(&node.base);
21962045
2197 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2198 try stack.append(State { .IfToken = Token.Id.LBrace });
2199 try stack.append(State {
2200 .FieldInitListItemOrEnd = ListSave(@typeOf(node.op.StructInitializer)) {
2201 .list = &node.op.StructInitializer,
2202 .ptr = &node.rtoken,
2203 }
2204 });
2046 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2047 try stack.append(State{ .IfToken = Token.Id.LBrace });
2048 try stack.append(State{ .FieldInitListItemOrEnd = ListSave(@typeOf(node.op.StructInitializer)){
2049 .list = &node.op.StructInitializer,
2050 .ptr = &node.rtoken,
2051 } });
22052052 continue;
22062053 }
22072054
2208 const node = try arena.construct(ast.Node.SuffixOp {
2209 .base = ast.Node {.id = ast.Node.Id.SuffixOp },
2055 const node = try arena.construct(ast.Node.SuffixOp{
2056 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
22102057 .lhs = lhs,
2211 .op = ast.Node.SuffixOp.Op {
2212 .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena),
2213 },
2058 .op = ast.Node.SuffixOp.Op{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) },
22142059 .rtoken = undefined,
22152060 });
22162061 opt_ctx.store(&node.base);
2217 stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2218 try stack.append(State { .IfToken = Token.Id.LBrace });
2219 try stack.append(State {
2220 .ExprListItemOrEnd = ExprListCtx {
2221 .list = &node.op.ArrayInitializer,
2222 .end = Token.Id.RBrace,
2223 .ptr = &node.rtoken,
2224 }
2225 });
2062 stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2063 try stack.append(State{ .IfToken = Token.Id.LBrace });
2064 try stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2065 .list = &node.op.ArrayInitializer,
2066 .end = Token.Id.RBrace,
2067 .ptr = &node.rtoken,
2068 } });
22262069 continue;
22272070 },
22282071
22292072 State.TypeExprBegin => |opt_ctx| {
2230 stack.append(State { .TypeExprEnd = opt_ctx }) catch unreachable;
2231 try stack.append(State { .PrefixOpExpression = opt_ctx });
2073 stack.append(State{ .TypeExprEnd = opt_ctx }) catch unreachable;
2074 try stack.append(State{ .PrefixOpExpression = opt_ctx });
22322075 continue;
22332076 },
22342077
......@@ -2236,16 +2079,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
22362079 const lhs = opt_ctx.get() ?? continue;
22372080
22382081 if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| {
2239 const node = try arena.construct(ast.Node.InfixOp {
2240 .base = ast.Node {.id = ast.Node.Id.InfixOp },
2082 const node = try arena.construct(ast.Node.InfixOp{
2083 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
22412084 .lhs = lhs,
22422085 .op_token = bang,
22432086 .op = ast.Node.InfixOp.Op.ErrorUnion,
22442087 .rhs = undefined,
22452088 });
22462089 opt_ctx.store(&node.base);
2247 stack.append(State { .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable;
2248 try stack.append(State { .PrefixOpExpression = OptionalCtx { .Required = &node.rhs } });
2090 stack.append(State{ .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable;
2091 try stack.append(State{ .PrefixOpExpression = OptionalCtx{ .Required = &node.rhs } });
22492092 continue;
22502093 }
22512094 },
......@@ -2255,8 +2098,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
22552098 const token_index = token.index;
22562099 const token_ptr = token.ptr;
22572100 if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| {
2258 var node = try arena.construct(ast.Node.PrefixOp {
2259 .base = ast.Node {.id = ast.Node.Id.PrefixOp },
2101 var node = try arena.construct(ast.Node.PrefixOp{
2102 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
22602103 .op_token = token_index,
22612104 .op = prefix_id,
22622105 .rhs = undefined,
......@@ -2265,8 +2108,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
22652108
22662109 // Treat '**' token as two derefs
22672110 if (token_ptr.id == Token.Id.AsteriskAsterisk) {
2268 const child = try arena.construct(ast.Node.PrefixOp {
2269 .base = ast.Node {.id = ast.Node.Id.PrefixOp},
2111 const child = try arena.construct(ast.Node.PrefixOp{
2112 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
22702113 .op_token = token_index,
22712114 .op = prefix_id,
22722115 .rhs = undefined,
......@@ -2275,40 +2118,38 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
22752118 node = child;
22762119 }
22772120
2278 stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable;
2121 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
22792122 if (node.op == ast.Node.PrefixOp.Op.AddrOf) {
2280 try stack.append(State { .AddrOfModifiers = &node.op.AddrOf });
2123 try stack.append(State{ .AddrOfModifiers = &node.op.AddrOf });
22812124 }
22822125 continue;
22832126 } else {
22842127 putBackToken(&tok_it, &tree);
2285 stack.append(State { .SuffixOpExpressionBegin = opt_ctx }) catch unreachable;
2128 stack.append(State{ .SuffixOpExpressionBegin = opt_ctx }) catch unreachable;
22862129 continue;
22872130 }
22882131 },
22892132
22902133 State.SuffixOpExpressionBegin => |opt_ctx| {
22912134 if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| {
2292 const async_node = try arena.construct(ast.Node.AsyncAttribute {
2293 .base = ast.Node {.id = ast.Node.Id.AsyncAttribute},
2135 const async_node = try arena.construct(ast.Node.AsyncAttribute{
2136 .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute },
22942137 .async_token = async_token,
22952138 .allocator_type = null,
22962139 .rangle_bracket = null,
22972140 });
2298 stack.append(State {
2299 .AsyncEnd = AsyncEndCtx {
2300 .ctx = opt_ctx,
2301 .attribute = async_node,
2302 }
2303 }) catch unreachable;
2304 try stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() });
2305 try stack.append(State { .PrimaryExpression = opt_ctx.toRequired() });
2306 try stack.append(State { .AsyncAllocator = async_node });
2141 stack.append(State{ .AsyncEnd = AsyncEndCtx{
2142 .ctx = opt_ctx,
2143 .attribute = async_node,
2144 } }) catch unreachable;
2145 try stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() });
2146 try stack.append(State{ .PrimaryExpression = opt_ctx.toRequired() });
2147 try stack.append(State{ .AsyncAllocator = async_node });
23072148 continue;
23082149 }
23092150
2310 stack.append(State { .SuffixOpExpressionEnd = opt_ctx }) catch unreachable;
2311 try stack.append(State { .PrimaryExpression = opt_ctx });
2151 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx }) catch unreachable;
2152 try stack.append(State{ .PrimaryExpression = opt_ctx });
23122153 continue;
23132154 },
23142155
......@@ -2320,48 +2161,42 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
23202161 const token_ptr = token.ptr;
23212162 switch (token_ptr.id) {
23222163 Token.Id.LParen => {
2323 const node = try arena.construct(ast.Node.SuffixOp {
2324 .base = ast.Node {.id = ast.Node.Id.SuffixOp },
2164 const node = try arena.construct(ast.Node.SuffixOp{
2165 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
23252166 .lhs = lhs,
2326 .op = ast.Node.SuffixOp.Op {
2327 .Call = ast.Node.SuffixOp.Op.Call {
2328 .params = ast.Node.SuffixOp.Op.Call.ParamList.init(arena),
2329 .async_attr = null,
2330 }
2331 },
2167 .op = ast.Node.SuffixOp.Op{ .Call = ast.Node.SuffixOp.Op.Call{
2168 .params = ast.Node.SuffixOp.Op.Call.ParamList.init(arena),
2169 .async_attr = null,
2170 } },
23322171 .rtoken = undefined,
23332172 });
23342173 opt_ctx.store(&node.base);
23352174
2336 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2337 try stack.append(State {
2338 .ExprListItemOrEnd = ExprListCtx {
2339 .list = &node.op.Call.params,
2340 .end = Token.Id.RParen,
2341 .ptr = &node.rtoken,
2342 }
2343 });
2175 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2176 try stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2177 .list = &node.op.Call.params,
2178 .end = Token.Id.RParen,
2179 .ptr = &node.rtoken,
2180 } });
23442181 continue;
23452182 },
23462183 Token.Id.LBracket => {
2347 const node = try arena.construct(ast.Node.SuffixOp {
2348 .base = ast.Node {.id = ast.Node.Id.SuffixOp },
2184 const node = try arena.construct(ast.Node.SuffixOp{
2185 .base = ast.Node{ .id = ast.Node.Id.SuffixOp },
23492186 .lhs = lhs,
2350 .op = ast.Node.SuffixOp.Op {
2351 .ArrayAccess = undefined,
2352 },
2353 .rtoken = undefined
2187 .op = ast.Node.SuffixOp.Op{ .ArrayAccess = undefined },
2188 .rtoken = undefined,
23542189 });
23552190 opt_ctx.store(&node.base);
23562191
2357 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2358 try stack.append(State { .SliceOrArrayAccess = node });
2359 try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayAccess }});
2192 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2193 try stack.append(State{ .SliceOrArrayAccess = node });
2194 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.op.ArrayAccess } });
23602195 continue;
23612196 },
23622197 Token.Id.Period => {
2363 const node = try arena.construct(ast.Node.InfixOp {
2364 .base = ast.Node {.id = ast.Node.Id.InfixOp },
2198 const node = try arena.construct(ast.Node.InfixOp{
2199 .base = ast.Node{ .id = ast.Node.Id.InfixOp },
23652200 .lhs = lhs,
23662201 .op_token = token_index,
23672202 .op = ast.Node.InfixOp.Op.Period,
......@@ -2369,8 +2204,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
23692204 });
23702205 opt_ctx.store(&node.base);
23712206
2372 stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2373 try stack.append(State { .Identifier = OptionalCtx { .Required = &node.rhs } });
2207 stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable;
2208 try stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.rhs } });
23742209 continue;
23752210 },
23762211 else => {
......@@ -2399,7 +2234,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
23992234 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.UndefinedLiteral, token.index);
24002235 continue;
24012236 },
2402 Token.Id.Keyword_true, Token.Id.Keyword_false => {
2237 Token.Id.Keyword_true,
2238 Token.Id.Keyword_false => {
24032239 _ = try createToCtxLiteral(arena, opt_ctx, ast.Node.BoolLiteral, token.index);
24042240 continue;
24052241 },
......@@ -2420,10 +2256,8 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
24202256 continue;
24212257 },
24222258 Token.Id.Keyword_promise => {
2423 const node = try arena.construct(ast.Node.PromiseType {
2424 .base = ast.Node {
2425 .id = ast.Node.Id.PromiseType,
2426 },
2259 const node = try arena.construct(ast.Node.PromiseType{
2260 .base = ast.Node{ .id = ast.Node.Id.PromiseType },
24272261 .promise_token = token.index,
24282262 .result = null,
24292263 });
......@@ -2435,121 +2269,108 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
24352269 putBackToken(&tok_it, &tree);
24362270 continue;
24372271 }
2438 node.result = ast.Node.PromiseType.Result {
2272 node.result = ast.Node.PromiseType.Result{
24392273 .arrow_token = next_token_index,
24402274 .return_type = undefined,
24412275 };
24422276 const return_type_ptr = &((??node.result).return_type);
2443 try stack.append(State { .Expression = OptionalCtx { .Required = return_type_ptr, } });
2277 try stack.append(State{ .Expression = OptionalCtx{ .Required = return_type_ptr } });
24442278 continue;
24452279 },
2446 Token.Id.StringLiteral, Token.Id.MultilineStringLiteralLine => {
2280 Token.Id.StringLiteral,
2281 Token.Id.MultilineStringLiteralLine => {
24472282 opt_ctx.store((try parseStringLiteral(arena, &tok_it, token.ptr, token.index, &tree)) ?? unreachable);
24482283 continue;
24492284 },
24502285 Token.Id.LParen => {
2451 const node = try arena.construct(ast.Node.GroupedExpression {
2452 .base = ast.Node {.id = ast.Node.Id.GroupedExpression },
2286 const node = try arena.construct(ast.Node.GroupedExpression{
2287 .base = ast.Node{ .id = ast.Node.Id.GroupedExpression },
24532288 .lparen = token.index,
24542289 .expr = undefined,
24552290 .rparen = undefined,
24562291 });
24572292 opt_ctx.store(&node.base);
24582293
2459 stack.append(State {
2460 .ExpectTokenSave = ExpectTokenSave {
2461 .id = Token.Id.RParen,
2462 .ptr = &node.rparen,
2463 }
2464 }) catch unreachable;
2465 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
2294 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2295 .id = Token.Id.RParen,
2296 .ptr = &node.rparen,
2297 } }) catch unreachable;
2298 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
24662299 continue;
24672300 },
24682301 Token.Id.Builtin => {
2469 const node = try arena.construct(ast.Node.BuiltinCall {
2470 .base = ast.Node {.id = ast.Node.Id.BuiltinCall },
2302 const node = try arena.construct(ast.Node.BuiltinCall{
2303 .base = ast.Node{ .id = ast.Node.Id.BuiltinCall },
24712304 .builtin_token = token.index,
24722305 .params = ast.Node.BuiltinCall.ParamList.init(arena),
24732306 .rparen_token = undefined,
24742307 });
24752308 opt_ctx.store(&node.base);
24762309
2477 stack.append(State {
2478 .ExprListItemOrEnd = ExprListCtx {
2479 .list = &node.params,
2480 .end = Token.Id.RParen,
2481 .ptr = &node.rparen_token,
2482 }
2483 }) catch unreachable;
2484 try stack.append(State { .ExpectToken = Token.Id.LParen, });
2310 stack.append(State{ .ExprListItemOrEnd = ExprListCtx{
2311 .list = &node.params,
2312 .end = Token.Id.RParen,
2313 .ptr = &node.rparen_token,
2314 } }) catch unreachable;
2315 try stack.append(State{ .ExpectToken = Token.Id.LParen });
24852316 continue;
24862317 },
24872318 Token.Id.LBracket => {
2488 const node = try arena.construct(ast.Node.PrefixOp {
2489 .base = ast.Node {.id = ast.Node.Id.PrefixOp },
2319 const node = try arena.construct(ast.Node.PrefixOp{
2320 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
24902321 .op_token = token.index,
24912322 .op = undefined,
24922323 .rhs = undefined,
24932324 });
24942325 opt_ctx.store(&node.base);
24952326
2496 stack.append(State { .SliceOrArrayType = node }) catch unreachable;
2327 stack.append(State{ .SliceOrArrayType = node }) catch unreachable;
24972328 continue;
24982329 },
24992330 Token.Id.Keyword_error => {
2500 stack.append(State {
2501 .ErrorTypeOrSetDecl = ErrorTypeOrSetDeclCtx {
2502 .error_token = token.index,
2503 .opt_ctx = opt_ctx
2504 }
2505 }) catch unreachable;
2331 stack.append(State{ .ErrorTypeOrSetDecl = ErrorTypeOrSetDeclCtx{
2332 .error_token = token.index,
2333 .opt_ctx = opt_ctx,
2334 } }) catch unreachable;
25062335 continue;
25072336 },
25082337 Token.Id.Keyword_packed => {
2509 stack.append(State {
2510 .ContainerKind = ContainerKindCtx {
2511 .opt_ctx = opt_ctx,
2512 .ltoken = token.index,
2513 .layout = ast.Node.ContainerDecl.Layout.Packed,
2514 },
2515 }) catch unreachable;
2338 stack.append(State{ .ContainerKind = ContainerKindCtx{
2339 .opt_ctx = opt_ctx,
2340 .ltoken = token.index,
2341 .layout = ast.Node.ContainerDecl.Layout.Packed,
2342 } }) catch unreachable;
25162343 continue;
25172344 },
25182345 Token.Id.Keyword_extern => {
2519 stack.append(State {
2520 .ExternType = ExternTypeCtx {
2521 .opt_ctx = opt_ctx,
2522 .extern_token = token.index,
2523 .comments = null,
2524 },
2525 }) catch unreachable;
2346 stack.append(State{ .ExternType = ExternTypeCtx{
2347 .opt_ctx = opt_ctx,
2348 .extern_token = token.index,
2349 .comments = null,
2350 } }) catch unreachable;
25262351 continue;
25272352 },
2528 Token.Id.Keyword_struct, Token.Id.Keyword_union, Token.Id.Keyword_enum => {
2353 Token.Id.Keyword_struct,
2354 Token.Id.Keyword_union,
2355 Token.Id.Keyword_enum => {
25292356 putBackToken(&tok_it, &tree);
2530 stack.append(State {
2531 .ContainerKind = ContainerKindCtx {
2532 .opt_ctx = opt_ctx,
2533 .ltoken = token.index,
2534 .layout = ast.Node.ContainerDecl.Layout.Auto,
2535 },
2536 }) catch unreachable;
2357 stack.append(State{ .ContainerKind = ContainerKindCtx{
2358 .opt_ctx = opt_ctx,
2359 .ltoken = token.index,
2360 .layout = ast.Node.ContainerDecl.Layout.Auto,
2361 } }) catch unreachable;
25372362 continue;
25382363 },
25392364 Token.Id.Identifier => {
2540 stack.append(State {
2541 .MaybeLabeledExpression = MaybeLabeledExpressionCtx {
2542 .label = token.index,
2543 .opt_ctx = opt_ctx
2544 }
2545 }) catch unreachable;
2365 stack.append(State{ .MaybeLabeledExpression = MaybeLabeledExpressionCtx{
2366 .label = token.index,
2367 .opt_ctx = opt_ctx,
2368 } }) catch unreachable;
25462369 continue;
25472370 },
25482371 Token.Id.Keyword_fn => {
2549 const fn_proto = try arena.construct(ast.Node.FnProto {
2550 .base = ast.Node {
2551 .id = ast.Node.Id.FnProto,
2552 },
2372 const fn_proto = try arena.construct(ast.Node.FnProto{
2373 .base = ast.Node{ .id = ast.Node.Id.FnProto },
25532374 .doc_comments = null,
25542375 .visib_token = null,
25552376 .name_token = null,
......@@ -2565,14 +2386,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
25652386 .align_expr = null,
25662387 });
25672388 opt_ctx.store(&fn_proto.base);
2568 stack.append(State { .FnProto = fn_proto }) catch unreachable;
2389 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
25692390 continue;
25702391 },
2571 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {
2572 const fn_proto = try arena.construct(ast.Node.FnProto {
2573 .base = ast.Node {
2574 .id = ast.Node.Id.FnProto,
2575 },
2392 Token.Id.Keyword_nakedcc,
2393 Token.Id.Keyword_stdcallcc => {
2394 const fn_proto = try arena.construct(ast.Node.FnProto{
2395 .base = ast.Node{ .id = ast.Node.Id.FnProto },
25762396 .doc_comments = null,
25772397 .visib_token = null,
25782398 .name_token = null,
......@@ -2588,18 +2408,16 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
25882408 .align_expr = null,
25892409 });
25902410 opt_ctx.store(&fn_proto.base);
2591 stack.append(State { .FnProto = fn_proto }) catch unreachable;
2592 try stack.append(State {
2593 .ExpectTokenSave = ExpectTokenSave {
2594 .id = Token.Id.Keyword_fn,
2595 .ptr = &fn_proto.fn_token
2596 }
2597 });
2411 stack.append(State{ .FnProto = fn_proto }) catch unreachable;
2412 try stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2413 .id = Token.Id.Keyword_fn,
2414 .ptr = &fn_proto.fn_token,
2415 } });
25982416 continue;
25992417 },
26002418 Token.Id.Keyword_asm => {
2601 const node = try arena.construct(ast.Node.Asm {
2602 .base = ast.Node {.id = ast.Node.Id.Asm },
2419 const node = try arena.construct(ast.Node.Asm{
2420 .base = ast.Node{ .id = ast.Node.Id.Asm },
26032421 .asm_token = token.index,
26042422 .volatile_token = null,
26052423 .template = undefined,
......@@ -2610,94 +2428,77 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
26102428 });
26112429 opt_ctx.store(&node.base);
26122430
2613 stack.append(State {
2614 .ExpectTokenSave = ExpectTokenSave {
2615 .id = Token.Id.RParen,
2616 .ptr = &node.rparen,
2617 }
2618 }) catch unreachable;
2619 try stack.append(State { .AsmClobberItems = &node.clobbers });
2620 try stack.append(State { .IfToken = Token.Id.Colon });
2621 try stack.append(State { .AsmInputItems = &node.inputs });
2622 try stack.append(State { .IfToken = Token.Id.Colon });
2623 try stack.append(State { .AsmOutputItems = &node.outputs });
2624 try stack.append(State { .IfToken = Token.Id.Colon });
2625 try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.template } });
2626 try stack.append(State { .ExpectToken = Token.Id.LParen });
2627 try stack.append(State {
2628 .OptionalTokenSave = OptionalTokenSave {
2629 .id = Token.Id.Keyword_volatile,
2630 .ptr = &node.volatile_token,
2631 }
2632 });
2431 stack.append(State{ .ExpectTokenSave = ExpectTokenSave{
2432 .id = Token.Id.RParen,
2433 .ptr = &node.rparen,
2434 } }) catch unreachable;
2435 try stack.append(State{ .AsmClobberItems = &node.clobbers });
2436 try stack.append(State{ .IfToken = Token.Id.Colon });
2437 try stack.append(State{ .AsmInputItems = &node.inputs });
2438 try stack.append(State{ .IfToken = Token.Id.Colon });
2439 try stack.append(State{ .AsmOutputItems = &node.outputs });
2440 try stack.append(State{ .IfToken = Token.Id.Colon });
2441 try stack.append(State{ .StringLiteral = OptionalCtx{ .Required = &node.template } });
2442 try stack.append(State{ .ExpectToken = Token.Id.LParen });
2443 try stack.append(State{ .OptionalTokenSave = OptionalTokenSave{
2444 .id = Token.Id.Keyword_volatile,
2445 .ptr = &node.volatile_token,
2446 } });
26332447 },
26342448 Token.Id.Keyword_inline => {
2635 stack.append(State {
2636 .Inline = InlineCtx {
2637 .label = null,
2638 .inline_token = token.index,
2639 .opt_ctx = opt_ctx,
2640 }
2641 }) catch unreachable;
2449 stack.append(State{ .Inline = InlineCtx{
2450 .label = null,
2451 .inline_token = token.index,
2452 .opt_ctx = opt_ctx,
2453 } }) catch unreachable;
26422454 continue;
26432455 },
26442456 else => {
26452457 if (!try parseBlockExpr(&stack, arena, opt_ctx, token.ptr, token.index)) {
26462458 putBackToken(&tok_it, &tree);
26472459 if (opt_ctx != OptionalCtx.Optional) {
2648 *(try tree.errors.addOne()) = Error {
2649 .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr { .token = token.index },
2650 };
2460 ((try tree.errors.addOne())).* = Error{ .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr{ .token = token.index } };
26512461 return tree;
26522462 }
26532463 }
26542464 continue;
2655 }
2465 },
26562466 }
26572467 },
26582468
2659
26602469 State.ErrorTypeOrSetDecl => |ctx| {
26612470 if (eatToken(&tok_it, &tree, Token.Id.LBrace) == null) {
26622471 _ = try createToCtxLiteral(arena, ctx.opt_ctx, ast.Node.ErrorType, ctx.error_token);
26632472 continue;
26642473 }
26652474
2666 const node = try arena.construct(ast.Node.ErrorSetDecl {
2667 .base = ast.Node {
2668 .id = ast.Node.Id.ErrorSetDecl,
2669 },
2475 const node = try arena.construct(ast.Node.ErrorSetDecl{
2476 .base = ast.Node{ .id = ast.Node.Id.ErrorSetDecl },
26702477 .error_token = ctx.error_token,
26712478 .decls = ast.Node.ErrorSetDecl.DeclList.init(arena),
26722479 .rbrace_token = undefined,
26732480 });
26742481 ctx.opt_ctx.store(&node.base);
26752482
2676 stack.append(State {
2677 .ErrorTagListItemOrEnd = ListSave(@typeOf(node.decls)) {
2678 .list = &node.decls,
2679 .ptr = &node.rbrace_token,
2680 }
2681 }) catch unreachable;
2483 stack.append(State{ .ErrorTagListItemOrEnd = ListSave(@typeOf(node.decls)){
2484 .list = &node.decls,
2485 .ptr = &node.rbrace_token,
2486 } }) catch unreachable;
26822487 continue;
26832488 },
26842489 State.StringLiteral => |opt_ctx| {
26852490 const token = nextToken(&tok_it, &tree);
26862491 const token_index = token.index;
26872492 const token_ptr = token.ptr;
2688 opt_ctx.store(
2689 (try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) ?? {
2690 putBackToken(&tok_it, &tree);
2691 if (opt_ctx != OptionalCtx.Optional) {
2692 *(try tree.errors.addOne()) = Error {
2693 .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr { .token = token_index },
2694 };
2695 return tree;
2696 }
2697
2698 continue;
2493 opt_ctx.store((try parseStringLiteral(arena, &tok_it, token_ptr, token_index, &tree)) ?? {
2494 putBackToken(&tok_it, &tree);
2495 if (opt_ctx != OptionalCtx.Optional) {
2496 ((try tree.errors.addOne())).* = Error{ .ExpectedPrimaryExpr = Error.ExpectedPrimaryExpr{ .token = token_index } };
2497 return tree;
26992498 }
2700 );
2499
2500 continue;
2501 });
27012502 },
27022503
27032504 State.Identifier => |opt_ctx| {
......@@ -2710,12 +2511,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27102511 const token = nextToken(&tok_it, &tree);
27112512 const token_index = token.index;
27122513 const token_ptr = token.ptr;
2713 *(try tree.errors.addOne()) = Error {
2714 .ExpectedToken = Error.ExpectedToken {
2715 .token = token_index,
2716 .expected_id = Token.Id.Identifier,
2717 },
2718 };
2514 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2515 .token = token_index,
2516 .expected_id = Token.Id.Identifier,
2517 } };
27192518 return tree;
27202519 }
27212520 },
......@@ -2726,23 +2525,19 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27262525 const ident_token_index = ident_token.index;
27272526 const ident_token_ptr = ident_token.ptr;
27282527 if (ident_token_ptr.id != Token.Id.Identifier) {
2729 *(try tree.errors.addOne()) = Error {
2730 .ExpectedToken = Error.ExpectedToken {
2731 .token = ident_token_index,
2732 .expected_id = Token.Id.Identifier,
2733 },
2734 };
2528 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2529 .token = ident_token_index,
2530 .expected_id = Token.Id.Identifier,
2531 } };
27352532 return tree;
27362533 }
27372534
2738 const node = try arena.construct(ast.Node.ErrorTag {
2739 .base = ast.Node {
2740 .id = ast.Node.Id.ErrorTag,
2741 },
2535 const node = try arena.construct(ast.Node.ErrorTag{
2536 .base = ast.Node{ .id = ast.Node.Id.ErrorTag },
27422537 .doc_comments = comments,
27432538 .name_token = ident_token_index,
27442539 });
2745 *node_ptr = &node.base;
2540 node_ptr.* = &node.base;
27462541 continue;
27472542 },
27482543
......@@ -2751,12 +2546,10 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27512546 const token_index = token.index;
27522547 const token_ptr = token.ptr;
27532548 if (token_ptr.id != token_id) {
2754 *(try tree.errors.addOne()) = Error {
2755 .ExpectedToken = Error.ExpectedToken {
2756 .token = token_index,
2757 .expected_id = token_id,
2758 },
2759 };
2549 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2550 .token = token_index,
2551 .expected_id = token_id,
2552 } };
27602553 return tree;
27612554 }
27622555 continue;
......@@ -2766,15 +2559,13 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27662559 const token_index = token.index;
27672560 const token_ptr = token.ptr;
27682561 if (token_ptr.id != expect_token_save.id) {
2769 *(try tree.errors.addOne()) = Error {
2770 .ExpectedToken = Error.ExpectedToken {
2771 .token = token_index,
2772 .expected_id = expect_token_save.id,
2773 },
2774 };
2562 ((try tree.errors.addOne())).* = Error{ .ExpectedToken = Error.ExpectedToken{
2563 .token = token_index,
2564 .expected_id = expect_token_save.id,
2565 } };
27752566 return tree;
27762567 }
2777 *expect_token_save.ptr = token_index;
2568 (expect_token_save.ptr).* = token_index;
27782569 continue;
27792570 },
27802571 State.IfToken => |token_id| {
......@@ -2787,7 +2578,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27872578 },
27882579 State.IfTokenSave => |if_token_save| {
27892580 if (eatToken(&tok_it, &tree, if_token_save.id)) |token_index| {
2790 *if_token_save.ptr = token_index;
2581 (if_token_save.ptr).* = token_index;
27912582 continue;
27922583 }
27932584
......@@ -2796,7 +2587,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
27962587 },
27972588 State.OptionalTokenSave => |optional_token_save| {
27982589 if (eatToken(&tok_it, &tree, optional_token_save.id)) |token_index| {
2799 *optional_token_save.ptr = token_index;
2590 (optional_token_save.ptr).* = token_index;
28002591 continue;
28012592 }
28022593
......@@ -2919,28 +2710,28 @@ const OptionalCtx = union(enum) {
29192710 Required: &&ast.Node,
29202711
29212712 pub fn store(self: &const OptionalCtx, value: &ast.Node) void {
2922 switch (*self) {
2923 OptionalCtx.Optional => |ptr| *ptr = value,
2924 OptionalCtx.RequiredNull => |ptr| *ptr = value,
2925 OptionalCtx.Required => |ptr| *ptr = value,
2713 switch (self.*) {
2714 OptionalCtx.Optional => |ptr| ptr.* = value,
2715 OptionalCtx.RequiredNull => |ptr| ptr.* = value,
2716 OptionalCtx.Required => |ptr| ptr.* = value,
29262717 }
29272718 }
29282719
29292720 pub fn get(self: &const OptionalCtx) ?&ast.Node {
2930 switch (*self) {
2931 OptionalCtx.Optional => |ptr| return *ptr,
2932 OptionalCtx.RequiredNull => |ptr| return ??*ptr,
2933 OptionalCtx.Required => |ptr| return *ptr,
2721 switch (self.*) {
2722 OptionalCtx.Optional => |ptr| return ptr.*,
2723 OptionalCtx.RequiredNull => |ptr| return ??ptr.*,
2724 OptionalCtx.Required => |ptr| return ptr.*,
29342725 }
29352726 }
29362727
29372728 pub fn toRequired(self: &const OptionalCtx) OptionalCtx {
2938 switch (*self) {
2729 switch (self.*) {
29392730 OptionalCtx.Optional => |ptr| {
2940 return OptionalCtx { .RequiredNull = ptr };
2731 return OptionalCtx{ .RequiredNull = ptr };
29412732 },
2942 OptionalCtx.RequiredNull => |ptr| return *self,
2943 OptionalCtx.Required => |ptr| return *self,
2733 OptionalCtx.RequiredNull => |ptr| return self.*,
2734 OptionalCtx.Required => |ptr| return self.*,
29442735 }
29452736 }
29462737};
......@@ -3062,7 +2853,6 @@ const State = union(enum) {
30622853 Identifier: OptionalCtx,
30632854 ErrorTag: &&ast.Node,
30642855
3065
30662856 IfToken: @TagType(Token.Id),
30672857 IfTokenSave: ExpectTokenSave,
30682858 ExpectToken: @TagType(Token.Id),
......@@ -3072,16 +2862,14 @@ const State = union(enum) {
30722862
30732863fn pushDocComment(arena: &mem.Allocator, line_comment: TokenIndex, result: &?&ast.Node.DocComment) !void {
30742864 const node = blk: {
3075 if (*result) |comment_node| {
2865 if (result.*) |comment_node| {
30762866 break :blk comment_node;
30772867 } else {
3078 const comment_node = try arena.construct(ast.Node.DocComment {
3079 .base = ast.Node {
3080 .id = ast.Node.Id.DocComment,
3081 },
2868 const comment_node = try arena.construct(ast.Node.DocComment{
2869 .base = ast.Node{ .id = ast.Node.Id.DocComment },
30822870 .lines = ast.Node.DocComment.LineList.init(arena),
30832871 });
3084 *result = comment_node;
2872 result.* = comment_node;
30852873 break :blk comment_node;
30862874 }
30872875 };
......@@ -3102,24 +2890,20 @@ fn eatDocComments(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, t
31022890
31032891fn eatLineComment(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) !?&ast.Node.LineComment {
31042892 const token = eatToken(tok_it, tree, Token.Id.LineComment) ?? return null;
3105 return try arena.construct(ast.Node.LineComment {
3106 .base = ast.Node {
3107 .id = ast.Node.Id.LineComment,
3108 },
2893 return try arena.construct(ast.Node.LineComment{
2894 .base = ast.Node{ .id = ast.Node.Id.LineComment },
31092895 .token = token,
31102896 });
31112897}
31122898
3113fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator,
3114 token_ptr: &const Token, token_index: TokenIndex, tree: &ast.Tree) !?&ast.Node
3115{
2899fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterator, token_ptr: &const Token, token_index: TokenIndex, tree: &ast.Tree) !?&ast.Node {
31162900 switch (token_ptr.id) {
31172901 Token.Id.StringLiteral => {
31182902 return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base;
31192903 },
31202904 Token.Id.MultilineStringLiteralLine => {
3121 const node = try arena.construct(ast.Node.MultilineStringLiteral {
3122 .base = ast.Node { .id = ast.Node.Id.MultilineStringLiteral },
2905 const node = try arena.construct(ast.Node.MultilineStringLiteral{
2906 .base = ast.Node{ .id = ast.Node.Id.MultilineStringLiteral },
31232907 .lines = ast.Node.MultilineStringLiteral.LineList.init(arena),
31242908 });
31252909 try node.lines.push(token_index);
......@@ -3143,12 +2927,11 @@ fn parseStringLiteral(arena: &mem.Allocator, tok_it: &ast.Tree.TokenList.Iterato
31432927 }
31442928}
31452929
3146fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx,
3147 token_ptr: &const Token, token_index: TokenIndex) !bool {
2930fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx, token_ptr: &const Token, token_index: TokenIndex) !bool {
31482931 switch (token_ptr.id) {
31492932 Token.Id.Keyword_suspend => {
3150 const node = try arena.construct(ast.Node.Suspend {
3151 .base = ast.Node {.id = ast.Node.Id.Suspend },
2933 const node = try arena.construct(ast.Node.Suspend{
2934 .base = ast.Node{ .id = ast.Node.Id.Suspend },
31522935 .label = null,
31532936 .suspend_token = token_index,
31542937 .payload = null,
......@@ -3156,13 +2939,13 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
31562939 });
31572940 ctx.store(&node.base);
31582941
3159 stack.append(State { .SuspendBody = node }) catch unreachable;
3160 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
2942 stack.append(State{ .SuspendBody = node }) catch unreachable;
2943 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
31612944 return true;
31622945 },
31632946 Token.Id.Keyword_if => {
3164 const node = try arena.construct(ast.Node.If {
3165 .base = ast.Node {.id = ast.Node.Id.If },
2947 const node = try arena.construct(ast.Node.If{
2948 .base = ast.Node{ .id = ast.Node.Id.If },
31662949 .if_token = token_index,
31672950 .condition = undefined,
31682951 .payload = null,
......@@ -3171,41 +2954,35 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
31712954 });
31722955 ctx.store(&node.base);
31732956
3174 stack.append(State { .Else = &node.@"else" }) catch unreachable;
3175 try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } });
3176 try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } });
3177 try stack.append(State { .ExpectToken = Token.Id.RParen });
3178 try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } });
3179 try stack.append(State { .ExpectToken = Token.Id.LParen });
2957 stack.append(State{ .Else = &node.@"else" }) catch unreachable;
2958 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } });
2959 try stack.append(State{ .PointerPayload = OptionalCtx{ .Optional = &node.payload } });
2960 try stack.append(State{ .ExpectToken = Token.Id.RParen });
2961 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.condition } });
2962 try stack.append(State{ .ExpectToken = Token.Id.LParen });
31802963 return true;
31812964 },
31822965 Token.Id.Keyword_while => {
3183 stack.append(State {
3184 .While = LoopCtx {
3185 .label = null,
3186 .inline_token = null,
3187 .loop_token = token_index,
3188 .opt_ctx = *ctx,
3189 }
3190 }) catch unreachable;
2966 stack.append(State{ .While = LoopCtx{
2967 .label = null,
2968 .inline_token = null,
2969 .loop_token = token_index,
2970 .opt_ctx = ctx.*,
2971 } }) catch unreachable;
31912972 return true;
31922973 },
31932974 Token.Id.Keyword_for => {
3194 stack.append(State {
3195 .For = LoopCtx {
3196 .label = null,
3197 .inline_token = null,
3198 .loop_token = token_index,
3199 .opt_ctx = *ctx,
3200 }
3201 }) catch unreachable;
2975 stack.append(State{ .For = LoopCtx{
2976 .label = null,
2977 .inline_token = null,
2978 .loop_token = token_index,
2979 .opt_ctx = ctx.*,
2980 } }) catch unreachable;
32022981 return true;
32032982 },
32042983 Token.Id.Keyword_switch => {
3205 const node = try arena.construct(ast.Node.Switch {
3206 .base = ast.Node {
3207 .id = ast.Node.Id.Switch,
3208 },
2984 const node = try arena.construct(ast.Node.Switch{
2985 .base = ast.Node{ .id = ast.Node.Id.Switch },
32092986 .switch_token = token_index,
32102987 .expr = undefined,
32112988 .cases = ast.Node.Switch.CaseList.init(arena),
......@@ -3213,45 +2990,43 @@ fn parseBlockExpr(stack: &std.ArrayList(State), arena: &mem.Allocator, ctx: &con
32132990 });
32142991 ctx.store(&node.base);
32152992
3216 stack.append(State {
3217 .SwitchCaseOrEnd = ListSave(@typeOf(node.cases)) {
3218 .list = &node.cases,
3219 .ptr = &node.rbrace,
3220 },
3221 }) catch unreachable;
3222 try stack.append(State { .ExpectToken = Token.Id.LBrace });
3223 try stack.append(State { .ExpectToken = Token.Id.RParen });
3224 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
3225 try stack.append(State { .ExpectToken = Token.Id.LParen });
2993 stack.append(State{ .SwitchCaseOrEnd = ListSave(@typeOf(node.cases)){
2994 .list = &node.cases,
2995 .ptr = &node.rbrace,
2996 } }) catch unreachable;
2997 try stack.append(State{ .ExpectToken = Token.Id.LBrace });
2998 try stack.append(State{ .ExpectToken = Token.Id.RParen });
2999 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
3000 try stack.append(State{ .ExpectToken = Token.Id.LParen });
32263001 return true;
32273002 },
32283003 Token.Id.Keyword_comptime => {
3229 const node = try arena.construct(ast.Node.Comptime {
3230 .base = ast.Node {.id = ast.Node.Id.Comptime },
3004 const node = try arena.construct(ast.Node.Comptime{
3005 .base = ast.Node{ .id = ast.Node.Id.Comptime },
32313006 .comptime_token = token_index,
32323007 .expr = undefined,
32333008 .doc_comments = null,
32343009 });
32353010 ctx.store(&node.base);
32363011
3237 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
3012 try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } });
32383013 return true;
32393014 },
32403015 Token.Id.LBrace => {
3241 const block = try arena.construct(ast.Node.Block {
3242 .base = ast.Node {.id = ast.Node.Id.Block },
3016 const block = try arena.construct(ast.Node.Block{
3017 .base = ast.Node{ .id = ast.Node.Id.Block },
32433018 .label = null,
32443019 .lbrace = token_index,
32453020 .statements = ast.Node.Block.StatementList.init(arena),
32463021 .rbrace = undefined,
32473022 });
32483023 ctx.store(&block.base);
3249 stack.append(State { .Block = block }) catch unreachable;
3024 stack.append(State{ .Block = block }) catch unreachable;
32503025 return true;
32513026 },
32523027 else => {
32533028 return false;
3254 }
3029 },
32553030 }
32563031}
32573032
......@@ -3265,20 +3040,16 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:
32653040 const token_index = token.index;
32663041 const token_ptr = token.ptr;
32673042 switch (token_ptr.id) {
3268 Token.Id.Comma => return ExpectCommaOrEndResult { .end_token = null},
3043 Token.Id.Comma => return ExpectCommaOrEndResult{ .end_token = null },
32693044 else => {
32703045 if (end == token_ptr.id) {
3271 return ExpectCommaOrEndResult { .end_token = token_index };
3046 return ExpectCommaOrEndResult{ .end_token = token_index };
32723047 }
32733048
3274 return ExpectCommaOrEndResult {
3275 .parse_error = Error {
3276 .ExpectedCommaOrEnd = Error.ExpectedCommaOrEnd {
3277 .token = token_index,
3278 .end_id = end,
3279 },
3280 },
3281 };
3049 return ExpectCommaOrEndResult{ .parse_error = Error{ .ExpectedCommaOrEnd = Error.ExpectedCommaOrEnd{
3050 .token = token_index,
3051 .end_id = end,
3052 } } };
32823053 },
32833054 }
32843055}
......@@ -3286,103 +3057,102 @@ fn expectCommaOrEnd(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, end:
32863057fn tokenIdToAssignment(id: &const Token.Id) ?ast.Node.InfixOp.Op {
32873058 // TODO: We have to cast all cases because of this:
32883059 // error: expected type '?InfixOp', found '?@TagType(InfixOp)'
3289 return switch (*id) {
3290 Token.Id.AmpersandEqual => ast.Node.InfixOp.Op { .AssignBitAnd = {} },
3291 Token.Id.AngleBracketAngleBracketLeftEqual => ast.Node.InfixOp.Op { .AssignBitShiftLeft = {} },
3292 Token.Id.AngleBracketAngleBracketRightEqual => ast.Node.InfixOp.Op { .AssignBitShiftRight = {} },
3293 Token.Id.AsteriskEqual => ast.Node.InfixOp.Op { .AssignTimes = {} },
3294 Token.Id.AsteriskPercentEqual => ast.Node.InfixOp.Op { .AssignTimesWarp = {} },
3295 Token.Id.CaretEqual => ast.Node.InfixOp.Op { .AssignBitXor = {} },
3296 Token.Id.Equal => ast.Node.InfixOp.Op { .Assign = {} },
3297 Token.Id.MinusEqual => ast.Node.InfixOp.Op { .AssignMinus = {} },
3298 Token.Id.MinusPercentEqual => ast.Node.InfixOp.Op { .AssignMinusWrap = {} },
3299 Token.Id.PercentEqual => ast.Node.InfixOp.Op { .AssignMod = {} },
3300 Token.Id.PipeEqual => ast.Node.InfixOp.Op { .AssignBitOr = {} },
3301 Token.Id.PlusEqual => ast.Node.InfixOp.Op { .AssignPlus = {} },
3302 Token.Id.PlusPercentEqual => ast.Node.InfixOp.Op { .AssignPlusWrap = {} },
3303 Token.Id.SlashEqual => ast.Node.InfixOp.Op { .AssignDiv = {} },
3060 return switch (id.*) {
3061 Token.Id.AmpersandEqual => ast.Node.InfixOp.Op{ .AssignBitAnd = {} },
3062 Token.Id.AngleBracketAngleBracketLeftEqual => ast.Node.InfixOp.Op{ .AssignBitShiftLeft = {} },
3063 Token.Id.AngleBracketAngleBracketRightEqual => ast.Node.InfixOp.Op{ .AssignBitShiftRight = {} },
3064 Token.Id.AsteriskEqual => ast.Node.InfixOp.Op{ .AssignTimes = {} },
3065 Token.Id.AsteriskPercentEqual => ast.Node.InfixOp.Op{ .AssignTimesWarp = {} },
3066 Token.Id.CaretEqual => ast.Node.InfixOp.Op{ .AssignBitXor = {} },
3067 Token.Id.Equal => ast.Node.InfixOp.Op{ .Assign = {} },
3068 Token.Id.MinusEqual => ast.Node.InfixOp.Op{ .AssignMinus = {} },
3069 Token.Id.MinusPercentEqual => ast.Node.InfixOp.Op{ .AssignMinusWrap = {} },
3070 Token.Id.PercentEqual => ast.Node.InfixOp.Op{ .AssignMod = {} },
3071 Token.Id.PipeEqual => ast.Node.InfixOp.Op{ .AssignBitOr = {} },
3072 Token.Id.PlusEqual => ast.Node.InfixOp.Op{ .AssignPlus = {} },
3073 Token.Id.PlusPercentEqual => ast.Node.InfixOp.Op{ .AssignPlusWrap = {} },
3074 Token.Id.SlashEqual => ast.Node.InfixOp.Op{ .AssignDiv = {} },
33043075 else => null,
33053076 };
33063077}
33073078
33083079fn tokenIdToUnwrapExpr(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
33093080 return switch (id) {
3310 Token.Id.Keyword_catch => ast.Node.InfixOp.Op { .Catch = null },
3311 Token.Id.QuestionMarkQuestionMark => ast.Node.InfixOp.Op { .UnwrapMaybe = void{} },
3081 Token.Id.Keyword_catch => ast.Node.InfixOp.Op{ .Catch = null },
3082 Token.Id.QuestionMarkQuestionMark => ast.Node.InfixOp.Op{ .UnwrapMaybe = void{} },
33123083 else => null,
33133084 };
33143085}
33153086
33163087fn tokenIdToComparison(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
33173088 return switch (id) {
3318 Token.Id.BangEqual => ast.Node.InfixOp.Op { .BangEqual = void{} },
3319 Token.Id.EqualEqual => ast.Node.InfixOp.Op { .EqualEqual = void{} },
3320 Token.Id.AngleBracketLeft => ast.Node.InfixOp.Op { .LessThan = void{} },
3321 Token.Id.AngleBracketLeftEqual => ast.Node.InfixOp.Op { .LessOrEqual = void{} },
3322 Token.Id.AngleBracketRight => ast.Node.InfixOp.Op { .GreaterThan = void{} },
3323 Token.Id.AngleBracketRightEqual => ast.Node.InfixOp.Op { .GreaterOrEqual = void{} },
3089 Token.Id.BangEqual => ast.Node.InfixOp.Op{ .BangEqual = void{} },
3090 Token.Id.EqualEqual => ast.Node.InfixOp.Op{ .EqualEqual = void{} },
3091 Token.Id.AngleBracketLeft => ast.Node.InfixOp.Op{ .LessThan = void{} },
3092 Token.Id.AngleBracketLeftEqual => ast.Node.InfixOp.Op{ .LessOrEqual = void{} },
3093 Token.Id.AngleBracketRight => ast.Node.InfixOp.Op{ .GreaterThan = void{} },
3094 Token.Id.AngleBracketRightEqual => ast.Node.InfixOp.Op{ .GreaterOrEqual = void{} },
33243095 else => null,
33253096 };
33263097}
33273098
33283099fn tokenIdToBitShift(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
33293100 return switch (id) {
3330 Token.Id.AngleBracketAngleBracketLeft => ast.Node.InfixOp.Op { .BitShiftLeft = void{} },
3331 Token.Id.AngleBracketAngleBracketRight => ast.Node.InfixOp.Op { .BitShiftRight = void{} },
3101 Token.Id.AngleBracketAngleBracketLeft => ast.Node.InfixOp.Op{ .BitShiftLeft = void{} },
3102 Token.Id.AngleBracketAngleBracketRight => ast.Node.InfixOp.Op{ .BitShiftRight = void{} },
33323103 else => null,
33333104 };
33343105}
33353106
33363107fn tokenIdToAddition(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
33373108 return switch (id) {
3338 Token.Id.Minus => ast.Node.InfixOp.Op { .Sub = void{} },
3339 Token.Id.MinusPercent => ast.Node.InfixOp.Op { .SubWrap = void{} },
3340 Token.Id.Plus => ast.Node.InfixOp.Op { .Add = void{} },
3341 Token.Id.PlusPercent => ast.Node.InfixOp.Op { .AddWrap = void{} },
3342 Token.Id.PlusPlus => ast.Node.InfixOp.Op { .ArrayCat = void{} },
3109 Token.Id.Minus => ast.Node.InfixOp.Op{ .Sub = void{} },
3110 Token.Id.MinusPercent => ast.Node.InfixOp.Op{ .SubWrap = void{} },
3111 Token.Id.Plus => ast.Node.InfixOp.Op{ .Add = void{} },
3112 Token.Id.PlusPercent => ast.Node.InfixOp.Op{ .AddWrap = void{} },
3113 Token.Id.PlusPlus => ast.Node.InfixOp.Op{ .ArrayCat = void{} },
33433114 else => null,
33443115 };
33453116}
33463117
33473118fn tokenIdToMultiply(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op {
33483119 return switch (id) {
3349 Token.Id.Slash => ast.Node.InfixOp.Op { .Div = void{} },
3350 Token.Id.Asterisk => ast.Node.InfixOp.Op { .Mult = void{} },
3351 Token.Id.AsteriskAsterisk => ast.Node.InfixOp.Op { .ArrayMult = void{} },
3352 Token.Id.AsteriskPercent => ast.Node.InfixOp.Op { .MultWrap = void{} },
3353 Token.Id.Percent => ast.Node.InfixOp.Op { .Mod = void{} },
3354 Token.Id.PipePipe => ast.Node.InfixOp.Op { .MergeErrorSets = void{} },
3120 Token.Id.Slash => ast.Node.InfixOp.Op{ .Div = void{} },
3121 Token.Id.Asterisk => ast.Node.InfixOp.Op{ .Mult = void{} },
3122 Token.Id.AsteriskAsterisk => ast.Node.InfixOp.Op{ .ArrayMult = void{} },
3123 Token.Id.AsteriskPercent => ast.Node.InfixOp.Op{ .MultWrap = void{} },
3124 Token.Id.Percent => ast.Node.InfixOp.Op{ .Mod = void{} },
3125 Token.Id.PipePipe => ast.Node.InfixOp.Op{ .MergeErrorSets = void{} },
33553126 else => null,
33563127 };
33573128}
33583129
33593130fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
33603131 return switch (id) {
3361 Token.Id.Bang => ast.Node.PrefixOp.Op { .BoolNot = void{} },
3362 Token.Id.Tilde => ast.Node.PrefixOp.Op { .BitNot = void{} },
3363 Token.Id.Minus => ast.Node.PrefixOp.Op { .Negation = void{} },
3364 Token.Id.MinusPercent => ast.Node.PrefixOp.Op { .NegationWrap = void{} },
3365 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op { .Deref = void{} },
3366 Token.Id.Ampersand => ast.Node.PrefixOp.Op {
3367 .AddrOf = ast.Node.PrefixOp.AddrOfInfo {
3368 .align_expr = null,
3369 .bit_offset_start_token = null,
3370 .bit_offset_end_token = null,
3371 .const_token = null,
3372 .volatile_token = null,
3373 },
3374 },
3375 Token.Id.QuestionMark => ast.Node.PrefixOp.Op { .MaybeType = void{} },
3376 Token.Id.QuestionMarkQuestionMark => ast.Node.PrefixOp.Op { .UnwrapMaybe = void{} },
3377 Token.Id.Keyword_await => ast.Node.PrefixOp.Op { .Await = void{} },
3378 Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{ } },
3132 Token.Id.Bang => ast.Node.PrefixOp.Op{ .BoolNot = void{} },
3133 Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },
3134 Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },
3135 Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },
3136 Token.Id.Asterisk,
3137 Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{ .PointerType = void{} },
3138 Token.Id.Ampersand => ast.Node.PrefixOp.Op{ .AddrOf = ast.Node.PrefixOp.AddrOfInfo{
3139 .align_expr = null,
3140 .bit_offset_start_token = null,
3141 .bit_offset_end_token = null,
3142 .const_token = null,
3143 .volatile_token = null,
3144 } },
3145 Token.Id.QuestionMark => ast.Node.PrefixOp.Op{ .MaybeType = void{} },
3146 Token.Id.QuestionMarkQuestionMark => ast.Node.PrefixOp.Op{ .UnwrapMaybe = void{} },
3147 Token.Id.Keyword_await => ast.Node.PrefixOp.Op{ .Await = void{} },
3148 Token.Id.Keyword_try => ast.Node.PrefixOp.Op{ .Try = void{} },
33793149 else => null,
33803150 };
33813151}
33823152
33833153fn createLiteral(arena: &mem.Allocator, comptime T: type, token_index: TokenIndex) !&T {
3384 return arena.construct(T {
3385 .base = ast.Node {.id = ast.Node.typeToId(T)},
3154 return arena.construct(T{
3155 .base = ast.Node{ .id = ast.Node.typeToId(T) },
33863156 .token = token_index,
33873157 });
33883158}
......@@ -3397,15 +3167,14 @@ fn createToCtxLiteral(arena: &mem.Allocator, opt_ctx: &const OptionalCtx, compti
33973167fn eatToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree, id: @TagType(Token.Id)) ?TokenIndex {
33983168 const token = nextToken(tok_it, tree);
33993169
3400 if (token.ptr.id == id)
3401 return token.index;
3170 if (token.ptr.id == id) return token.index;
34023171
34033172 putBackToken(tok_it, tree);
34043173 return null;
34053174}
34063175
34073176fn nextToken(tok_it: &ast.Tree.TokenList.Iterator, tree: &ast.Tree) AnnotatedToken {
3408 const result = AnnotatedToken {
3177 const result = AnnotatedToken{
34093178 .index = tok_it.index,
34103179 .ptr = ??tok_it.next(),
34113180 };
std/zig/render.zig+49-44
......@@ -7,7 +7,7 @@ const Token = std.zig.Token;
77
88const indent_delta = 4;
99
10pub const Error = error {
10pub const Error = error{
1111 /// Ran out of memory allocating call stack frames to complete rendering.
1212 OutOfMemory,
1313};
......@@ -17,9 +17,9 @@ pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(
1717
1818 var it = tree.root_node.decls.iterator(0);
1919 while (it.next()) |decl| {
20 try renderTopLevelDecl(allocator, stream, tree, 0, *decl);
20 try renderTopLevelDecl(allocator, stream, tree, 0, decl.*);
2121 if (it.peek()) |next_decl| {
22 const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);
22 const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
2323 try stream.writeByteNTimes('\n', n);
2424 }
2525 }
......@@ -154,10 +154,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
154154 var it = block.statements.iterator(0);
155155 while (it.next()) |statement| {
156156 try stream.writeByteNTimes(' ', block_indent);
157 try renderStatement(allocator, stream, tree, block_indent, *statement);
157 try renderStatement(allocator, stream, tree, block_indent, statement.*);
158158
159159 if (it.peek()) |next_statement| {
160 const n = if (nodeLineOffset(tree, *statement, *next_statement) >= 2) u8(2) else u8(1);
160 const n = if (nodeLineOffset(tree, statement.*, next_statement.*) >= 2) u8(2) else u8(1);
161161 try stream.writeByteNTimes('\n', n);
162162 }
163163 }
......@@ -203,7 +203,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
203203 try stream.write(" ");
204204 try renderExpression(allocator, stream, tree, indent, body);
205205 }
206
207206 },
208207
209208 ast.Node.Id.InfixOp => {
......@@ -307,12 +306,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
307306 },
308307 ast.Node.PrefixOp.Op.BitNot => try stream.write("~"),
309308 ast.Node.PrefixOp.Op.BoolNot => try stream.write("!"),
310 ast.Node.PrefixOp.Op.Deref => try stream.write("*"),
311309 ast.Node.PrefixOp.Op.Negation => try stream.write("-"),
312310 ast.Node.PrefixOp.Op.NegationWrap => try stream.write("-%"),
313311 ast.Node.PrefixOp.Op.Try => try stream.write("try "),
314312 ast.Node.PrefixOp.Op.UnwrapMaybe => try stream.write("??"),
315313 ast.Node.PrefixOp.Op.MaybeType => try stream.write("?"),
314 ast.Node.PrefixOp.Op.PointerType => try stream.write("*"),
316315 ast.Node.PrefixOp.Op.Await => try stream.write("await "),
317316 ast.Node.PrefixOp.Op.Cancel => try stream.write("cancel "),
318317 ast.Node.PrefixOp.Op.Resume => try stream.write("resume "),
......@@ -336,7 +335,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
336335
337336 var it = call_info.params.iterator(0);
338337 while (it.next()) |param_node| {
339 try renderExpression(allocator, stream, tree, indent, *param_node);
338 try renderExpression(allocator, stream, tree, indent, param_node.*);
340339 if (it.peek() != null) {
341340 try stream.write(", ");
342341 }
......@@ -352,6 +351,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
352351 try stream.write("]");
353352 },
354353
354 ast.Node.SuffixOp.Op.Deref => {
355 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
356 try stream.write(".*");
357 },
358
355359 @TagType(ast.Node.SuffixOp.Op).Slice => |range| {
356360 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
357361 try stream.write("[");
......@@ -371,7 +375,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
371375 }
372376
373377 if (field_inits.len == 1) {
374 const field_init = *field_inits.at(0);
378 const field_init = field_inits.at(0).*;
375379
376380 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
377381 try stream.write("{ ");
......@@ -388,12 +392,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
388392 var it = field_inits.iterator(0);
389393 while (it.next()) |field_init| {
390394 try stream.writeByteNTimes(' ', new_indent);
391 try renderExpression(allocator, stream, tree, new_indent, *field_init);
392 if ((*field_init).id != ast.Node.Id.LineComment) {
395 try renderExpression(allocator, stream, tree, new_indent, field_init.*);
396 if ((field_init.*).id != ast.Node.Id.LineComment) {
393397 try stream.write(",");
394398 }
395399 if (it.peek()) |next_field_init| {
396 const n = if (nodeLineOffset(tree, *field_init, *next_field_init) >= 2) u8(2) else u8(1);
400 const n = if (nodeLineOffset(tree, field_init.*, next_field_init.*) >= 2) u8(2) else u8(1);
397401 try stream.writeByteNTimes('\n', n);
398402 }
399403 }
......@@ -404,14 +408,13 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
404408 },
405409
406410 ast.Node.SuffixOp.Op.ArrayInitializer => |*exprs| {
407
408411 if (exprs.len == 0) {
409412 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
410413 try stream.write("{}");
411414 return;
412415 }
413416 if (exprs.len == 1) {
414 const expr = *exprs.at(0);
417 const expr = exprs.at(0).*;
415418
416419 try renderExpression(allocator, stream, tree, indent, suffix_op.lhs);
417420 try stream.write("{");
......@@ -428,11 +431,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
428431 var it = exprs.iterator(0);
429432 while (it.next()) |expr| {
430433 try stream.writeByteNTimes(' ', new_indent);
431 try renderExpression(allocator, stream, tree, new_indent, *expr);
434 try renderExpression(allocator, stream, tree, new_indent, expr.*);
432435 try stream.write(",");
433436
434437 if (it.peek()) |next_expr| {
435 const n = if (nodeLineOffset(tree, *expr, *next_expr) >= 2) u8(2) else u8(1);
438 const n = if (nodeLineOffset(tree, expr.*, next_expr.*) >= 2) u8(2) else u8(1);
436439 try stream.writeByteNTimes('\n', n);
437440 }
438441 }
......@@ -465,7 +468,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
465468 ast.Node.ControlFlowExpression.Kind.Return => {
466469 try stream.print("return");
467470 },
468
469471 }
470472
471473 if (flow_expr.rhs) |rhs| {
......@@ -571,7 +573,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
571573 switch (container_decl.layout) {
572574 ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "),
573575 ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "),
574 ast.Node.ContainerDecl.Layout.Auto => { },
576 ast.Node.ContainerDecl.Layout.Auto => {},
575577 }
576578
577579 switch (container_decl.kind) {
......@@ -607,10 +609,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
607609 var it = container_decl.fields_and_decls.iterator(0);
608610 while (it.next()) |decl| {
609611 try stream.writeByteNTimes(' ', new_indent);
610 try renderTopLevelDecl(allocator, stream, tree, new_indent, *decl);
612 try renderTopLevelDecl(allocator, stream, tree, new_indent, decl.*);
611613
612614 if (it.peek()) |next_decl| {
613 const n = if (nodeLineOffset(tree, *decl, *next_decl) >= 2) u8(2) else u8(1);
615 const n = if (nodeLineOffset(tree, decl.*, next_decl.*) >= 2) u8(2) else u8(1);
614616 try stream.writeByteNTimes('\n', n);
615617 }
616618 }
......@@ -630,7 +632,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
630632 }
631633
632634 if (err_set_decl.decls.len == 1) blk: {
633 const node = *err_set_decl.decls.at(0);
635 const node = err_set_decl.decls.at(0).*;
634636
635637 // if there are any doc comments or same line comments
636638 // don't try to put it all on one line
......@@ -640,7 +642,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
640642 break :blk;
641643 }
642644
643
644645 try stream.write("error{");
645646 try renderTopLevelDecl(allocator, stream, tree, indent, node);
646647 try stream.write("}");
......@@ -653,12 +654,12 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
653654 var it = err_set_decl.decls.iterator(0);
654655 while (it.next()) |node| {
655656 try stream.writeByteNTimes(' ', new_indent);
656 try renderTopLevelDecl(allocator, stream, tree, new_indent, *node);
657 if ((*node).id != ast.Node.Id.LineComment) {
657 try renderTopLevelDecl(allocator, stream, tree, new_indent, node.*);
658 if ((node.*).id != ast.Node.Id.LineComment) {
658659 try stream.write(",");
659660 }
660661 if (it.peek()) |next_node| {
661 const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);
662 const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
662663 try stream.writeByteNTimes('\n', n);
663664 }
664665 }
......@@ -672,9 +673,9 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
672673 const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
673674 try stream.print("\n");
674675
675 var i : usize = 0;
676 var i: usize = 0;
676677 while (i < multiline_str_literal.lines.len) : (i += 1) {
677 const t = *multiline_str_literal.lines.at(i);
678 const t = multiline_str_literal.lines.at(i).*;
678679 try stream.writeByteNTimes(' ', indent + indent_delta);
679680 try stream.print("{}", tree.tokenSlice(t));
680681 }
......@@ -691,7 +692,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
691692
692693 var it = builtin_call.params.iterator(0);
693694 while (it.next()) |param_node| {
694 try renderExpression(allocator, stream, tree, indent, *param_node);
695 try renderExpression(allocator, stream, tree, indent, param_node.*);
695696 if (it.peek() != null) {
696697 try stream.write(", ");
697698 }
......@@ -736,7 +737,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
736737
737738 var it = fn_proto.params.iterator(0);
738739 while (it.next()) |param_decl_node| {
739 try renderParamDecl(allocator, stream, tree, indent, *param_decl_node);
740 try renderParamDecl(allocator, stream, tree, indent, param_decl_node.*);
740741
741742 if (it.peek() != null) {
742743 try stream.write(", ");
......@@ -760,7 +761,6 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
760761 try renderExpression(allocator, stream, tree, indent, node);
761762 },
762763 }
763
764764 },
765765
766766 ast.Node.Id.PromiseType => {
......@@ -797,10 +797,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
797797 var it = switch_node.cases.iterator(0);
798798 while (it.next()) |node| {
799799 try stream.writeByteNTimes(' ', new_indent);
800 try renderExpression(allocator, stream, tree, new_indent, *node);
800 try renderExpression(allocator, stream, tree, new_indent, node.*);
801801
802802 if (it.peek()) |next_node| {
803 const n = if (nodeLineOffset(tree, *node, *next_node) >= 2) u8(2) else u8(1);
803 const n = if (nodeLineOffset(tree, node.*, next_node.*) >= 2) u8(2) else u8(1);
804804 try stream.writeByteNTimes('\n', n);
805805 }
806806 }
......@@ -815,7 +815,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
815815
816816 var it = switch_case.items.iterator(0);
817817 while (it.next()) |node| {
818 try renderExpression(allocator, stream, tree, indent, *node);
818 try renderExpression(allocator, stream, tree, indent, node.*);
819819
820820 if (it.peek() != null) {
821821 try stream.write(",\n");
......@@ -864,8 +864,10 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
864864 try stream.print("{}", tree.tokenSlice(else_node.else_token));
865865
866866 const block_body = switch (else_node.body.id) {
867 ast.Node.Id.Block, ast.Node.Id.If,
868 ast.Node.Id.For, ast.Node.Id.While,
867 ast.Node.Id.Block,
868 ast.Node.Id.If,
869 ast.Node.Id.For,
870 ast.Node.Id.While,
869871 ast.Node.Id.Switch => true,
870872 else => false,
871873 };
......@@ -990,7 +992,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
990992 try renderExpression(allocator, stream, tree, indent, if_node.body);
991993
992994 switch (if_node.body.id) {
993 ast.Node.Id.Block, ast.Node.Id.If, ast.Node.Id.For, ast.Node.Id.While, ast.Node.Id.Switch => {
995 ast.Node.Id.Block,
996 ast.Node.Id.If,
997 ast.Node.Id.For,
998 ast.Node.Id.While,
999 ast.Node.Id.Switch => {
9941000 if (if_node.@"else") |@"else"| {
9951001 if (if_node.body.id == ast.Node.Id.Block) {
9961002 try stream.write(" ");
......@@ -1013,7 +1019,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
10131019
10141020 try renderExpression(allocator, stream, tree, indent, @"else".body);
10151021 }
1016 }
1022 },
10171023 }
10181024 },
10191025
......@@ -1036,11 +1042,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
10361042 {
10371043 var it = asm_node.outputs.iterator(0);
10381044 while (it.next()) |asm_output| {
1039 const node = &(*asm_output).base;
1045 const node = &(asm_output.*).base;
10401046 try renderExpression(allocator, stream, tree, indent_extra, node);
10411047
10421048 if (it.peek()) |next_asm_output| {
1043 const next_node = &(*next_asm_output).base;
1049 const next_node = &(next_asm_output.*).base;
10441050 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
10451051 try stream.writeByte(',');
10461052 try stream.writeByteNTimes('\n', n);
......@@ -1056,11 +1062,11 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
10561062 {
10571063 var it = asm_node.inputs.iterator(0);
10581064 while (it.next()) |asm_input| {
1059 const node = &(*asm_input).base;
1065 const node = &(asm_input.*).base;
10601066 try renderExpression(allocator, stream, tree, indent_extra, node);
10611067
10621068 if (it.peek()) |next_asm_input| {
1063 const next_node = &(*next_asm_input).base;
1069 const next_node = &(next_asm_input.*).base;
10641070 const n = if (nodeLineOffset(tree, node, next_node) >= 2) u8(2) else u8(1);
10651071 try stream.writeByte(',');
10661072 try stream.writeByteNTimes('\n', n);
......@@ -1076,7 +1082,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
10761082 {
10771083 var it = asm_node.clobbers.iterator(0);
10781084 while (it.next()) |node| {
1079 try renderExpression(allocator, stream, tree, indent_once, *node);
1085 try renderExpression(allocator, stream, tree, indent_once, node.*);
10801086
10811087 if (it.peek() != null) {
10821088 try stream.write(", ");
......@@ -1245,8 +1251,7 @@ fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) (@type
12451251 const comment = node.doc_comments ?? return;
12461252 var it = comment.lines.iterator(0);
12471253 while (it.next()) |line_token_index| {
1248 try stream.print("{}\n", tree.tokenSlice(*line_token_index));
1254 try stream.print("{}\n", tree.tokenSlice(line_token_index.*));
12491255 try stream.writeByteNTimes(' ', indent);
12501256 }
12511257}
1252
test/behavior.zig+1
......@@ -35,6 +35,7 @@ comptime {
3535 _ = @import("cases/namespace_depends_on_compile_var/index.zig");
3636 _ = @import("cases/new_stack_call.zig");
3737 _ = @import("cases/null.zig");
38 _ = @import("cases/pointers.zig");
3839 _ = @import("cases/pub_enum/index.zig");
3940 _ = @import("cases/ref_var_in_if_after_if_2nd_switch_prong.zig");
4041 _ = @import("cases/reflection.zig");
test/cases/align.zig+60-26
......@@ -10,7 +10,9 @@ test "global variable alignment" {
1010 assert(@typeOf(slice) == []align(4) u8);
1111}
1212
13fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
13fn derp() align(@sizeOf(usize) * 2) i32 {
14 return 1234;
15}
1416fn noop1() align(1) void {}
1517fn noop4() align(4) void {}
1618
......@@ -22,7 +24,6 @@ test "function alignment" {
2224 noop4();
2325}
2426
25
2627var baz: packed struct {
2728 a: u32,
2829 b: u32,
......@@ -32,7 +33,6 @@ test "packed struct alignment" {
3233 assert(@typeOf(&baz.b) == &align(1) u32);
3334}
3435
35
3636const blah: packed struct {
3737 a: u3,
3838 b: u3,
......@@ -53,29 +53,43 @@ test "implicitly decreasing pointer alignment" {
5353 assert(addUnaligned(&a, &b) == 7);
5454}
5555
56fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) u32 { return *a + *b; }
56fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) u32 {
57 return a.* + b.*;
58}
5759
5860test "implicitly decreasing slice alignment" {
5961 const a: u32 align(4) = 3;
6062 const b: u32 align(8) = 4;
6163 assert(addUnalignedSlice((&a)[0..1], (&b)[0..1]) == 7);
6264}
63fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 { return a[0] + b[0]; }
65fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
66 return a[0] + b[0];
67}
6468
6569test "specifying alignment allows pointer cast" {
6670 testBytesAlign(0x33);
6771}
6872fn testBytesAlign(b: u8) void {
69 var bytes align(4) = []u8{b, b, b, b};
73 var bytes align(4) = []u8 {
74 b,
75 b,
76 b,
77 b,
78 };
7079 const ptr = @ptrCast(&u32, &bytes[0]);
71 assert(*ptr == 0x33333333);
80 assert(ptr.* == 0x33333333);
7281}
7382
7483test "specifying alignment allows slice cast" {
7584 testBytesAlignSlice(0x33);
7685}
7786fn testBytesAlignSlice(b: u8) void {
78 var bytes align(4) = []u8{b, b, b, b};
87 var bytes align(4) = []u8 {
88 b,
89 b,
90 b,
91 b,
92 };
7993 const slice = ([]u32)(bytes[0..]);
8094 assert(slice[0] == 0x33333333);
8195}
......@@ -89,11 +103,14 @@ fn expectsOnly1(x: &align(1) u32) void {
89103 expects4(@alignCast(4, x));
90104}
91105fn expects4(x: &align(4) u32) void {
92 *x += 1;
106 x.* += 1;
93107}
94108
95109test "@alignCast slices" {
96 var array align(4) = []u32{1, 1};
110 var array align(4) = []u32 {
111 1,
112 1,
113 };
97114 const slice = array[0..];
98115 sliceExpectsOnly1(slice);
99116 assert(slice[0] == 2);
......@@ -105,31 +122,34 @@ fn sliceExpects4(slice: []align(4) u32) void {
105122 slice[0] += 1;
106123}
107124
108
109125test "implicitly decreasing fn alignment" {
110126 testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
111127 testImplicitlyDecreaseFnAlign(alignedBig, 5678);
112128}
113129
114fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void {
130fn testImplicitlyDecreaseFnAlign(ptr: fn() align(1) i32, answer: i32) void {
115131 assert(ptr() == answer);
116132}
117133
118fn alignedSmall() align(8) i32 { return 1234; }
119fn alignedBig() align(16) i32 { return 5678; }
120
134fn alignedSmall() align(8) i32 {
135 return 1234;
136}
137fn alignedBig() align(16) i32 {
138 return 5678;
139}
121140
122141test "@alignCast functions" {
123142 assert(fnExpectsOnly1(simple4) == 0x19);
124143}
125fn fnExpectsOnly1(ptr: fn()align(1) i32) i32 {
144fn fnExpectsOnly1(ptr: fn() align(1) i32) i32 {
126145 return fnExpects4(@alignCast(4, ptr));
127146}
128fn fnExpects4(ptr: fn()align(4) i32) i32 {
147fn fnExpects4(ptr: fn() align(4) i32) i32 {
129148 return ptr();
130149}
131fn simple4() align(4) i32 { return 0x19; }
132
150fn simple4() align(4) i32 {
151 return 0x19;
152}
133153
134154test "generic function with align param" {
135155 assert(whyWouldYouEverDoThis(1) == 0x1);
......@@ -137,8 +157,9 @@ test "generic function with align param" {
137157 assert(whyWouldYouEverDoThis(8) == 0x1);
138158}
139159
140fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 { return 0x1; }
141
160fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
161 return 0x1;
162}
142163
143164test "@ptrCast preserves alignment of bigger source" {
144165 var x: u32 align(16) = 1234;
......@@ -146,24 +167,38 @@ test "@ptrCast preserves alignment of bigger source" {
146167 assert(@typeOf(ptr) == &align(16) u8);
147168}
148169
149
150170test "compile-time known array index has best alignment possible" {
151171 // take full advantage of over-alignment
152 var array align(4) = []u8 {1, 2, 3, 4};
172 var array align(4) = []u8 {
173 1,
174 2,
175 3,
176 4,
177 };
153178 assert(@typeOf(&array[0]) == &align(4) u8);
154179 assert(@typeOf(&array[1]) == &u8);
155180 assert(@typeOf(&array[2]) == &align(2) u8);
156181 assert(@typeOf(&array[3]) == &u8);
157182
158183 // because align is too small but we still figure out to use 2
159 var bigger align(2) = []u64{1, 2, 3, 4};
184 var bigger align(2) = []u64 {
185 1,
186 2,
187 3,
188 4,
189 };
160190 assert(@typeOf(&bigger[0]) == &align(2) u64);
161191 assert(@typeOf(&bigger[1]) == &align(2) u64);
162192 assert(@typeOf(&bigger[2]) == &align(2) u64);
163193 assert(@typeOf(&bigger[3]) == &align(2) u64);
164194
165195 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
166 var smaller align(2) = []u32{1, 2, 3, 4};
196 var smaller align(2) = []u32 {
197 1,
198 2,
199 3,
200 4,
201 };
167202 testIndex(&smaller[0], 0, &align(2) u32);
168203 testIndex(&smaller[0], 1, &align(2) u32);
169204 testIndex(&smaller[0], 2, &align(2) u32);
......@@ -182,7 +217,6 @@ fn testIndex2(ptr: &align(4) u8, index: usize, comptime T: type) void {
182217 assert(@typeOf(&ptr[index]) == T);
183218}
184219
185
186220test "alignstack" {
187221 assert(fnWithAlignedStack() == 1234);
188222}
test/cases/alignof.zig+5-1
......@@ -1,7 +1,11 @@
11const assert = @import("std").debug.assert;
22const builtin = @import("builtin");
33
4const Foo = struct { x: u32, y: u32, z: u32, };
4const Foo = struct {
5 x: u32,
6 y: u32,
7 z: u32,
8};
59
610test "@alignOf(T) before referencing T" {
711 comptime assert(@alignOf(Foo) != @maxValue(usize));
test/cases/array.zig+31-10
......@@ -2,9 +2,9 @@ const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
44test "arrays" {
5 var array : [5]u32 = undefined;
5 var array: [5]u32 = undefined;
66
7 var i : u32 = 0;
7 var i: u32 = 0;
88 while (i < 5) {
99 array[i] = i + 1;
1010 i = array[i];
......@@ -34,24 +34,41 @@ test "void arrays" {
3434}
3535
3636test "array literal" {
37 const hex_mult = []u16{4096, 256, 16, 1};
37 const hex_mult = []u16 {
38 4096,
39 256,
40 16,
41 1,
42 };
3843
3944 assert(hex_mult.len == 4);
4045 assert(hex_mult[1] == 256);
4146}
4247
4348test "array dot len const expr" {
44 assert(comptime x: {break :x some_array.len == 4;});
49 assert(comptime x: {
50 break :x some_array.len == 4;
51 });
4552}
4653
4754const ArrayDotLenConstExpr = struct {
4855 y: [some_array.len]u8,
4956};
50const some_array = []u8 {0, 1, 2, 3};
51
57const some_array = []u8 {
58 0,
59 1,
60 2,
61 3,
62};
5263
5364test "nested arrays" {
54 const array_of_strings = [][]const u8 {"hello", "this", "is", "my", "thing"};
65 const array_of_strings = [][]const u8 {
66 "hello",
67 "this",
68 "is",
69 "my",
70 "thing",
71 };
5572 for (array_of_strings) |s, i| {
5673 if (i == 0) assert(mem.eql(u8, s, "hello"));
5774 if (i == 1) assert(mem.eql(u8, s, "this"));
......@@ -61,7 +78,6 @@ test "nested arrays" {
6178 }
6279}
6380
64
6581var s_array: [8]Sub = undefined;
6682const Sub = struct {
6783 b: u8,
......@@ -70,7 +86,9 @@ const Str = struct {
7086 a: []Sub,
7187};
7288test "set global var array via slice embedded in struct" {
73 var s = Str { .a = s_array[0..]};
89 var s = Str {
90 .a = s_array[0..],
91 };
7492
7593 s.a[0].b = 1;
7694 s.a[1].b = 2;
......@@ -82,7 +100,10 @@ test "set global var array via slice embedded in struct" {
82100}
83101
84102test "array literal with specified size" {
85 var array = [2]u8{1, 2};
103 var array = [2]u8 {
104 1,
105 2,
106 };
86107 assert(array[0] == 1);
87108 assert(array[1] == 2);
88109}
test/cases/bitcast.zig+6-2
......@@ -10,5 +10,9 @@ fn testBitCast_i32_u32() void {
1010 assert(conv2(@maxValue(u32)) == -1);
1111}
1212
13fn conv(x: i32) u32 { return @bitCast(u32, x); }
14fn conv2(x: u32) i32 { return @bitCast(i32, x); }
13fn conv(x: i32) u32 {
14 return @bitCast(u32, x);
15}
16fn conv2(x: u32) i32 {
17 return @bitCast(i32, x);
18}
test/cases/bugs/394.zig+14-3
......@@ -1,9 +1,20 @@
1const E = union(enum) { A: [9]u8, B: u64, };
2const S = struct { x: u8, y: E, };
1const E = union(enum) {
2 A: [9]u8,
3 B: u64,
4};
5const S = struct {
6 x: u8,
7 y: E,
8};
39
410const assert = @import("std").debug.assert;
511
612test "bug 394 fixed" {
7 const x = S { .x = 3, .y = E {.B = 1 } };
13 const x = S {
14 .x = 3,
15 .y = E {
16 .B = 1,
17 },
18 };
819 assert(x.x == 3);
920}
test/cases/bugs/655.zig+1-1
......@@ -8,5 +8,5 @@ test "function with &const parameter with type dereferenced by namespace" {
88}
99
1010fn foo(x: &const other_file.Integer) void {
11 std.debug.assert(*x == 1234);
11 std.debug.assert(x.* == 1234);
1212}
test/cases/bugs/656.zig+7-4
......@@ -14,12 +14,15 @@ test "nullable if after an if in a switch prong of a switch with 2 prongs in an
1414}
1515
1616fn foo(a: bool, b: bool) void {
17 var prefix_op = PrefixOp { .AddrOf = Value { .align_expr = 1234 } };
18 if (a) {
19 } else {
17 var prefix_op = PrefixOp {
18 .AddrOf = Value {
19 .align_expr = 1234,
20 },
21 };
22 if (a) {} else {
2023 switch (prefix_op) {
2124 PrefixOp.AddrOf => |addr_of_info| {
22 if (b) { }
25 if (b) {}
2326 if (addr_of_info.align_expr) |align_expr| {
2427 assert(align_expr == 1234);
2528 }
test/cases/bugs/828.zig+5-5
......@@ -1,10 +1,10 @@
11const CountBy = struct {
22 a: usize,
3
3
44 const One = CountBy {
55 .a = 1,
66 };
7
7
88 pub fn counter(self: &const CountBy) Counter {
99 return Counter {
1010 .i = 0,
......@@ -14,7 +14,7 @@ const CountBy = struct {
1414
1515const Counter = struct {
1616 i: usize,
17
17
1818 pub fn count(self: &Counter) bool {
1919 self.i += 1;
2020 return self.i <= 10;
......@@ -24,8 +24,8 @@ const Counter = struct {
2424fn constCount(comptime cb: &const CountBy, comptime unused: u32) void {
2525 comptime {
2626 var cnt = cb.counter();
27 if(cnt.i != 0) @compileError("Counter instance reused!");
28 while(cnt.count()){}
27 if (cnt.i != 0) @compileError("Counter instance reused!");
28 while (cnt.count()) {}
2929 }
3030}
3131
test/cases/bugs/920.zig+12-7
......@@ -12,8 +12,7 @@ const ZigTable = struct {
1212 zero_case: fn(&Random, f64) f64,
1313};
1414
15fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64,
16 comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable {
15fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64, comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable {
1716 var tables: ZigTable = undefined;
1817
1918 tables.is_symmetric = is_symmetric;
......@@ -26,12 +25,12 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
2625
2726 for (tables.x[2..256]) |*entry, i| {
2827 const last = tables.x[2 + i - 1];
29 *entry = f_inv(v / last + f(last));
28 entry.* = f_inv(v / last + f(last));
3029 }
3130 tables.x[256] = 0;
3231
3332 for (tables.f[0..]) |*entry, i| {
34 *entry = f(tables.x[i]);
33 entry.* = f(tables.x[i]);
3534 }
3635
3736 return tables;
......@@ -40,9 +39,15 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
4039const norm_r = 3.6541528853610088;
4140const norm_v = 0.00492867323399;
4241
43fn norm_f(x: f64) f64 { return math.exp(-x * x / 2.0); }
44fn norm_f_inv(y: f64) f64 { return math.sqrt(-2.0 * math.ln(y)); }
45fn norm_zero_case(random: &Random, u: f64) f64 { return 0.0; }
42fn norm_f(x: f64) f64 {
43 return math.exp(-x * x / 2.0);
44}
45fn norm_f_inv(y: f64) f64 {
46 return math.sqrt(-2.0 * math.ln(y));
47}
48fn norm_zero_case(random: &Random, u: f64) f64 {
49 return 0.0;
50}
4651
4752const NormalDist = blk: {
4853 @setEvalBranchQuota(30000);
test/cases/cast.zig+42-28
......@@ -17,7 +17,7 @@ test "pointer reinterpret const float to int" {
1717 const float: f64 = 5.99999999999994648725e-01;
1818 const float_ptr = &float;
1919 const int_ptr = @ptrCast(&const i32, float_ptr);
20 const int_val = *int_ptr;
20 const int_val = int_ptr.*;
2121 assert(int_val == 858993411);
2222}
2323
......@@ -29,25 +29,31 @@ test "implicitly cast a pointer to a const pointer of it" {
2929}
3030
3131fn funcWithConstPtrPtr(x: &const &i32) void {
32 **x += 1;
32 x.*.* += 1;
3333}
3434
3535test "implicitly cast a container to a const pointer of it" {
36 const z = Struct(void) { .x = void{} };
36 const z = Struct(void) {
37 .x = void{},
38 };
3739 assert(0 == @sizeOf(@typeOf(z)));
3840 assert(void{} == Struct(void).pointer(z).x);
3941 assert(void{} == Struct(void).pointer(&z).x);
4042 assert(void{} == Struct(void).maybePointer(z).x);
4143 assert(void{} == Struct(void).maybePointer(&z).x);
4244 assert(void{} == Struct(void).maybePointer(null).x);
43 const s = Struct(u8) { .x = 42 };
45 const s = Struct(u8) {
46 .x = 42,
47 };
4448 assert(0 != @sizeOf(@typeOf(s)));
4549 assert(42 == Struct(u8).pointer(s).x);
4650 assert(42 == Struct(u8).pointer(&s).x);
4751 assert(42 == Struct(u8).maybePointer(s).x);
4852 assert(42 == Struct(u8).maybePointer(&s).x);
4953 assert(0 == Struct(u8).maybePointer(null).x);
50 const u = Union { .x = 42 };
54 const u = Union {
55 .x = 42,
56 };
5157 assert(42 == Union.pointer(u).x);
5258 assert(42 == Union.pointer(&u).x);
5359 assert(42 == Union.maybePointer(u).x);
......@@ -67,12 +73,14 @@ fn Struct(comptime T: type) type {
6773 x: T,
6874
6975 fn pointer(self: &const Self) Self {
70 return *self;
76 return self.*;
7177 }
7278
7379 fn maybePointer(self: ?&const Self) Self {
74 const none = Self { .x = if (T == void) void{} else 0 };
75 return *(self ?? &none);
80 const none = Self {
81 .x = if (T == void) void{} else 0,
82 };
83 return (self ?? &none).*;
7684 }
7785 };
7886}
......@@ -81,12 +89,14 @@ const Union = union {
8189 x: u8,
8290
8391 fn pointer(self: &const Union) Union {
84 return *self;
92 return self.*;
8593 }
8694
8795 fn maybePointer(self: ?&const Union) Union {
88 const none = Union { .x = 0 };
89 return *(self ?? &none);
96 const none = Union {
97 .x = 0,
98 };
99 return (self ?? &none).*;
90100 }
91101};
92102
......@@ -95,11 +105,11 @@ const Enum = enum {
95105 Some,
96106
97107 fn pointer(self: &const Enum) Enum {
98 return *self;
108 return self.*;
99109 }
100110
101111 fn maybePointer(self: ?&const Enum) Enum {
102 return *(self ?? &Enum.None);
112 return (self ?? &Enum.None).*;
103113 }
104114};
105115
......@@ -108,19 +118,21 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
108118 const Self = this;
109119 x: u8,
110120 fn constConst(p: &const &const Self) u8 {
111 return (*p).x;
121 return (p.*).x;
112122 }
113123 fn maybeConstConst(p: ?&const &const Self) u8 {
114 return (*??p).x;
124 return ((??p).*).x;
115125 }
116126 fn constConstConst(p: &const &const &const Self) u8 {
117 return (**p).x;
127 return (p.*.*).x;
118128 }
119129 fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
120 return (**??p).x;
130 return ((??p).*.*).x;
121131 }
122132 };
123 const s = S { .x = 42 };
133 const s = S {
134 .x = 42,
135 };
124136 const p = &s;
125137 const q = &p;
126138 const r = &q;
......@@ -154,7 +166,6 @@ fn boolToStr(b: bool) []const u8 {
154166 return if (b) "true" else "false";
155167}
156168
157
158169test "peer resolve array and const slice" {
159170 testPeerResolveArrayConstSlice(true);
160171 comptime testPeerResolveArrayConstSlice(true);
......@@ -168,12 +179,12 @@ fn testPeerResolveArrayConstSlice(b: bool) void {
168179
169180test "integer literal to &const int" {
170181 const x: &const i32 = 3;
171 assert(*x == 3);
182 assert(x.* == 3);
172183}
173184
174185test "string literal to &const []const u8" {
175186 const x: &const []const u8 = "hello";
176 assert(mem.eql(u8, *x, "hello"));
187 assert(mem.eql(u8, x.*, "hello"));
177188}
178189
179190test "implicitly cast from T to error!?T" {
......@@ -191,7 +202,9 @@ fn castToMaybeTypeError(z: i32) void {
191202 const f = z;
192203 const g: error!?i32 = f;
193204
194 const a = A{ .a = z };
205 const a = A {
206 .a = z,
207 };
195208 const b: error!?A = a;
196209 assert((??(b catch unreachable)).a == 1);
197210}
......@@ -205,7 +218,6 @@ fn implicitIntLitToMaybe() void {
205218 const g: error!?i32 = 1;
206219}
207220
208
209221test "return null from fn() error!?&T" {
210222 const a = returnNullFromMaybeTypeErrorRef();
211223 const b = returnNullLitFromMaybeTypeErrorRef();
......@@ -235,7 +247,6 @@ fn peerTypeTAndMaybeT(c: bool, b: bool) ?usize {
235247 return usize(3);
236248}
237249
238
239250test "peer type resolution: [0]u8 and []const u8" {
240251 assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
241252 assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
......@@ -246,7 +257,7 @@ test "peer type resolution: [0]u8 and []const u8" {
246257}
247258fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
248259 if (a) {
249 return []const u8 {};
260 return []const u8{};
250261 }
251262
252263 return slice[0..1];
......@@ -261,7 +272,6 @@ fn castToMaybeSlice() ?[]const u8 {
261272 return "hi";
262273}
263274
264
265275test "implicitly cast from [0]T to error![]T" {
266276 testCastZeroArrayToErrSliceMut();
267277 comptime testCastZeroArrayToErrSliceMut();
......@@ -329,7 +339,6 @@ fn foo(args: ...) void {
329339 assert(@typeOf(args[0]) == &const [5]u8);
330340}
331341
332
333342test "peer type resolution: error and [N]T" {
334343 // TODO: implicit error!T to error!U where T can implicitly cast to U
335344 //assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
......@@ -378,7 +387,12 @@ fn cast128Float(x: u128) f128 {
378387}
379388
380389test "const slice widen cast" {
381 const bytes align(4) = []u8{0x12, 0x12, 0x12, 0x12};
390 const bytes align(4) = []u8 {
391 0x12,
392 0x12,
393 0x12,
394 0x12,
395 };
382396
383397 const u32_value = ([]const u32)(bytes[0..])[0];
384398 assert(u32_value == 0x12121212);
test/cases/coroutines.zig+9-9
......@@ -36,7 +36,7 @@ async fn testAsyncSeq() void {
3636 suspend;
3737 seq('d');
3838}
39var points = []u8{0} ** "abcdefg".len;
39var points = []u8 {0} ** "abcdefg".len;
4040var index: usize = 0;
4141
4242fn seq(c: u8) void {
......@@ -94,7 +94,7 @@ async fn await_another() i32 {
9494 return 1234;
9595}
9696
97var await_points = []u8{0} ** "abcdefghi".len;
97var await_points = []u8 {0} ** "abcdefghi".len;
9898var await_seq_index: usize = 0;
9999
100100fn await_seq(c: u8) void {
......@@ -102,7 +102,6 @@ fn await_seq(c: u8) void {
102102 await_seq_index += 1;
103103}
104104
105
106105var early_final_result: i32 = 0;
107106
108107test "coroutine await early return" {
......@@ -126,7 +125,7 @@ async fn early_another() i32 {
126125 return 1234;
127126}
128127
129var early_points = []u8{0} ** "abcdef".len;
128var early_points = []u8 {0} ** "abcdef".len;
130129var early_seq_index: usize = 0;
131130
132131fn early_seq(c: u8) void {
......@@ -175,8 +174,8 @@ test "async fn pointer in a struct field" {
175174}
176175
177176async<&std.mem.Allocator> fn simpleAsyncFn2(y: &i32) void {
178 defer *y += 2;
179 *y += 1;
177 defer y.* += 2;
178 y.* += 1;
180179 suspend;
181180}
182181
......@@ -205,7 +204,8 @@ test "error return trace across suspend points - async return" {
205204 cancel p2;
206205}
207206
208fn nonFailing() promise->error!void {
207// TODO https://github.com/zig-lang/zig/issues/760
208fn nonFailing() (promise->error!void) {
209209 return async<std.debug.global_allocator> suspendThenFail() catch unreachable;
210210}
211211
......@@ -239,7 +239,7 @@ async fn testBreakFromSuspend(my_result: &i32) void {
239239 s: suspend |p| {
240240 break :s;
241241 }
242 *my_result += 1;
242 my_result.* += 1;
243243 suspend;
244 *my_result += 1;
244 my_result.* += 1;
245245}
test/cases/defer.zig+12-3
......@@ -5,9 +5,18 @@ var index: usize = undefined;
55
66fn runSomeErrorDefers(x: bool) !bool {
77 index = 0;
8 defer {result[index] = 'a'; index += 1;}
9 errdefer {result[index] = 'b'; index += 1;}
10 defer {result[index] = 'c'; index += 1;}
8 defer {
9 result[index] = 'a';
10 index += 1;
11 }
12 errdefer {
13 result[index] = 'b';
14 index += 1;
15 }
16 defer {
17 result[index] = 'c';
18 index += 1;
19 }
1120 return if (x) x else error.FalseNotAllowed;
1221}
1322
test/cases/enum.zig+548-58
......@@ -2,8 +2,15 @@ const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
44test "enum type" {
5 const foo1 = Foo{ .One = 13};
6 const foo2 = Foo{. Two = Point { .x = 1234, .y = 5678, }};
5 const foo1 = Foo {
6 .One = 13,
7 };
8 const foo2 = Foo {
9 .Two = Point {
10 .x = 1234,
11 .y = 5678,
12 },
13 };
714 const bar = Bar.B;
815
916 assert(bar == Bar.B);
......@@ -41,26 +48,31 @@ const Bar = enum {
4148};
4249
4350fn returnAnInt(x: i32) Foo {
44 return Foo { .One = x };
51 return Foo {
52 .One = x,
53 };
4554}
4655
47
4856test "constant enum with payload" {
49 var empty = AnEnumWithPayload {.Empty = {}};
50 var full = AnEnumWithPayload {.Full = 13};
57 var empty = AnEnumWithPayload {
58 .Empty = {},
59 };
60 var full = AnEnumWithPayload {
61 .Full = 13,
62 };
5163 shouldBeEmpty(empty);
5264 shouldBeNotEmpty(full);
5365}
5466
5567fn shouldBeEmpty(x: &const AnEnumWithPayload) void {
56 switch (*x) {
68 switch (x.*) {
5769 AnEnumWithPayload.Empty => {},
5870 else => unreachable,
5971 }
6072}
6173
6274fn shouldBeNotEmpty(x: &const AnEnumWithPayload) void {
63 switch (*x) {
75 switch (x.*) {
6476 AnEnumWithPayload.Empty => unreachable,
6577 else => {},
6678 }
......@@ -71,8 +83,6 @@ const AnEnumWithPayload = union(enum) {
7183 Full: i32,
7284};
7385
74
75
7686const Number = enum {
7787 Zero,
7888 One,
......@@ -93,7 +103,6 @@ fn shouldEqual(n: Number, expected: u3) void {
93103 assert(u3(n) == expected);
94104}
95105
96
97106test "int to enum" {
98107 testIntToEnumEval(3);
99108}
......@@ -108,7 +117,6 @@ const IntToEnumNumber = enum {
108117 Four,
109118};
110119
111
112120test "@tagName" {
113121 assert(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
114122 comptime assert(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
......@@ -124,7 +132,6 @@ const BareNumber = enum {
124132 Three,
125133};
126134
127
128135test "enum alignment" {
129136 comptime {
130137 assert(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
......@@ -137,47 +144,529 @@ const AlignTestEnum = union(enum) {
137144 B: u64,
138145};
139146
140const ValueCount1 = enum { I0 };
141const ValueCount2 = enum { I0, I1 };
147const ValueCount1 = enum {
148 I0,
149};
150const ValueCount2 = enum {
151 I0,
152 I1,
153};
142154const ValueCount256 = enum {
143 I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15,
144 I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31,
145 I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47,
146 I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63,
147 I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79,
148 I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95,
149 I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109,
150 I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123,
151 I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137,
152 I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151,
153 I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165,
154 I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179,
155 I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193,
156 I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207,
157 I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221,
158 I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235,
159 I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249,
160 I250, I251, I252, I253, I254, I255
155 I0,
156 I1,
157 I2,
158 I3,
159 I4,
160 I5,
161 I6,
162 I7,
163 I8,
164 I9,
165 I10,
166 I11,
167 I12,
168 I13,
169 I14,
170 I15,
171 I16,
172 I17,
173 I18,
174 I19,
175 I20,
176 I21,
177 I22,
178 I23,
179 I24,
180 I25,
181 I26,
182 I27,
183 I28,
184 I29,
185 I30,
186 I31,
187 I32,
188 I33,
189 I34,
190 I35,
191 I36,
192 I37,
193 I38,
194 I39,
195 I40,
196 I41,
197 I42,
198 I43,
199 I44,
200 I45,
201 I46,
202 I47,
203 I48,
204 I49,
205 I50,
206 I51,
207 I52,
208 I53,
209 I54,
210 I55,
211 I56,
212 I57,
213 I58,
214 I59,
215 I60,
216 I61,
217 I62,
218 I63,
219 I64,
220 I65,
221 I66,
222 I67,
223 I68,
224 I69,
225 I70,
226 I71,
227 I72,
228 I73,
229 I74,
230 I75,
231 I76,
232 I77,
233 I78,
234 I79,
235 I80,
236 I81,
237 I82,
238 I83,
239 I84,
240 I85,
241 I86,
242 I87,
243 I88,
244 I89,
245 I90,
246 I91,
247 I92,
248 I93,
249 I94,
250 I95,
251 I96,
252 I97,
253 I98,
254 I99,
255 I100,
256 I101,
257 I102,
258 I103,
259 I104,
260 I105,
261 I106,
262 I107,
263 I108,
264 I109,
265 I110,
266 I111,
267 I112,
268 I113,
269 I114,
270 I115,
271 I116,
272 I117,
273 I118,
274 I119,
275 I120,
276 I121,
277 I122,
278 I123,
279 I124,
280 I125,
281 I126,
282 I127,
283 I128,
284 I129,
285 I130,
286 I131,
287 I132,
288 I133,
289 I134,
290 I135,
291 I136,
292 I137,
293 I138,
294 I139,
295 I140,
296 I141,
297 I142,
298 I143,
299 I144,
300 I145,
301 I146,
302 I147,
303 I148,
304 I149,
305 I150,
306 I151,
307 I152,
308 I153,
309 I154,
310 I155,
311 I156,
312 I157,
313 I158,
314 I159,
315 I160,
316 I161,
317 I162,
318 I163,
319 I164,
320 I165,
321 I166,
322 I167,
323 I168,
324 I169,
325 I170,
326 I171,
327 I172,
328 I173,
329 I174,
330 I175,
331 I176,
332 I177,
333 I178,
334 I179,
335 I180,
336 I181,
337 I182,
338 I183,
339 I184,
340 I185,
341 I186,
342 I187,
343 I188,
344 I189,
345 I190,
346 I191,
347 I192,
348 I193,
349 I194,
350 I195,
351 I196,
352 I197,
353 I198,
354 I199,
355 I200,
356 I201,
357 I202,
358 I203,
359 I204,
360 I205,
361 I206,
362 I207,
363 I208,
364 I209,
365 I210,
366 I211,
367 I212,
368 I213,
369 I214,
370 I215,
371 I216,
372 I217,
373 I218,
374 I219,
375 I220,
376 I221,
377 I222,
378 I223,
379 I224,
380 I225,
381 I226,
382 I227,
383 I228,
384 I229,
385 I230,
386 I231,
387 I232,
388 I233,
389 I234,
390 I235,
391 I236,
392 I237,
393 I238,
394 I239,
395 I240,
396 I241,
397 I242,
398 I243,
399 I244,
400 I245,
401 I246,
402 I247,
403 I248,
404 I249,
405 I250,
406 I251,
407 I252,
408 I253,
409 I254,
410 I255,
161411};
162412const ValueCount257 = enum {
163 I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15,
164 I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31,
165 I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47,
166 I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63,
167 I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79,
168 I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95,
169 I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109,
170 I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123,
171 I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137,
172 I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151,
173 I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165,
174 I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179,
175 I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193,
176 I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207,
177 I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221,
178 I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235,
179 I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249,
180 I250, I251, I252, I253, I254, I255, I256
413 I0,
414 I1,
415 I2,
416 I3,
417 I4,
418 I5,
419 I6,
420 I7,
421 I8,
422 I9,
423 I10,
424 I11,
425 I12,
426 I13,
427 I14,
428 I15,
429 I16,
430 I17,
431 I18,
432 I19,
433 I20,
434 I21,
435 I22,
436 I23,
437 I24,
438 I25,
439 I26,
440 I27,
441 I28,
442 I29,
443 I30,
444 I31,
445 I32,
446 I33,
447 I34,
448 I35,
449 I36,
450 I37,
451 I38,
452 I39,
453 I40,
454 I41,
455 I42,
456 I43,
457 I44,
458 I45,
459 I46,
460 I47,
461 I48,
462 I49,
463 I50,
464 I51,
465 I52,
466 I53,
467 I54,
468 I55,
469 I56,
470 I57,
471 I58,
472 I59,
473 I60,
474 I61,
475 I62,
476 I63,
477 I64,
478 I65,
479 I66,
480 I67,
481 I68,
482 I69,
483 I70,
484 I71,
485 I72,
486 I73,
487 I74,
488 I75,
489 I76,
490 I77,
491 I78,
492 I79,
493 I80,
494 I81,
495 I82,
496 I83,
497 I84,
498 I85,
499 I86,
500 I87,
501 I88,
502 I89,
503 I90,
504 I91,
505 I92,
506 I93,
507 I94,
508 I95,
509 I96,
510 I97,
511 I98,
512 I99,
513 I100,
514 I101,
515 I102,
516 I103,
517 I104,
518 I105,
519 I106,
520 I107,
521 I108,
522 I109,
523 I110,
524 I111,
525 I112,
526 I113,
527 I114,
528 I115,
529 I116,
530 I117,
531 I118,
532 I119,
533 I120,
534 I121,
535 I122,
536 I123,
537 I124,
538 I125,
539 I126,
540 I127,
541 I128,
542 I129,
543 I130,
544 I131,
545 I132,
546 I133,
547 I134,
548 I135,
549 I136,
550 I137,
551 I138,
552 I139,
553 I140,
554 I141,
555 I142,
556 I143,
557 I144,
558 I145,
559 I146,
560 I147,
561 I148,
562 I149,
563 I150,
564 I151,
565 I152,
566 I153,
567 I154,
568 I155,
569 I156,
570 I157,
571 I158,
572 I159,
573 I160,
574 I161,
575 I162,
576 I163,
577 I164,
578 I165,
579 I166,
580 I167,
581 I168,
582 I169,
583 I170,
584 I171,
585 I172,
586 I173,
587 I174,
588 I175,
589 I176,
590 I177,
591 I178,
592 I179,
593 I180,
594 I181,
595 I182,
596 I183,
597 I184,
598 I185,
599 I186,
600 I187,
601 I188,
602 I189,
603 I190,
604 I191,
605 I192,
606 I193,
607 I194,
608 I195,
609 I196,
610 I197,
611 I198,
612 I199,
613 I200,
614 I201,
615 I202,
616 I203,
617 I204,
618 I205,
619 I206,
620 I207,
621 I208,
622 I209,
623 I210,
624 I211,
625 I212,
626 I213,
627 I214,
628 I215,
629 I216,
630 I217,
631 I218,
632 I219,
633 I220,
634 I221,
635 I222,
636 I223,
637 I224,
638 I225,
639 I226,
640 I227,
641 I228,
642 I229,
643 I230,
644 I231,
645 I232,
646 I233,
647 I234,
648 I235,
649 I236,
650 I237,
651 I238,
652 I239,
653 I240,
654 I241,
655 I242,
656 I243,
657 I244,
658 I245,
659 I246,
660 I247,
661 I248,
662 I249,
663 I250,
664 I251,
665 I252,
666 I253,
667 I254,
668 I255,
669 I256,
181670};
182671
183672test "enum sizes" {
......@@ -189,11 +678,11 @@ test "enum sizes" {
189678 }
190679}
191680
192const Small2 = enum (u2) {
681const Small2 = enum(u2) {
193682 One,
194683 Two,
195684};
196const Small = enum (u2) {
685const Small = enum(u2) {
197686 One,
198687 Two,
199688 Three,
......@@ -213,8 +702,7 @@ test "set enum tag type" {
213702 }
214703}
215704
216
217const A = enum (u3) {
705const A = enum(u3) {
218706 One,
219707 Two,
220708 Three,
......@@ -225,7 +713,7 @@ const A = enum (u3) {
225713 Four2,
226714};
227715
228const B = enum (u3) {
716const B = enum(u3) {
229717 One3,
230718 Two3,
231719 Three3,
......@@ -236,7 +724,7 @@ const B = enum (u3) {
236724 Four23,
237725};
238726
239const C = enum (u2) {
727const C = enum(u2) {
240728 One4,
241729 Two4,
242730 Three4,
......@@ -389,7 +877,9 @@ test "enum with tag values don't require parens" {
389877}
390878
391879test "enum with 1 field but explicit tag type should still have the tag type" {
392 const Enum = enum(u8) { B = 2 };
880 const Enum = enum(u8) {
881 B = 2,
882 };
393883 comptime @import("std").debug.assert(@sizeOf(Enum) == @sizeOf(u8));
394884}
395885
test/cases/enum_with_members.zig+7-3
......@@ -7,7 +7,7 @@ const ET = union(enum) {
77 UINT: u32,
88
99 pub fn print(a: &const ET, buf: []u8) error!usize {
10 return switch (*a) {
10 return switch (a.*) {
1111 ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
1212 ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
1313 };
......@@ -15,8 +15,12 @@ const ET = union(enum) {
1515};
1616
1717test "enum with members" {
18 const a = ET { .SINT = -42 };
19 const b = ET { .UINT = 42 };
18 const a = ET {
19 .SINT = -42,
20 };
21 const b = ET {
22 .UINT = 42,
23 };
2024 var buf: [20]u8 = undefined;
2125
2226 assert((a.print(buf[0..]) catch unreachable) == 3);
test/cases/error.zig+30-26
......@@ -30,14 +30,12 @@ test "@errorName" {
3030 assert(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
3131}
3232
33
3433test "error values" {
3534 const a = i32(error.err1);
3635 const b = i32(error.err2);
3736 assert(a != b);
3837}
3938
40
4139test "redefinition of error values allowed" {
4240 shouldBeNotEqual(error.AnError, error.SecondError);
4341}
......@@ -45,7 +43,6 @@ fn shouldBeNotEqual(a: error, b: error) void {
4543 if (a == b) unreachable;
4644}
4745
48
4946test "error binary operator" {
5047 const a = errBinaryOperatorG(true) catch 3;
5148 const b = errBinaryOperatorG(false) catch 3;
......@@ -56,20 +53,20 @@ fn errBinaryOperatorG(x: bool) error!isize {
5653 return if (x) error.ItBroke else isize(10);
5754}
5855
59
6056test "unwrap simple value from error" {
6157 const i = unwrapSimpleValueFromErrorDo() catch unreachable;
6258 assert(i == 13);
6359}
64fn unwrapSimpleValueFromErrorDo() error!isize { return 13; }
65
60fn unwrapSimpleValueFromErrorDo() error!isize {
61 return 13;
62}
6663
6764test "error return in assignment" {
6865 doErrReturnInAssignment() catch unreachable;
6966}
7067
7168fn doErrReturnInAssignment() error!void {
72 var x : i32 = undefined;
69 var x: i32 = undefined;
7370 x = try makeANonErr();
7471}
7572
......@@ -95,7 +92,10 @@ test "error set type " {
9592 comptime testErrorSetType();
9693}
9794
98const MyErrSet = error {OutOfMemory, FileNotFound};
95const MyErrSet = error {
96 OutOfMemory,
97 FileNotFound,
98};
9999
100100fn testErrorSetType() void {
101101 assert(@memberCount(MyErrSet) == 2);
......@@ -109,14 +109,19 @@ fn testErrorSetType() void {
109109 }
110110}
111111
112
113112test "explicit error set cast" {
114113 testExplicitErrorSetCast(Set1.A);
115114 comptime testExplicitErrorSetCast(Set1.A);
116115}
117116
118const Set1 = error{A, B};
119const Set2 = error{A, C};
117const Set1 = error {
118 A,
119 B,
120};
121const Set2 = error {
122 A,
123 C,
124};
120125
121126fn testExplicitErrorSetCast(set1: Set1) void {
122127 var x = Set2(set1);
......@@ -129,7 +134,8 @@ test "comptime test error for empty error set" {
129134 comptime testComptimeTestErrorEmptySet(1234);
130135}
131136
132const EmptyErrorSet = error {};
137const EmptyErrorSet = error {
138};
133139
134140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {
135141 if (x) |v| assert(v == 1234) else |err| @compileError("bad");
......@@ -145,7 +151,9 @@ test "comptime err to int of error set with only 1 possible value" {
145151 testErrToIntWithOnePossibleValue(error.A, u32(error.A));
146152 comptime testErrToIntWithOnePossibleValue(error.A, u32(error.A));
147153}
148fn testErrToIntWithOnePossibleValue(x: error{A}, comptime value: u32) void {
154fn testErrToIntWithOnePossibleValue(x: error {
155 A,
156}, comptime value: u32) void {
149157 if (u32(x) != value) {
150158 @compileError("bad");
151159 }
......@@ -176,7 +184,6 @@ fn quux_1() !i32 {
176184 return error.C;
177185}
178186
179
180187test "error: fn returning empty error set can be passed as fn returning any error" {
181188 entry();
182189 comptime entry();
......@@ -186,24 +193,24 @@ fn entry() void {
186193 foo2(bar2);
187194}
188195
189fn foo2(f: fn()error!void) void {
196fn foo2(f: fn() error!void) void {
190197 const x = f();
191198}
192199
193fn bar2() (error{}!void) { }
194
200fn bar2() (error {
201}!void) {}
195202
196203test "error: Zero sized error set returned with value payload crash" {
197204 _ = foo3(0);
198205 _ = comptime foo3(0);
199206}
200207
201const Error = error{};
208const Error = error {
209};
202210fn foo3(b: usize) Error!usize {
203211 return b;
204212}
205213
206
207214test "error: Infer error set from literals" {
208215 _ = nullLiteral("n") catch |err| handleErrors(err);
209216 _ = floatLiteral("n") catch |err| handleErrors(err);
......@@ -215,29 +222,26 @@ test "error: Infer error set from literals" {
215222
216223fn handleErrors(err: var) noreturn {
217224 switch (err) {
218 error.T => {}
225 error.T => {},
219226 }
220227
221228 unreachable;
222229}
223230
224231fn nullLiteral(str: []const u8) !?i64 {
225 if (str[0] == 'n')
226 return null;
232 if (str[0] == 'n') return null;
227233
228234 return error.T;
229235}
230236
231237fn floatLiteral(str: []const u8) !?f64 {
232 if (str[0] == 'n')
233 return 1.0;
238 if (str[0] == 'n') return 1.0;
234239
235240 return error.T;
236241}
237242
238243fn intLiteral(str: []const u8) !?i64 {
239 if (str[0] == 'n')
240 return 1;
244 if (str[0] == 'n') return 1;
241245
242246 return error.T;
243247}
test/cases/eval.zig+88-55
......@@ -11,8 +11,6 @@ fn fibonacci(x: i32) i32 {
1111 return fibonacci(x - 1) + fibonacci(x - 2);
1212}
1313
14
15
1614fn unwrapAndAddOne(blah: ?i32) i32 {
1715 return ??blah + 1;
1816}
......@@ -40,13 +38,13 @@ test "inline variable gets result of const if" {
4038 assert(gimme1or2(false) == 2);
4139}
4240
43
4441test "static function evaluation" {
4542 assert(statically_added_number == 3);
4643}
4744const statically_added_number = staticAdd(1, 2);
48fn staticAdd(a: i32, b: i32) i32 { return a + b; }
49
45fn staticAdd(a: i32, b: i32) i32 {
46 return a + b;
47}
5048
5149test "const expr eval on single expr blocks" {
5250 assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
......@@ -64,9 +62,6 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
6462 return result;
6563}
6664
67
68
69
7065test "statically initialized list" {
7166 assert(static_point_list[0].x == 1);
7267 assert(static_point_list[0].y == 2);
......@@ -77,7 +72,10 @@ const Point = struct {
7772 x: i32,
7873 y: i32,
7974};
80const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) };
75const static_point_list = []Point {
76 makePoint(1, 2),
77 makePoint(3, 4),
78};
8179fn makePoint(x: i32, y: i32) Point {
8280 return Point {
8381 .x = x,
......@@ -85,7 +83,6 @@ fn makePoint(x: i32, y: i32) Point {
8583 };
8684}
8785
88
8986test "static eval list init" {
9087 assert(static_vec3.data[2] == 1.0);
9188 assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
......@@ -96,17 +93,19 @@ pub const Vec3 = struct {
9693};
9794pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
9895 return Vec3 {
99 .data = []f32 { x, y, z, },
96 .data = []f32 {
97 x,
98 y,
99 z,
100 },
100101 };
101102}
102103
103
104104test "constant expressions" {
105 var array : [array_size]u8 = undefined;
105 var array: [array_size]u8 = undefined;
106106 assert(@sizeOf(@typeOf(array)) == 20);
107107}
108const array_size : u8 = 20;
109
108const array_size: u8 = 20;
110109
111110test "constant struct with negation" {
112111 assert(vertices[0].x == -0.6);
......@@ -119,12 +118,29 @@ const Vertex = struct {
119118 b: f32,
120119};
121120const vertices = []Vertex {
122 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
123 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
124 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
121 Vertex {
122 .x = -0.6,
123 .y = -0.4,
124 .r = 1.0,
125 .g = 0.0,
126 .b = 0.0,
127 },
128 Vertex {
129 .x = 0.6,
130 .y = -0.4,
131 .r = 0.0,
132 .g = 1.0,
133 .b = 0.0,
134 },
135 Vertex {
136 .x = 0.0,
137 .y = 0.6,
138 .r = 0.0,
139 .g = 0.0,
140 .b = 1.0,
141 },
125142};
126143
127
128144test "statically initialized struct" {
129145 st_init_str_foo.x += 1;
130146 assert(st_init_str_foo.x == 14);
......@@ -133,15 +149,21 @@ const StInitStrFoo = struct {
133149 x: i32,
134150 y: bool,
135151};
136var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
137
152var st_init_str_foo = StInitStrFoo {
153 .x = 13,
154 .y = true,
155};
138156
139157test "statically initalized array literal" {
140 const y : [4]u8 = st_init_arr_lit_x;
158 const y: [4]u8 = st_init_arr_lit_x;
141159 assert(y[3] == 4);
142160}
143const st_init_arr_lit_x = []u8{1,2,3,4};
144
161const st_init_arr_lit_x = []u8 {
162 1,
163 2,
164 3,
165 4,
166};
145167
146168test "const slice" {
147169 comptime {
......@@ -198,14 +220,29 @@ const CmdFn = struct {
198220 func: fn(i32) i32,
199221};
200222
201const cmd_fns = []CmdFn{
202 CmdFn {.name = "one", .func = one},
203 CmdFn {.name = "two", .func = two},
204 CmdFn {.name = "three", .func = three},
223const cmd_fns = []CmdFn {
224 CmdFn {
225 .name = "one",
226 .func = one,
227 },
228 CmdFn {
229 .name = "two",
230 .func = two,
231 },
232 CmdFn {
233 .name = "three",
234 .func = three,
235 },
205236};
206fn one(value: i32) i32 { return value + 1; }
207fn two(value: i32) i32 { return value + 2; }
208fn three(value: i32) i32 { return value + 3; }
237fn one(value: i32) i32 {
238 return value + 1;
239}
240fn two(value: i32) i32 {
241 return value + 2;
242}
243fn three(value: i32) i32 {
244 return value + 3;
245}
209246
210247fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
211248 var result: i32 = start_value;
......@@ -229,7 +266,7 @@ test "eval @setRuntimeSafety at compile-time" {
229266 assert(result == 1234);
230267}
231268
232fn fnWithSetRuntimeSafety() i32{
269fn fnWithSetRuntimeSafety() i32 {
233270 @setRuntimeSafety(true);
234271 return 1234;
235272}
......@@ -244,7 +281,6 @@ fn fnWithFloatMode() f32 {
244281 return 1234.0;
245282}
246283
247
248284const SimpleStruct = struct {
249285 field: i32,
250286
......@@ -253,7 +289,9 @@ const SimpleStruct = struct {
253289 }
254290};
255291
256var simple_struct = SimpleStruct{ .field = 1234, };
292var simple_struct = SimpleStruct {
293 .field = 1234,
294};
257295
258296const bound_fn = simple_struct.method;
259297
......@@ -261,8 +299,6 @@ test "call method on bound fn referring to var instance" {
261299 assert(bound_fn() == 1237);
262300}
263301
264
265
266302test "ptr to local array argument at comptime" {
267303 comptime {
268304 var bytes: [10]u8 = undefined;
......@@ -277,7 +313,6 @@ fn modifySomeBytes(bytes: []u8) void {
277313 bytes[9] = 'b';
278314}
279315
280
281316test "comparisons 0 <= uint and 0 > uint should be comptime" {
282317 testCompTimeUIntComparisons(1234);
283318}
......@@ -296,8 +331,6 @@ fn testCompTimeUIntComparisons(x: u32) void {
296331 }
297332}
298333
299
300
301334test "const ptr to variable data changes at runtime" {
302335 assert(foo_ref.name[0] == 'a');
303336 foo_ref.name = "b";
......@@ -308,11 +341,11 @@ const Foo = struct {
308341 name: []const u8,
309342};
310343
311var foo_contents = Foo { .name = "a", };
344var foo_contents = Foo {
345 .name = "a",
346};
312347const foo_ref = &foo_contents;
313348
314
315
316349test "create global array with for loop" {
317350 assert(global_array[5] == 5 * 5);
318351 assert(global_array[9] == 9 * 9);
......@@ -321,7 +354,7 @@ test "create global array with for loop" {
321354const global_array = x: {
322355 var result: [10]usize = undefined;
323356 for (result) |*item, index| {
324 *item = index * index;
357 item.* = index * index;
325358 }
326359 break :x result;
327360};
......@@ -379,7 +412,7 @@ test "f128 at compile time is lossy" {
379412
380413pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
381414 return struct {
382 pub const Node = struct { };
415 pub const Node = struct {};
383416 };
384417}
385418
......@@ -401,10 +434,10 @@ fn copyWithPartialInline(s: []u32, b: []u8) void {
401434 comptime var i: usize = 0;
402435 inline while (i < 4) : (i += 1) {
403436 s[i] = 0;
404 s[i] |= u32(b[i*4+0]) << 24;
405 s[i] |= u32(b[i*4+1]) << 16;
406 s[i] |= u32(b[i*4+2]) << 8;
407 s[i] |= u32(b[i*4+3]) << 0;
437 s[i] |= u32(b[i * 4 + 0]) << 24;
438 s[i] |= u32(b[i * 4 + 1]) << 16;
439 s[i] |= u32(b[i * 4 + 2]) << 8;
440 s[i] |= u32(b[i * 4 + 3]) << 0;
408441 }
409442}
410443
......@@ -413,7 +446,7 @@ test "binary math operator in partially inlined function" {
413446 var b: [16]u8 = undefined;
414447
415448 for (b) |*r, i|
416 *r = u8(i + 1);
449 r.* = u8(i + 1);
417450
418451 copyWithPartialInline(s[0..], b[0..]);
419452 assert(s[0] == 0x1020304);
......@@ -422,7 +455,6 @@ test "binary math operator in partially inlined function" {
422455 assert(s[3] == 0xd0e0f10);
423456}
424457
425
426458test "comptime function with the same args is memoized" {
427459 comptime {
428460 assert(MakeType(i32) == MakeType(i32));
......@@ -447,12 +479,12 @@ test "comptime function with mutable pointer is not memoized" {
447479}
448480
449481fn increment(value: &i32) void {
450 *value += 1;
482 value.* += 1;
451483}
452484
453485fn generateTable(comptime T: type) [1010]T {
454 var res : [1010]T = undefined;
455 var i : usize = 0;
486 var res: [1010]T = undefined;
487 var i: usize = 0;
456488 while (i < 1010) : (i += 1) {
457489 res[i] = T(i);
458490 }
......@@ -496,9 +528,10 @@ const SingleFieldStruct = struct {
496528 }
497529};
498530test "const ptr to comptime mutable data is not memoized" {
499
500531 comptime {
501 var foo = SingleFieldStruct {.x = 1};
532 var foo = SingleFieldStruct {
533 .x = 1,
534 };
502535 assert(foo.read_x() == 1);
503536 foo.x = 2;
504537 assert(foo.read_x() == 2);
test/cases/fn.zig+26-18
......@@ -7,7 +7,6 @@ fn testParamsAdd(a: i32, b: i32) i32 {
77 return a + b;
88}
99
10
1110test "local variables" {
1211 testLocVars(2);
1312}
......@@ -16,7 +15,6 @@ fn testLocVars(b: i32) void {
1615 if (a + b != 3) unreachable;
1716}
1817
19
2018test "void parameters" {
2119 voidFun(1, void{}, 2, {});
2220}
......@@ -27,9 +25,8 @@ fn voidFun(a: i32, b: void, c: i32, d: void) void {
2725 return vv;
2826}
2927
30
3128test "mutable local variables" {
32 var zero : i32 = 0;
29 var zero: i32 = 0;
3330 assert(zero == 0);
3431
3532 var i = i32(0);
......@@ -41,7 +38,7 @@ test "mutable local variables" {
4138
4239test "separate block scopes" {
4340 {
44 const no_conflict : i32 = 5;
41 const no_conflict: i32 = 5;
4542 assert(no_conflict == 5);
4643 }
4744
......@@ -56,8 +53,7 @@ test "call function with empty string" {
5653 acceptsString("");
5754}
5855
59fn acceptsString(foo: []u8) void { }
60
56fn acceptsString(foo: []u8) void {}
6157
6258fn @"weird function name"() i32 {
6359 return 1234;
......@@ -70,31 +66,43 @@ test "implicit cast function unreachable return" {
7066 wantsFnWithVoid(fnWithUnreachable);
7167}
7268
73fn wantsFnWithVoid(f: fn() void) void { }
69fn wantsFnWithVoid(f: fn() void) void {}
7470
7571fn fnWithUnreachable() noreturn {
7672 unreachable;
7773}
7874
79
8075test "function pointers" {
81 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
76 const fns = []@typeOf(fn1) {
77 fn1,
78 fn2,
79 fn3,
80 fn4,
81 };
8282 for (fns) |f, i| {
8383 assert(f() == u32(i) + 5);
8484 }
8585}
86fn fn1() u32 {return 5;}
87fn fn2() u32 {return 6;}
88fn fn3() u32 {return 7;}
89fn fn4() u32 {return 8;}
90
86fn fn1() u32 {
87 return 5;
88}
89fn fn2() u32 {
90 return 6;
91}
92fn fn3() u32 {
93 return 7;
94}
95fn fn4() u32 {
96 return 8;
97}
9198
9299test "inline function call" {
93100 assert(@inlineCall(add, 3, 9) == 12);
94101}
95102
96fn add(a: i32, b: i32) i32 { return a + b; }
97
103fn add(a: i32, b: i32) i32 {
104 return a + b;
105}
98106
99107test "number literal as an argument" {
100108 numberLiteralArg(3);
......@@ -110,4 +118,4 @@ test "assign inline fn to const variable" {
110118 a();
111119}
112120
113inline fn inlineFn() void { }
121inline fn inlineFn() void {}
test/cases/for.zig+37-7
......@@ -3,8 +3,14 @@ const assert = std.debug.assert;
33const mem = std.mem;
44
55test "continue in for loop" {
6 const array = []i32 {1, 2, 3, 4, 5};
7 var sum : i32 = 0;
6 const array = []i32 {
7 1,
8 2,
9 3,
10 4,
11 5,
12 };
13 var sum: i32 = 0;
814 for (array) |x| {
915 sum += x;
1016 if (x < 3) {
......@@ -24,17 +30,39 @@ test "for loop with pointer elem var" {
2430}
2531fn mangleString(s: []u8) void {
2632 for (s) |*c| {
27 *c += 1;
33 c.* += 1;
2834 }
2935}
3036
3137test "basic for loop" {
32 const expected_result = []u8{9, 8, 7, 6, 0, 1, 2, 3, 9, 8, 7, 6, 0, 1, 2, 3 };
38 const expected_result = []u8 {
39 9,
40 8,
41 7,
42 6,
43 0,
44 1,
45 2,
46 3,
47 9,
48 8,
49 7,
50 6,
51 0,
52 1,
53 2,
54 3,
55 };
3356
3457 var buffer: [expected_result.len]u8 = undefined;
3558 var buf_index: usize = 0;
3659
37 const array = []u8 {9, 8, 7, 6};
60 const array = []u8 {
61 9,
62 8,
63 7,
64 6,
65 };
3866 for (array) |item| {
3967 buffer[buf_index] = item;
4068 buf_index += 1;
......@@ -65,7 +93,8 @@ fn testBreakOuter() void {
6593 var array = "aoeu";
6694 var count: usize = 0;
6795 outer: for (array) |_| {
68 for (array) |_2| { // TODO shouldn't get error for redeclaring "_"
96 // TODO shouldn't get error for redeclaring "_"
97 for (array) |_2| {
6998 count += 1;
7099 break :outer;
71100 }
......@@ -82,7 +111,8 @@ fn testContinueOuter() void {
82111 var array = "aoeu";
83112 var counter: usize = 0;
84113 outer: for (array) |_| {
85 for (array) |_2| { // TODO shouldn't get error for redeclaring "_"
114 // TODO shouldn't get error for redeclaring "_"
115 for (array) |_2| {
86116 counter += 1;
87117 continue :outer;
88118 }
test/cases/generics.zig+28-14
......@@ -37,7 +37,6 @@ test "fn with comptime args" {
3737 assert(sameButWithFloats(0.43, 0.49) == 0.49);
3838}
3939
40
4140test "var params" {
4241 assert(max_i32(12, 34) == 34);
4342 assert(max_f64(1.2, 3.4) == 3.4);
......@@ -60,7 +59,6 @@ fn max_f64(a: f64, b: f64) f64 {
6059 return max_var(a, b);
6160}
6261
63
6462pub fn List(comptime T: type) type {
6563 return SmallList(T, 8);
6664}
......@@ -82,10 +80,15 @@ test "function with return type type" {
8280 assert(list2.prealloc_items.len == 8);
8381}
8482
85
8683test "generic struct" {
87 var a1 = GenNode(i32) {.value = 13, .next = null,};
88 var b1 = GenNode(bool) {.value = true, .next = null,};
84 var a1 = GenNode(i32) {
85 .value = 13,
86 .next = null,
87 };
88 var b1 = GenNode(bool) {
89 .value = true,
90 .next = null,
91 };
8992 assert(a1.value == 13);
9093 assert(a1.value == a1.getVal());
9194 assert(b1.getVal());
......@@ -94,7 +97,9 @@ fn GenNode(comptime T: type) type {
9497 return struct {
9598 value: T,
9699 next: ?&GenNode(T),
97 fn getVal(n: &const GenNode(T)) T { return n.value; }
100 fn getVal(n: &const GenNode(T)) T {
101 return n.value;
102 }
98103 };
99104}
100105
......@@ -107,7 +112,6 @@ fn GenericDataThing(comptime count: isize) type {
107112 };
108113}
109114
110
111115test "use generic param in generic param" {
112116 assert(aGenericFn(i32, 3, 4) == 7);
113117}
......@@ -115,21 +119,31 @@ fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
115119 return a + b;
116120}
117121
118
119122test "generic fn with implicit cast" {
120123 assert(getFirstByte(u8, []u8 {13}) == 13);
121 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
124 assert(getFirstByte(u16, []u16 {
125 0,
126 13,
127 }) == 0);
128}
129fn getByte(ptr: ?&const u8) u8 {
130 return (??ptr).*;
122131}
123fn getByte(ptr: ?&const u8) u8 {return *??ptr;}
124132fn getFirstByte(comptime T: type, mem: []const T) u8 {
125133 return getByte(@ptrCast(&const u8, &mem[0]));
126134}
127135
136const foos = []fn(var) bool {
137 foo1,
138 foo2,
139};
128140
129const foos = []fn(var) bool { foo1, foo2 };
130
131fn foo1(arg: var) bool { return arg; }
132fn foo2(arg: var) bool { return !arg; }
141fn foo1(arg: var) bool {
142 return arg;
143}
144fn foo2(arg: var) bool {
145 return !arg;
146}
133147
134148test "array of generic fns" {
135149 assert(foos[0](true));
test/cases/if.zig-1
......@@ -23,7 +23,6 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {
2323 }
2424}
2525
26
2726test "else if expression" {
2827 assert(elseIfExpressionF(1) == 1);
2928}
test/cases/import/a_namespace.zig+3-1
......@@ -1 +1,3 @@
1pub fn foo() i32 { return 1234; }
1pub fn foo() i32 {
2 return 1234;
3}
test/cases/ir_block_deps.zig+3-1
......@@ -11,7 +11,9 @@ fn foo(id: u64) !i32 {
1111 };
1212}
1313
14fn getErrInt() error!i32 { return 0; }
14fn getErrInt() error!i32 {
15 return 0;
16}
1517
1618test "ir block deps" {
1719 assert((foo(1) catch unreachable) == 0);
test/cases/math.zig+40-52
......@@ -28,25 +28,12 @@ fn testDivision() void {
2828 assert(divTrunc(f32, -5.0, 3.0) == -1.0);
2929
3030 comptime {
31 assert(
32 1194735857077236777412821811143690633098347576 %
33 508740759824825164163191790951174292733114988 ==
34 177254337427586449086438229241342047632117600);
35 assert(@rem(-1194735857077236777412821811143690633098347576,
36 508740759824825164163191790951174292733114988) ==
37 -177254337427586449086438229241342047632117600);
38 assert(1194735857077236777412821811143690633098347576 /
39 508740759824825164163191790951174292733114988 ==
40 2);
41 assert(@divTrunc(-1194735857077236777412821811143690633098347576,
42 508740759824825164163191790951174292733114988) ==
43 -2);
44 assert(@divTrunc(1194735857077236777412821811143690633098347576,
45 -508740759824825164163191790951174292733114988) ==
46 -2);
47 assert(@divTrunc(-1194735857077236777412821811143690633098347576,
48 -508740759824825164163191790951174292733114988) ==
49 2);
31 assert(1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600);
32 assert(@rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600);
33 assert(1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2);
34 assert(@divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2);
35 assert(@divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2);
36 assert(@divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2);
5037 assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
5138 }
5239}
......@@ -114,18 +101,28 @@ fn ctz(x: var) usize {
114101
115102test "assignment operators" {
116103 var i: u32 = 0;
117 i += 5; assert(i == 5);
118 i -= 2; assert(i == 3);
119 i *= 20; assert(i == 60);
120 i /= 3; assert(i == 20);
121 i %= 11; assert(i == 9);
122 i <<= 1; assert(i == 18);
123 i >>= 2; assert(i == 4);
104 i += 5;
105 assert(i == 5);
106 i -= 2;
107 assert(i == 3);
108 i *= 20;
109 assert(i == 60);
110 i /= 3;
111 assert(i == 20);
112 i %= 11;
113 assert(i == 9);
114 i <<= 1;
115 assert(i == 18);
116 i >>= 2;
117 assert(i == 4);
124118 i = 6;
125 i &= 5; assert(i == 4);
126 i ^= 6; assert(i == 2);
119 i &= 5;
120 assert(i == 4);
121 i ^= 6;
122 assert(i == 2);
127123 i = 6;
128 i |= 3; assert(i == 7);
124 i |= 3;
125 assert(i == 7);
129126}
130127
131128test "three expr in a row" {
......@@ -138,7 +135,7 @@ fn testThreeExprInARow(f: bool, t: bool) void {
138135 assertFalse(1 | 2 | 4 != 7);
139136 assertFalse(3 ^ 6 ^ 8 != 13);
140137 assertFalse(7 & 14 & 28 != 4);
141 assertFalse(9 << 1 << 2 != 9 << 3);
138 assertFalse(9 << 1 << 2 != 9 << 3);
142139 assertFalse(90 >> 1 >> 2 != 90 >> 3);
143140 assertFalse(100 - 1 + 1000 != 1099);
144141 assertFalse(5 * 4 / 2 % 3 != 1);
......@@ -150,7 +147,6 @@ fn assertFalse(b: bool) void {
150147 assert(!b);
151148}
152149
153
154150test "const number literal" {
155151 const one = 1;
156152 const eleven = ten + one;
......@@ -159,8 +155,6 @@ test "const number literal" {
159155}
160156const ten = 10;
161157
162
163
164158test "unsigned wrapping" {
165159 testUnsignedWrappingEval(@maxValue(u32));
166160 comptime testUnsignedWrappingEval(@maxValue(u32));
......@@ -214,8 +208,12 @@ const DivResult = struct {
214208};
215209
216210test "binary not" {
217 assert(comptime x: {break :x ~u16(0b1010101010101010) == 0b0101010101010101;});
218 assert(comptime x: {break :x ~u64(2147483647) == 18446744071562067968;});
211 assert(comptime x: {
212 break :x ~u16(0b1010101010101010) == 0b0101010101010101;
213 });
214 assert(comptime x: {
215 break :x ~u64(2147483647) == 18446744071562067968;
216 });
219217 testBinaryNot(0b1010101010101010);
220218}
221219
......@@ -319,27 +317,15 @@ fn testShrExact(x: u8) void {
319317
320318test "big number addition" {
321319 comptime {
322 assert(
323 35361831660712422535336160538497375248 +
324 101752735581729509668353361206450473702 ==
325 137114567242441932203689521744947848950);
326 assert(
327 594491908217841670578297176641415611445982232488944558774612 +
328 390603545391089362063884922208143568023166603618446395589768 ==
329 985095453608931032642182098849559179469148836107390954364380);
320 assert(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
321 assert(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
330322 }
331323}
332324
333325test "big number multiplication" {
334326 comptime {
335 assert(
336 45960427431263824329884196484953148229 *
337 128339149605334697009938835852565949723 ==
338 5898522172026096622534201617172456926982464453350084962781392314016180490567);
339 assert(
340 594491908217841670578297176641415611445982232488944558774612 *
341 390603545391089362063884922208143568023166603618446395589768 ==
342 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
327 assert(45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567);
328 assert(594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
343329 }
344330}
345331
......@@ -405,7 +391,9 @@ test "f128" {
405391 comptime test_f128();
406392}
407393
408fn make_f128(x: f128) f128 { return x; }
394fn make_f128(x: f128) f128 {
395 return x;
396}
409397
410398fn test_f128() void {
411399 assert(@sizeOf(f128) == 16);
test/cases/misc.zig+138-87
......@@ -4,6 +4,7 @@ const cstr = @import("std").cstr;
44const builtin = @import("builtin");
55
66// normal comment
7
78/// this is a documentation comment
89/// doc comment line 2
910fn emptyFunctionWithComments() void {}
......@@ -16,8 +17,7 @@ comptime {
1617 @export("disabledExternFn", disabledExternFn, builtin.GlobalLinkage.Internal);
1718}
1819
19extern fn disabledExternFn() void {
20}
20extern fn disabledExternFn() void {}
2121
2222test "call disabled extern fn" {
2323 disabledExternFn();
......@@ -110,17 +110,29 @@ fn testShortCircuit(f: bool, t: bool) void {
110110 var hit_3 = f;
111111 var hit_4 = f;
112112
113 if (t or x: {assert(f); break :x f;}) {
113 if (t or x: {
114 assert(f);
115 break :x f;
116 }) {
114117 hit_1 = t;
115118 }
116 if (f or x: { hit_2 = t; break :x f; }) {
119 if (f or x: {
120 hit_2 = t;
121 break :x f;
122 }) {
117123 assert(f);
118124 }
119125
120 if (t and x: { hit_3 = t; break :x f; }) {
126 if (t and x: {
127 hit_3 = t;
128 break :x f;
129 }) {
121130 assert(f);
122131 }
123 if (f and x: {assert(f); break :x f;}) {
132 if (f and x: {
133 assert(f);
134 break :x f;
135 }) {
124136 assert(f);
125137 } else {
126138 hit_4 = t;
......@@ -146,8 +158,8 @@ test "return string from function" {
146158 assert(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
147159}
148160
149const g1 : i32 = 1233 + 1;
150var g2 : i32 = 0;
161const g1: i32 = 1233 + 1;
162var g2: i32 = 0;
151163
152164test "global variables" {
153165 assert(g2 == 0);
......@@ -155,10 +167,9 @@ test "global variables" {
155167 assert(g2 == 1234);
156168}
157169
158
159170test "memcpy and memset intrinsics" {
160 var foo : [20]u8 = undefined;
161 var bar : [20]u8 = undefined;
171 var foo: [20]u8 = undefined;
172 var bar: [20]u8 = undefined;
162173
163174 @memset(&foo[0], 'A', foo.len);
164175 @memcpy(&bar[0], &foo[0], bar.len);
......@@ -167,12 +178,14 @@ test "memcpy and memset intrinsics" {
167178}
168179
169180test "builtin static eval" {
170 const x : i32 = comptime x: {break :x 1 + 2 + 3;};
181 const x: i32 = comptime x: {
182 break :x 1 + 2 + 3;
183 };
171184 assert(x == comptime 6);
172185}
173186
174187test "slicing" {
175 var array : [20]i32 = undefined;
188 var array: [20]i32 = undefined;
176189
177190 array[5] = 1234;
178191
......@@ -187,15 +200,15 @@ test "slicing" {
187200 if (slice_rest.len != 10) unreachable;
188201}
189202
190
191203test "constant equal function pointers" {
192204 const alias = emptyFn;
193 assert(comptime x: {break :x emptyFn == alias;});
205 assert(comptime x: {
206 break :x emptyFn == alias;
207 });
194208}
195209
196210fn emptyFn() void {}
197211
198
199212test "hex escape" {
200213 assert(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
201214}
......@@ -219,7 +232,7 @@ test "string escapes" {
219232}
220233
221234test "multiline string" {
222 const s1 =
235 const s1 =
223236 \\one
224237 \\two)
225238 \\three
......@@ -229,7 +242,7 @@ test "multiline string" {
229242}
230243
231244test "multiline C string" {
232 const s1 =
245 const s1 =
233246 c\\one
234247 c\\two)
235248 c\\three
......@@ -238,18 +251,16 @@ test "multiline C string" {
238251 assert(cstr.cmp(s1, s2) == 0);
239252}
240253
241
242254test "type equality" {
243255 assert(&const u8 != &u8);
244256}
245257
246
247258const global_a: i32 = 1234;
248259const global_b: &const i32 = &global_a;
249260const global_c: &const f32 = @ptrCast(&const f32, global_b);
250261test "compile time global reinterpret" {
251262 const d = @ptrCast(&const i32, global_c);
252 assert(*d == 1234);
263 assert(d.* == 1234);
253264}
254265
255266test "explicit cast maybe pointers" {
......@@ -261,12 +272,11 @@ test "generic malloc free" {
261272 const a = memAlloc(u8, 10) catch unreachable;
262273 memFree(u8, a);
263274}
264var some_mem : [100]u8 = undefined;
275var some_mem: [100]u8 = undefined;
265276fn memAlloc(comptime T: type, n: usize) error![]T {
266277 return @ptrCast(&T, &some_mem[0])[0..n];
267278}
268fn memFree(comptime T: type, memory: []T) void { }
269
279fn memFree(comptime T: type, memory: []T) void {}
270280
271281test "cast undefined" {
272282 const array: [100]u8 = undefined;
......@@ -275,32 +285,35 @@ test "cast undefined" {
275285}
276286fn testCastUndefined(x: []const u8) void {}
277287
278
279288test "cast small unsigned to larger signed" {
280289 assert(castSmallUnsignedToLargerSigned1(200) == i16(200));
281290 assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999));
282291}
283fn castSmallUnsignedToLargerSigned1(x: u8) i16 { return x; }
284fn castSmallUnsignedToLargerSigned2(x: u16) i64 { return x; }
285
292fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
293 return x;
294}
295fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
296 return x;
297}
286298
287299test "implicit cast after unreachable" {
288300 assert(outer() == 1234);
289301}
290fn inner() i32 { return 1234; }
302fn inner() i32 {
303 return 1234;
304}
291305fn outer() i64 {
292306 return inner();
293307}
294308
295
296309test "pointer dereferencing" {
297310 var x = i32(3);
298311 const y = &x;
299312
300 *y += 1;
313 y.* += 1;
301314
302315 assert(x == 4);
303 assert(*y == 4);
316 assert(y.* == 4);
304317}
305318
306319test "call result of if else expression" {
......@@ -310,9 +323,12 @@ test "call result of if else expression" {
310323fn f2(x: bool) []const u8 {
311324 return (if (x) fA else fB)();
312325}
313fn fA() []const u8 { return "a"; }
314fn fB() []const u8 { return "b"; }
315
326fn fA() []const u8 {
327 return "a";
328}
329fn fB() []const u8 {
330 return "b";
331}
316332
317333test "const expression eval handling of variables" {
318334 var x = true;
......@@ -321,8 +337,6 @@ test "const expression eval handling of variables" {
321337 }
322338}
323339
324
325
326340test "constant enum initialization with differing sizes" {
327341 test3_1(test3_foo);
328342 test3_2(test3_bar);
......@@ -336,10 +350,17 @@ const Test3Point = struct {
336350 x: i32,
337351 y: i32,
338352};
339const test3_foo = Test3Foo { .Three = Test3Point {.x = 3, .y = 4}};
340const test3_bar = Test3Foo { .Two = 13};
353const test3_foo = Test3Foo {
354 .Three = Test3Point {
355 .x = 3,
356 .y = 4,
357 },
358};
359const test3_bar = Test3Foo {
360 .Two = 13,
361};
341362fn test3_1(f: &const Test3Foo) void {
342 switch (*f) {
363 switch (f.*) {
343364 Test3Foo.Three => |pt| {
344365 assert(pt.x == 3);
345366 assert(pt.y == 4);
......@@ -348,7 +369,7 @@ fn test3_1(f: &const Test3Foo) void {
348369 }
349370}
350371fn test3_2(f: &const Test3Foo) void {
351 switch (*f) {
372 switch (f.*) {
352373 Test3Foo.Two => |x| {
353374 assert(x == 13);
354375 },
......@@ -356,23 +377,19 @@ fn test3_2(f: &const Test3Foo) void {
356377 }
357378}
358379
359
360380test "character literals" {
361381 assert('\'' == single_quote);
362382}
363383const single_quote = '\'';
364384
365
366
367385test "take address of parameter" {
368386 testTakeAddressOfParameter(12.34);
369387}
370388fn testTakeAddressOfParameter(f: f32) void {
371389 const f_ptr = &f;
372 assert(*f_ptr == 12.34);
390 assert(f_ptr.* == 12.34);
373391}
374392
375
376393test "pointer comparison" {
377394 const a = ([]const u8)("a");
378395 const b = &a;
......@@ -382,23 +399,30 @@ fn ptrEql(a: &const []const u8, b: &const []const u8) bool {
382399 return a == b;
383400}
384401
385
386402test "C string concatenation" {
387403 const a = c"OK" ++ c" IT " ++ c"WORKED";
388404 const b = c"OK IT WORKED";
389405
390406 const len = cstr.len(b);
391407 const len_with_null = len + 1;
392 {var i: u32 = 0; while (i < len_with_null) : (i += 1) {
393 assert(a[i] == b[i]);
394 }}
408 {
409 var i: u32 = 0;
410 while (i < len_with_null) : (i += 1) {
411 assert(a[i] == b[i]);
412 }
413 }
395414 assert(a[len] == 0);
396415 assert(b[len] == 0);
397416}
398417
399418test "cast slice to u8 slice" {
400419 assert(@sizeOf(i32) == 4);
401 var big_thing_array = []i32{1, 2, 3, 4};
420 var big_thing_array = []i32 {
421 1,
422 2,
423 3,
424 4,
425 };
402426 const big_thing_slice: []i32 = big_thing_array[0..];
403427 const bytes = ([]u8)(big_thing_slice);
404428 assert(bytes.len == 4 * 4);
......@@ -421,25 +445,22 @@ test "pointer to void return type" {
421445}
422446fn testPointerToVoidReturnType() error!void {
423447 const a = testPointerToVoidReturnType2();
424 return *a;
448 return a.*;
425449}
426450const test_pointer_to_void_return_type_x = void{};
427451fn testPointerToVoidReturnType2() &const void {
428452 return &test_pointer_to_void_return_type_x;
429453}
430454
431
432455test "non const ptr to aliased type" {
433456 const int = i32;
434457 assert(?&int == ?&i32);
435458}
436459
437
438
439460test "array 2D const double ptr" {
440461 const rect_2d_vertexes = [][1]f32 {
441 []f32{1.0},
442 []f32{2.0},
462 []f32 {1.0},
463 []f32 {2.0},
443464 };
444465 testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
445466}
......@@ -450,10 +471,21 @@ fn testArray2DConstDoublePtr(ptr: &const f32) void {
450471}
451472
452473const Tid = builtin.TypeId;
453const AStruct = struct { x: i32, };
454const AnEnum = enum { One, Two, };
455const AUnionEnum = union(enum) { One: i32, Two: void, };
456const AUnion = union { One: void, Two: void };
474const AStruct = struct {
475 x: i32,
476};
477const AnEnum = enum {
478 One,
479 Two,
480};
481const AUnionEnum = union(enum) {
482 One: i32,
483 Two: void,
484};
485const AUnion = union {
486 One: void,
487 Two: void,
488};
457489
458490test "@typeId" {
459491 comptime {
......@@ -481,9 +513,11 @@ test "@typeId" {
481513 assert(@typeId(@typeOf(AUnionEnum.One)) == Tid.Enum);
482514 assert(@typeId(AUnionEnum) == Tid.Union);
483515 assert(@typeId(AUnion) == Tid.Union);
484 assert(@typeId(fn()void) == Tid.Fn);
516 assert(@typeId(fn() void) == Tid.Fn);
485517 assert(@typeId(@typeOf(builtin)) == Tid.Namespace);
486 assert(@typeId(@typeOf(x: {break :x this;})) == Tid.Block);
518 assert(@typeId(@typeOf(x: {
519 break :x this;
520 })) == Tid.Block);
487521 // TODO bound fn
488522 // TODO arg tuple
489523 // TODO opaque
......@@ -499,8 +533,7 @@ test "@canImplicitCast" {
499533}
500534
501535test "@typeName" {
502 const Struct = struct {
503 };
536 const Struct = struct {};
504537 const Union = union {
505538 unused: u8,
506539 };
......@@ -525,14 +558,19 @@ fn TypeFromFn(comptime T: type) type {
525558test "volatile load and store" {
526559 var number: i32 = 1234;
527560 const ptr = (&volatile i32)(&number);
528 *ptr += 1;
529 assert(*ptr == 1235);
561 ptr.* += 1;
562 assert(ptr.* == 1235);
530563}
531564
532565test "slice string literal has type []const u8" {
533566 comptime {
534567 assert(@typeOf("aoeu"[0..]) == []const u8);
535 const array = []i32{1, 2, 3, 4};
568 const array = []i32 {
569 1,
570 2,
571 3,
572 4,
573 };
536574 assert(@typeOf(array[0..]) == []const i32);
537575 }
538576}
......@@ -544,12 +582,15 @@ const GDTEntry = struct {
544582 field: i32,
545583};
546584var gdt = []GDTEntry {
547 GDTEntry {.field = 1},
548 GDTEntry {.field = 2},
585 GDTEntry {
586 .field = 1,
587 },
588 GDTEntry {
589 .field = 2,
590 },
549591};
550592var global_ptr = &gdt[0];
551593
552
553594// can't really run this test but we can make sure it has no compile error
554595// and generates code
555596const vram = @intToPtr(&volatile u8, 0x20000000)[0..0x8000];
......@@ -584,7 +625,7 @@ test "comptime if inside runtime while which unconditionally breaks" {
584625}
585626fn testComptimeIfInsideRuntimeWhileWhichUnconditionallyBreaks(cond: bool) void {
586627 while (cond) {
587 if (false) { }
628 if (false) {}
588629 break;
589630 }
590631}
......@@ -607,7 +648,9 @@ fn testStructInFn() void {
607648 kind: BlockKind,
608649 };
609650
610 var block = Block { .kind = 1234 };
651 var block = Block {
652 .kind = 1234,
653 };
611654
612655 block.kind += 1;
613656
......@@ -617,7 +660,9 @@ fn testStructInFn() void {
617660fn fnThatClosesOverLocalConst() type {
618661 const c = 1;
619662 return struct {
620 fn g() i32 { return c; }
663 fn g() i32 {
664 return c;
665 }
621666 };
622667}
623668
......@@ -635,22 +680,29 @@ fn thisIsAColdFn() void {
635680 @setCold(true);
636681}
637682
638
639const PackedStruct = packed struct { a: u8, b: u8, };
640const PackedUnion = packed union { a: u8, b: u32, };
641const PackedEnum = packed enum { A, B, };
683const PackedStruct = packed struct {
684 a: u8,
685 b: u8,
686};
687const PackedUnion = packed union {
688 a: u8,
689 b: u32,
690};
691const PackedEnum = packed enum {
692 A,
693 B,
694};
642695
643696test "packed struct, enum, union parameters in extern function" {
644 testPackedStuff(
645 PackedStruct{.a = 1, .b = 2},
646 PackedUnion{.a = 1},
647 PackedEnum.A,
648 );
649}
650
651export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {
697 testPackedStuff(PackedStruct {
698 .a = 1,
699 .b = 2,
700 }, PackedUnion {
701 .a = 1,
702 }, PackedEnum.A);
652703}
653704
705export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {}
654706
655707test "slicing zero length array" {
656708 const s1 = ""[0..];
......@@ -661,7 +713,6 @@ test "slicing zero length array" {
661713 assert(mem.eql(u32, s2, []u32{}));
662714}
663715
664
665716const addr1 = @ptrCast(&const u8, emptyFn);
666717test "comptime cast fn to ptr" {
667718 const addr2 = @ptrCast(&const u8, emptyFn);
test/cases/namespace_depends_on_compile_var/index.zig+1-1
......@@ -8,7 +8,7 @@ test "namespace depends on compile var" {
88 assert(!some_namespace.a_bool);
99 }
1010}
11const some_namespace = switch(builtin.os) {
11const some_namespace = switch (builtin.os) {
1212 builtin.Os.linux => @import("a.zig"),
1313 else => @import("b.zig"),
1414};
test/cases/new_stack_call.zig+1-1
......@@ -19,7 +19,7 @@ fn targetFunction(x: i32) usize {
1919
2020 var local_variable: i32 = 42;
2121 const ptr = &local_variable;
22 *ptr += 1;
22 ptr.* += 1;
2323
2424 assert(local_variable == 43);
2525 return @ptrToInt(ptr);
test/cases/null.zig+12-15
......@@ -1,7 +1,7 @@
11const assert = @import("std").debug.assert;
22
33test "nullable type" {
4 const x : ?bool = true;
4 const x: ?bool = true;
55
66 if (x) |y| {
77 if (y) {
......@@ -13,13 +13,13 @@ test "nullable type" {
1313 unreachable;
1414 }
1515
16 const next_x : ?i32 = null;
16 const next_x: ?i32 = null;
1717
1818 const z = next_x ?? 1234;
1919
2020 assert(z == 1234);
2121
22 const final_x : ?i32 = 13;
22 const final_x: ?i32 = 13;
2323
2424 const num = final_x ?? unreachable;
2525
......@@ -30,19 +30,17 @@ test "test maybe object and get a pointer to the inner value" {
3030 var maybe_bool: ?bool = true;
3131
3232 if (maybe_bool) |*b| {
33 *b = false;
33 b.* = false;
3434 }
3535
3636 assert(??maybe_bool == false);
3737}
3838
39
4039test "rhs maybe unwrap return" {
4140 const x: ?bool = true;
4241 const y = x ?? return;
4342}
4443
45
4644test "maybe return" {
4745 maybeReturnImpl();
4846 comptime maybeReturnImpl();
......@@ -50,8 +48,7 @@ test "maybe return" {
5048
5149fn maybeReturnImpl() void {
5250 assert(??foo(1235));
53 if (foo(null) != null)
54 unreachable;
51 if (foo(null) != null) unreachable;
5552 assert(!??foo(1234));
5653}
5754
......@@ -60,12 +57,16 @@ fn foo(x: ?i32) ?bool {
6057 return value > 1234;
6158}
6259
63
6460test "if var maybe pointer" {
65 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
61 assert(shouldBeAPlus1(Particle {
62 .a = 14,
63 .b = 1,
64 .c = 1,
65 .d = 1,
66 }) == 15);
6667}
6768fn shouldBeAPlus1(p: &const Particle) u64 {
68 var maybe_particle: ?Particle = *p;
69 var maybe_particle: ?Particle = p.*;
6970 if (maybe_particle) |*particle| {
7071 particle.a += 1;
7172 }
......@@ -81,7 +82,6 @@ const Particle = struct {
8182 d: u64,
8283};
8384
84
8585test "null literal outside function" {
8686 const is_null = here_is_a_null_literal.context == null;
8787 assert(is_null);
......@@ -96,7 +96,6 @@ const here_is_a_null_literal = SillyStruct {
9696 .context = null,
9797};
9898
99
10099test "test null runtime" {
101100 testTestNullRuntime(null);
102101}
......@@ -123,8 +122,6 @@ fn bar(x: ?void) ?void {
123122 }
124123}
125124
126
127
128125const StructWithNullable = struct {
129126 field: ?i32,
130127};
test/cases/pointers.zig created+14
......@@ -0,0 +1,14 @@
1const std = @import("std");
2const assert = std.debug.assert;
3
4test "dereference pointer" {
5 comptime testDerefPtr();
6 testDerefPtr();
7}
8
9fn testDerefPtr() void {
10 var x: i32 = 1234;
11 var y = &x;
12 y.* += 1;
13 assert(x == 1235);
14}
test/cases/ref_var_in_if_after_if_2nd_switch_prong.zig+1-1
......@@ -23,7 +23,7 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
2323 if (c) {
2424 const output_path = b;
2525
26 if (c2) { }
26 if (c2) {}
2727
2828 a(output_path);
2929 }
test/cases/reflection.zig+3-2
......@@ -23,7 +23,9 @@ test "reflection: function return type, var args, and param types" {
2323 }
2424}
2525
26fn dummy(a: bool, b: i32, c: f32) i32 { return 1234; }
26fn dummy(a: bool, b: i32, c: f32) i32 {
27 return 1234;
28}
2729fn dummy_varargs(args: ...) void {}
2830
2931test "reflection: struct member types and names" {
......@@ -54,7 +56,6 @@ test "reflection: enum member types and names" {
5456 assert(mem.eql(u8, @memberName(Bar, 2), "Three"));
5557 assert(mem.eql(u8, @memberName(Bar, 3), "Four"));
5658 }
57
5859}
5960
6061test "reflection: @field" {
test/cases/slice.zig+6-2
......@@ -18,7 +18,11 @@ test "slice child property" {
1818}
1919
2020test "runtime safety lets us slice from len..len" {
21 var an_array = []u8{1, 2, 3};
21 var an_array = []u8 {
22 1,
23 2,
24 3,
25 };
2226 assert(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
2327}
2428
......@@ -27,7 +31,7 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
2731}
2832
2933test "implicitly cast array of size 0 to slice" {
30 var msg = []u8 {};
34 var msg = []u8{};
3135 assertLenIsZero(msg);
3236}
3337
test/cases/struct.zig+48-35
......@@ -2,9 +2,11 @@ const assert = @import("std").debug.assert;
22const builtin = @import("builtin");
33
44const StructWithNoFields = struct {
5 fn add(a: i32, b: i32) i32 { return a + b; }
5 fn add(a: i32, b: i32) i32 {
6 return a + b;
7 }
68};
7const empty_global_instance = StructWithNoFields {};
9const empty_global_instance = StructWithNoFields{};
810
911test "call struct static method" {
1012 const result = StructWithNoFields.add(3, 4);
......@@ -34,12 +36,11 @@ test "void struct fields" {
3436 assert(@sizeOf(VoidStructFieldsFoo) == 4);
3537}
3638const VoidStructFieldsFoo = struct {
37 a : void,
38 b : i32,
39 c : void,
39 a: void,
40 b: i32,
41 c: void,
4042};
4143
42
4344test "structs" {
4445 var foo: StructFoo = undefined;
4546 @memset(@ptrCast(&u8, &foo), 0, @sizeOf(StructFoo));
......@@ -50,9 +51,9 @@ test "structs" {
5051 assert(foo.c == 100);
5152}
5253const StructFoo = struct {
53 a : i32,
54 b : bool,
55 c : f32,
54 a: i32,
55 b: bool,
56 c: f32,
5657};
5758fn testFoo(foo: &const StructFoo) void {
5859 assert(foo.b);
......@@ -61,7 +62,6 @@ fn testMutation(foo: &StructFoo) void {
6162 foo.c = 100;
6263}
6364
64
6565const Node = struct {
6666 val: Val,
6767 next: &Node,
......@@ -72,10 +72,10 @@ const Val = struct {
7272};
7373
7474test "struct point to self" {
75 var root : Node = undefined;
75 var root: Node = undefined;
7676 root.val.x = 1;
7777
78 var node : Node = undefined;
78 var node: Node = undefined;
7979 node.next = &root;
8080 node.val.x = 2;
8181
......@@ -85,8 +85,8 @@ test "struct point to self" {
8585}
8686
8787test "struct byval assign" {
88 var foo1 : StructFoo = undefined;
89 var foo2 : StructFoo = undefined;
88 var foo1: StructFoo = undefined;
89 var foo2: StructFoo = undefined;
9090
9191 foo1.a = 1234;
9292 foo2.a = 0;
......@@ -96,46 +96,57 @@ test "struct byval assign" {
9696}
9797
9898fn structInitializer() void {
99 const val = Val { .x = 42 };
99 const val = Val {
100 .x = 42,
101 };
100102 assert(val.x == 42);
101103}
102104
103
104105test "fn call of struct field" {
105 assert(callStructField(Foo {.ptr = aFunc,}) == 13);
106 assert(callStructField(Foo {
107 .ptr = aFunc,
108 }) == 13);
106109}
107110
108111const Foo = struct {
109112 ptr: fn() i32,
110113};
111114
112fn aFunc() i32 { return 13; }
115fn aFunc() i32 {
116 return 13;
117}
113118
114119fn callStructField(foo: &const Foo) i32 {
115120 return foo.ptr();
116121}
117122
118
119123test "store member function in variable" {
120 const instance = MemberFnTestFoo { .x = 1234, };
124 const instance = MemberFnTestFoo {
125 .x = 1234,
126 };
121127 const memberFn = MemberFnTestFoo.member;
122128 const result = memberFn(instance);
123129 assert(result == 1234);
124130}
125131const MemberFnTestFoo = struct {
126132 x: i32,
127 fn member(foo: &const MemberFnTestFoo) i32 { return foo.x; }
133 fn member(foo: &const MemberFnTestFoo) i32 {
134 return foo.x;
135 }
128136};
129137
130
131138test "call member function directly" {
132 const instance = MemberFnTestFoo { .x = 1234, };
139 const instance = MemberFnTestFoo {
140 .x = 1234,
141 };
133142 const result = MemberFnTestFoo.member(instance);
134143 assert(result == 1234);
135144}
136145
137146test "member functions" {
138 const r = MemberFnRand {.seed = 1234};
147 const r = MemberFnRand {
148 .seed = 1234,
149 };
139150 assert(r.getSeed() == 1234);
140151}
141152const MemberFnRand = struct {
......@@ -170,17 +181,16 @@ const EmptyStruct = struct {
170181 }
171182};
172183
173
174184test "return empty struct from fn" {
175185 _ = testReturnEmptyStructFromFn();
176186}
177187const EmptyStruct2 = struct {};
178188fn testReturnEmptyStructFromFn() EmptyStruct2 {
179 return EmptyStruct2 {};
189 return EmptyStruct2{};
180190}
181191
182192test "pass slice of empty struct to fn" {
183 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
193 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2 {EmptyStruct2{}}) == 1);
184194}
185195fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
186196 return slice.len;
......@@ -201,7 +211,6 @@ test "packed struct" {
201211 assert(four == 4);
202212}
203213
204
205214const BitField1 = packed struct {
206215 a: u3,
207216 b: u3,
......@@ -301,7 +310,7 @@ test "packed array 24bits" {
301310 assert(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);
302311 }
303312
304 var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
313 var bytes = []u8 {0} ** (@sizeOf(FooArray24Bits) + 1);
305314 bytes[bytes.len - 1] = 0xaa;
306315 const ptr = &([]FooArray24Bits)(bytes[0..bytes.len - 1])[0];
307316 assert(ptr.a == 0);
......@@ -351,7 +360,7 @@ test "aligned array of packed struct" {
351360 assert(@sizeOf(FooArrayOfAligned) == 2 * 2);
352361 }
353362
354 var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
363 var bytes = []u8 {0xbb} ** @sizeOf(FooArrayOfAligned);
355364 const ptr = &([]FooArrayOfAligned)(bytes[0..bytes.len])[0];
356365
357366 assert(ptr.a[0].a == 0xbb);
......@@ -360,11 +369,15 @@ test "aligned array of packed struct" {
360369 assert(ptr.a[1].b == 0xbb);
361370}
362371
363
364
365372test "runtime struct initialization of bitfield" {
366 const s1 = Nibbles { .x = x1, .y = x1 };
367 const s2 = Nibbles { .x = u4(x2), .y = u4(x2) };
373 const s1 = Nibbles {
374 .x = x1,
375 .y = x1,
376 };
377 const s2 = Nibbles {
378 .x = u4(x2),
379 .y = u4(x2),
380 };
368381
369382 assert(s1.x == x1);
370383 assert(s1.y == x1);
......@@ -394,7 +407,7 @@ test "native bit field understands endianness" {
394407 var all: u64 = 0x7765443322221111;
395408 var bytes: [8]u8 = undefined;
396409 @memcpy(&bytes[0], @ptrCast(&u8, &all), 8);
397 var bitfields = *@ptrCast(&Bitfields, &bytes[0]);
410 var bitfields = @ptrCast(&Bitfields, &bytes[0]).*;
398411
399412 assert(bitfields.f1 == 0x1111);
400413 assert(bitfields.f2 == 0x2222);
test/cases/struct_contains_null_ptr_itself.zig-1
......@@ -19,4 +19,3 @@ pub const Node = struct {
1919pub const NodeLineComment = struct {
2020 base: Node,
2121};
22
test/cases/struct_contains_slice_of_itself.zig+1-1
......@@ -6,7 +6,7 @@ const Node = struct {
66};
77
88test "struct contains slice of itself" {
9 var other_nodes = []Node{
9 var other_nodes = []Node {
1010 Node {
1111 .payload = 31,
1212 .children = []Node{},
test/cases/switch.zig+33-16
......@@ -6,7 +6,10 @@ test "switch with numbers" {
66
77fn testSwitchWithNumbers(x: u32) void {
88 const result = switch (x) {
9 1, 2, 3, 4 ... 8 => false,
9 1,
10 2,
11 3,
12 4 ... 8 => false,
1013 13 => true,
1114 else => false,
1215 };
......@@ -34,8 +37,10 @@ test "implicit comptime switch" {
3437 const result = switch (x) {
3538 3 => 10,
3639 4 => 11,
37 5, 6 => 12,
38 7, 8 => 13,
40 5,
41 6 => 12,
42 7,
43 8 => 13,
3944 else => 14,
4045 };
4146
......@@ -61,7 +66,6 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
6166 }
6267}
6368
64
6569test "switch statement" {
6670 nonConstSwitch(SwitchStatmentFoo.C);
6771}
......@@ -81,11 +85,16 @@ const SwitchStatmentFoo = enum {
8185 D,
8286};
8387
84
8588test "switch prong with variable" {
86 switchProngWithVarFn(SwitchProngWithVarEnum { .One = 13});
87 switchProngWithVarFn(SwitchProngWithVarEnum { .Two = 13.0});
88 switchProngWithVarFn(SwitchProngWithVarEnum { .Meh = {}});
89 switchProngWithVarFn(SwitchProngWithVarEnum {
90 .One = 13,
91 });
92 switchProngWithVarFn(SwitchProngWithVarEnum {
93 .Two = 13.0,
94 });
95 switchProngWithVarFn(SwitchProngWithVarEnum {
96 .Meh = {},
97 });
8998}
9099const SwitchProngWithVarEnum = union(enum) {
91100 One: i32,
......@@ -93,7 +102,7 @@ const SwitchProngWithVarEnum = union(enum) {
93102 Meh: void,
94103};
95104fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) void {
96 switch(*a) {
105 switch (a.*) {
97106 SwitchProngWithVarEnum.One => |x| {
98107 assert(x == 13);
99108 },
......@@ -112,9 +121,11 @@ test "switch on enum using pointer capture" {
112121}
113122
114123fn testSwitchEnumPtrCapture() void {
115 var value = SwitchProngWithVarEnum { .One = 1234 };
124 var value = SwitchProngWithVarEnum {
125 .One = 1234,
126 };
116127 switch (value) {
117 SwitchProngWithVarEnum.One => |*x| *x += 1,
128 SwitchProngWithVarEnum.One => |*x| x.* += 1,
118129 else => unreachable,
119130 }
120131 switch (value) {
......@@ -125,8 +136,12 @@ fn testSwitchEnumPtrCapture() void {
125136
126137test "switch with multiple expressions" {
127138 const x = switch (returnsFive()) {
128 1, 2, 3 => 1,
129 4, 5, 6 => 2,
139 1,
140 2,
141 3 => 1,
142 4,
143 5,
144 6 => 2,
130145 else => i32(3),
131146 };
132147 assert(x == 2);
......@@ -135,14 +150,15 @@ fn returnsFive() i32 {
135150 return 5;
136151}
137152
138
139153const Number = union(enum) {
140154 One: u64,
141155 Two: u8,
142156 Three: f32,
143157};
144158
145const number = Number { .Three = 1.23 };
159const number = Number {
160 .Three = 1.23,
161};
146162
147163fn returnsFalse() bool {
148164 switch (number) {
......@@ -198,7 +214,8 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
198214 return switch (x) {
199215 0 ... 100 => u8(0),
200216 101 ... 200 => 1,
201 201, 203 => 2,
217 201,
218 203 => 2,
202219 202 => 4,
203220 204 ... 255 => 3,
204221 };
test/cases/switch_prong_err_enum.zig+6-2
......@@ -14,14 +14,18 @@ const FormValue = union(enum) {
1414
1515fn doThing(form_id: u64) error!FormValue {
1616 return switch (form_id) {
17 17 => FormValue { .Address = try readOnce() },
17 17 => FormValue {
18 .Address = try readOnce(),
19 },
1820 else => error.InvalidDebugInfo,
1921 };
2022}
2123
2224test "switch prong returns error enum" {
2325 switch (doThing(17) catch unreachable) {
24 FormValue.Address => |payload| { assert(payload == 1); },
26 FormValue.Address => |payload| {
27 assert(payload == 1);
28 },
2529 else => unreachable,
2630 }
2731 assert(read_count == 1);
test/cases/switch_prong_implicit_cast.zig+6-2
......@@ -7,8 +7,12 @@ const FormValue = union(enum) {
77
88fn foo(id: u64) !FormValue {
99 return switch (id) {
10 2 => FormValue { .Two = true },
11 1 => FormValue { .One = {} },
10 2 => FormValue {
11 .Two = true,
12 },
13 1 => FormValue {
14 .One = {},
15 },
1216 else => return error.Whatever,
1317 };
1418}
test/cases/try.zig+3-5
......@@ -3,14 +3,12 @@ const assert = @import("std").debug.assert;
33test "try on error union" {
44 tryOnErrorUnionImpl();
55 comptime tryOnErrorUnionImpl();
6
76}
87
98fn tryOnErrorUnionImpl() void {
10 const x = if (returnsTen()) |val|
11 val + 1
12 else |err| switch (err) {
13 error.ItBroke, error.NoMem => 1,
9 const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
10 error.ItBroke,
11 error.NoMem => 1,
1412 error.CrappedOut => i32(2),
1513 else => unreachable,
1614 };
test/cases/undefined.zig+2-2
......@@ -63,6 +63,6 @@ test "assign undefined to struct with method" {
6363}
6464
6565test "type name of undefined" {
66 const x = undefined;
67 assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
66 const x = undefined;
67 assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
6868}
test/cases/union.zig+50-36
......@@ -10,38 +10,41 @@ const Agg = struct {
1010 val2: Value,
1111};
1212
13const v1 = Value { .Int = 1234 };
14const v2 = Value { .Array = []u8{3} ** 9 };
13const v1 = Value{ .Int = 1234 };
14const v2 = Value{ .Array = []u8{3} ** 9 };
1515
16const err = (error!Agg)(Agg {
16const err = (error!Agg)(Agg{
1717 .val1 = v1,
1818 .val2 = v2,
1919});
2020
21const array = []Value { v1, v2, v1, v2};
22
21const array = []Value{
22 v1,
23 v2,
24 v1,
25 v2,
26};
2327
2428test "unions embedded in aggregate types" {
2529 switch (array[1]) {
2630 Value.Array => |arr| assert(arr[4] == 3),
2731 else => unreachable,
2832 }
29 switch((err catch unreachable).val1) {
33 switch ((err catch unreachable).val1) {
3034 Value.Int => |x| assert(x == 1234),
3135 else => unreachable,
3236 }
3337}
3438
35
3639const Foo = union {
3740 float: f64,
3841 int: i32,
3942};
4043
4144test "basic unions" {
42 var foo = Foo { .int = 1 };
45 var foo = Foo{ .int = 1 };
4346 assert(foo.int == 1);
44 foo = Foo {.float = 12.34};
47 foo = Foo{ .float = 12.34 };
4548 assert(foo.float == 12.34);
4649}
4750
......@@ -66,11 +69,11 @@ test "init union with runtime value" {
6669}
6770
6871fn setFloat(foo: &Foo, x: f64) void {
69 *foo = Foo { .float = x };
72 foo.* = Foo{ .float = x };
7073}
7174
7275fn setInt(foo: &Foo, x: i32) void {
73 *foo = Foo { .int = x };
76 foo.* = Foo{ .int = x };
7477}
7578
7679const FooExtern = extern union {
......@@ -79,13 +82,12 @@ const FooExtern = extern union {
7982};
8083
8184test "basic extern unions" {
82 var foo = FooExtern { .int = 1 };
85 var foo = FooExtern{ .int = 1 };
8386 assert(foo.int == 1);
8487 foo.float = 12.34;
8588 assert(foo.float == 12.34);
8689}
8790
88
8991const Letter = enum {
9092 A,
9193 B,
......@@ -103,12 +105,12 @@ test "union with specified enum tag" {
103105}
104106
105107fn doTest() void {
106 assert(bar(Payload {.A = 1234}) == -10);
108 assert(bar(Payload{ .A = 1234 }) == -10);
107109}
108110
109111fn bar(value: &const Payload) i32 {
110 assert(Letter(*value) == Letter.A);
111 return switch (*value) {
112 assert(Letter(value.*) == Letter.A);
113 return switch (value.*) {
112114 Payload.A => |x| return x - 1244,
113115 Payload.B => |x| if (x == 12.34) i32(20) else 21,
114116 Payload.C => |x| if (x) i32(30) else 31,
......@@ -141,13 +143,13 @@ const MultipleChoice2 = union(enum(u32)) {
141143
142144test "union(enum(u32)) with specified and unspecified tag values" {
143145 comptime assert(@TagType(@TagType(MultipleChoice2)) == u32);
144 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2 {.C = 123});
145 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2 { .C = 123} );
146 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
147 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
146148}
147149
148150fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void {
149 assert(u32(@TagType(MultipleChoice2)(*x)) == 60);
150 assert(1123 == switch (*x) {
151 assert(u32(@TagType(MultipleChoice2)(x.*)) == 60);
152 assert(1123 == switch (x.*) {
151153 MultipleChoice2.A => 1,
152154 MultipleChoice2.B => 2,
153155 MultipleChoice2.C => |v| i32(1000) + v,
......@@ -160,10 +162,9 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void
160162 });
161163}
162164
163
164165const ExternPtrOrInt = extern union {
165166 ptr: &u8,
166 int: u64
167 int: u64,
167168};
168169test "extern union size" {
169170 comptime assert(@sizeOf(ExternPtrOrInt) == 8);
......@@ -171,7 +172,7 @@ test "extern union size" {
171172
172173const PackedPtrOrInt = packed union {
173174 ptr: &u8,
174 int: u64
175 int: u64,
175176};
176177test "extern union size" {
177178 comptime assert(@sizeOf(PackedPtrOrInt) == 8);
......@@ -184,8 +185,16 @@ test "union with only 1 field which is void should be zero bits" {
184185 comptime assert(@sizeOf(ZeroBits) == 0);
185186}
186187
187const TheTag = enum {A, B, C};
188const TheUnion = union(TheTag) { A: i32, B: i32, C: i32 };
188const TheTag = enum {
189 A,
190 B,
191 C,
192};
193const TheUnion = union(TheTag) {
194 A: i32,
195 B: i32,
196 C: i32,
197};
189198test "union field access gives the enum values" {
190199 assert(TheUnion.A == TheTag.A);
191200 assert(TheUnion.B == TheTag.B);
......@@ -193,20 +202,28 @@ test "union field access gives the enum values" {
193202}
194203
195204test "cast union to tag type of union" {
196 testCastUnionToTagType(TheUnion {.B = 1234});
197 comptime testCastUnionToTagType(TheUnion {.B = 1234});
205 testCastUnionToTagType(TheUnion{ .B = 1234 });
206 comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
198207}
199208
200209fn testCastUnionToTagType(x: &const TheUnion) void {
201 assert(TheTag(*x) == TheTag.B);
210 assert(TheTag(x.*) == TheTag.B);
202211}
203212
204213test "cast tag type of union to union" {
205214 var x: Value2 = Letter2.B;
206215 assert(Letter2(x) == Letter2.B);
207216}
208const Letter2 = enum { A, B, C };
209const Value2 = union(Letter2) { A: i32, B, C, };
217const Letter2 = enum {
218 A,
219 B,
220 C,
221};
222const Value2 = union(Letter2) {
223 A: i32,
224 B,
225 C,
226};
210227
211228test "implicit cast union to its tag type" {
212229 var x: Value2 = Letter2.B;
......@@ -227,19 +244,16 @@ const TheUnion2 = union(enum) {
227244};
228245
229246fn assertIsTheUnion2Item1(value: &const TheUnion2) void {
230 assert(*value == TheUnion2.Item1);
247 assert(value.* == TheUnion2.Item1);
231248}
232249
233
234250pub const PackThis = union(enum) {
235251 Invalid: bool,
236252 StringLiteral: u2,
237253};
238254
239255test "constant packed union" {
240 testConstPackedUnion([]PackThis {
241 PackThis { .StringLiteral = 1 },
242 });
256 testConstPackedUnion([]PackThis{PackThis{ .StringLiteral = 1 }});
243257}
244258
245259fn testConstPackedUnion(expected_tokens: []const PackThis) void {
......@@ -252,7 +266,7 @@ test "switch on union with only 1 field" {
252266 switch (r) {
253267 PartialInst.Compiled => {
254268 var z: PartialInstWithPayload = undefined;
255 z = PartialInstWithPayload { .Compiled = 1234 };
269 z = PartialInstWithPayload{ .Compiled = 1234 };
256270 switch (z) {
257271 PartialInstWithPayload.Compiled => |x| {
258272 assert(x == 1234);
test/cases/var_args.zig+16-9
......@@ -2,9 +2,12 @@ const assert = @import("std").debug.assert;
22
33fn add(args: ...) i32 {
44 var sum = i32(0);
5 {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
6 sum += args[i];
7 }}
5 {
6 comptime var i: usize = 0;
7 inline while (i < args.len) : (i += 1) {
8 sum += args[i];
9 }
10 }
811 return sum;
912}
1013
......@@ -55,18 +58,23 @@ fn extraFn(extra: u32, args: ...) usize {
5558 return args.len;
5659}
5760
61const foos = []fn(...) bool {
62 foo1,
63 foo2,
64};
5865
59const foos = []fn(...) bool { foo1, foo2 };
60
61fn foo1(args: ...) bool { return true; }
62fn foo2(args: ...) bool { return false; }
66fn foo1(args: ...) bool {
67 return true;
68}
69fn foo2(args: ...) bool {
70 return false;
71}
6372
6473test "array of var args functions" {
6574 assert(foos[0]());
6675 assert(!foos[1]());
6776}
6877
69
7078test "pass array and slice of same array to var args should have same pointers" {
7179 const array = "hi";
7280 const slice: []const u8 = array;
......@@ -79,7 +87,6 @@ fn assertSlicePtrsEql(args: ...) void {
7987 assert(s1.ptr == s2.ptr);
8088}
8189
82
8390test "pass zero length array to var args param" {
8491 doNothingWithFirstArg("");
8592}
test/cases/while.zig+41-24
......@@ -1,7 +1,7 @@
11const assert = @import("std").debug.assert;
22
33test "while loop" {
4 var i : i32 = 0;
4 var i: i32 = 0;
55 while (i < 4) {
66 i += 1;
77 }
......@@ -35,7 +35,7 @@ test "continue and break" {
3535}
3636var continue_and_break_counter: i32 = 0;
3737fn runContinueAndBreakTest() void {
38 var i : i32 = 0;
38 var i: i32 = 0;
3939 while (true) {
4040 continue_and_break_counter += 2;
4141 i += 1;
......@@ -58,10 +58,13 @@ fn returnWithImplicitCastFromWhileLoopTest() error!void {
5858
5959test "while with continue expression" {
6060 var sum: i32 = 0;
61 {var i: i32 = 0; while (i < 10) : (i += 1) {
62 if (i == 5) continue;
63 sum += i;
64 }}
61 {
62 var i: i32 = 0;
63 while (i < 10) : (i += 1) {
64 if (i == 5) continue;
65 sum += i;
66 }
67 }
6568 assert(sum == 40);
6669}
6770
......@@ -117,17 +120,13 @@ test "while with error union condition" {
117120
118121var numbers_left: i32 = undefined;
119122fn getNumberOrErr() error!i32 {
120 return if (numbers_left == 0)
121 error.OutOfNumbers
122 else x: {
123 return if (numbers_left == 0) error.OutOfNumbers else x: {
123124 numbers_left -= 1;
124125 break :x numbers_left;
125126 };
126127}
127128fn getNumberOrNull() ?i32 {
128 return if (numbers_left == 0)
129 null
130 else x: {
129 return if (numbers_left == 0) null else x: {
131130 numbers_left -= 1;
132131 break :x numbers_left;
133132 };
......@@ -136,42 +135,48 @@ fn getNumberOrNull() ?i32 {
136135test "while on nullable with else result follow else prong" {
137136 const result = while (returnNull()) |value| {
138137 break value;
139 } else i32(2);
138 } else
139 i32(2);
140140 assert(result == 2);
141141}
142142
143143test "while on nullable with else result follow break prong" {
144144 const result = while (returnMaybe(10)) |value| {
145145 break value;
146 } else i32(2);
146 } else
147 i32(2);
147148 assert(result == 10);
148149}
149150
150151test "while on error union with else result follow else prong" {
151152 const result = while (returnError()) |value| {
152153 break value;
153 } else |err| i32(2);
154 } else|err|
155 i32(2);
154156 assert(result == 2);
155157}
156158
157159test "while on error union with else result follow break prong" {
158160 const result = while (returnSuccess(10)) |value| {
159161 break value;
160 } else |err| i32(2);
162 } else|err|
163 i32(2);
161164 assert(result == 10);
162165}
163166
164167test "while on bool with else result follow else prong" {
165168 const result = while (returnFalse()) {
166169 break i32(10);
167 } else i32(2);
170 } else
171 i32(2);
168172 assert(result == 2);
169173}
170174
171175test "while on bool with else result follow break prong" {
172176 const result = while (returnTrue()) {
173177 break i32(10);
174 } else i32(2);
178 } else
179 i32(2);
175180 assert(result == 10);
176181}
177182
......@@ -202,9 +207,21 @@ fn testContinueOuter() void {
202207 }
203208}
204209
205fn returnNull() ?i32 { return null; }
206fn returnMaybe(x: i32) ?i32 { return x; }
207fn returnError() error!i32 { return error.YouWantedAnError; }
208fn returnSuccess(x: i32) error!i32 { return x; }
209fn returnFalse() bool { return false; }
210fn returnTrue() bool { return true; }
210fn returnNull() ?i32 {
211 return null;
212}
213fn returnMaybe(x: i32) ?i32 {
214 return x;
215}
216fn returnError() error!i32 {
217 return error.YouWantedAnError;
218}
219fn returnSuccess(x: i32) error!i32 {
220 return x;
221}
222fn returnFalse() bool {
223 return false;
224}
225fn returnTrue() bool {
226 return true;
227}
test/compare_output.zig+2-2
......@@ -287,9 +287,9 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
287287 \\export fn compare_fn(a: ?&const c_void, b: ?&const c_void) c_int {
288288 \\ const a_int = @ptrCast(&align(1) const i32, a ?? unreachable);
289289 \\ const b_int = @ptrCast(&align(1) const i32, b ?? unreachable);
290 \\ if (*a_int < *b_int) {
290 \\ if (a_int.* < b_int.*) {
291291 \\ return -1;
292 \\ } else if (*a_int > *b_int) {
292 \\ } else if (a_int.* > b_int.*) {
293293 \\ return 1;
294294 \\ } else {
295295 \\ return 0;
test/compile_errors.zig+15-15
......@@ -4,7 +4,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
44 cases.add("invalid deref on switch target",
55 \\comptime {
66 \\ var tile = Tile.Empty;
7 \\ switch (*tile) {
7 \\ switch (tile.*) {
88 \\ Tile.Empty => {},
99 \\ Tile.Filled => {},
1010 \\ }
......@@ -14,7 +14,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
1414 \\ Filled,
1515 \\};
1616 ,
17 ".tmp_source.zig:3:13: error: invalid deref on switch target");
17 ".tmp_source.zig:3:17: error: invalid deref on switch target");
1818
1919 cases.add("invalid field access in comptime",
2020 \\comptime { var x = doesnt_exist.whatever; }
......@@ -1408,14 +1408,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
14081408 \\ Two: i32,
14091409 \\};
14101410 \\fn bad_eql_2(a: &const EnumWithData, b: &const EnumWithData) bool {
1411 \\ return *a == *b;
1411 \\ return a.* == b.*;
14121412 \\}
14131413 \\
14141414 \\export fn entry1() usize { return @sizeOf(@typeOf(bad_eql_1)); }
14151415 \\export fn entry2() usize { return @sizeOf(@typeOf(bad_eql_2)); }
14161416 ,
14171417 ".tmp_source.zig:2:14: error: operator not allowed for type '[]u8'",
1418 ".tmp_source.zig:9:15: error: operator not allowed for type 'EnumWithData'");
1418 ".tmp_source.zig:9:16: error: operator not allowed for type 'EnumWithData'");
14191419
14201420 cases.add("non-const switch number literal",
14211421 \\export fn foo() void {
......@@ -1513,7 +1513,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
15131513 \\var bytes: [ext()]u8 = undefined;
15141514 \\export fn f() void {
15151515 \\ for (bytes) |*b, i| {
1516 \\ *b = u8(i);
1516 \\ b.* = u8(i);
15171517 \\ }
15181518 \\}
15191519 , ".tmp_source.zig:2:13: error: unable to evaluate constant expression");
......@@ -1819,7 +1819,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
18191819 \\}
18201820 \\
18211821 \\fn bar(x: &const u3) u3 {
1822 \\ return *x;
1822 \\ return x.*;
18231823 \\}
18241824 \\
18251825 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
......@@ -1903,12 +1903,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
19031903 \\var s_buffer: [10]u8 = undefined;
19041904 \\pub fn pass(in: []u8) []u8 {
19051905 \\ var out = &s_buffer;
1906 \\ *out[0] = in[0];
1907 \\ return (*out)[0..1];
1906 \\ out[0].* = in[0];
1907 \\ return out.*[0..1];
19081908 \\}
19091909 \\
19101910 \\export fn entry() usize { return @sizeOf(@typeOf(pass)); }
1911 , ".tmp_source.zig:4:5: error: attempt to dereference non pointer type '[10]u8'");
1911 , ".tmp_source.zig:4:11: error: attempt to dereference non pointer type '[10]u8'");
19121912
19131913 cases.add("pass const ptr to mutable ptr fn",
19141914 \\fn foo() bool {
......@@ -2434,7 +2434,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
24342434 \\}
24352435 \\
24362436 \\fn bar(x: &u32) void {
2437 \\ *x += 1;
2437 \\ x.* += 1;
24382438 \\}
24392439 ,
24402440 ".tmp_source.zig:8:13: error: expected type '&u32', found '&align(1) u32'");
......@@ -2461,7 +2461,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
24612461 \\export fn entry() u32 {
24622462 \\ var bytes: [4]u8 = []u8{0x01, 0x02, 0x03, 0x04};
24632463 \\ const ptr = @ptrCast(&u32, &bytes[0]);
2464 \\ return *ptr;
2464 \\ return ptr.*;
24652465 \\}
24662466 ,
24672467 ".tmp_source.zig:3:17: error: cast increases pointer alignment",
......@@ -2540,14 +2540,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
25402540 \\
25412541 \\export fn entry(opaque: &Opaque) void {
25422542 \\ var m2 = &2;
2543 \\ const y: u32 = *m2;
2543 \\ const y: u32 = m2.*;
25442544 \\
25452545 \\ var a = undefined;
25462546 \\ var b = 1;
25472547 \\ var c = 1.0;
25482548 \\ var d = this;
25492549 \\ var e = null;
2550 \\ var f = *opaque;
2550 \\ var f = opaque.*;
25512551 \\ var g = i32;
25522552 \\ var h = @import("std");
25532553 \\ var i = (Foo {}).bar;
......@@ -3136,13 +3136,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
31363136 \\ foo(a);
31373137 \\}
31383138 \\fn foo(a: &const Payload) void {
3139 \\ switch (*a) {
3139 \\ switch (a.*) {
31403140 \\ Payload.A => {},
31413141 \\ else => unreachable,
31423142 \\ }
31433143 \\}
31443144 ,
3145 ".tmp_source.zig:11:13: error: switch on union which has no attached enum",
3145 ".tmp_source.zig:11:14: error: switch on union which has no attached enum",
31463146 ".tmp_source.zig:1:17: note: consider 'union(enum)' here");
31473147
31483148 cases.add("enum in field count range but not matching tag",
test/standalone/brace_expansion/main.zig+23-20
......@@ -16,7 +16,7 @@ const Token = union(enum) {
1616
1717var global_allocator: &mem.Allocator = undefined;
1818
19fn tokenize(input:[] const u8) !ArrayList(Token) {
19fn tokenize(input: []const u8) !ArrayList(Token) {
2020 const State = enum {
2121 Start,
2222 Word,
......@@ -29,7 +29,8 @@ fn tokenize(input:[] const u8) !ArrayList(Token) {
2929 for (input) |b, i| {
3030 switch (state) {
3131 State.Start => switch (b) {
32 'a'...'z', 'A'...'Z' => {
32 'a' ... 'z',
33 'A' ... 'Z' => {
3334 state = State.Word;
3435 tok_begin = i;
3536 },
......@@ -39,9 +40,12 @@ fn tokenize(input:[] const u8) !ArrayList(Token) {
3940 else => return error.InvalidInput,
4041 },
4142 State.Word => switch (b) {
42 'a'...'z', 'A'...'Z' => {},
43 '{', '}', ',' => {
44 try token_list.append(Token { .Word = input[tok_begin..i] });
43 'a' ... 'z',
44 'A' ... 'Z' => {},
45 '{',
46 '}',
47 ',' => {
48 try token_list.append(Token{ .Word = input[tok_begin..i] });
4549 switch (b) {
4650 '{' => try token_list.append(Token.OpenBrace),
4751 '}' => try token_list.append(Token.CloseBrace),
......@@ -56,7 +60,7 @@ fn tokenize(input:[] const u8) !ArrayList(Token) {
5660 }
5761 switch (state) {
5862 State.Start => {},
59 State.Word => try token_list.append(Token {.Word = input[tok_begin..] }),
63 State.Word => try token_list.append(Token{ .Word = input[tok_begin..] }),
6064 }
6165 try token_list.append(Token.Eof);
6266 return token_list;
......@@ -68,24 +72,24 @@ const Node = union(enum) {
6872 Combine: []Node,
6973};
7074
71const ParseError = error {
75const ParseError = error{
7276 InvalidInput,
7377 OutOfMemory,
7478};
7579
7680fn parse(tokens: &const ArrayList(Token), token_index: &usize) ParseError!Node {
77 const first_token = tokens.items[*token_index];
78 *token_index += 1;
81 const first_token = tokens.items[token_index.*];
82 token_index.* += 1;
7983
8084 const result_node = switch (first_token) {
81 Token.Word => |word| Node { .Scalar = word },
85 Token.Word => |word| Node{ .Scalar = word },
8286 Token.OpenBrace => blk: {
8387 var list = ArrayList(Node).init(global_allocator);
8488 while (true) {
8589 try list.append(try parse(tokens, token_index));
8690
87 const token = tokens.items[*token_index];
88 *token_index += 1;
91 const token = tokens.items[token_index.*];
92 token_index.* += 1;
8993
9094 switch (token) {
9195 Token.CloseBrace => break,
......@@ -93,17 +97,18 @@ fn parse(tokens: &const ArrayList(Token), token_index: &usize) ParseError!Node {
9397 else => return error.InvalidInput,
9498 }
9599 }
96 break :blk Node { .List = list };
100 break :blk Node{ .List = list };
97101 },
98102 else => return error.InvalidInput,
99103 };
100104
101 switch (tokens.items[*token_index]) {
102 Token.Word, Token.OpenBrace => {
105 switch (tokens.items[token_index.*]) {
106 Token.Word,
107 Token.OpenBrace => {
103108 const pair = try global_allocator.alloc(Node, 2);
104109 pair[0] = result_node;
105110 pair[1] = try parse(tokens, token_index);
106 return Node { .Combine = pair };
111 return Node{ .Combine = pair };
107112 },
108113 else => return result_node,
109114 }
......@@ -137,13 +142,11 @@ fn expandString(input: []const u8, output: &Buffer) !void {
137142 }
138143}
139144
140const ExpandNodeError = error {
141 OutOfMemory,
142};
145const ExpandNodeError = error{OutOfMemory};
143146
144147fn expandNode(node: &const Node, output: &ArrayList(Buffer)) ExpandNodeError!void {
145148 assert(output.len == 0);
146 switch (*node) {
149 switch (node.*) {
147150 Node.Scalar => |scalar| {
148151 try output.append(try Buffer.init(global_allocator, scalar));
149152 },
test/tests.zig+90-100
......@@ -27,18 +27,18 @@ const TestTarget = struct {
2727 environ: builtin.Environ,
2828};
2929
30const test_targets = []TestTarget {
31 TestTarget {
30const test_targets = []TestTarget{
31 TestTarget{
3232 .os = builtin.Os.linux,
3333 .arch = builtin.Arch.x86_64,
3434 .environ = builtin.Environ.gnu,
3535 },
36 TestTarget {
36 TestTarget{
3737 .os = builtin.Os.macosx,
3838 .arch = builtin.Arch.x86_64,
3939 .environ = builtin.Environ.unknown,
4040 },
41 TestTarget {
41 TestTarget{
4242 .os = builtin.Os.windows,
4343 .arch = builtin.Arch.x86_64,
4444 .environ = builtin.Environ.msvc,
......@@ -49,7 +49,7 @@ const max_stdout_size = 1 * 1024 * 1024; // 1 MB
4949
5050pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
5151 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
52 *cases = CompareOutputContext {
52 cases.* = CompareOutputContext{
5353 .b = b,
5454 .step = b.step("test-compare-output", "Run the compare output tests"),
5555 .test_index = 0,
......@@ -63,7 +63,7 @@ pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) &build
6363
6464pub fn addRuntimeSafetyTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
6565 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
66 *cases = CompareOutputContext {
66 cases.* = CompareOutputContext{
6767 .b = b,
6868 .step = b.step("test-runtime-safety", "Run the runtime safety tests"),
6969 .test_index = 0,
......@@ -77,7 +77,7 @@ pub fn addRuntimeSafetyTests(b: &build.Builder, test_filter: ?[]const u8) &build
7777
7878pub fn addCompileErrorTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
7979 const cases = b.allocator.create(CompileErrorContext) catch unreachable;
80 *cases = CompileErrorContext {
80 cases.* = CompileErrorContext{
8181 .b = b,
8282 .step = b.step("test-compile-errors", "Run the compile error tests"),
8383 .test_index = 0,
......@@ -91,7 +91,7 @@ pub fn addCompileErrorTests(b: &build.Builder, test_filter: ?[]const u8) &build.
9191
9292pub fn addBuildExampleTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
9393 const cases = b.allocator.create(BuildExamplesContext) catch unreachable;
94 *cases = BuildExamplesContext {
94 cases.* = BuildExamplesContext{
9595 .b = b,
9696 .step = b.step("test-build-examples", "Build the examples"),
9797 .test_index = 0,
......@@ -105,7 +105,7 @@ pub fn addBuildExampleTests(b: &build.Builder, test_filter: ?[]const u8) &build.
105105
106106pub fn addAssembleAndLinkTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
107107 const cases = b.allocator.create(CompareOutputContext) catch unreachable;
108 *cases = CompareOutputContext {
108 cases.* = CompareOutputContext{
109109 .b = b,
110110 .step = b.step("test-asm-link", "Run the assemble and link tests"),
111111 .test_index = 0,
......@@ -119,7 +119,7 @@ pub fn addAssembleAndLinkTests(b: &build.Builder, test_filter: ?[]const u8) &bui
119119
120120pub fn addTranslateCTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
121121 const cases = b.allocator.create(TranslateCContext) catch unreachable;
122 *cases = TranslateCContext {
122 cases.* = TranslateCContext{
123123 .b = b,
124124 .step = b.step("test-translate-c", "Run the C transation tests"),
125125 .test_index = 0,
......@@ -133,7 +133,7 @@ pub fn addTranslateCTests(b: &build.Builder, test_filter: ?[]const u8) &build.St
133133
134134pub fn addGenHTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
135135 const cases = b.allocator.create(GenHContext) catch unreachable;
136 *cases = GenHContext {
136 cases.* = GenHContext{
137137 .b = b,
138138 .step = b.step("test-gen-h", "Run the C header file generation tests"),
139139 .test_index = 0,
......@@ -145,22 +145,26 @@ pub fn addGenHTests(b: &build.Builder, test_filter: ?[]const u8) &build.Step {
145145 return cases.step;
146146}
147147
148
149pub fn addPkgTests(b: &build.Builder, test_filter: ?[]const u8, root_src: []const u8,
150 name:[] const u8, desc: []const u8, with_lldb: bool) &build.Step
151{
148pub fn addPkgTests(b: &build.Builder, test_filter: ?[]const u8, root_src: []const u8, name: []const u8, desc: []const u8, with_lldb: bool) &build.Step {
152149 const step = b.step(b.fmt("test-{}", name), desc);
153150 for (test_targets) |test_target| {
154151 const is_native = (test_target.os == builtin.os and test_target.arch == builtin.arch);
155 for ([]Mode{Mode.Debug, Mode.ReleaseSafe, Mode.ReleaseFast, Mode.ReleaseSmall}) |mode| {
156 for ([]bool{false, true}) |link_libc| {
152 for ([]Mode{
153 Mode.Debug,
154 Mode.ReleaseSafe,
155 Mode.ReleaseFast,
156 Mode.ReleaseSmall,
157 }) |mode| {
158 for ([]bool{
159 false,
160 true,
161 }) |link_libc| {
157162 if (link_libc and !is_native) {
158163 // don't assume we have a cross-compiling libc set up
159164 continue;
160165 }
161166 const these_tests = b.addTest(root_src);
162 these_tests.setNamePrefix(b.fmt("{}-{}-{}-{}-{} ", name, @tagName(test_target.os),
163 @tagName(test_target.arch), @tagName(mode), if (link_libc) "c" else "bare"));
167 these_tests.setNamePrefix(b.fmt("{}-{}-{}-{}-{} ", name, @tagName(test_target.os), @tagName(test_target.arch), @tagName(mode), if (link_libc) "c" else "bare"));
164168 these_tests.setFilter(test_filter);
165169 these_tests.setBuildMode(mode);
166170 if (!is_native) {
......@@ -171,7 +175,15 @@ pub fn addPkgTests(b: &build.Builder, test_filter: ?[]const u8, root_src: []cons
171175 }
172176 if (with_lldb) {
173177 these_tests.setExecCmd([]?[]const u8{
174 "lldb", null, "-o", "run", "-o", "bt", "-o", "exit"});
178 "lldb",
179 null,
180 "-o",
181 "run",
182 "-o",
183 "bt",
184 "-o",
185 "exit",
186 });
175187 }
176188 step.dependOn(&these_tests.step);
177189 }
......@@ -206,7 +218,7 @@ pub const CompareOutputContext = struct {
206218 };
207219
208220 pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
209 self.sources.append(SourceFile {
221 self.sources.append(SourceFile{
210222 .filename = filename,
211223 .source = source,
212224 }) catch unreachable;
......@@ -226,13 +238,10 @@ pub const CompareOutputContext = struct {
226238 test_index: usize,
227239 cli_args: []const []const u8,
228240
229 pub fn create(context: &CompareOutputContext, exe_path: []const u8,
230 name: []const u8, expected_output: []const u8,
231 cli_args: []const []const u8) &RunCompareOutputStep
232 {
241 pub fn create(context: &CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) &RunCompareOutputStep {
233242 const allocator = context.b.allocator;
234243 const ptr = allocator.create(RunCompareOutputStep) catch unreachable;
235 *ptr = RunCompareOutputStep {
244 ptr.* = RunCompareOutputStep{
236245 .context = context,
237246 .exe_path = exe_path,
238247 .name = name,
......@@ -258,7 +267,7 @@ pub const CompareOutputContext = struct {
258267 args.append(arg) catch unreachable;
259268 }
260269
261 warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);
270 warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
262271
263272 const child = os.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable;
264273 defer child.deinit();
......@@ -295,7 +304,6 @@ pub const CompareOutputContext = struct {
295304 },
296305 }
297306
298
299307 if (!mem.eql(u8, self.expected_output, stdout.toSliceConst())) {
300308 warn(
301309 \\
......@@ -318,12 +326,10 @@ pub const CompareOutputContext = struct {
318326 name: []const u8,
319327 test_index: usize,
320328
321 pub fn create(context: &CompareOutputContext, exe_path: []const u8,
322 name: []const u8) &RuntimeSafetyRunStep
323 {
329 pub fn create(context: &CompareOutputContext, exe_path: []const u8, name: []const u8) &RuntimeSafetyRunStep {
324330 const allocator = context.b.allocator;
325331 const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable;
326 *ptr = RuntimeSafetyRunStep {
332 ptr.* = RuntimeSafetyRunStep{
327333 .context = context,
328334 .exe_path = exe_path,
329335 .name = name,
......@@ -340,7 +346,7 @@ pub const CompareOutputContext = struct {
340346
341347 const full_exe_path = b.pathFromRoot(self.exe_path);
342348
343 warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);
349 warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
344350
345351 const child = os.ChildProcess.init([][]u8{full_exe_path}, b.allocator) catch unreachable;
346352 defer child.deinit();
......@@ -358,19 +364,16 @@ pub const CompareOutputContext = struct {
358364 switch (term) {
359365 Term.Exited => |code| {
360366 if (code != expected_exit_code) {
361 warn("\nProgram expected to exit with code {} " ++
362 "but exited with code {}\n", expected_exit_code, code);
367 warn("\nProgram expected to exit with code {} " ++ "but exited with code {}\n", expected_exit_code, code);
363368 return error.TestFailed;
364369 }
365370 },
366371 Term.Signal => |sig| {
367 warn("\nProgram expected to exit with code {} " ++
368 "but instead signaled {}\n", expected_exit_code, sig);
372 warn("\nProgram expected to exit with code {} " ++ "but instead signaled {}\n", expected_exit_code, sig);
369373 return error.TestFailed;
370374 },
371375 else => {
372 warn("\nProgram expected to exit with code {}" ++
373 " but exited in an unexpected way\n", expected_exit_code);
376 warn("\nProgram expected to exit with code {}" ++ " but exited in an unexpected way\n", expected_exit_code);
374377 return error.TestFailed;
375378 },
376379 }
......@@ -379,10 +382,8 @@ pub const CompareOutputContext = struct {
379382 }
380383 };
381384
382 pub fn createExtra(self: &CompareOutputContext, name: []const u8, source: []const u8,
383 expected_output: []const u8, special: Special) TestCase
384 {
385 var tc = TestCase {
385 pub fn createExtra(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8, special: Special) TestCase {
386 var tc = TestCase{
386387 .name = name,
387388 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
388389 .expected_output = expected_output,
......@@ -395,9 +396,7 @@ pub const CompareOutputContext = struct {
395396 return tc;
396397 }
397398
398 pub fn create(self: &CompareOutputContext, name: []const u8, source: []const u8,
399 expected_output: []const u8) TestCase
400 {
399 pub fn create(self: &CompareOutputContext, name: []const u8, source: []const u8, expected_output: []const u8) TestCase {
401400 return createExtra(self, name, source, expected_output, Special.None);
402401 }
403402
......@@ -431,8 +430,7 @@ pub const CompareOutputContext = struct {
431430 Special.Asm => {
432431 const annotated_case_name = fmt.allocPrint(self.b.allocator, "assemble-and-link {}", case.name) catch unreachable;
433432 if (self.test_filter) |filter| {
434 if (mem.indexOf(u8, annotated_case_name, filter) == null)
435 return;
433 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
436434 }
437435
438436 const exe = b.addExecutable("test", null);
......@@ -444,19 +442,21 @@ pub const CompareOutputContext = struct {
444442 exe.step.dependOn(&write_src.step);
445443 }
446444
447 const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(), annotated_case_name,
448 case.expected_output, case.cli_args);
445 const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(), annotated_case_name, case.expected_output, case.cli_args);
449446 run_and_cmp_output.step.dependOn(&exe.step);
450447
451448 self.step.dependOn(&run_and_cmp_output.step);
452449 },
453450 Special.None => {
454 for ([]Mode{Mode.Debug, Mode.ReleaseSafe, Mode.ReleaseFast, Mode.ReleaseSmall}) |mode| {
455 const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {} ({})",
456 "compare-output", case.name, @tagName(mode)) catch unreachable;
451 for ([]Mode{
452 Mode.Debug,
453 Mode.ReleaseSafe,
454 Mode.ReleaseFast,
455 Mode.ReleaseSmall,
456 }) |mode| {
457 const annotated_case_name = fmt.allocPrint(self.b.allocator, "{} {} ({})", "compare-output", case.name, @tagName(mode)) catch unreachable;
457458 if (self.test_filter) |filter| {
458 if (mem.indexOf(u8, annotated_case_name, filter) == null)
459 continue;
459 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
460460 }
461461
462462 const exe = b.addExecutable("test", root_src);
......@@ -471,8 +471,7 @@ pub const CompareOutputContext = struct {
471471 exe.step.dependOn(&write_src.step);
472472 }
473473
474 const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(),
475 annotated_case_name, case.expected_output, case.cli_args);
474 const run_and_cmp_output = RunCompareOutputStep.create(self, exe.getOutputPath(), annotated_case_name, case.expected_output, case.cli_args);
476475 run_and_cmp_output.step.dependOn(&exe.step);
477476
478477 self.step.dependOn(&run_and_cmp_output.step);
......@@ -481,8 +480,7 @@ pub const CompareOutputContext = struct {
481480 Special.RuntimeSafety => {
482481 const annotated_case_name = fmt.allocPrint(self.b.allocator, "safety {}", case.name) catch unreachable;
483482 if (self.test_filter) |filter| {
484 if (mem.indexOf(u8, annotated_case_name, filter) == null)
485 return;
483 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
486484 }
487485
488486 const exe = b.addExecutable("test", root_src);
......@@ -524,7 +522,7 @@ pub const CompileErrorContext = struct {
524522 };
525523
526524 pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
527 self.sources.append(SourceFile {
525 self.sources.append(SourceFile{
528526 .filename = filename,
529527 .source = source,
530528 }) catch unreachable;
......@@ -543,12 +541,10 @@ pub const CompileErrorContext = struct {
543541 case: &const TestCase,
544542 build_mode: Mode,
545543
546 pub fn create(context: &CompileErrorContext, name: []const u8,
547 case: &const TestCase, build_mode: Mode) &CompileCmpOutputStep
548 {
544 pub fn create(context: &CompileErrorContext, name: []const u8, case: &const TestCase, build_mode: Mode) &CompileCmpOutputStep {
549545 const allocator = context.b.allocator;
550546 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
551 *ptr = CompileCmpOutputStep {
547 ptr.* = CompileCmpOutputStep{
552548 .step = build.Step.init("CompileCmpOutput", allocator, make),
553549 .context = context,
554550 .name = name,
......@@ -586,7 +582,7 @@ pub const CompileErrorContext = struct {
586582 Mode.ReleaseSmall => zig_args.append("--release-small") catch unreachable,
587583 }
588584
589 warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);
585 warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
590586
591587 if (b.verbose) {
592588 printInvocation(zig_args.toSliceConst());
......@@ -626,7 +622,6 @@ pub const CompileErrorContext = struct {
626622 },
627623 }
628624
629
630625 const stdout = stdout_buf.toSliceConst();
631626 const stderr = stderr_buf.toSliceConst();
632627
......@@ -666,11 +661,9 @@ pub const CompileErrorContext = struct {
666661 warn("\n");
667662 }
668663
669 pub fn create(self: &CompileErrorContext, name: []const u8, source: []const u8,
670 expected_lines: ...) &TestCase
671 {
664 pub fn create(self: &CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
672665 const tc = self.b.allocator.create(TestCase) catch unreachable;
673 *tc = TestCase {
666 tc.* = TestCase{
674667 .name = name,
675668 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
676669 .expected_errors = ArrayList([]const u8).init(self.b.allocator),
......@@ -705,12 +698,13 @@ pub const CompileErrorContext = struct {
705698 pub fn addCase(self: &CompileErrorContext, case: &const TestCase) void {
706699 const b = self.b;
707700
708 for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
709 const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {} ({})",
710 case.name, @tagName(mode)) catch unreachable;
701 for ([]Mode{
702 Mode.Debug,
703 Mode.ReleaseFast,
704 }) |mode| {
705 const annotated_case_name = fmt.allocPrint(self.b.allocator, "compile-error {} ({})", case.name, @tagName(mode)) catch unreachable;
711706 if (self.test_filter) |filter| {
712 if (mem.indexOf(u8, annotated_case_name, filter) == null)
713 continue;
707 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
714708 }
715709
716710 const compile_and_cmp_errors = CompileCmpOutputStep.create(self, annotated_case_name, case, mode);
......@@ -744,8 +738,7 @@ pub const BuildExamplesContext = struct {
744738
745739 const annotated_case_name = b.fmt("build {} (Debug)", build_file);
746740 if (self.test_filter) |filter| {
747 if (mem.indexOf(u8, annotated_case_name, filter) == null)
748 return;
741 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
749742 }
750743
751744 var zig_args = ArrayList([]const u8).init(b.allocator);
......@@ -773,12 +766,15 @@ pub const BuildExamplesContext = struct {
773766 pub fn addAllArgs(self: &BuildExamplesContext, root_src: []const u8, link_libc: bool) void {
774767 const b = self.b;
775768
776 for ([]Mode{Mode.Debug, Mode.ReleaseSafe, Mode.ReleaseFast, Mode.ReleaseSmall}) |mode| {
777 const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {} ({})",
778 root_src, @tagName(mode)) catch unreachable;
769 for ([]Mode{
770 Mode.Debug,
771 Mode.ReleaseSafe,
772 Mode.ReleaseFast,
773 Mode.ReleaseSmall,
774 }) |mode| {
775 const annotated_case_name = fmt.allocPrint(self.b.allocator, "build {} ({})", root_src, @tagName(mode)) catch unreachable;
779776 if (self.test_filter) |filter| {
780 if (mem.indexOf(u8, annotated_case_name, filter) == null)
781 continue;
777 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
782778 }
783779
784780 const exe = b.addExecutable("test", root_src);
......@@ -813,7 +809,7 @@ pub const TranslateCContext = struct {
813809 };
814810
815811 pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
816 self.sources.append(SourceFile {
812 self.sources.append(SourceFile{
817813 .filename = filename,
818814 .source = source,
819815 }) catch unreachable;
......@@ -834,7 +830,7 @@ pub const TranslateCContext = struct {
834830 pub fn create(context: &TranslateCContext, name: []const u8, case: &const TestCase) &TranslateCCmpOutputStep {
835831 const allocator = context.b.allocator;
836832 const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable;
837 *ptr = TranslateCCmpOutputStep {
833 ptr.* = TranslateCCmpOutputStep{
838834 .step = build.Step.init("ParseCCmpOutput", allocator, make),
839835 .context = context,
840836 .name = name,
......@@ -857,7 +853,7 @@ pub const TranslateCContext = struct {
857853 zig_args.append("translate-c") catch unreachable;
858854 zig_args.append(b.pathFromRoot(root_src)) catch unreachable;
859855
860 warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);
856 warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
861857
862858 if (b.verbose) {
863859 printInvocation(zig_args.toSliceConst());
......@@ -939,11 +935,9 @@ pub const TranslateCContext = struct {
939935 warn("\n");
940936 }
941937
942 pub fn create(self: &TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8,
943 source: []const u8, expected_lines: ...) &TestCase
944 {
938 pub fn create(self: &TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
945939 const tc = self.b.allocator.create(TestCase) catch unreachable;
946 *tc = TestCase {
940 tc.* = TestCase{
947941 .name = name,
948942 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
949943 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
......@@ -977,8 +971,7 @@ pub const TranslateCContext = struct {
977971
978972 const annotated_case_name = fmt.allocPrint(self.b.allocator, "translate-c {}", case.name) catch unreachable;
979973 if (self.test_filter) |filter| {
980 if (mem.indexOf(u8, annotated_case_name, filter) == null)
981 return;
974 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
982975 }
983976
984977 const translate_c_and_cmp = TranslateCCmpOutputStep.create(self, annotated_case_name, case);
......@@ -1009,7 +1002,7 @@ pub const GenHContext = struct {
10091002 };
10101003
10111004 pub fn addSourceFile(self: &TestCase, filename: []const u8, source: []const u8) void {
1012 self.sources.append(SourceFile {
1005 self.sources.append(SourceFile{
10131006 .filename = filename,
10141007 .source = source,
10151008 }) catch unreachable;
......@@ -1031,7 +1024,7 @@ pub const GenHContext = struct {
10311024 pub fn create(context: &GenHContext, h_path: []const u8, name: []const u8, case: &const TestCase) &GenHCmpOutputStep {
10321025 const allocator = context.b.allocator;
10331026 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
1034 *ptr = GenHCmpOutputStep {
1027 ptr.* = GenHCmpOutputStep{
10351028 .step = build.Step.init("ParseCCmpOutput", allocator, make),
10361029 .context = context,
10371030 .h_path = h_path,
......@@ -1047,7 +1040,7 @@ pub const GenHContext = struct {
10471040 const self = @fieldParentPtr(GenHCmpOutputStep, "step", step);
10481041 const b = self.context.b;
10491042
1050 warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);
1043 warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name);
10511044
10521045 const full_h_path = b.pathFromRoot(self.h_path);
10531046 const actual_h = try io.readFileAlloc(b.allocator, full_h_path);
......@@ -1076,11 +1069,9 @@ pub const GenHContext = struct {
10761069 warn("\n");
10771070 }
10781071
1079 pub fn create(self: &GenHContext, filename: []const u8, name: []const u8,
1080 source: []const u8, expected_lines: ...) &TestCase
1081 {
1072 pub fn create(self: &GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) &TestCase {
10821073 const tc = self.b.allocator.create(TestCase) catch unreachable;
1083 *tc = TestCase {
1074 tc.* = TestCase{
10841075 .name = name,
10851076 .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator),
10861077 .expected_lines = ArrayList([]const u8).init(self.b.allocator),
......@@ -1105,8 +1096,7 @@ pub const GenHContext = struct {
11051096 const mode = builtin.Mode.Debug;
11061097 const annotated_case_name = fmt.allocPrint(self.b.allocator, "gen-h {} ({})", case.name, @tagName(mode)) catch unreachable;
11071098 if (self.test_filter) |filter| {
1108 if (mem.indexOf(u8, annotated_case_name, filter) == null)
1109 return;
1099 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
11101100 }
11111101
11121102 const obj = b.addObject("test", root_src);
test/translate_c.zig+50-50
......@@ -720,43 +720,43 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
720720 \\ var a: c_int = 0;
721721 \\ a += x: {
722722 \\ const _ref = &a;
723 \\ (*_ref) = ((*_ref) + 1);
724 \\ break :x *_ref;
723 \\ _ref.* = (_ref.* + 1);
724 \\ break :x _ref.*;
725725 \\ };
726726 \\ a -= x: {
727727 \\ const _ref = &a;
728 \\ (*_ref) = ((*_ref) - 1);
729 \\ break :x *_ref;
728 \\ _ref.* = (_ref.* - 1);
729 \\ break :x _ref.*;
730730 \\ };
731731 \\ a *= x: {
732732 \\ const _ref = &a;
733 \\ (*_ref) = ((*_ref) * 1);
734 \\ break :x *_ref;
733 \\ _ref.* = (_ref.* * 1);
734 \\ break :x _ref.*;
735735 \\ };
736736 \\ a &= x: {
737737 \\ const _ref = &a;
738 \\ (*_ref) = ((*_ref) & 1);
739 \\ break :x *_ref;
738 \\ _ref.* = (_ref.* & 1);
739 \\ break :x _ref.*;
740740 \\ };
741741 \\ a |= x: {
742742 \\ const _ref = &a;
743 \\ (*_ref) = ((*_ref) | 1);
744 \\ break :x *_ref;
743 \\ _ref.* = (_ref.* | 1);
744 \\ break :x _ref.*;
745745 \\ };
746746 \\ a ^= x: {
747747 \\ const _ref = &a;
748 \\ (*_ref) = ((*_ref) ^ 1);
749 \\ break :x *_ref;
748 \\ _ref.* = (_ref.* ^ 1);
749 \\ break :x _ref.*;
750750 \\ };
751751 \\ a >>= @import("std").math.Log2Int(c_int)(x: {
752752 \\ const _ref = &a;
753 \\ (*_ref) = ((*_ref) >> @import("std").math.Log2Int(c_int)(1));
754 \\ break :x *_ref;
753 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_int)(1));
754 \\ break :x _ref.*;
755755 \\ });
756756 \\ a <<= @import("std").math.Log2Int(c_int)(x: {
757757 \\ const _ref = &a;
758 \\ (*_ref) = ((*_ref) << @import("std").math.Log2Int(c_int)(1));
759 \\ break :x *_ref;
758 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_int)(1));
759 \\ break :x _ref.*;
760760 \\ });
761761 \\}
762762 );
......@@ -778,43 +778,43 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
778778 \\ var a: c_uint = c_uint(0);
779779 \\ a +%= x: {
780780 \\ const _ref = &a;
781 \\ (*_ref) = ((*_ref) +% c_uint(1));
782 \\ break :x *_ref;
781 \\ _ref.* = (_ref.* +% c_uint(1));
782 \\ break :x _ref.*;
783783 \\ };
784784 \\ a -%= x: {
785785 \\ const _ref = &a;
786 \\ (*_ref) = ((*_ref) -% c_uint(1));
787 \\ break :x *_ref;
786 \\ _ref.* = (_ref.* -% c_uint(1));
787 \\ break :x _ref.*;
788788 \\ };
789789 \\ a *%= x: {
790790 \\ const _ref = &a;
791 \\ (*_ref) = ((*_ref) *% c_uint(1));
792 \\ break :x *_ref;
791 \\ _ref.* = (_ref.* *% c_uint(1));
792 \\ break :x _ref.*;
793793 \\ };
794794 \\ a &= x: {
795795 \\ const _ref = &a;
796 \\ (*_ref) = ((*_ref) & c_uint(1));
797 \\ break :x *_ref;
796 \\ _ref.* = (_ref.* & c_uint(1));
797 \\ break :x _ref.*;
798798 \\ };
799799 \\ a |= x: {
800800 \\ const _ref = &a;
801 \\ (*_ref) = ((*_ref) | c_uint(1));
802 \\ break :x *_ref;
801 \\ _ref.* = (_ref.* | c_uint(1));
802 \\ break :x _ref.*;
803803 \\ };
804804 \\ a ^= x: {
805805 \\ const _ref = &a;
806 \\ (*_ref) = ((*_ref) ^ c_uint(1));
807 \\ break :x *_ref;
806 \\ _ref.* = (_ref.* ^ c_uint(1));
807 \\ break :x _ref.*;
808808 \\ };
809809 \\ a >>= @import("std").math.Log2Int(c_uint)(x: {
810810 \\ const _ref = &a;
811 \\ (*_ref) = ((*_ref) >> @import("std").math.Log2Int(c_uint)(1));
812 \\ break :x *_ref;
811 \\ _ref.* = (_ref.* >> @import("std").math.Log2Int(c_uint)(1));
812 \\ break :x _ref.*;
813813 \\ });
814814 \\ a <<= @import("std").math.Log2Int(c_uint)(x: {
815815 \\ const _ref = &a;
816 \\ (*_ref) = ((*_ref) << @import("std").math.Log2Int(c_uint)(1));
817 \\ break :x *_ref;
816 \\ _ref.* = (_ref.* << @import("std").math.Log2Int(c_uint)(1));
817 \\ break :x _ref.*;
818818 \\ });
819819 \\}
820820 );
......@@ -853,26 +853,26 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
853853 \\ u -%= 1;
854854 \\ i = x: {
855855 \\ const _ref = &i;
856 \\ const _tmp = *_ref;
857 \\ (*_ref) += 1;
856 \\ const _tmp = _ref.*;
857 \\ _ref.* += 1;
858858 \\ break :x _tmp;
859859 \\ };
860860 \\ i = x: {
861861 \\ const _ref = &i;
862 \\ const _tmp = *_ref;
863 \\ (*_ref) -= 1;
862 \\ const _tmp = _ref.*;
863 \\ _ref.* -= 1;
864864 \\ break :x _tmp;
865865 \\ };
866866 \\ u = x: {
867867 \\ const _ref = &u;
868 \\ const _tmp = *_ref;
869 \\ (*_ref) +%= 1;
868 \\ const _tmp = _ref.*;
869 \\ _ref.* +%= 1;
870870 \\ break :x _tmp;
871871 \\ };
872872 \\ u = x: {
873873 \\ const _ref = &u;
874 \\ const _tmp = *_ref;
875 \\ (*_ref) -%= 1;
874 \\ const _tmp = _ref.*;
875 \\ _ref.* -%= 1;
876876 \\ break :x _tmp;
877877 \\ };
878878 \\}
......@@ -901,23 +901,23 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
901901 \\ u -%= 1;
902902 \\ i = x: {
903903 \\ const _ref = &i;
904 \\ (*_ref) += 1;
905 \\ break :x *_ref;
904 \\ _ref.* += 1;
905 \\ break :x _ref.*;
906906 \\ };
907907 \\ i = x: {
908908 \\ const _ref = &i;
909 \\ (*_ref) -= 1;
910 \\ break :x *_ref;
909 \\ _ref.* -= 1;
910 \\ break :x _ref.*;
911911 \\ };
912912 \\ u = x: {
913913 \\ const _ref = &u;
914 \\ (*_ref) +%= 1;
915 \\ break :x *_ref;
914 \\ _ref.* +%= 1;
915 \\ break :x _ref.*;
916916 \\ };
917917 \\ u = x: {
918918 \\ const _ref = &u;
919 \\ (*_ref) -%= 1;
920 \\ break :x *_ref;
919 \\ _ref.* -%= 1;
920 \\ break :x _ref.*;
921921 \\ };
922922 \\}
923923 );
......@@ -985,7 +985,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
985985 \\}
986986 ,
987987 \\pub export fn foo(x: ?&c_int) void {
988 \\ (*??x) = 1;
988 \\ (??x).* = 1;
989989 \\}
990990 );
991991
......@@ -1013,7 +1013,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
10131013 \\pub fn foo() c_int {
10141014 \\ var x: c_int = 1234;
10151015 \\ var ptr: ?&c_int = &x;
1016 \\ return *??ptr;
1016 \\ return (??ptr).*;
10171017 \\}
10181018 );
10191019