authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-03 17:30:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-03 17:30:44-07:00
log5144c4fa37c3c433d814c9c3b2182fa224ea6826
tree2ffe680c4ca0578ce7c72a54e949167700bf4dff
parenta398afa7cc50aea4a0a55aeaa369914447f20e3d

exporting .h file supports void


4 files changed, 15 insertions(+), 20 deletions(-)

README.md+1
......@@ -43,6 +43,7 @@ make
4343## Roadmap
4444
4545 * unreachable <--> noreturn attribute
46 * error for extern function with void parameter
4647 * unused label error
4748 * loops
4849 * structs
src/codegen.cpp+14-13
......@@ -938,7 +938,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
938938 do_code_gen(g);
939939}
940940
941static Buf *to_c_type(CodeGen *g, AstNode *type_node) {
941static void to_c_type(CodeGen *g, AstNode *type_node, Buf *out_buf) {
942942 assert(type_node->type == NodeTypeType);
943943 assert(type_node->codegen_node);
944944
......@@ -947,13 +947,16 @@ static Buf *to_c_type(CodeGen *g, AstNode *type_node) {
947947
948948 if (type_entry == g->builtin_types.entry_u8) {
949949 g->c_stdint_used = true;
950 return buf_create_from_str("uint8_t");
950 buf_init_from_str(out_buf, "uint8_t");
951951 } else if (type_entry == g->builtin_types.entry_i32) {
952952 g->c_stdint_used = true;
953 return buf_create_from_str("int32_t");
953 buf_init_from_str(out_buf, "int32_t");
954954 } else if (type_entry == g->builtin_types.entry_unreachable) {
955 g->c_unreachable_used = true;
956 return g->c_unreachable_macro;
955 buf_init_from_str(out_buf, "__attribute__((__noreturn__)) void");
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");
957960 } else {
958961 zig_panic("TODO to_c_type");
959962 }
......@@ -971,9 +974,6 @@ static void generate_h_file(CodeGen *g) {
971974 Buf *extern_c_macro = buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name));
972975 buf_upcase(extern_c_macro);
973976
974 g->c_unreachable_macro = buf_sprintf("%s_UNREACHABLE", buf_ptr(g->root_out_name));
975 buf_upcase(g->c_unreachable_macro);
976
977977 Buf h_buf = BUF_INIT;
978978 buf_resize(&h_buf, 0);
979979 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) {
985985 if (fn_proto->visib_mod != FnProtoVisibModExport)
986986 continue;
987987
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);
989990
990991 buf_appendf(&h_buf, "%s %s %s(",
991992 buf_ptr(export_macro),
992 buf_ptr(return_type_c),
993 buf_ptr(&return_type_c),
993994 buf_ptr(&fn_proto->name));
994995
996 Buf param_type_c = BUF_INIT;
995997 if (fn_proto->params.length) {
996998 for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) {
997999 AstNode *param_decl_node = fn_proto->params.at(param_i);
9981000 AstNode *param_type = param_decl_node->data.param_decl.type;
1001 to_c_type(g, param_type, &param_type_c);
9991002 buf_appendf(&h_buf, "%s %s",
1000 buf_ptr(to_c_type(g, param_type)),
1003 buf_ptr(&param_type_c),
10011004 buf_ptr(&param_decl_node->data.param_decl.name));
10021005 if (param_i < fn_proto->params.length - 1)
10031006 buf_appendf(&h_buf, ", ");
......@@ -1020,8 +1023,6 @@ static void generate_h_file(CodeGen *g) {
10201023
10211024 if (g->c_stdint_used)
10221025 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));
10251026
10261027 fprintf(out_h, "\n");
10271028
src/parseh.cpp-5
......@@ -290,11 +290,6 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl
290290
291291 buf_init_from_str(&p->cur_fn->name, clang_getCString(name));
292292
293 if (buf_eql_str(&p->cur_fn->name, "exit")) {
294 print_location(p);
295 zig_panic("Found it");
296 }
297
298293 p->cur_fn->arg_count = clang_getNumArgTypes(fn_type);
299294 p->cur_fn->args = allocate<Arg>(p->cur_fn->arg_count);
300295
src/semantic_info.hpp-2
......@@ -102,8 +102,6 @@ struct CodeGen {
102102 FnTableEntry *cur_fn;
103103 LLVMBasicBlockRef cur_basic_block;
104104 bool c_stdint_used;
105 bool c_unreachable_used;
106 Buf *c_unreachable_macro;
107105 AstNode *root_export_decl;
108106 int version_major;
109107 int version_minor;