| author | |
| committer | |
| log | a524e57090f3e3412292dbe6b3e4fe4fb7bad1ea |
| tree | bee166a7f12e3859152cf046ba5d636e01ca1f59 |
| parent | 80b719d967d7241182e237b42ade1cd88494c8e8 |
extern function declarations do not have a body, so allow setting
the rhs for FnDecl to 0 to indicate this is the case.4 files changed, 52 insertions(+), 14 deletions(-)
lib/std/zig/ast.zig+12-4| ... | ... | @@ -521,7 +521,8 @@ pub const Tree = struct { |
| 521 | 521 | .IfSimple, |
| 522 | 522 | .WhileSimple, |
| 523 | 523 | .ForSimple, |
| 524 | .FnDecl, | |
| 524 | .FnProtoSimple, | |
| 525 | .FnProtoMulti, | |
| 525 | 526 | .PtrTypeAligned, |
| 526 | 527 | .PtrTypeSentinel, |
| 527 | 528 | .PtrType, |
| ... | ... | @@ -538,8 +539,6 @@ pub const Tree = struct { |
| 538 | 539 | .AsmSimple, |
| 539 | 540 | .AsmOutput, |
| 540 | 541 | .AsmInput, |
| 541 | .FnProtoSimple, | |
| 542 | .FnProtoMulti, | |
| 543 | 542 | .ErrorValue, |
| 544 | 543 | => return datas[n].rhs + end_offset, |
| 545 | 544 | |
| ... | ... | @@ -804,6 +803,13 @@ pub const Tree = struct { |
| 804 | 803 | return main_tokens[n] + end_offset; |
| 805 | 804 | } |
| 806 | 805 | }, |
| 806 | .FnDecl => { | |
| 807 | if (datas[n].rhs != 0) { | |
| 808 | n = datas[n].rhs; | |
| 809 | } else { | |
| 810 | n = datas[n].lhs; | |
| 811 | } | |
| 812 | }, | |
| 807 | 813 | .FnProtoOne => { |
| 808 | 814 | const extra = tree.extraData(datas[n].lhs, Node.FnProtoOne); |
| 809 | 815 | // linksection, callconv, align can appear in any order, so we |
| ... | ... | @@ -2520,7 +2526,9 @@ pub const Node = struct { |
| 2520 | 2526 | /// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `FnProto[lhs]`. |
| 2521 | 2527 | /// anytype and ... parameters are omitted from the AST tree. |
| 2522 | 2528 | FnProto, |
| 2523 | /// lhs is the FnProto, rhs is the function body block. | |
| 2529 | /// lhs is the FnProto. | |
| 2530 | /// rhs is the function body block if non-zero. | |
| 2531 | /// if rhs is zero, the funtion decl has no body (e.g. an extern function) | |
| 2524 | 2532 | FnDecl, |
| 2525 | 2533 | /// `anyframe->rhs`. main_token is `anyframe`. `lhs` is arrow token index. |
| 2526 | 2534 | AnyFrameType, |
lib/std/zig/parse.zig+8-1| ... | ... | @@ -516,7 +516,14 @@ const Parser = struct { |
| 516 | 516 | .Semicolon => { |
| 517 | 517 | const semicolon_token = p.nextToken(); |
| 518 | 518 | try p.parseAppendedDocComment(semicolon_token); |
| 519 | return fn_proto; | |
| 519 | return p.addNode(.{ | |
| 520 | .tag = .FnDecl, | |
| 521 | .main_token = p.nodes.items(.main_token)[fn_proto], | |
| 522 | .data = .{ | |
| 523 | .lhs = fn_proto, | |
| 524 | .rhs = 0, | |
| 525 | }, | |
| 526 | }); | |
| 520 | 527 | }, |
| 521 | 528 | .LBrace => { |
| 522 | 529 | const body_block = try p.parseBlock(); |
lib/std/zig/parser_test.zig+23-7| ... | ... | @@ -298,12 +298,12 @@ test "zig fmt: grouped expressions (parentheses)" { |
| 298 | 298 | ); |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | //test "zig fmt: c pointer type" { | |
| 302 | // try testCanonical( | |
| 303 | // \\pub extern fn repro() [*c]const u8; | |
| 304 | // \\ | |
| 305 | // ); | |
| 306 | //} | |
| 301 | test "zig fmt: c pointer type" { | |
| 302 | try testCanonical( | |
| 303 | \\pub extern fn repro() [*c]const u8; | |
| 304 | \\ | |
| 305 | ); | |
| 306 | } | |
| 307 | 307 | |
| 308 | 308 | test "zig fmt: builtin call with trailing comma" { |
| 309 | 309 | try testCanonical( |
| ... | ... | @@ -2339,7 +2339,23 @@ test "zig fmt: alignment" { |
| 2339 | 2339 | // \\ |
| 2340 | 2340 | // ); |
| 2341 | 2341 | //} |
| 2342 | // | |
| 2342 | ||
| 2343 | test "zig fmt: function attributes" { | |
| 2344 | try testCanonical( | |
| 2345 | \\export fn foo() void {} | |
| 2346 | \\pub export fn foo() void {} | |
| 2347 | \\extern fn foo() void; | |
| 2348 | \\pub extern fn foo() void; | |
| 2349 | \\extern "c" fn foo() void; | |
| 2350 | \\pub extern "c" fn foo() void; | |
| 2351 | \\inline fn foo() void {} | |
| 2352 | \\pub inline fn foo() void {} | |
| 2353 | \\noinline fn foo() void {} | |
| 2354 | \\pub noinline fn foo() void {} | |
| 2355 | \\ | |
| 2356 | ); | |
| 2357 | } | |
| 2358 | ||
| 2343 | 2359 | //test "zig fmt: pointer attributes" { |
| 2344 | 2360 | // try testCanonical( |
| 2345 | 2361 | // \\extern fn f1(s: *align(*u8) u8) c_int; |
lib/std/zig/render.zig+9-2| ... | ... | @@ -121,6 +121,8 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E |
| 121 | 121 | .Keyword_export, |
| 122 | 122 | .Keyword_pub, |
| 123 | 123 | .StringLiteral, |
| 124 | .Keyword_inline, | |
| 125 | .Keyword_noinline, | |
| 124 | 126 | => continue, |
| 125 | 127 | |
| 126 | 128 | else => { |
| ... | ... | @@ -132,8 +134,13 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E |
| 132 | 134 | while (i < fn_token) : (i += 1) { |
| 133 | 135 | try renderToken(ais, tree, i, .Space); |
| 134 | 136 | } |
| 135 | try renderExpression(ais, tree, fn_proto, .Space); | |
| 136 | return renderExpression(ais, tree, datas[decl].rhs, space); | |
| 137 | if (datas[decl].rhs != 0) { | |
| 138 | try renderExpression(ais, tree, fn_proto, .Space); | |
| 139 | return renderExpression(ais, tree, datas[decl].rhs, space); | |
| 140 | } else { | |
| 141 | try renderExpression(ais, tree, fn_proto, .None); | |
| 142 | return renderToken(ais, tree, tree.lastToken(fn_proto) + 1, space); // semicolon | |
| 143 | } | |
| 137 | 144 | }, |
| 138 | 145 | .FnProtoSimple, |
| 139 | 146 | .FnProtoMulti, |