Skip to content

Commit 77278fc

Browse files
author
bors-servo
authored
Auto merge of #396 - emilio:enum-alias, r=fitzgen
codegen: Prefer use instead of type aliases. r? @fitzgen
2 parents f103d8a + 0ceeaf8 commit 77278fc

File tree

13 files changed

+78
-30
lines changed

13 files changed

+78
-30
lines changed

bindgen-integration/cpp/Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ class Test {
88
Test(int foo);
99
Test(double foo);
1010
};
11+
12+
typedef Test TypeAlias;

libbindgen/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ cfg-if = "0.1.0"
2929
clang-sys = { version = "0.12", features = ["runtime", "clang_3_9"] }
3030
lazy_static = "0.2.1"
3131
rustc-serialize = "0.3.19"
32-
syntex_syntax = "0.50"
32+
syntex_syntax = "0.54"
3333
regex = "0.2"
3434

3535
[dependencies.aster]
3636
features = ["with-syntex"]
37-
version = "0.34"
37+
version = "0.38"
3838

3939
[dependencies.env_logger]
4040
optional = true
@@ -46,7 +46,7 @@ version = "0.3"
4646

4747
[dependencies.quasi]
4848
features = ["with-syntex"]
49-
version = "0.26"
49+
version = "0.29"
5050

5151
[features]
5252
assert_no_dangling_items = []

libbindgen/src/codegen/helpers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,15 @@ pub mod ast_ty {
153153
}
154154

155155
pub fn float_expr(f: f64) -> P<ast::Expr> {
156-
use aster::str::ToInternedString;
156+
use aster::symbol::ToSymbol;
157157
let mut string = f.to_string();
158158

159159
// So it gets properly recognised as a floating point constant.
160160
if !string.contains('.') {
161161
string.push('.');
162162
}
163163

164-
let interned_str = string.as_str().to_interned_string();
165-
let kind = ast::LitKind::FloatUnsuffixed(interned_str);
164+
let kind = ast::LitKind::FloatUnsuffixed(string.as_str().to_symbol());
166165
aster::AstBuilder::new().expr().lit().build_lit(kind)
167166
}
168167

libbindgen/src/codegen/mod.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,46 @@ impl CodeGenerator for Type {
523523
typedef = typedef.attr().doc(comment);
524524
}
525525

