std.zig: update to new pointer syntax

master
Andrew Kelley 2018-06-01 01:19:26 -04:00
parent 2a7c8c5b10
commit 4d13ab07de
4 changed files with 62 additions and 52 deletions

View File

@ -1501,23 +1501,23 @@ pub const Node = struct {
rhs: *Node,
pub const Op = union(enum) {
AddrOf: AddrOfInfo,
AddressOf,
ArrayType: *Node,
Await,
BitNot,
BoolNot,
Cancel,
PointerType,
MaybeType,
Negation,
NegationWrap,
Resume,
SliceType: AddrOfInfo,
PtrType: PtrInfo,
SliceType: PtrInfo,
Try,
UnwrapMaybe,
};
pub const AddrOfInfo = struct {
pub const PtrInfo = struct {
align_info: ?Align,
const_token: ?TokenIndex,
volatile_token: ?TokenIndex,

View File

@ -1533,14 +1533,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
State.SliceOrArrayType => |node| {
if (eatToken(&tok_it, &tree, Token.Id.RBracket)) |_| {
node.op = ast.Node.PrefixOp.Op{
.SliceType = ast.Node.PrefixOp.AddrOfInfo{
.SliceType = ast.Node.PrefixOp.PtrInfo{
.align_info = null,
.const_token = null,
.volatile_token = null,
},
};
stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
try stack.append(State{ .AddrOfModifiers = &node.op.SliceType });
try stack.append(State{ .PtrTypeModifiers = &node.op.SliceType });
continue;
}
@ -1551,7 +1551,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
State.AddrOfModifiers => |addr_of_info| {
State.PtrTypeModifiers => |addr_of_info| {
const token = nextToken(&tok_it, &tree);
const token_index = token.index;
const token_ptr = token.ptr;
@ -1562,7 +1562,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
((try tree.errors.addOne())).* = Error{ .ExtraAlignQualifier = Error.ExtraAlignQualifier{ .token = token_index } };
return tree;
}
addr_of_info.align_info = ast.Node.PrefixOp.AddrOfInfo.Align{
addr_of_info.align_info = ast.Node.PrefixOp.PtrInfo.Align{
.node = undefined,
.bit_range = null,
};
@ -1603,7 +1603,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
const token = nextToken(&tok_it, &tree);
switch (token.ptr.id) {
Token.Id.Colon => {
align_info.bit_range = ast.Node.PrefixOp.AddrOfInfo.Align.BitRange(undefined);
align_info.bit_range = ast.Node.PrefixOp.PtrInfo.Align.BitRange(undefined);
const bit_range = &??align_info.bit_range;
try stack.append(State{ .ExpectToken = Token.Id.RParen });
@ -2220,7 +2220,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
});
opt_ctx.store(&node.base);
// Treat '**' token as two derefs
// Treat '**' token as two pointer types
if (token_ptr.id == Token.Id.AsteriskAsterisk) {
const child = try arena.construct(ast.Node.PrefixOp{
.base = ast.Node{ .id = ast.Node.Id.PrefixOp },
@ -2233,8 +2233,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
}
stack.append(State{ .TypeExprBegin = OptionalCtx{ .Required = &node.rhs } }) catch unreachable;
if (node.op == ast.Node.PrefixOp.Op.AddrOf) {
try stack.append(State{ .AddrOfModifiers = &node.op.AddrOf });
if (node.op == ast.Node.PrefixOp.Op.PtrType) {
try stack.append(State{ .PtrTypeModifiers = &node.op.PtrType });
}
continue;
} else {
@ -2963,8 +2963,8 @@ const State = union(enum) {
ExternType: ExternTypeCtx,
SliceOrArrayAccess: *ast.Node.SuffixOp,
SliceOrArrayType: *ast.Node.PrefixOp,
AddrOfModifiers: *ast.Node.PrefixOp.AddrOfInfo,
AlignBitRange: *ast.Node.PrefixOp.AddrOfInfo.Align,
PtrTypeModifiers: *ast.Node.PrefixOp.PtrInfo,
AlignBitRange: *ast.Node.PrefixOp.PtrInfo.Align,
Payload: OptionalCtx,
PointerPayload: OptionalCtx,
@ -3291,9 +3291,9 @@ fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op {
Token.Id.Tilde => ast.Node.PrefixOp.Op{ .BitNot = void{} },
Token.Id.Minus => ast.Node.PrefixOp.Op{ .Negation = void{} },
Token.Id.MinusPercent => ast.Node.PrefixOp.Op{ .NegationWrap = void{} },
Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{ .PointerType = void{} },
Token.Id.Ampersand => ast.Node.PrefixOp.Op{
.AddrOf = ast.Node.PrefixOp.AddrOfInfo{
Token.Id.Ampersand => ast.Node.PrefixOp.Op{ .AddressOf = void{} },
Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op{
.PtrType = ast.Node.PrefixOp.PtrInfo{
.align_info = null,
.const_token = null,
.volatile_token = null,

View File

@ -529,7 +529,7 @@ test "zig fmt: line comment after doc comment" {
test "zig fmt: float literal with exponent" {
try testCanonical(
\\test "bit field alignment" {
\\ assert(@typeOf(&blah.b) == &align(1:3:6) const u3);
\\ assert(@typeOf(&blah.b) == *align(1:3:6) const u3);
\\}
\\
);
@ -1040,7 +1040,7 @@ test "zig fmt: alignment" {
test "zig fmt: C main" {
try testCanonical(
\\fn main(argc: c_int, argv: &&u8) c_int {
\\fn main(argc: c_int, argv: **u8) c_int {
\\ const a = b;
\\}
\\
@ -1049,7 +1049,7 @@ test "zig fmt: C main" {
test "zig fmt: return" {
try testCanonical(
\\fn foo(argc: c_int, argv: &&u8) c_int {
\\fn foo(argc: c_int, argv: **u8) c_int {
\\ return 0;
\\}
\\
@ -1062,20 +1062,20 @@ test "zig fmt: return" {
test "zig fmt: pointer attributes" {
try testCanonical(
\\extern fn f1(s: &align(&u8) u8) c_int;
\\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
\\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
\\extern fn f4(s: &align(1) const volatile u8) c_int;
\\extern fn f1(s: *align(*u8) u8) c_int;
\\extern fn f2(s: **align(1) *const *volatile u8) c_int;
\\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
\\extern fn f4(s: *align(1) const volatile u8) c_int;
\\
);
}
test "zig fmt: slice attributes" {
try testCanonical(
\\extern fn f1(s: &align(&u8) u8) c_int;
\\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
\\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
\\extern fn f4(s: &align(1) const volatile u8) c_int;
\\extern fn f1(s: *align(*u8) u8) c_int;
\\extern fn f2(s: **align(1) *const *volatile u8) c_int;
\\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
\\extern fn f4(s: *align(1) const volatile u8) c_int;
\\
);
}
@ -1212,18 +1212,18 @@ test "zig fmt: var type" {
test "zig fmt: functions" {
try testCanonical(
\\extern fn puts(s: &const u8) c_int;
\\extern "c" fn puts(s: &const u8) c_int;
\\export fn puts(s: &const u8) c_int;
\\inline fn puts(s: &const u8) c_int;
\\pub extern fn puts(s: &const u8) c_int;
\\pub extern "c" fn puts(s: &const u8) c_int;
\\pub export fn puts(s: &const u8) c_int;
\\pub inline fn puts(s: &const u8) c_int;
\\pub extern fn puts(s: &const u8) align(2 + 2) c_int;
\\pub extern "c" fn puts(s: &const u8) align(2 + 2) c_int;
\\pub export fn puts(s: &const u8) align(2 + 2) c_int;
\\pub inline fn puts(s: &const u8) align(2 + 2) c_int;
\\extern fn puts(s: *const u8) c_int;
\\extern "c" fn puts(s: *const u8) c_int;
\\export fn puts(s: *const u8) c_int;
\\inline fn puts(s: *const u8) c_int;
\\pub extern fn puts(s: *const u8) c_int;
\\pub extern "c" fn puts(s: *const u8) c_int;
\\pub export fn puts(s: *const u8) c_int;
\\pub inline fn puts(s: *const u8) c_int;
\\pub extern fn puts(s: *const u8) align(2 + 2) c_int;
\\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int;
\\pub export fn puts(s: *const u8) align(2 + 2) c_int;
\\pub inline fn puts(s: *const u8) align(2 + 2) c_int;
\\
);
}

View File

@ -343,9 +343,13 @@ fn renderExpression(
const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);
switch (prefix_op_node.op) {
ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| {
try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // &
if (addr_of_info.align_info) |align_info| {
ast.Node.PrefixOp.Op.PtrType => |ptr_info| {
const star_offset = switch (tree.tokens.at(prefix_op_node.op_token).id) {
Token.Id.AsteriskAsterisk => usize(1),
else => usize(0),
};
try renderTokenOffset(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None, star_offset); // *
if (ptr_info.align_info) |align_info| {
const lparen_token = tree.prevToken(align_info.node.firstToken());
const align_token = tree.prevToken(lparen_token);
@ -370,19 +374,19 @@ fn renderExpression(
try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
}
}
if (addr_of_info.const_token) |const_token| {
if (ptr_info.const_token) |const_token| {
try renderToken(tree, stream, const_token, indent, start_col, Space.Space); // const
}
if (addr_of_info.volatile_token) |volatile_token| {
if (ptr_info.volatile_token) |volatile_token| {
try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space); // volatile
}
},
ast.Node.PrefixOp.Op.SliceType => |addr_of_info| {
ast.Node.PrefixOp.Op.SliceType => |ptr_info| {
try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None); // [
try renderToken(tree, stream, tree.nextToken(prefix_op_node.op_token), indent, start_col, Space.None); // ]
if (addr_of_info.align_info) |align_info| {
if (ptr_info.align_info) |align_info| {
const lparen_token = tree.prevToken(align_info.node.firstToken());
const align_token = tree.prevToken(lparen_token);
@ -407,10 +411,10 @@ fn renderExpression(
try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // )
}
}
if (addr_of_info.const_token) |const_token| {
if (ptr_info.const_token) |const_token| {
try renderToken(tree, stream, const_token, indent, start_col, Space.Space);
}
if (addr_of_info.volatile_token) |volatile_token| {
if (ptr_info.volatile_token) |volatile_token| {
try renderToken(tree, stream, volatile_token, indent, start_col, Space.Space);
}
},
@ -426,7 +430,7 @@ fn renderExpression(
ast.Node.PrefixOp.Op.NegationWrap,
ast.Node.PrefixOp.Op.UnwrapMaybe,
ast.Node.PrefixOp.Op.MaybeType,
ast.Node.PrefixOp.Op.PointerType,
ast.Node.PrefixOp.Op.AddressOf,
=> {
try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None);
},
@ -1761,7 +1765,9 @@ const Space = enum {
BlockStart,
};
fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space) (@typeOf(stream).Child.Error || Error)!void {
fn renderTokenOffset(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space,
token_skip_bytes: usize,
) (@typeOf(stream).Child.Error || Error)!void {
if (space == Space.BlockStart) {
if (start_col.* < indent + indent_delta)
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
}
var token = tree.tokens.at(token_index);
try stream.write(mem.trimRight(u8, tree.tokenSlicePtr(token), " "));
try stream.write(mem.trimRight(u8, tree.tokenSlicePtr(token)[token_skip_bytes..], " "));
if (space == Space.NoComment)
return;
@ -1927,6 +1933,10 @@ fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent
}
}
fn renderToken(tree: *ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, start_col: *usize, space: Space,) (@typeOf(stream).Child.Error || Error)!void {
return renderTokenOffset(tree, stream, token_index, indent, start_col, space, 0);
}
fn renderDocComments(
tree: *ast.Tree,
stream: var,