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 {...@@ -1501,23 +1501,23 @@ pub const Node = struct {
1501 rhs: *Node,1501 rhs: *Node,
15021502
1503 pub const Op = union(enum) {1503 pub const Op = union(enum) {
1504 AddrOf: AddrOfInfo,1504 AddressOf,
1505 ArrayType: *Node,1505 ArrayType: *Node,
1506 Await,1506 Await,
1507 BitNot,1507 BitNot,
1508 BoolNot,1508 BoolNot,
1509 Cancel,1509 Cancel,
1510 PointerType,
1511 MaybeType,1510 MaybeType,
1512 Negation,1511 Negation,
1513 NegationWrap,1512 NegationWrap,
1514 Resume,1513 Resume,
1515 SliceType: AddrOfInfo,1514 PtrType: PtrInfo,
1515 SliceType: PtrInfo,
1516 Try,1516 Try,
1517 UnwrapMaybe,1517 UnwrapMaybe,
1518 };1518 };
15191519
1520 pub const AddrOfInfo = struct {1520 pub const PtrInfo = struct {
1521 align_info: ?Align,1521 align_info: ?Align,
1522 const_token: ?TokenIndex,1522 const_token: ?TokenIndex,
1523 volatile_token: ?TokenIndex,1523 volatile_token: ?TokenIndex,
std/zig/parse.zig+13-13
...@@ -1533,14 +1533,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -1533,14 +1533,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
1533 State.SliceOrArrayType => |node| {1533 State.SliceOrArrayType => |node| {
1534 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {1534 if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {
1535 node.op = ast.Node.PrefixOp.Op{1535 node.op = ast.Node.PrefixOp.Op{
1536 .SliceType = ast.Node.PrefixOp.AddrOfInfo{1536 .SliceType = ast.Node.PrefixOp.PtrInfo{
1537 .align_info = null,1537 .align_info = null,
1538 .const_token = null,1538 .const_token = null,
1539 .volatile_token = null,1539 .volatile_token = null,
1540 },1540 },
1541 };1541 };
1542 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;1542 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 });
1544 continue;1544 continue;
1545 }1545 }
15461546
...@@ -1551,7 +1551,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -1551,7 +1551,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
1551 continue;1551 continue;
1552 },1552 },
15531553
1554 State.AddrOfModifiers => |addr_of_info| {1554 State.PtrTypeModifiers => |addr_of_info| {
1555 const token = nextToken(&tok_it, &tree);1555 const token = nextToken(&tok_it, &tree);
1556 const token_index = token.index;1556 const token_index = token.index;
1557 const token_ptr = token.ptr;1557 const token_ptr = token.ptr;
...@@ -1562,7 +1562,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -1562,7 +1562,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
1562 ((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };1562 ((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };
1563 return tree;1563 return tree;
1564 }1564 }
1565 addr_of_info.align_info = ast.Node.PrefixOp.AddrOfInfo.Align{1565 addr_of_info.align_info = ast.Node.PrefixOp.PtrInfo.Align{
1566 .node = undefined,1566 .node = undefined,
1567 .bit_range = null,1567 .bit_range = null,
1568 };1568 };
...@@ -1603,7 +1603,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -1603,7 +1603,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
1603 const token = nextToken(&tok_it, &tree);1603 const token = nextToken(&tok_it, &tree);
1604 switch (token.ptr.id) {1604 switch (token.ptr.id) {
1605 Token.Id.Colon => {1605 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);
1607 const bit_range = &??align_info.bit_range;1607 const bit_range = &??align_info.bit_range;
16081608
1609 try stack.append(State{ .ExpectToken = Token.Id.RParen });1609 try stack.append(State{ .ExpectToken = Token.Id.RParen });
...@@ -2220,7 +2220,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -2220,7 +2220,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
2220 });2220 });
2221 opt_ctx.store(&node.base);2221 opt_ctx.store(&node.base);
22222222
2223 // Treat '**' token as two derefs2223 // Treat '**' token as two pointer types
2224 if (token_ptr.id == Token.Id.AsteriskAsterisk) {2224 if (token_ptr.id == Token.Id.AsteriskAsterisk) {
2225 const child = try arena.construct(ast.Node.PrefixOp{2225 const child = try arena.construct(ast.Node.PrefixOp{
2226 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },2226 .base = ast.Node{ .id = ast.Node.Id.PrefixOp },
...@@ -2233,8 +2233,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -2233,8 +2233,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
2233 }2233 }
22342234
2235 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;2235 stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
2236 if (node.op == ast.Node.PrefixOp.Op.AddrOf) {2236 if (node.op == ast.Node.PrefixOp.Op.PtrType) {
2237 try stack.append(State{ .AddrOfModifiers = &node.op.AddrOf });2237 try stack.append(State{ .PtrTypeModifiers = &node.op.PtrType });
2238 }2238 }
2239 continue;2239 continue;
2240 } else {2240 } else {
...@@ -2963,8 +2963,8 @@ const State = union(enum) {...@@ -2963,8 +2963,8 @@ const State = union(enum) {
2963 ExternType: ExternTypeCtx,2963 ExternType: ExternTypeCtx,
2964 SliceOrArrayAccess: *ast.Node.SuffixOp,2964 SliceOrArrayAccess: *ast.Node.SuffixOp,
2965 SliceOrArrayType: *ast.Node.PrefixOp,2965 SliceOrArrayType: *ast.Node.PrefixOp,
2966 AddrOfModifiers: *ast.Node.PrefixOp.AddrOfInfo,2966 PtrTypeModifiers: *ast.Node.PrefixOp.PtrInfo,
2967 AlignBitRange: *ast.Node.PrefixOp.AddrOfInfo.Align,2967 AlignBitRange: *ast.Node.PrefixOp.PtrInfo.Align,
29682968
2969 Payload: OptionalCtx,2969 Payload: OptionalCtx,
2970 PointerPayload: OptionalCtx,2970 PointerPayload: OptionalCtx,
...@@ -3291,9 +3291,9 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {...@@ -3291,9 +3291,9 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
3291 Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },3291 Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },
3292 Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },3292 Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },
3293 Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },3293 Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },
3294 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{ .PointerType = void{} },3294 Token.Id.Ampersand => ast.Node.PrefixOp.Op{ .AddressOf = void{} },
3295 Token.Id.Ampersand => ast.Node.PrefixOp.Op{3295 Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{
3296 .AddrOf = ast.Node.PrefixOp.AddrOfInfo{3296 .PtrType = ast.Node.PrefixOp.PtrInfo{
3297 .align_info = null,3297 .align_info = null,
3298 .const_token = null,3298 .const_token = null,
3299 .volatile_token = null,3299 .volatile_token = null,
std/zig/parser_test.zig+23-23
...@@ -529,7 +529,7 @@ test "zig fmt: line comment after doc comment" {...@@ -529,7 +529,7 @@ test "zig fmt: line comment after doc comment" {
529test "zig fmt: float literal with exponent" {529test "zig fmt: float literal with exponent" {
530 try testCanonical(530 try testCanonical(
531 \\test "bit field alignment" {531 \\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);
533 \\}533 \\}
534 \\534 \\
535 );535 );
...@@ -1040,7 +1040,7 @@ test "zig fmt: alignment" {...@@ -1040,7 +1040,7 @@ test "zig fmt: alignment" {
10401040
1041test "zig fmt: C main" {1041test "zig fmt: C main" {
1042 try testCanonical(1042 try testCanonical(
1043 \\fn main(argc: c_int, argv: &&u8) c_int {1043 \\fn main(argc: c_int, argv: **u8) c_int {
1044 \\ const a = b;1044 \\ const a = b;
1045 \\}1045 \\}
1046 \\1046 \\
...@@ -1049,7 +1049,7 @@ test "zig fmt: C main" {...@@ -1049,7 +1049,7 @@ test "zig fmt: C main" {
10491049
1050test "zig fmt: return" {1050test "zig fmt: return" {
1051 try testCanonical(1051 try testCanonical(
1052 \\fn foo(argc: c_int, argv: &&u8) c_int {1052 \\fn foo(argc: c_int, argv: **u8) c_int {
1053 \\ return 0;1053 \\ return 0;
1054 \\}1054 \\}
1055 \\1055 \\
...@@ -1062,20 +1062,20 @@ test "zig fmt: return" {...@@ -1062,20 +1062,20 @@ test "zig fmt: return" {
10621062
1063test "zig fmt: pointer attributes" {1063test "zig fmt: pointer attributes" {
1064 try testCanonical(1064 try testCanonical(
1065 \\extern fn f1(s: &align(&u8) 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;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;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;1068 \\extern fn f4(s: *align(1) const volatile u8) c_int;
1069 \\1069 \\
1070 );1070 );
1071}1071}
10721072
1073test "zig fmt: slice attributes" {1073test "zig fmt: slice attributes" {
1074 try testCanonical(1074 try testCanonical(
1075 \\extern fn f1(s: &align(&u8) 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;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;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;1078 \\extern fn f4(s: *align(1) const volatile u8) c_int;
1079 \\1079 \\
1080 );1080 );
1081}1081}
...@@ -1212,18 +1212,18 @@ test "zig fmt: var type" {...@@ -1212,18 +1212,18 @@ test "zig fmt: var type" {
12121212
1213test "zig fmt: functions" {1213test "zig fmt: functions" {
1214 try testCanonical(1214 try testCanonical(
1215 \\extern fn puts(s: &const u8) c_int;1215 \\extern fn puts(s: *const u8) c_int;
1216 \\extern "c" 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;1217 \\export fn puts(s: *const u8) c_int;
1218 \\inline 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;1219 \\pub extern fn puts(s: *const u8) c_int;
1220 \\pub extern "c" 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;1221 \\pub export fn puts(s: *const u8) c_int;
1222 \\pub inline 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;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;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;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;1226 \\pub inline fn puts(s: *const u8) align(2 + 2) c_int;
1227 \\1227 \\
1228 );1228 );
1229}1229}
std/zig/render.zig+22-12
...@@ -343,9 +343,13 @@ fn renderExpression(...@@ -343,9 +343,13 @@ fn renderExpression(
343 const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);343 const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);
344344
345 switch (prefix_op_node.op) {345 switch (prefix_op_node.op) {
346 ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| {346 ast.Node.PrefixOp.Op.PtrType => |ptr_info| {
347 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // &347 const star_offset = switch (tree.tokens.at(prefix_op_node.op_token).id) {
348 if (addr_of_info.align_info) |align_info| {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| {
349 const lparen_token = tree.prevToken(align_info.node.firstToken());353 const lparen_token = tree.prevToken(align_info.node.firstToken());
350 const align_token = tree.prevToken(lparen_token);354 const align_token = tree.prevToken(lparen_token);
351355
...@@ -370,19 +374,19 @@ fn renderExpression(...@@ -370,19 +374,19 @@ fn renderExpression(
370 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )374 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
371 }375 }
372 }376 }
373 if (addr_of_info.const_token) |const_token| {377 if (ptr_info.const_token) |const_token| {
374 try renderToken(tree, stream, const_token, indent, start_col, Space.Space); // const378 try renderToken(tree, stream, const_token, indent, start_col, Space.Space); // const
375 }379 }
376 if (addr_of_info.volatile_token) |volatile_token| {380 if (ptr_info.volatile_token) |volatile_token| {
377 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space); // volatile381 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space); // volatile
378 }382 }
379 },383 },
380384
381 ast.Node.PrefixOp.Op.SliceType => |addr_of_info| {385 ast.Node.PrefixOp.Op.SliceType => |ptr_info| {
382 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // [386 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // [
383 try renderToken(tree, stream, tree.nextToken(prefix_op_node.op_token), indent, start_col, Space.None); // ]387 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| {
386 const lparen_token = tree.prevToken(align_info.node.firstToken());390 const lparen_token = tree.prevToken(align_info.node.firstToken());
387 const align_token = tree.prevToken(lparen_token);391 const align_token = tree.prevToken(lparen_token);
388392
...@@ -407,10 +411,10 @@ fn renderExpression(...@@ -407,10 +411,10 @@ fn renderExpression(
407 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )411 try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
408 }412 }
409 }413 }
410 if (addr_of_info.const_token) |const_token| {414 if (ptr_info.const_token) |const_token| {
411 try renderToken(tree, stream, const_token, indent, start_col, Space.Space);415 try renderToken(tree, stream, const_token, indent, start_col, Space.Space);
412 }416 }
413 if (addr_of_info.volatile_token) |volatile_token| {417 if (ptr_info.volatile_token) |volatile_token| {
414 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space);418 try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space);
415 }419 }
416 },420 },
...@@ -426,7 +430,7 @@ fn renderExpression(...@@ -426,7 +430,7 @@ fn renderExpression(
426 ast.Node.PrefixOp.Op.NegationWrap,430 ast.Node.PrefixOp.Op.NegationWrap,
427 ast.Node.PrefixOp.Op.UnwrapMaybe,431 ast.Node.PrefixOp.Op.UnwrapMaybe,
428 ast.Node.PrefixOp.Op.MaybeType,432 ast.Node.PrefixOp.Op.MaybeType,
429 ast.Node.PrefixOp.Op.PointerType,433 ast.Node.PrefixOp.Op.AddressOf,
430 => {434 => {
431 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None);435 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None);
432 },436 },
...@@ -1761,7 +1765,9 @@ const Space = enum {...@@ -1761,7 +1765,9 @@ const Space = enum {
1761 BlockStart,1765 BlockStart,
1762};1766};
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 {
1765 if (space == Space.BlockStart) {1771 if (space == Space.BlockStart) {
1766 if (start_col.* < indent + indent_delta)1772 if (start_col.* < indent + indent_delta)
1767 return renderToken(tree, stream, token_index, indent, start_col, Space.Space);1773 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...@@ -1772,7 +1778,7 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent
1772 }1778 }
17731779
1774 var token = tree.tokens.at(token_index);1780 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
1777 if (space == Space.NoComment)1783 if (space == Space.NoComment)
1778 return;1784 return;
...@@ -1927,6 +1933,10 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent...@@ -1927,6 +1933,10 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent
1927 }1933 }
1928}1934}
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
1930fn renderDocComments(1940fn renderDocComments(
1931 tree: *ast.Tree,1941 tree: *ast.Tree,
1932 stream: var,1942 stream: var,