authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-05-04 09:06:20-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-05-05 09:37:28-06:00
log7ada59f873738931b4d162372dff0a33442112b6
treed49d68fb89c784186c069584b91fb6e2a841dba2
parente6955688ac2255d3813e8d2994b6661bc651369b
signature Commit is signed but in an unrecognized format.

remove nakedcc/stdcallcc/async fn/extern fn fnproto


11 files changed, 9 insertions(+), 165 deletions(-)

doc/docgen.zig-2
......@@ -800,7 +800,6 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok
800800 .Keyword_for,
801801 .Keyword_if,
802802 .Keyword_inline,
803 .Keyword_nakedcc,
804803 .Keyword_noalias,
805804 .Keyword_noinline,
806805 .Keyword_nosuspend,
......@@ -813,7 +812,6 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok
813812 .Keyword_return,
814813 .Keyword_linksection,
815814 .Keyword_callconv,
816 .Keyword_stdcallcc,
817815 .Keyword_struct,
818816 .Keyword_suspend,
819817 .Keyword_switch,
doc/langref.html.in+1-6
......@@ -10088,7 +10088,7 @@ TopLevelDecl
1008810088 / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl
1008910089 / KEYWORD_usingnamespace Expr SEMICOLON
1009010090
10091FnProto &lt;- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
10091FnProto &lt;- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
1009210092
1009310093VarDecl &lt;- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLON
1009410094
......@@ -10255,11 +10255,6 @@ WhileContinueExpr &lt;- COLON LPAREN AssignExpr RPAREN
1025510255
1025610256LinkSection &lt;- KEYWORD_linksection LPAREN Expr RPAREN
1025710257
10258# Fn specific
10259FnCC
10260 &lt;- KEYWORD_extern
10261 / KEYWORD_async
10262
1026310258ParamDecl &lt;- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType
1026410259
1026510260ParamType
lib/std/zig/ast.zig-2
......@@ -875,7 +875,6 @@ pub const Node = struct {
875875 return_type: ReturnType,
876876 var_args_token: ?TokenIndex,
877877 extern_export_inline_token: ?TokenIndex,
878 cc_token: ?TokenIndex,
879878 body_node: ?*Node,
880879 lib_name: ?*Node, // populated if this is an extern declaration
881880 align_expr: ?*Node, // populated if align(A) is present
......@@ -929,7 +928,6 @@ pub const Node = struct {
929928 if (self.visib_token) |visib_token| return visib_token;
930929 if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token;
931930 assert(self.lib_name == null);
932 if (self.cc_token) |cc_token| return cc_token;
933931 return self.fn_token;
934932 }
935933
lib/std/zig/parse.zig+2-51
......@@ -335,22 +335,9 @@ fn parseTopLevelDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
335335 return use_node;
336336}
337337
338/// FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
338/// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
339339fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
340 const cc = parseFnCC(arena, it, tree);
341 const fn_token = eatToken(it, .Keyword_fn) orelse {
342 if (cc) |fnCC| {
343 if (fnCC == .Extern) {
344 putBackToken(it, fnCC.Extern); // 'extern' is also used in ContainerDecl
345 } else {
346 try tree.errors.push(.{
347 .ExpectedToken = .{ .token = it.index, .expected_id = .Keyword_fn },
348 });
349 return error.ParseError;
350 }
351 }
352 return null;
353 };
340 const fn_token = eatToken(it, .Keyword_fn) orelse return null;
354341 const name_token = eatToken(it, .Identifier);
355342 const lparen = try expectToken(it, tree, .LParen);
356343 const params = try parseParamDeclList(arena, it, tree);
......@@ -389,7 +376,6 @@ fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
389376 .return_type = return_type,
390377 .var_args_token = var_args_token,
391378 .extern_export_inline_token = null,
392 .cc_token = null,
393379 .body_node = null,
394380 .lib_name = null,
395381 .align_expr = align_expr,
......@@ -397,13 +383,6 @@ fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
397383 .callconv_expr = callconv_expr,
398384 };
399385
400 if (cc) |kind| {
401 switch (kind) {
402 .CC => |token| fn_proto_node.cc_token = token,
403 .Extern => |token| fn_proto_node.extern_export_inline_token = token,
404 }
405 }
406
407386 return &fn_proto_node.base;
408387}
409388
......@@ -1196,16 +1175,6 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11961175 const maybe_async = eatToken(it, .Keyword_async);
11971176 if (maybe_async) |async_token| {
11981177 const token_fn = eatToken(it, .Keyword_fn);
1199 if (token_fn != null) {
1200 // HACK: If we see the keyword `fn`, then we assume that
1201 // we are parsing an async fn proto, and not a call.
1202 // We therefore put back all tokens consumed by the async
1203 // prefix...
1204 putBackToken(it, token_fn.?);
1205 putBackToken(it, async_token);
1206 return parsePrimaryTypeExpr(arena, it, tree);
1207 }
1208 // TODO: Implement hack for parsing `async fn ...` in ast_parse_suffix_expr
12091178 var res = try expectNode(arena, it, tree, parsePrimaryTypeExpr, .{
12101179 .ExpectedPrimaryTypeExpr = .{ .token = it.index },
12111180 });
......@@ -1778,24 +1747,6 @@ fn parseCallconv(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
17781747 return expr_node;
17791748}
17801749
1781/// FnCC
1782/// <- KEYWORD_nakedcc
1783/// / KEYWORD_stdcallcc
1784/// / KEYWORD_extern
1785/// / KEYWORD_async
1786fn parseFnCC(arena: *Allocator, it: *TokenIterator, tree: *Tree) ?FnCC {
1787 if (eatToken(it, .Keyword_nakedcc)) |token| return FnCC{ .CC = token };
1788 if (eatToken(it, .Keyword_stdcallcc)) |token| return FnCC{ .CC = token };
1789 if (eatToken(it, .Keyword_extern)) |token| return FnCC{ .Extern = token };
1790 if (eatToken(it, .Keyword_async)) |token| return FnCC{ .CC = token };
1791 return null;
1792}
1793
1794const FnCC = union(enum) {
1795 CC: TokenIndex,
1796 Extern: TokenIndex,
1797};
1798
17991750/// ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType
18001751fn parseParamDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
18011752 const doc_comments = try parseDocComment(arena, it, tree);
lib/std/zig/parser_test.zig-16
......@@ -123,22 +123,6 @@ test "zig fmt: trailing comma in fn parameter list" {
123123 );
124124}
125125
126// TODO: Remove nakedcc/stdcallcc once zig 0.6.0 is released. See https://github.com/ziglang/zig/pull/3977
127test "zig fmt: convert extern/nakedcc/stdcallcc into callconv(...)" {
128 try testTransform(
129 \\nakedcc fn foo1() void {}
130 \\stdcallcc fn foo2() void {}
131 \\extern fn foo3() void {}
132 \\extern "mylib" fn foo4() void {}
133 ,
134 \\fn foo1() callconv(.Naked) void {}
135 \\fn foo2() callconv(.Stdcall) void {}
136 \\fn foo3() callconv(.C) void {}
137 \\fn foo4() callconv(.C) void {}
138 \\
139 );
140}
141
142126test "zig fmt: comptime struct field" {
143127 try testCanonical(
144128 \\const Foo = struct {
lib/std/zig/render.zig+1-23
......@@ -1413,32 +1413,14 @@ fn renderExpression(
14131413 try renderToken(tree, stream, visib_token_index, indent, start_col, Space.Space); // pub
14141414 }
14151415
1416 // Some extra machinery is needed to rewrite the old-style cc
1417 // notation to the new callconv one
1418 var cc_rewrite_str: ?[*:0]const u8 = null;
14191416 if (fn_proto.extern_export_inline_token) |extern_export_inline_token| {
1420 const tok = tree.tokens.at(extern_export_inline_token);
1421 if (tok.id != .Keyword_extern or fn_proto.body_node == null) {
1422 try renderToken(tree, stream, extern_export_inline_token, indent, start_col, Space.Space); // extern/export
1423 } else {
1424 cc_rewrite_str = ".C";
1425 fn_proto.lib_name = null;
1426 }
1417 try renderToken(tree, stream, extern_export_inline_token, indent, start_col, Space.Space); // extern/export
14271418 }
14281419
14291420 if (fn_proto.lib_name) |lib_name| {
14301421 try renderExpression(allocator, stream, tree, indent, start_col, lib_name, Space.Space);
14311422 }
14321423
1433 if (fn_proto.cc_token) |cc_token| {
1434 var str = tree.tokenSlicePtr(tree.tokens.at(cc_token));
1435 if (mem.eql(u8, str, "stdcallcc")) {
1436 cc_rewrite_str = ".Stdcall";
1437 } else if (mem.eql(u8, str, "nakedcc")) {
1438 cc_rewrite_str = ".Naked";
1439 } else try renderToken(tree, stream, cc_token, indent, start_col, Space.Space); // stdcallcc
1440 }
1441
14421424 const lparen = if (fn_proto.name_token) |name_token| blk: {
14431425 try renderToken(tree, stream, fn_proto.fn_token, indent, start_col, Space.Space); // fn
14441426 try renderToken(tree, stream, name_token, indent, start_col, Space.None); // name
......@@ -1528,10 +1510,6 @@ fn renderExpression(
15281510 try renderToken(tree, stream, callconv_lparen, indent, start_col, Space.None); // (
15291511 try renderExpression(allocator, stream, tree, indent, start_col, callconv_expr, Space.None);
15301512 try renderToken(tree, stream, callconv_rparen, indent, start_col, Space.Space); // )
1531 } else if (cc_rewrite_str) |str| {
1532 try stream.writeAll("callconv(");
1533 try stream.writeAll(mem.spanZ(str));
1534 try stream.writeAll(") ");
15351513 }
15361514
15371515 switch (fn_proto.return_type) {
lib/std/zig/tokenizer.zig-6
......@@ -47,7 +47,6 @@ pub const Token = struct {
4747 Keyword.init("for", .Keyword_for),
4848 Keyword.init("if", .Keyword_if),
4949 Keyword.init("inline", .Keyword_inline),
50 Keyword.init("nakedcc", .Keyword_nakedcc),
5150 Keyword.init("noalias", .Keyword_noalias),
5251 Keyword.init("noasync", .Keyword_nosuspend), // TODO: remove this
5352 Keyword.init("noinline", .Keyword_noinline),
......@@ -60,7 +59,6 @@ pub const Token = struct {
6059 Keyword.init("resume", .Keyword_resume),
6160 Keyword.init("return", .Keyword_return),
6261 Keyword.init("linksection", .Keyword_linksection),
63 Keyword.init("stdcallcc", .Keyword_stdcallcc),
6462 Keyword.init("struct", .Keyword_struct),
6563 Keyword.init("suspend", .Keyword_suspend),
6664 Keyword.init("switch", .Keyword_switch),
......@@ -181,7 +179,6 @@ pub const Token = struct {
181179 Keyword_for,
182180 Keyword_if,
183181 Keyword_inline,
184 Keyword_nakedcc,
185182 Keyword_noalias,
186183 Keyword_noinline,
187184 Keyword_nosuspend,
......@@ -194,7 +191,6 @@ pub const Token = struct {
194191 Keyword_resume,
195192 Keyword_return,
196193 Keyword_linksection,
197 Keyword_stdcallcc,
198194 Keyword_struct,
199195 Keyword_suspend,
200196 Keyword_switch,
......@@ -306,7 +302,6 @@ pub const Token = struct {
306302 .Keyword_for => "for",
307303 .Keyword_if => "if",
308304 .Keyword_inline => "inline",
309 .Keyword_nakedcc => "nakedcc",
310305 .Keyword_noalias => "noalias",
311306 .Keyword_noinline => "noinline",
312307 .Keyword_nosuspend => "nosuspend",
......@@ -318,7 +313,6 @@ pub const Token = struct {
318313 .Keyword_resume => "resume",
319314 .Keyword_return => "return",
320315 .Keyword_linksection => "linksection",
321 .Keyword_stdcallcc => "stdcallcc",
322316 .Keyword_struct => "struct",
323317 .Keyword_suspend => "suspend",
324318 .Keyword_switch => "switch",
src-self-hosted/translate_c.zig-3
......@@ -4094,7 +4094,6 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a
40944094 .return_type = proto_alias.return_type,
40954095 .var_args_token = null,
40964096 .extern_export_inline_token = inline_tok,
4097 .cc_token = null,
40984097 .body_node = null,
40994098 .lib_name = null,
41004099 .align_expr = null,
......@@ -4753,7 +4752,6 @@ fn finishTransFnProto(
47534752 .return_type = .{ .Explicit = return_type_node },
47544753 .var_args_token = null, // TODO this field is broken in the AST data model
47554754 .extern_export_inline_token = extern_export_inline_tok,
4756 .cc_token = null,
47574755 .body_node = null,
47584756 .lib_name = null,
47594757 .align_expr = align_expr,
......@@ -5119,7 +5117,6 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8,
51195117 .return_type = .{ .Explicit = &type_of.base },
51205118 .doc_comments = null,
51215119 .var_args_token = null,
5122 .cc_token = null,
51235120 .body_node = null,
51245121 .lib_name = null,
51255122 .align_expr = null,
src/all_types.hpp-1
......@@ -718,7 +718,6 @@ struct AstNodeFnProto {
718718 Buf doc_comments;
719719
720720 FnInline fn_inline;
721 bool is_async;
722721
723722 VisibMod visib_mod;
724723 bool auto_err_set;
src/analyze.cpp-2
......@@ -1528,8 +1528,6 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
15281528}
15291529
15301530CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto) {
1531 if (fn_proto->is_async)
1532 return CallingConventionAsync;
15331531 // Compatible with the C ABI
15341532 if (fn_proto->is_extern || fn_proto->is_export)
15351533 return CallingConventionC;
src/parser.cpp+5-53
......@@ -93,7 +93,6 @@ static AstNode *ast_parse_field_init(ParseContext *pc);
9393static AstNode *ast_parse_while_continue_expr(ParseContext *pc);
9494static AstNode *ast_parse_link_section(ParseContext *pc);
9595static AstNode *ast_parse_callconv(ParseContext *pc);
96static Optional<AstNodeFnProto> ast_parse_fn_cc(ParseContext *pc);
9796static AstNode *ast_parse_param_decl(ParseContext *pc);
9897static AstNode *ast_parse_param_type(ParseContext *pc);
9998static AstNode *ast_parse_if_prefix(ParseContext *pc);
......@@ -707,7 +706,6 @@ static AstNode *ast_parse_top_level_decl(ParseContext *pc, VisibMod visib_mod, B
707706 fn_proto->column = first->start_column;
708707 fn_proto->data.fn_proto.visib_mod = visib_mod;
709708 fn_proto->data.fn_proto.doc_comments = *doc_comments;
710 // ast_parse_fn_cc may set it
711709 if (!fn_proto->data.fn_proto.is_extern)
712710 fn_proto->data.fn_proto.is_extern = first->id == TokenIdKeywordExtern;
713711 fn_proto->data.fn_proto.is_export = first->id == TokenIdKeywordExport;
......@@ -788,29 +786,11 @@ static AstNode *ast_parse_top_level_decl(ParseContext *pc, VisibMod visib_mod, B
788786 return nullptr;
789787}
790788
791// FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
789// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
792790static AstNode *ast_parse_fn_proto(ParseContext *pc) {
793 Token *first = peek_token(pc);
794 AstNodeFnProto fn_cc;
795 Token *fn;
796 if (ast_parse_fn_cc(pc).unwrap(&fn_cc)) {
797 // The extern keyword for fn CC is also used for container decls.
798 // We therefore put it back, as allow container decl to consume it
799 // later.
800 if (fn_cc.is_extern) {
801 fn = eat_token_if(pc, TokenIdKeywordFn);
802 if (fn == nullptr) {
803 put_back_token(pc);
804 return nullptr;
805 }
806 } else {
807 fn = expect_token(pc, TokenIdKeywordFn);
808 }
809 } else {
810 fn_cc = {};
811 fn = eat_token_if(pc, TokenIdKeywordFn);
812 if (fn == nullptr)
813 return nullptr;
791 Token *first = eat_token_if(pc, TokenIdKeywordFn);
792 if (first == nullptr) {
793 return nullptr;
814794 }
815795
816796 Token *identifier = eat_token_if(pc, TokenIdSymbol);
......@@ -830,7 +810,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) {
830810 }
831811
832812 AstNode *res = ast_create_node(pc, NodeTypeFnProto, first);
833 res->data.fn_proto = fn_cc;
813 res->data.fn_proto = {};
834814 res->data.fn_proto.name = token_buf(identifier);
835815 res->data.fn_proto.params = params;
836816 res->data.fn_proto.align_expr = align_expr;
......@@ -1524,17 +1504,6 @@ static AstNode *ast_parse_error_union_expr(ParseContext *pc) {
15241504static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
15251505 Token *async_token = eat_token_if(pc, TokenIdKeywordAsync);
15261506 if (async_token) {
1527 if (eat_token_if(pc, TokenIdKeywordFn) != nullptr) {
1528 // HACK: If we see the keyword `fn`, then we assume that
1529 // we are parsing an async fn proto, and not a call.
1530 // We therefore put back all tokens consumed by the async
1531 // prefix...
1532 put_back_token(pc);
1533 put_back_token(pc);
1534
1535 return ast_parse_primary_type_expr(pc);
1536 }
1537
15381507 AstNode *child = ast_expect(pc, ast_parse_primary_type_expr);
15391508 while (true) {
15401509 AstNode *suffix = ast_parse_suffix_op(pc);
......@@ -2187,23 +2156,6 @@ static AstNode *ast_parse_callconv(ParseContext *pc) {
21872156 return res;
21882157}
21892158
2190// FnCC
2191// <- KEYWORD_extern
2192// / KEYWORD_async
2193static Optional<AstNodeFnProto> ast_parse_fn_cc(ParseContext *pc) {
2194 AstNodeFnProto res = {};
2195 if (eat_token_if(pc, TokenIdKeywordAsync) != nullptr) {
2196 res.is_async = true;
2197 return Optional<AstNodeFnProto>::some(res);
2198 }
2199 if (eat_token_if(pc, TokenIdKeywordExtern) != nullptr) {
2200 res.is_extern = true;
2201 return Optional<AstNodeFnProto>::some(res);
2202 }
2203
2204 return Optional<AstNodeFnProto>::none();
2205}
2206
22072159// ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType
22082160static AstNode *ast_parse_param_decl(ParseContext *pc) {
22092161 Buf doc_comments = BUF_INIT;