authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-01 01:19:26-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-01 01:19:26-04:00
log4d13ab07de85e8dd357f7d894f5b2d09e6305115
treefba71795a99873bc598666a08e8413af617dc497
parent2a7c8c5b1076667f5b50748c8153fe64ec5b9f13

std.zig: update to new pointer syntax


4 files changed, 62 insertions(+), 52 deletions(-)

std/zig/ast.zig+4-4
......@@ -1501,23 +1501,23 @@ pub const Node = struct {
15011501 rhs: *Node,
15021502
15031503 pub const Op = union(enum) {
1504 AddrOf: AddrOfInfo,
1504 AddressOf,
15051505 ArrayType: *Node,
15061506 Await,
15071507 BitNot,
15081508 BoolNot,
15091509 Cancel,
1510 PointerType,
15111510 MaybeType,
15121511 Negation,
15131512 NegationWrap,
15141513 Resume,
1515 SliceType: AddrOfInfo,
1514 PtrType: PtrInfo,
1515 SliceType: PtrInfo,
15161516 Try,
15171517 UnwrapMaybe,
15181518 };
15191519
1520 pub const AddrOfInfo = struct {
1520 pub const PtrInfo = struct {
15211521 align_info: ?Align,
15221522 const_token: ?TokenIndex,
15231523 volatile_token: ?TokenIndex,
std/zig/parse.zig+13-13
......@@ -1533,14 +1533,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
15331533 State.SliceOrArrayType => |node| {
15341534 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {
15351535 node.op = ast.Node.PrefixOp.Op{
1536 .SliceType = ast.Node.PrefixOp.AddrOfInfo{
1536 .SliceType = ast.Node.PrefixOp.PtrInfo{
15371537 .align_info = null,
15381538 .const_token = null,
15391539 .volatile_token = null,
15401540 },
15411541 };
15421542 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
1543 try stack.append(State{ .AddrOfModifiers = &node.op.SliceType });
1543 try stack.append(State{ .PtrTypeModifiers = &node.op.SliceType });
15441544 continue;
15451545 }
15461546
......@@ -1551,7 +1551,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
15511551 continue;
15521552 },
15531553
1554 State.AddrOfModifiers => |addr_of_info| {
1554 State.PtrTypeModifiers => |addr_of_info| {
15551555 const token = nextToken(&tok_it, &tree);
15561556 const token_index = token.index;
15571557 const token_ptr = token.ptr;
......@@ -1562,7 +1562,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
15621562 ((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };
15631563 return tree;
15641564 }
1565 addr_of_info.align_info = ast.Node.PrefixOp.AddrOfInfo.Align{
1565 addr_of_info.align_info = ast.Node.PrefixOp.PtrInfo.Align{
15661566 .node = undefined,
15671567 .bit_range = null,
15681568 };
......@@ -1603,7 +1603,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
16031603 const token = nextToken(&tok_it, &tree);
16041604 switch (token.ptr.id) {
16051605 Token.Id.Colon => {
1606 align_info.bit_range = ast.Node.PrefixOp.AddrOfInfo.Align.BitRange(undefined);
1606 align_info.bit_range = ast.Node.PrefixOp.PtrInfo.Align.BitRange(undefined);
16071607 const bit_range = &??align_info.bit_range;
16081608
16091609 try stack.append(State{ .ExpectToken = Token.Id.RParen });
......@@ -2220,7 +2220,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22202220 });
22212221 opt_ctx.store(&node.base);
22222222
2223 // Treat '**' token as two derefs
2223 // Treat '**' token as two pointer types
22242224 if (token_ptr.id == Token.Id.AsteriskAsterisk) {
22252225 const child = try arena.construct(ast.Node.PrefixOp{
22262226 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
......@@ -2233,8 +2233,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22332233 }
22342234
22352235 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
2236 if (node.op == ast.Node.PrefixOp.Op.AddrOf) {
2237 try stack.append(State{ .AddrOfModifiers = &node.op.AddrOf });
2236 if (node.op == ast.Node.PrefixOp.Op.PtrType) {
2237 try stack.append(State{ .PtrTypeModifiers = &node.op.PtrType });
22382238 }
22392239 continue;
22402240 } else {
......@@ -2963,8 +2963,8 @@ const State = union(enum) {
29632963 ExternType: ExternTypeCtx,
29642964 SliceOrArrayAccess: *ast.Node.SuffixOp,
29652965 SliceOrArrayType: *ast.Node.PrefixOp,
2966 AddrOfModifiers: *ast.Node.PrefixOp.AddrOfInfo,
2967 AlignBitRange: *ast.Node.PrefixOp.AddrOfInfo.Align,
2966 PtrTypeModifiers: *ast.Node.PrefixOp.PtrInfo,
2967 AlignBitRange: *ast.Node.PrefixOp.PtrInfo.Align,
29682968
29692969 Payload: OptionalCtx,
29702970 PointerPayload: OptionalCtx,
......@@ -3291,9 +3291,9 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
32913291 Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },
32923292 Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },
32933293 Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },
3294 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{ .PointerType = void{} },
3295 Token.Id.Ampersand => ast.Node.PrefixOp.Op{
3296 .AddrOf = ast.Node.PrefixOp.AddrOfInfo{
3294 Token.Id.Ampersand => ast.Node.PrefixOp.Op{ .AddressOf = void{} },
3295 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{
3296 .PtrType = ast.Node.PrefixOp.PtrInfo{
32973297 .align_info = null,
32983298 .const_token = null,
32993299 .volatile_token = null,
std/zig/parser_test.zig+23-23
......@@ -529,7 +529,7 @@ test "zig fmt: line comment after doc comment" {
529529test "zig fmt: float literal with exponent" {
530530 try testCanonical(
531531 \\test "bit field alignment" {
532 \\ assert(@typeOf(&blah.b) == &align(1:3:6) const u3);
532 \\ assert(@typeOf(&blah.b) == *align(1:3:6) const u3);
533533 \\}
534534 \\
535535 );
......@@ -1040,7 +1040,7 @@ test "zig fmt: alignment" {
10401040
10411041test "zig fmt: C main" {
10421042 try testCanonical(
1043 \\fn main(argc: c_int, argv: &&u8) c_int {
1043 \\fn main(argc: c_int, argv: **u8) c_int {
10441044 \\ const a = b;
10451045 \\}
10461046 \\
......@@ -1049,7 +1049,7 @@ test "zig fmt: C main" {
10491049
10501050test "zig fmt: return" {
10511051 try testCanonical(
1052 \\fn foo(argc: c_int, argv: &&u8) c_int {
1052 \\fn foo(argc: c_int, argv: **u8) c_int {
10531053 \\ return 0;
10541054 \\}
10551055 \\
......@@ -1062,20 +1062,20 @@ test "zig fmt: return" {
10621062
10631063test "zig fmt: pointer attributes" {
10641064 try testCanonical(
1065 \\extern fn f1(s: &align(&u8) u8) c_int;
1066 \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
1067 \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
1068 \\extern fn f4(s: &align(1) const volatile u8) c_int;
1065 \\extern fn f1(s: *align(*u8) u8) c_int;
1066 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
1067 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
1068 \\extern fn f4(s: *align(1) const volatile u8) c_int;
10691069 \\
10701070 );
10711071}
10721072
10731073test "zig fmt: slice attributes" {
10741074 try testCanonical(
1075 \\extern fn f1(s: &align(&u8) u8) c_int;
1076 \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
1077 \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
1078 \\extern fn f4(s: &align(1) const volatile u8) c_int;
1075 \\extern fn f1(s: *align(*u8) u8) c_int;
1076 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
1077 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
1078 \\extern fn f4(s: *align(1) const volatile u8) c_int;
10791079 \\
10801080 );
10811081}
......@@ -1212,18 +1212,18 @@ test "zig fmt: var type" {
12121212
12131213test "zig fmt: functions" {
12141214 try testCanonical(
1215 \\extern fn puts(s: &const u8) c_int;
1216 \\extern "c" fn puts(s: &const u8) c_int;
1217 \\export fn puts(s: &const u8) c_int;
1218 \\inline fn puts(s: &const u8) c_int;
1219 \\pub extern fn puts(s: &const u8) c_int;
1220 \\pub extern "c" fn puts(s: &const u8) c_int;
1221 \\pub export fn puts(s: &const u8) c_int;
1222 \\pub inline fn puts(s: &const u8) c_int;
1223 \\pub extern fn puts(s: &const u8) align(2 + 2) c_int;
1224 \\pub extern "c" fn puts(s: &const u8) align(2 + 2) c_int;
1225 \\pub export fn puts(s: &const u8) align(2 + 2) c_int;
1226 \\pub inline fn puts(s: &const u8) align(2 + 2) c_int;
1215 \\extern fn puts(s: *const u8) c_int;
1216 \\extern "c" fn puts(s: *const u8) c_int;
1217 \\export fn puts(s: *const u8) c_int;
1218 \\inline fn puts(s: *const u8) c_int;
1219 \\pub extern fn puts(s: *const u8) c_int;
1220 \\pub extern "c" fn puts(s: *const u8) c_int;
1221 \\pub export fn puts(s: *const u8) c_int;
1222 \\pub inline fn puts(s: *const u8) c_int;
1223 \\pub extern fn puts(s: *const u8) align(2 + 2) c_int;
1224 \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int;
1225 \\pub export fn puts(s: *const u8) align(2 + 2) c_int;
1226 \\pub inline fn puts(s: *const u8) align(2 + 2) c_int;
12271227 \\
12281228 );
12291229}
std/zig/render.zig+22-12
......@@ -343,9 +343,13 @@ fn renderExpression(
343343 const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);
344344
345345 switch (prefix_op_node.op) {
346 ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| {
347 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // &
348 if (addr_of_info.align_info) |align_info| {
346 ast.Node.PrefixOp.Op.PtrType => |ptr_info| {
347 const star_offset = switch (tree.tokens.at(prefix_op_node.op_token).id) {
348 Token.Id.AsteriskAsterisk => usize(1),
349 else => usize(0),
350 };
351 try renderTokenOffset(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None, star_offset); // *
352 if (ptr_info.align_info) |align_info| {
349353 const lparen_token = tree.prevToken(align_info.node.firstToken());
350354 const align_token = tree.prevToken(lparen_token);
351355
......@@ -370,19 +374,19 @@ fn renderExpression(
370374 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
371375 }
372376 }
373 if (addr_of_info.const_token) |const_token| {
377 if (ptr_info.const_token) |const_token| {
374378 try renderToken(tree, stream, const_token, indent, start_col, Space.Space); // const
375379 }
376 if (addr_of_info.volatile_token) |volatile_token| {
380 if (ptr_info.volatile_token) |volatile_token| {
377381 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space); // volatile
378382 }
379383 },
380384
381 ast.Node.PrefixOp.Op.SliceType => |addr_of_info| {
385 ast.Node.PrefixOp.Op.SliceType => |ptr_info| {
382386 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // [
383387 try renderToken(tree, stream, tree.nextToken(prefix_op_node.op_token), indent, start_col, Space.None); // ]
384388
385 if (addr_of_info.align_info) |align_info| {
389 if (ptr_info.align_info) |align_info| {
386390 const lparen_token = tree.prevToken(align_info.node.firstToken());
387391 const align_token = tree.prevToken(lparen_token);
388392
......@@ -407,10 +411,10 @@ fn renderExpression(
407411 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
408412 }
409413 }
410 if (addr_of_info.const_token) |const_token| {
414 if (ptr_info.const_token) |const_token| {
411415 try renderToken(tree, stream, const_token, indent, start_col, Space.Space);
412416 }
413 if (addr_of_info.volatile_token) |volatile_token| {
417 if (ptr_info.volatile_token) |volatile_token| {
414418 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space);
415419 }
416420 },
......@@ -426,7 +430,7 @@ fn renderExpression(
426430 ast.Node.PrefixOp.Op.NegationWrap,
427431 ast.Node.PrefixOp.Op.UnwrapMaybe,
428432 ast.Node.PrefixOp.Op.MaybeType,
429 ast.Node.PrefixOp.Op.PointerType,
433 ast.Node.PrefixOp.Op.AddressOf,
430434 => {
431435 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None);
432436 },
......@@ -1761,7 +1765,9 @@ const Space = enum {
17611765 BlockStart,
17621766};
17631767
1764fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space) (@typeOf(stream).Child.Error || Error)!void {
1768fn renderTokenOffset(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space,
1769 token_skip_bytes: usize,
1770) (@typeOf(stream).Child.Error || Error)!void {
17651771 if (space == Space.BlockStart) {
17661772 if (start_col.* < indent + indent_delta)
17671773 return renderToken(tree, stream, token_index, indent, start_col, Space.Space);
......@@ -1772,7 +1778,7 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent
17721778 }
17731779
17741780 var token = tree.tokens.at(token_index);
1775 try stream.write(mem.trimRight(u8, tree.tokenSlicePtr(token), " "));
1781 try stream.write(mem.trimRight(u8, tree.tokenSlicePtr(token)[token_skip_bytes..], " "));
17761782
17771783 if (space == Space.NoComment)
17781784 return;
......@@ -1927,6 +1933,10 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent
19271933 }
19281934}
19291935
1936fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space,) (@typeOf(stream).Child.Error || Error)!void {
1937 return renderTokenOffset(tree, stream, token_index, indent, start_col, space, 0);
1938}
1939
19301940fn renderDocComments(
19311941 tree: *ast.Tree,
19321942 stream: var,