diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-13 21:30:49 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-13 21:30:49 +0100 |
commit | dfb25b389bec5492b6fd61e92a2552cab03a6afc (patch) | |
tree | 7543d75c1a760d7681d0c1daf9447469d7509c2c | |
parent | aaa7280a08b65aebe42b5405aec2bf72ae780885 (diff) |
codegen: Prefer use instead of type aliases.
-rw-r--r-- | libbindgen/src/codegen/mod.rs | 46 | ||||
-rw-r--r-- | libbindgen/src/ir/ty.rs | 8 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/anon_enum.rs | 6 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/bitfield_method_mangling.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/inherit_typedef.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/reparented_replacement.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/union_fields.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/unknown_attr.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/headers/anon_enum.hpp | 5 |
9 files changed, 56 insertions, 19 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index b223783e..b4388020 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -523,22 +523,40 @@ impl CodeGenerator for Type { typedef = typedef.attr().doc(comment); } - let mut generics = typedef.type_(rust_name).generics(); - for template_arg in applicable_template_args.iter() { - let template_arg = ctx.resolve_type(*template_arg); - if template_arg.is_named() { - let name = template_arg.name().unwrap(); - if name.contains("typename ") { - warn!("Item contained `typename`'d template \ - parameter: {:?}", item); - return; + // We prefer using `pub use` over `pub type` because of: + // https://github.com/rust-lang/rust/issues/26264 + let simple_enum_path = match inner_rust_type.node { + ast::TyKind::Path(None, ref p) => { + if applicable_template_args.is_empty() && + !inner_item.expect_type().canonical_type(ctx).is_builtin_or_named() && + p.segments.iter().all(|p| p.parameters.is_none()) { + Some(p.clone()) + } else { + None } - generics = - generics.ty_param_id(template_arg.name().unwrap()); - } - } + }, + _ => None, + }; - let typedef = generics.build().build_ty(inner_rust_type); + let typedef = if let Some(p) = simple_enum_path { + typedef.use_().build(p).as_(rust_name) + } else { + let mut generics = typedef.type_(rust_name).generics(); + for template_arg in applicable_template_args.iter() { + let template_arg = ctx.resolve_type(*template_arg); + if template_arg.is_named() { + let name = template_arg.name().unwrap(); + if name.contains("typename ") { + warn!("Item contained `typename`'d template \ + parameter: {:?}", item); + return; + } + generics = + generics.ty_param_id(template_arg.name().unwrap()); + } + } + generics.build().build_ty(inner_rust_type) + }; result.push(typedef) } TypeKind::Enum(ref ei) => { diff --git a/libbindgen/src/ir/ty.rs b/libbindgen/src/ir/ty.rs index e83ef4fd..c1ed5b64 100644 --- a/libbindgen/src/ir/ty.rs +++ b/libbindgen/src/ir/ty.rs @@ -123,6 +123,14 @@ impl Type { } } + /// Is this an enum type? + pub fn is_enum(&self) -> bool { + match self.kind { + TypeKind::Enum(..) => true, + _ => false, + } + } + /// Is this either a builtin or named type? pub fn is_builtin_or_named(&self) -> bool { match self.kind { diff --git a/libbindgen/tests/expectations/tests/anon_enum.rs b/libbindgen/tests/expectations/tests/anon_enum.rs index 075830e6..8a287d3d 100644 --- a/libbindgen/tests/expectations/tests/anon_enum.rs +++ b/libbindgen/tests/expectations/tests/anon_enum.rs @@ -22,3 +22,9 @@ fn bindgen_test_layout_Test() { impl Clone for Test { fn clone(&self) -> Self { *self } } +pub const Foo: _bindgen_ty_1 = _bindgen_ty_1::Foo; +pub const Bar: _bindgen_ty_1 = _bindgen_ty_1::Bar; +#[repr(u32)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum _bindgen_ty_1 { Foo = 0, Bar = 1, } +pub use _bindgen_ty_1 as Baz; diff --git a/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs b/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs index 5aba8abb..b7869bf8 100644 --- a/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs +++ b/libbindgen/tests/expectations/tests/bitfield_method_mangling.rs @@ -46,4 +46,4 @@ impl _bindgen_ty_1 { ((val as u32 as u32) << 24u32) & (4278190080usize as u32); } } -pub type mach_msg_type_descriptor_t = _bindgen_ty_1; +pub use _bindgen_ty_1 as mach_msg_type_descriptor_t; diff --git a/libbindgen/tests/expectations/tests/inherit_typedef.rs b/libbindgen/tests/expectations/tests/inherit_typedef.rs index ca9041e2..b66cb724 100644 --- a/libbindgen/tests/expectations/tests/inherit_typedef.rs +++ b/libbindgen/tests/expectations/tests/inherit_typedef.rs @@ -17,7 +17,7 @@ fn bindgen_test_layout_Foo() { impl Clone for Foo { fn clone(&self) -> Self { *self } } -pub type TypedefedFoo = Foo; +pub use Foo as TypedefedFoo; #[repr(C)] #[derive(Debug, Copy)] pub struct Bar { diff --git a/libbindgen/tests/expectations/tests/reparented_replacement.rs b/libbindgen/tests/expectations/tests/reparented_replacement.rs index 74ee229c..e8ccc931 100644 --- a/libbindgen/tests/expectations/tests/reparented_replacement.rs +++ b/libbindgen/tests/expectations/tests/reparented_replacement.rs @@ -25,5 +25,5 @@ pub mod root { fn clone(&self) -> Self { *self } } } - pub type ReferencesBar = root::foo::Bar; + pub use root::foo::Bar as ReferencesBar; } diff --git a/libbindgen/tests/expectations/tests/union_fields.rs b/libbindgen/tests/expectations/tests/union_fields.rs index 495e80f9..655a30ff 100644 --- a/libbindgen/tests/expectations/tests/union_fields.rs +++ b/libbindgen/tests/expectations/tests/union_fields.rs @@ -44,4 +44,4 @@ fn bindgen_test_layout__bindgen_ty_1() { impl Clone for _bindgen_ty_1 { fn clone(&self) -> Self { *self } } -pub type nsStyleUnion = _bindgen_ty_1; +pub use _bindgen_ty_1 as nsStyleUnion; diff --git a/libbindgen/tests/expectations/tests/unknown_attr.rs b/libbindgen/tests/expectations/tests/unknown_attr.rs index fd9cce45..8ed508c5 100644 --- a/libbindgen/tests/expectations/tests/unknown_attr.rs +++ b/libbindgen/tests/expectations/tests/unknown_attr.rs @@ -13,4 +13,4 @@ pub struct _bindgen_ty_1 { impl Clone for _bindgen_ty_1 { fn clone(&self) -> Self { *self } } -pub type max_align_t = _bindgen_ty_1; +pub use _bindgen_ty_1 as max_align_t; diff --git a/libbindgen/tests/headers/anon_enum.hpp b/libbindgen/tests/headers/anon_enum.hpp index c7405202..1961fe6c 100644 --- a/libbindgen/tests/headers/anon_enum.hpp +++ b/libbindgen/tests/headers/anon_enum.hpp @@ -3,3 +3,8 @@ struct Test { float bar; enum { T_NONE }; }; + +typedef enum { + Foo, + Bar, +} Baz; |