Commit 5951b79af4

Andrew Kelley <andrew@ziglang.org>
2020-01-06 21:23:05
remove stdcallcc, extern, nakedcc from stage1; zig fmt rewrites
1 parent 0a9daeb
lib/std/zig/parser_test.zig
@@ -10,13 +10,15 @@ test "zig fmt: change @typeOf to @TypeOf" {
 }
 
 // TODO: Remove nakedcc/stdcallcc once zig 0.6.0 is released. See https://github.com/ziglang/zig/pull/3977
-test "zig fmt: convert nakedcc/stdcallcc into callconv(...)" {
+test "zig fmt: convert extern/nakedcc/stdcallcc into callconv(...)" {
     try testTransform(
         \\nakedcc fn foo1() void {}
         \\stdcallcc fn foo2() void {}
+        \\extern fn foo3() void {}
     ,
         \\fn foo1() callconv(.Naked) void {}
         \\fn foo2() callconv(.Stdcall) void {}
+        \\fn foo3() callconv(.C) void {}
         \\
     );
 }
lib/std/zig/render.zig
@@ -1311,17 +1311,22 @@ 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| {
-                try renderToken(tree, stream, extern_export_inline_token, indent, start_col, Space.Space); // extern/export
+                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";
+                }
             }
 
             if (fn_proto.lib_name) |lib_name| {
                 try renderExpression(allocator, stream, tree, indent, start_col, lib_name, Space.Space);
             }
 
