Commit 5df091fea9

Andrew Kelley <superjoe30@gmail.com>
2016-03-01 23:26:41
c_void is provided outside of C imports
1 parent 660a506
doc/langref.md
@@ -204,6 +204,7 @@ c_ulong         unsigned long       for ABI compatibility with C
 c_longlong      long long           for ABI compatibility with C
 c_ulonglong     unsigned long long  for ABI compatibility with C
 c_long_double   long double         for ABI compatibility with C
+c_void          void                for ABI compatibility with C
 ```
 
 ### Boolean Type
src/all_types.hpp
@@ -1062,6 +1062,7 @@ struct CodeGen {
         TypeTableEntry *entry_int[2][4]; // [signed,unsigned][8,16,32,64]
         TypeTableEntry *entry_c_int[CIntTypeCount];
         TypeTableEntry *entry_c_long_double;
+        TypeTableEntry *entry_c_void;
         TypeTableEntry *entry_u8;
         TypeTableEntry *entry_u16;
         TypeTableEntry *entry_u32;
src/analyze.cpp
@@ -2320,9 +2320,9 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
             bool pointer_only = false;
             return analyze_decl_ref(g, node, decl_node, pointer_only);
         } else {
+            const char *import_name = namespace_import->path ? buf_ptr(namespace_import->path) : "(C import)";
             add_node_error(g, node,
-                buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name),
-                    buf_ptr(namespace_import->path)));
+                buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), import_name));
             return g->builtin_types.entry_invalid;
         }
     } else {
src/codegen.cpp
@@ -3551,6 +3551,11 @@ static void define_builtin_types(CodeGen *g) {
     g->builtin_types.entry_i32 = get_int_type(g, true, 32);
     g->builtin_types.entry_i64 = get_int_type(g, true, 64);
 
+    {
+        g->builtin_types.entry_c_void = get_typedecl_type(g, "c_void", g->builtin_types.entry_u8);
+        g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
+    }
+
     {
         TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPureError);
         buf_init_from_str(&entry->name, "error");
src/parseh.cpp
@@ -36,7 +36,6 @@ struct Context {
     ZigList<ErrorMsg *> *errors;
     bool warnings_on;
     VisibMod visib_mod;
-    TypeTableEntry *c_void_type;
     AstNode *root;
     HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> global_type_table;
     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
     return node;
 }
 
-static TypeTableEntry *get_c_void_type(Context *c) {
-    if (!c->c_void_type) {
-        c->c_void_type = get_typedecl_type(c->codegen, "c_void", c->codegen->builtin_types.entry_u8);
-        add_typedef_node(c, c->c_void_type);
-    }
-
-    return c->c_void_type;
-}
-
 static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {
-    if (!c->c_void_type) {
-        return false;
-    }
     while (type_entry->id == TypeTableEntryIdTypeDecl) {
-        if (type_entry == c->c_void_type) {
+        if (type_entry == c->codegen->builtin_types.entry_c_void) {
             return true;
         }
         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
                 const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
                 switch (builtin_ty->getKind()) {
                     case BuiltinType::Void:
-                        return get_c_void_type(c);
+                        return c->codegen->builtin_types.entry_c_void;
                     case BuiltinType::Bool:
                         return c->codegen->builtin_types.entry_bool;
                     case BuiltinType::Char_U:
test/run_tests.cpp
@@ -1095,7 +1095,7 @@ pub fn main(args: [][]u8) -> %void {
     add_simple_case_libc("expose function pointer to C land", R"SOURCE(
 const c = @c_import(@c_include("stdlib.h"));
 
-export fn compare_fn(a: ?&const c.c_void, b: ?&const c.c_void) -> c_int {
+export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
     const a_int = (&i32)(a ?? unreachable{});
     const b_int = (&i32)(b ?? unreachable{});
     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 {
 export fn main(args: c_int, argv: &&u8) -> c_int {
     var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
 
-    c.qsort((&c.c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
+    c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
 
     for (array) |item, i| {
         if (item != i) {
@@ -1802,8 +1802,7 @@ pub const Foo1 = enum_Foo._1;)OUTPUT",
 
     add_parseh_case("restrict -> noalias", R"SOURCE(
 void foo(void *restrict bar, void *restrict);
-    )SOURCE", 1, R"OUTPUT(pub type c_void = u8;
-pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
+    )SOURCE", 1, R"OUTPUT(pub extern fn foo(noalias bar: ?&c_void, noalias arg1: ?&c_void);)OUTPUT");
 
     add_parseh_case("simple struct", R"SOURCE(
 struct Foo {
@@ -1916,8 +1915,7 @@ struct Bar {
     add_parseh_case("typedef void", R"SOURCE(
 typedef void Foo;
 Foo fun(Foo *a);
-    )SOURCE", 3,
-            "pub type c_void = u8;",
+    )SOURCE", 2,
             "pub const Foo = c_void;",
             "pub extern fn fun(a: ?&c_void);");