From c5c301b08a6a604d15e4ac6f18cb4ad5a916b4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 1 Feb 2021 13:37:58 +0100 Subject: [PATCH] ir: Support pointers to zsts. Fixes #654 --- src/bindgen/ir/ty.rs | 12 ++---------- tests/expectations/zst.both.c | 11 +++++++++++ tests/expectations/zst.both.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.c | 11 +++++++++++ tests/expectations/zst.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.cpp | 16 ++++++++++++++++ tests/expectations/zst.pyx | 13 +++++++++++++ tests/expectations/zst.tag.c | 11 +++++++++++ tests/expectations/zst.tag.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.tag.pyx | 13 +++++++++++++ tests/rust/zst.rs | 10 ++++++++++ 11 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 tests/expectations/zst.both.c create mode 100644 tests/expectations/zst.both.compat.c create mode 100644 tests/expectations/zst.c create mode 100644 tests/expectations/zst.compat.c create mode 100644 tests/expectations/zst.cpp create mode 100644 tests/expectations/zst.pyx create mode 100644 tests/expectations/zst.tag.c create mode 100644 tests/expectations/zst.tag.compat.c create mode 100644 tests/expectations/zst.tag.pyx create mode 100644 tests/rust/zst.rs diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index 5574d2f..7d59f01 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -367,11 +367,7 @@ impl Type { let converted = match converted { Some(converted) => converted, - None => { - return Err("Cannot have a pointer to a zero sized type. If you are \ - trying to represent `void*` use `c_void*`." - .to_owned()); - } + None => Type::Primitive(PrimitiveType::Void), }; // TODO(emilio): we could make these use is_ref: true. @@ -388,11 +384,7 @@ impl Type { let converted = match converted { Some(converted) => converted, - None => { - return Err("Cannot have a pointer to a zero sized type. If you are \ - trying to represent `void*` use `c_void*`." - .to_owned()); - } + None => Type::Primitive(PrimitiveType::Void), }; let is_const = pointer.mutability.is_none(); diff --git a/tests/expectations/zst.both.c b/tests/expectations/zst.both.c new file mode 100644 index 0000000..b03132f --- /dev/null +++ b/tests/expectations/zst.both.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct TraitObject { + void *data; + void *vtable; +} TraitObject; + +void *root(const void *ptr, struct TraitObject t); diff --git a/tests/expectations/zst.both.compat.c b/tests/expectations/zst.both.compat.c new file mode 100644 index 0000000..154f14f --- /dev/null +++ b/tests/expectations/zst.both.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +typedef struct TraitObject { + void *data; + void *vtable; +} TraitObject; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, struct TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.c b/tests/expectations/zst.c new file mode 100644 index 0000000..04caa7d --- /dev/null +++ b/tests/expectations/zst.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct { + void *data; + void *vtable; +} TraitObject; + +void *root(const void *ptr, TraitObject t); diff --git a/tests/expectations/zst.compat.c b/tests/expectations/zst.compat.c new file mode 100644 index 0000000..5dbc3ea --- /dev/null +++ b/tests/expectations/zst.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +typedef struct { + void *data; + void *vtable; +} TraitObject; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.cpp b/tests/expectations/zst.cpp new file mode 100644 index 0000000..6d6a4e2 --- /dev/null +++ b/tests/expectations/zst.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +extern "C" { + +void *root(const void *ptr, TraitObject t); + +} // extern "C" diff --git a/tests/expectations/zst.pyx b/tests/expectations/zst.pyx new file mode 100644 index 0000000..2e96545 --- /dev/null +++ b/tests/expectations/zst.pyx @@ -0,0 +1,13 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + ctypedef struct TraitObject: + void *data; + void *vtable; + + void *root(const void *ptr, TraitObject t); diff --git a/tests/expectations/zst.tag.c b/tests/expectations/zst.tag.c new file mode 100644 index 0000000..0a999af --- /dev/null +++ b/tests/expectations/zst.tag.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +void *root(const void *ptr, struct TraitObject t); diff --git a/tests/expectations/zst.tag.compat.c b/tests/expectations/zst.tag.compat.c new file mode 100644 index 0000000..8a115b7 --- /dev/null +++ b/tests/expectations/zst.tag.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, struct TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.tag.pyx b/tests/expectations/zst.tag.pyx new file mode 100644 index 0000000..03235cc --- /dev/null +++ b/tests/expectations/zst.tag.pyx @@ -0,0 +1,13 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + cdef struct TraitObject: + void *data; + void *vtable; + + void *root(const void *ptr, TraitObject t); diff --git a/tests/rust/zst.rs b/tests/rust/zst.rs new file mode 100644 index 0000000..8a3e467 --- /dev/null +++ b/tests/rust/zst.rs @@ -0,0 +1,10 @@ +#[repr(C)] +pub struct TraitObject { + pub data: *mut (), + pub vtable: *mut (), +} + +#[no_mangle] +pub extern "C" fn root(ptr: *const (), t: TraitObject) -> *mut () { + std::ptr::null_mut() +}