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
204204c_longlong long long for ABI compatibility with C
205205c_ulonglong unsigned long long for ABI compatibility with C
206206c_long_double long double for ABI compatibility with C
207c_void void for ABI compatibility with C
207208```
208209
209210### Boolean Type
src/all_types.hpp+1
......@@ -1062,6 +1062,7 @@ struct CodeGen {
10621062 TypeTableEntry *entry_int[2][4]; // [signed,unsigned][8,16,32,64]
10631063 TypeTableEntry *entry_c_int[CIntTypeCount];
10641064 TypeTableEntry *entry_c_long_double;
1065 TypeTableEntry *entry_c_void;
10651066 TypeTableEntry *entry_u8;
10661067 TypeTableEntry *entry_u16;
10671068 TypeTableEntry *entry_u32;
src/analyze.cpp+2-2
......@@ -2320,9 +2320,9 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
23202320 bool pointer_only = false;
23212321 return analyze_decl_ref(g, node, decl_node, pointer_only);
23222322 } else {
2323 const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
23232324 add_node_error(g, node,
2324 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
2325 buf_ptr(namespace_import->path)));
2325 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), import_name));
23262326 return g->builtin_types.entry_invalid;
23272327 }
23282328 } else {
src/codegen.cpp+5
......@@ -3551,6 +3551,11 @@ static void define_builtin_types(CodeGen *g) {
35513551 g->builtin_types.entry_i32 = get_int_type(g, true, 32);
35523552 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
35543559 {
35553560 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPureError);
35563561 buf_init_from_str(&entry->name, "error");
src/parseh.cpp+2-15
......@@ -36,7 +36,6 @@ struct Context {
3636 ZigList<ErrorMsg *> *errors;
3737 bool warnings_on;
3838 VisibMod visib_mod;
39 TypeTableEntry *c_void_type;
4039 AstNode *root;
4140 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table;
4241 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
292291 return node;
293292}
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
304294static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {
305 if (!c->c_void_type) {
306 return false;
307 }
308295 while (type_entry->id == TypeTableEntryIdTypeDecl) {
309 if (type_entry == c->c_void_type) {
296 if (type_entry == c->codegen->builtin_types.entry_c_void) {
310297 return true;
311298 }
312299 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
336323 const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
337324 switch (builtin_ty->getKind()) {
338325 case BuiltinType::Void:
339 return get_c_void_type(c);
326 return c->codegen->builtin_types.entry_c_void;
340327 case BuiltinType::Bool:
341328 return c->codegen->builtin_types.entry_bool;
342329 case BuiltinType::Char_U:
test/run_tests.cpp+4-6
......@@ -1095,7 +1095,7 @@ pub fn main(args: [][]u8) -> %void {
10951095 add_simple_case_libc("expose function pointer to C land", R"SOURCE(
10961096const 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 {
10991099 const a_int = (&i32)(a ?? unreachable{});
11001100 const b_int = (&i32)(b ?? unreachable{});
11011101 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 {
11101110export fn main(args: c_int, argv: &&u8) -> c_int {
11111111 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
11151115 for (array) |item, i| {
11161116 if (item != i) {
......@@ -1802,8 +1802,7 @@ pub const Foo1 = enum_Foo._1;)OUTPUT",
18021802
18031803 add_parseh_case("restrict -> noalias", R"SOURCE(
18041804void foo(void *restrict bar, void *restrict);
1805 )SOURCE", 1, R"OUTPUT(pub type c_void = u8;
1806pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
1805 )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
18071806
18081807 add_parseh_case("simple struct", R"SOURCE(
18091808struct Foo {
......@@ -1916,8 +1915,7 @@ struct Bar {
19161915 add_parseh_case("typedef void", R"SOURCE(
19171916typedef void Foo;
19181917Foo fun(Foo *a);
1919 )SOURCE", 3,
1920 "pub type c_void = u8;",
1918 )SOURCE", 2,
19211919 "pub const Foo = c_void;",
19221920 "pub extern fn fun(a: ?&c_void);");
19231921