diff options
author | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-11 00:29:58 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <ecoal95@gmail.com> | 2016-11-11 00:42:00 +0100 |
commit | 5e41fb90044adab76fe6a0a2560ea5e2c53ac6f8 (patch) | |
tree | a0374a43c24136f61d45b3fd77a55dcf57f4f787 | |
parent | b3322ddf2ae66a287bf53e94bcf1500ae5981c74 (diff) |
ir: Search for compound structures when we have unexposed type and base class cursor.
And it's not a valid identifier as a template parameter. See the comment and the
tests for details.
-rw-r--r-- | src/ir/comp.rs | 3 | ||||
-rw-r--r-- | src/ir/item.rs | 3 | ||||
-rw-r--r-- | src/ir/ty.rs | 50 | ||||
-rw-r--r-- | tests/expectations/tests/forward-inherit-struct-with-fields.rs | 17 | ||||
-rw-r--r-- | tests/expectations/tests/forward-inherit-struct.rs | 18 | ||||
-rw-r--r-- | tests/expectations/tests/inherit-namespaced.rs | 18 | ||||
-rw-r--r-- | tests/expectations/tests/multiple-inherit-empty-correct-layout.rs | 45 | ||||
-rw-r--r-- | tests/headers/forward-inherit-struct-with-fields.hpp | 8 | ||||
-rw-r--r-- | tests/headers/forward-inherit-struct.hpp | 5 | ||||
-rw-r--r-- | tests/headers/inherit-namespaced.hpp | 4 | ||||
-rw-r--r-- | tests/headers/multiple-inherit-empty-correct-layout.hpp | 3 |
11 files changed, 170 insertions, 4 deletions
diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 41e5c3d3..bd794a98 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -635,7 +635,7 @@ impl CompInfo { ci.has_vtable = cur.is_virtual_base(); } let type_id = - Item::from_ty(&cur.cur_type(), None, None, ctx) + Item::from_ty(&cur.cur_type(), Some(cur), None, ctx) .expect("BaseSpecifier"); ci.base_members.push(type_id); } @@ -763,6 +763,7 @@ impl CompInfo { CXCursor_UnionDecl => CompKind::Union, CXCursor_ClassDecl | CXCursor_StructDecl => CompKind::Struct, + CXCursor_CXXBaseSpecifier | CXCursor_ClassTemplatePartialSpecialization | CXCursor_ClassTemplate => { match cursor.template_kind() { diff --git a/src/ir/item.rs b/src/ir/item.rs index c6d80a08..691cfec2 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -1043,8 +1043,7 @@ impl ClangItemParser for Item { } } // If we have recursed into the AST all we know, and we still - // haven't found what we've got, let's - // just make a named type. + // haven't found what we've got, let's just make a named type. // // This is what happens with some template members, for example. // diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 77dc61be..57490cc2 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -540,8 +540,56 @@ impl Type { } else if let Some(location) = location { match location.kind() { CXCursor_ClassTemplatePartialSpecialization | + CXCursor_CXXBaseSpecifier | CXCursor_ClassTemplate => { - name = location.spelling(); + if location.kind() == CXCursor_CXXBaseSpecifier { + // In the case we're parsing a base specifier + // inside an unexposed or invalid type, it means + // that we're parsing one of two things: + // + // * A template parameter. + // * A complex class that isn't exposed. + // + // This means, unfortunately, that there's no + // good way to differentiate between them. + // + // Probably we could try to look at the + // declaration and complicate more this logic, + // but we'll keep it simple... if it's a valid + // C++ identifier, we'll consider it as a + // template parameter. + // + // This is because: + // + // * We expect every other base that is a + // proper identifier (that is, a simple + // struct/union declaration), to be exposed, + // so this path can't be reached in that + // case. + // + // * Quite conveniently, complex base + // specifiers preserve their full names (that + // is: Foo<T> instead of Foo). We can take + // advantage of this. + // + // If we find some edge case where this doesn't + // work (which I guess is unlikely, see the + // different test cases[1][2][3][4]), we'd need + // to find more creative ways of differentiating + // these two cases. + // + // [1]: inherit_named.hpp + // [2]: forward-inherit-struct-with-fields.hpp + // [3]: forward-inherit-struct.hpp + // [4]: inherit-namespaced.hpp + if location.spelling() + .chars() + .all(|c| c.is_alphanumeric() || c == '_') { + return Err(ParseError::Recurse); + } + } else { + name = location.spelling(); + } let complex = CompInfo::from_ty(potential_id, ty, Some(location), diff --git a/tests/expectations/tests/forward-inherit-struct-with-fields.rs b/tests/expectations/tests/forward-inherit-struct-with-fields.rs new file mode 100644 index 00000000..84104971 --- /dev/null +++ b/tests/expectations/tests/forward-inherit-struct-with-fields.rs @@ -0,0 +1,17 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Rooted<T> { + pub _base: RootedBase<T>, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct RootedBase<T> { + pub foo: *mut T, + pub next: *mut Rooted<T>, +} diff --git a/tests/expectations/tests/forward-inherit-struct.rs b/tests/expectations/tests/forward-inherit-struct.rs new file mode 100644 index 00000000..e053adcd --- /dev/null +++ b/tests/expectations/tests/forward-inherit-struct.rs @@ -0,0 +1,18 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Rooted<T> { + pub _address: u8, + pub _phantom_0: ::std::marker::PhantomData<T>, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct RootedBase<T> { + pub _address: u8, + pub _phantom_0: ::std::marker::PhantomData<T>, +} diff --git a/tests/expectations/tests/inherit-namespaced.rs b/tests/expectations/tests/inherit-namespaced.rs new file mode 100644 index 00000000..a58058b0 --- /dev/null +++ b/tests/expectations/tests/inherit-namespaced.rs @@ -0,0 +1,18 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct RootedBase<T> { + pub _address: u8, + pub _phantom_0: ::std::marker::PhantomData<T>, +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Rooted<T> { + pub _address: u8, + pub _phantom_0: ::std::marker::PhantomData<T>, +} diff --git a/tests/expectations/tests/multiple-inherit-empty-correct-layout.rs b/tests/expectations/tests/multiple-inherit-empty-correct-layout.rs new file mode 100644 index 00000000..5e9cf522 --- /dev/null +++ b/tests/expectations/tests/multiple-inherit-empty-correct-layout.rs @@ -0,0 +1,45 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Foo { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_Foo() { + assert_eq!(::std::mem::size_of::<Foo>() , 1usize); + assert_eq!(::std::mem::align_of::<Foo>() , 1usize); +} +impl Clone for Foo { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Bar { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_Bar() { + assert_eq!(::std::mem::size_of::<Bar>() , 1usize); + assert_eq!(::std::mem::align_of::<Bar>() , 1usize); +} +impl Clone for Bar { + fn clone(&self) -> Self { *self } +} +#[repr(C)] +#[derive(Debug, Copy)] +pub struct Baz { + pub _address: u8, +} +#[test] +fn bindgen_test_layout_Baz() { + assert_eq!(::std::mem::size_of::<Baz>() , 1usize); + assert_eq!(::std::mem::align_of::<Baz>() , 1usize); +} +impl Clone for Baz { + fn clone(&self) -> Self { *self } +} diff --git a/tests/headers/forward-inherit-struct-with-fields.hpp b/tests/headers/forward-inherit-struct-with-fields.hpp new file mode 100644 index 00000000..437fff5d --- /dev/null +++ b/tests/headers/forward-inherit-struct-with-fields.hpp @@ -0,0 +1,8 @@ +template <typename> class Rooted; +namespace js { + template <typename T> class RootedBase { + T* foo; + Rooted<T>* next; + }; +} +template <typename T> class Rooted : js::RootedBase<T> {}; diff --git a/tests/headers/forward-inherit-struct.hpp b/tests/headers/forward-inherit-struct.hpp new file mode 100644 index 00000000..ac7aef5e --- /dev/null +++ b/tests/headers/forward-inherit-struct.hpp @@ -0,0 +1,5 @@ +template <typename> class Rooted; +namespace js { + template <typename T> class RootedBase {}; +} +template <typename T> class Rooted : js::RootedBase<T> {}; diff --git a/tests/headers/inherit-namespaced.hpp b/tests/headers/inherit-namespaced.hpp new file mode 100644 index 00000000..61eafd5a --- /dev/null +++ b/tests/headers/inherit-namespaced.hpp @@ -0,0 +1,4 @@ +namespace js { + template <typename T> class RootedBase {}; +} +template <typename T> class Rooted : js::RootedBase<T> {}; diff --git a/tests/headers/multiple-inherit-empty-correct-layout.hpp b/tests/headers/multiple-inherit-empty-correct-layout.hpp new file mode 100644 index 00000000..1e2b133a --- /dev/null +++ b/tests/headers/multiple-inherit-empty-correct-layout.hpp @@ -0,0 +1,3 @@ +struct Foo {}; +struct Bar {}; +struct Baz : public Foo, public Bar {}; |