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...@@ -43,6 +43,7 @@ make
43## Roadmap43## Roadmap
4444
45 * unreachable <--> noreturn attribute45 * unreachable <--> noreturn attribute
46 * error for extern function with void parameter
46 * unused label error47 * unused label error
47 * loops48 * loops
48 * structs49 * structs
src/codegen.cpp+14-13
...@@ -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}
940940
941static Buf *to_c_type(CodeGen *g, AstNode *type_node) {941static 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);
944944
...@@ -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) {
947947
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);
973976
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;
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
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));
994995
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) {
10201023
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));
10251026
1026 fprintf(out_h, "\n");1027 fprintf(out_h, "\n");
10271028
src/parseh.cpp-5
...@@ -290,11 +290,6 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl...@@ -290,11 +290,6 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl
290290
291 buf_init_from_str(&p->cur_fn->name, clang_getCString(name));291 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
298 p->cur_fn->arg_count = clang_getNumArgTypes(fn_type);293 p->cur_fn->arg_count = clang_getNumArgTypes(fn_type);
299 p->cur_fn->args = allocate<Arg>(p->cur_fn->arg_count);294 p->cur_fn->args = allocate<Arg>(p->cur_fn->arg_count);
300295
src/semantic_info.hpp-2
...@@ -102,8 +102,6 @@ struct CodeGen {...@@ -102,8 +102,6 @@ struct CodeGen {
102 FnTableEntry *cur_fn;102 FnTableEntry *cur_fn;
103 LLVMBasicBlockRef cur_basic_block;103 LLVMBasicBlockRef cur_basic_block;
104 bool c_stdint_used;104 bool c_stdint_used;
105 bool c_unreachable_used;
106 Buf *c_unreachable_macro;
107 AstNode *root_export_decl;105 AstNode *root_export_decl;
108 int version_major;106 int version_major;
109 int version_minor;107 int version_minor;