| ... | @@ -938,7 +938,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) { | ... | @@ -938,7 +938,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) { |
| 938 | do_code_gen(g); | 938 | do_code_gen(g); |
| 939 | } | 939 | } |
| 940 | | 940 | |
| 941 | static Buf *to_c_type(CodeGen *g, AstNode *type_node) { | 941 | static void to_c_type(CodeGen *g, AstNode *type_node, Buf *out_buf) { |
| 942 | assert(type_node->type == NodeTypeType); | 942 | assert(type_node->type == NodeTypeType); |
| 943 | assert(type_node->codegen_node); | 943 | assert(type_node->codegen_node); |
| 944 | | 944 | |
| ... | @@ -947,13 +947,16 @@ static Buf *to_c_type(CodeGen *g, AstNode *type_node) { | ... | @@ -947,13 +947,16 @@ static Buf *to_c_type(CodeGen *g, AstNode *type_node) { |
| 947 | | 947 | |
| 948 | if (type_entry == g->builtin_types.entry_u8) { | 948 | if (type_entry == g->builtin_types.entry_u8) { |
| 949 | g->c_stdint_used = true; | 949 | g->c_stdint_used = true; |
| 950 | return buf_create_from_str("uint8_t"); | 950 | buf_init_from_str(out_buf, "uint8_t"); |
| 951 | } else if (type_entry == g->builtin_types.entry_i32) { | 951 | } else if (type_entry == g->builtin_types.entry_i32) { |
| 952 | g->c_stdint_used = true; | 952 | g->c_stdint_used = true; |
| 953 | return buf_create_from_str("int32_t"); | 953 | buf_init_from_str(out_buf, "int32_t"); |
| 954 | } else if (type_entry == g->builtin_types.entry_unreachable) { | 954 | } else if (type_entry == g->builtin_types.entry_unreachable) { |
| 955 | g->c_unreachable_used = true; | 955 | buf_init_from_str(out_buf, "__attribute__((__noreturn__)) void"); |
| 956 | return g->c_unreachable_macro; | 956 | } else if (type_entry == g->builtin_types.entry_bool) { |
| | 957 | buf_init_from_str(out_buf, "unsigned char"); |
| | 958 | } else if (type_entry == g->builtin_types.entry_void) { |
| | 959 | buf_init_from_str(out_buf, "void"); |
| 957 | } else { | 960 | } else { |
| 958 | zig_panic("TODO to_c_type"); | 961 | zig_panic("TODO to_c_type"); |
| 959 | } | 962 | } |
| ... | @@ -971,9 +974,6 @@ static void generate_h_file(CodeGen *g) { | ... | @@ -971,9 +974,6 @@ static void generate_h_file(CodeGen *g) { |
| 971 | Buf *extern_c_macro = buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)); | 974 | Buf *extern_c_macro = buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)); |
| 972 | buf_upcase(extern_c_macro); | 975 | buf_upcase(extern_c_macro); |
| 973 | | 976 | |
| 974 | g->c_unreachable_macro = buf_sprintf("%s_UNREACHABLE", buf_ptr(g->root_out_name)); | | |
| 975 | buf_upcase(g->c_unreachable_macro); | | |
| 976 | | | |
| 977 | Buf h_buf = BUF_INIT; | 977 | Buf h_buf = BUF_INIT; |
| 978 | buf_resize(&h_buf, 0); | 978 | buf_resize(&h_buf, 0); |
| 979 | for (int fn_def_i = 0; fn_def_i < g->fn_defs.length; fn_def_i += 1) { | 979 | for (int fn_def_i = 0; fn_def_i < g->fn_defs.length; fn_def_i += 1) { |
| ... | @@ -985,19 +985,22 @@ static void generate_h_file(CodeGen *g) { | ... | @@ -985,19 +985,22 @@ static void generate_h_file(CodeGen *g) { |
| 985 | if (fn_proto->visib_mod != FnProtoVisibModExport) | 985 | if (fn_proto->visib_mod != FnProtoVisibModExport) |
| 986 | continue; | 986 | continue; |
| 987 | | 987 | |
| 988 | Buf *return_type_c = to_c_type(g, fn_proto->return_type); | 988 | Buf return_type_c = BUF_INIT; |
| | 989 | to_c_type(g, fn_proto->return_type, &return_type_c); |
| 989 | | 990 | |
| 990 | buf_appendf(&h_buf, "%s %s %s(", | 991 | buf_appendf(&h_buf, "%s %s %s(", |
| 991 | buf_ptr(export_macro), | 992 | buf_ptr(export_macro), |
| 992 | buf_ptr(return_type_c), | 993 | buf_ptr(&return_type_c), |
| 993 | buf_ptr(&fn_proto->name)); | 994 | buf_ptr(&fn_proto->name)); |
| 994 | | 995 | |
| | 996 | Buf param_type_c = BUF_INIT; |
| 995 | if (fn_proto->params.length) { | 997 | if (fn_proto->params.length) { |
| 996 | for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) { | 998 | for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) { |
| 997 | AstNode *param_decl_node = fn_proto->params.at(param_i); | 999 | AstNode *param_decl_node = fn_proto->params.at(param_i); |
| 998 | AstNode *param_type = param_decl_node->data.param_decl.type; | 1000 | AstNode *param_type = param_decl_node->data.param_decl.type; |
| | 1001 | to_c_type(g, param_type, &param_type_c); |
| 999 | buf_appendf(&h_buf, "%s %s", | 1002 | buf_appendf(&h_buf, "%s %s", |
| 1000 | buf_ptr(to_c_type(g, param_type)), | 1003 | buf_ptr(&param_type_c), |
| 1001 | buf_ptr(&param_decl_node->data.param_decl.name)); | 1004 | buf_ptr(&param_decl_node->data.param_decl.name)); |
| 1002 | if (param_i < fn_proto->params.length - 1) | 1005 | if (param_i < fn_proto->params.length - 1) |
| 1003 | buf_appendf(&h_buf, ", "); | 1006 | buf_appendf(&h_buf, ", "); |
| ... | @@ -1020,8 +1023,6 @@ static void generate_h_file(CodeGen *g) { | ... | @@ -1020,8 +1023,6 @@ static void generate_h_file(CodeGen *g) { |
| 1020 | | 1023 | |
| 1021 | if (g->c_stdint_used) | 1024 | if (g->c_stdint_used) |
| 1022 | fprintf(out_h, "#include <stdint.h>\n"); | 1025 | fprintf(out_h, "#include <stdint.h>\n"); |
| 1023 | if (g->c_unreachable_used) | | |
| 1024 | fprintf(out_h, "#define %s __attribute__((__noreturn__)) void\n", buf_ptr(g->c_unreachable_macro)); | | |
| 1025 | | 1026 | |
| 1026 | fprintf(out_h, "\n"); | 1027 | fprintf(out_h, "\n"); |
| 1027 | | 1028 | |