526-
let mut generics = typedef.type_(rust_name).generics();
527-
for template_arg in applicable_template_args.iter() {
528-
let template_arg = ctx.resolve_type(*template_arg);
529-
if template_arg.is_named() {
530-
let name = template_arg.name().unwrap();
531-
if name.contains("typename ") {
532-
warn!("Item contained `typename`'d template \
533-
parameter: {:?}", item);
534-
return;
526+
// We prefer using `pub use` over `pub type` because of:
527+
// https://github.com/rust-lang/rust/issues/26264
528+
let simple_enum_path = match inner_rust_type.node {
529+
ast::TyKind::Path(None, ref p) => {
530+
if applicable_template_args.is_empty() &&
531+
!inner_item.expect_type().canonical_type(ctx).is_builtin_or_named() &&
532+
p.segments.iter().all(|p| p.parameters.is_none()) {
533+
Some(p.clone())
534+
} else {
535+
None
535536
}
536-
generics =
537-
generics.ty_param_id(template_arg.name().unwrap());
538-
}
539-
}
537+
},
538+
_ => None,
539+
};
540540

541-
let typedef = generics.build().build_ty(inner_rust_type);
541+
let typedef = if let Some(mut p) = simple_enum_path {
542+
if p.segments.len() == 1 {
543+
p.segments.insert(0, ast::PathSegment {
544+
identifier: ctx.ext_cx().ident_of("self"),
545+
parameters: None,
546+
});
547+
}
548+
typedef.use_().build(p).as_(rust_name)
549+
} else {
550+
let mut generics = typedef.type_(rust_name).generics();
551+
for template_arg in applicable_template_args.iter() {
552+
let template_arg = ctx.resolve_type(*template_arg);
553+
if template_arg.is_named() {
554+
let name = template_arg.name().unwrap();
555+
if name.contains("typename ") {
556+
warn!("Item contained `typename`'d template \
557+
parameter: {:?}", item);
558+
return;
559+
}
560+
generics =
561+
generics.ty_param_id(template_arg.name().unwrap());
562+
}
563+
}
564+
generics.build().build_ty(inner_rust_type)
565+
};
542566
result.push(typedef)
543567
}
544568
TypeKind::Enum(ref ei) => {
@@ -1863,16 +1887,19 @@ impl ToRustTy for Type {
18631887
if let ast::TyKind::Path(_, ref mut path) = inner_ty.node {
18641888
let template_args = template_args.iter()
18651889
.map(|arg| arg.to_rust_ty(ctx))
1866-
.collect();
1890+
.collect::<Vec<_>>();
18671891

1868-
path.segments.last_mut().unwrap().parameters =
1869-
ast::PathParameters::AngleBracketed(
1892+
path.segments.last_mut().unwrap().parameters = if template_args.is_empty() {
1893+
None
1894+
} else {
1895+
Some(P(ast::PathParameters::AngleBracketed(
18701896
ast::AngleBracketedParameterData {
18711897
lifetimes: vec![],
18721898
types: P::from_vec(template_args),
18731899
bindings: P::from_vec(vec![]),
18741900
}
1875-
);
1901+
)))
1902+
}
18761903
}
18771904

18781905
P(inner_ty)

libbindgen/src/ir/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ impl<'ctx> BindgenContext<'ctx> {
461461
pub fn gen<F, Out>(&mut self, cb: F) -> Out
462462
where F: FnOnce(&Self) -> Out,
463463
{
464+
use aster::symbol::ToSymbol;
464465
use syntax::ext::expand::ExpansionConfig;
465466
use syntax::codemap::{ExpnInfo, MacroBang, NameAndSpan};
466467
use syntax::ext::base;
@@ -475,7 +476,7 @@ impl<'ctx> BindgenContext<'ctx> {
475476
ctx.0.bt_push(ExpnInfo {
476477
call_site: self.span,
477478
callee: NameAndSpan {
478-
format: MacroBang(parse::token::intern("")),
479+
format: MacroBang("".to_symbol()),
479480
allow_internal_unstable: false,
480481
span: None,
481482
},

libbindgen/src/ir/ty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ impl Type {
123123
}
124124
}
125125

126+
/// Is this an enum type?
127+
pub fn is_enum(&self) -> bool {
128+
match self.kind {
129+
TypeKind::Enum(..) => true,
130+
_ => false,
131+
}
132+
}
133+
126134
/// Is this either a builtin or named type?
127135
pub fn is_builtin_or_named(&self) -> bool {
128136
match self.kind {

libbindgen/tests/expectations/tests/anon_enum.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ fn bindgen_test_layout_Test() {
2222
impl Clone for Test {
2323
fn clone(&self) -> Self { *self }
2424
}
25+
pub const Foo: _bindgen_ty_1 = _bindgen_ty_1::Foo;
26+
pub const Bar: _bindgen_ty_1 = _bindgen_ty_1::Bar;
27+
#[repr(u32)]
28+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
29+
pub enum _bindgen_ty_1 { Foo = 0, Bar = 1, }
30+
pub use self::_bindgen_ty_1 as Baz;

libbindgen/tests/expectations/tests/bitfield_method_mangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ impl _bindgen_ty_1 {
4646
((val as u32 as u32) << 24u32) & (4278190080usize as u32);
4747
}
4848
}
49-
pub type mach_msg_type_descriptor_t = _bindgen_ty_1;
49+
pub use self::_bindgen_ty_1 as mach_msg_type_descriptor_t;

libbindgen/tests/expectations/tests/inherit_typedef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn bindgen_test_layout_Foo() {
1717
impl Clone for Foo {
1818
fn clone(&self) -> Self { *self }
1919
}
20-
pub type TypedefedFoo = Foo;
20+
pub use self::Foo as TypedefedFoo;
2121
#[repr(C)]
2222
#[derive(Debug, Copy)]
2323
pub struct Bar {

libbindgen/tests/expectations/tests/reparented_replacement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub mod root {
2525
fn clone(&self) -> Self { *self }
2626
}
2727
}
28-
pub type ReferencesBar = root::foo::Bar;
28+
pub use root::foo::Bar as ReferencesBar;
2929
}

libbindgen/tests/expectations/tests/union_fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ fn bindgen_test_layout__bindgen_ty_1() {
4444
impl Clone for _bindgen_ty_1 {
4545
fn clone(&self) -> Self { *self }
4646
}
47-
pub type nsStyleUnion = _bindgen_ty_1;
47+
pub use self::_bindgen_ty_1 as nsStyleUnion;

libbindgen/tests/expectations/tests/unknown_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ pub struct _bindgen_ty_1 {
1313
impl Clone for _bindgen_ty_1 {
1414
fn clone(&self) -> Self { *self }
1515
}
16-
pub type max_align_t = _bindgen_ty_1;
16+
pub use self::_bindgen_ty_1 as max_align_t;

libbindgen/tests/headers/anon_enum.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ struct Test {
33
float bar;
44
enum { T_NONE };
55
};
6+
7+
typedef enum {
8+
Foo,
9+
Bar,
10+
} Baz;

0 commit comments

Comments
 (0)