From 7ada59f873738931b4d162372dff0a33442112b6 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Mon, 4 May 2020 09:06:20 -0600 Subject: [PATCH] remove nakedcc/stdcallcc/async fn/extern fn fnproto --- doc/docgen.zig | 2 -- doc/langref.html.in | 7 +--- lib/std/zig/ast.zig | 2 -- lib/std/zig/parse.zig | 53 ++---------------------------- lib/std/zig/parser_test.zig | 16 --------- lib/std/zig/render.zig | 24 +------------- lib/std/zig/tokenizer.zig | 6 ---- src-self-hosted/translate_c.zig | 3 -- src/all_types.hpp | 1 - src/analyze.cpp | 2 -- src/parser.cpp | 58 +++------------------------------ 11 files changed, 9 insertions(+), 165 deletions(-) diff --git a/doc/docgen.zig b/doc/docgen.zig index a249a526a1b24377ab1486237515831e7753be44..92a7353fa143f7d8f3f166c3828a0fcb1c198f24 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -800,7 +800,6 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok .Keyword_for, .Keyword_if, .Keyword_inline, - .Keyword_nakedcc, .Keyword_noalias, .Keyword_noinline, .Keyword_nosuspend, @@ -813,7 +812,6 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok .Keyword_return, .Keyword_linksection, .Keyword_callconv, - .Keyword_stdcallcc, .Keyword_struct, .Keyword_suspend, .Keyword_switch, diff --git a/doc/langref.html.in b/doc/langref.html.in index 6026912893d48ec5641045dc764378e8908191a5..b2e1f6e6f66b12534ae63d7a8296a87329b8e3f1 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -10088,7 +10088,7 @@ TopLevelDecl / (KEYWORD_export / KEYWORD_extern STRINGLITERALSINGLE?)? KEYWORD_threadlocal? VarDecl / KEYWORD_usingnamespace Expr SEMICOLON -FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) +FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLON @@ -10255,11 +10255,6 @@ WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN -# Fn specific -FnCC - <- KEYWORD_extern - / KEYWORD_async - ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType ParamType diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 50051040d93c18e36f3070cf3730cd67e45569ff..220b0390f1ef0bacdbc60ff50f45184b8c87e0b9 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -875,7 +875,6 @@ pub const Node = struct { return_type: ReturnType, var_args_token: ?TokenIndex, extern_export_inline_token: ?TokenIndex, - cc_token: ?TokenIndex, body_node: ?*Node, lib_name: ?*Node, // populated if this is an extern declaration align_expr: ?*Node, // populated if align(A) is present @@ -929,7 +928,6 @@ pub const Node = struct { if (self.visib_token) |visib_token| return visib_token; if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token; assert(self.lib_name == null); - if (self.cc_token) |cc_token| return cc_token; return self.fn_token; } diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index d7f4d76cf6f57faac997390a7a8283f4b079c2e6..ac1c1f8b15fcbec18eb69bd4b3a04e5cfa6ee7d9 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -335,22 +335,9 @@ fn parseTopLevelDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node return use_node; } -/// FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) +/// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { - const cc = parseFnCC(arena, it, tree); - const fn_token = eatToken(it, .Keyword_fn) orelse { - if (cc) |fnCC| { - if (fnCC == .Extern) { - putBackToken(it, fnCC.Extern); // 'extern' is also used in ContainerDecl - } else { - try tree.errors.push(.{ - .ExpectedToken = .{ .token = it.index, .expected_id = .Keyword_fn }, - }); - return error.ParseError; - } - } - return null; - }; + const fn_token = eatToken(it, .Keyword_fn) orelse return null; const name_token = eatToken(it, .Identifier); const lparen = try expectToken(it, tree, .LParen); const params = try parseParamDeclList(arena, it, tree); @@ -389,7 +376,6 @@ fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { .return_type = return_type, .var_args_token = var_args_token, .extern_export_inline_token = null, - .cc_token = null, .body_node = null, .lib_name = null, .align_expr = align_expr, @@ -397,13 +383,6 @@ fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { .callconv_expr = callconv_expr, }; - if (cc) |kind| { - switch (kind) { - .CC => |token| fn_proto_node.cc_token = token, - .Extern => |token| fn_proto_node.extern_export_inline_token = token, - } - } - return &fn_proto_node.base; } @@ -1196,16 +1175,6 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { const maybe_async = eatToken(it, .Keyword_async); if (maybe_async) |async_token| { const token_fn = eatToken(it, .Keyword_fn); - if (token_fn != null) { - // HACK: If we see the keyword `fn`, then we assume that - // we are parsing an async fn proto, and not a call. - // We therefore put back all tokens consumed by the async - // prefix... - putBackToken(it, token_fn.?); - putBackToken(it, async_token); - return parsePrimaryTypeExpr(arena, it, tree); - } - // TODO: Implement hack for parsing `async fn ...` in ast_parse_suffix_expr var res = try expectNode(arena, it, tree, parsePrimaryTypeExpr, .{ .ExpectedPrimaryTypeExpr = .{ .token = it.index }, }); @@ -1778,24 +1747,6 @@ fn parseCallconv(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { return expr_node; } -/// FnCC -/// <- KEYWORD_nakedcc -/// / KEYWORD_stdcallcc -/// / KEYWORD_extern -/// / KEYWORD_async -fn parseFnCC(arena: *Allocator, it: *TokenIterator, tree: *Tree) ?FnCC { - if (eatToken(it, .Keyword_nakedcc)) |token| return FnCC{ .CC = token }; - if (eatToken(it, .Keyword_stdcallcc)) |token| return FnCC{ .CC = token }; - if (eatToken(it, .Keyword_extern)) |token| return FnCC{ .Extern = token }; - if (eatToken(it, .Keyword_async)) |token| return FnCC{ .CC = token }; - return null; -} - -const FnCC = union(enum) { - CC: TokenIndex, - Extern: TokenIndex, -}; - /// ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType fn parseParamDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { const doc_comments = try parseDocComment(arena, it, tree); diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index ebc6b92b78a2fe0047b5af0767062f8b317a3b87..198ce1ad94153b776807327f867c6f3519c1d0f9 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -123,22 +123,6 @@ test "zig fmt: trailing comma in fn parameter list" { ); } -// TODO: Remove nakedcc/stdcallcc once zig 0.6.0 is released. See https://github.com/ziglang/zig/pull/3977 -test "zig fmt: convert extern/nakedcc/stdcallcc into callconv(...)" { - try testTransform( - \\nakedcc fn foo1() void {} - \\stdcallcc fn foo2() void {} - \\extern fn foo3() void {} - \\extern "mylib" fn foo4() void {} - , - \\fn foo1() callconv(.Naked) void {} - \\fn foo2() callconv(.Stdcall) void {} - \\fn foo3() callconv(.C) void {} - \\fn foo4() callconv(.C) void {} - \\ - ); -} - test "zig fmt: comptime struct field" { try testCanonical( \\const Foo = struct { diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 441a91a28ae7bb6f134215653df34c3177da790f..3e2cebcd8aba72c8a91144d52a18c9b94bee53cc 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1413,32 +1413,14 @@ fn renderExpression( try renderToken(tree, stream, visib_token_index, indent, start_col, Space.Space); // pub } - // Some extra machinery is needed to rewrite the old-style cc - // notation to the new callconv one - var cc_rewrite_str: ?[*:0]const u8 = null; if (fn_proto.extern_export_inline_token) |extern_export_inline_token| { - const tok = tree.tokens.at(extern_export_inline_token); - if (tok.id != .Keyword_extern or fn_proto.body_node == null) { - try renderToken(tree, stream, extern_export_inline_token, indent, start_col, Space.Space); // extern/export - } else { - cc_rewrite_str = ".C"; - fn_proto.lib_name = null; - } + try renderToken(tree, stream, extern_export_inline_token, indent, start_col, Space.Space); // extern/export } if (fn_proto.lib_name) |lib_name| { try renderExpression(allocator, stream, tree, indent, start_col, lib_name, Space.Space); } - if (fn_proto.cc_token) |cc_token| { - var str = tree.tokenSlicePtr(tree.tokens.at(cc_token)); - if (mem.eql(u8, str, "stdcallcc")) { - cc_rewrite_str = ".Stdcall"; - } else if (mem.eql(u8, str, "nakedcc")) { - cc_rewrite_str = ".Naked"; - } else try renderToken(tree, stream, cc_token, indent, start_col, Space.Space); // stdcallcc - } - const lparen = if (fn_proto.name_token) |name_token| blk: { try renderToken(tree, stream, fn_proto.fn_token, indent, start_col, Space.Space); // fn try renderToken(tree, stream, name_token, indent, start_col, Space.None); // name @@ -1528,10 +1510,6 @@ fn renderExpression( try renderToken(tree, stream, callconv_lparen, indent, start_col, Space.None); // ( try renderExpression(allocator, stream, tree, indent, start_col, callconv_expr, Space.None); try renderToken(tree, stream, callconv_rparen, indent, start_col, Space.Space); // ) - } else if (cc_rewrite_str) |str| { - try stream.writeAll("callconv("); - try stream.writeAll(mem.spanZ(str)); - try stream.writeAll(") "); } switch (fn_proto.return_type) { diff --git a/lib/std/zig/tokenizer.zig b/lib/std/zig/tokenizer.zig index b6c1a06276ac02744e51db18c9e7aa4210d8bf76..089711871ea04cde2498e0fcf2b1e36886335bd8 100644 --- a/lib/std/zig/tokenizer.zig +++ b/lib/std/zig/tokenizer.zig @@ -47,7 +47,6 @@ pub const Token = struct { Keyword.init("for", .Keyword_for), Keyword.init("if", .Keyword_if), Keyword.init("inline", .Keyword_inline), - Keyword.init("nakedcc", .Keyword_nakedcc), Keyword.init("noalias", .Keyword_noalias), Keyword.init("noasync", .Keyword_nosuspend), // TODO: remove this Keyword.init("noinline", .Keyword_noinline), @@ -60,7 +59,6 @@ pub const Token = struct { Keyword.init("resume", .Keyword_resume), Keyword.init("return", .Keyword_return), Keyword.init("linksection", .Keyword_linksection), - Keyword.init("stdcallcc", .Keyword_stdcallcc), Keyword.init("struct", .Keyword_struct), Keyword.init("suspend", .Keyword_suspend), Keyword.init("switch", .Keyword_switch), @@ -181,7 +179,6 @@ pub const Token = struct { Keyword_for, Keyword_if, Keyword_inline, - Keyword_nakedcc, Keyword_noalias, Keyword_noinline, Keyword_nosuspend, @@ -194,7 +191,6 @@ pub const Token = struct { Keyword_resume, Keyword_return, Keyword_linksection, - Keyword_stdcallcc, Keyword_struct, Keyword_suspend, Keyword_switch, @@ -306,7 +302,6 @@ pub const Token = struct { .Keyword_for => "for", .Keyword_if => "if", .Keyword_inline => "inline", - .Keyword_nakedcc => "nakedcc", .Keyword_noalias => "noalias", .Keyword_noinline => "noinline", .Keyword_nosuspend => "nosuspend", @@ -318,7 +313,6 @@ pub const Token = struct { .Keyword_resume => "resume", .Keyword_return => "return", .Keyword_linksection => "linksection", - .Keyword_stdcallcc => "stdcallcc", .Keyword_struct => "struct", .Keyword_suspend => "suspend", .Keyword_switch => "switch", diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 1c689e9f76ae3423af8ad667193f04f73cae11b2..6bbb133bb137e93ddbfd375646374eb38f7bebc1 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -4094,7 +4094,6 @@ fn transCreateNodeMacroFn(c: *Context, name: []const u8, ref: *ast.Node, proto_a .return_type = proto_alias.return_type, .var_args_token = null, .extern_export_inline_token = inline_tok, - .cc_token = null, .body_node = null, .lib_name = null, .align_expr = null, @@ -4753,7 +4752,6 @@ fn finishTransFnProto( .return_type = .{ .Explicit = return_type_node }, .var_args_token = null, // TODO this field is broken in the AST data model .extern_export_inline_token = extern_export_inline_tok, - .cc_token = null, .body_node = null, .lib_name = null, .align_expr = align_expr, @@ -5119,7 +5117,6 @@ fn transMacroFnDefine(c: *Context, it: *CTokenList.Iterator, source: []const u8, .return_type = .{ .Explicit = &type_of.base }, .doc_comments = null, .var_args_token = null, - .cc_token = null, .body_node = null, .lib_name = null, .align_expr = null, diff --git a/src/all_types.hpp b/src/all_types.hpp index aeffa440ab2b0f23777ea30925f32b5f0a23408f..61b6cbd74a52e4c29aa13a824ff140b8fb053a6d 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -718,7 +718,6 @@ struct AstNodeFnProto { Buf doc_comments; FnInline fn_inline; - bool is_async; VisibMod visib_mod; bool auto_err_set; diff --git a/src/analyze.cpp b/src/analyze.cpp index 4c3103c48cf692db2cbd2d8a9753b345476a971f..39f797a9ec9a96a186eac451e76852a0269a3d10 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1528,8 +1528,6 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) { } CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto) { - if (fn_proto->is_async) - return CallingConventionAsync; // Compatible with the C ABI if (fn_proto->is_extern || fn_proto->is_export) return CallingConventionC; diff --git a/src/parser.cpp b/src/parser.cpp index 71f7adf7d1552e11d1b7e90360b3472ae8683195..1a929cd1b1a89159aa00441eeb9258d241d30f96 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -93,7 +93,6 @@ static AstNode *ast_parse_field_init(ParseContext *pc); static AstNode *ast_parse_while_continue_expr(ParseContext *pc); static AstNode *ast_parse_link_section(ParseContext *pc); static AstNode *ast_parse_callconv(ParseContext *pc); -static Optional ast_parse_fn_cc(ParseContext *pc); static AstNode *ast_parse_param_decl(ParseContext *pc); static AstNode *ast_parse_param_type(ParseContext *pc); static AstNode *ast_parse_if_prefix(ParseContext *pc); @@ -707,7 +706,6 @@ static AstNode *ast_parse_top_level_decl(ParseContext *pc, VisibMod visib_mod, B fn_proto->column = first->start_column; fn_proto->data.fn_proto.visib_mod = visib_mod; fn_proto->data.fn_proto.doc_comments = *doc_comments; - // ast_parse_fn_cc may set it if (!fn_proto->data.fn_proto.is_extern) fn_proto->data.fn_proto.is_extern = first->id == TokenIdKeywordExtern; 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 return nullptr; } -// FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) +// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr) static AstNode *ast_parse_fn_proto(ParseContext *pc) { - Token *first = peek_token(pc); - AstNodeFnProto fn_cc; - Token *fn; - if (ast_parse_fn_cc(pc).unwrap(&fn_cc)) { - // The extern keyword for fn CC is also used for container decls. - // We therefore put it back, as allow container decl to consume it - // later. - if (fn_cc.is_extern) { - fn = eat_token_if(pc, TokenIdKeywordFn); - if (fn == nullptr) { - put_back_token(pc); - return nullptr; - } - } else { - fn = expect_token(pc, TokenIdKeywordFn); - } - } else { - fn_cc = {}; - fn = eat_token_if(pc, TokenIdKeywordFn); - if (fn == nullptr) - return nullptr; + Token *first = eat_token_if(pc, TokenIdKeywordFn); + if (first == nullptr) { + return nullptr; } Token *identifier = eat_token_if(pc, TokenIdSymbol); @@ -830,7 +810,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) { } AstNode *res = ast_create_node(pc, NodeTypeFnProto, first); - res->data.fn_proto = fn_cc; + res->data.fn_proto = {}; res->data.fn_proto.name = token_buf(identifier); res->data.fn_proto.params = params; res->data.fn_proto.align_expr = align_expr; @@ -1524,17 +1504,6 @@ static AstNode *ast_parse_error_union_expr(ParseContext *pc) { static AstNode *ast_parse_suffix_expr(ParseContext *pc) { Token *async_token = eat_token_if(pc, TokenIdKeywordAsync); if (async_token) { - if (eat_token_if(pc, TokenIdKeywordFn) != nullptr) { - // HACK: If we see the keyword `fn`, then we assume that - // we are parsing an async fn proto, and not a call. - // We therefore put back all tokens consumed by the async - // prefix... - put_back_token(pc); - put_back_token(pc); - - return ast_parse_primary_type_expr(pc); - } - AstNode *child = ast_expect(pc, ast_parse_primary_type_expr); while (true) { AstNode *suffix = ast_parse_suffix_op(pc); @@ -2187,23 +2156,6 @@ static AstNode *ast_parse_callconv(ParseContext *pc) { return res; } -// FnCC -// <- KEYWORD_extern -// / KEYWORD_async -static Optional ast_parse_fn_cc(ParseContext *pc) { - AstNodeFnProto res = {}; - if (eat_token_if(pc, TokenIdKeywordAsync) != nullptr) { - res.is_async = true; - return Optional::some(res); - } - if (eat_token_if(pc, TokenIdKeywordExtern) != nullptr) { - res.is_extern = true; - return Optional::some(res); - } - - return Optional::none(); -} - // ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType static AstNode *ast_parse_param_decl(ParseContext *pc) { Buf doc_comments = BUF_INIT; -- 2.54.0