| author | |
| committer | |
| log | 66717db735b9ddac9298bf08fcf95e7e11629fee |
| tree | 54dea549c62d851da9269b09eba8e596450ee330 |
| parent | de1f57926f212f18a98884fa8b1d0df7f7bc7f03 |
See #632
better fits the convention of using keywords for control flow41 files changed, 812 insertions(+), 803 deletions(-)
doc/home.html.in+12-12| ... | ... | @@ -75,10 +75,10 @@ |
| 75 | 75 | |
| 76 | 76 | pub fn main() -> %void { |
| 77 | 77 | // If this program is run without stdout attached, exit with an error. |
| 78 | var stdout_file = %return std.io.getStdOut(); | |
| 78 | var stdout_file = try std.io.getStdOut(); | |
| 79 | 79 | // If this program encounters pipe failure when printing to stdout, exit |
| 80 | 80 | // with an error. |
| 81 | %return stdout_file.write("Hello, world!\n"); | |
| 81 | try stdout_file.write("Hello, world!\n"); | |
| 82 | 82 | }</code></pre> |
| 83 | 83 | <p>Build this with:</p> |
| 84 | 84 | <pre>zig build-exe hello.zig</pre> |
| ... | ... | @@ -105,9 +105,9 @@ export fn main(argc: c_int, argv: &amp;&amp;u8) -&gt; c_int { |
| 105 | 105 | var x: T = 0; |
| 106 | 106 | |
| 107 | 107 | for (buf) |c| { |
| 108 | const digit = %return charToDigit(c, radix); | |
| 109 | x = %return mulOverflow(T, x, radix); | |
| 110 | x = %return addOverflow(T, x, digit); | |
| 108 | const digit = try charToDigit(c, radix); | |
| 109 | x = try mulOverflow(T, x, radix); | |
| 110 | x = try addOverflow(T, x, digit); | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | return x; |
| ... | ... | @@ -234,14 +234,14 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K)-&gt |
| 234 | 234 | |
| 235 | 235 | pub fn put(hm: &amp;Self, key: K, value: V) -&gt; %void { |
| 236 | 236 | if (hm.entries.len == 0) { |
| 237 | %return hm.initCapacity(16); | |
| 237 | try hm.initCapacity(16); | |
| 238 | 238 | } |
| 239 | 239 | hm.incrementModificationCount(); |
| 240 | 240 | |
| 241 | 241 | // if we get too full (60%), double the capacity |
| 242 | 242 | if (hm.size * 5 &gt;= hm.entries.len * 3) { |
| 243 | 243 | const old_entries = hm.entries; |
| 244 | %return hm.initCapacity(hm.entries.len * 2); | |
| 244 | try hm.initCapacity(hm.entries.len * 2); | |
| 245 | 245 | // dump all of the old elements into the new table |
| 246 | 246 | for (old_entries) |*old_entry| { |
| 247 | 247 | if (old_entry.used) { |
| ... | ... | @@ -296,7 +296,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn(key: K)-&gt |
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | fn initCapacity(hm: &amp;Self, capacity: usize) -&gt; %void { |
| 299 | hm.entries = %return hm.allocator.alloc(Entry, capacity); | |
| 299 | hm.entries = try hm.allocator.alloc(Entry, capacity); | |
| 300 | 300 | hm.size = 0; |
| 301 | 301 | hm.max_distance_from_start_index = 0; |
| 302 | 302 | for (hm.entries) |*entry| { |
| ... | ... | @@ -420,7 +420,7 @@ pub fn main() -&gt; %void { |
| 420 | 420 | const arg = os.args.at(arg_i); |
| 421 | 421 | if (mem.eql(u8, arg, "-")) { |
| 422 | 422 | catted_anything = true; |
| 423 | %return cat_stream(&amp;io.stdin); | |
| 423 | try cat_stream(&amp;io.stdin); | |
| 424 | 424 | } else if (arg[0] == '-') { |
| 425 | 425 | return usage(exe); |
| 426 | 426 | } else { |
| ... | ... | @@ -431,13 +431,13 @@ pub fn main() -&gt; %void { |
| 431 | 431 | defer is.close(); |
| 432 | 432 | |
| 433 | 433 | catted_anything = true; |
| 434 | %return cat_stream(&amp;is); | |
| 434 | try cat_stream(&amp;is); | |
| 435 | 435 | } |
| 436 | 436 | } |
| 437 | 437 | if (!catted_anything) { |
| 438 | %return cat_stream(&amp;io.stdin); | |
| 438 | try cat_stream(&amp;io.stdin); | |
| 439 | 439 | } |
| 440 | %return io.stdout.flush(); | |
| 440 | try io.stdout.flush(); | |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | fn usage(exe: []const u8) -&gt; %void { |
doc/langref.html.in+24-22| ... | ... | @@ -268,10 +268,10 @@ |
| 268 | 268 | |
| 269 | 269 | pub fn main() -&gt; %void { |
| 270 | 270 | // If this program is run without stdout attached, exit with an error. |
| 271 | var stdout_file = %return std.io.getStdOut(); | |
| 271 | var stdout_file = try std.io.getStdOut(); | |
| 272 | 272 | // If this program encounters pipe failure when printing to stdout, exit |
| 273 | 273 | // with an error. |
| 274 | %return stdout_file.write("Hello, world!\n"); | |
| 274 | try stdout_file.write("Hello, world!\n"); | |
| 275 | 275 | }</code></pre> |
| 276 | 276 | <pre><code class="sh">$ zig build-exe hello.zig |
| 277 | 277 | $ ./hello |
| ... | ... | @@ -3224,14 +3224,14 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 { |
| 3224 | 3224 | // ... |
| 3225 | 3225 | }</code></pre> |
| 3226 | 3226 | <p> |
| 3227 | There is a shortcut for this. The <code>%return</code> expression: | |
| 3227 | There is a shortcut for this. The <code>try</code> expression: | |
| 3228 | 3228 | </p> |
| 3229 | 3229 | <pre><code class="zig">fn doAThing(str: []u8) -&gt; %void { |
| 3230 | const number = %return parseU64(str, 10); | |
| 3230 | const number = try parseU64(str, 10); | |
| 3231 | 3231 | // ... |
| 3232 | 3232 | }</code></pre> |
| 3233 | 3233 | <p> |
| 3234 | <code>%return</code> evaluates an error union expression. If it is an error, it returns | |
| 3234 | <code>try</code> evaluates an error union expression. If it is an error, it returns | |
| 3235 | 3235 | from the current function with the same error. Otherwise, the expression results in |
| 3236 | 3236 | the unwrapped value. |
| 3237 | 3237 | </p> |
| ... | ... | @@ -3278,7 +3278,7 @@ pub fn parseU64(buf: []const u8, radix: u8) -&gt; %u64 { |
| 3278 | 3278 | Example: |
| 3279 | 3279 | </p> |
| 3280 | 3280 | <pre><code class="zig">fn createFoo(param: i32) -&gt; %Foo { |
| 3281 | const foo = %return tryToAllocateFoo(); | |
| 3281 | const foo = try tryToAllocateFoo(); | |
| 3282 | 3282 | // now we have allocated foo. we need to free it if the function fails. |
| 3283 | 3283 | // but we want to return it if the function succeeds. |
| 3284 | 3284 | %defer deallocateFoo(foo); |
| ... | ... | @@ -3928,11 +3928,11 @@ pub fn printf(self: &amp;OutStream, comptime format: []const u8, args: ...) -&gt |
| 3928 | 3928 | switch (state) { |
| 3929 | 3929 | State.Start =&gt; switch (c) { |
| 3930 | 3930 | '{' =&gt; { |
| 3931 | if (start_index &lt; i) %return self.write(format[start_index...i]); | |
| 3931 | if (start_index &lt; i) try self.write(format[start_index...i]); | |
| 3932 | 3932 | state = State.OpenBrace; |
| 3933 | 3933 | }, |
| 3934 | 3934 | '}' =&gt; { |
| 3935 | if (start_index &lt; i) %return self.write(format[start_index...i]); | |
| 3935 | if (start_index &lt; i) try self.write(format[start_index...i]); | |
| 3936 | 3936 | state = State.CloseBrace; |
| 3937 | 3937 | }, |
| 3938 | 3938 | else =&gt; {}, |
| ... | ... | @@ -3943,7 +3943,7 @@ pub fn printf(self: &amp;OutStream, comptime format: []const u8, args: ...) -&gt |
| 3943 | 3943 | start_index = i; |
| 3944 | 3944 | }, |
| 3945 | 3945 | '}' =&gt; { |
| 3946 | %return self.printValue(args[next_arg]); | |
| 3946 | try self.printValue(args[next_arg]); | |
| 3947 | 3947 | next_arg += 1; |
| 3948 | 3948 | state = State.Start; |
| 3949 | 3949 | start_index = i + 1; |
| ... | ... | @@ -3968,9 +3968,9 @@ pub fn printf(self: &amp;OutStream, comptime format: []const u8, args: ...) -&gt |
| 3968 | 3968 | } |
| 3969 | 3969 | } |
| 3970 | 3970 | if (start_index &lt; format.len) { |
| 3971 | %return self.write(format[start_index...format.len]); | |
| 3971 | try self.write(format[start_index...format.len]); | |
| 3972 | 3972 | } |
| 3973 | %return self.flush(); | |
| 3973 | try self.flush(); | |
| 3974 | 3974 | }</code></pre> |
| 3975 | 3975 | <p> |
| 3976 | 3976 | This is a proof of concept implementation; the actual function in the standard library has more |
| ... | ... | @@ -3984,12 +3984,12 @@ pub fn printf(self: &amp;OutStream, comptime format: []const u8, args: ...) -&gt |
| 3984 | 3984 | and emits a function that actually looks like this: |
| 3985 | 3985 | </p> |
| 3986 | 3986 | <pre><code class="zig">pub fn printf(self: &amp;OutStream, arg0: i32, arg1: []const u8) -&gt; %void { |
| 3987 | %return self.write("here is a string: '"); | |
| 3988 | %return self.printValue(arg0); | |
| 3989 | %return self.write("' here is a number: "); | |
| 3990 | %return self.printValue(arg1); | |
| 3991 | %return self.write("\n"); | |
| 3992 | %return self.flush(); | |
| 3987 | try self.write("here is a string: '"); | |
| 3988 | try self.printValue(arg0); | |
| 3989 | try self.write("' here is a number: "); | |
| 3990 | try self.printValue(arg1); | |
| 3991 | try self.write("\n"); | |
| 3992 | try self.flush(); | |
| 3993 | 3993 | }</code></pre> |
| 3994 | 3994 | <p> |
| 3995 | 3995 | <code>printValue</code> is a function that takes a parameter of any type, and does different things depending |
| ... | ... | @@ -5891,7 +5891,7 @@ TypeExpr = PrefixOpExpression | "var" |
| 5891 | 5891 | |
| 5892 | 5892 | BlockOrExpression = Block | Expression |
| 5893 | 5893 | |
| 5894 | Expression = ReturnExpression | BreakExpression | AssignmentExpression | |
| 5894 | Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | |
| 5895 | 5895 | |
| 5896 | 5896 | AsmExpression = "asm" option("volatile") "(" String option(AsmOutput) ")" |
| 5897 | 5897 | |
| ... | ... | @@ -5915,7 +5915,7 @@ AssignmentExpression = UnwrapExpression AssignmentOperator UnwrapExpression | Un |
| 5915 | 5915 | |
| 5916 | 5916 | AssignmentOperator = "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "&lt;&lt;=" | "&gt;&gt;=" | "&amp;=" | "^=" | "|=" | "*%=" | "+%=" | "-%=" |
| 5917 | 5917 | |
| 5918 | BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | |
| 5918 | BlockExpression(body) = Block | IfExpression(body) | IfErrorExpression(body) | TestExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body) | |
| 5919 | 5919 | |
| 5920 | 5920 | CompTimeExpression(body) = "comptime" body |
| 5921 | 5921 | |
| ... | ... | @@ -5929,7 +5929,9 @@ ForExpression(body) = option(Symbol ":") option("inline") "for" "(" Expression " |
| 5929 | 5929 | |
| 5930 | 5930 | BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression |
| 5931 | 5931 | |
| 5932 | ReturnExpression = option("%") "return" option(Expression) | |
| 5932 | ReturnExpression = "return" option(Expression) | |
| 5933 | ||
| 5934 | TryExpression = "try" Expression | |
| 5933 | 5935 | |
| 5934 | 5936 | BreakExpression = "break" option(":" Symbol) option(Expression) |
| 5935 | 5937 | |
| ... | ... | @@ -5937,7 +5939,7 @@ Defer(body) = option("%") "defer" body |
| 5937 | 5939 | |
| 5938 | 5940 | IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body)) |
| 5939 | 5941 | |
| 5940 | TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body) | |
| 5942 | IfErrorExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body) | |
| 5941 | 5943 | |
| 5942 | 5944 | TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body)) |
| 5943 | 5945 | |
| ... | ... | @@ -5987,7 +5989,7 @@ ContainerInitBody = list(StructLiteralField, ",") | list(Expression, ",") |
| 5987 | 5989 | |
| 5988 | 5990 | StructLiteralField = "." Symbol "=" Expression |
| 5989 | 5991 | |
| 5990 | PrefixOp = "!" | "-" | "~" | "*" | ("&amp;" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | |
| 5992 | PrefixOp = "!" | "-" | "~" | "*" | ("&amp;" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | "try" | |
| 5991 | 5993 | |
| 5992 | 5994 | PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." Symbol) | ContainerDecl | ("continue" option(":" Symbol)) |
| 5993 | 5995 |
example/cat/main.zig+8-8| ... | ... | @@ -7,16 +7,16 @@ const allocator = std.debug.global_allocator; |
| 7 | 7 | |
| 8 | 8 | pub fn main() -> %void { |
| 9 | 9 | var args_it = os.args(); |
| 10 | const exe = %return unwrapArg(??args_it.next(allocator)); | |
| 10 | const exe = try unwrapArg(??args_it.next(allocator)); | |
| 11 | 11 | var catted_anything = false; |
| 12 | var stdout_file = %return io.getStdOut(); | |
| 12 | var stdout_file = try io.getStdOut(); | |
| 13 | 13 | |
| 14 | 14 | while (args_it.next(allocator)) |arg_or_err| { |
| 15 | const arg = %return unwrapArg(arg_or_err); | |
| 15 | const arg = try unwrapArg(arg_or_err); | |
| 16 | 16 | if (mem.eql(u8, arg, "-")) { |
| 17 | 17 | catted_anything = true; |
| 18 | var stdin_file = %return io.getStdIn(); | |
| 19 | %return cat_file(&stdout_file, &stdin_file); | |
| 18 | var stdin_file = try io.getStdIn(); | |
| 19 | try cat_file(&stdout_file, &stdin_file); | |
| 20 | 20 | } else if (arg[0] == '-') { |
| 21 | 21 | return usage(exe); |
| 22 | 22 | } else { |
| ... | ... | @@ -27,12 +27,12 @@ pub fn main() -> %void { |
| 27 | 27 | defer file.close(); |
| 28 | 28 | |
| 29 | 29 | catted_anything = true; |
| 30 | %return cat_file(&stdout_file, &file); | |
| 30 | try cat_file(&stdout_file, &file); | |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | if (!catted_anything) { |
| 34 | var stdin_file = %return io.getStdIn(); | |
| 35 | %return cat_file(&stdout_file, &stdin_file); | |
| 34 | var stdin_file = try io.getStdIn(); | |
| 35 | try cat_file(&stdout_file, &stdin_file); | |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | 38 |
example/guess_number/main.zig+9-9| ... | ... | @@ -6,13 +6,13 @@ const Rand = std.rand.Rand; |
| 6 | 6 | const os = std.os; |
| 7 | 7 | |
| 8 | 8 | pub fn main() -> %void { |
| 9 | var stdout_file = %return io.getStdOut(); | |
| 9 | var stdout_file = try io.getStdOut(); | |
| 10 | 10 | var stdout_file_stream = io.FileOutStream.init(&stdout_file); |
| 11 | 11 | const stdout = &stdout_file_stream.stream; |
| 12 | 12 | |
| 13 | var stdin_file = %return io.getStdIn(); | |
| 13 | var stdin_file = try io.getStdIn(); | |
| 14 | 14 | |
| 15 | %return stdout.print("Welcome to the Guess Number Game in Zig.\n"); | |
| 15 | try stdout.print("Welcome to the Guess Number Game in Zig.\n"); | |
| 16 | 16 | |
| 17 | 17 | var seed_bytes: [@sizeOf(usize)]u8 = undefined; |
| 18 | 18 | %%os.getRandomBytes(seed_bytes[0..]); |
| ... | ... | @@ -22,24 +22,24 @@ pub fn main() -> %void { |
| 22 | 22 | const answer = rand.range(u8, 0, 100) + 1; |
| 23 | 23 | |
| 24 | 24 | while (true) { |
| 25 | %return stdout.print("\nGuess a number between 1 and 100: "); | |
| 25 | try stdout.print("\nGuess a number between 1 and 100: "); | |
| 26 | 26 | var line_buf : [20]u8 = undefined; |
| 27 | 27 | |
| 28 | 28 | const line_len = stdin_file.read(line_buf[0..]) %% |err| { |
| 29 | %return stdout.print("Unable to read from stdin: {}\n", @errorName(err)); | |
| 29 | try stdout.print("Unable to read from stdin: {}\n", @errorName(err)); | |
| 30 | 30 | return err; |
| 31 | 31 | }; |
| 32 | 32 | |
| 33 | 33 | const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) %% { |
| 34 | %return stdout.print("Invalid number.\n"); | |
| 34 | try stdout.print("Invalid number.\n"); | |
| 35 | 35 | continue; |
| 36 | 36 | }; |
| 37 | 37 | if (guess > answer) { |
| 38 | %return stdout.print("Guess lower.\n"); | |
| 38 | try stdout.print("Guess lower.\n"); | |
| 39 | 39 | } else if (guess < answer) { |
| 40 | %return stdout.print("Guess higher.\n"); | |
| 40 | try stdout.print("Guess higher.\n"); | |
| 41 | 41 | } else { |
| 42 | %return stdout.print("You win!\n"); | |
| 42 | try stdout.print("You win!\n"); | |
| 43 | 43 | return; |
| 44 | 44 | } |
| 45 | 45 | } |
example/hello_world/hello.zig+2-2| ... | ... | @@ -2,8 +2,8 @@ const std = @import("std"); |
| 2 | 2 | |
| 3 | 3 | pub fn main() -> %void { |
| 4 | 4 | // If this program is run without stdout attached, exit with an error. |
| 5 | var stdout_file = %return std.io.getStdOut(); | |
| 5 | var stdout_file = try std.io.getStdOut(); | |
| 6 | 6 | // If this program encounters pipe failure when printing to stdout, exit |
| 7 | 7 | // with an error. |
| 8 | %return stdout_file.write("Hello, world!\n"); | |
| 8 | try stdout_file.write("Hello, world!\n"); | |
| 9 | 9 | } |
src-self-hosted/main.zig+38-38| ... | ... | @@ -40,18 +40,18 @@ const Cmd = enum { |
| 40 | 40 | }; |
| 41 | 41 | |
| 42 | 42 | fn badArgs(comptime format: []const u8, args: ...) -> error { |
| 43 | var stderr = %return io.getStdErr(); | |
| 43 | var stderr = try io.getStdErr(); | |
| 44 | 44 | var stderr_stream_adapter = io.FileOutStream.init(&stderr); |
| 45 | 45 | const stderr_stream = &stderr_stream_adapter.stream; |
| 46 | %return stderr_stream.print(format ++ "\n\n", args); | |
| 47 | %return printUsage(&stderr_stream_adapter.stream); | |
| 46 | try stderr_stream.print(format ++ "\n\n", args); | |
| 47 | try printUsage(&stderr_stream_adapter.stream); | |
| 48 | 48 | return error.InvalidCommandLineArguments; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | pub fn main2() -> %void { |
| 52 | 52 | const allocator = std.heap.c_allocator; |
| 53 | 53 | |
| 54 | const args = %return os.argsAlloc(allocator); | |
| 54 | const args = try os.argsAlloc(allocator); | |
| 55 | 55 | defer os.argsFree(allocator, args); |
| 56 | 56 | |
| 57 | 57 | var cmd = Cmd.None; |
| ... | ... | @@ -167,7 +167,7 @@ pub fn main2() -> %void { |
| 167 | 167 | @panic("TODO --test-cmd-bin"); |
| 168 | 168 | } else if (arg[1] == 'L' and arg.len > 2) { |
| 169 | 169 | // alias for --library-path |
| 170 | %return lib_dirs.append(arg[1..]); | |
| 170 | try lib_dirs.append(arg[1..]); | |
| 171 | 171 | } else if (mem.eql(u8, arg, "--pkg-begin")) { |
| 172 | 172 | @panic("TODO --pkg-begin"); |
| 173 | 173 | } else if (mem.eql(u8, arg, "--pkg-end")) { |
| ... | ... | @@ -217,24 +217,24 @@ pub fn main2() -> %void { |
| 217 | 217 | } else if (mem.eql(u8, arg, "--dynamic-linker")) { |
| 218 | 218 | dynamic_linker_arg = args[arg_i]; |
| 219 | 219 | } else if (mem.eql(u8, arg, "-isystem")) { |
| 220 | %return clang_argv.append("-isystem"); | |
| 221 | %return clang_argv.append(args[arg_i]); | |
| 220 | try clang_argv.append("-isystem"); | |
| 221 | try clang_argv.append(args[arg_i]); | |
| 222 | 222 | } else if (mem.eql(u8, arg, "-dirafter")) { |
| 223 | %return clang_argv.append("-dirafter"); | |
| 224 | %return clang_argv.append(args[arg_i]); | |
| 223 | try clang_argv.append("-dirafter"); | |
| 224 | try clang_argv.append(args[arg_i]); | |
| 225 | 225 | } else if (mem.eql(u8, arg, "-mllvm")) { |
| 226 | %return clang_argv.append("-mllvm"); | |
| 227 | %return clang_argv.append(args[arg_i]); | |
| 226 | try clang_argv.append("-mllvm"); | |
| 227 | try clang_argv.append(args[arg_i]); | |
| 228 | 228 | |
| 229 | %return llvm_argv.append(args[arg_i]); | |
| 229 | try llvm_argv.append(args[arg_i]); | |
| 230 | 230 | } else if (mem.eql(u8, arg, "--library-path") or mem.eql(u8, arg, "-L")) { |
| 231 | %return lib_dirs.append(args[arg_i]); | |
| 231 | try lib_dirs.append(args[arg_i]); | |
| 232 | 232 | } else if (mem.eql(u8, arg, "--library")) { |
| 233 | %return link_libs.append(args[arg_i]); | |
| 233 | try link_libs.append(args[arg_i]); | |
| 234 | 234 | } else if (mem.eql(u8, arg, "--object")) { |
| 235 | %return objects.append(args[arg_i]); | |
| 235 | try objects.append(args[arg_i]); | |
| 236 | 236 | } else if (mem.eql(u8, arg, "--assembly")) { |
| 237 | %return asm_files.append(args[arg_i]); | |
| 237 | try asm_files.append(args[arg_i]); | |
| 238 | 238 | } else if (mem.eql(u8, arg, "--cache-dir")) { |
| 239 | 239 | cache_dir_arg = args[arg_i]; |
| 240 | 240 | } else if (mem.eql(u8, arg, "--target-arch")) { |
| ... | ... | @@ -248,21 +248,21 @@ pub fn main2() -> %void { |
| 248 | 248 | } else if (mem.eql(u8, arg, "-mios-version-min")) { |
| 249 | 249 | mios_version_min = args[arg_i]; |
| 250 | 250 | } else if (mem.eql(u8, arg, "-framework")) { |
| 251 | %return frameworks.append(args[arg_i]); | |
| 251 | try frameworks.append(args[arg_i]); | |
| 252 | 252 | } else if (mem.eql(u8, arg, "--linker-script")) { |
| 253 | 253 | linker_script_arg = args[arg_i]; |
| 254 | 254 | } else if (mem.eql(u8, arg, "-rpath")) { |
| 255 | %return rpath_list.append(args[arg_i]); | |
| 255 | try rpath_list.append(args[arg_i]); | |
| 256 | 256 | } else if (mem.eql(u8, arg, "--test-filter")) { |
| 257 | %return test_filters.append(args[arg_i]); | |
| 257 | try test_filters.append(args[arg_i]); | |
| 258 | 258 | } else if (mem.eql(u8, arg, "--test-name-prefix")) { |
| 259 | 259 | test_name_prefix_arg = args[arg_i]; |
| 260 | 260 | } else if (mem.eql(u8, arg, "--ver-major")) { |
| 261 | ver_major = %return std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 261 | ver_major = try std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 262 | 262 | } else if (mem.eql(u8, arg, "--ver-minor")) { |
| 263 | ver_minor = %return std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 263 | ver_minor = try std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 264 | 264 | } else if (mem.eql(u8, arg, "--ver-patch")) { |
| 265 | ver_patch = %return std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 265 | ver_patch = try std.fmt.parseUnsigned(u32, args[arg_i], 10); | |
| 266 | 266 | } else if (mem.eql(u8, arg, "--test-cmd")) { |
| 267 | 267 | @panic("TODO --test-cmd"); |
| 268 | 268 | } else { |
| ... | ... | @@ -367,13 +367,13 @@ pub fn main2() -> %void { |
| 367 | 367 | const zig_root_source_file = if (cmd == Cmd.TranslateC) null else in_file_arg; |
| 368 | 368 | |
| 369 | 369 | const chosen_cache_dir = cache_dir_arg ?? default_zig_cache_name; |
| 370 | const full_cache_dir = %return os.path.resolve(allocator, ".", chosen_cache_dir); | |
| 370 | const full_cache_dir = try os.path.resolve(allocator, ".", chosen_cache_dir); | |
| 371 | 371 | defer allocator.free(full_cache_dir); |
| 372 | 372 | |
| 373 | const zig_lib_dir = %return resolveZigLibDir(allocator, zig_install_prefix); | |
| 373 | const zig_lib_dir = try resolveZigLibDir(allocator, zig_install_prefix); | |
| 374 | 374 | %defer allocator.free(zig_lib_dir); |
| 375 | 375 | |
| 376 | const module = %return Module.create(allocator, root_name, zig_root_source_file, | |
| 376 | const module = try Module.create(allocator, root_name, zig_root_source_file, | |
| 377 | 377 | Target.Native, build_kind, build_mode, zig_lib_dir, full_cache_dir); |
| 378 | 378 | defer module.destroy(); |
| 379 | 379 | |
| ... | ... | @@ -424,7 +424,7 @@ pub fn main2() -> %void { |
| 424 | 424 | module.rpath_list = rpath_list.toSliceConst(); |
| 425 | 425 | |
| 426 | 426 | for (link_libs.toSliceConst()) |name| { |
| 427 | _ = %return module.addLinkLib(name, true); | |
| 427 | _ = try module.addLinkLib(name, true); | |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | module.windows_subsystem_windows = mwindows; |
| ... | ... | @@ -455,8 +455,8 @@ pub fn main2() -> %void { |
| 455 | 455 | module.link_objects = objects.toSliceConst(); |
| 456 | 456 | module.assembly_files = asm_files.toSliceConst(); |
| 457 | 457 | |
| 458 | %return module.build(); | |
| 459 | %return module.link(out_file); | |
| 458 | try module.build(); | |
| 459 | try module.link(out_file); | |
| 460 | 460 | }, |
| 461 | 461 | Cmd.TranslateC => @panic("TODO translate-c"), |
| 462 | 462 | Cmd.Test => @panic("TODO test cmd"), |
| ... | ... | @@ -464,16 +464,16 @@ pub fn main2() -> %void { |
| 464 | 464 | } |
| 465 | 465 | }, |
| 466 | 466 | Cmd.Version => { |
| 467 | var stdout_file = %return io.getStdErr(); | |
| 468 | %return stdout_file.write(std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); | |
| 469 | %return stdout_file.write("\n"); | |
| 467 | var stdout_file = try io.getStdErr(); | |
| 468 | try stdout_file.write(std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); | |
| 469 | try stdout_file.write("\n"); | |
| 470 | 470 | }, |
| 471 | 471 | Cmd.Targets => @panic("TODO zig targets"), |
| 472 | 472 | } |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | fn printUsage(stream: &io.OutStream) -> %void { |
| 476 | %return stream.write( | |
| 476 | try stream.write( | |
| 477 | 477 | \\Usage: zig [command] [options] |
| 478 | 478 | \\ |
| 479 | 479 | \\Commands: |
| ... | ... | @@ -549,8 +549,8 @@ fn printUsage(stream: &io.OutStream) -> %void { |
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | fn printZen() -> %void { |
| 552 | var stdout_file = %return io.getStdErr(); | |
| 553 | %return stdout_file.write( | |
| 552 | var stdout_file = try io.getStdErr(); | |
| 553 | try stdout_file.write( | |
| 554 | 554 | \\ |
| 555 | 555 | \\ * Communicate intent precisely. |
| 556 | 556 | \\ * Edge cases matter. |
| ... | ... | @@ -586,13 +586,13 @@ fn resolveZigLibDir(allocator: &mem.Allocator, zig_install_prefix_arg: ?[]const |
| 586 | 586 | |
| 587 | 587 | /// Caller must free result |
| 588 | 588 | fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) -> %[]u8 { |
| 589 | const test_zig_dir = %return os.path.join(allocator, test_path, "lib", "zig"); | |
| 589 | const test_zig_dir = try os.path.join(allocator, test_path, "lib", "zig"); | |
| 590 | 590 | %defer allocator.free(test_zig_dir); |
| 591 | 591 | |
| 592 | const test_index_file = %return os.path.join(allocator, test_zig_dir, "std", "index.zig"); | |
| 592 | const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig"); | |
| 593 | 593 | defer allocator.free(test_index_file); |
| 594 | 594 | |
| 595 | var file = %return io.File.openRead(test_index_file, allocator); | |
| 595 | var file = try io.File.openRead(test_index_file, allocator); | |
| 596 | 596 | file.close(); |
| 597 | 597 | |
| 598 | 598 | return test_zig_dir; |
| ... | ... | @@ -600,7 +600,7 @@ fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) -> %[] |
| 600 | 600 | |
| 601 | 601 | /// Caller must free result |
| 602 | 602 | fn findZigLibDir(allocator: &mem.Allocator) -> %[]u8 { |
| 603 | const self_exe_path = %return os.selfExeDirPath(allocator); | |
| 603 | const self_exe_path = try os.selfExeDirPath(allocator); | |
| 604 | 604 | defer allocator.free(self_exe_path); |
| 605 | 605 | |
| 606 | 606 | var cur_path: []const u8 = self_exe_path; |
src-self-hosted/module.zig+13-13| ... | ... | @@ -112,7 +112,7 @@ pub const Module = struct { |
| 112 | 112 | pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target, |
| 113 | 113 | kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) -> %&Module |
| 114 | 114 | { |
| 115 | var name_buffer = %return Buffer.init(allocator, name); | |
| 115 | var name_buffer = try Buffer.init(allocator, name); | |
| 116 | 116 | %defer name_buffer.deinit(); |
| 117 | 117 | |
| 118 | 118 | const context = c.LLVMContextCreate() ?? return error.OutOfMemory; |
| ... | ... | @@ -124,7 +124,7 @@ pub const Module = struct { |
| 124 | 124 | const builder = c.LLVMCreateBuilderInContext(context) ?? return error.OutOfMemory; |
| 125 | 125 | %defer c.LLVMDisposeBuilder(builder); |
| 126 | 126 | |
| 127 | const module_ptr = %return allocator.create(Module); | |
| 127 | const module_ptr = try allocator.create(Module); | |
| 128 | 128 | %defer allocator.destroy(module_ptr); |
| 129 | 129 | |
| 130 | 130 | *module_ptr = Module { |
| ... | ... | @@ -200,7 +200,7 @@ pub const Module = struct { |
| 200 | 200 | |
| 201 | 201 | pub fn build(self: &Module) -> %void { |
| 202 | 202 | if (self.llvm_argv.len != 0) { |
| 203 | var c_compatible_args = %return std.cstr.NullTerminated2DArray.fromSlices(self.allocator, | |
| 203 | var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(self.allocator, | |
| 204 | 204 | [][]const []const u8 { [][]const u8{"zig (LLVM option parsing)"}, self.llvm_argv, }); |
| 205 | 205 | defer c_compatible_args.deinit(); |
| 206 | 206 | c.ZigLLVMParseCommandLineOptions(self.llvm_argv.len + 1, c_compatible_args.ptr); |
| ... | ... | @@ -208,13 +208,13 @@ pub const Module = struct { |
| 208 | 208 | |
| 209 | 209 | const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path"); |
| 210 | 210 | const root_src_real_path = os.path.real(self.allocator, root_src_path) %% |err| { |
| 211 | %return printError("unable to get real path '{}': {}", root_src_path, err); | |
| 211 | try printError("unable to get real path '{}': {}", root_src_path, err); | |
| 212 | 212 | return err; |
| 213 | 213 | }; |
| 214 | 214 | %defer self.allocator.free(root_src_real_path); |
| 215 | 215 | |
| 216 | 216 | const source_code = io.readFileAllocExtra(root_src_real_path, self.allocator, 3) %% |err| { |
| 217 | %return printError("unable to open '{}': {}", root_src_real_path, err); | |
| 217 | try printError("unable to open '{}': {}", root_src_real_path, err); | |
| 218 | 218 | return err; |
| 219 | 219 | }; |
| 220 | 220 | %defer self.allocator.free(source_code); |
| ... | ... | @@ -244,16 +244,16 @@ pub const Module = struct { |
| 244 | 244 | var parser = Parser.init(&tokenizer, self.allocator, root_src_real_path); |
| 245 | 245 | defer parser.deinit(); |
| 246 | 246 | |
| 247 | const root_node = %return parser.parse(); | |
| 247 | const root_node = try parser.parse(); | |
| 248 | 248 | defer parser.freeAst(root_node); |
| 249 | 249 | |
| 250 | var stderr_file = %return std.io.getStdErr(); | |
| 250 | var stderr_file = try std.io.getStdErr(); | |
| 251 | 251 | var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file); |
| 252 | 252 | const out_stream = &stderr_file_out_stream.stream; |
| 253 | %return parser.renderAst(out_stream, root_node); | |
| 253 | try parser.renderAst(out_stream, root_node); | |
| 254 | 254 | |
| 255 | 255 | warn("====fmt:====\n"); |
| 256 | %return parser.renderSource(out_stream, root_node); | |
| 256 | try parser.renderSource(out_stream, root_node); | |
| 257 | 257 | |
| 258 | 258 | warn("====ir:====\n"); |
| 259 | 259 | warn("TODO\n\n"); |
| ... | ... | @@ -282,14 +282,14 @@ pub const Module = struct { |
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | const link_lib = %return self.allocator.create(LinkLib); | |
| 285 | const link_lib = try self.allocator.create(LinkLib); | |
| 286 | 286 | *link_lib = LinkLib { |
| 287 | 287 | .name = name, |
| 288 | 288 | .path = null, |
| 289 | 289 | .provided_explicitly = provided_explicitly, |
| 290 | 290 | .symbols = ArrayList([]u8).init(self.allocator), |
| 291 | 291 | }; |
| 292 | %return self.link_libs_list.append(link_lib); | |
| 292 | try self.link_libs_list.append(link_lib); | |
| 293 | 293 | if (is_libc) { |
| 294 | 294 | self.libc_link_lib = link_lib; |
| 295 | 295 | } |
| ... | ... | @@ -298,8 +298,8 @@ pub const Module = struct { |
| 298 | 298 | }; |
| 299 | 299 | |
| 300 | 300 | fn printError(comptime format: []const u8, args: ...) -> %void { |
| 301 | var stderr_file = %return std.io.getStdErr(); | |
| 301 | var stderr_file = try std.io.getStdErr(); | |
| 302 | 302 | var stderr_file_out_stream = std.io.FileOutStream.init(&stderr_file); |
| 303 | 303 | const out_stream = &stderr_file_out_stream.stream; |
| 304 | %return out_stream.print(format, args); | |
| 304 | try out_stream.print(format, args); | |
| 305 | 305 | } |
src-self-hosted/parser.zig+149-149| ... | ... | @@ -58,7 +58,7 @@ pub const Parser = struct { |
| 58 | 58 | switch (*self) { |
| 59 | 59 | DestPtr.Field => |ptr| *ptr = value, |
| 60 | 60 | DestPtr.NullableField => |ptr| *ptr = value, |
| 61 | DestPtr.List => |list| %return list.append(value), | |
| 61 | DestPtr.List => |list| try list.append(value), | |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | }; |
| ... | ... | @@ -126,10 +126,10 @@ pub const Parser = struct { |
| 126 | 126 | defer self.deinitUtilityArrayList(stack); |
| 127 | 127 | |
| 128 | 128 | const root_node = x: { |
| 129 | const root_node = %return self.createRoot(); | |
| 129 | const root_node = try self.createRoot(); | |
| 130 | 130 | %defer self.allocator.destroy(root_node); |
| 131 | 131 | // This stack append has to succeed for freeAst to work |
| 132 | %return stack.append(State.TopLevel); | |
| 132 | try stack.append(State.TopLevel); | |
| 133 | 133 | break :x root_node; |
| 134 | 134 | }; |
| 135 | 135 | assert(self.cleanup_root_node == null); |
| ... | ... | @@ -194,18 +194,18 @@ pub const Parser = struct { |
| 194 | 194 | Token.Id.Keyword_var, Token.Id.Keyword_const => { |
| 195 | 195 | stack.append(State.TopLevel) %% unreachable; |
| 196 | 196 | // TODO shouldn't need these casts |
| 197 | const var_decl_node = %return self.createAttachVarDecl(&root_node.decls, ctx.visib_token, | |
| 197 | const var_decl_node = try self.createAttachVarDecl(&root_node.decls, ctx.visib_token, | |
| 198 | 198 | token, (?Token)(null), ctx.extern_token); |
| 199 | %return stack.append(State { .VarDecl = var_decl_node }); | |
| 199 | try stack.append(State { .VarDecl = var_decl_node }); | |
| 200 | 200 | continue; |
| 201 | 201 | }, |
| 202 | 202 | Token.Id.Keyword_fn => { |
| 203 | 203 | stack.append(State.TopLevel) %% unreachable; |
| 204 | 204 | // TODO shouldn't need these casts |
| 205 | const fn_proto = %return self.createAttachFnProto(&root_node.decls, token, | |
| 205 | const fn_proto = try self.createAttachFnProto(&root_node.decls, token, | |
| 206 | 206 | ctx.extern_token, (?Token)(null), (?Token)(null), (?Token)(null)); |
| 207 | %return stack.append(State { .FnDef = fn_proto }); | |
| 208 | %return stack.append(State { .FnProto = fn_proto }); | |
| 207 | try stack.append(State { .FnDef = fn_proto }); | |
| 208 | try stack.append(State { .FnProto = fn_proto }); | |
| 209 | 209 | continue; |
| 210 | 210 | }, |
| 211 | 211 | Token.Id.StringLiteral => { |
| ... | ... | @@ -213,24 +213,24 @@ pub const Parser = struct { |
| 213 | 213 | }, |
| 214 | 214 | Token.Id.Keyword_coldcc, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => { |
| 215 | 215 | stack.append(State.TopLevel) %% unreachable; |
| 216 | const fn_token = %return self.eatToken(Token.Id.Keyword_fn); | |
| 216 | const fn_token = try self.eatToken(Token.Id.Keyword_fn); | |
| 217 | 217 | // TODO shouldn't need this cast |
| 218 | const fn_proto = %return self.createAttachFnProto(&root_node.decls, fn_token, | |
| 218 | const fn_proto = try self.createAttachFnProto(&root_node.decls, fn_token, | |
| 219 | 219 | ctx.extern_token, (?Token)(token), (?Token)(null), (?Token)(null)); |
| 220 | %return stack.append(State { .FnDef = fn_proto }); | |
| 221 | %return stack.append(State { .FnProto = fn_proto }); | |
| 220 | try stack.append(State { .FnDef = fn_proto }); | |
| 221 | try stack.append(State { .FnProto = fn_proto }); | |
| 222 | 222 | continue; |
| 223 | 223 | }, |
| 224 | 224 | else => return self.parseError(token, "expected variable declaration or function, found {}", @tagName(token.id)), |
| 225 | 225 | } |
| 226 | 226 | }, |
| 227 | 227 | State.VarDecl => |var_decl| { |
| 228 | var_decl.name_token = %return self.eatToken(Token.Id.Identifier); | |
| 228 | var_decl.name_token = try self.eatToken(Token.Id.Identifier); | |
| 229 | 229 | stack.append(State { .VarDeclAlign = var_decl }) %% unreachable; |
| 230 | 230 | |
| 231 | 231 | const next_token = self.getNextToken(); |
| 232 | 232 | if (next_token.id == Token.Id.Colon) { |
| 233 | %return stack.append(State { .TypeExpr = DestPtr {.NullableField = &var_decl.type_node} }); | |
| 233 | try stack.append(State { .TypeExpr = DestPtr {.NullableField = &var_decl.type_node} }); | |
| 234 | 234 | continue; |
| 235 | 235 | } |
| 236 | 236 | |
| ... | ... | @@ -242,9 +242,9 @@ pub const Parser = struct { |
| 242 | 242 | |
| 243 | 243 | const next_token = self.getNextToken(); |
| 244 | 244 | if (next_token.id == Token.Id.Keyword_align) { |
| 245 | _ = %return self.eatToken(Token.Id.LParen); | |
| 246 | %return stack.append(State { .ExpectToken = Token.Id.RParen }); | |
| 247 | %return stack.append(State { .Expression = DestPtr{.NullableField = &var_decl.align_node} }); | |
| 245 | _ = try self.eatToken(Token.Id.LParen); | |
| 246 | try stack.append(State { .ExpectToken = Token.Id.RParen }); | |
| 247 | try stack.append(State { .Expression = DestPtr{.NullableField = &var_decl.align_node} }); | |
| 248 | 248 | continue; |
| 249 | 249 | } |
| 250 | 250 | |
| ... | ... | @@ -256,7 +256,7 @@ pub const Parser = struct { |
| 256 | 256 | if (token.id == Token.Id.Equal) { |
| 257 | 257 | var_decl.eq_token = token; |
| 258 | 258 | stack.append(State { .ExpectToken = Token.Id.Semicolon }) %% unreachable; |
| 259 | %return stack.append(State { | |
| 259 | try stack.append(State { | |
| 260 | 260 | .Expression = DestPtr {.NullableField = &var_decl.init_node}, |
| 261 | 261 | }); |
| 262 | 262 | continue; |
| ... | ... | @@ -267,14 +267,14 @@ pub const Parser = struct { |
| 267 | 267 | return self.parseError(token, "expected '=' or ';', found {}", @tagName(token.id)); |
| 268 | 268 | }, |
| 269 | 269 | State.ExpectToken => |token_id| { |
| 270 | _ = %return self.eatToken(token_id); | |
| 270 | _ = try self.eatToken(token_id); | |
| 271 | 271 | continue; |
| 272 | 272 | }, |
| 273 | 273 | |
| 274 | 274 | State.Expression => |dest_ptr| { |
| 275 | 275 | // save the dest_ptr for later |
| 276 | 276 | stack.append(state) %% unreachable; |
| 277 | %return stack.append(State.ExpectOperand); | |
| 277 | try stack.append(State.ExpectOperand); | |
| 278 | 278 | continue; |
| 279 | 279 | }, |
| 280 | 280 | State.ExpectOperand => { |
| ... | ... | @@ -283,13 +283,13 @@ pub const Parser = struct { |
| 283 | 283 | const token = self.getNextToken(); |
| 284 | 284 | switch (token.id) { |
| 285 | 285 | Token.Id.Keyword_return => { |
| 286 | %return stack.append(State { .PrefixOp = %return self.createPrefixOp(token, | |
| 286 | try stack.append(State { .PrefixOp = try self.createPrefixOp(token, | |
| 287 | 287 | ast.NodePrefixOp.PrefixOp.Return) }); |
| 288 | %return stack.append(State.ExpectOperand); | |
| 288 | try stack.append(State.ExpectOperand); | |
| 289 | 289 | continue; |
| 290 | 290 | }, |
| 291 | 291 | Token.Id.Ampersand => { |
| 292 | const prefix_op = %return self.createPrefixOp(token, ast.NodePrefixOp.PrefixOp{ | |
| 292 | const prefix_op = try self.createPrefixOp(token, ast.NodePrefixOp.PrefixOp{ | |
| 293 | 293 | .AddrOf = ast.NodePrefixOp.AddrOfInfo { |
| 294 | 294 | .align_expr = null, |
| 295 | 295 | .bit_offset_start_token = null, |
| ... | ... | @@ -298,30 +298,30 @@ pub const Parser = struct { |
| 298 | 298 | .volatile_token = null, |
| 299 | 299 | } |
| 300 | 300 | }); |
| 301 | %return stack.append(State { .PrefixOp = prefix_op }); | |
| 302 | %return stack.append(State.ExpectOperand); | |
| 303 | %return stack.append(State { .AddrOfModifiers = &prefix_op.op.AddrOf }); | |
| 301 | try stack.append(State { .PrefixOp = prefix_op }); | |
| 302 | try stack.append(State.ExpectOperand); | |
| 303 | try stack.append(State { .AddrOfModifiers = &prefix_op.op.AddrOf }); | |
| 304 | 304 | continue; |
| 305 | 305 | }, |
| 306 | 306 | Token.Id.Identifier => { |
| 307 | %return stack.append(State { | |
| 308 | .Operand = &(%return self.createIdentifier(token)).base | |
| 307 | try stack.append(State { | |
| 308 | .Operand = &(try self.createIdentifier(token)).base | |
| 309 | 309 | }); |
| 310 | %return stack.append(State.AfterOperand); | |
| 310 | try stack.append(State.AfterOperand); | |
| 311 | 311 | continue; |
| 312 | 312 | }, |
| 313 | 313 | Token.Id.IntegerLiteral => { |
| 314 | %return stack.append(State { | |
| 315 | .Operand = &(%return self.createIntegerLiteral(token)).base | |
| 314 | try stack.append(State { | |
| 315 | .Operand = &(try self.createIntegerLiteral(token)).base | |
| 316 | 316 | }); |
| 317 | %return stack.append(State.AfterOperand); | |
| 317 | try stack.append(State.AfterOperand); | |
| 318 | 318 | continue; |
| 319 | 319 | }, |
| 320 | 320 | Token.Id.FloatLiteral => { |
| 321 | %return stack.append(State { | |
| 322 | .Operand = &(%return self.createFloatLiteral(token)).base | |
| 321 | try stack.append(State { | |
| 322 | .Operand = &(try self.createFloatLiteral(token)).base | |
| 323 | 323 | }); |
| 324 | %return stack.append(State.AfterOperand); | |
| 324 | try stack.append(State.AfterOperand); | |
| 325 | 325 | continue; |
| 326 | 326 | }, |
| 327 | 327 | else => return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)), |
| ... | ... | @@ -335,17 +335,17 @@ pub const Parser = struct { |
| 335 | 335 | var token = self.getNextToken(); |
| 336 | 336 | switch (token.id) { |
| 337 | 337 | Token.Id.EqualEqual => { |
| 338 | %return stack.append(State { | |
| 339 | .InfixOp = %return self.createInfixOp(token, ast.NodeInfixOp.InfixOp.EqualEqual) | |
| 338 | try stack.append(State { | |
| 339 | .InfixOp = try self.createInfixOp(token, ast.NodeInfixOp.InfixOp.EqualEqual) | |
| 340 | 340 | }); |
| 341 | %return stack.append(State.ExpectOperand); | |
| 341 | try stack.append(State.ExpectOperand); | |
| 342 | 342 | continue; |
| 343 | 343 | }, |
| 344 | 344 | Token.Id.BangEqual => { |
| 345 | %return stack.append(State { | |
| 346 | .InfixOp = %return self.createInfixOp(token, ast.NodeInfixOp.InfixOp.BangEqual) | |
| 345 | try stack.append(State { | |
| 346 | .InfixOp = try self.createInfixOp(token, ast.NodeInfixOp.InfixOp.BangEqual) | |
| 347 | 347 | }); |
| 348 | %return stack.append(State.ExpectOperand); | |
| 348 | try stack.append(State.ExpectOperand); | |
| 349 | 349 | continue; |
| 350 | 350 | }, |
| 351 | 351 | else => { |
| ... | ... | @@ -357,7 +357,7 @@ pub const Parser = struct { |
| 357 | 357 | switch (stack.pop()) { |
| 358 | 358 | State.Expression => |dest_ptr| { |
| 359 | 359 | // we're done |
| 360 | %return dest_ptr.store(expression); | |
| 360 | try dest_ptr.store(expression); | |
| 361 | 361 | break; |
| 362 | 362 | }, |
| 363 | 363 | State.InfixOp => |infix_op| { |
| ... | ... | @@ -385,9 +385,9 @@ pub const Parser = struct { |
| 385 | 385 | Token.Id.Keyword_align => { |
| 386 | 386 | stack.append(state) %% unreachable; |
| 387 | 387 | if (addr_of_info.align_expr != null) return self.parseError(token, "multiple align qualifiers"); |
| 388 | _ = %return self.eatToken(Token.Id.LParen); | |
| 389 | %return stack.append(State { .ExpectToken = Token.Id.RParen }); | |
| 390 | %return stack.append(State { .Expression = DestPtr{.NullableField = &addr_of_info.align_expr} }); | |
| 388 | _ = try self.eatToken(Token.Id.LParen); | |
| 389 | try stack.append(State { .ExpectToken = Token.Id.RParen }); | |
| 390 | try stack.append(State { .Expression = DestPtr{.NullableField = &addr_of_info.align_expr} }); | |
| 391 | 391 | continue; |
| 392 | 392 | }, |
| 393 | 393 | Token.Id.Keyword_const => { |
| ... | ... | @@ -422,8 +422,8 @@ pub const Parser = struct { |
| 422 | 422 | |
| 423 | 423 | State.FnProto => |fn_proto| { |
| 424 | 424 | stack.append(State { .FnProtoAlign = fn_proto }) %% unreachable; |
| 425 | %return stack.append(State { .ParamDecl = fn_proto }); | |
| 426 | %return stack.append(State { .ExpectToken = Token.Id.LParen }); | |
| 425 | try stack.append(State { .ParamDecl = fn_proto }); | |
| 426 | try stack.append(State { .ExpectToken = Token.Id.LParen }); | |
| 427 | 427 | |
| 428 | 428 | const next_token = self.getNextToken(); |
| 429 | 429 | if (next_token.id == Token.Id.Identifier) { |
| ... | ... | @@ -455,7 +455,7 @@ pub const Parser = struct { |
| 455 | 455 | if (token.id == Token.Id.RParen) { |
| 456 | 456 | continue; |
| 457 | 457 | } |
| 458 | const param_decl = %return self.createAttachParamDecl(&fn_proto.params); | |
| 458 | const param_decl = try self.createAttachParamDecl(&fn_proto.params); | |
| 459 | 459 | if (token.id == Token.Id.Keyword_comptime) { |
| 460 | 460 | param_decl.comptime_token = token; |
| 461 | 461 | token = self.getNextToken(); |
| ... | ... | @@ -481,8 +481,8 @@ pub const Parser = struct { |
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | stack.append(State { .ParamDecl = fn_proto }) %% unreachable; |
| 484 | %return stack.append(State.ParamDeclComma); | |
| 485 | %return stack.append(State { | |
| 484 | try stack.append(State.ParamDeclComma); | |
| 485 | try stack.append(State { | |
| 486 | 486 | .TypeExpr = DestPtr {.Field = &param_decl.type_node} |
| 487 | 487 | }); |
| 488 | 488 | continue; |
| ... | ... | @@ -504,7 +504,7 @@ pub const Parser = struct { |
| 504 | 504 | const token = self.getNextToken(); |
| 505 | 505 | switch(token.id) { |
| 506 | 506 | Token.Id.LBrace => { |
| 507 | const block = %return self.createBlock(token); | |
| 507 | const block = try self.createBlock(token); | |
| 508 | 508 | fn_proto.body_node = &block.base; |
| 509 | 509 | stack.append(State { .Block = block }) %% unreachable; |
| 510 | 510 | continue; |
| ... | ... | @@ -524,7 +524,7 @@ pub const Parser = struct { |
| 524 | 524 | else => { |
| 525 | 525 | self.putBackToken(token); |
| 526 | 526 | stack.append(State { .Block = block }) %% unreachable; |
| 527 | %return stack.append(State { .Statement = block }); | |
| 527 | try stack.append(State { .Statement = block }); | |
| 528 | 528 | continue; |
| 529 | 529 | }, |
| 530 | 530 | } |
| ... | ... | @@ -538,9 +538,9 @@ pub const Parser = struct { |
| 538 | 538 | const mut_token = self.getNextToken(); |
| 539 | 539 | if (mut_token.id == Token.Id.Keyword_var or mut_token.id == Token.Id.Keyword_const) { |
| 540 | 540 | // TODO shouldn't need these casts |
| 541 | const var_decl = %return self.createAttachVarDecl(&block.statements, (?Token)(null), | |
| 541 | const var_decl = try self.createAttachVarDecl(&block.statements, (?Token)(null), | |
| 542 | 542 | mut_token, (?Token)(comptime_token), (?Token)(null)); |
| 543 | %return stack.append(State { .VarDecl = var_decl }); | |
| 543 | try stack.append(State { .VarDecl = var_decl }); | |
| 544 | 544 | continue; |
| 545 | 545 | } |
| 546 | 546 | self.putBackToken(mut_token); |
| ... | ... | @@ -552,16 +552,16 @@ pub const Parser = struct { |
| 552 | 552 | const mut_token = self.getNextToken(); |
| 553 | 553 | if (mut_token.id == Token.Id.Keyword_var or mut_token.id == Token.Id.Keyword_const) { |
| 554 | 554 | // TODO shouldn't need these casts |
| 555 | const var_decl = %return self.createAttachVarDecl(&block.statements, (?Token)(null), | |
| 555 | const var_decl = try self.createAttachVarDecl(&block.statements, (?Token)(null), | |
| 556 | 556 | mut_token, (?Token)(null), (?Token)(null)); |
| 557 | %return stack.append(State { .VarDecl = var_decl }); | |
| 557 | try stack.append(State { .VarDecl = var_decl }); | |
| 558 | 558 | continue; |
| 559 | 559 | } |
| 560 | 560 | self.putBackToken(mut_token); |
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | stack.append(State { .ExpectToken = Token.Id.Semicolon }) %% unreachable; |
| 564 | %return stack.append(State { .Expression = DestPtr{.List = &block.statements} }); | |
| 564 | try stack.append(State { .Expression = DestPtr{.List = &block.statements} }); | |
| 565 | 565 | continue; |
| 566 | 566 | }, |
| 567 | 567 | |
| ... | ... | @@ -576,7 +576,7 @@ pub const Parser = struct { |
| 576 | 576 | } |
| 577 | 577 | |
| 578 | 578 | fn createRoot(self: &Parser) -> %&ast.NodeRoot { |
| 579 | const node = %return self.allocator.create(ast.NodeRoot); | |
| 579 | const node = try self.allocator.create(ast.NodeRoot); | |
| 580 | 580 | %defer self.allocator.destroy(node); |
| 581 | 581 | |
| 582 | 582 | *node = ast.NodeRoot { |
| ... | ... | @@ -589,7 +589,7 @@ pub const Parser = struct { |
| 589 | 589 | fn createVarDecl(self: &Parser, visib_token: &const ?Token, mut_token: &const Token, comptime_token: &const ?Token, |
| 590 | 590 | extern_token: &const ?Token) -> %&ast.NodeVarDecl |
| 591 | 591 | { |
| 592 | const node = %return self.allocator.create(ast.NodeVarDecl); | |
| 592 | const node = try self.allocator.create(ast.NodeVarDecl); | |
| 593 | 593 | %defer self.allocator.destroy(node); |
| 594 | 594 | |
| 595 | 595 | *node = ast.NodeVarDecl { |
| ... | ... | @@ -612,7 +612,7 @@ pub const Parser = struct { |
| 612 | 612 | fn createFnProto(self: &Parser, fn_token: &const Token, extern_token: &const ?Token, |
| 613 | 613 | cc_token: &const ?Token, visib_token: &const ?Token, inline_token: &const ?Token) -> %&ast.NodeFnProto |
| 614 | 614 | { |
| 615 | const node = %return self.allocator.create(ast.NodeFnProto); | |
| 615 | const node = try self.allocator.create(ast.NodeFnProto); | |
| 616 | 616 | %defer self.allocator.destroy(node); |
| 617 | 617 | |
| 618 | 618 | *node = ast.NodeFnProto { |
| ... | ... | @@ -634,7 +634,7 @@ pub const Parser = struct { |
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | fn createParamDecl(self: &Parser) -> %&ast.NodeParamDecl { |
| 637 | const node = %return self.allocator.create(ast.NodeParamDecl); | |
| 637 | const node = try self.allocator.create(ast.NodeParamDecl); | |
| 638 | 638 | %defer self.allocator.destroy(node); |
| 639 | 639 | |
| 640 | 640 | *node = ast.NodeParamDecl { |
| ... | ... | @@ -649,7 +649,7 @@ pub const Parser = struct { |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | 651 | fn createBlock(self: &Parser, begin_token: &const Token) -> %&ast.NodeBlock { |
| 652 | const node = %return self.allocator.create(ast.NodeBlock); | |
| 652 | const node = try self.allocator.create(ast.NodeBlock); | |
| 653 | 653 | %defer self.allocator.destroy(node); |
| 654 | 654 | |
| 655 | 655 | *node = ast.NodeBlock { |
| ... | ... | @@ -662,7 +662,7 @@ pub const Parser = struct { |
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | fn createInfixOp(self: &Parser, op_token: &const Token, op: &const ast.NodeInfixOp.InfixOp) -> %&ast.NodeInfixOp { |
| 665 | const node = %return self.allocator.create(ast.NodeInfixOp); | |
| 665 | const node = try self.allocator.create(ast.NodeInfixOp); | |
| 666 | 666 | %defer self.allocator.destroy(node); |
| 667 | 667 | |
| 668 | 668 | *node = ast.NodeInfixOp { |
| ... | ... | @@ -676,7 +676,7 @@ pub const Parser = struct { |
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | fn createPrefixOp(self: &Parser, op_token: &const Token, op: &const ast.NodePrefixOp.PrefixOp) -> %&ast.NodePrefixOp { |
| 679 | const node = %return self.allocator.create(ast.NodePrefixOp); | |
| 679 | const node = try self.allocator.create(ast.NodePrefixOp); | |
| 680 | 680 | %defer self.allocator.destroy(node); |
| 681 | 681 | |
| 682 | 682 | *node = ast.NodePrefixOp { |
| ... | ... | @@ -689,7 +689,7 @@ pub const Parser = struct { |
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | fn createIdentifier(self: &Parser, name_token: &const Token) -> %&ast.NodeIdentifier { |
| 692 | const node = %return self.allocator.create(ast.NodeIdentifier); | |
| 692 | const node = try self.allocator.create(ast.NodeIdentifier); | |
| 693 | 693 | %defer self.allocator.destroy(node); |
| 694 | 694 | |
| 695 | 695 | *node = ast.NodeIdentifier { |
| ... | ... | @@ -700,7 +700,7 @@ pub const Parser = struct { |
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | fn createIntegerLiteral(self: &Parser, token: &const Token) -> %&ast.NodeIntegerLiteral { |
| 703 | const node = %return self.allocator.create(ast.NodeIntegerLiteral); | |
| 703 | const node = try self.allocator.create(ast.NodeIntegerLiteral); | |
| 704 | 704 | %defer self.allocator.destroy(node); |
| 705 | 705 | |
| 706 | 706 | *node = ast.NodeIntegerLiteral { |
| ... | ... | @@ -711,7 +711,7 @@ pub const Parser = struct { |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | fn createFloatLiteral(self: &Parser, token: &const Token) -> %&ast.NodeFloatLiteral { |
| 714 | const node = %return self.allocator.create(ast.NodeFloatLiteral); | |
| 714 | const node = try self.allocator.create(ast.NodeFloatLiteral); | |
| 715 | 715 | %defer self.allocator.destroy(node); |
| 716 | 716 | |
| 717 | 717 | *node = ast.NodeFloatLiteral { |
| ... | ... | @@ -722,16 +722,16 @@ pub const Parser = struct { |
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | fn createAttachIdentifier(self: &Parser, dest_ptr: &const DestPtr, name_token: &const Token) -> %&ast.NodeIdentifier { |
| 725 | const node = %return self.createIdentifier(name_token); | |
| 725 | const node = try self.createIdentifier(name_token); | |
| 726 | 726 | %defer self.allocator.destroy(node); |
| 727 | %return dest_ptr.store(&node.base); | |
| 727 | try dest_ptr.store(&node.base); | |
| 728 | 728 | return node; |
| 729 | 729 | } |
| 730 | 730 | |
| 731 | 731 | fn createAttachParamDecl(self: &Parser, list: &ArrayList(&ast.Node)) -> %&ast.NodeParamDecl { |
| 732 | const node = %return self.createParamDecl(); | |
| 732 | const node = try self.createParamDecl(); | |
| 733 | 733 | %defer self.allocator.destroy(node); |
| 734 | %return list.append(&node.base); | |
| 734 | try list.append(&node.base); | |
| 735 | 735 | return node; |
| 736 | 736 | } |
| 737 | 737 | |
| ... | ... | @@ -739,18 +739,18 @@ pub const Parser = struct { |
| 739 | 739 | extern_token: &const ?Token, cc_token: &const ?Token, visib_token: &const ?Token, |
| 740 | 740 | inline_token: &const ?Token) -> %&ast.NodeFnProto |
| 741 | 741 | { |
| 742 | const node = %return self.createFnProto(fn_token, extern_token, cc_token, visib_token, inline_token); | |
| 742 | const node = try self.createFnProto(fn_token, extern_token, cc_token, visib_token, inline_token); | |
| 743 | 743 | %defer self.allocator.destroy(node); |
| 744 | %return list.append(&node.base); | |
| 744 | try list.append(&node.base); | |
| 745 | 745 | return node; |
| 746 | 746 | } |
| 747 | 747 | |
| 748 | 748 | fn createAttachVarDecl(self: &Parser, list: &ArrayList(&ast.Node), visib_token: &const ?Token, |
| 749 | 749 | mut_token: &const Token, comptime_token: &const ?Token, extern_token: &const ?Token) -> %&ast.NodeVarDecl |
| 750 | 750 | { |
| 751 | const node = %return self.createVarDecl(visib_token, mut_token, comptime_token, extern_token); | |
| 751 | const node = try self.createVarDecl(visib_token, mut_token, comptime_token, extern_token); | |
| 752 | 752 | %defer self.allocator.destroy(node); |
| 753 | %return list.append(&node.base); | |
| 753 | try list.append(&node.base); | |
| 754 | 754 | return node; |
| 755 | 755 | } |
| 756 | 756 | |
| ... | ... | @@ -783,7 +783,7 @@ pub const Parser = struct { |
| 783 | 783 | |
| 784 | 784 | fn eatToken(self: &Parser, id: @TagType(Token.Id)) -> %Token { |
| 785 | 785 | const token = self.getNextToken(); |
| 786 | %return self.expectToken(token, id); | |
| 786 | try self.expectToken(token, id); | |
| 787 | 787 | return token; |
| 788 | 788 | } |
| 789 | 789 | |
| ... | ... | @@ -812,7 +812,7 @@ pub const Parser = struct { |
| 812 | 812 | var stack = self.initUtilityArrayList(RenderAstFrame); |
| 813 | 813 | defer self.deinitUtilityArrayList(stack); |
| 814 | 814 | |
| 815 | %return stack.append(RenderAstFrame { | |
| 815 | try stack.append(RenderAstFrame { | |
| 816 | 816 | .node = &root_node.base, |
| 817 | 817 | .indent = 0, |
| 818 | 818 | }); |
| ... | ... | @@ -821,13 +821,13 @@ pub const Parser = struct { |
| 821 | 821 | { |
| 822 | 822 | var i: usize = 0; |
| 823 | 823 | while (i < frame.indent) : (i += 1) { |
| 824 | %return stream.print(" "); | |
| 824 | try stream.print(" "); | |
| 825 | 825 | } |
| 826 | 826 | } |
| 827 | %return stream.print("{}\n", @tagName(frame.node.id)); | |
| 827 | try stream.print("{}\n", @tagName(frame.node.id)); | |
| 828 | 828 | var child_i: usize = 0; |
| 829 | 829 | while (frame.node.iterate(child_i)) |child| : (child_i += 1) { |
| 830 | %return stack.append(RenderAstFrame { | |
| 830 | try stack.append(RenderAstFrame { | |
| 831 | 831 | .node = child, |
| 832 | 832 | .indent = frame.indent + 2, |
| 833 | 833 | }); |
| ... | ... | @@ -856,7 +856,7 @@ pub const Parser = struct { |
| 856 | 856 | while (i != 0) { |
| 857 | 857 | i -= 1; |
| 858 | 858 | const decl = root_node.decls.items[i]; |
| 859 | %return stack.append(RenderState {.TopLevelDecl = decl}); | |
| 859 | try stack.append(RenderState {.TopLevelDecl = decl}); | |
| 860 | 860 | } |
| 861 | 861 | } |
| 862 | 862 | |
| ... | ... | @@ -870,42 +870,42 @@ pub const Parser = struct { |
| 870 | 870 | const fn_proto = @fieldParentPtr(ast.NodeFnProto, "base", decl); |
| 871 | 871 | if (fn_proto.visib_token) |visib_token| { |
| 872 | 872 | switch (visib_token.id) { |
| 873 | Token.Id.Keyword_pub => %return stream.print("pub "), | |
| 874 | Token.Id.Keyword_export => %return stream.print("export "), | |
| 873 | Token.Id.Keyword_pub => try stream.print("pub "), | |
| 874 | Token.Id.Keyword_export => try stream.print("export "), | |
| 875 | 875 | else => unreachable, |
| 876 | 876 | } |
| 877 | 877 | } |
| 878 | 878 | if (fn_proto.extern_token) |extern_token| { |
| 879 | %return stream.print("{} ", self.tokenizer.getTokenSlice(extern_token)); | |
| 879 | try stream.print("{} ", self.tokenizer.getTokenSlice(extern_token)); | |
| 880 | 880 | } |
| 881 | %return stream.print("fn"); | |
| 881 | try stream.print("fn"); | |
| 882 | 882 | |
| 883 | 883 | if (fn_proto.name_token) |name_token| { |
| 884 | %return stream.print(" {}", self.tokenizer.getTokenSlice(name_token)); | |
| 884 | try stream.print(" {}", self.tokenizer.getTokenSlice(name_token)); | |
| 885 | 885 | } |
| 886 | 886 | |
| 887 | %return stream.print("("); | |
| 887 | try stream.print("("); | |
| 888 | 888 | |
| 889 | %return stack.append(RenderState { .Text = "\n" }); | |
| 889 | try stack.append(RenderState { .Text = "\n" }); | |
| 890 | 890 | if (fn_proto.body_node == null) { |
| 891 | %return stack.append(RenderState { .Text = ";" }); | |
| 891 | try stack.append(RenderState { .Text = ";" }); | |
| 892 | 892 | } |
| 893 | 893 | |
| 894 | %return stack.append(RenderState { .FnProtoRParen = fn_proto}); | |
| 894 | try stack.append(RenderState { .FnProtoRParen = fn_proto}); | |
| 895 | 895 | var i = fn_proto.params.len; |
| 896 | 896 | while (i != 0) { |
| 897 | 897 | i -= 1; |
| 898 | 898 | const param_decl_node = fn_proto.params.items[i]; |
| 899 | %return stack.append(RenderState { .ParamDecl = param_decl_node}); | |
| 899 | try stack.append(RenderState { .ParamDecl = param_decl_node}); | |
| 900 | 900 | if (i != 0) { |
| 901 | %return stack.append(RenderState { .Text = ", " }); | |
| 901 | try stack.append(RenderState { .Text = ", " }); | |
| 902 | 902 | } |
| 903 | 903 | } |
| 904 | 904 | }, |
| 905 | 905 | ast.Node.Id.VarDecl => { |
| 906 | 906 | const var_decl = @fieldParentPtr(ast.NodeVarDecl, "base", decl); |
| 907 | %return stack.append(RenderState { .Text = "\n"}); | |
| 908 | %return stack.append(RenderState { .VarDecl = var_decl}); | |
| 907 | try stack.append(RenderState { .Text = "\n"}); | |
| 908 | try stack.append(RenderState { .VarDecl = var_decl}); | |
| 909 | 909 | |
| 910 | 910 | }, |
| 911 | 911 | else => unreachable, |
| ... | ... | @@ -914,111 +914,111 @@ pub const Parser = struct { |
| 914 | 914 | |
| 915 | 915 | RenderState.VarDecl => |var_decl| { |
| 916 | 916 | if (var_decl.visib_token) |visib_token| { |
| 917 | %return stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); | |
| 917 | try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); | |
| 918 | 918 | } |
| 919 | 919 | if (var_decl.extern_token) |extern_token| { |
| 920 | %return stream.print("{} ", self.tokenizer.getTokenSlice(extern_token)); | |
| 920 | try stream.print("{} ", self.tokenizer.getTokenSlice(extern_token)); | |
| 921 | 921 | if (var_decl.lib_name != null) { |
| 922 | 922 | @panic("TODO"); |
| 923 | 923 | } |
| 924 | 924 | } |
| 925 | 925 | if (var_decl.comptime_token) |comptime_token| { |
| 926 | %return stream.print("{} ", self.tokenizer.getTokenSlice(comptime_token)); | |
| 926 | try stream.print("{} ", self.tokenizer.getTokenSlice(comptime_token)); | |
| 927 | 927 | } |
| 928 | %return stream.print("{} ", self.tokenizer.getTokenSlice(var_decl.mut_token)); | |
| 929 | %return stream.print("{}", self.tokenizer.getTokenSlice(var_decl.name_token)); | |
| 928 | try stream.print("{} ", self.tokenizer.getTokenSlice(var_decl.mut_token)); | |
| 929 | try stream.print("{}", self.tokenizer.getTokenSlice(var_decl.name_token)); | |
| 930 | 930 | |
| 931 | %return stack.append(RenderState { .Text = ";" }); | |
| 931 | try stack.append(RenderState { .Text = ";" }); | |
| 932 | 932 | if (var_decl.init_node) |init_node| { |
| 933 | %return stack.append(RenderState { .Expression = init_node }); | |
| 934 | %return stack.append(RenderState { .Text = " = " }); | |
| 933 | try stack.append(RenderState { .Expression = init_node }); | |
| 934 | try stack.append(RenderState { .Text = " = " }); | |
| 935 | 935 | } |
| 936 | 936 | if (var_decl.align_node) |align_node| { |
| 937 | %return stack.append(RenderState { .Text = ")" }); | |
| 938 | %return stack.append(RenderState { .Expression = align_node }); | |
| 939 | %return stack.append(RenderState { .Text = " align(" }); | |
| 937 | try stack.append(RenderState { .Text = ")" }); | |
| 938 | try stack.append(RenderState { .Expression = align_node }); | |
| 939 | try stack.append(RenderState { .Text = " align(" }); | |
| 940 | 940 | } |
| 941 | 941 | if (var_decl.type_node) |type_node| { |
| 942 | %return stream.print(": "); | |
| 943 | %return stack.append(RenderState { .Expression = type_node }); | |
| 942 | try stream.print(": "); | |
| 943 | try stack.append(RenderState { .Expression = type_node }); | |
| 944 | 944 | } |
| 945 | 945 | }, |
| 946 | 946 | |
| 947 | 947 | RenderState.ParamDecl => |base| { |
| 948 | 948 | const param_decl = @fieldParentPtr(ast.NodeParamDecl, "base", base); |
| 949 | 949 | if (param_decl.comptime_token) |comptime_token| { |
| 950 | %return stream.print("{} ", self.tokenizer.getTokenSlice(comptime_token)); | |
| 950 | try stream.print("{} ", self.tokenizer.getTokenSlice(comptime_token)); | |
| 951 | 951 | } |
| 952 | 952 | if (param_decl.noalias_token) |noalias_token| { |
| 953 | %return stream.print("{} ", self.tokenizer.getTokenSlice(noalias_token)); | |
| 953 | try stream.print("{} ", self.tokenizer.getTokenSlice(noalias_token)); | |
| 954 | 954 | } |
| 955 | 955 | if (param_decl.name_token) |name_token| { |
| 956 | %return stream.print("{}: ", self.tokenizer.getTokenSlice(name_token)); | |
| 956 | try stream.print("{}: ", self.tokenizer.getTokenSlice(name_token)); | |
| 957 | 957 | } |
| 958 | 958 | if (param_decl.var_args_token) |var_args_token| { |
| 959 | %return stream.print("{}", self.tokenizer.getTokenSlice(var_args_token)); | |
| 959 | try stream.print("{}", self.tokenizer.getTokenSlice(var_args_token)); | |
| 960 | 960 | } else { |
| 961 | %return stack.append(RenderState { .Expression = param_decl.type_node}); | |
| 961 | try stack.append(RenderState { .Expression = param_decl.type_node}); | |
| 962 | 962 | } |
| 963 | 963 | }, |
| 964 | 964 | RenderState.Text => |bytes| { |
| 965 | %return stream.write(bytes); | |
| 965 | try stream.write(bytes); | |
| 966 | 966 | }, |
| 967 | 967 | RenderState.Expression => |base| switch (base.id) { |
| 968 | 968 | ast.Node.Id.Identifier => { |
| 969 | 969 | const identifier = @fieldParentPtr(ast.NodeIdentifier, "base", base); |
| 970 | %return stream.print("{}", self.tokenizer.getTokenSlice(identifier.name_token)); | |
| 970 | try stream.print("{}", self.tokenizer.getTokenSlice(identifier.name_token)); | |
| 971 | 971 | }, |
| 972 | 972 | ast.Node.Id.Block => { |
| 973 | 973 | const block = @fieldParentPtr(ast.NodeBlock, "base", base); |
| 974 | %return stream.write("{"); | |
| 975 | %return stack.append(RenderState { .Text = "}"}); | |
| 976 | %return stack.append(RenderState.PrintIndent); | |
| 977 | %return stack.append(RenderState { .Indent = indent}); | |
| 978 | %return stack.append(RenderState { .Text = "\n"}); | |
| 974 | try stream.write("{"); | |
| 975 | try stack.append(RenderState { .Text = "}"}); | |
| 976 | try stack.append(RenderState.PrintIndent); | |
| 977 | try stack.append(RenderState { .Indent = indent}); | |
| 978 | try stack.append(RenderState { .Text = "\n"}); | |
| 979 | 979 | var i = block.statements.len; |
| 980 | 980 | while (i != 0) { |
| 981 | 981 | i -= 1; |
| 982 | 982 | const statement_node = block.statements.items[i]; |
| 983 | %return stack.append(RenderState { .Statement = statement_node}); | |
| 984 | %return stack.append(RenderState.PrintIndent); | |
| 985 | %return stack.append(RenderState { .Indent = indent + indent_delta}); | |
| 986 | %return stack.append(RenderState { .Text = "\n" }); | |
| 983 | try stack.append(RenderState { .Statement = statement_node}); | |
| 984 | try stack.append(RenderState.PrintIndent); | |
| 985 | try stack.append(RenderState { .Indent = indent + indent_delta}); | |
| 986 | try stack.append(RenderState { .Text = "\n" }); | |
| 987 | 987 | } |
| 988 | 988 | }, |
| 989 | 989 | ast.Node.Id.InfixOp => { |
| 990 | 990 | const prefix_op_node = @fieldParentPtr(ast.NodeInfixOp, "base", base); |
| 991 | %return stack.append(RenderState { .Expression = prefix_op_node.rhs }); | |
| 991 | try stack.append(RenderState { .Expression = prefix_op_node.rhs }); | |
| 992 | 992 | switch (prefix_op_node.op) { |
| 993 | 993 | ast.NodeInfixOp.InfixOp.EqualEqual => { |
| 994 | %return stack.append(RenderState { .Text = " == "}); | |
| 994 | try stack.append(RenderState { .Text = " == "}); | |
| 995 | 995 | }, |
| 996 | 996 | ast.NodeInfixOp.InfixOp.BangEqual => { |
| 997 | %return stack.append(RenderState { .Text = " != "}); | |
| 997 | try stack.append(RenderState { .Text = " != "}); | |
| 998 | 998 | }, |
| 999 | 999 | else => unreachable, |
| 1000 | 1000 | } |
| 1001 | %return stack.append(RenderState { .Expression = prefix_op_node.lhs }); | |
| 1001 | try stack.append(RenderState { .Expression = prefix_op_node.lhs }); | |
| 1002 | 1002 | }, |
| 1003 | 1003 | ast.Node.Id.PrefixOp => { |
| 1004 | 1004 | const prefix_op_node = @fieldParentPtr(ast.NodePrefixOp, "base", base); |
| 1005 | %return stack.append(RenderState { .Expression = prefix_op_node.rhs }); | |
| 1005 | try stack.append(RenderState { .Expression = prefix_op_node.rhs }); | |
| 1006 | 1006 | switch (prefix_op_node.op) { |
| 1007 | 1007 | ast.NodePrefixOp.PrefixOp.Return => { |
| 1008 | %return stream.write("return "); | |
| 1008 | try stream.write("return "); | |
| 1009 | 1009 | }, |
| 1010 | 1010 | ast.NodePrefixOp.PrefixOp.AddrOf => |addr_of_info| { |
| 1011 | %return stream.write("&"); | |
| 1011 | try stream.write("&"); | |
| 1012 | 1012 | if (addr_of_info.volatile_token != null) { |
| 1013 | %return stack.append(RenderState { .Text = "volatile "}); | |
| 1013 | try stack.append(RenderState { .Text = "volatile "}); | |
| 1014 | 1014 | } |
| 1015 | 1015 | if (addr_of_info.const_token != null) { |
| 1016 | %return stack.append(RenderState { .Text = "const "}); | |
| 1016 | try stack.append(RenderState { .Text = "const "}); | |
| 1017 | 1017 | } |
| 1018 | 1018 | if (addr_of_info.align_expr) |align_expr| { |
| 1019 | %return stream.print("align("); | |
| 1020 | %return stack.append(RenderState { .Text = ") "}); | |
| 1021 | %return stack.append(RenderState { .Expression = align_expr}); | |
| 1019 | try stream.print("align("); | |
| 1020 | try stack.append(RenderState { .Text = ") "}); | |
| 1021 | try stack.append(RenderState { .Expression = align_expr}); | |
| 1022 | 1022 | } |
| 1023 | 1023 | }, |
| 1024 | 1024 | else => unreachable, |
| ... | ... | @@ -1026,42 +1026,42 @@ pub const Parser = struct { |
| 1026 | 1026 | }, |
| 1027 | 1027 | ast.Node.Id.IntegerLiteral => { |
| 1028 | 1028 | const integer_literal = @fieldParentPtr(ast.NodeIntegerLiteral, "base", base); |
| 1029 | %return stream.print("{}", self.tokenizer.getTokenSlice(integer_literal.token)); | |
| 1029 | try stream.print("{}", self.tokenizer.getTokenSlice(integer_literal.token)); | |
| 1030 | 1030 | }, |
| 1031 | 1031 | ast.Node.Id.FloatLiteral => { |
| 1032 | 1032 | const float_literal = @fieldParentPtr(ast.NodeFloatLiteral, "base", base); |
| 1033 | %return stream.print("{}", self.tokenizer.getTokenSlice(float_literal.token)); | |
| 1033 | try stream.print("{}", self.tokenizer.getTokenSlice(float_literal.token)); | |
| 1034 | 1034 | }, |
| 1035 | 1035 | else => unreachable, |
| 1036 | 1036 | }, |
| 1037 | 1037 | RenderState.FnProtoRParen => |fn_proto| { |
| 1038 | %return stream.print(")"); | |
| 1038 | try stream.print(")"); | |
| 1039 | 1039 | if (fn_proto.align_expr != null) { |
| 1040 | 1040 | @panic("TODO"); |
| 1041 | 1041 | } |
| 1042 | 1042 | if (fn_proto.return_type) |return_type| { |
| 1043 | %return stream.print(" -> "); | |
| 1043 | try stream.print(" -> "); | |
| 1044 | 1044 | if (fn_proto.body_node) |body_node| { |
| 1045 | %return stack.append(RenderState { .Expression = body_node}); | |
| 1046 | %return stack.append(RenderState { .Text = " "}); | |
| 1045 | try stack.append(RenderState { .Expression = body_node}); | |
| 1046 | try stack.append(RenderState { .Text = " "}); | |
| 1047 | 1047 | } |
| 1048 | %return stack.append(RenderState { .Expression = return_type}); | |
| 1048 | try stack.append(RenderState { .Expression = return_type}); | |
| 1049 | 1049 | } |
| 1050 | 1050 | }, |
| 1051 | 1051 | RenderState.Statement => |base| { |
| 1052 | 1052 | switch (base.id) { |
| 1053 | 1053 | ast.Node.Id.VarDecl => { |
| 1054 | 1054 | const var_decl = @fieldParentPtr(ast.NodeVarDecl, "base", base); |
| 1055 | %return stack.append(RenderState { .VarDecl = var_decl}); | |
| 1055 | try stack.append(RenderState { .VarDecl = var_decl}); | |
| 1056 | 1056 | }, |
| 1057 | 1057 | else => { |
| 1058 | %return stack.append(RenderState { .Text = ";"}); | |
| 1059 | %return stack.append(RenderState { .Expression = base}); | |
| 1058 | try stack.append(RenderState { .Text = ";"}); | |
| 1059 | try stack.append(RenderState { .Expression = base}); | |
| 1060 | 1060 | }, |
| 1061 | 1061 | } |
| 1062 | 1062 | }, |
| 1063 | 1063 | RenderState.Indent => |new_indent| indent = new_indent, |
| 1064 | RenderState.PrintIndent => %return stream.writeByteNTimes(' ', indent), | |
| 1064 | RenderState.PrintIndent => try stream.writeByteNTimes(' ', indent), | |
| 1065 | 1065 | } |
| 1066 | 1066 | } |
| 1067 | 1067 | } |
| ... | ... | @@ -1096,12 +1096,12 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) -> %[]u8 { |
| 1096 | 1096 | var parser = Parser.init(&tokenizer, allocator, "(memory buffer)"); |
| 1097 | 1097 | defer parser.deinit(); |
| 1098 | 1098 | |
| 1099 | const root_node = %return parser.parse(); | |
| 1099 | const root_node = try parser.parse(); | |
| 1100 | 1100 | defer parser.freeAst(root_node); |
| 1101 | 1101 | |
| 1102 | var buffer = %return std.Buffer.initSize(allocator, 0); | |
| 1102 | var buffer = try std.Buffer.initSize(allocator, 0); | |
| 1103 | 1103 | var buffer_out_stream = io.BufferOutStream.init(&buffer); |
| 1104 | %return parser.renderSource(&buffer_out_stream.stream, root_node); | |
| 1104 | try parser.renderSource(&buffer_out_stream.stream, root_node); | |
| 1105 | 1105 | return buffer.toOwnedSlice(); |
| 1106 | 1106 | } |
| 1107 | 1107 |
src/ast_render.cpp+1-1| ... | ... | @@ -85,7 +85,7 @@ static const char *visib_mod_string(VisibMod mod) { |
| 85 | 85 | static const char *return_string(ReturnKind kind) { |
| 86 | 86 | switch (kind) { |
| 87 | 87 | case ReturnKindUnconditional: return "return"; |
| 88 | case ReturnKindError: return "%return"; | |
| 88 | case ReturnKindError: return "try"; | |
| 89 | 89 | } |
| 90 | 90 | zig_unreachable(); |
| 91 | 91 | } |
src/parser.cpp+33-29| ... | ... | @@ -225,6 +225,7 @@ static AstNode *ast_parse_return_expr(ParseContext *pc, size_t *token_index); |
| 225 | 225 | static AstNode *ast_parse_grouped_expr(ParseContext *pc, size_t *token_index, bool mandatory); |
| 226 | 226 | static AstNode *ast_parse_container_decl(ParseContext *pc, size_t *token_index, bool mandatory); |
| 227 | 227 | static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory); |
| 228 | static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index); | |
| 228 | 229 | |
| 229 | 230 | static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) { |
| 230 | 231 | if (token->id == token_id) { |
| ... | ... | @@ -1003,25 +1004,21 @@ static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) { |
| 1003 | 1004 | |
| 1004 | 1005 | /* |
| 1005 | 1006 | PrefixOpExpression : PrefixOp PrefixOpExpression | SuffixOpExpression |
| 1006 | PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | |
| 1007 | PrefixOp = "!" | "-" | "~" | "*" | ("&amp;" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "%" | "%%" | "??" | "-%" | "try" | |
| 1007 | 1008 | */ |
| 1008 | 1009 | static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) { |
| 1009 | 1010 | Token *token = &pc->tokens->at(*token_index); |
| 1010 | 1011 | if (token->id == TokenIdAmpersand) { |
| 1011 | 1012 | return ast_parse_addr_of(pc, token_index); |
| 1012 | 1013 | } |
| 1014 | if (token->id == TokenIdKeywordTry) { | |
| 1015 | return ast_parse_try_expr(pc, token_index); | |
| 1016 | } | |
| 1013 | 1017 | PrefixOp prefix_op = tok_to_prefix_op(token); |
| 1014 | 1018 | if (prefix_op == PrefixOpInvalid) { |
| 1015 | 1019 | return ast_parse_suffix_op_expr(pc, token_index, mandatory); |
| 1016 | 1020 | } |
| 1017 | 1021 | |
| 1018 | if (prefix_op == PrefixOpError || prefix_op == PrefixOpMaybe) { | |
| 1019 | Token *maybe_return = &pc->tokens->at(*token_index + 1); | |
| 1020 | if (maybe_return->id == TokenIdKeywordReturn) { | |
| 1021 | return ast_parse_return_expr(pc, token_index); | |
| 1022 | } | |
| 1023 | } | |
| 1024 | ||
| 1025 | 1022 | *token_index += 1; |
| 1026 | 1023 | |
| 1027 | 1024 | |
| ... | ... | @@ -1438,38 +1435,41 @@ static AstNode *ast_parse_if_try_test_expr(ParseContext *pc, size_t *token_index |
| 1438 | 1435 | } |
| 1439 | 1436 | |
| 1440 | 1437 | /* |
| 1441 | ReturnExpression : option("%") "return" option(Expression) | |
| 1438 | ReturnExpression : "return" option(Expression) | |
| 1442 | 1439 | */ |
| 1443 | 1440 | static AstNode *ast_parse_return_expr(ParseContext *pc, size_t *token_index) { |
| 1444 | 1441 | Token *token = &pc->tokens->at(*token_index); |
| 1445 | 1442 | |
| 1446 | NodeType node_type; | |
| 1447 | ReturnKind kind; | |
| 1448 | ||
| 1449 | if (token->id == TokenIdPercent) { | |
| 1450 | Token *next_token = &pc->tokens->at(*token_index + 1); | |
| 1451 | if (next_token->id == TokenIdKeywordReturn) { | |
| 1452 | kind = ReturnKindError; | |
| 1453 | node_type = NodeTypeReturnExpr; | |
| 1454 | *token_index += 2; | |
| 1455 | } else { | |
| 1456 | return nullptr; | |
| 1457 | } | |
| 1458 | } else if (token->id == TokenIdKeywordReturn) { | |
| 1459 | kind = ReturnKindUnconditional; | |
| 1460 | node_type = NodeTypeReturnExpr; | |
| 1461 | *token_index += 1; | |
| 1462 | } else { | |
| 1443 | if (token->id != TokenIdKeywordReturn) { | |
| 1463 | 1444 | return nullptr; |
| 1464 | 1445 | } |
| 1446 | *token_index += 1; | |
| 1465 | 1447 | |
| 1466 | AstNode *node = ast_create_node(pc, node_type, token); | |
| 1467 | node->data.return_expr.kind = kind; | |
| 1448 | AstNode *node = ast_create_node(pc, NodeTypeReturnExpr, token); | |
| 1449 | node->data.return_expr.kind = ReturnKindUnconditional; | |
| 1468 | 1450 | node->data.return_expr.expr = ast_parse_expression(pc, token_index, false); |
| 1469 | 1451 | |
| 1470 | 1452 | return node; |
| 1471 | 1453 | } |
| 1472 | 1454 | |
| 1455 | /* | |
| 1456 | TryExpression : "try" Expression | |
| 1457 | */ | |
| 1458 | static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index) { | |
| 1459 | Token *token = &pc->tokens->at(*token_index); | |
| 1460 | ||
| 1461 | if (token->id != TokenIdKeywordTry) { | |
| 1462 | return nullptr; | |
| 1463 | } | |
| 1464 | *token_index += 1; | |
| 1465 | ||
| 1466 | AstNode *node = ast_create_node(pc, NodeTypeReturnExpr, token); | |
| 1467 | node->data.return_expr.kind = ReturnKindError; | |
| 1468 | node->data.return_expr.expr = ast_parse_expression(pc, token_index, true); | |
| 1469 | ||
| 1470 | return node; | |
| 1471 | } | |
| 1472 | ||
| 1473 | 1473 | /* |
| 1474 | 1474 | BreakExpression = "break" option(":" Symbol) option(Expression) |
| 1475 | 1475 | */ |
| ... | ... | @@ -2124,7 +2124,7 @@ static AstNode *ast_parse_block_or_expression(ParseContext *pc, size_t *token_in |
| 2124 | 2124 | } |
| 2125 | 2125 | |
| 2126 | 2126 | /* |
| 2127 | Expression = ReturnExpression | BreakExpression | AssignmentExpression | |
| 2127 | Expression = TryExpression | ReturnExpression | BreakExpression | AssignmentExpression | |
| 2128 | 2128 | */ |
| 2129 | 2129 | static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool mandatory) { |
| 2130 | 2130 | Token *token = &pc->tokens->at(*token_index); |
| ... | ... | @@ -2133,6 +2133,10 @@ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool |
| 2133 | 2133 | if (return_expr) |
| 2134 | 2134 | return return_expr; |
| 2135 | 2135 | |
| 2136 | AstNode *try_expr = ast_parse_try_expr(pc, token_index); | |
| 2137 | if (try_expr) | |
| 2138 | return try_expr; | |
| 2139 | ||
| 2136 | 2140 | AstNode *break_expr = ast_parse_break_expr(pc, token_index); |
| 2137 | 2141 | if (break_expr) |
| 2138 | 2142 | return break_expr; |
src/tokenizer.cpp+2| ... | ... | @@ -141,6 +141,7 @@ static const struct ZigKeyword zig_keywords[] = { |
| 141 | 141 | {"test", TokenIdKeywordTest}, |
| 142 | 142 | {"this", TokenIdKeywordThis}, |
| 143 | 143 | {"true", TokenIdKeywordTrue}, |
| 144 | {"try", TokenIdKeywordTry}, | |
| 144 | 145 | {"undefined", TokenIdKeywordUndefined}, |
| 145 | 146 | {"union", TokenIdKeywordUnion}, |
| 146 | 147 | {"unreachable", TokenIdKeywordUnreachable}, |
| ... | ... | @@ -1541,6 +1542,7 @@ const char * token_name(TokenId id) { |
| 1541 | 1542 | case TokenIdKeywordTest: return "test"; |
| 1542 | 1543 | case TokenIdKeywordThis: return "this"; |
| 1543 | 1544 | case TokenIdKeywordTrue: return "true"; |
| 1545 | case TokenIdKeywordTry: return "try"; | |
| 1544 | 1546 | case TokenIdKeywordUndefined: return "undefined"; |
| 1545 | 1547 | case TokenIdKeywordUnion: return "union"; |
| 1546 | 1548 | case TokenIdKeywordUnreachable: return "unreachable"; |
src/tokenizer.hpp+1| ... | ... | @@ -80,6 +80,7 @@ enum TokenId { |
| 80 | 80 | TokenIdKeywordTest, |
| 81 | 81 | TokenIdKeywordThis, |
| 82 | 82 | TokenIdKeywordTrue, |
| 83 | TokenIdKeywordTry, | |
| 83 | 84 | TokenIdKeywordUndefined, |
| 84 | 85 | TokenIdKeywordUnion, |
| 85 | 86 | TokenIdKeywordUnreachable, |
std/array_list.zig+5-5| ... | ... | @@ -60,18 +60,18 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | pub fn append(l: &Self, item: &const T) -> %void { |
| 63 | const new_item_ptr = %return l.addOne(); | |
| 63 | const new_item_ptr = try l.addOne(); | |
| 64 | 64 | *new_item_ptr = *item; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | pub fn appendSlice(l: &Self, items: []align(A) const T) -> %void { |
| 68 | %return l.ensureCapacity(l.len + items.len); | |
| 68 | try l.ensureCapacity(l.len + items.len); | |
| 69 | 69 | mem.copy(T, l.items[l.len..], items); |
| 70 | 70 | l.len += items.len; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | pub fn resize(l: &Self, new_len: usize) -> %void { |
| 74 | %return l.ensureCapacity(new_len); | |
| 74 | try l.ensureCapacity(new_len); | |
| 75 | 75 | l.len = new_len; |
| 76 | 76 | } |
| 77 | 77 | |
| ... | ... | @@ -87,12 +87,12 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ |
| 87 | 87 | better_capacity += better_capacity / 2 + 8; |
| 88 | 88 | if (better_capacity >= new_capacity) break; |
| 89 | 89 | } |
| 90 | l.items = %return l.allocator.alignedRealloc(T, A, l.items, better_capacity); | |
| 90 | l.items = try l.allocator.alignedRealloc(T, A, l.items, better_capacity); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | pub fn addOne(l: &Self) -> %&T { |
| 94 | 94 | const new_length = l.len + 1; |
| 95 | %return l.ensureCapacity(new_length); | |
| 95 | try l.ensureCapacity(new_length); | |
| 96 | 96 | const result = &l.items[l.len]; |
| 97 | 97 | l.len = new_length; |
| 98 | 98 | return result; |
std/base64.zig+35-35| ... | ... | @@ -379,37 +379,37 @@ test "base64" { |
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | fn testBase64() -> %void { |
| 382 | %return testAllApis("", ""); | |
| 383 | %return testAllApis("f", "Zg=="); | |
| 384 | %return testAllApis("fo", "Zm8="); | |
| 385 | %return testAllApis("foo", "Zm9v"); | |
| 386 | %return testAllApis("foob", "Zm9vYg=="); | |
| 387 | %return testAllApis("fooba", "Zm9vYmE="); | |
| 388 | %return testAllApis("foobar", "Zm9vYmFy"); | |
| 389 | ||
| 390 | %return testDecodeIgnoreSpace("", " "); | |
| 391 | %return testDecodeIgnoreSpace("f", "Z g= ="); | |
| 392 | %return testDecodeIgnoreSpace("fo", " Zm8="); | |
| 393 | %return testDecodeIgnoreSpace("foo", "Zm9v "); | |
| 394 | %return testDecodeIgnoreSpace("foob", "Zm9vYg = = "); | |
| 395 | %return testDecodeIgnoreSpace("fooba", "Zm9v YmE="); | |
| 396 | %return testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y "); | |
| 382 | try testAllApis("", ""); | |
| 383 | try testAllApis("f", "Zg=="); | |
| 384 | try testAllApis("fo", "Zm8="); | |
| 385 | try testAllApis("foo", "Zm9v"); | |
| 386 | try testAllApis("foob", "Zm9vYg=="); | |
| 387 | try testAllApis("fooba", "Zm9vYmE="); | |
| 388 | try testAllApis("foobar", "Zm9vYmFy"); | |
| 389 | ||
| 390 | try testDecodeIgnoreSpace("", " "); | |
| 391 | try testDecodeIgnoreSpace("f", "Z g= ="); | |
| 392 | try testDecodeIgnoreSpace("fo", " Zm8="); | |
| 393 | try testDecodeIgnoreSpace("foo", "Zm9v "); | |
| 394 | try testDecodeIgnoreSpace("foob", "Zm9vYg = = "); | |
| 395 | try testDecodeIgnoreSpace("fooba", "Zm9v YmE="); | |
| 396 | try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y "); | |
| 397 | 397 | |
| 398 | 398 | // test getting some api errors |
| 399 | %return testError("A", error.InvalidPadding); | |
| 400 | %return testError("AA", error.InvalidPadding); | |
| 401 | %return testError("AAA", error.InvalidPadding); | |
| 402 | %return testError("A..A", error.InvalidCharacter); | |
| 403 | %return testError("AA=A", error.InvalidCharacter); | |
| 404 | %return testError("AA/=", error.InvalidPadding); | |
| 405 | %return testError("A/==", error.InvalidPadding); | |
| 406 | %return testError("A===", error.InvalidCharacter); | |
| 407 | %return testError("====", error.InvalidCharacter); | |
| 408 | ||
| 409 | %return testOutputTooSmallError("AA=="); | |
| 410 | %return testOutputTooSmallError("AAA="); | |
| 411 | %return testOutputTooSmallError("AAAA"); | |
| 412 | %return testOutputTooSmallError("AAAAAA=="); | |
| 399 | try testError("A", error.InvalidPadding); | |
| 400 | try testError("AA", error.InvalidPadding); | |
| 401 | try testError("AAA", error.InvalidPadding); | |
| 402 | try testError("A..A", error.InvalidCharacter); | |
| 403 | try testError("AA=A", error.InvalidCharacter); | |
| 404 | try testError("AA/=", error.InvalidPadding); | |
| 405 | try testError("A/==", error.InvalidPadding); | |
| 406 | try testError("A===", error.InvalidCharacter); | |
| 407 | try testError("====", error.InvalidCharacter); | |
| 408 | ||
| 409 | try testOutputTooSmallError("AA=="); | |
| 410 | try testOutputTooSmallError("AAA="); | |
| 411 | try testOutputTooSmallError("AAAA"); | |
| 412 | try testOutputTooSmallError("AAAAAA=="); | |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %void { |
| ... | ... | @@ -424,8 +424,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %v |
| 424 | 424 | // Base64Decoder |
| 425 | 425 | { |
| 426 | 426 | var buffer: [0x100]u8 = undefined; |
| 427 | var decoded = buffer[0..%return standard_decoder.calcSize(expected_encoded)]; | |
| 428 | %return standard_decoder.decode(decoded, expected_encoded); | |
| 427 | var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)]; | |
| 428 | try standard_decoder.decode(decoded, expected_encoded); | |
| 429 | 429 | assert(mem.eql(u8, decoded, expected_decoded)); |
| 430 | 430 | } |
| 431 | 431 | |
| ... | ... | @@ -434,8 +434,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) -> %v |
| 434 | 434 | const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init( |
| 435 | 435 | standard_alphabet_chars, standard_pad_char, ""); |
| 436 | 436 | var buffer: [0x100]u8 = undefined; |
| 437 | var decoded = buffer[0..%return Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; | |
| 438 | var written = %return standard_decoder_ignore_nothing.decode(decoded, expected_encoded); | |
| 437 | var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; | |
| 438 | var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded); | |
| 439 | 439 | assert(written <= decoded.len); |
| 440 | 440 | assert(mem.eql(u8, decoded[0..written], expected_decoded)); |
| 441 | 441 | } |
| ... | ... | @@ -453,8 +453,8 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) -> % |
| 453 | 453 | const standard_decoder_ignore_space = Base64DecoderWithIgnore.init( |
| 454 | 454 | standard_alphabet_chars, standard_pad_char, " "); |
| 455 | 455 | var buffer: [0x100]u8 = undefined; |
| 456 | var decoded = buffer[0..%return Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; | |
| 457 | var written = %return standard_decoder_ignore_space.decode(decoded, encoded); | |
| 456 | var decoded = buffer[0..try Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; | |
| 457 | var written = try standard_decoder_ignore_space.decode(decoded, encoded); | |
| 458 | 458 | assert(mem.eql(u8, decoded[0..written], expected_decoded)); |
| 459 | 459 | } |
| 460 | 460 |
std/buf_map.zig+6-6| ... | ... | @@ -29,16 +29,16 @@ pub const BufMap = struct { |
| 29 | 29 | |
| 30 | 30 | pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void { |
| 31 | 31 | if (self.hash_map.get(key)) |entry| { |
| 32 | const value_copy = %return self.copy(value); | |
| 32 | const value_copy = try self.copy(value); | |
| 33 | 33 | %defer self.free(value_copy); |
| 34 | _ = %return self.hash_map.put(key, value_copy); | |
| 34 | _ = try self.hash_map.put(key, value_copy); | |
| 35 | 35 | self.free(entry.value); |
| 36 | 36 | } else { |
| 37 | const key_copy = %return self.copy(key); | |
| 37 | const key_copy = try self.copy(key); | |
| 38 | 38 | %defer self.free(key_copy); |
| 39 | const value_copy = %return self.copy(value); | |
| 39 | const value_copy = try self.copy(value); | |
| 40 | 40 | %defer self.free(value_copy); |
| 41 | _ = %return self.hash_map.put(key_copy, value_copy); | |
| 41 | _ = try self.hash_map.put(key_copy, value_copy); | |
| 42 | 42 | } |
| 43 | 43 | } |
| 44 | 44 | |
| ... | ... | @@ -68,7 +68,7 @@ pub const BufMap = struct { |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | fn copy(self: &BufMap, value: []const u8) -> %[]const u8 { |
| 71 | const result = %return self.hash_map.allocator.alloc(u8, value.len); | |
| 71 | const result = try self.hash_map.allocator.alloc(u8, value.len); | |
| 72 | 72 | mem.copy(u8, result, value); |
| 73 | 73 | return result; |
| 74 | 74 | } |
std/buf_set.zig+3-3| ... | ... | @@ -26,9 +26,9 @@ pub const BufSet = struct { |
| 26 | 26 | |
| 27 | 27 | pub fn put(self: &BufSet, key: []const u8) -> %void { |
| 28 | 28 | if (self.hash_map.get(key) == null) { |
| 29 | const key_copy = %return self.copy(key); | |
| 29 | const key_copy = try self.copy(key); | |
| 30 | 30 | %defer self.free(key_copy); |
| 31 | _ = %return self.hash_map.put(key_copy, {}); | |
| 31 | _ = try self.hash_map.put(key_copy, {}); | |
| 32 | 32 | } |
| 33 | 33 | } |
| 34 | 34 | |
| ... | ... | @@ -56,7 +56,7 @@ pub const BufSet = struct { |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | fn copy(self: &BufSet, value: []const u8) -> %[]const u8 { |
| 59 | const result = %return self.hash_map.allocator.alloc(u8, value.len); | |
| 59 | const result = try self.hash_map.allocator.alloc(u8, value.len); | |
| 60 | 60 | mem.copy(u8, result, value); |
| 61 | 61 | return result; |
| 62 | 62 | } |
std/buffer.zig+6-6| ... | ... | @@ -13,7 +13,7 @@ pub const Buffer = struct { |
| 13 | 13 | |
| 14 | 14 | /// Must deinitialize with deinit. |
| 15 | 15 | pub fn init(allocator: &Allocator, m: []const u8) -> %Buffer { |
| 16 | var self = %return initSize(allocator, m.len); | |
| 16 | var self = try initSize(allocator, m.len); | |
| 17 | 17 | mem.copy(u8, self.list.items, m); |
| 18 | 18 | return self; |
| 19 | 19 | } |
| ... | ... | @@ -21,7 +21,7 @@ pub const Buffer = struct { |
| 21 | 21 | /// Must deinitialize with deinit. |
| 22 | 22 | pub fn initSize(allocator: &Allocator, size: usize) -> %Buffer { |
| 23 | 23 | var self = initNull(allocator); |
| 24 | %return self.resize(size); | |
| 24 | try self.resize(size); | |
| 25 | 25 | return self; |
| 26 | 26 | } |
| 27 | 27 | |
| ... | ... | @@ -81,7 +81,7 @@ pub const Buffer = struct { |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | pub fn resize(self: &Buffer, new_len: usize) -> %void { |
| 84 | %return self.list.resize(new_len + 1); | |
| 84 | try self.list.resize(new_len + 1); | |
| 85 | 85 | self.list.items[self.len()] = 0; |
| 86 | 86 | } |
| 87 | 87 | |
| ... | ... | @@ -95,7 +95,7 @@ pub const Buffer = struct { |
| 95 | 95 | |
| 96 | 96 | pub fn append(self: &Buffer, m: []const u8) -> %void { |
| 97 | 97 | const old_len = self.len(); |
| 98 | %return self.resize(old_len + m.len); | |
| 98 | try self.resize(old_len + m.len); | |
| 99 | 99 | mem.copy(u8, self.list.toSlice()[old_len..], m); |
| 100 | 100 | } |
| 101 | 101 | |
| ... | ... | @@ -113,7 +113,7 @@ pub const Buffer = struct { |
| 113 | 113 | pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) -> %void { |
| 114 | 114 | var prev_size: usize = self.len(); |
| 115 | 115 | const new_size = prev_size + count; |
| 116 | %return self.resize(new_size); | |
| 116 | try self.resize(new_size); | |
| 117 | 117 | |
| 118 | 118 | var i: usize = prev_size; |
| 119 | 119 | while (i < new_size) : (i += 1) { |
| ... | ... | @@ -138,7 +138,7 @@ pub const Buffer = struct { |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | pub fn replaceContents(self: &const Buffer, m: []const u8) -> %void { |
| 141 | %return self.resize(m.len); | |
| 141 | try self.resize(m.len); | |
| 142 | 142 | mem.copy(u8, self.list.toSlice(), m); |
| 143 | 143 | } |
| 144 | 144 |
std/build.zig+23-23| ... | ... | @@ -250,13 +250,13 @@ pub const Builder = struct { |
| 250 | 250 | %%wanted_steps.append(&self.default_step); |
| 251 | 251 | } else { |
| 252 | 252 | for (step_names) |step_name| { |
| 253 | const s = %return self.getTopLevelStepByName(step_name); | |
| 253 | const s = try self.getTopLevelStepByName(step_name); | |
| 254 | 254 | %%wanted_steps.append(s); |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | for (wanted_steps.toSliceConst()) |s| { |
| 259 | %return self.makeOneStep(s); | |
| 259 | try self.makeOneStep(s); | |
| 260 | 260 | } |
| 261 | 261 | } |
| 262 | 262 | |
| ... | ... | @@ -310,7 +310,7 @@ pub const Builder = struct { |
| 310 | 310 | |
| 311 | 311 | s.loop_flag = false; |
| 312 | 312 | |
| 313 | %return s.make(); | |
| 313 | try s.make(); | |
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | fn getTopLevelStepByName(self: &Builder, name: []const u8) -> %&Step { |
| ... | ... | @@ -680,7 +680,7 @@ pub const Builder = struct { |
| 680 | 680 | if (os.path.isAbsolute(name)) { |
| 681 | 681 | return name; |
| 682 | 682 | } |
| 683 | const full_path = %return os.path.join(self.allocator, search_prefix, "bin", | |
| 683 | const full_path = try os.path.join(self.allocator, search_prefix, "bin", | |
| 684 | 684 | self.fmt("{}{}", name, exe_extension)); |
| 685 | 685 | if (os.path.real(self.allocator, full_path)) |real_path| { |
| 686 | 686 | return real_path; |
| ... | ... | @@ -696,7 +696,7 @@ pub const Builder = struct { |
| 696 | 696 | } |
| 697 | 697 | var it = mem.split(PATH, []u8{os.path.delimiter}); |
| 698 | 698 | while (it.next()) |path| { |
| 699 | const full_path = %return os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); | |
| 699 | const full_path = try os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); | |
| 700 | 700 | if (os.path.real(self.allocator, full_path)) |real_path| { |
| 701 | 701 | return real_path; |
| 702 | 702 | } else |_| { |
| ... | ... | @@ -710,7 +710,7 @@ pub const Builder = struct { |
| 710 | 710 | return name; |
| 711 | 711 | } |
| 712 | 712 | for (paths) |path| { |
| 713 | const full_path = %return os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); | |
| 713 | const full_path = try os.path.join(self.allocator, path, self.fmt("{}{}", name, exe_extension)); | |
| 714 | 714 | if (os.path.real(self.allocator, full_path)) |real_path| { |
| 715 | 715 | return real_path; |
| 716 | 716 | } else |_| { |
| ... | ... | @@ -1345,10 +1345,10 @@ pub const LibExeObjStep = struct { |
| 1345 | 1345 | } |
| 1346 | 1346 | } |
| 1347 | 1347 | |
| 1348 | %return builder.spawnChild(zig_args.toSliceConst()); | |
| 1348 | try builder.spawnChild(zig_args.toSliceConst()); | |
| 1349 | 1349 | |
| 1350 | 1350 | if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) { |
| 1351 | %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, | |
| 1351 | try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, | |
| 1352 | 1352 | self.name_only_filename); |
| 1353 | 1353 | } |
| 1354 | 1354 | } |
| ... | ... | @@ -1423,7 +1423,7 @@ pub const LibExeObjStep = struct { |
| 1423 | 1423 | |
| 1424 | 1424 | self.appendCompileFlags(&cc_args); |
| 1425 | 1425 | |
| 1426 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1426 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1427 | 1427 | }, |
| 1428 | 1428 | Kind.Lib => { |
| 1429 | 1429 | for (self.source_files.toSliceConst()) |source_file| { |
| ... | ... | @@ -1440,14 +1440,14 @@ pub const LibExeObjStep = struct { |
| 1440 | 1440 | |
| 1441 | 1441 | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file); |
| 1442 | 1442 | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1443 | %return builder.makePath(cache_o_dir); | |
| 1443 | try builder.makePath(cache_o_dir); | |
| 1444 | 1444 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1445 | 1445 | %%cc_args.append("-o"); |
| 1446 | 1446 | %%cc_args.append(builder.pathFromRoot(cache_o_file)); |
| 1447 | 1447 | |
| 1448 | 1448 | self.appendCompileFlags(&cc_args); |
| 1449 | 1449 | |
| 1450 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1450 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1451 | 1451 | |
| 1452 | 1452 | %%self.object_files.append(cache_o_file); |
| 1453 | 1453 | } |
| ... | ... | @@ -1466,14 +1466,14 @@ pub const LibExeObjStep = struct { |
| 1466 | 1466 | %%cc_args.append(builder.pathFromRoot(object_file)); |
| 1467 | 1467 | } |
| 1468 | 1468 | |
| 1469 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1469 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1470 | 1470 | |
| 1471 | 1471 | // ranlib |
| 1472 | 1472 | %%cc_args.resize(0); |
| 1473 | 1473 | %%cc_args.append("ranlib"); |
| 1474 | 1474 | %%cc_args.append(output_path); |
| 1475 | 1475 | |
| 1476 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1476 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1477 | 1477 | } else { |
| 1478 | 1478 | %%cc_args.resize(0); |
| 1479 | 1479 | %%cc_args.append(cc); |
| ... | ... | @@ -1537,10 +1537,10 @@ pub const LibExeObjStep = struct { |
| 1537 | 1537 | } |
| 1538 | 1538 | } |
| 1539 | 1539 | |
| 1540 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1540 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1541 | 1541 | |
| 1542 | 1542 | if (self.target.wantSharedLibSymLinks()) { |
| 1543 | %return doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, | |
| 1543 | try doAtomicSymLinks(builder.allocator, output_path, self.major_only_filename, | |
| 1544 | 1544 | self.name_only_filename); |
| 1545 | 1545 | } |
| 1546 | 1546 | } |
| ... | ... | @@ -1556,7 +1556,7 @@ pub const LibExeObjStep = struct { |
| 1556 | 1556 | |
| 1557 | 1557 | const cache_o_src = %%os.path.join(builder.allocator, builder.cache_root, source_file); |
| 1558 | 1558 | const cache_o_dir = os.path.dirname(cache_o_src); |
| 1559 | %return builder.makePath(cache_o_dir); | |
| 1559 | try builder.makePath(cache_o_dir); | |
| 1560 | 1560 | const cache_o_file = builder.fmt("{}{}", cache_o_src, self.target.oFileExt()); |
| 1561 | 1561 | %%cc_args.append("-o"); |
| 1562 | 1562 | %%cc_args.append(builder.pathFromRoot(cache_o_file)); |
| ... | ... | @@ -1570,7 +1570,7 @@ pub const LibExeObjStep = struct { |
| 1570 | 1570 | %%cc_args.append(builder.pathFromRoot(dir)); |
| 1571 | 1571 | } |
| 1572 | 1572 | |
| 1573 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1573 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1574 | 1574 | |
| 1575 | 1575 | %%self.object_files.append(cache_o_file); |
| 1576 | 1576 | } |
| ... | ... | @@ -1619,7 +1619,7 @@ pub const LibExeObjStep = struct { |
| 1619 | 1619 | } |
| 1620 | 1620 | } |
| 1621 | 1621 | |
| 1622 | %return builder.spawnChild(cc_args.toSliceConst()); | |
| 1622 | try builder.spawnChild(cc_args.toSliceConst()); | |
| 1623 | 1623 | }, |
| 1624 | 1624 | } |
| 1625 | 1625 | } |
| ... | ... | @@ -1770,7 +1770,7 @@ pub const TestStep = struct { |
| 1770 | 1770 | %%zig_args.append(lib_path); |
| 1771 | 1771 | } |
| 1772 | 1772 | |
| 1773 | %return builder.spawnChild(zig_args.toSliceConst()); | |
| 1773 | try builder.spawnChild(zig_args.toSliceConst()); | |
| 1774 | 1774 | } |
| 1775 | 1775 | }; |
| 1776 | 1776 | |
| ... | ... | @@ -1847,9 +1847,9 @@ const InstallArtifactStep = struct { |
| 1847 | 1847 | LibExeObjStep.Kind.Exe => usize(0o755), |
| 1848 | 1848 | LibExeObjStep.Kind.Lib => if (self.artifact.static) usize(0o666) else usize(0o755), |
| 1849 | 1849 | }; |
| 1850 | %return builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); | |
| 1850 | try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); | |
| 1851 | 1851 | if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) { |
| 1852 | %return doAtomicSymLinks(builder.allocator, self.dest_file, | |
| 1852 | try doAtomicSymLinks(builder.allocator, self.dest_file, | |
| 1853 | 1853 | self.artifact.major_only_filename, self.artifact.name_only_filename); |
| 1854 | 1854 | } |
| 1855 | 1855 | } |
| ... | ... | @@ -1872,7 +1872,7 @@ pub const InstallFileStep = struct { |
| 1872 | 1872 | |
| 1873 | 1873 | fn make(step: &Step) -> %void { |
| 1874 | 1874 | const self = @fieldParentPtr(InstallFileStep, "step", step); |
| 1875 | %return self.builder.copyFile(self.src_path, self.dest_path); | |
| 1875 | try self.builder.copyFile(self.src_path, self.dest_path); | |
| 1876 | 1876 | } |
| 1877 | 1877 | }; |
| 1878 | 1878 | |
| ... | ... | @@ -1973,7 +1973,7 @@ pub const Step = struct { |
| 1973 | 1973 | if (self.done_flag) |
| 1974 | 1974 | return; |
| 1975 | 1975 | |
| 1976 | %return self.makeFn(self); | |
| 1976 | try self.makeFn(self); | |
| 1977 | 1977 | self.done_flag = true; |
| 1978 | 1978 | } |
| 1979 | 1979 |
std/cstr.zig+2-2| ... | ... | @@ -43,7 +43,7 @@ fn testCStrFnsImpl() { |
| 43 | 43 | /// have a null byte after it. |
| 44 | 44 | /// Caller owns the returned memory. |
| 45 | 45 | pub fn addNullByte(allocator: &mem.Allocator, slice: []const u8) -> %[]u8 { |
| 46 | const result = %return allocator.alloc(u8, slice.len + 1); | |
| 46 | const result = try allocator.alloc(u8, slice.len + 1); | |
| 47 | 47 | mem.copy(u8, result, slice); |
| 48 | 48 | result[slice.len] = 0; |
| 49 | 49 | return result; |
| ... | ... | @@ -70,7 +70,7 @@ pub const NullTerminated2DArray = struct { |
| 70 | 70 | const index_size = @sizeOf(usize) * new_len; // size of the ptrs |
| 71 | 71 | byte_count += index_size; |
| 72 | 72 | |
| 73 | const buf = %return allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count); | |
| 73 | const buf = try allocator.alignedAlloc(u8, @alignOf(?&u8), byte_count); | |
| 74 | 74 | %defer allocator.free(buf); |
| 75 | 75 | |
| 76 | 76 | var write_index = index_size; |
std/debug/failing_allocator.zig+2-2| ... | ... | @@ -33,7 +33,7 @@ pub const FailingAllocator = struct { |
| 33 | 33 | if (self.index == self.fail_index) { |
| 34 | 34 | return error.OutOfMemory; |
| 35 | 35 | } |
| 36 | const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment); | |
| 36 | const result = try self.internal_allocator.allocFn(self.internal_allocator, n, alignment); | |
| 37 | 37 | self.allocated_bytes += result.len; |
| 38 | 38 | self.index += 1; |
| 39 | 39 | return result; |
| ... | ... | @@ -48,7 +48,7 @@ pub const FailingAllocator = struct { |
| 48 | 48 | if (self.index == self.fail_index) { |
| 49 | 49 | return error.OutOfMemory; |
| 50 | 50 | } |
| 51 | const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment); | |
| 51 | const result = try self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment); | |
| 52 | 52 | self.allocated_bytes += new_size - old_mem.len; |
| 53 | 53 | self.deallocations += 1; |
| 54 | 54 | self.index += 1; |
std/debug/index.zig+124-124| ... | ... | @@ -29,7 +29,7 @@ fn getStderrStream() -> %&io.OutStream { |
| 29 | 29 | if (stderr_stream) |st| { |
| 30 | 30 | return st; |
| 31 | 31 | } else { |
| 32 | stderr_file = %return io.getStdErr(); | |
| 32 | stderr_file = try io.getStdErr(); | |
| 33 | 33 | stderr_file_out_stream = io.FileOutStream.init(&stderr_file); |
| 34 | 34 | const st = &stderr_file_out_stream.stream; |
| 35 | 35 | stderr_stream = st; |
| ... | ... | @@ -118,18 +118,18 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty |
| 118 | 118 | .compile_unit_list = ArrayList(CompileUnit).init(allocator), |
| 119 | 119 | }; |
| 120 | 120 | const st = &stack_trace; |
| 121 | st.self_exe_file = %return os.openSelfExe(); | |
| 121 | st.self_exe_file = try os.openSelfExe(); | |
| 122 | 122 | defer st.self_exe_file.close(); |
| 123 | 123 | |
| 124 | %return st.elf.openFile(allocator, &st.self_exe_file); | |
| 124 | try st.elf.openFile(allocator, &st.self_exe_file); | |
| 125 | 125 | defer st.elf.close(); |
| 126 | 126 | |
| 127 | st.debug_info = (%return st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo; | |
| 128 | st.debug_abbrev = (%return st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo; | |
| 129 | st.debug_str = (%return st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo; | |
| 130 | st.debug_line = (%return st.elf.findSection(".debug_line")) ?? return error.MissingDebugInfo; | |
| 131 | st.debug_ranges = (%return st.elf.findSection(".debug_ranges")); | |
| 132 | %return scanAllCompileUnits(st); | |
| 127 | st.debug_info = (try st.elf.findSection(".debug_info")) ?? return error.MissingDebugInfo; | |
| 128 | st.debug_abbrev = (try st.elf.findSection(".debug_abbrev")) ?? return error.MissingDebugInfo; | |
| 129 | st.debug_str = (try st.elf.findSection(".debug_str")) ?? return error.MissingDebugInfo; | |
| 130 | st.debug_line = (try st.elf.findSection(".debug_line")) ?? return error.MissingDebugInfo; | |
| 131 | st.debug_ranges = (try st.elf.findSection(".debug_ranges")); | |
| 132 | try scanAllCompileUnits(st); | |
| 133 | 133 | |
| 134 | 134 | var ignored_count: usize = 0; |
| 135 | 135 | |
| ... | ... | @@ -147,25 +147,25 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty |
| 147 | 147 | const ptr_hex = if (@sizeOf(usize) == 4) "0x{x8}" else "0x{x16}"; |
| 148 | 148 | |
| 149 | 149 | const compile_unit = findCompileUnit(st, return_address) %% { |
| 150 | %return out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", | |
| 150 | try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", | |
| 151 | 151 | return_address); |
| 152 | 152 | continue; |
| 153 | 153 | }; |
| 154 | const compile_unit_name = %return compile_unit.die.getAttrString(st, DW.AT_name); | |
| 154 | const compile_unit_name = try compile_unit.die.getAttrString(st, DW.AT_name); | |
| 155 | 155 | if (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| { |
| 156 | 156 | defer line_info.deinit(); |
| 157 | %return out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ | |
| 157 | try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ | |
| 158 | 158 | DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", |
| 159 | 159 | line_info.file_name, line_info.line, line_info.column, |
| 160 | 160 | return_address, compile_unit_name); |
| 161 | 161 | if (printLineFromFile(st.allocator(), out_stream, line_info)) { |
| 162 | 162 | if (line_info.column == 0) { |
| 163 | %return out_stream.write("\n"); | |
| 163 | try out_stream.write("\n"); | |
| 164 | 164 | } else { |
| 165 | 165 | {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) { |
| 166 | %return out_stream.writeByte(' '); | |
| 166 | try out_stream.writeByte(' '); | |
| 167 | 167 | }} |
| 168 | %return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); | |
| 168 | try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); | |
| 169 | 169 | } |
| 170 | 170 | } else |err| switch (err) { |
| 171 | 171 | error.EndOfFile, error.PathNotFound => {}, |
| ... | ... | @@ -173,7 +173,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty |
| 173 | 173 | } |
| 174 | 174 | } else |err| switch (err) { |
| 175 | 175 | error.MissingDebugInfo, error.InvalidDebugInfo => { |
| 176 | %return out_stream.print(ptr_hex ++ " in ??? ({})\n", | |
| 176 | try out_stream.print(ptr_hex ++ " in ??? ({})\n", | |
| 177 | 177 | return_address, compile_unit_name); |
| 178 | 178 | }, |
| 179 | 179 | else => return err, |
| ... | ... | @@ -181,22 +181,22 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty |
| 181 | 181 | } |
| 182 | 182 | }, |
| 183 | 183 | builtin.ObjectFormat.coff => { |
| 184 | %return out_stream.write("(stack trace unavailable for COFF object format)\n"); | |
| 184 | try out_stream.write("(stack trace unavailable for COFF object format)\n"); | |
| 185 | 185 | }, |
| 186 | 186 | builtin.ObjectFormat.macho => { |
| 187 | %return out_stream.write("(stack trace unavailable for Mach-O object format)\n"); | |
| 187 | try out_stream.write("(stack trace unavailable for Mach-O object format)\n"); | |
| 188 | 188 | }, |
| 189 | 189 | builtin.ObjectFormat.wasm => { |
| 190 | %return out_stream.write("(stack trace unavailable for WASM object format)\n"); | |
| 190 | try out_stream.write("(stack trace unavailable for WASM object format)\n"); | |
| 191 | 191 | }, |
| 192 | 192 | builtin.ObjectFormat.unknown => { |
| 193 | %return out_stream.write("(stack trace unavailable for unknown object format)\n"); | |
| 193 | try out_stream.write("(stack trace unavailable for unknown object format)\n"); | |
| 194 | 194 | }, |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_info: &const LineInfo) -> %void { |
| 199 | var f = %return io.File.openRead(line_info.file_name, allocator); | |
| 199 | var f = try io.File.openRead(line_info.file_name, allocator); | |
| 200 | 200 | defer f.close(); |
| 201 | 201 | // TODO fstat and make sure that the file has the correct size |
| 202 | 202 | |
| ... | ... | @@ -205,12 +205,12 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: &io.OutStream, line_ |
| 205 | 205 | var column: usize = 1; |
| 206 | 206 | var abs_index: usize = 0; |
| 207 | 207 | while (true) { |
| 208 | const amt_read = %return f.read(buf[0..]); | |
| 208 | const amt_read = try f.read(buf[0..]); | |
| 209 | 209 | const slice = buf[0..amt_read]; |
| 210 | 210 | |
| 211 | 211 | for (slice) |byte| { |
| 212 | 212 | if (line == line_info.line) { |
| 213 | %return out_stream.writeByte(byte); | |
| 213 | try out_stream.writeByte(byte); | |
| 214 | 214 | if (byte == '\n') { |
| 215 | 215 | return; |
| 216 | 216 | } |
| ... | ... | @@ -437,7 +437,7 @@ const LineNumberProgram = struct { |
| 437 | 437 | const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { |
| 438 | 438 | return error.InvalidDebugInfo; |
| 439 | 439 | } else self.include_dirs[file_entry.dir_index]; |
| 440 | const file_name = %return os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); | |
| 440 | const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); | |
| 441 | 441 | %defer self.file_entries.allocator.free(file_name); |
| 442 | 442 | return LineInfo { |
| 443 | 443 | .line = if (self.prev_line >= 0) usize(self.prev_line) else 0, |
| ... | ... | @@ -461,73 +461,73 @@ const LineNumberProgram = struct { |
| 461 | 461 | fn readStringRaw(allocator: &mem.Allocator, in_stream: &io.InStream) -> %[]u8 { |
| 462 | 462 | var buf = ArrayList(u8).init(allocator); |
| 463 | 463 | while (true) { |
| 464 | const byte = %return in_stream.readByte(); | |
| 464 | const byte = try in_stream.readByte(); | |
| 465 | 465 | if (byte == 0) |
| 466 | 466 | break; |
| 467 | %return buf.append(byte); | |
| 467 | try buf.append(byte); | |
| 468 | 468 | } |
| 469 | 469 | return buf.toSlice(); |
| 470 | 470 | } |
| 471 | 471 | |
| 472 | 472 | fn getString(st: &ElfStackTrace, offset: u64) -> %[]u8 { |
| 473 | 473 | const pos = st.debug_str.offset + offset; |
| 474 | %return st.self_exe_file.seekTo(pos); | |
| 474 | try st.self_exe_file.seekTo(pos); | |
| 475 | 475 | return st.readString(); |
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | fn readAllocBytes(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %[]u8 { |
| 479 | const buf = %return global_allocator.alloc(u8, size); | |
| 479 | const buf = try global_allocator.alloc(u8, size); | |
| 480 | 480 | %defer global_allocator.free(buf); |
| 481 | if ((%return in_stream.read(buf)) < size) return error.EndOfFile; | |
| 481 | if ((try in_stream.read(buf)) < size) return error.EndOfFile; | |
| 482 | 482 | return buf; |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { |
| 486 | const buf = %return readAllocBytes(allocator, in_stream, size); | |
| 486 | const buf = try readAllocBytes(allocator, in_stream, size); | |
| 487 | 487 | return FormValue { .Block = buf }; |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { |
| 491 | const block_len = %return in_stream.readVarInt(builtin.Endian.Little, usize, size); | |
| 491 | const block_len = try in_stream.readVarInt(builtin.Endian.Little, usize, size); | |
| 492 | 492 | return parseFormValueBlockLen(allocator, in_stream, block_len); |
| 493 | 493 | } |
| 494 | 494 | |
| 495 | 495 | fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue { |
| 496 | 496 | return FormValue { .Const = Constant { |
| 497 | 497 | .signed = signed, |
| 498 | .payload = %return readAllocBytes(allocator, in_stream, size), | |
| 498 | .payload = try readAllocBytes(allocator, in_stream, size), | |
| 499 | 499 | }}; |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | fn parseFormValueDwarfOffsetSize(in_stream: &io.InStream, is_64: bool) -> %u64 { |
| 503 | return if (is_64) %return in_stream.readIntLe(u64) | |
| 504 | else u64(%return in_stream.readIntLe(u32)) ; | |
| 503 | return if (is_64) try in_stream.readIntLe(u64) | |
| 504 | else u64(try in_stream.readIntLe(u32)) ; | |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 { |
| 508 | return if (@sizeOf(usize) == 4) u64(%return in_stream.readIntLe(u32)) | |
| 509 | else if (@sizeOf(usize) == 8) %return in_stream.readIntLe(u64) | |
| 508 | return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) | |
| 509 | else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) | |
| 510 | 510 | else unreachable; |
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { |
| 514 | const buf = %return readAllocBytes(allocator, in_stream, size); | |
| 514 | const buf = try readAllocBytes(allocator, in_stream, size); | |
| 515 | 515 | return FormValue { .Ref = buf }; |
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | fn parseFormValueRef(allocator: &mem.Allocator, in_stream: &io.InStream, comptime T: type) -> %FormValue { |
| 519 | const block_len = %return in_stream.readIntLe(T); | |
| 519 | const block_len = try in_stream.readIntLe(T); | |
| 520 | 520 | return parseFormValueRefLen(allocator, in_stream, block_len); |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue { |
| 524 | 524 | return switch (form_id) { |
| 525 | DW.FORM_addr => FormValue { .Address = %return parseFormValueTargetAddrSize(in_stream) }, | |
| 525 | DW.FORM_addr => FormValue { .Address = try parseFormValueTargetAddrSize(in_stream) }, | |
| 526 | 526 | DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1), |
| 527 | 527 | DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2), |
| 528 | 528 | DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4), |
| 529 | 529 | DW.FORM_block => x: { |
| 530 | const block_len = %return readULeb128(in_stream); | |
| 530 | const block_len = try readULeb128(in_stream); | |
| 531 | 531 | return parseFormValueBlockLen(allocator, in_stream, block_len); |
| 532 | 532 | }, |
| 533 | 533 | DW.FORM_data1 => parseFormValueConstant(allocator, in_stream, false, 1), |
| ... | ... | @@ -535,35 +535,35 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u |
| 535 | 535 | DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4), |
| 536 | 536 | DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8), |
| 537 | 537 | DW.FORM_udata, DW.FORM_sdata => { |
| 538 | const block_len = %return readULeb128(in_stream); | |
| 538 | const block_len = try readULeb128(in_stream); | |
| 539 | 539 | const signed = form_id == DW.FORM_sdata; |
| 540 | 540 | return parseFormValueConstant(allocator, in_stream, signed, block_len); |
| 541 | 541 | }, |
| 542 | 542 | DW.FORM_exprloc => { |
| 543 | const size = %return readULeb128(in_stream); | |
| 544 | const buf = %return readAllocBytes(allocator, in_stream, size); | |
| 543 | const size = try readULeb128(in_stream); | |
| 544 | const buf = try readAllocBytes(allocator, in_stream, size); | |
| 545 | 545 | return FormValue { .ExprLoc = buf }; |
| 546 | 546 | }, |
| 547 | DW.FORM_flag => FormValue { .Flag = (%return in_stream.readByte()) != 0 }, | |
| 547 | DW.FORM_flag => FormValue { .Flag = (try in_stream.readByte()) != 0 }, | |
| 548 | 548 | DW.FORM_flag_present => FormValue { .Flag = true }, |
| 549 | DW.FORM_sec_offset => FormValue { .SecOffset = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 549 | DW.FORM_sec_offset => FormValue { .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 550 | 550 | |
| 551 | 551 | DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8), |
| 552 | 552 | DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16), |
| 553 | 553 | DW.FORM_ref4 => parseFormValueRef(allocator, in_stream, u32), |
| 554 | 554 | DW.FORM_ref8 => parseFormValueRef(allocator, in_stream, u64), |
| 555 | 555 | DW.FORM_ref_udata => { |
| 556 | const ref_len = %return readULeb128(in_stream); | |
| 556 | const ref_len = try readULeb128(in_stream); | |
| 557 | 557 | return parseFormValueRefLen(allocator, in_stream, ref_len); |
| 558 | 558 | }, |
| 559 | 559 | |
| 560 | DW.FORM_ref_addr => FormValue { .RefAddr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 561 | DW.FORM_ref_sig8 => FormValue { .RefSig8 = %return in_stream.readIntLe(u64) }, | |
| 560 | DW.FORM_ref_addr => FormValue { .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 561 | DW.FORM_ref_sig8 => FormValue { .RefSig8 = try in_stream.readIntLe(u64) }, | |
| 562 | 562 | |
| 563 | DW.FORM_string => FormValue { .String = %return readStringRaw(allocator, in_stream) }, | |
| 564 | DW.FORM_strp => FormValue { .StrPtr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 563 | DW.FORM_string => FormValue { .String = try readStringRaw(allocator, in_stream) }, | |
| 564 | DW.FORM_strp => FormValue { .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) }, | |
| 565 | 565 | DW.FORM_indirect => { |
| 566 | const child_form_id = %return readULeb128(in_stream); | |
| 566 | const child_form_id = try readULeb128(in_stream); | |
| 567 | 567 | return parseFormValue(allocator, in_stream, child_form_id, is_64); |
| 568 | 568 | }, |
| 569 | 569 | else => error.InvalidDebugInfo, |
| ... | ... | @@ -576,23 +576,23 @@ fn parseAbbrevTable(st: &ElfStackTrace) -> %AbbrevTable { |
| 576 | 576 | const in_stream = &in_file_stream.stream; |
| 577 | 577 | var result = AbbrevTable.init(st.allocator()); |
| 578 | 578 | while (true) { |
| 579 | const abbrev_code = %return readULeb128(in_stream); | |
| 579 | const abbrev_code = try readULeb128(in_stream); | |
| 580 | 580 | if (abbrev_code == 0) |
| 581 | 581 | return result; |
| 582 | %return result.append(AbbrevTableEntry { | |
| 582 | try result.append(AbbrevTableEntry { | |
| 583 | 583 | .abbrev_code = abbrev_code, |
| 584 | .tag_id = %return readULeb128(in_stream), | |
| 585 | .has_children = (%return in_stream.readByte()) == DW.CHILDREN_yes, | |
| 584 | .tag_id = try readULeb128(in_stream), | |
| 585 | .has_children = (try in_stream.readByte()) == DW.CHILDREN_yes, | |
| 586 | 586 | .attrs = ArrayList(AbbrevAttr).init(st.allocator()), |
| 587 | 587 | }); |
| 588 | 588 | const attrs = &result.items[result.len - 1].attrs; |
| 589 | 589 | |
| 590 | 590 | while (true) { |
| 591 | const attr_id = %return readULeb128(in_stream); | |
| 592 | const form_id = %return readULeb128(in_stream); | |
| 591 | const attr_id = try readULeb128(in_stream); | |
| 592 | const form_id = try readULeb128(in_stream); | |
| 593 | 593 | if (attr_id == 0 and form_id == 0) |
| 594 | 594 | break; |
| 595 | %return attrs.append(AbbrevAttr { | |
| 595 | try attrs.append(AbbrevAttr { | |
| 596 | 596 | .attr_id = attr_id, |
| 597 | 597 | .form_id = form_id, |
| 598 | 598 | }); |
| ... | ... | @@ -608,10 +608,10 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) -> %&const AbbrevTable |
| 608 | 608 | return &header.table; |
| 609 | 609 | } |
| 610 | 610 | } |
| 611 | %return st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset); | |
| 612 | %return st.abbrev_table_list.append(AbbrevTableHeader { | |
| 611 | try st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset); | |
| 612 | try st.abbrev_table_list.append(AbbrevTableHeader { | |
| 613 | 613 | .offset = abbrev_offset, |
| 614 | .table = %return parseAbbrevTable(st), | |
| 614 | .table = try parseAbbrevTable(st), | |
| 615 | 615 | }); |
| 616 | 616 | return &st.abbrev_table_list.items[st.abbrev_table_list.len - 1].table; |
| 617 | 617 | } |
| ... | ... | @@ -628,7 +628,7 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) - |
| 628 | 628 | const in_file = &st.self_exe_file; |
| 629 | 629 | var in_file_stream = io.FileInStream.init(in_file); |
| 630 | 630 | const in_stream = &in_file_stream.stream; |
| 631 | const abbrev_code = %return readULeb128(in_stream); | |
| 631 | const abbrev_code = try readULeb128(in_stream); | |
| 632 | 632 | const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo; |
| 633 | 633 | |
| 634 | 634 | var result = Die { |
| ... | ... | @@ -636,18 +636,18 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) - |
| 636 | 636 | .has_children = table_entry.has_children, |
| 637 | 637 | .attrs = ArrayList(Die.Attr).init(st.allocator()), |
| 638 | 638 | }; |
| 639 | %return result.attrs.resize(table_entry.attrs.len); | |
| 639 | try result.attrs.resize(table_entry.attrs.len); | |
| 640 | 640 | for (table_entry.attrs.toSliceConst()) |attr, i| { |
| 641 | 641 | result.attrs.items[i] = Die.Attr { |
| 642 | 642 | .id = attr.attr_id, |
| 643 | .value = %return parseFormValue(st.allocator(), in_stream, attr.form_id, is_64), | |
| 643 | .value = try parseFormValue(st.allocator(), in_stream, attr.form_id, is_64), | |
| 644 | 644 | }; |
| 645 | 645 | } |
| 646 | 646 | return result; |
| 647 | 647 | } |
| 648 | 648 | |
| 649 | 649 | fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, target_address: usize) -> %LineInfo { |
| 650 | const compile_unit_cwd = %return compile_unit.die.getAttrString(st, DW.AT_comp_dir); | |
| 650 | const compile_unit_cwd = try compile_unit.die.getAttrString(st, DW.AT_comp_dir); | |
| 651 | 651 | |
| 652 | 652 | const in_file = &st.self_exe_file; |
| 653 | 653 | const debug_line_end = st.debug_line.offset + st.debug_line.size; |
| ... | ... | @@ -658,10 +658,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 658 | 658 | const in_stream = &in_file_stream.stream; |
| 659 | 659 | |
| 660 | 660 | while (this_offset < debug_line_end) : (this_index += 1) { |
| 661 | %return in_file.seekTo(this_offset); | |
| 661 | try in_file.seekTo(this_offset); | |
| 662 | 662 | |
| 663 | 663 | var is_64: bool = undefined; |
| 664 | const unit_length = %return readInitialLength(in_stream, &is_64); | |
| 664 | const unit_length = try readInitialLength(in_stream, &is_64); | |
| 665 | 665 | if (unit_length == 0) |
| 666 | 666 | return error.MissingDebugInfo; |
| 667 | 667 | const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); |
| ... | ... | @@ -671,37 +671,37 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 671 | 671 | continue; |
| 672 | 672 | } |
| 673 | 673 | |
| 674 | const version = %return in_stream.readInt(st.elf.endian, u16); | |
| 674 | const version = try in_stream.readInt(st.elf.endian, u16); | |
| 675 | 675 | if (version != 2) return error.InvalidDebugInfo; |
| 676 | 676 | |
| 677 | const prologue_length = %return in_stream.readInt(st.elf.endian, u32); | |
| 678 | const prog_start_offset = (%return in_file.getPos()) + prologue_length; | |
| 677 | const prologue_length = try in_stream.readInt(st.elf.endian, u32); | |
| 678 | const prog_start_offset = (try in_file.getPos()) + prologue_length; | |
| 679 | 679 | |
| 680 | const minimum_instruction_length = %return in_stream.readByte(); | |
| 680 | const minimum_instruction_length = try in_stream.readByte(); | |
| 681 | 681 | if (minimum_instruction_length == 0) return error.InvalidDebugInfo; |
| 682 | 682 | |
| 683 | const default_is_stmt = (%return in_stream.readByte()) != 0; | |
| 684 | const line_base = %return in_stream.readByteSigned(); | |
| 683 | const default_is_stmt = (try in_stream.readByte()) != 0; | |
| 684 | const line_base = try in_stream.readByteSigned(); | |
| 685 | 685 | |
| 686 | const line_range = %return in_stream.readByte(); | |
| 686 | const line_range = try in_stream.readByte(); | |
| 687 | 687 | if (line_range == 0) |
| 688 | 688 | return error.InvalidDebugInfo; |
| 689 | 689 | |
| 690 | const opcode_base = %return in_stream.readByte(); | |
| 690 | const opcode_base = try in_stream.readByte(); | |
| 691 | 691 | |
| 692 | const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1); | |
| 692 | const standard_opcode_lengths = try st.allocator().alloc(u8, opcode_base - 1); | |
| 693 | 693 | |
| 694 | 694 | {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) { |
| 695 | standard_opcode_lengths[i] = %return in_stream.readByte(); | |
| 695 | standard_opcode_lengths[i] = try in_stream.readByte(); | |
| 696 | 696 | }} |
| 697 | 697 | |
| 698 | 698 | var include_directories = ArrayList([]u8).init(st.allocator()); |
| 699 | %return include_directories.append(compile_unit_cwd); | |
| 699 | try include_directories.append(compile_unit_cwd); | |
| 700 | 700 | while (true) { |
| 701 | const dir = %return st.readString(); | |
| 701 | const dir = try st.readString(); | |
| 702 | 702 | if (dir.len == 0) |
| 703 | 703 | break; |
| 704 | %return include_directories.append(dir); | |
| 704 | try include_directories.append(dir); | |
| 705 | 705 | } |
| 706 | 706 | |
| 707 | 707 | var file_entries = ArrayList(FileEntry).init(st.allocator()); |
| ... | ... | @@ -709,13 +709,13 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 709 | 709 | &file_entries, target_address); |
| 710 | 710 | |
| 711 | 711 | while (true) { |
| 712 | const file_name = %return st.readString(); | |
| 712 | const file_name = try st.readString(); | |
| 713 | 713 | if (file_name.len == 0) |
| 714 | 714 | break; |
| 715 | const dir_index = %return readULeb128(in_stream); | |
| 716 | const mtime = %return readULeb128(in_stream); | |
| 717 | const len_bytes = %return readULeb128(in_stream); | |
| 718 | %return file_entries.append(FileEntry { | |
| 715 | const dir_index = try readULeb128(in_stream); | |
| 716 | const mtime = try readULeb128(in_stream); | |
| 717 | const len_bytes = try readULeb128(in_stream); | |
| 718 | try file_entries.append(FileEntry { | |
| 719 | 719 | .file_name = file_name, |
| 720 | 720 | .dir_index = dir_index, |
| 721 | 721 | .mtime = mtime, |
| ... | ... | @@ -723,33 +723,33 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 723 | 723 | }); |
| 724 | 724 | } |
| 725 | 725 | |
| 726 | %return in_file.seekTo(prog_start_offset); | |
| 726 | try in_file.seekTo(prog_start_offset); | |
| 727 | 727 | |
| 728 | 728 | while (true) { |
| 729 | const opcode = %return in_stream.readByte(); | |
| 729 | const opcode = try in_stream.readByte(); | |
| 730 | 730 | |
| 731 | 731 | var sub_op: u8 = undefined; // TODO move this to the correct scope and fix the compiler crash |
| 732 | 732 | if (opcode == DW.LNS_extended_op) { |
| 733 | const op_size = %return readULeb128(in_stream); | |
| 733 | const op_size = try readULeb128(in_stream); | |
| 734 | 734 | if (op_size < 1) |
| 735 | 735 | return error.InvalidDebugInfo; |
| 736 | sub_op = %return in_stream.readByte(); | |
| 736 | sub_op = try in_stream.readByte(); | |
| 737 | 737 | switch (sub_op) { |
| 738 | 738 | DW.LNE_end_sequence => { |
| 739 | 739 | prog.end_sequence = true; |
| 740 | if (%return prog.checkLineMatch()) |info| return info; | |
| 740 | if (try prog.checkLineMatch()) |info| return info; | |
| 741 | 741 | return error.MissingDebugInfo; |
| 742 | 742 | }, |
| 743 | 743 | DW.LNE_set_address => { |
| 744 | const addr = %return in_stream.readInt(st.elf.endian, usize); | |
| 744 | const addr = try in_stream.readInt(st.elf.endian, usize); | |
| 745 | 745 | prog.address = addr; |
| 746 | 746 | }, |
| 747 | 747 | DW.LNE_define_file => { |
| 748 | const file_name = %return st.readString(); | |
| 749 | const dir_index = %return readULeb128(in_stream); | |
| 750 | const mtime = %return readULeb128(in_stream); | |
| 751 | const len_bytes = %return readULeb128(in_stream); | |
| 752 | %return file_entries.append(FileEntry { | |
| 748 | const file_name = try st.readString(); | |
| 749 | const dir_index = try readULeb128(in_stream); | |
| 750 | const mtime = try readULeb128(in_stream); | |
| 751 | const len_bytes = try readULeb128(in_stream); | |
| 752 | try file_entries.append(FileEntry { | |
| 753 | 753 | .file_name = file_name, |
| 754 | 754 | .dir_index = dir_index, |
| 755 | 755 | .mtime = mtime, |
| ... | ... | @@ -758,7 +758,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 758 | 758 | }, |
| 759 | 759 | else => { |
| 760 | 760 | const fwd_amt = math.cast(isize, op_size - 1) %% return error.InvalidDebugInfo; |
| 761 | %return in_file.seekForward(fwd_amt); | |
| 761 | try in_file.seekForward(fwd_amt); | |
| 762 | 762 | }, |
| 763 | 763 | } |
| 764 | 764 | } else if (opcode >= opcode_base) { |
| ... | ... | @@ -768,28 +768,28 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 768 | 768 | const inc_line = i32(line_base) + i32(adjusted_opcode % line_range); |
| 769 | 769 | prog.line += inc_line; |
| 770 | 770 | prog.address += inc_addr; |
| 771 | if (%return prog.checkLineMatch()) |info| return info; | |
| 771 | if (try prog.checkLineMatch()) |info| return info; | |
| 772 | 772 | prog.basic_block = false; |
| 773 | 773 | } else { |
| 774 | 774 | switch (opcode) { |
| 775 | 775 | DW.LNS_copy => { |
| 776 | if (%return prog.checkLineMatch()) |info| return info; | |
| 776 | if (try prog.checkLineMatch()) |info| return info; | |
| 777 | 777 | prog.basic_block = false; |
| 778 | 778 | }, |
| 779 | 779 | DW.LNS_advance_pc => { |
| 780 | const arg = %return readULeb128(in_stream); | |
| 780 | const arg = try readULeb128(in_stream); | |
| 781 | 781 | prog.address += arg * minimum_instruction_length; |
| 782 | 782 | }, |
| 783 | 783 | DW.LNS_advance_line => { |
| 784 | const arg = %return readILeb128(in_stream); | |
| 784 | const arg = try readILeb128(in_stream); | |
| 785 | 785 | prog.line += arg; |
| 786 | 786 | }, |
| 787 | 787 | DW.LNS_set_file => { |
| 788 | const arg = %return readULeb128(in_stream); | |
| 788 | const arg = try readULeb128(in_stream); | |
| 789 | 789 | prog.file = arg; |
| 790 | 790 | }, |
| 791 | 791 | DW.LNS_set_column => { |
| 792 | const arg = %return readULeb128(in_stream); | |
| 792 | const arg = try readULeb128(in_stream); | |
| 793 | 793 | prog.column = arg; |
| 794 | 794 | }, |
| 795 | 795 | DW.LNS_negate_stmt => { |
| ... | ... | @@ -803,7 +803,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 803 | 803 | prog.address += inc_addr; |
| 804 | 804 | }, |
| 805 | 805 | DW.LNS_fixed_advance_pc => { |
| 806 | const arg = %return in_stream.readInt(st.elf.endian, u16); | |
| 806 | const arg = try in_stream.readInt(st.elf.endian, u16); | |
| 807 | 807 | prog.address += arg; |
| 808 | 808 | }, |
| 809 | 809 | DW.LNS_set_prologue_end => { |
| ... | ... | @@ -812,7 +812,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 812 | 812 | if (opcode - 1 >= standard_opcode_lengths.len) |
| 813 | 813 | return error.InvalidDebugInfo; |
| 814 | 814 | const len_bytes = standard_opcode_lengths[opcode - 1]; |
| 815 | %return in_file.seekForward(len_bytes); | |
| 815 | try in_file.seekForward(len_bytes); | |
| 816 | 816 | }, |
| 817 | 817 | } |
| 818 | 818 | } |
| ... | ... | @@ -833,31 +833,31 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 833 | 833 | const in_stream = &in_file_stream.stream; |
| 834 | 834 | |
| 835 | 835 | while (this_unit_offset < debug_info_end) { |
| 836 | %return st.self_exe_file.seekTo(this_unit_offset); | |
| 836 | try st.self_exe_file.seekTo(this_unit_offset); | |
| 837 | 837 | |
| 838 | 838 | var is_64: bool = undefined; |
| 839 | const unit_length = %return readInitialLength(in_stream, &is_64); | |
| 839 | const unit_length = try readInitialLength(in_stream, &is_64); | |
| 840 | 840 | if (unit_length == 0) |
| 841 | 841 | return; |
| 842 | 842 | const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); |
| 843 | 843 | |
| 844 | const version = %return in_stream.readInt(st.elf.endian, u16); | |
| 844 | const version = try in_stream.readInt(st.elf.endian, u16); | |
| 845 | 845 | if (version < 2 or version > 5) return error.InvalidDebugInfo; |
| 846 | 846 | |
| 847 | 847 | const debug_abbrev_offset = |
| 848 | if (is_64) %return in_stream.readInt(st.elf.endian, u64) | |
| 849 | else %return in_stream.readInt(st.elf.endian, u32); | |
| 848 | if (is_64) try in_stream.readInt(st.elf.endian, u64) | |
| 849 | else try in_stream.readInt(st.elf.endian, u32); | |
| 850 | 850 | |
| 851 | const address_size = %return in_stream.readByte(); | |
| 851 | const address_size = try in_stream.readByte(); | |
| 852 | 852 | if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; |
| 853 | 853 | |
| 854 | const compile_unit_pos = %return st.self_exe_file.getPos(); | |
| 855 | const abbrev_table = %return getAbbrevTable(st, debug_abbrev_offset); | |
| 854 | const compile_unit_pos = try st.self_exe_file.getPos(); | |
| 855 | const abbrev_table = try getAbbrevTable(st, debug_abbrev_offset); | |
| 856 | 856 | |
| 857 | %return st.self_exe_file.seekTo(compile_unit_pos); | |
| 857 | try st.self_exe_file.seekTo(compile_unit_pos); | |
| 858 | 858 | |
| 859 | const compile_unit_die = %return st.allocator().create(Die); | |
| 860 | *compile_unit_die = %return parseDie(st, abbrev_table, is_64); | |
| 859 | const compile_unit_die = try st.allocator().create(Die); | |
| 860 | *compile_unit_die = try parseDie(st, abbrev_table, is_64); | |
| 861 | 861 | |
| 862 | 862 | if (compile_unit_die.tag_id != DW.TAG_compile_unit) |
| 863 | 863 | return error.InvalidDebugInfo; |
| ... | ... | @@ -868,7 +868,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 868 | 868 | const pc_end = switch (*high_pc_value) { |
| 869 | 869 | FormValue.Address => |value| value, |
| 870 | 870 | FormValue.Const => |value| b: { |
| 871 | const offset = %return value.asUnsignedLe(); | |
| 871 | const offset = try value.asUnsignedLe(); | |
| 872 | 872 | break :b (low_pc + offset); |
| 873 | 873 | }, |
| 874 | 874 | else => return error.InvalidDebugInfo, |
| ... | ... | @@ -887,7 +887,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 887 | 887 | } |
| 888 | 888 | }; |
| 889 | 889 | |
| 890 | %return st.compile_unit_list.append(CompileUnit { | |
| 890 | try st.compile_unit_list.append(CompileUnit { | |
| 891 | 891 | .version = version, |
| 892 | 892 | .is_64 = is_64, |
| 893 | 893 | .pc_range = pc_range, |
| ... | ... | @@ -911,10 +911,10 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> %&const CompileUn |
| 911 | 911 | if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| { |
| 912 | 912 | var base_address: usize = 0; |
| 913 | 913 | if (st.debug_ranges) |debug_ranges| { |
| 914 | %return st.self_exe_file.seekTo(debug_ranges.offset + ranges_offset); | |
| 914 | try st.self_exe_file.seekTo(debug_ranges.offset + ranges_offset); | |
| 915 | 915 | while (true) { |
| 916 | const begin_addr = %return in_stream.readIntLe(usize); | |
| 917 | const end_addr = %return in_stream.readIntLe(usize); | |
| 916 | const begin_addr = try in_stream.readIntLe(usize); | |
| 917 | const end_addr = try in_stream.readIntLe(usize); | |
| 918 | 918 | if (begin_addr == 0 and end_addr == 0) { |
| 919 | 919 | break; |
| 920 | 920 | } |
| ... | ... | @@ -937,7 +937,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> %&const CompileUn |
| 937 | 937 | } |
| 938 | 938 | |
| 939 | 939 | fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 { |
| 940 | const first_32_bits = %return in_stream.readIntLe(u32); | |
| 940 | const first_32_bits = try in_stream.readIntLe(u32); | |
| 941 | 941 | *is_64 = (first_32_bits == 0xffffffff); |
| 942 | 942 | if (*is_64) { |
| 943 | 943 | return in_stream.readIntLe(u64); |
| ... | ... | @@ -952,7 +952,7 @@ fn readULeb128(in_stream: &io.InStream) -> %u64 { |
| 952 | 952 | var shift: usize = 0; |
| 953 | 953 | |
| 954 | 954 | while (true) { |
| 955 | const byte = %return in_stream.readByte(); | |
| 955 | const byte = try in_stream.readByte(); | |
| 956 | 956 | |
| 957 | 957 | var operand: u64 = undefined; |
| 958 | 958 | |
| ... | ... | @@ -973,7 +973,7 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { |
| 973 | 973 | var shift: usize = 0; |
| 974 | 974 | |
| 975 | 975 | while (true) { |
| 976 | const byte = %return in_stream.readByte(); | |
| 976 | const byte = try in_stream.readByte(); | |
| 977 | 977 | |
| 978 | 978 | var operand: i64 = undefined; |
| 979 | 979 |
std/elf.zig+53-53| ... | ... | @@ -82,8 +82,8 @@ pub const Elf = struct { |
| 82 | 82 | |
| 83 | 83 | /// Call close when done. |
| 84 | 84 | pub fn openPath(elf: &Elf, allocator: &mem.Allocator, path: []const u8) -> %void { |
| 85 | %return elf.prealloc_file.open(path); | |
| 86 | %return elf.openFile(allocator, &elf.prealloc_file); | |
| 85 | try elf.prealloc_file.open(path); | |
| 86 | try elf.openFile(allocator, &elf.prealloc_file); | |
| 87 | 87 | elf.auto_close_stream = true; |
| 88 | 88 | } |
| 89 | 89 | |
| ... | ... | @@ -97,28 +97,28 @@ pub const Elf = struct { |
| 97 | 97 | const in = &file_stream.stream; |
| 98 | 98 | |
| 99 | 99 | var magic: [4]u8 = undefined; |
| 100 | %return in.readNoEof(magic[0..]); | |
| 100 | try in.readNoEof(magic[0..]); | |
| 101 | 101 | if (!mem.eql(u8, magic, "\x7fELF")) return error.InvalidFormat; |
| 102 | 102 | |
| 103 | elf.is_64 = switch (%return in.readByte()) { | |
| 103 | elf.is_64 = switch (try in.readByte()) { | |
| 104 | 104 | 1 => false, |
| 105 | 105 | 2 => true, |
| 106 | 106 | else => return error.InvalidFormat, |
| 107 | 107 | }; |
| 108 | 108 | |
| 109 | elf.endian = switch (%return in.readByte()) { | |
| 109 | elf.endian = switch (try in.readByte()) { | |
| 110 | 110 | 1 => builtin.Endian.Little, |
| 111 | 111 | 2 => builtin.Endian.Big, |
| 112 | 112 | else => return error.InvalidFormat, |
| 113 | 113 | }; |
| 114 | 114 | |
| 115 | const version_byte = %return in.readByte(); | |
| 115 | const version_byte = try in.readByte(); | |
| 116 | 116 | if (version_byte != 1) return error.InvalidFormat; |
| 117 | 117 | |
| 118 | 118 | // skip over padding |
| 119 | %return elf.in_file.seekForward(9); | |
| 119 | try elf.in_file.seekForward(9); | |
| 120 | 120 | |
| 121 | elf.file_type = switch (%return in.readInt(elf.endian, u16)) { | |
| 121 | elf.file_type = switch (try in.readInt(elf.endian, u16)) { | |
| 122 | 122 | 1 => FileType.Relocatable, |
| 123 | 123 | 2 => FileType.Executable, |
| 124 | 124 | 3 => FileType.Shared, |
| ... | ... | @@ -126,7 +126,7 @@ pub const Elf = struct { |
| 126 | 126 | else => return error.InvalidFormat, |
| 127 | 127 | }; |
| 128 | 128 | |
| 129 | elf.arch = switch (%return in.readInt(elf.endian, u16)) { | |
| 129 | elf.arch = switch (try in.readInt(elf.endian, u16)) { | |
| 130 | 130 | 0x02 => Arch.Sparc, |
| 131 | 131 | 0x03 => Arch.x86, |
| 132 | 132 | 0x08 => Arch.Mips, |
| ... | ... | @@ -139,88 +139,88 @@ pub const Elf = struct { |
| 139 | 139 | else => return error.InvalidFormat, |
| 140 | 140 | }; |
| 141 | 141 | |
| 142 | const elf_version = %return in.readInt(elf.endian, u32); | |
| 142 | const elf_version = try in.readInt(elf.endian, u32); | |
| 143 | 143 | if (elf_version != 1) return error.InvalidFormat; |
| 144 | 144 | |
| 145 | 145 | if (elf.is_64) { |
| 146 | elf.entry_addr = %return in.readInt(elf.endian, u64); | |
| 147 | elf.program_header_offset = %return in.readInt(elf.endian, u64); | |
| 148 | elf.section_header_offset = %return in.readInt(elf.endian, u64); | |
| 146 | elf.entry_addr = try in.readInt(elf.endian, u64); | |
| 147 | elf.program_header_offset = try in.readInt(elf.endian, u64); | |
| 148 | elf.section_header_offset = try in.readInt(elf.endian, u64); | |
| 149 | 149 | } else { |
| 150 | elf.entry_addr = u64(%return in.readInt(elf.endian, u32)); | |
| 151 | elf.program_header_offset = u64(%return in.readInt(elf.endian, u32)); | |
| 152 | elf.section_header_offset = u64(%return in.readInt(elf.endian, u32)); | |
| 150 | elf.entry_addr = u64(try in.readInt(elf.endian, u32)); | |
| 151 | elf.program_header_offset = u64(try in.readInt(elf.endian, u32)); | |
| 152 | elf.section_header_offset = u64(try in.readInt(elf.endian, u32)); | |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | // skip over flags |
| 156 | %return elf.in_file.seekForward(4); | |
| 156 | try elf.in_file.seekForward(4); | |
| 157 | 157 | |
| 158 | const header_size = %return in.readInt(elf.endian, u16); | |
| 158 | const header_size = try in.readInt(elf.endian, u16); | |
| 159 | 159 | if ((elf.is_64 and header_size != 64) or |
| 160 | 160 | (!elf.is_64 and header_size != 52)) |
| 161 | 161 | { |
| 162 | 162 | return error.InvalidFormat; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | const ph_entry_size = %return in.readInt(elf.endian, u16); | |
| 166 | const ph_entry_count = %return in.readInt(elf.endian, u16); | |
| 167 | const sh_entry_size = %return in.readInt(elf.endian, u16); | |
| 168 | const sh_entry_count = %return in.readInt(elf.endian, u16); | |
| 169 | elf.string_section_index = u64(%return in.readInt(elf.endian, u16)); | |
| 165 | const ph_entry_size = try in.readInt(elf.endian, u16); | |
| 166 | const ph_entry_count = try in.readInt(elf.endian, u16); | |
| 167 | const sh_entry_size = try in.readInt(elf.endian, u16); | |
| 168 | const sh_entry_count = try in.readInt(elf.endian, u16); | |
| 169 | elf.string_section_index = u64(try in.readInt(elf.endian, u16)); | |
| 170 | 170 | |
| 171 | 171 | if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat; |
| 172 | 172 | |
| 173 | 173 | const sh_byte_count = u64(sh_entry_size) * u64(sh_entry_count); |
| 174 | const end_sh = %return math.add(u64, elf.section_header_offset, sh_byte_count); | |
| 174 | const end_sh = try math.add(u64, elf.section_header_offset, sh_byte_count); | |
| 175 | 175 | const ph_byte_count = u64(ph_entry_size) * u64(ph_entry_count); |
| 176 | const end_ph = %return math.add(u64, elf.program_header_offset, ph_byte_count); | |
| 176 | const end_ph = try math.add(u64, elf.program_header_offset, ph_byte_count); | |
| 177 | 177 | |
| 178 | const stream_end = %return elf.in_file.getEndPos(); | |
| 178 | const stream_end = try elf.in_file.getEndPos(); | |
| 179 | 179 | if (stream_end < end_sh or stream_end < end_ph) { |
| 180 | 180 | return error.InvalidFormat; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | %return elf.in_file.seekTo(elf.section_header_offset); | |
| 183 | try elf.in_file.seekTo(elf.section_header_offset); | |
| 184 | 184 | |
| 185 | elf.section_headers = %return elf.allocator.alloc(SectionHeader, sh_entry_count); | |
| 185 | elf.section_headers = try elf.allocator.alloc(SectionHeader, sh_entry_count); | |
| 186 | 186 | %defer elf.allocator.free(elf.section_headers); |
| 187 | 187 | |
| 188 | 188 | if (elf.is_64) { |
| 189 | 189 | if (sh_entry_size != 64) return error.InvalidFormat; |
| 190 | 190 | |
| 191 | 191 | for (elf.section_headers) |*elf_section| { |
| 192 | elf_section.name = %return in.readInt(elf.endian, u32); | |
| 193 | elf_section.sh_type = %return in.readInt(elf.endian, u32); | |
| 194 | elf_section.flags = %return in.readInt(elf.endian, u64); | |
| 195 | elf_section.addr = %return in.readInt(elf.endian, u64); | |
| 196 | elf_section.offset = %return in.readInt(elf.endian, u64); | |
| 197 | elf_section.size = %return in.readInt(elf.endian, u64); | |
| 198 | elf_section.link = %return in.readInt(elf.endian, u32); | |
| 199 | elf_section.info = %return in.readInt(elf.endian, u32); | |
| 200 | elf_section.addr_align = %return in.readInt(elf.endian, u64); | |
| 201 | elf_section.ent_size = %return in.readInt(elf.endian, u64); | |
| 192 | elf_section.name = try in.readInt(elf.endian, u32); | |
| 193 | elf_section.sh_type = try in.readInt(elf.endian, u32); | |
| 194 | elf_section.flags = try in.readInt(elf.endian, u64); | |
| 195 | elf_section.addr = try in.readInt(elf.endian, u64); | |
| 196 | elf_section.offset = try in.readInt(elf.endian, u64); | |
| 197 | elf_section.size = try in.readInt(elf.endian, u64); | |
| 198 | elf_section.link = try in.readInt(elf.endian, u32); | |
| 199 | elf_section.info = try in.readInt(elf.endian, u32); | |
| 200 | elf_section.addr_align = try in.readInt(elf.endian, u64); | |
| 201 | elf_section.ent_size = try in.readInt(elf.endian, u64); | |
| 202 | 202 | } |
| 203 | 203 | } else { |
| 204 | 204 | if (sh_entry_size != 40) return error.InvalidFormat; |
| 205 | 205 | |
| 206 | 206 | for (elf.section_headers) |*elf_section| { |
| 207 | 207 | // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ? |
| 208 | elf_section.name = %return in.readInt(elf.endian, u32); | |
| 209 | elf_section.sh_type = %return in.readInt(elf.endian, u32); | |
| 210 | elf_section.flags = u64(%return in.readInt(elf.endian, u32)); | |
| 211 | elf_section.addr = u64(%return in.readInt(elf.endian, u32)); | |
| 212 | elf_section.offset = u64(%return in.readInt(elf.endian, u32)); | |
| 213 | elf_section.size = u64(%return in.readInt(elf.endian, u32)); | |
| 214 | elf_section.link = %return in.readInt(elf.endian, u32); | |
| 215 | elf_section.info = %return in.readInt(elf.endian, u32); | |
| 216 | elf_section.addr_align = u64(%return in.readInt(elf.endian, u32)); | |
| 217 | elf_section.ent_size = u64(%return in.readInt(elf.endian, u32)); | |
| 208 | elf_section.name = try in.readInt(elf.endian, u32); | |
| 209 | elf_section.sh_type = try in.readInt(elf.endian, u32); | |
| 210 | elf_section.flags = u64(try in.readInt(elf.endian, u32)); | |
| 211 | elf_section.addr = u64(try in.readInt(elf.endian, u32)); | |
| 212 | elf_section.offset = u64(try in.readInt(elf.endian, u32)); | |
| 213 | elf_section.size = u64(try in.readInt(elf.endian, u32)); | |
| 214 | elf_section.link = try in.readInt(elf.endian, u32); | |
| 215 | elf_section.info = try in.readInt(elf.endian, u32); | |
| 216 | elf_section.addr_align = u64(try in.readInt(elf.endian, u32)); | |
| 217 | elf_section.ent_size = u64(try in.readInt(elf.endian, u32)); | |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | for (elf.section_headers) |*elf_section| { |
| 222 | 222 | if (elf_section.sh_type != SHT_NOBITS) { |
| 223 | const file_end_offset = %return math.add(u64, elf_section.offset, elf_section.size); | |
| 223 | const file_end_offset = try math.add(u64, elf_section.offset, elf_section.size); | |
| 224 | 224 | if (stream_end < file_end_offset) return error.InvalidFormat; |
| 225 | 225 | } |
| 226 | 226 | } |
| ... | ... | @@ -247,15 +247,15 @@ pub const Elf = struct { |
| 247 | 247 | if (elf_section.sh_type == SHT_NULL) continue; |
| 248 | 248 | |
| 249 | 249 | const name_offset = elf.string_section.offset + elf_section.name; |
| 250 | %return elf.in_file.seekTo(name_offset); | |
| 250 | try elf.in_file.seekTo(name_offset); | |
| 251 | 251 | |
| 252 | 252 | for (name) |expected_c| { |
| 253 | const target_c = %return in.readByte(); | |
| 253 | const target_c = try in.readByte(); | |
| 254 | 254 | if (target_c == 0 or expected_c != target_c) continue :section_loop; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | { |
| 258 | const null_byte = %return in.readByte(); | |
| 258 | const null_byte = try in.readByte(); | |
| 259 | 259 | if (null_byte == 0) return elf_section; |
| 260 | 260 | } |
| 261 | 261 | } |
| ... | ... | @@ -264,6 +264,6 @@ pub const Elf = struct { |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | pub fn seekToSection(elf: &Elf, elf_section: &SectionHeader) -> %void { |
| 267 | %return elf.in_file.seekTo(elf_section.offset); | |
| 267 | try elf.in_file.seekTo(elf_section.offset); | |
| 268 | 268 | } |
| 269 | 269 | }; |
std/fmt/index.zig+34-34| ... | ... | @@ -40,13 +40,13 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 40 | 40 | State.Start => switch (c) { |
| 41 | 41 | '{' => { |
| 42 | 42 | if (start_index < i) { |
| 43 | %return output(context, fmt[start_index..i]); | |
| 43 | try output(context, fmt[start_index..i]); | |
| 44 | 44 | } |
| 45 | 45 | state = State.OpenBrace; |
| 46 | 46 | }, |
| 47 | 47 | '}' => { |
| 48 | 48 | if (start_index < i) { |
| 49 | %return output(context, fmt[start_index..i]); | |
| 49 | try output(context, fmt[start_index..i]); | |
| 50 | 50 | } |
| 51 | 51 | state = State.CloseBrace; |
| 52 | 52 | }, |
| ... | ... | @@ -58,7 +58,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 58 | 58 | start_index = i; |
| 59 | 59 | }, |
| 60 | 60 | '}' => { |
| 61 | %return formatValue(args[next_arg], context, output); | |
| 61 | try formatValue(args[next_arg], context, output); | |
| 62 | 62 | next_arg += 1; |
| 63 | 63 | state = State.Start; |
| 64 | 64 | start_index = i + 1; |
| ... | ... | @@ -110,7 +110,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 110 | 110 | }, |
| 111 | 111 | State.Integer => switch (c) { |
| 112 | 112 | '}' => { |
| 113 | %return formatInt(args[next_arg], radix, uppercase, width, context, output); | |
| 113 | try formatInt(args[next_arg], radix, uppercase, width, context, output); | |
| 114 | 114 | next_arg += 1; |
| 115 | 115 | state = State.Start; |
| 116 | 116 | start_index = i + 1; |
| ... | ... | @@ -124,7 +124,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 124 | 124 | State.IntegerWidth => switch (c) { |
| 125 | 125 | '}' => { |
| 126 | 126 | width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); |
| 127 | %return formatInt(args[next_arg], radix, uppercase, width, context, output); | |
| 127 | try formatInt(args[next_arg], radix, uppercase, width, context, output); | |
| 128 | 128 | next_arg += 1; |
| 129 | 129 | state = State.Start; |
| 130 | 130 | start_index = i + 1; |
| ... | ... | @@ -134,7 +134,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 134 | 134 | }, |
| 135 | 135 | State.Float => switch (c) { |
| 136 | 136 | '}' => { |
| 137 | %return formatFloatDecimal(args[next_arg], 0, context, output); | |
| 137 | try formatFloatDecimal(args[next_arg], 0, context, output); | |
| 138 | 138 | next_arg += 1; |
| 139 | 139 | state = State.Start; |
| 140 | 140 | start_index = i + 1; |
| ... | ... | @@ -148,7 +148,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 148 | 148 | State.FloatWidth => switch (c) { |
| 149 | 149 | '}' => { |
| 150 | 150 | width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); |
| 151 | %return formatFloatDecimal(args[next_arg], width, context, output); | |
| 151 | try formatFloatDecimal(args[next_arg], width, context, output); | |
| 152 | 152 | next_arg += 1; |
| 153 | 153 | state = State.Start; |
| 154 | 154 | start_index = i + 1; |
| ... | ... | @@ -159,7 +159,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 159 | 159 | State.BufWidth => switch (c) { |
| 160 | 160 | '}' => { |
| 161 | 161 | width = comptime %%parseUnsigned(usize, fmt[width_start..i], 10); |
| 162 | %return formatBuf(args[next_arg], width, context, output); | |
| 162 | try formatBuf(args[next_arg], width, context, output); | |
| 163 | 163 | next_arg += 1; |
| 164 | 164 | state = State.Start; |
| 165 | 165 | start_index = i + 1; |
| ... | ... | @@ -169,7 +169,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 169 | 169 | }, |
| 170 | 170 | State.Character => switch (c) { |
| 171 | 171 | '}' => { |
| 172 | %return formatAsciiChar(args[next_arg], context, output); | |
| 172 | try formatAsciiChar(args[next_arg], context, output); | |
| 173 | 173 | next_arg += 1; |
| 174 | 174 | state = State.Start; |
| 175 | 175 | start_index = i + 1; |
| ... | ... | @@ -187,7 +187,7 @@ pub fn format(context: var, output: fn(@typeOf(context), []const u8)->%void, |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | if (start_index < fmt.len) { |
| 190 | %return output(context, fmt[start_index..]); | |
| 190 | try output(context, fmt[start_index..]); | |
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
| ... | ... | @@ -221,7 +221,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons |
| 221 | 221 | } |
| 222 | 222 | }, |
| 223 | 223 | builtin.TypeId.Error => { |
| 224 | %return output(context, "error."); | |
| 224 | try output(context, "error."); | |
| 225 | 225 | return output(context, @errorName(value)); |
| 226 | 226 | }, |
| 227 | 227 | builtin.TypeId.Pointer => { |
| ... | ... | @@ -247,12 +247,12 @@ pub fn formatAsciiChar(c: u8, context: var, output: fn(@typeOf(context), []const |
| 247 | 247 | pub fn formatBuf(buf: []const u8, width: usize, |
| 248 | 248 | context: var, output: fn(@typeOf(context), []const u8)->%void) -> %void |
| 249 | 249 | { |
| 250 | %return output(context, buf); | |
| 250 | try output(context, buf); | |
| 251 | 251 | |
| 252 | 252 | var leftover_padding = if (width > buf.len) (width - buf.len) else return; |
| 253 | 253 | const pad_byte: u8 = ' '; |
| 254 | 254 | while (leftover_padding > 0) : (leftover_padding -= 1) { |
| 255 | %return output(context, (&pad_byte)[0..1]); | |
| 255 | try output(context, (&pad_byte)[0..1]); | |
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
| ... | ... | @@ -264,7 +264,7 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons |
| 264 | 264 | return output(context, "NaN"); |
| 265 | 265 | } |
| 266 | 266 | if (math.signbit(x)) { |
| 267 | %return output(context, "-"); | |
| 267 | try output(context, "-"); | |
| 268 | 268 | x = -x; |
| 269 | 269 | } |
| 270 | 270 | if (math.isPositiveInf(x)) { |
| ... | ... | @@ -276,21 +276,21 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons |
| 276 | 276 | |
| 277 | 277 | var buffer: [32]u8 = undefined; |
| 278 | 278 | const float_decimal = errol3(x, buffer[0..]); |
| 279 | %return output(context, float_decimal.digits[0..1]); | |
| 280 | %return output(context, "."); | |
| 279 | try output(context, float_decimal.digits[0..1]); | |
| 280 | try output(context, "."); | |
| 281 | 281 | if (float_decimal.digits.len > 1) { |
| 282 | 282 | const num_digits = if (@typeOf(value) == f32) |
| 283 | 283 | math.min(usize(9), float_decimal.digits.len) |
| 284 | 284 | else |
| 285 | 285 | float_decimal.digits.len; |
| 286 | %return output(context, float_decimal.digits[1 .. num_digits]); | |
| 286 | try output(context, float_decimal.digits[1 .. num_digits]); | |
| 287 | 287 | } else { |
| 288 | %return output(context, "0"); | |
| 288 | try output(context, "0"); | |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | if (float_decimal.exp != 1) { |
| 292 | %return output(context, "e"); | |
| 293 | %return formatInt(float_decimal.exp - 1, 10, false, 0, context, output); | |
| 292 | try output(context, "e"); | |
| 293 | try formatInt(float_decimal.exp - 1, 10, false, 0, context, output); | |
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | |
| ... | ... | @@ -302,7 +302,7 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn |
| 302 | 302 | return output(context, "NaN"); |
| 303 | 303 | } |
| 304 | 304 | if (math.signbit(x)) { |
| 305 | %return output(context, "-"); | |
| 305 | try output(context, "-"); | |
| 306 | 306 | x = -x; |
| 307 | 307 | } |
| 308 | 308 | if (math.isPositiveInf(x)) { |
| ... | ... | @@ -317,8 +317,8 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn |
| 317 | 317 | |
| 318 | 318 | const num_left_digits = if (float_decimal.exp > 0) usize(float_decimal.exp) else 1; |
| 319 | 319 | |
| 320 | %return output(context, float_decimal.digits[0 .. num_left_digits]); | |
| 321 | %return output(context, "."); | |
| 320 | try output(context, float_decimal.digits[0 .. num_left_digits]); | |
| 321 | try output(context, "."); | |
| 322 | 322 | if (float_decimal.digits.len > 1) { |
| 323 | 323 | const num_valid_digtis = if (@typeOf(value) == f32) math.min(usize(7), float_decimal.digits.len) |
| 324 | 324 | else |
| ... | ... | @@ -328,9 +328,9 @@ pub fn formatFloatDecimal(value: var, precision: usize, context: var, output: fn |
| 328 | 328 | math.min(precision, (num_valid_digtis-num_left_digits)) |
| 329 | 329 | else |
| 330 | 330 | num_valid_digtis - num_left_digits; |
| 331 | %return output(context, float_decimal.digits[num_left_digits .. (num_left_digits + num_right_digits)]); | |
| 331 | try output(context, float_decimal.digits[num_left_digits .. (num_left_digits + num_right_digits)]); | |
| 332 | 332 | } else { |
| 333 | %return output(context, "0"); | |
| 333 | try output(context, "0"); | |
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | 336 | |
| ... | ... | @@ -351,7 +351,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, |
| 351 | 351 | const uint = @IntType(false, @typeOf(value).bit_count); |
| 352 | 352 | if (value < 0) { |
| 353 | 353 | const minus_sign: u8 = '-'; |
| 354 | %return output(context, (&minus_sign)[0..1]); | |
| 354 | try output(context, (&minus_sign)[0..1]); | |
| 355 | 355 | const new_value = uint(-(value + 1)) + 1; |
| 356 | 356 | const new_width = if (width == 0) 0 else (width - 1); |
| 357 | 357 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, output); |
| ... | ... | @@ -359,7 +359,7 @@ fn formatIntSigned(value: var, base: u8, uppercase: bool, width: usize, |
| 359 | 359 | return formatIntUnsigned(uint(value), base, uppercase, width, context, output); |
| 360 | 360 | } else { |
| 361 | 361 | const plus_sign: u8 = '+'; |
| 362 | %return output(context, (&plus_sign)[0..1]); | |
| 362 | try output(context, (&plus_sign)[0..1]); | |
| 363 | 363 | const new_value = uint(value); |
| 364 | 364 | const new_width = if (width == 0) 0 else (width - 1); |
| 365 | 365 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, output); |
| ... | ... | @@ -391,7 +391,7 @@ fn formatIntUnsigned(value: var, base: u8, uppercase: bool, width: usize, |
| 391 | 391 | const zero_byte: u8 = '0'; |
| 392 | 392 | var leftover_padding = padding - index; |
| 393 | 393 | while (true) { |
| 394 | %return output(context, (&zero_byte)[0..1]); | |
| 394 | try output(context, (&zero_byte)[0..1]); | |
| 395 | 395 | leftover_padding -= 1; |
| 396 | 396 | if (leftover_padding == 0) |
| 397 | 397 | break; |
| ... | ... | @@ -428,7 +428,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) -> %T { |
| 428 | 428 | if (buf.len == 0) |
| 429 | 429 | return T(0); |
| 430 | 430 | if (buf[0] == '-') { |
| 431 | return math.negate(%return parseUnsigned(T, buf[1..], radix)); | |
| 431 | return math.negate(try parseUnsigned(T, buf[1..], radix)); | |
| 432 | 432 | } else if (buf[0] == '+') { |
| 433 | 433 | return parseUnsigned(T, buf[1..], radix); |
| 434 | 434 | } else { |
| ... | ... | @@ -450,9 +450,9 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) -> %T { |
| 450 | 450 | var x: T = 0; |
| 451 | 451 | |
| 452 | 452 | for (buf) |c| { |
| 453 | const digit = %return charToDigit(c, radix); | |
| 454 | x = %return math.mul(T, x, radix); | |
| 455 | x = %return math.add(T, x, digit); | |
| 453 | const digit = try charToDigit(c, radix); | |
| 454 | x = try math.mul(T, x, radix); | |
| 455 | x = try math.add(T, x, digit); | |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | return x; |
| ... | ... | @@ -494,7 +494,7 @@ fn bufPrintWrite(context: &BufPrintContext, bytes: []const u8) -> %void { |
| 494 | 494 | |
| 495 | 495 | pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) -> %[]u8 { |
| 496 | 496 | var context = BufPrintContext { .remaining = buf, }; |
| 497 | %return format(&context, bufPrintWrite, fmt, args); | |
| 497 | try format(&context, bufPrintWrite, fmt, args); | |
| 498 | 498 | return buf[0..buf.len - context.remaining.len]; |
| 499 | 499 | } |
| 500 | 500 | |
| ... | ... | @@ -502,7 +502,7 @@ pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ... |
| 502 | 502 | var size: usize = 0; |
| 503 | 503 | // Cannot fail because `countSize` cannot fail. |
| 504 | 504 | %%format(&size, countSize, fmt, args); |
| 505 | const buf = %return allocator.alloc(u8, size); | |
| 505 | const buf = try allocator.alloc(u8, size); | |
| 506 | 506 | return bufPrint(buf, fmt, args); |
| 507 | 507 | } |
| 508 | 508 |
std/hash_map.zig+3-3| ... | ... | @@ -83,14 +83,14 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 83 | 83 | /// Returns the value that was already there. |
| 84 | 84 | pub fn put(hm: &Self, key: K, value: &const V) -> %?V { |
| 85 | 85 | if (hm.entries.len == 0) { |
| 86 | %return hm.initCapacity(16); | |
| 86 | try hm.initCapacity(16); | |
| 87 | 87 | } |
| 88 | 88 | hm.incrementModificationCount(); |
| 89 | 89 | |
| 90 | 90 | // if we get too full (60%), double the capacity |
| 91 | 91 | if (hm.size * 5 >= hm.entries.len * 3) { |
| 92 | 92 | const old_entries = hm.entries; |
| 93 | %return hm.initCapacity(hm.entries.len * 2); | |
| 93 | try hm.initCapacity(hm.entries.len * 2); | |
| 94 | 94 | // dump all of the old elements into the new table |
| 95 | 95 | for (old_entries) |*old_entry| { |
| 96 | 96 | if (old_entry.used) { |
| ... | ... | @@ -149,7 +149,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | fn initCapacity(hm: &Self, capacity: usize) -> %void { |
| 152 | hm.entries = %return hm.allocator.alloc(Entry, capacity); | |
| 152 | hm.entries = try hm.allocator.alloc(Entry, capacity); | |
| 153 | 153 | hm.size = 0; |
| 154 | 154 | hm.max_distance_from_start_index = 0; |
| 155 | 155 | for (hm.entries) |*entry| { |
std/heap.zig+1-1| ... | ... | @@ -124,7 +124,7 @@ pub const IncrementingAllocator = struct { |
| 124 | 124 | if (new_size <= old_mem.len) { |
| 125 | 125 | return old_mem[0..new_size]; |
| 126 | 126 | } else { |
| 127 | const result = %return alloc(allocator, new_size, alignment); | |
| 127 | const result = try alloc(allocator, new_size, alignment); | |
| 128 | 128 | mem.copy(u8, result, old_mem); |
| 129 | 129 | return result; |
| 130 | 130 | } |
std/io.zig+34-34| ... | ... | @@ -51,7 +51,7 @@ error EndOfFile; |
| 51 | 51 | |
| 52 | 52 | pub fn getStdErr() -> %File { |
| 53 | 53 | const handle = if (is_windows) |
| 54 | %return os.windowsGetStdHandle(system.STD_ERROR_HANDLE) | |
| 54 | try os.windowsGetStdHandle(system.STD_ERROR_HANDLE) | |
| 55 | 55 | else if (is_posix) |
| 56 | 56 | system.STDERR_FILENO |
| 57 | 57 | else |
| ... | ... | @@ -61,7 +61,7 @@ pub fn getStdErr() -> %File { |
| 61 | 61 | |
| 62 | 62 | pub fn getStdOut() -> %File { |
| 63 | 63 | const handle = if (is_windows) |
| 64 | %return os.windowsGetStdHandle(system.STD_OUTPUT_HANDLE) | |
| 64 | try os.windowsGetStdHandle(system.STD_OUTPUT_HANDLE) | |
| 65 | 65 | else if (is_posix) |
| 66 | 66 | system.STDOUT_FILENO |
| 67 | 67 | else |
| ... | ... | @@ -71,7 +71,7 @@ pub fn getStdOut() -> %File { |
| 71 | 71 | |
| 72 | 72 | pub fn getStdIn() -> %File { |
| 73 | 73 | const handle = if (is_windows) |
| 74 | %return os.windowsGetStdHandle(system.STD_INPUT_HANDLE) | |
| 74 | try os.windowsGetStdHandle(system.STD_INPUT_HANDLE) | |
| 75 | 75 | else if (is_posix) |
| 76 | 76 | system.STDIN_FILENO |
| 77 | 77 | else |
| ... | ... | @@ -131,10 +131,10 @@ pub const File = struct { |
| 131 | 131 | pub fn openRead(path: []const u8, allocator: ?&mem.Allocator) -> %File { |
| 132 | 132 | if (is_posix) { |
| 133 | 133 | const flags = system.O_LARGEFILE|system.O_RDONLY; |
| 134 | const fd = %return os.posixOpen(path, flags, 0, allocator); | |
| 134 | const fd = try os.posixOpen(path, flags, 0, allocator); | |
| 135 | 135 | return openHandle(fd); |
| 136 | 136 | } else if (is_windows) { |
| 137 | const handle = %return os.windowsOpen(path, system.GENERIC_READ, system.FILE_SHARE_READ, | |
| 137 | const handle = try os.windowsOpen(path, system.GENERIC_READ, system.FILE_SHARE_READ, | |
| 138 | 138 | system.OPEN_EXISTING, system.FILE_ATTRIBUTE_NORMAL, allocator); |
| 139 | 139 | return openHandle(handle); |
| 140 | 140 | } else { |
| ... | ... | @@ -156,10 +156,10 @@ pub const File = struct { |
| 156 | 156 | pub fn openWriteMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) -> %File { |
| 157 | 157 | if (is_posix) { |
| 158 | 158 | const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC; |
| 159 | const fd = %return os.posixOpen(path, flags, mode, allocator); | |
| 159 | const fd = try os.posixOpen(path, flags, mode, allocator); | |
| 160 | 160 | return openHandle(fd); |
| 161 | 161 | } else if (is_windows) { |
| 162 | const handle = %return os.windowsOpen(path, system.GENERIC_WRITE, | |
| 162 | const handle = try os.windowsOpen(path, system.GENERIC_WRITE, | |
| 163 | 163 | system.FILE_SHARE_WRITE|system.FILE_SHARE_READ|system.FILE_SHARE_DELETE, |
| 164 | 164 | system.CREATE_ALWAYS, system.FILE_ATTRIBUTE_NORMAL, allocator); |
| 165 | 165 | return openHandle(handle); |
| ... | ... | @@ -322,9 +322,9 @@ pub const File = struct { |
| 322 | 322 | |
| 323 | 323 | fn write(self: &File, bytes: []const u8) -> %void { |
| 324 | 324 | if (is_posix) { |
| 325 | %return os.posixWrite(self.handle, bytes); | |
| 325 | try os.posixWrite(self.handle, bytes); | |
| 326 | 326 | } else if (is_windows) { |
| 327 | %return os.windowsWrite(self.handle, bytes); | |
| 327 | try os.windowsWrite(self.handle, bytes); | |
| 328 | 328 | } else { |
| 329 | 329 | @compileError("Unsupported OS"); |
| 330 | 330 | } |
| ... | ... | @@ -344,12 +344,12 @@ pub const InStream = struct { |
| 344 | 344 | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and |
| 345 | 345 | /// the contents read from the stream are lost. |
| 346 | 346 | pub fn readAllBuffer(self: &InStream, buffer: &Buffer, max_size: usize) -> %void { |
| 347 | %return buffer.resize(0); | |
| 347 | try buffer.resize(0); | |
| 348 | 348 | |
| 349 | 349 | var actual_buf_len: usize = 0; |
| 350 | 350 | while (true) { |
| 351 | 351 | const dest_slice = buffer.toSlice()[actual_buf_len..]; |
| 352 | const bytes_read = %return self.readFn(self, dest_slice); | |
| 352 | const bytes_read = try self.readFn(self, dest_slice); | |
| 353 | 353 | actual_buf_len += bytes_read; |
| 354 | 354 | |
| 355 | 355 | if (bytes_read != dest_slice.len) { |
| ... | ... | @@ -360,7 +360,7 @@ pub const InStream = struct { |
| 360 | 360 | const new_buf_size = math.min(max_size, actual_buf_len + os.page_size); |
| 361 | 361 | if (new_buf_size == actual_buf_len) |
| 362 | 362 | return error.StreamTooLong; |
| 363 | %return buffer.resize(new_buf_size); | |
| 363 | try buffer.resize(new_buf_size); | |
| 364 | 364 | } |
| 365 | 365 | } |
| 366 | 366 | |
| ... | ... | @@ -372,7 +372,7 @@ pub const InStream = struct { |
| 372 | 372 | var buf = Buffer.initNull(allocator); |
| 373 | 373 | defer buf.deinit(); |
| 374 | 374 | |
| 375 | %return self.readAllBuffer(&buf, max_size); | |
| 375 | try self.readAllBuffer(&buf, max_size); | |
| 376 | 376 | return buf.toOwnedSlice(); |
| 377 | 377 | } |
| 378 | 378 | |
| ... | ... | @@ -381,10 +381,10 @@ pub const InStream = struct { |
| 381 | 381 | /// If `buffer.len()` would exceed `max_size`, `error.StreamTooLong` is returned and the contents |
| 382 | 382 | /// read from the stream so far are lost. |
| 383 | 383 | pub fn readUntilDelimiterBuffer(self: &InStream, buffer: &Buffer, delimiter: u8, max_size: usize) -> %void { |
| 384 | %return buf.resize(0); | |
| 384 | try buf.resize(0); | |
| 385 | 385 | |
| 386 | 386 | while (true) { |
| 387 | var byte: u8 = %return self.readByte(); | |
| 387 | var byte: u8 = try self.readByte(); | |
| 388 | 388 | |
| 389 | 389 | if (byte == delimiter) { |
| 390 | 390 | return; |
| ... | ... | @@ -394,7 +394,7 @@ pub const InStream = struct { |
| 394 | 394 | return error.StreamTooLong; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | %return buf.appendByte(byte); | |
| 397 | try buf.appendByte(byte); | |
| 398 | 398 | } |
| 399 | 399 | } |
| 400 | 400 | |
| ... | ... | @@ -408,7 +408,7 @@ pub const InStream = struct { |
| 408 | 408 | var buf = Buffer.initNull(allocator); |
| 409 | 409 | defer buf.deinit(); |
| 410 | 410 | |
| 411 | %return self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); | |
| 411 | try self.readUntilDelimiterBuffer(self, &buf, delimiter, max_size); | |
| 412 | 412 | return buf.toOwnedSlice(); |
| 413 | 413 | } |
| 414 | 414 | |
| ... | ... | @@ -421,20 +421,20 @@ pub const InStream = struct { |
| 421 | 421 | |
| 422 | 422 | /// Same as `read` but end of stream returns `error.EndOfStream`. |
| 423 | 423 | pub fn readNoEof(self: &InStream, buf: []u8) -> %void { |
| 424 | const amt_read = %return self.read(buf); | |
| 424 | const amt_read = try self.read(buf); | |
| 425 | 425 | if (amt_read < buf.len) return error.EndOfStream; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | /// Reads 1 byte from the stream or returns `error.EndOfStream`. |
| 429 | 429 | pub fn readByte(self: &InStream) -> %u8 { |
| 430 | 430 | var result: [1]u8 = undefined; |
| 431 | %return self.readNoEof(result[0..]); | |
| 431 | try self.readNoEof(result[0..]); | |
| 432 | 432 | return result[0]; |
| 433 | 433 | } |
| 434 | 434 | |
| 435 | 435 | /// Same as `readByte` except the returned byte is signed. |
| 436 | 436 | pub fn readByteSigned(self: &InStream) -> %i8 { |
| 437 | return @bitCast(i8, %return self.readByte()); | |
| 437 | return @bitCast(i8, try self.readByte()); | |
| 438 | 438 | } |
| 439 | 439 | |
| 440 | 440 | pub fn readIntLe(self: &InStream, comptime T: type) -> %T { |
| ... | ... | @@ -447,7 +447,7 @@ pub const InStream = struct { |
| 447 | 447 | |
| 448 | 448 | pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) -> %T { |
| 449 | 449 | var bytes: [@sizeOf(T)]u8 = undefined; |
| 450 | %return self.readNoEof(bytes[0..]); | |
| 450 | try self.readNoEof(bytes[0..]); | |
| 451 | 451 | return mem.readInt(bytes, T, endian); |
| 452 | 452 | } |
| 453 | 453 | |
| ... | ... | @@ -456,7 +456,7 @@ pub const InStream = struct { |
| 456 | 456 | assert(size <= 8); |
| 457 | 457 | var input_buf: [8]u8 = undefined; |
| 458 | 458 | const input_slice = input_buf[0..size]; |
| 459 | %return self.readNoEof(input_slice); | |
| 459 | try self.readNoEof(input_slice); | |
| 460 | 460 | return mem.readInt(input_slice, T, endian); |
| 461 | 461 | } |
| 462 | 462 | |
| ... | ... | @@ -483,7 +483,7 @@ pub const OutStream = struct { |
| 483 | 483 | const slice = (&byte)[0..1]; |
| 484 | 484 | var i: usize = 0; |
| 485 | 485 | while (i < n) : (i += 1) { |
| 486 | %return self.writeFn(self, slice); | |
| 486 | try self.writeFn(self, slice); | |
| 487 | 487 | } |
| 488 | 488 | } |
| 489 | 489 | }; |
| ... | ... | @@ -493,9 +493,9 @@ pub const OutStream = struct { |
| 493 | 493 | /// size buffer is too small, and the provided allocator is null, `error.NameTooLong` is returned. |
| 494 | 494 | /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory. |
| 495 | 495 | pub fn writeFile(path: []const u8, data: []const u8, allocator: ?&mem.Allocator) -> %void { |
| 496 | var file = %return File.openWrite(path, allocator); | |
| 496 | var file = try File.openWrite(path, allocator); | |
| 497 | 497 | defer file.close(); |
| 498 | %return file.write(data); | |
| 498 | try file.write(data); | |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | /// On success, caller owns returned buffer. |
| ... | ... | @@ -505,15 +505,15 @@ pub fn readFileAlloc(path: []const u8, allocator: &mem.Allocator) -> %[]u8 { |
| 505 | 505 | /// On success, caller owns returned buffer. |
| 506 | 506 | /// Allocates extra_len extra bytes at the end of the file buffer, which are uninitialized. |
| 507 | 507 | pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len: usize) -> %[]u8 { |
| 508 | var file = %return File.openRead(path, allocator); | |
| 508 | var file = try File.openRead(path, allocator); | |
| 509 | 509 | defer file.close(); |
| 510 | 510 | |
| 511 | const size = %return file.getEndPos(); | |
| 512 | const buf = %return allocator.alloc(u8, size + extra_len); | |
| 511 | const size = try file.getEndPos(); | |
| 512 | const buf = try allocator.alloc(u8, size + extra_len); | |
| 513 | 513 | %defer allocator.free(buf); |
| 514 | 514 | |
| 515 | 515 | var adapter = FileInStream.init(&file); |
| 516 | %return adapter.stream.readNoEof(buf[0..size]); | |
| 516 | try adapter.stream.readNoEof(buf[0..size]); | |
| 517 | 517 | return buf; |
| 518 | 518 | } |
| 519 | 519 | |
| ... | ... | @@ -565,11 +565,11 @@ pub fn BufferedInStreamCustom(comptime buffer_size: usize) -> type { |
| 565 | 565 | // we can read more data from the unbuffered stream |
| 566 | 566 | if (dest_space < buffer_size) { |
| 567 | 567 | self.start_index = 0; |
| 568 | self.end_index = %return self.unbuffered_in_stream.read(self.buffer[0..]); | |
| 568 | self.end_index = try self.unbuffered_in_stream.read(self.buffer[0..]); | |
| 569 | 569 | } else { |
| 570 | 570 | // asking for so much data that buffering is actually less efficient. |
| 571 | 571 | // forward the request directly to the unbuffered stream |
| 572 | const amt_read = %return self.unbuffered_in_stream.read(dest[dest_index..]); | |
| 572 | const amt_read = try self.unbuffered_in_stream.read(dest[dest_index..]); | |
| 573 | 573 | return dest_index + amt_read; |
| 574 | 574 | } |
| 575 | 575 | } else { |
| ... | ... | @@ -616,7 +616,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { |
| 616 | 616 | if (self.index == 0) |
| 617 | 617 | return; |
| 618 | 618 | |
| 619 | %return self.unbuffered_out_stream.write(self.buffer[0..self.index]); | |
| 619 | try self.unbuffered_out_stream.write(self.buffer[0..self.index]); | |
| 620 | 620 | self.index = 0; |
| 621 | 621 | } |
| 622 | 622 | |
| ... | ... | @@ -624,7 +624,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { |
| 624 | 624 | const self = @fieldParentPtr(Self, "stream", out_stream); |
| 625 | 625 | |
| 626 | 626 | if (bytes.len >= self.buffer.len) { |
| 627 | %return self.flush(); | |
| 627 | try self.flush(); | |
| 628 | 628 | return self.unbuffered_out_stream.write(bytes); |
| 629 | 629 | } |
| 630 | 630 | var src_index: usize = 0; |
| ... | ... | @@ -636,7 +636,7 @@ pub fn BufferedOutStreamCustom(comptime buffer_size: usize) -> type { |
| 636 | 636 | self.index += copy_amt; |
| 637 | 637 | assert(self.index <= self.buffer.len); |
| 638 | 638 | if (self.index == self.buffer.len) { |
| 639 | %return self.flush(); | |
| 639 | try self.flush(); | |
| 640 | 640 | } |
| 641 | 641 | src_index += copy_amt; |
| 642 | 642 | } |
std/linked_list.zig+1-1| ... | ... | @@ -188,7 +188,7 @@ pub fn LinkedList(comptime T: type) -> type { |
| 188 | 188 | /// Returns: |
| 189 | 189 | /// A pointer to the new node. |
| 190 | 190 | pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) -> %&Node { |
| 191 | var node = %return list.allocateNode(allocator); | |
| 191 | var node = try list.allocateNode(allocator); | |
| 192 | 192 | *node = Node.init(data); |
| 193 | 193 | return node; |
| 194 | 194 | } |
std/mem.zig+8-8| ... | ... | @@ -27,7 +27,7 @@ pub const Allocator = struct { |
| 27 | 27 | freeFn: fn (self: &Allocator, old_mem: []u8), |
| 28 | 28 | |
| 29 | 29 | fn create(self: &Allocator, comptime T: type) -> %&T { |
| 30 | const slice = %return self.alloc(T, 1); | |
| 30 | const slice = try self.alloc(T, 1); | |
| 31 | 31 | return &slice[0]; |
| 32 | 32 | } |
| 33 | 33 | |
| ... | ... | @@ -42,8 +42,8 @@ pub const Allocator = struct { |
| 42 | 42 | fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29, |
| 43 | 43 | n: usize) -> %[]align(alignment) T |
| 44 | 44 | { |
| 45 | const byte_count = %return math.mul(usize, @sizeOf(T), n); | |
| 46 | const byte_slice = %return self.allocFn(self, byte_count, alignment); | |
| 45 | const byte_count = try math.mul(usize, @sizeOf(T), n); | |
| 46 | const byte_slice = try self.allocFn(self, byte_count, alignment); | |
| 47 | 47 | // This loop should get optimized out in ReleaseFast mode |
| 48 | 48 | for (byte_slice) |*byte| { |
| 49 | 49 | *byte = undefined; |
| ... | ... | @@ -63,8 +63,8 @@ pub const Allocator = struct { |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | const old_byte_slice = ([]u8)(old_mem); |
| 66 | const byte_count = %return math.mul(usize, @sizeOf(T), n); | |
| 67 | const byte_slice = %return self.reallocFn(self, old_byte_slice, byte_count, alignment); | |
| 66 | const byte_count = try math.mul(usize, @sizeOf(T), n); | |
| 67 | const byte_slice = try self.reallocFn(self, old_byte_slice, byte_count, alignment); | |
| 68 | 68 | // This loop should get optimized out in ReleaseFast mode |
| 69 | 69 | for (byte_slice[old_byte_slice.len..]) |*byte| { |
| 70 | 70 | *byte = undefined; |
| ... | ... | @@ -142,7 +142,7 @@ pub const FixedBufferAllocator = struct { |
| 142 | 142 | if (new_size <= old_mem.len) { |
| 143 | 143 | return old_mem[0..new_size]; |
| 144 | 144 | } else { |
| 145 | const result = %return alloc(allocator, new_size, alignment); | |
| 145 | const result = try alloc(allocator, new_size, alignment); | |
| 146 | 146 | copy(u8, result, old_mem); |
| 147 | 147 | return result; |
| 148 | 148 | } |
| ... | ... | @@ -198,7 +198,7 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) -> bool { |
| 198 | 198 | |
| 199 | 199 | /// Copies ::m to newly allocated memory. Caller is responsible to free it. |
| 200 | 200 | pub fn dupe(allocator: &Allocator, comptime T: type, m: []const T) -> %[]T { |
| 201 | const new_buf = %return allocator.alloc(T, m.len); | |
| 201 | const new_buf = try allocator.alloc(T, m.len); | |
| 202 | 202 | copy(T, new_buf, m); |
| 203 | 203 | return new_buf; |
| 204 | 204 | } |
| ... | ... | @@ -425,7 +425,7 @@ pub fn join(allocator: &Allocator, sep: u8, strings: ...) -> %[]u8 { |
| 425 | 425 | } |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | const buf = %return allocator.alloc(u8, total_strings_len); | |
| 428 | const buf = try allocator.alloc(u8, total_strings_len); | |
| 429 | 429 | %defer allocator.free(buf); |
| 430 | 430 | |
| 431 | 431 | var buf_index: usize = 0; |
std/net.zig+1-1| ... | ... | @@ -133,7 +133,7 @@ pub fn connectAddr(addr: &Address, port: u16) -> %Connection { |
| 133 | 133 | |
| 134 | 134 | pub fn connect(hostname: []const u8, port: u16) -> %Connection { |
| 135 | 135 | var addrs_buf: [1]Address = undefined; |
| 136 | const addrs_slice = %return lookup(hostname, addrs_buf[0..]); | |
| 136 | const addrs_slice = try lookup(hostname, addrs_buf[0..]); | |
| 137 | 137 | const main_addr = &addrs_slice[0]; |
| 138 | 138 | |
| 139 | 139 | return connectAddr(main_addr, port); |
std/os/child_process.zig+45-45| ... | ... | @@ -75,7 +75,7 @@ pub const ChildProcess = struct { |
| 75 | 75 | /// First argument in argv is the executable. |
| 76 | 76 | /// On success must call deinit. |
| 77 | 77 | pub fn init(argv: []const []const u8, allocator: &mem.Allocator) -> %&ChildProcess { |
| 78 | const child = %return allocator.create(ChildProcess); | |
| 78 | const child = try allocator.create(ChildProcess); | |
| 79 | 79 | %defer allocator.destroy(child); |
| 80 | 80 | |
| 81 | 81 | *child = ChildProcess { |
| ... | ... | @@ -104,7 +104,7 @@ pub const ChildProcess = struct { |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | pub fn setUserName(self: &ChildProcess, name: []const u8) -> %void { |
| 107 | const user_info = %return os.getUserInfo(name); | |
| 107 | const user_info = try os.getUserInfo(name); | |
| 108 | 108 | self.uid = user_info.uid; |
| 109 | 109 | self.gid = user_info.gid; |
| 110 | 110 | } |
| ... | ... | @@ -120,7 +120,7 @@ pub const ChildProcess = struct { |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | pub fn spawnAndWait(self: &ChildProcess) -> %Term { |
| 123 | %return self.spawn(); | |
| 123 | try self.spawn(); | |
| 124 | 124 | return self.wait(); |
| 125 | 125 | } |
| 126 | 126 | |
| ... | ... | @@ -200,7 +200,7 @@ pub const ChildProcess = struct { |
| 200 | 200 | child.cwd = cwd; |
| 201 | 201 | child.env_map = env_map; |
| 202 | 202 | |
| 203 | %return child.spawn(); | |
| 203 | try child.spawn(); | |
| 204 | 204 | |
| 205 | 205 | var stdout = Buffer.initNull(allocator); |
| 206 | 206 | var stderr = Buffer.initNull(allocator); |
| ... | ... | @@ -210,11 +210,11 @@ pub const ChildProcess = struct { |
| 210 | 210 | var stdout_file_in_stream = io.FileInStream.init(&??child.stdout); |
| 211 | 211 | var stderr_file_in_stream = io.FileInStream.init(&??child.stderr); |
| 212 | 212 | |
| 213 | %return stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size); | |
| 214 | %return stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size); | |
| 213 | try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size); | |
| 214 | try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size); | |
| 215 | 215 | |
| 216 | 216 | return ExecResult { |
| 217 | .term = %return child.wait(), | |
| 217 | .term = try child.wait(), | |
| 218 | 218 | .stdout = stdout.toOwnedSlice(), |
| 219 | 219 | .stderr = stderr.toOwnedSlice(), |
| 220 | 220 | }; |
| ... | ... | @@ -226,7 +226,7 @@ pub const ChildProcess = struct { |
| 226 | 226 | return term; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | %return self.waitUnwrappedWindows(); | |
| 229 | try self.waitUnwrappedWindows(); | |
| 230 | 230 | return ??self.term; |
| 231 | 231 | } |
| 232 | 232 | |
| ... | ... | @@ -308,8 +308,8 @@ pub const ChildProcess = struct { |
| 308 | 308 | // pid potentially wrote an error. This way we can do a blocking |
| 309 | 309 | // read on the error pipe and either get @maxValue(ErrInt) (no error) or |
| 310 | 310 | // an error code. |
| 311 | %return writeIntFd(self.err_pipe[1], @maxValue(ErrInt)); | |
| 312 | const err_int = %return readIntFd(self.err_pipe[0]); | |
| 311 | try writeIntFd(self.err_pipe[1], @maxValue(ErrInt)); | |
| 312 | const err_int = try readIntFd(self.err_pipe[0]); | |
| 313 | 313 | // Here we potentially return the fork child's error |
| 314 | 314 | // from the parent pid. |
| 315 | 315 | if (err_int != @maxValue(ErrInt)) { |
| ... | ... | @@ -335,18 +335,18 @@ pub const ChildProcess = struct { |
| 335 | 335 | // TODO atomically set a flag saying that we already did this |
| 336 | 336 | install_SIGCHLD_handler(); |
| 337 | 337 | |
| 338 | const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) %return makePipe() else undefined; | |
| 338 | const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined; | |
| 339 | 339 | %defer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); }; |
| 340 | 340 | |
| 341 | const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) %return makePipe() else undefined; | |
| 341 | const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined; | |
| 342 | 342 | %defer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); }; |
| 343 | 343 | |
| 344 | const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) %return makePipe() else undefined; | |
| 344 | const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined; | |
| 345 | 345 | %defer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); }; |
| 346 | 346 | |
| 347 | 347 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); |
| 348 | 348 | const dev_null_fd = if (any_ignore) |
| 349 | %return os.posixOpen("/dev/null", posix.O_RDWR, 0, null) | |
| 349 | try os.posixOpen("/dev/null", posix.O_RDWR, 0, null) | |
| 350 | 350 | else |
| 351 | 351 | undefined |
| 352 | 352 | ; |
| ... | ... | @@ -359,14 +359,14 @@ pub const ChildProcess = struct { |
| 359 | 359 | break :x env_map; |
| 360 | 360 | } else x: { |
| 361 | 361 | we_own_env_map = true; |
| 362 | env_map_owned = %return os.getEnvMap(self.allocator); | |
| 362 | env_map_owned = try os.getEnvMap(self.allocator); | |
| 363 | 363 | break :x &env_map_owned; |
| 364 | 364 | }; |
| 365 | 365 | defer { if (we_own_env_map) env_map_owned.deinit(); } |
| 366 | 366 | |
| 367 | 367 | // This pipe is used to communicate errors between the time of fork |
| 368 | 368 | // and execve from the child process to the parent process. |
| 369 | const err_pipe = %return makePipe(); | |
| 369 | const err_pipe = try makePipe(); | |
| 370 | 370 | %defer destroyPipe(err_pipe); |
| 371 | 371 | |
| 372 | 372 | block_SIGCHLD(); |
| ... | ... | @@ -452,14 +452,14 @@ pub const ChildProcess = struct { |
| 452 | 452 | self.stderr_behavior == StdIo.Ignore); |
| 453 | 453 | |
| 454 | 454 | const nul_handle = if (any_ignore) |
| 455 | %return os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, | |
| 455 | try os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, | |
| 456 | 456 | windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null) |
| 457 | 457 | else |
| 458 | 458 | undefined |
| 459 | 459 | ; |
| 460 | 460 | defer { if (any_ignore) os.close(nul_handle); } |
| 461 | 461 | if (any_ignore) { |
| 462 | %return windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); | |
| 462 | try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); | |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | |
| ... | ... | @@ -467,7 +467,7 @@ pub const ChildProcess = struct { |
| 467 | 467 | var g_hChildStd_IN_Wr: ?windows.HANDLE = null; |
| 468 | 468 | switch (self.stdin_behavior) { |
| 469 | 469 | StdIo.Pipe => { |
| 470 | %return windowsMakePipeIn(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, saAttr); | |
| 470 | try windowsMakePipeIn(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, saAttr); | |
| 471 | 471 | }, |
| 472 | 472 | StdIo.Ignore => { |
| 473 | 473 | g_hChildStd_IN_Rd = nul_handle; |
| ... | ... | @@ -485,7 +485,7 @@ pub const ChildProcess = struct { |
| 485 | 485 | var g_hChildStd_OUT_Wr: ?windows.HANDLE = null; |
| 486 | 486 | switch (self.stdout_behavior) { |
| 487 | 487 | StdIo.Pipe => { |
| 488 | %return windowsMakePipeOut(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, saAttr); | |
| 488 | try windowsMakePipeOut(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, saAttr); | |
| 489 | 489 | }, |
| 490 | 490 | StdIo.Ignore => { |
| 491 | 491 | g_hChildStd_OUT_Wr = nul_handle; |
| ... | ... | @@ -503,7 +503,7 @@ pub const ChildProcess = struct { |
| 503 | 503 | var g_hChildStd_ERR_Wr: ?windows.HANDLE = null; |
| 504 | 504 | switch (self.stderr_behavior) { |
| 505 | 505 | StdIo.Pipe => { |
| 506 | %return windowsMakePipeOut(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, saAttr); | |
| 506 | try windowsMakePipeOut(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, saAttr); | |
| 507 | 507 | }, |
| 508 | 508 | StdIo.Ignore => { |
| 509 | 509 | g_hChildStd_ERR_Wr = nul_handle; |
| ... | ... | @@ -517,7 +517,7 @@ pub const ChildProcess = struct { |
| 517 | 517 | } |
| 518 | 518 | %defer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); }; |
| 519 | 519 | |
| 520 | const cmd_line = %return windowsCreateCommandLine(self.allocator, self.argv); | |
| 520 | const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv); | |
| 521 | 521 | defer self.allocator.free(cmd_line); |
| 522 | 522 | |
| 523 | 523 | var siStartInfo = windows.STARTUPINFOA { |
| ... | ... | @@ -544,7 +544,7 @@ pub const ChildProcess = struct { |
| 544 | 544 | var piProcInfo: windows.PROCESS_INFORMATION = undefined; |
| 545 | 545 | |
| 546 | 546 | const cwd_slice = if (self.cwd) |cwd| |
| 547 | %return cstr.addNullByte(self.allocator, cwd) | |
| 547 | try cstr.addNullByte(self.allocator, cwd) | |
| 548 | 548 | else |
| 549 | 549 | null |
| 550 | 550 | ; |
| ... | ... | @@ -552,7 +552,7 @@ pub const ChildProcess = struct { |
| 552 | 552 | const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null; |
| 553 | 553 | |
| 554 | 554 | const maybe_envp_buf = if (self.env_map) |env_map| |
| 555 | %return os.createWindowsEnvBlock(self.allocator, env_map) | |
| 555 | try os.createWindowsEnvBlock(self.allocator, env_map) | |
| 556 | 556 | else |
| 557 | 557 | null |
| 558 | 558 | ; |
| ... | ... | @@ -563,11 +563,11 @@ pub const ChildProcess = struct { |
| 563 | 563 | // to match posix semantics |
| 564 | 564 | const app_name = x: { |
| 565 | 565 | if (self.cwd) |cwd| { |
| 566 | const resolved = %return os.path.resolve(self.allocator, cwd, self.argv[0]); | |
| 566 | const resolved = try os.path.resolve(self.allocator, cwd, self.argv[0]); | |
| 567 | 567 | defer self.allocator.free(resolved); |
| 568 | break :x %return cstr.addNullByte(self.allocator, resolved); | |
| 568 | break :x try cstr.addNullByte(self.allocator, resolved); | |
| 569 | 569 | } else { |
| 570 | break :x %return cstr.addNullByte(self.allocator, self.argv[0]); | |
| 570 | break :x try cstr.addNullByte(self.allocator, self.argv[0]); | |
| 571 | 571 | } |
| 572 | 572 | }; |
| 573 | 573 | defer self.allocator.free(app_name); |
| ... | ... | @@ -578,12 +578,12 @@ pub const ChildProcess = struct { |
| 578 | 578 | if (no_path_err != error.FileNotFound) |
| 579 | 579 | return no_path_err; |
| 580 | 580 | |
| 581 | const PATH = %return os.getEnvVarOwned(self.allocator, "PATH"); | |
| 581 | const PATH = try os.getEnvVarOwned(self.allocator, "PATH"); | |
| 582 | 582 | defer self.allocator.free(PATH); |
| 583 | 583 | |
| 584 | 584 | var it = mem.split(PATH, ";"); |
| 585 | 585 | while (it.next()) |search_path| { |
| 586 | const joined_path = %return os.path.join(self.allocator, search_path, app_name); | |
| 586 | const joined_path = try os.path.join(self.allocator, search_path, app_name); | |
| 587 | 587 | defer self.allocator.free(joined_path); |
| 588 | 588 | |
| 589 | 589 | if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, |
| ... | ... | @@ -625,10 +625,10 @@ pub const ChildProcess = struct { |
| 625 | 625 | |
| 626 | 626 | fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) -> %void { |
| 627 | 627 | switch (stdio) { |
| 628 | StdIo.Pipe => %return os.posixDup2(pipe_fd, std_fileno), | |
| 628 | StdIo.Pipe => try os.posixDup2(pipe_fd, std_fileno), | |
| 629 | 629 | StdIo.Close => os.close(std_fileno), |
| 630 | 630 | StdIo.Inherit => {}, |
| 631 | StdIo.Ignore => %return os.posixDup2(dev_null_fd, std_fileno), | |
| 631 | StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno), | |
| 632 | 632 | } |
| 633 | 633 | } |
| 634 | 634 | |
| ... | ... | @@ -656,35 +656,35 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ? |
| 656 | 656 | /// Caller must dealloc. |
| 657 | 657 | /// Guarantees a null byte at result[result.len]. |
| 658 | 658 | fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) -> %[]u8 { |
| 659 | var buf = %return Buffer.initSize(allocator, 0); | |
| 659 | var buf = try Buffer.initSize(allocator, 0); | |
| 660 | 660 | defer buf.deinit(); |
| 661 | 661 | |
| 662 | 662 | for (argv) |arg, arg_i| { |
| 663 | 663 | if (arg_i != 0) |
| 664 | %return buf.appendByte(' '); | |
| 664 | try buf.appendByte(' '); | |
| 665 | 665 | if (mem.indexOfAny(u8, arg, " \t\n\"") == null) { |
| 666 | %return buf.append(arg); | |
| 666 | try buf.append(arg); | |
| 667 | 667 | continue; |
| 668 | 668 | } |
| 669 | %return buf.appendByte('"'); | |
| 669 | try buf.appendByte('"'); | |
| 670 | 670 | var backslash_count: usize = 0; |
| 671 | 671 | for (arg) |byte| { |
| 672 | 672 | switch (byte) { |
| 673 | 673 | '\\' => backslash_count += 1, |
| 674 | 674 | '"' => { |
| 675 | %return buf.appendByteNTimes('\\', backslash_count * 2 + 1); | |
| 676 | %return buf.appendByte('"'); | |
| 675 | try buf.appendByteNTimes('\\', backslash_count * 2 + 1); | |
| 676 | try buf.appendByte('"'); | |
| 677 | 677 | backslash_count = 0; |
| 678 | 678 | }, |
| 679 | 679 | else => { |
| 680 | %return buf.appendByteNTimes('\\', backslash_count); | |
| 681 | %return buf.appendByte(byte); | |
| 680 | try buf.appendByteNTimes('\\', backslash_count); | |
| 681 | try buf.appendByte(byte); | |
| 682 | 682 | backslash_count = 0; |
| 683 | 683 | }, |
| 684 | 684 | } |
| 685 | 685 | } |
| 686 | %return buf.appendByteNTimes('\\', backslash_count * 2); | |
| 687 | %return buf.appendByte('"'); | |
| 686 | try buf.appendByteNTimes('\\', backslash_count * 2); | |
| 687 | try buf.appendByte('"'); | |
| 688 | 688 | } |
| 689 | 689 | |
| 690 | 690 | return buf.toOwnedSlice(); |
| ... | ... | @@ -721,9 +721,9 @@ fn windowsSetHandleInfo(h: windows.HANDLE, mask: windows.DWORD, flags: windows.D |
| 721 | 721 | fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) -> %void { |
| 722 | 722 | var rd_h: windows.HANDLE = undefined; |
| 723 | 723 | var wr_h: windows.HANDLE = undefined; |
| 724 | %return windowsMakePipe(&rd_h, &wr_h, sattr); | |
| 724 | try windowsMakePipe(&rd_h, &wr_h, sattr); | |
| 725 | 725 | %defer windowsDestroyPipe(rd_h, wr_h); |
| 726 | %return windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0); | |
| 726 | try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0); | |
| 727 | 727 | *rd = rd_h; |
| 728 | 728 | *wr = wr_h; |
| 729 | 729 | } |
| ... | ... | @@ -731,9 +731,9 @@ fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const S |
| 731 | 731 | fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) -> %void { |
| 732 | 732 | var rd_h: windows.HANDLE = undefined; |
| 733 | 733 | var wr_h: windows.HANDLE = undefined; |
| 734 | %return windowsMakePipe(&rd_h, &wr_h, sattr); | |
| 734 | try windowsMakePipe(&rd_h, &wr_h, sattr); | |
| 735 | 735 | %defer windowsDestroyPipe(rd_h, wr_h); |
| 736 | %return windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0); | |
| 736 | try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0); | |
| 737 | 737 | *rd = rd_h; |
| 738 | 738 | *wr = wr_h; |
| 739 | 739 | } |
std/os/get_user_id.zig+2-2| ... | ... | @@ -31,7 +31,7 @@ error CorruptPasswordFile; |
| 31 | 31 | // like NIS, AD, etc. See `man nss` or look at an strace for `id myuser`. |
| 32 | 32 | |
| 33 | 33 | pub fn posixGetUserInfo(name: []const u8) -> %UserInfo { |
| 34 | var in_stream = %return io.InStream.open("/etc/passwd", null); | |
| 34 | var in_stream = try io.InStream.open("/etc/passwd", null); | |
| 35 | 35 | defer in_stream.close(); |
| 36 | 36 | |
| 37 | 37 | var buf: [os.page_size]u8 = undefined; |
| ... | ... | @@ -41,7 +41,7 @@ pub fn posixGetUserInfo(name: []const u8) -> %UserInfo { |
| 41 | 41 | var gid: u32 = 0; |
| 42 | 42 | |
| 43 | 43 | while (true) { |
| 44 | const amt_read = %return in_stream.read(buf[0..]); | |
| 44 | const amt_read = try in_stream.read(buf[0..]); | |
| 45 | 45 | for (buf[0..amt_read]) |byte| { |
| 46 | 46 | switch (state) { |
| 47 | 47 | State.Start => switch (byte) { |
std/os/index.zig+69-69| ... | ... | @@ -92,11 +92,11 @@ pub fn getRandomBytes(buf: []u8) -> %void { |
| 92 | 92 | return; |
| 93 | 93 | }, |
| 94 | 94 | Os.macosx, Os.ios => { |
| 95 | const fd = %return posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, | |
| 95 | const fd = try posixOpen("/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, | |
| 96 | 96 | 0, null); |
| 97 | 97 | defer close(fd); |
| 98 | 98 | |
| 99 | %return posixRead(fd, buf); | |
| 99 | try posixRead(fd, buf); | |
| 100 | 100 | }, |
| 101 | 101 | Os.windows => { |
| 102 | 102 | var hCryptProv: windows.HCRYPTPROV = undefined; |
| ... | ... | @@ -256,7 +256,7 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al |
| 256 | 256 | if (file_path.len < stack_buf.len) { |
| 257 | 257 | path0 = stack_buf[0..file_path.len + 1]; |
| 258 | 258 | } else if (allocator) |a| { |
| 259 | path0 = %return a.alloc(u8, file_path.len + 1); | |
| 259 | path0 = try a.alloc(u8, file_path.len + 1); | |
| 260 | 260 | need_free = true; |
| 261 | 261 | } else { |
| 262 | 262 | return error.NameTooLong; |
| ... | ... | @@ -314,14 +314,14 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) -> %void { |
| 314 | 314 | |
| 315 | 315 | pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap) -> %[]?&u8 { |
| 316 | 316 | const envp_count = env_map.count(); |
| 317 | const envp_buf = %return allocator.alloc(?&u8, envp_count + 1); | |
| 317 | const envp_buf = try allocator.alloc(?&u8, envp_count + 1); | |
| 318 | 318 | mem.set(?&u8, envp_buf, null); |
| 319 | 319 | %defer freeNullDelimitedEnvMap(allocator, envp_buf); |
| 320 | 320 | { |
| 321 | 321 | var it = env_map.iterator(); |
| 322 | 322 | var i: usize = 0; |
| 323 | 323 | while (it.next()) |pair| : (i += 1) { |
| 324 | const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2); | |
| 324 | const env_buf = try allocator.alloc(u8, pair.key.len + pair.value.len + 2); | |
| 325 | 325 | @memcpy(&env_buf[0], pair.key.ptr, pair.key.len); |
| 326 | 326 | env_buf[pair.key.len] = '='; |
| 327 | 327 | @memcpy(&env_buf[pair.key.len + 1], pair.value.ptr, pair.value.len); |
| ... | ... | @@ -351,7 +351,7 @@ pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) { |
| 351 | 351 | pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, |
| 352 | 352 | allocator: &Allocator) -> %void |
| 353 | 353 | { |
| 354 | const argv_buf = %return allocator.alloc(?&u8, argv.len + 1); | |
| 354 | const argv_buf = try allocator.alloc(?&u8, argv.len + 1); | |
| 355 | 355 | mem.set(?&u8, argv_buf, null); |
| 356 | 356 | defer { |
| 357 | 357 | for (argv_buf) |arg| { |
| ... | ... | @@ -361,7 +361,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, |
| 361 | 361 | allocator.free(argv_buf); |
| 362 | 362 | } |
| 363 | 363 | for (argv) |arg, i| { |
| 364 | const arg_buf = %return allocator.alloc(u8, arg.len + 1); | |
| 364 | const arg_buf = try allocator.alloc(u8, arg.len + 1); | |
| 365 | 365 | @memcpy(&arg_buf[0], arg.ptr, arg.len); |
| 366 | 366 | arg_buf[arg.len] = 0; |
| 367 | 367 | |
| ... | ... | @@ -369,7 +369,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, |
| 369 | 369 | } |
| 370 | 370 | argv_buf[argv.len] = null; |
| 371 | 371 | |
| 372 | const envp_buf = %return createNullDelimitedEnvMap(allocator, env_map); | |
| 372 | const envp_buf = try createNullDelimitedEnvMap(allocator, env_map); | |
| 373 | 373 | defer freeNullDelimitedEnvMap(allocator, envp_buf); |
| 374 | 374 | |
| 375 | 375 | const exe_path = argv[0]; |
| ... | ... | @@ -381,7 +381,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, |
| 381 | 381 | // PATH.len because it is >= the largest search_path |
| 382 | 382 | // +1 for the / to join the search path and exe_path |
| 383 | 383 | // +1 for the null terminating byte |
| 384 | const path_buf = %return allocator.alloc(u8, PATH.len + exe_path.len + 2); | |
| 384 | const path_buf = try allocator.alloc(u8, PATH.len + exe_path.len + 2); | |
| 385 | 385 | defer allocator.free(path_buf); |
| 386 | 386 | var it = mem.split(PATH, ":"); |
| 387 | 387 | var seen_eacces = false; |
| ... | ... | @@ -450,7 +450,7 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { |
| 450 | 450 | |
| 451 | 451 | i += 1; // skip over null byte |
| 452 | 452 | |
| 453 | %return result.set(key, value); | |
| 453 | try result.set(key, value); | |
| 454 | 454 | } |
| 455 | 455 | } else { |
| 456 | 456 | for (posix_environ_raw) |ptr| { |
| ... | ... | @@ -462,7 +462,7 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { |
| 462 | 462 | while (ptr[end_i] != 0) : (end_i += 1) {} |
| 463 | 463 | const value = ptr[line_i + 1..end_i]; |
| 464 | 464 | |
| 465 | %return result.set(key, value); | |
| 465 | try result.set(key, value); | |
| 466 | 466 | } |
| 467 | 467 | return result; |
| 468 | 468 | } |
| ... | ... | @@ -490,14 +490,14 @@ error EnvironmentVariableNotFound; |
| 490 | 490 | /// Caller must free returned memory. |
| 491 | 491 | pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { |
| 492 | 492 | if (is_windows) { |
| 493 | const key_with_null = %return cstr.addNullByte(allocator, key); | |
| 493 | const key_with_null = try cstr.addNullByte(allocator, key); | |
| 494 | 494 | defer allocator.free(key_with_null); |
| 495 | 495 | |
| 496 | var buf = %return allocator.alloc(u8, 256); | |
| 496 | var buf = try allocator.alloc(u8, 256); | |
| 497 | 497 | %defer allocator.free(buf); |
| 498 | 498 | |
| 499 | 499 | while (true) { |
| 500 | const windows_buf_len = %return math.cast(windows.DWORD, buf.len); | |
| 500 | const windows_buf_len = try math.cast(windows.DWORD, buf.len); | |
| 501 | 501 | const result = windows.GetEnvironmentVariableA(key_with_null.ptr, buf.ptr, windows_buf_len); |
| 502 | 502 | |
| 503 | 503 | if (result == 0) { |
| ... | ... | @@ -509,7 +509,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { |
| 509 | 509 | } |
| 510 | 510 | |
| 511 | 511 | if (result > buf.len) { |
| 512 | buf = %return allocator.realloc(u8, buf, result); | |
| 512 | buf = try allocator.realloc(u8, buf, result); | |
| 513 | 513 | continue; |
| 514 | 514 | } |
| 515 | 515 | |
| ... | ... | @@ -525,7 +525,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 { |
| 525 | 525 | pub fn getCwd(allocator: &Allocator) -> %[]u8 { |
| 526 | 526 | switch (builtin.os) { |
| 527 | 527 | Os.windows => { |
| 528 | var buf = %return allocator.alloc(u8, 256); | |
| 528 | var buf = try allocator.alloc(u8, 256); | |
| 529 | 529 | %defer allocator.free(buf); |
| 530 | 530 | |
| 531 | 531 | while (true) { |
| ... | ... | @@ -539,7 +539,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 { |
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | if (result > buf.len) { |
| 542 | buf = %return allocator.realloc(u8, buf, result); | |
| 542 | buf = try allocator.realloc(u8, buf, result); | |
| 543 | 543 | continue; |
| 544 | 544 | } |
| 545 | 545 | |
| ... | ... | @@ -547,12 +547,12 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 { |
| 547 | 547 | } |
| 548 | 548 | }, |
| 549 | 549 | else => { |
| 550 | var buf = %return allocator.alloc(u8, 1024); | |
| 550 | var buf = try allocator.alloc(u8, 1024); | |
| 551 | 551 | %defer allocator.free(buf); |
| 552 | 552 | while (true) { |
| 553 | 553 | const err = posix.getErrno(posix.getcwd(buf.ptr, buf.len)); |
| 554 | 554 | if (err == posix.ERANGE) { |
| 555 | buf = %return allocator.realloc(u8, buf, buf.len * 2); | |
| 555 | buf = try allocator.realloc(u8, buf, buf.len * 2); | |
| 556 | 556 | continue; |
| 557 | 557 | } else if (err > 0) { |
| 558 | 558 | return unexpectedErrorPosix(err); |
| ... | ... | @@ -578,9 +578,9 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con |
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void { |
| 581 | const existing_with_null = %return cstr.addNullByte(allocator, existing_path); | |
| 581 | const existing_with_null = try cstr.addNullByte(allocator, existing_path); | |
| 582 | 582 | defer allocator.free(existing_with_null); |
| 583 | const new_with_null = %return cstr.addNullByte(allocator, new_path); | |
| 583 | const new_with_null = try cstr.addNullByte(allocator, new_path); | |
| 584 | 584 | defer allocator.free(new_with_null); |
| 585 | 585 | |
| 586 | 586 | if (windows.CreateSymbolicLinkA(existing_with_null.ptr, new_with_null.ptr, 0) == 0) { |
| ... | ... | @@ -592,7 +592,7 @@ pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path |
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void { |
| 595 | const full_buf = %return allocator.alloc(u8, existing_path.len + new_path.len + 2); | |
| 595 | const full_buf = try allocator.alloc(u8, existing_path.len + new_path.len + 2); | |
| 596 | 596 | defer allocator.free(full_buf); |
| 597 | 597 | |
| 598 | 598 | const existing_buf = full_buf; |
| ... | ... | @@ -638,11 +638,11 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: |
| 638 | 638 | } |
| 639 | 639 | |
| 640 | 640 | var rand_buf: [12]u8 = undefined; |
| 641 | const tmp_path = %return allocator.alloc(u8, new_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); | |
| 641 | const tmp_path = try allocator.alloc(u8, new_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); | |
| 642 | 642 | defer allocator.free(tmp_path); |
| 643 | 643 | mem.copy(u8, tmp_path[0..], new_path); |
| 644 | 644 | while (true) { |
| 645 | %return getRandomBytes(rand_buf[0..]); | |
| 645 | try getRandomBytes(rand_buf[0..]); | |
| 646 | 646 | b64_fs_encoder.encode(tmp_path[new_path.len..], rand_buf); |
| 647 | 647 | if (symLink(allocator, existing_path, tmp_path)) { |
| 648 | 648 | return rename(allocator, tmp_path, new_path); |
| ... | ... | @@ -669,7 +669,7 @@ error FileNotFound; |
| 669 | 669 | error AccessDenied; |
| 670 | 670 | |
| 671 | 671 | pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void { |
| 672 | const buf = %return allocator.alloc(u8, file_path.len + 1); | |
| 672 | const buf = try allocator.alloc(u8, file_path.len + 1); | |
| 673 | 673 | defer allocator.free(buf); |
| 674 | 674 | |
| 675 | 675 | mem.copy(u8, buf, file_path); |
| ... | ... | @@ -687,7 +687,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void |
| 687 | 687 | } |
| 688 | 688 | |
| 689 | 689 | pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void { |
| 690 | const buf = %return allocator.alloc(u8, file_path.len + 1); | |
| 690 | const buf = try allocator.alloc(u8, file_path.len + 1); | |
| 691 | 691 | defer allocator.free(buf); |
| 692 | 692 | |
| 693 | 693 | mem.copy(u8, buf, file_path); |
| ... | ... | @@ -721,30 +721,30 @@ pub fn copyFile(allocator: &Allocator, source_path: []const u8, dest_path: []con |
| 721 | 721 | /// Guaranteed to be atomic. |
| 722 | 722 | pub fn copyFileMode(allocator: &Allocator, source_path: []const u8, dest_path: []const u8, mode: usize) -> %void { |
| 723 | 723 | var rand_buf: [12]u8 = undefined; |
| 724 | const tmp_path = %return allocator.alloc(u8, dest_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); | |
| 724 | const tmp_path = try allocator.alloc(u8, dest_path.len + base64.Base64Encoder.calcSize(rand_buf.len)); | |
| 725 | 725 | defer allocator.free(tmp_path); |
| 726 | 726 | mem.copy(u8, tmp_path[0..], dest_path); |
| 727 | %return getRandomBytes(rand_buf[0..]); | |
| 727 | try getRandomBytes(rand_buf[0..]); | |
| 728 | 728 | b64_fs_encoder.encode(tmp_path[dest_path.len..], rand_buf); |
| 729 | 729 | |
| 730 | var out_file = %return io.File.openWriteMode(tmp_path, mode, allocator); | |
| 730 | var out_file = try io.File.openWriteMode(tmp_path, mode, allocator); | |
| 731 | 731 | defer out_file.close(); |
| 732 | 732 | %defer _ = deleteFile(allocator, tmp_path); |
| 733 | 733 | |
| 734 | var in_file = %return io.File.openRead(source_path, allocator); | |
| 734 | var in_file = try io.File.openRead(source_path, allocator); | |
| 735 | 735 | defer in_file.close(); |
| 736 | 736 | |
| 737 | 737 | var buf: [page_size]u8 = undefined; |
| 738 | 738 | while (true) { |
| 739 | const amt = %return in_file.read(buf[0..]); | |
| 740 | %return out_file.write(buf[0..amt]); | |
| 739 | const amt = try in_file.read(buf[0..]); | |
| 740 | try out_file.write(buf[0..amt]); | |
| 741 | 741 | if (amt != buf.len) |
| 742 | 742 | return rename(allocator, tmp_path, dest_path); |
| 743 | 743 | } |
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8) -> %void { |
| 747 | const full_buf = %return allocator.alloc(u8, old_path.len + new_path.len + 2); | |
| 747 | const full_buf = try allocator.alloc(u8, old_path.len + new_path.len + 2); | |
| 748 | 748 | defer allocator.free(full_buf); |
| 749 | 749 | |
| 750 | 750 | const old_buf = full_buf; |
| ... | ... | @@ -797,7 +797,7 @@ pub fn makeDir(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 797 | 797 | } |
| 798 | 798 | |
| 799 | 799 | pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 800 | const path_buf = %return cstr.addNullByte(allocator, dir_path); | |
| 800 | const path_buf = try cstr.addNullByte(allocator, dir_path); | |
| 801 | 801 | defer allocator.free(path_buf); |
| 802 | 802 | |
| 803 | 803 | if (windows.CreateDirectoryA(path_buf.ptr, null) == 0) { |
| ... | ... | @@ -811,7 +811,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 811 | 811 | } |
| 812 | 812 | |
| 813 | 813 | pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 814 | const path_buf = %return cstr.addNullByte(allocator, dir_path); | |
| 814 | const path_buf = try cstr.addNullByte(allocator, dir_path); | |
| 815 | 815 | defer allocator.free(path_buf); |
| 816 | 816 | |
| 817 | 817 | const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755)); |
| ... | ... | @@ -837,7 +837,7 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 837 | 837 | /// Calls makeDir recursively to make an entire path. Returns success if the path |
| 838 | 838 | /// already exists and is a directory. |
| 839 | 839 | pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void { |
| 840 | const resolved_path = %return path.resolve(allocator, full_path); | |
| 840 | const resolved_path = try path.resolve(allocator, full_path); | |
| 841 | 841 | defer allocator.free(resolved_path); |
| 842 | 842 | |
| 843 | 843 | var end_index: usize = resolved_path.len; |
| ... | ... | @@ -875,7 +875,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void { |
| 875 | 875 | /// Returns ::error.DirNotEmpty if the directory is not empty. |
| 876 | 876 | /// To delete a directory recursively, see ::deleteTree |
| 877 | 877 | pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 878 | const path_buf = %return allocator.alloc(u8, dir_path.len + 1); | |
| 878 | const path_buf = try allocator.alloc(u8, dir_path.len + 1); | |
| 879 | 879 | defer allocator.free(path_buf); |
| 880 | 880 | |
| 881 | 881 | mem.copy(u8, path_buf, dir_path); |
| ... | ... | @@ -927,14 +927,14 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) -> %void { |
| 927 | 927 | var full_entry_buf = ArrayList(u8).init(allocator); |
| 928 | 928 | defer full_entry_buf.deinit(); |
| 929 | 929 | |
| 930 | while (%return dir.next()) |entry| { | |
| 931 | %return full_entry_buf.resize(full_path.len + entry.name.len + 1); | |
| 930 | while (try dir.next()) |entry| { | |
| 931 | try full_entry_buf.resize(full_path.len + entry.name.len + 1); | |
| 932 | 932 | const full_entry_path = full_entry_buf.toSlice(); |
| 933 | 933 | mem.copy(u8, full_entry_path, full_path); |
| 934 | 934 | full_entry_path[full_path.len] = '/'; |
| 935 | 935 | mem.copy(u8, full_entry_path[full_path.len + 1..], entry.name); |
| 936 | 936 | |
| 937 | %return deleteTree(allocator, full_entry_path); | |
| 937 | try deleteTree(allocator, full_entry_path); | |
| 938 | 938 | } |
| 939 | 939 | } |
| 940 | 940 | return deleteDir(allocator, full_path); |
| ... | ... | @@ -973,7 +973,7 @@ pub const Dir = struct { |
| 973 | 973 | }; |
| 974 | 974 | |
| 975 | 975 | pub fn open(allocator: &Allocator, dir_path: []const u8) -> %Dir { |
| 976 | const fd = %return posixOpen(dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0, allocator); | |
| 976 | const fd = try posixOpen(dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0, allocator); | |
| 977 | 977 | return Dir { |
| 978 | 978 | .allocator = allocator, |
| 979 | 979 | .fd = fd, |
| ... | ... | @@ -994,7 +994,7 @@ pub const Dir = struct { |
| 994 | 994 | start_over: while (true) { |
| 995 | 995 | if (self.index >= self.end_index) { |
| 996 | 996 | if (self.buf.len == 0) { |
| 997 | self.buf = %return self.allocator.alloc(u8, page_size); | |
| 997 | self.buf = try self.allocator.alloc(u8, page_size); | |
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | while (true) { |
| ... | ... | @@ -1004,7 +1004,7 @@ pub const Dir = struct { |
| 1004 | 1004 | switch (err) { |
| 1005 | 1005 | posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable, |
| 1006 | 1006 | posix.EINVAL => { |
| 1007 | self.buf = %return self.allocator.realloc(u8, self.buf, self.buf.len * 2); | |
| 1007 | self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2); | |
| 1008 | 1008 | continue; |
| 1009 | 1009 | }, |
| 1010 | 1010 | else => return unexpectedErrorPosix(err), |
| ... | ... | @@ -1048,7 +1048,7 @@ pub const Dir = struct { |
| 1048 | 1048 | }; |
| 1049 | 1049 | |
| 1050 | 1050 | pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 1051 | const path_buf = %return allocator.alloc(u8, dir_path.len + 1); | |
| 1051 | const path_buf = try allocator.alloc(u8, dir_path.len + 1); | |
| 1052 | 1052 | defer allocator.free(path_buf); |
| 1053 | 1053 | |
| 1054 | 1054 | mem.copy(u8, path_buf, dir_path); |
| ... | ... | @@ -1072,13 +1072,13 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void { |
| 1072 | 1072 | |
| 1073 | 1073 | /// Read value of a symbolic link. |
| 1074 | 1074 | pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1075 | const path_buf = %return allocator.alloc(u8, pathname.len + 1); | |
| 1075 | const path_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1076 | 1076 | defer allocator.free(path_buf); |
| 1077 | 1077 | |
| 1078 | 1078 | mem.copy(u8, path_buf, pathname); |
| 1079 | 1079 | path_buf[pathname.len] = 0; |
| 1080 | 1080 | |
| 1081 | var result_buf = %return allocator.alloc(u8, 1024); | |
| 1081 | var result_buf = try allocator.alloc(u8, 1024); | |
| 1082 | 1082 | %defer allocator.free(result_buf); |
| 1083 | 1083 | while (true) { |
| 1084 | 1084 | const ret_val = posix.readlink(path_buf.ptr, result_buf.ptr, result_buf.len); |
| ... | ... | @@ -1097,7 +1097,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1097 | 1097 | }; |
| 1098 | 1098 | } |
| 1099 | 1099 | if (ret_val == result_buf.len) { |
| 1100 | result_buf = %return allocator.realloc(u8, result_buf, result_buf.len * 2); | |
| 1100 | result_buf = try allocator.realloc(u8, result_buf, result_buf.len * 2); | |
| 1101 | 1101 | continue; |
| 1102 | 1102 | } |
| 1103 | 1103 | return allocator.shrink(u8, result_buf, ret_val); |
| ... | ... | @@ -1320,7 +1320,7 @@ pub const ArgIteratorWindows = struct { |
| 1320 | 1320 | } |
| 1321 | 1321 | |
| 1322 | 1322 | fn internalNext(self: &ArgIteratorWindows, allocator: &Allocator) -> %[]u8 { |
| 1323 | var buf = %return Buffer.initSize(allocator, 0); | |
| 1323 | var buf = try Buffer.initSize(allocator, 0); | |
| 1324 | 1324 | defer buf.deinit(); |
| 1325 | 1325 | |
| 1326 | 1326 | var backslash_count: usize = 0; |
| ... | ... | @@ -1330,34 +1330,34 @@ pub const ArgIteratorWindows = struct { |
| 1330 | 1330 | 0 => return buf.toOwnedSlice(), |
| 1331 | 1331 | '"' => { |
| 1332 | 1332 | const quote_is_real = backslash_count % 2 == 0; |
| 1333 | %return self.emitBackslashes(&buf, backslash_count / 2); | |
| 1333 | try self.emitBackslashes(&buf, backslash_count / 2); | |
| 1334 | 1334 | backslash_count = 0; |
| 1335 | 1335 | |
| 1336 | 1336 | if (quote_is_real) { |
| 1337 | 1337 | self.seen_quote_count += 1; |
| 1338 | 1338 | if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) { |
| 1339 | %return buf.appendByte('"'); | |
| 1339 | try buf.appendByte('"'); | |
| 1340 | 1340 | } |
| 1341 | 1341 | } else { |
| 1342 | %return buf.appendByte('"'); | |
| 1342 | try buf.appendByte('"'); | |
| 1343 | 1343 | } |
| 1344 | 1344 | }, |
| 1345 | 1345 | '\\' => { |
| 1346 | 1346 | backslash_count += 1; |
| 1347 | 1347 | }, |
| 1348 | 1348 | ' ', '\t' => { |
| 1349 | %return self.emitBackslashes(&buf, backslash_count); | |
| 1349 | try self.emitBackslashes(&buf, backslash_count); | |
| 1350 | 1350 | backslash_count = 0; |
| 1351 | 1351 | if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) { |
| 1352 | %return buf.appendByte(byte); | |
| 1352 | try buf.appendByte(byte); | |
| 1353 | 1353 | } else { |
| 1354 | 1354 | return buf.toOwnedSlice(); |
| 1355 | 1355 | } |
| 1356 | 1356 | }, |
| 1357 | 1357 | else => { |
| 1358 | %return self.emitBackslashes(&buf, backslash_count); | |
| 1358 | try self.emitBackslashes(&buf, backslash_count); | |
| 1359 | 1359 | backslash_count = 0; |
| 1360 | %return buf.appendByte(byte); | |
| 1360 | try buf.appendByte(byte); | |
| 1361 | 1361 | }, |
| 1362 | 1362 | } |
| 1363 | 1363 | } |
| ... | ... | @@ -1366,7 +1366,7 @@ pub const ArgIteratorWindows = struct { |
| 1366 | 1366 | fn emitBackslashes(self: &ArgIteratorWindows, buf: &Buffer, emit_count: usize) -> %void { |
| 1367 | 1367 | var i: usize = 0; |
| 1368 | 1368 | while (i < emit_count) : (i += 1) { |
| 1369 | %return buf.appendByte('\\'); | |
| 1369 | try buf.appendByte('\\'); | |
| 1370 | 1370 | } |
| 1371 | 1371 | } |
| 1372 | 1372 | |
| ... | ... | @@ -1430,24 +1430,24 @@ pub fn args() -> ArgIterator { |
| 1430 | 1430 | pub fn argsAlloc(allocator: &mem.Allocator) -> %[]const []u8 { |
| 1431 | 1431 | // TODO refactor to only make 1 allocation. |
| 1432 | 1432 | var it = args(); |
| 1433 | var contents = %return Buffer.initSize(allocator, 0); | |
| 1433 | var contents = try Buffer.initSize(allocator, 0); | |
| 1434 | 1434 | defer contents.deinit(); |
| 1435 | 1435 | |
| 1436 | 1436 | var slice_list = ArrayList(usize).init(allocator); |
| 1437 | 1437 | defer slice_list.deinit(); |
| 1438 | 1438 | |
| 1439 | 1439 | while (it.next(allocator)) |arg_or_err| { |
| 1440 | const arg = %return arg_or_err; | |
| 1440 | const arg = try arg_or_err; | |
| 1441 | 1441 | defer allocator.free(arg); |
| 1442 | %return contents.append(arg); | |
| 1443 | %return slice_list.append(arg.len); | |
| 1442 | try contents.append(arg); | |
| 1443 | try slice_list.append(arg.len); | |
| 1444 | 1444 | } |
| 1445 | 1445 | |
| 1446 | 1446 | const contents_slice = contents.toSliceConst(); |
| 1447 | 1447 | const slice_sizes = slice_list.toSliceConst(); |
| 1448 | const slice_list_bytes = %return math.mul(usize, @sizeOf([]u8), slice_sizes.len); | |
| 1449 | const total_bytes = %return math.add(usize, slice_list_bytes, contents_slice.len); | |
| 1450 | const buf = %return allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); | |
| 1448 | const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len); | |
| 1449 | const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len); | |
| 1450 | const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes); | |
| 1451 | 1451 | %defer allocator.free(buf); |
| 1452 | 1452 | |
| 1453 | 1453 | const result_slice_list = ([][]u8)(buf[0..slice_list_bytes]); |
| ... | ... | @@ -1560,10 +1560,10 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { |
| 1560 | 1560 | return readLink(allocator, "/proc/self/exe"); |
| 1561 | 1561 | }, |
| 1562 | 1562 | Os.windows => { |
| 1563 | var out_path = %return Buffer.initSize(allocator, 0xff); | |
| 1563 | var out_path = try Buffer.initSize(allocator, 0xff); | |
| 1564 | 1564 | %defer out_path.deinit(); |
| 1565 | 1565 | while (true) { |
| 1566 | const dword_len = %return math.cast(windows.DWORD, out_path.len()); | |
| 1566 | const dword_len = try math.cast(windows.DWORD, out_path.len()); | |
| 1567 | 1567 | const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len); |
| 1568 | 1568 | if (copied_amt <= 0) { |
| 1569 | 1569 | const err = windows.GetLastError(); |
| ... | ... | @@ -1576,14 +1576,14 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { |
| 1576 | 1576 | return out_path.toOwnedSlice(); |
| 1577 | 1577 | } |
| 1578 | 1578 | const new_len = (out_path.len() << 1) | 0b1; |
| 1579 | %return out_path.resize(new_len); | |
| 1579 | try out_path.resize(new_len); | |
| 1580 | 1580 | } |
| 1581 | 1581 | }, |
| 1582 | 1582 | Os.macosx, Os.ios => { |
| 1583 | 1583 | var u32_len: u32 = 0; |
| 1584 | 1584 | const ret1 = c._NSGetExecutablePath(undefined, &u32_len); |
| 1585 | 1585 | assert(ret1 != 0); |
| 1586 | const bytes = %return allocator.alloc(u8, u32_len); | |
| 1586 | const bytes = try allocator.alloc(u8, u32_len); | |
| 1587 | 1587 | %defer allocator.free(bytes); |
| 1588 | 1588 | const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len); |
| 1589 | 1589 | assert(ret2 == 0); |
| ... | ... | @@ -1602,13 +1602,13 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 { |
| 1602 | 1602 | // the file path looks something like `/a/b/c/exe (deleted)` |
| 1603 | 1603 | // This path cannot be opened, but it's valid for determining the directory |
| 1604 | 1604 | // the executable was in when it was run. |
| 1605 | const full_exe_path = %return readLink(allocator, "/proc/self/exe"); | |
| 1605 | const full_exe_path = try readLink(allocator, "/proc/self/exe"); | |
| 1606 | 1606 | %defer allocator.free(full_exe_path); |
| 1607 | 1607 | const dir = path.dirname(full_exe_path); |
| 1608 | 1608 | return allocator.shrink(u8, full_exe_path, dir.len); |
| 1609 | 1609 | }, |
| 1610 | 1610 | Os.windows, Os.macosx, Os.ios => { |
| 1611 | const self_exe_path = %return selfExePath(allocator); | |
| 1611 | const self_exe_path = try selfExePath(allocator); | |
| 1612 | 1612 | %defer allocator.free(self_exe_path); |
| 1613 | 1613 | const dirname = os.path.dirname(self_exe_path); |
| 1614 | 1614 | return allocator.shrink(u8, self_exe_path, dirname.len); |
std/os/path.zig+21-21| ... | ... | @@ -412,13 +412,13 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 412 | 412 | if (have_abs_path) { |
| 413 | 413 | switch (have_drive_kind) { |
| 414 | 414 | WindowsPath.Kind.Drive => { |
| 415 | result = %return allocator.alloc(u8, max_size); | |
| 415 | result = try allocator.alloc(u8, max_size); | |
| 416 | 416 | |
| 417 | 417 | mem.copy(u8, result, result_disk_designator); |
| 418 | 418 | result_index += result_disk_designator.len; |
| 419 | 419 | }, |
| 420 | 420 | WindowsPath.Kind.NetworkShare => { |
| 421 | result = %return allocator.alloc(u8, max_size); | |
| 421 | result = try allocator.alloc(u8, max_size); | |
| 422 | 422 | var it = mem.split(paths[first_index], "/\\"); |
| 423 | 423 | const server_name = ??it.next(); |
| 424 | 424 | const other_name = ??it.next(); |
| ... | ... | @@ -438,10 +438,10 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 438 | 438 | }, |
| 439 | 439 | WindowsPath.Kind.None => { |
| 440 | 440 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 441 | const cwd = %return os.getCwd(allocator); | |
| 441 | const cwd = try os.getCwd(allocator); | |
| 442 | 442 | defer allocator.free(cwd); |
| 443 | 443 | const parsed_cwd = windowsParsePath(cwd); |
| 444 | result = %return allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); | |
| 444 | result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); | |
| 445 | 445 | mem.copy(u8, result, parsed_cwd.disk_designator); |
| 446 | 446 | result_index += parsed_cwd.disk_designator.len; |
| 447 | 447 | result_disk_designator = result[0..parsed_cwd.disk_designator.len]; |
| ... | ... | @@ -454,10 +454,10 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) -> %[]u8 |
| 454 | 454 | } else { |
| 455 | 455 | assert(is_windows); // resolveWindows called on non windows can't use getCwd |
| 456 | 456 | // TODO call get cwd for the result_disk_designator instead of the global one |
| 457 | const cwd = %return os.getCwd(allocator); | |
| 457 | const cwd = try os.getCwd(allocator); | |
| 458 | 458 | defer allocator.free(cwd); |
| 459 | 459 | |
| 460 | result = %return allocator.alloc(u8, max_size + cwd.len + 1); | |
| 460 | result = try allocator.alloc(u8, max_size + cwd.len + 1); | |
| 461 | 461 | |
| 462 | 462 | mem.copy(u8, result, cwd); |
| 463 | 463 | result_index += cwd.len; |
| ... | ... | @@ -542,12 +542,12 @@ pub fn resolvePosix(allocator: &Allocator, paths: []const []const u8) -> %[]u8 { |
| 542 | 542 | var result_index: usize = 0; |
| 543 | 543 | |
| 544 | 544 | if (have_abs) { |
| 545 | result = %return allocator.alloc(u8, max_size); | |
| 545 | result = try allocator.alloc(u8, max_size); | |
| 546 | 546 | } else { |
| 547 | 547 | assert(!is_windows); // resolvePosix called on windows can't use getCwd |
| 548 | const cwd = %return os.getCwd(allocator); | |
| 548 | const cwd = try os.getCwd(allocator); | |
| 549 | 549 | defer allocator.free(cwd); |
| 550 | result = %return allocator.alloc(u8, max_size + cwd.len + 1); | |
| 550 | result = try allocator.alloc(u8, max_size + cwd.len + 1); | |
| 551 | 551 | mem.copy(u8, result, cwd); |
| 552 | 552 | result_index += cwd.len; |
| 553 | 553 | } |
| ... | ... | @@ -899,11 +899,11 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u |
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 { |
| 902 | const resolved_from = %return resolveWindows(allocator, [][]const u8{from}); | |
| 902 | const resolved_from = try resolveWindows(allocator, [][]const u8{from}); | |
| 903 | 903 | defer allocator.free(resolved_from); |
| 904 | 904 | |
| 905 | 905 | var clean_up_resolved_to = true; |
| 906 | const resolved_to = %return resolveWindows(allocator, [][]const u8{to}); | |
| 906 | const resolved_to = try resolveWindows(allocator, [][]const u8{to}); | |
| 907 | 907 | defer if (clean_up_resolved_to) allocator.free(resolved_to); |
| 908 | 908 | |
| 909 | 909 | const parsed_from = windowsParsePath(resolved_from); |
| ... | ... | @@ -942,7 +942,7 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) |
| 942 | 942 | up_count += 1; |
| 943 | 943 | } |
| 944 | 944 | const up_index_end = up_count * "..\\".len; |
| 945 | const result = %return allocator.alloc(u8, up_index_end + to_rest.len); | |
| 945 | const result = try allocator.alloc(u8, up_index_end + to_rest.len); | |
| 946 | 946 | %defer allocator.free(result); |
| 947 | 947 | |
| 948 | 948 | var result_index: usize = 0; |
| ... | ... | @@ -972,10 +972,10 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) |
| 972 | 972 | } |
| 973 | 973 | |
| 974 | 974 | pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u8 { |
| 975 | const resolved_from = %return resolvePosix(allocator, [][]const u8{from}); | |
| 975 | const resolved_from = try resolvePosix(allocator, [][]const u8{from}); | |
| 976 | 976 | defer allocator.free(resolved_from); |
| 977 | 977 | |
| 978 | const resolved_to = %return resolvePosix(allocator, [][]const u8{to}); | |
| 978 | const resolved_to = try resolvePosix(allocator, [][]const u8{to}); | |
| 979 | 979 | defer allocator.free(resolved_to); |
| 980 | 980 | |
| 981 | 981 | var from_it = mem.split(resolved_from, "/"); |
| ... | ... | @@ -992,7 +992,7 @@ pub fn relativePosix(allocator: &Allocator, from: []const u8, to: []const u8) -> |
| 992 | 992 | up_count += 1; |
| 993 | 993 | } |
| 994 | 994 | const up_index_end = up_count * "../".len; |
| 995 | const result = %return allocator.alloc(u8, up_index_end + to_rest.len); | |
| 995 | const result = try allocator.alloc(u8, up_index_end + to_rest.len); | |
| 996 | 996 | %defer allocator.free(result); |
| 997 | 997 | |
| 998 | 998 | var result_index: usize = 0; |
| ... | ... | @@ -1080,7 +1080,7 @@ error InputOutput; |
| 1080 | 1080 | pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1081 | 1081 | switch (builtin.os) { |
| 1082 | 1082 | Os.windows => { |
| 1083 | const pathname_buf = %return allocator.alloc(u8, pathname.len + 1); | |
| 1083 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1084 | 1084 | defer allocator.free(pathname_buf); |
| 1085 | 1085 | |
| 1086 | 1086 | mem.copy(u8, pathname_buf, pathname); |
| ... | ... | @@ -1099,7 +1099,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1099 | 1099 | }; |
| 1100 | 1100 | } |
| 1101 | 1101 | defer os.close(h_file); |
| 1102 | var buf = %return allocator.alloc(u8, 256); | |
| 1102 | var buf = try allocator.alloc(u8, 256); | |
| 1103 | 1103 | %defer allocator.free(buf); |
| 1104 | 1104 | while (true) { |
| 1105 | 1105 | const buf_len = math.cast(windows.DWORD, buf.len) %% return error.NameTooLong; |
| ... | ... | @@ -1116,7 +1116,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | if (result > buf.len) { |
| 1119 | buf = %return allocator.realloc(u8, buf, result); | |
| 1119 | buf = try allocator.realloc(u8, buf, result); | |
| 1120 | 1120 | continue; |
| 1121 | 1121 | } |
| 1122 | 1122 | |
| ... | ... | @@ -1140,10 +1140,10 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1140 | 1140 | Os.macosx, Os.ios => { |
| 1141 | 1141 | // TODO instead of calling the libc function here, port the implementation |
| 1142 | 1142 | // to Zig, and then remove the NameTooLong error possibility. |
| 1143 | const pathname_buf = %return allocator.alloc(u8, pathname.len + 1); | |
| 1143 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1144 | 1144 | defer allocator.free(pathname_buf); |
| 1145 | 1145 | |
| 1146 | const result_buf = %return allocator.alloc(u8, posix.PATH_MAX); | |
| 1146 | const result_buf = try allocator.alloc(u8, posix.PATH_MAX); | |
| 1147 | 1147 | %defer allocator.free(result_buf); |
| 1148 | 1148 | |
| 1149 | 1149 | mem.copy(u8, pathname_buf, pathname); |
| ... | ... | @@ -1168,7 +1168,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 1168 | 1168 | return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); |
| 1169 | 1169 | }, |
| 1170 | 1170 | Os.linux => { |
| 1171 | const fd = %return os.posixOpen(pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0, allocator); | |
| 1171 | const fd = try os.posixOpen(pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0, allocator); | |
| 1172 | 1172 | defer os.close(fd); |
| 1173 | 1173 | |
| 1174 | 1174 | var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; |
std/os/windows/util.zig+3-3| ... | ... | @@ -93,7 +93,7 @@ pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_m |
| 93 | 93 | if (file_path.len < stack_buf.len) { |
| 94 | 94 | path0 = stack_buf[0..file_path.len + 1]; |
| 95 | 95 | } else if (allocator) |a| { |
| 96 | path0 = %return a.alloc(u8, file_path.len + 1); | |
| 96 | path0 = try a.alloc(u8, file_path.len + 1); | |
| 97 | 97 | need_free = true; |
| 98 | 98 | } else { |
| 99 | 99 | return error.NameTooLong; |
| ... | ... | @@ -132,7 +132,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) |
| 132 | 132 | } |
| 133 | 133 | break :x bytes_needed; |
| 134 | 134 | }; |
| 135 | const result = %return allocator.alloc(u8, bytes_needed); | |
| 135 | const result = try allocator.alloc(u8, bytes_needed); | |
| 136 | 136 | %defer allocator.free(result); |
| 137 | 137 | |
| 138 | 138 | var it = env_map.iterator(); |
| ... | ... | @@ -153,7 +153,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) |
| 153 | 153 | |
| 154 | 154 | error DllNotFound; |
| 155 | 155 | pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) -> %windows.HMODULE { |
| 156 | const padded_buff = %return cstr.addNullByte(allocator, dll_path); | |
| 156 | const padded_buff = try cstr.addNullByte(allocator, dll_path); | |
| 157 | 157 | defer allocator.free(padded_buff); |
| 158 | 158 | return windows.LoadLibraryA(padded_buff.ptr) ?? error.DllNotFound; |
| 159 | 159 | } |
std/special/build_runner.zig+22-22| ... | ... | @@ -23,15 +23,15 @@ pub fn main() -> %void { |
| 23 | 23 | // skip my own exe name |
| 24 | 24 | _ = arg_it.skip(); |
| 25 | 25 | |
| 26 | const zig_exe = %return unwrapArg(arg_it.next(allocator) ?? { | |
| 26 | const zig_exe = try unwrapArg(arg_it.next(allocator) ?? { | |
| 27 | 27 | warn("Expected first argument to be path to zig compiler\n"); |
| 28 | 28 | return error.InvalidArgs; |
| 29 | 29 | }); |
| 30 | const build_root = %return unwrapArg(arg_it.next(allocator) ?? { | |
| 30 | const build_root = try unwrapArg(arg_it.next(allocator) ?? { | |
| 31 | 31 | warn("Expected second argument to be build root directory path\n"); |
| 32 | 32 | return error.InvalidArgs; |
| 33 | 33 | }); |
| 34 | const cache_root = %return unwrapArg(arg_it.next(allocator) ?? { | |
| 34 | const cache_root = try unwrapArg(arg_it.next(allocator) ?? { | |
| 35 | 35 | warn("Expected third argument to be cache root directory path\n"); |
| 36 | 36 | return error.InvalidArgs; |
| 37 | 37 | }); |
| ... | ... | @@ -58,36 +58,36 @@ pub fn main() -> %void { |
| 58 | 58 | } else |err| err; |
| 59 | 59 | |
| 60 | 60 | while (arg_it.next(allocator)) |err_or_arg| { |
| 61 | const arg = %return unwrapArg(err_or_arg); | |
| 61 | const arg = try unwrapArg(err_or_arg); | |
| 62 | 62 | if (mem.startsWith(u8, arg, "-D")) { |
| 63 | 63 | const option_contents = arg[2..]; |
| 64 | 64 | if (option_contents.len == 0) { |
| 65 | 65 | warn("Expected option name after '-D'\n\n"); |
| 66 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 66 | return usageAndErr(&builder, false, try stderr_stream); | |
| 67 | 67 | } |
| 68 | 68 | if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| { |
| 69 | 69 | const option_name = option_contents[0..name_end]; |
| 70 | 70 | const option_value = option_contents[name_end + 1..]; |
| 71 | 71 | if (builder.addUserInputOption(option_name, option_value)) |
| 72 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 72 | return usageAndErr(&builder, false, try stderr_stream); | |
| 73 | 73 | } else { |
| 74 | 74 | if (builder.addUserInputFlag(option_contents)) |
| 75 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 75 | return usageAndErr(&builder, false, try stderr_stream); | |
| 76 | 76 | } |
| 77 | 77 | } else if (mem.startsWith(u8, arg, "-")) { |
| 78 | 78 | if (mem.eql(u8, arg, "--verbose")) { |
| 79 | 79 | builder.verbose = true; |
| 80 | 80 | } else if (mem.eql(u8, arg, "--help")) { |
| 81 | return usage(&builder, false, %return stdout_stream); | |
| 81 | return usage(&builder, false, try stdout_stream); | |
| 82 | 82 | } else if (mem.eql(u8, arg, "--prefix")) { |
| 83 | prefix = %return unwrapArg(arg_it.next(allocator) ?? { | |
| 83 | prefix = try unwrapArg(arg_it.next(allocator) ?? { | |
| 84 | 84 | warn("Expected argument after --prefix\n\n"); |
| 85 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 85 | return usageAndErr(&builder, false, try stderr_stream); | |
| 86 | 86 | }); |
| 87 | 87 | } else if (mem.eql(u8, arg, "--search-prefix")) { |
| 88 | const search_prefix = %return unwrapArg(arg_it.next(allocator) ?? { | |
| 88 | const search_prefix = try unwrapArg(arg_it.next(allocator) ?? { | |
| 89 | 89 | warn("Expected argument after --search-prefix\n\n"); |
| 90 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 90 | return usageAndErr(&builder, false, try stderr_stream); | |
| 91 | 91 | }); |
| 92 | 92 | builder.addSearchPrefix(search_prefix); |
| 93 | 93 | } else if (mem.eql(u8, arg, "--verbose-tokenize")) { |
| ... | ... | @@ -104,7 +104,7 @@ pub fn main() -> %void { |
| 104 | 104 | builder.verbose_cimport = true; |
| 105 | 105 | } else { |
| 106 | 106 | warn("Unrecognized argument: {}\n\n", arg); |
| 107 | return usageAndErr(&builder, false, %return stderr_stream); | |
| 107 | return usageAndErr(&builder, false, try stderr_stream); | |
| 108 | 108 | } |
| 109 | 109 | } else { |
| 110 | 110 | %%targets.append(arg); |
| ... | ... | @@ -115,11 +115,11 @@ pub fn main() -> %void { |
| 115 | 115 | root.build(&builder); |
| 116 | 116 | |
| 117 | 117 | if (builder.validateUserInputDidItFail()) |
| 118 | return usageAndErr(&builder, true, %return stderr_stream); | |
| 118 | return usageAndErr(&builder, true, try stderr_stream); | |
| 119 | 119 | |
| 120 | 120 | builder.make(targets.toSliceConst()) %% |err| { |
| 121 | 121 | if (err == error.InvalidStepName) { |
| 122 | return usageAndErr(&builder, true, %return stderr_stream); | |
| 122 | return usageAndErr(&builder, true, try stderr_stream); | |
| 123 | 123 | } |
| 124 | 124 | return err; |
| 125 | 125 | }; |
| ... | ... | @@ -133,7 +133,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // This usage text has to be synchronized with src/main.cpp |
| 136 | %return out_stream.print( | |
| 136 | try out_stream.print( | |
| 137 | 137 | \\Usage: {} build [steps] [options] |
| 138 | 138 | \\ |
| 139 | 139 | \\Steps: |
| ... | ... | @@ -142,10 +142,10 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) |
| 142 | 142 | |
| 143 | 143 | const allocator = builder.allocator; |
| 144 | 144 | for (builder.top_level_steps.toSliceConst()) |top_level_step| { |
| 145 | %return out_stream.print(" {s22} {}\n", top_level_step.step.name, top_level_step.description); | |
| 145 | try out_stream.print(" {s22} {}\n", top_level_step.step.name, top_level_step.description); | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | %return out_stream.write( | |
| 148 | try out_stream.write( | |
| 149 | 149 | \\ |
| 150 | 150 | \\General Options: |
| 151 | 151 | \\ --help Print this help and exit |
| ... | ... | @@ -158,17 +158,17 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) |
| 158 | 158 | ); |
| 159 | 159 | |
| 160 | 160 | if (builder.available_options_list.len == 0) { |
| 161 | %return out_stream.print(" (none)\n"); | |
| 161 | try out_stream.print(" (none)\n"); | |
| 162 | 162 | } else { |
| 163 | 163 | for (builder.available_options_list.toSliceConst()) |option| { |
| 164 | const name = %return fmt.allocPrint(allocator, | |
| 164 | const name = try fmt.allocPrint(allocator, | |
| 165 | 165 | " -D{}=[{}]", option.name, Builder.typeIdName(option.type_id)); |
| 166 | 166 | defer allocator.free(name); |
| 167 | %return out_stream.print("{s24} {}\n", name, option.description); | |
| 167 | try out_stream.print("{s24} {}\n", name, option.description); | |
| 168 | 168 | } |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | %return out_stream.write( | |
| 171 | try out_stream.write( | |
| 172 | 172 | \\ |
| 173 | 173 | \\Advanced Options: |
| 174 | 174 | \\ --build-file [file] Override path to build.zig |
std/unicode.zig+1-1| ... | ... | @@ -162,7 +162,7 @@ fn testValid(bytes: []const u8, expected_codepoint: u32) { |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | fn testDecode(bytes: []const u8) -> %u32 { |
| 165 | const length = %return utf8ByteSequenceLength(bytes[0]); | |
| 165 | const length = try utf8ByteSequenceLength(bytes[0]); | |
| 166 | 166 | if (bytes.len < length) return error.UnexpectedEof; |
| 167 | 167 | std.debug.assert(bytes.len == length); |
| 168 | 168 | return utf8Decode(bytes); |
test/cases/error.zig+2-2| ... | ... | @@ -2,7 +2,7 @@ const assert = @import("std").debug.assert; |
| 2 | 2 | const mem = @import("std").mem; |
| 3 | 3 | |
| 4 | 4 | pub fn foo() -> %i32 { |
| 5 | const x = %return bar(); | |
| 5 | const x = try bar(); | |
| 6 | 6 | return x + 1; |
| 7 | 7 | } |
| 8 | 8 | |
| ... | ... | @@ -77,7 +77,7 @@ test "error return in assignment" { |
| 77 | 77 | |
| 78 | 78 | fn doErrReturnInAssignment() -> %void { |
| 79 | 79 | var x : i32 = undefined; |
| 80 | x = %return makeANonErr(); | |
| 80 | x = try makeANonErr(); | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | fn makeANonErr() -> %i32 { |
test/cases/ir_block_deps.zig+2-2| ... | ... | @@ -4,8 +4,8 @@ fn foo(id: u64) -> %i32 { |
| 4 | 4 | return switch (id) { |
| 5 | 5 | 1 => getErrInt(), |
| 6 | 6 | 2 => { |
| 7 | const size = %return getErrInt(); | |
| 8 | return %return getErrInt(); | |
| 7 | const size = try getErrInt(); | |
| 8 | return try getErrInt(); | |
| 9 | 9 | }, |
| 10 | 10 | else => error.ItBroke, |
| 11 | 11 | }; |
test/cases/switch_prong_err_enum.zig+1-1| ... | ... | @@ -16,7 +16,7 @@ const FormValue = union(enum) { |
| 16 | 16 | |
| 17 | 17 | fn doThing(form_id: u64) -> %FormValue { |
| 18 | 18 | return switch (form_id) { |
| 19 | 17 => FormValue { .Address = %return readOnce() }, | |
| 19 | 17 => FormValue { .Address = try readOnce() }, | |
| 20 | 20 | else => error.InvalidDebugInfo, |
| 21 | 21 | }; |
| 22 | 22 | } |
test/compare_output.zig+8-8| ... | ... | @@ -402,7 +402,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 402 | 402 | \\ %%stdout.print("before\n"); |
| 403 | 403 | \\ defer %%stdout.print("defer1\n"); |
| 404 | 404 | \\ %defer %%stdout.print("deferErr\n"); |
| 405 | \\ %return its_gonna_fail(); | |
| 405 | \\ try its_gonna_fail(); | |
| 406 | 406 | \\ defer %%stdout.print("defer3\n"); |
| 407 | 407 | \\ %%stdout.print("after\n"); |
| 408 | 408 | \\} |
| ... | ... | @@ -422,7 +422,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 422 | 422 | \\ %%stdout.print("before\n"); |
| 423 | 423 | \\ defer %%stdout.print("defer1\n"); |
| 424 | 424 | \\ %defer %%stdout.print("deferErr\n"); |
| 425 | \\ %return its_gonna_pass(); | |
| 425 | \\ try its_gonna_pass(); | |
| 426 | 426 | \\ defer %%stdout.print("defer3\n"); |
| 427 | 427 | \\ %%stdout.print("after\n"); |
| 428 | 428 | \\} |
| ... | ... | @@ -454,14 +454,14 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 454 | 454 | \\ |
| 455 | 455 | \\pub fn main() -> %void { |
| 456 | 456 | \\ var args_it = os.args(); |
| 457 | \\ var stdout_file = %return io.getStdOut(); | |
| 457 | \\ var stdout_file = try io.getStdOut(); | |
| 458 | 458 | \\ var stdout_adapter = io.FileOutStream.init(&stdout_file); |
| 459 | 459 | \\ const stdout = &stdout_adapter.stream; |
| 460 | 460 | \\ var index: usize = 0; |
| 461 | 461 | \\ _ = args_it.skip(); |
| 462 | 462 | \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) { |
| 463 | \\ const arg = %return arg_or_err; | |
| 464 | \\ %return stdout.print("{}: {}\n", index, arg); | |
| 463 | \\ const arg = try arg_or_err; | |
| 464 | \\ try stdout.print("{}: {}\n", index, arg); | |
| 465 | 465 | \\ } |
| 466 | 466 | \\} |
| 467 | 467 | , |
| ... | ... | @@ -495,14 +495,14 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 495 | 495 | \\ |
| 496 | 496 | \\pub fn main() -> %void { |
| 497 | 497 | \\ var args_it = os.args(); |
| 498 | \\ var stdout_file = %return io.getStdOut(); | |
| 498 | \\ var stdout_file = try io.getStdOut(); | |
| 499 | 499 | \\ var stdout_adapter = io.FileOutStream.init(&stdout_file); |
| 500 | 500 | \\ const stdout = &stdout_adapter.stream; |
| 501 | 501 | \\ var index: usize = 0; |
| 502 | 502 | \\ _ = args_it.skip(); |
| 503 | 503 | \\ while (args_it.next(allocator)) |arg_or_err| : (index += 1) { |
| 504 | \\ const arg = %return arg_or_err; | |
| 505 | \\ %return stdout.print("{}: {}\n", index, arg); | |
| 504 | \\ const arg = try arg_or_err; | |
| 505 | \\ try stdout.print("{}: {}\n", index, arg); | |
| 506 | 506 | \\ } |
| 507 | 507 | \\} |
| 508 | 508 | , |
test/compile_errors.zig+3-3| ... | ... | @@ -1051,9 +1051,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1051 | 1051 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } |
| 1052 | 1052 | , ".tmp_source.zig:3:26: error: expected signed integer type, found 'u32'"); |
| 1053 | 1053 | |
| 1054 | cases.add("%return in function with non error return type", | |
| 1054 | cases.add("try in function with non error return type", | |
| 1055 | 1055 | \\export fn f() { |
| 1056 | \\ %return something(); | |
| 1056 | \\ try something(); | |
| 1057 | 1057 | \\} |
| 1058 | 1058 | \\fn something() -> %void { } |
| 1059 | 1059 | , |
| ... | ... | @@ -1290,7 +1290,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1290 | 1290 | \\pub fn testTrickyDefer() -> %void { |
| 1291 | 1291 | \\ defer canFail() %% {}; |
| 1292 | 1292 | \\ |
| 1293 | \\ defer %return canFail(); | |
| 1293 | \\ defer try canFail(); | |
| 1294 | 1294 | \\ |
| 1295 | 1295 | \\ const a = maybeInt() ?? return; |
| 1296 | 1296 | \\} |