-            // 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.cc_token) |cc_token| {
                 var str = tree.tokenSlicePtr(tree.tokens.at(cc_token));
                 if (mem.eql(u8, str, "stdcallcc")) {
@@ -1405,7 +1410,7 @@ fn renderExpression(
                 const callconv_lparen = tree.prevToken(callconv_expr.firstToken());
                 const callconv_kw = tree.prevToken(callconv_lparen);
 
-                try renderToken(tree, stream, callconv_kw, indent, start_col, Space.None); // section
+                try renderToken(tree, stream, callconv_kw, indent, start_col, Space.None); // callconv
                 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); // )
lib/std/builtin.zig
@@ -322,7 +322,6 @@ pub const TypeInfo = union(enum) {
             pub const FnDecl = struct {
                 fn_type: type,
                 inline_type: Inline,
-                calling_convention: CallingConvention,
                 is_var_args: bool,
                 is_extern: bool,
                 is_export: bool,
src/all_types.hpp
@@ -653,8 +653,6 @@ struct AstNodeFnProto {
     Buf doc_comments;
 
     FnInline fn_inline;
-    bool is_nakedcc;
-    bool is_stdcallcc;
     bool is_async;
 
     VisibMod visib_mod;
@@ -1610,7 +1608,6 @@ struct ZigFn {
     Buf **param_names;
     IrInstruction *err_code_spill;
     AstNode *assumed_non_async;
-    CallingConvention cc;
 
     AstNode *fn_no_inline_set_node;
     AstNode *fn_static_eval_set_node;
src/analyze.cpp
@@ -1451,8 +1451,6 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
 ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
     ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
     buf_resize(&fn_type->name, 0);
-    if (fn_type->data.fn.fn_type_id.cc == CallingConventionC)
-        buf_append_str(&fn_type->name, "extern ");
     buf_appendf(&fn_type->name, "fn(");
     size_t i = 0;
     for (; i < fn_type_id->next_param_index; i += 1) {
@@ -1465,7 +1463,7 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
         buf_appendf(&fn_type->name, "%svar", comma_str);
     }
     buf_append_str(&fn_type->name, ")");
-    if (fn_type_id->cc != CallingConventionUnspecified && fn_type_id->cc != CallingConventionC) {
+    if (fn_type_id->cc != CallingConventionUnspecified) {
         buf_appendf(&fn_type->name, " callconv(%s)", calling_convention_name(fn_type_id->cc));
     }
     buf_append_str(&fn_type->name, " var");
@@ -1479,10 +1477,6 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
 }
 
 CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto) {
-    if (fn_proto->is_nakedcc)
-        return CallingConventionNaked;
-    if (fn_proto->is_stdcallcc)
-        return CallingConventionStdcall;
     if (fn_proto->is_async)
         return CallingConventionAsync;
     // Compatible with the C ABI
@@ -1764,13 +1758,15 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
     return err_set_type;
 }
 
-static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_scope, ZigFn *fn_entry) {
+static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_scope, ZigFn *fn_entry,
+        CallingConvention cc)
+{
     assert(proto_node->type == NodeTypeFnProto);
     AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
     Error err;
 
     FnTypeId fn_type_id = {0};
-    init_fn_type_id(&fn_type_id, proto_node, fn_entry->cc, proto_node->data.fn_proto.params.length);
+    init_fn_type_id(&fn_type_id, proto_node, cc, proto_node->data.fn_proto.params.length);
 
     for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
         AstNode *param_node = fn_proto->params.at(fn_type_id.next_param_index);
@@ -3432,7 +3428,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
 
         Scope *child_scope = fn_table_entry->fndef_scope ? &fn_table_entry->fndef_scope->base : tld_fn->base.parent_scope;
 
-        fn_table_entry->cc = cc_from_fn_proto(fn_proto);
+        CallingConvention cc;
         if (fn_proto->callconv_expr != nullptr) {
             ZigType *cc_enum_value = get_builtin_type(g, "CallingConvention");
 
@@ -3444,7 +3440,9 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
                 return;
             }
 
-            fn_table_entry->cc = (CallingConvention)bigint_as_u32(&result_val->data.x_enum_tag);
+            cc = (CallingConvention)bigint_as_u32(&result_val->data.x_enum_tag);
+        } else {
+            cc = cc_from_fn_proto(fn_proto);
         }
 
         if (fn_proto->section_expr != nullptr) {
@@ -3455,7 +3453,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
             }
         }
 
-        fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry);
+        fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry, cc);
 
         if (type_is_invalid(fn_table_entry->type_entry)) {
             tld_fn->base.resolution = TldResolutionInvalid;
src/codegen.cpp
@@ -519,7 +519,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
     if (cc == CallingConventionNaked) {
         addLLVMFnAttr(llvm_fn, "naked");
     } else {
-        ZigLLVMFunctionSetCallingConv(llvm_fn, get_llvm_cc(g, fn_type->data.fn.fn_type_id.cc));
+        ZigLLVMFunctionSetCallingConv(llvm_fn, get_llvm_cc(g, cc));
     }
 
     bool want_cold = fn->is_cold || cc == CallingConventionCold;
src/ir.cpp
@@ -18263,7 +18263,6 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstruction *source_i
         buf_init_from_buf(&impl_fn->symbol_name, &fn_entry->symbol_name);
         impl_fn->fndef_scope = create_fndef_scope(ira->codegen, impl_fn->body_node, parent_scope, impl_fn);
         impl_fn->child_scope = &impl_fn->fndef_scope->base;
-        impl_fn->cc = fn_entry->cc;
         FnTypeId inst_fn_type_id = {0};
         init_fn_type_id(&inst_fn_type_id, fn_proto_node, fn_type_id->cc, new_fn_arg_count);
         inst_fn_type_id.param_count = 0;
@@ -22599,29 +22598,24 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
                     fn_decl_fields[1]->special = ConstValSpecialStatic;
                     fn_decl_fields[1]->type = type_info_fn_decl_inline_type;
                     bigint_init_unsigned(&fn_decl_fields[1]->data.x_enum_tag, fn_entry->fn_inline);
-                    // calling_convention: TypeInfo.CallingConvention
-                    ensure_field_index(fn_decl_val->type, "calling_convention", 2);
-                    fn_decl_fields[2]->special = ConstValSpecialStatic;
-                    fn_decl_fields[2]->type = get_builtin_type(ira->codegen, "CallingConvention");
-                    bigint_init_unsigned(&fn_decl_fields[2]->data.x_enum_tag, fn_entry->cc);
                     // is_var_args: bool
-                    ensure_field_index(fn_decl_val->type, "is_var_args", 3);
+                    ensure_field_index(fn_decl_val->type, "is_var_args", 2);
                     bool is_varargs = fn_node->is_var_args;
                     fn_decl_fields[3]->special = ConstValSpecialStatic;
                     fn_decl_fields[3]->type = ira->codegen->builtin_types.entry_bool;
                     fn_decl_fields[3]->data.x_bool = is_varargs;
                     // is_extern: bool
-                    ensure_field_index(fn_decl_val->type, "is_extern", 4);
+                    ensure_field_index(fn_decl_val->type, "is_extern", 3);
                     fn_decl_fields[4]->special = ConstValSpecialStatic;
                     fn_decl_fields[4]->type = ira->codegen->builtin_types.entry_bool;
                     fn_decl_fields[4]->data.x_bool = fn_node->is_extern;
                     // is_export: bool
-                    ensure_field_index(fn_decl_val->type, "is_export", 5);
+                    ensure_field_index(fn_decl_val->type, "is_export", 4);
                     fn_decl_fields[5]->special = ConstValSpecialStatic;
                     fn_decl_fields[5]->type = ira->codegen->builtin_types.entry_bool;
                     fn_decl_fields[5]->data.x_bool = fn_node->is_export;
                     // lib_name: ?[]const u8
-                    ensure_field_index(fn_decl_val->type, "lib_name", 6);
+                    ensure_field_index(fn_decl_val->type, "lib_name", 5);
                     fn_decl_fields[6]->special = ConstValSpecialStatic;
                     ZigType *u8_ptr = get_pointer_to_type_extra(
                         ira->codegen, ira->codegen->builtin_types.entry_u8,
@@ -22637,12 +22631,12 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
                         fn_decl_fields[6]->data.x_optional = nullptr;
                     }
                     // return_type: type
-                    ensure_field_index(fn_decl_val->type, "return_type", 7);
+                    ensure_field_index(fn_decl_val->type, "return_type", 6);
                     fn_decl_fields[7]->special = ConstValSpecialStatic;
                     fn_decl_fields[7]->type = ira->codegen->builtin_types.entry_type;
                     fn_decl_fields[7]->data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
                     // arg_names: [][] const u8
-                    ensure_field_index(fn_decl_val->type, "arg_names", 8);
+                    ensure_field_index(fn_decl_val->type, "arg_names", 7);
                     size_t fn_arg_count = fn_entry->variable_list.length;
                     ZigValue *fn_arg_name_array = create_const_vals(1);
                     fn_arg_name_array->special = ConstValSpecialStatic;
src/parser.cpp
@@ -2117,20 +2117,10 @@ static AstNode *ast_parse_callconv(ParseContext *pc) {
 }
 
 // FnCC
-//     <- KEYWORD_nakedcc
-//      / KEYWORD_stdcallcc
-//      / KEYWORD_extern
+//     <- KEYWORD_extern
 //      / KEYWORD_async
 static Optional<AstNodeFnProto> ast_parse_fn_cc(ParseContext *pc) {
     AstNodeFnProto res = {};
-    if (eat_token_if(pc, TokenIdKeywordNakedCC) != nullptr) {
-        res.is_nakedcc = true;
-        return Optional<AstNodeFnProto>::some(res);
-    }
-    if (eat_token_if(pc, TokenIdKeywordStdcallCC) != nullptr) {
-        res.is_stdcallcc = true;
-        return Optional<AstNodeFnProto>::some(res);
-    }
     if (eat_token_if(pc, TokenIdKeywordAsync) != nullptr) {
         res.is_async = true;
         return Optional<AstNodeFnProto>::some(res);
src/tokenizer.cpp
@@ -127,7 +127,6 @@ static const struct ZigKeyword zig_keywords[] = {
     {"for", TokenIdKeywordFor},
     {"if", TokenIdKeywordIf},
     {"inline", TokenIdKeywordInline},
-    {"nakedcc", TokenIdKeywordNakedCC},
     {"noalias", TokenIdKeywordNoAlias},
     {"noasync", TokenIdKeywordNoAsync},
     {"noinline", TokenIdKeywordNoInline},
@@ -139,7 +138,6 @@ static const struct ZigKeyword zig_keywords[] = {
     {"resume", TokenIdKeywordResume},
     {"return", TokenIdKeywordReturn},
     {"linksection", TokenIdKeywordLinkSection},
-    {"stdcallcc", TokenIdKeywordStdcallCC},
     {"struct", TokenIdKeywordStruct},
     {"suspend", TokenIdKeywordSuspend},
     {"switch", TokenIdKeywordSwitch},
@@ -1562,7 +1560,6 @@ const char * token_name(TokenId id) {
         case TokenIdKeywordFor: return "for";
         case TokenIdKeywordIf: return "if";
         case TokenIdKeywordInline: return "inline";
-        case TokenIdKeywordNakedCC: return "nakedcc";
         case TokenIdKeywordNoAlias: return "noalias";
         case TokenIdKeywordNoAsync: return "noasync";
         case TokenIdKeywordNoInline: return "noinline";
@@ -1573,7 +1570,6 @@ const char * token_name(TokenId id) {
         case TokenIdKeywordPub: return "pub";
         case TokenIdKeywordReturn: return "return";
         case TokenIdKeywordLinkSection: return "linksection";
-        case TokenIdKeywordStdcallCC: return "stdcallcc";
         case TokenIdKeywordStruct: return "struct";
         case TokenIdKeywordSwitch: return "switch";
         case TokenIdKeywordTest: return "test";
src/tokenizer.hpp
@@ -77,7 +77,6 @@ enum TokenId {
     TokenIdKeywordInline,
     TokenIdKeywordNoInline,
     TokenIdKeywordLinkSection,
-    TokenIdKeywordNakedCC,
     TokenIdKeywordNoAlias,
     TokenIdKeywordNoAsync,
     TokenIdKeywordNull,
@@ -87,7 +86,6 @@ enum TokenId {
     TokenIdKeywordPub,
     TokenIdKeywordResume,
     TokenIdKeywordReturn,
-    TokenIdKeywordStdcallCC,
     TokenIdKeywordStruct,
     TokenIdKeywordSuspend,
     TokenIdKeywordSwitch,