Skip to content

ir: Search for compound structures when we have unexposed type and base class cursor. #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
50 changes: 49 additions & 1 deletion src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 17 additions & 0 deletions tests/expectations/tests/forward-inherit-struct-with-fields.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
18 changes: 18 additions & 0 deletions tests/expectations/tests/forward-inherit-struct.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
18 changes: 18 additions & 0 deletions tests/expectations/tests/inherit-namespaced.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
45 changes: 45 additions & 0 deletions tests/expectations/tests/multiple-inherit-empty-correct-layout.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
8 changes: 8 additions & 0 deletions tests/headers/forward-inherit-struct-with-fields.hpp
Original file line number Diff line number Diff line change
@@ -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> {};
5 changes: 5 additions & 0 deletions tests/headers/forward-inherit-struct.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
template <typename> class Rooted;
namespace js {
template <typename T> class RootedBase {};
}
template <typename T> class Rooted : js::RootedBase<T> {};
4 changes: 4 additions & 0 deletions tests/headers/inherit-namespaced.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace js {
template <typename T> class RootedBase {};
}
template <typename T> class Rooted : js::RootedBase<T> {};
3 changes: 3 additions & 0 deletions tests/headers/multiple-inherit-empty-correct-layout.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Foo {};
struct Bar {};
struct Baz : public Foo, public Bar {};