authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-03-01 15:26:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-03-01 15:26:41-07:00
log5df091fea98e91536f95e3b4bd5bcb0881f06989
tree72e34976d9bf0119a1f72cc9f991192c558de9c1
parent660a50661b22b279fed548387d19ffcddb031b23

c_void is provided outside of C imports


6 files changed, 15 insertions(+), 23 deletions(-)

doc/langref.md+1
...@@ -204,6 +204,7 @@ c_ulong unsigned long for ABI compatibility with C...@@ -204,6 +204,7 @@ c_ulong unsigned long for ABI compatibility with C
204c_longlong long long for ABI compatibility with C204c_longlong long long for ABI compatibility with C
205c_ulonglong unsigned long long for ABI compatibility with C205c_ulonglong unsigned long long for ABI compatibility with C
206c_long_double long double for ABI compatibility with C206c_long_double long double for ABI compatibility with C
207c_void void for ABI compatibility with C
207```208```
208209
209### Boolean Type210### Boolean Type
src/all_types.hpp+1
...@@ -1062,6 +1062,7 @@ struct CodeGen {...@@ -1062,6 +1062,7 @@ struct CodeGen {
1062 TypeTableEntry *entry_int[2][4]; // [signed,unsigned][8,16,32,64]1062 TypeTableEntry *entry_int[2][4]; // [signed,unsigned][8,16,32,64]
1063 TypeTableEntry *entry_c_int[CIntTypeCount];1063 TypeTableEntry *entry_c_int[CIntTypeCount];
1064 TypeTableEntry *entry_c_long_double;1064 TypeTableEntry *entry_c_long_double;
1065 TypeTableEntry *entry_c_void;
1065 TypeTableEntry *entry_u8;1066 TypeTableEntry *entry_u8;
1066 TypeTableEntry *entry_u16;1067 TypeTableEntry *entry_u16;
1067 TypeTableEntry *entry_u32;1068 TypeTableEntry *entry_u32;
src/analyze.cpp+2-2
...@@ -2320,9 +2320,9 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i...@@ -2320,9 +2320,9 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
2320 bool pointer_only = false;2320 bool pointer_only = false;
2321 return analyze_decl_ref(g, node, decl_node, pointer_only);2321 return analyze_decl_ref(g, node, decl_node, pointer_only);
2322 } else {2322 } else {
2323 const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
2323 add_node_error(g, node,2324 add_node_error(g, node,
2324 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),2325 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), import_name));
2325 buf_ptr(namespace_import->path)));
2326 return g->builtin_types.entry_invalid;2326 return g->builtin_types.entry_invalid;
2327 }2327 }
2328 } else {2328 } else {
src/codegen.cpp+5
...@@ -3551,6 +3551,11 @@ static void define_builtin_types(CodeGen *g) {...@@ -3551,6 +3551,11 @@ static void define_builtin_types(CodeGen *g) {
3551 g->builtin_types.entry_i32 = get_int_type(g, true, 32);3551 g->builtin_types.entry_i32 = get_int_type(g, true, 32);
3552 g->builtin_types.entry_i64 = get_int_type(g, true, 64);3552 g->builtin_types.entry_i64 = get_int_type(g, true, 64);
35533553
3554 {
3555 g->builtin_types.entry_c_void = get_typedecl_type(g, "c_void", g->builtin_types.entry_u8);
3556 g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
3557 }
3558
3554 {3559 {
3555 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPureError);3560 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPureError);
3556 buf_init_from_str(&entry->name, "error");3561 buf_init_from_str(&entry->name, "error");
src/parseh.cpp+2-15
...@@ -36,7 +36,6 @@ struct Context {...@@ -36,7 +36,6 @@ struct Context {
36 ZigList<ErrorMsg *> *errors;36 ZigList<ErrorMsg *> *errors;
37 bool warnings_on;37 bool warnings_on;
38 VisibMod visib_mod;38 VisibMod visib_mod;
39 TypeTableEntry *c_void_type;
40 AstNode *root;39 AstNode *root;
41 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table;40 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table;
42 HashMap<Buf *, GlobalValue, buf_hash, buf_eql_buf> global_value_table;41 HashMap<Buf *, GlobalValue, buf_hash, buf_eql_buf> global_value_table;
...@@ -292,21 +291,9 @@ static AstNode *add_const_var_node(Context *c, Buf *name, TypeTableEntry *type_e...@@ -292,21 +291,9 @@ static AstNode *add_const_var_node(Context *c, Buf *name, TypeTableEntry *type_e
292 return node;291 return node;
293}292}
294293
295static TypeTableEntry *get_c_void_type(Context *c) {
296 if (!c->c_void_type) {
297 c->c_void_type = get_typedecl_type(c->codegen, "c_void", c->codegen->builtin_types.entry_u8);
298 add_typedef_node(c, c->c_void_type);
299 }
300
301 return c->c_void_type;
302}
303
304static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {294static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {
305 if (!c->c_void_type) {
306 return false;
307 }
308 while (type_entry->id == TypeTableEntryIdTypeDecl) {295 while (type_entry->id == TypeTableEntryIdTypeDecl) {
309 if (type_entry == c->c_void_type) {296 if (type_entry == c->codegen->builtin_types.entry_c_void) {
310 return true;297 return true;
311 }298 }
312 type_entry = type_entry->data.type_decl.child_type;299 type_entry = type_entry->data.type_decl.child_type;
...@@ -336,7 +323,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const...@@ -336,7 +323,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
336 const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);323 const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
337 switch (builtin_ty->getKind()) {324 switch (builtin_ty->getKind()) {
338 case BuiltinType::Void:325 case BuiltinType::Void:
339 return get_c_void_type(c);326 return c->codegen->builtin_types.entry_c_void;
340 case BuiltinType::Bool:327 case BuiltinType::Bool:
341 return c->codegen->builtin_types.entry_bool;328 return c->codegen->builtin_types.entry_bool;
342 case BuiltinType::Char_U:329 case BuiltinType::Char_U:
test/run_tests.cpp+4-6
...@@ -1095,7 +1095,7 @@ pub fn main(args: [][]u8) -> %void {...@@ -1095,7 +1095,7 @@ pub fn main(args: [][]u8) -> %void {
1095 add_simple_case_libc("expose function pointer to C land", R"SOURCE(1095 add_simple_case_libc("expose function pointer to C land", R"SOURCE(
1096const c = @c_import(@c_include("stdlib.h"));1096const c = @c_import(@c_include("stdlib.h"));
10971097
1098export fn compare_fn(a: ?&const c.c_void, b: ?&const c.c_void) -> c_int {1098export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
1099 const a_int = (&i32)(a ?? unreachable{});1099 const a_int = (&i32)(a ?? unreachable{});
1100 const b_int = (&i32)(b ?? unreachable{});1100 const b_int = (&i32)(b ?? unreachable{});
1101 if (*a_int < *b_int) {1101 if (*a_int < *b_int) {
...@@ -1110,7 +1110,7 @@ export fn compare_fn(a: ?&const c.c_void, b: ?&const c.c_void) -> c_int {...@@ -1110,7 +1110,7 @@ export fn compare_fn(a: ?&const c.c_void, b: ?&const c.c_void) -> c_int {
1110export fn main(args: c_int, argv: &&u8) -> c_int {1110export fn main(args: c_int, argv: &&u8) -> c_int {
1111 var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };1111 var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
11121112
1113 c.qsort((&c.c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);1113 c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
11141114
1115 for (array) |item, i| {1115 for (array) |item, i| {
1116 if (item != i) {1116 if (item != i) {
...@@ -1802,8 +1802,7 @@ pub const Foo1 = enum_Foo._1;)OUTPUT",...@@ -1802,8 +1802,7 @@ pub const Foo1 = enum_Foo._1;)OUTPUT",
18021802
1803 add_parseh_case("restrict -> noalias", R"SOURCE(1803 add_parseh_case("restrict -> noalias", R"SOURCE(
1804void foo(void *restrict bar, void *restrict);1804void foo(void *restrict bar, void *restrict);
1805 )SOURCE", 1, R"OUTPUT(pub type c_void = u8;1805 )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
1806pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
18071806
1808 add_parseh_case("simple struct", R"SOURCE(1807 add_parseh_case("simple struct", R"SOURCE(
1809struct Foo {1808struct Foo {
...@@ -1916,8 +1915,7 @@ struct Bar {...@@ -1916,8 +1915,7 @@ struct Bar {
1916 add_parseh_case("typedef void", R"SOURCE(1915 add_parseh_case("typedef void", R"SOURCE(
1917typedef void Foo;1916typedef void Foo;
1918Foo fun(Foo *a);1917Foo fun(Foo *a);
1919 )SOURCE", 3,1918 )SOURCE", 2,
1920 "pub type c_void = u8;",
1921 "pub const Foo = c_void;",1919 "pub const Foo = c_void;",
1922 "pub extern fn fun(a: ?&c_void);");1920 "pub extern fn fun(a: ?&c_void);");
19231921