@@ -376,65 +376,13 @@ fn gen_mod(mut ctx: &mut GenCtx,
376
376
}
377
377
}
378
378
379
- // XXX: Replace the name-based lookup, or do it at parse-time,
380
- // to keep all the mess in the same place.
381
- fn type_opaque ( ctx : & GenCtx , ty : & Type ) -> bool {
382
- match * ty {
383
- TComp ( ref ci) if ci. borrow ( ) . opaque => return true ,
384
- _ => { }
385
- }
386
-
387
- let ty_name = ty. name ( ) ;
388
-
389
- match ty_name {
390
- Some ( ty_name)
391
- => ctx. options . opaque_types . iter ( ) . any ( |name| * name == ty_name) ,
392
- None => false ,
393
- }
394
- }
395
-
396
- fn global_opaque ( ctx : & GenCtx , global : & Global ) -> bool {
397
- let global_name = global. name ( ) ;
398
-
399
- match * global {
400
- GCompDecl ( ref ci) |
401
- GComp ( ref ci) if ci. borrow ( ) . opaque => return true ,
402
- _ => { }
403
- }
404
-
405
- // Can't make an opaque type without layout
406
- global. layout ( ) . is_some ( ) &&
407
- ctx. options . opaque_types . iter ( ) . any ( |name| * name == global_name)
408
- }
409
-
410
- fn type_blacklisted ( ctx : & GenCtx , global : & Global ) -> bool {
411
- let global_name = global. name ( ) ;
412
-
413
- ctx. options . blacklist_type . iter ( ) . any ( |name| * name == global_name)
414
- }
415
-
416
379
fn gen_global ( mut ctx : & mut GenCtx ,
417
380
g : Global ,
418
381
defs : & mut Vec < P < ast:: Item > > ) {
419
- // XXX unify with anotations both type_blacklisted
420
- // and type_opaque (which actually doesn't mean the same).
421
- if type_blacklisted ( ctx, & g) {
422
- return ;
423
- }
424
-
425
- if global_opaque ( ctx, & g) {
426
- let name = first ( rust_id ( ctx, & g. name ( ) ) ) ;
427
- let layout = g. layout ( ) . unwrap ( ) ;
428
- defs. push ( mk_opaque_struct ( ctx, & name, & layout) ) ;
429
- // This should always be true but anyways..
430
- defs. push ( mk_test_fn ( ctx, & name, & layout) ) ;
431
- return ;
432
- }
433
-
434
382
match g {
435
383
GType ( ti) => {
436
384
let t = ti. borrow ( ) . clone ( ) ;
437
- defs. push ( ctypedef_to_rs ( & mut ctx, t) )
385
+ defs. extend ( ctypedef_to_rs ( & mut ctx, t) . into_iter ( ) )
438
386
} ,
439
387
GCompDecl ( ci) => {
440
388
let c = ci. borrow ( ) . clone ( ) ;
@@ -802,7 +750,7 @@ fn tag_dup_decl(gs: &[Global]) -> Vec<Global> {
802
750
res
803
751
}
804
752
805
- fn ctypedef_to_rs ( ctx : & mut GenCtx , ty : TypeInfo ) -> P < ast:: Item > {
753
+ fn ctypedef_to_rs ( ctx : & mut GenCtx , ty : TypeInfo ) -> Vec < P < ast:: Item > > {
806
754
fn mk_item ( ctx : & mut GenCtx , name : & str , comment : & str , ty : & Type ) -> P < ast:: Item > {
807
755
let rust_name = rust_type_id ( ctx, name) ;
808
756
let rust_ty = if cty_is_translatable ( ty) {
@@ -829,7 +777,14 @@ fn ctypedef_to_rs(ctx: &mut GenCtx, ty: TypeInfo) -> P<ast::Item> {
829
777
} )
830
778
}
831
779
832
- match ty. ty {
780
+ if ty. opaque {
781
+ return vec ! [
782
+ mk_opaque_struct( ctx, & ty. name, & ty. layout) ,
783
+ mk_test_fn( ctx, & ty. name, & ty. layout) ,
784
+ ] ;
785
+ }
786
+
787
+ let item = match ty. ty {
833
788
TComp ( ref ci) => {
834
789
assert ! ( !ci. borrow( ) . name. is_empty( ) ) ;
835
790
mk_item ( ctx, & ty. name , & ty. comment , & ty. ty )
@@ -839,11 +794,26 @@ fn ctypedef_to_rs(ctx: &mut GenCtx, ty: TypeInfo) -> P<ast::Item> {
839
794
mk_item ( ctx, & ty. name , & ty. comment , & ty. ty )
840
795
} ,
841
796
_ => mk_item ( ctx, & ty. name , & ty. comment , & ty. ty ) ,
842
- }
797
+ } ;
798
+
799
+ vec ! [ item]
843
800
}
844
801
845
802
fn comp_to_rs ( ctx : & mut GenCtx , name : & str , ci : CompInfo )
846
803
-> Vec < P < ast:: Item > > {
804
+ if ci. hide {
805
+ return vec ! [ ] ;
806
+ }
807
+
808
+ if ci. opaque {
809
+ let name = first ( rust_id ( ctx, & ci. name ) ) ;
810
+ // The test should always be correct but...
811
+ return vec ! [
812
+ mk_opaque_struct( ctx, & name, & ci. layout) ,
813
+ mk_test_fn( ctx, & name, & ci. layout) ,
814
+ ] ;
815
+ }
816
+
847
817
match ci. kind {
848
818
CompKind :: Struct => cstruct_to_rs ( ctx, name, ci) ,
849
819
CompKind :: Union => cunion_to_rs ( ctx, name, ci) ,
@@ -852,27 +822,19 @@ fn comp_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo)
852
822
853
823
fn comp_attrs ( ctx : & GenCtx , ci : & CompInfo , name : & str , has_destructor : bool , extra : & mut Vec < P < ast:: Item > > ) -> Vec < ast:: Attribute > {
854
824
let mut attrs = mk_doc_attr ( ctx, & ci. comment ) ;
855
- attrs. push ( mk_repr_attr ( ctx, ci. layout ) ) ;
825
+ attrs. push ( mk_repr_attr ( ctx, & ci. layout ) ) ;
856
826
let mut derives = vec ! [ ] ;
857
827
828
+ if ci. can_derive_debug ( ) && ctx. options . derive_debug {
829
+ derives. push ( "Debug" ) ;
830
+ }
858
831
859
832
if has_destructor {
860
833
for attr in ctx. options . dtor_attrs . iter ( ) {
861
834
let attr = ctx. ext_cx . ident_of ( attr) ;
862
835
attrs. push ( quote_attr ! ( & ctx. ext_cx, #[ $attr] ) ) ;
863
836
}
864
837
} else {
865
- // TODO: make can_derive_debug more reliable in presence of opaque types and all that stuff
866
- let can_derive_debug = ci. members . iter ( )
867
- . all ( |member| match * member {
868
- CompMember :: Field ( ref f) |
869
- CompMember :: CompField ( _, ref f) => f. ty . can_derive_debug ( ) ,
870
- _ => true
871
- } ) ;
872
-
873
- if can_derive_debug && ctx. options . derive_debug {
874
- derives. push ( "Debug" ) ;
875
- }
876
838
derives. push ( "Copy" ) ;
877
839
878
840
// TODO: make mk_clone_impl work for template arguments,
@@ -906,8 +868,7 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo) -> Vec<P<ast::Item>
906
868
let mut unnamed: u32 = 0 ;
907
869
let mut bitfields: u32 = 0 ;
908
870
909
- if ci. hide ||
910
- ci. has_non_type_template_params ||
871
+ if ci. has_non_type_template_params ||
911
872
template_args. iter ( ) . any ( |f| f == & TVoid ) {
912
873
return vec ! ( ) ;
913
874
}
@@ -978,7 +939,7 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo) -> Vec<P<ast::Item>
978
939
let vf_name = format ! ( "_vftable_{}" , name) ;
979
940
let item = P ( ast:: Item {
980
941
ident : ctx. ext_cx . ident_of ( & vf_name) ,
981
- attrs : vec ! ( mk_repr_attr( ctx, layout) ) ,
942
+ attrs : vec ! ( mk_repr_attr( ctx, & layout) ) ,
982
943
id : ast:: DUMMY_NODE_ID ,
983
944
node : ast:: ItemKind :: Struct (
984
945
ast:: VariantData :: Struct ( vffields, ast:: DUMMY_NODE_ID ) ,
@@ -1060,7 +1021,7 @@ fn cstruct_to_rs(ctx: &mut GenCtx, name: &str, ci: CompInfo) -> Vec<P<ast::Item>
1060
1021
drop ( f. ty ) ; // to ensure it's not used unintentionally
1061
1022
1062
1023
let is_translatable = cty_is_translatable ( & f_ty) ;
1063
- if !is_translatable || type_opaque ( ctx , & f_ty) {
1024
+ if !is_translatable || f_ty. is_opaque ( ) {
1064
1025
// Be conservative here and assume it might have a
1065
1026
// destructor or some other serious constraint.
1066
1027
has_destructor = true ;
@@ -1814,7 +1775,7 @@ fn mk_link_name_attr(ctx: &mut GenCtx, name: String) -> ast::Attribute {
1814
1775
respan ( ctx. span , attr)
1815
1776
}
1816
1777
1817
- fn mk_repr_attr ( ctx : & GenCtx , layout : Layout ) -> ast:: Attribute {
1778
+ fn mk_repr_attr ( ctx : & GenCtx , layout : & Layout ) -> ast:: Attribute {
1818
1779
let mut values = vec ! ( P ( respan( ctx. span, ast:: MetaItemKind :: Word ( InternedString :: new( "C" ) ) ) ) ) ;
1819
1780
if layout. packed {
1820
1781
values. push ( P ( respan ( ctx. span , ast:: MetaItemKind :: Word ( InternedString :: new ( "packed" ) ) ) ) ) ;
@@ -2300,7 +2261,6 @@ fn mk_test_fn(ctx: &GenCtx, name: &str, layout: &Layout) -> P<ast::Item> {
2300
2261
}
2301
2262
2302
2263
fn mk_opaque_struct ( ctx : & GenCtx , name : & str , layout : & Layout ) -> P < ast:: Item > {
2303
- // XXX prevent this spurious clone
2304
2264
let blob_field = mk_blob_field ( ctx, "_bindgen_opaque_blob" , layout) ;
2305
2265
let variant_data = if layout. size == 0 {
2306
2266
ast:: VariantData :: Unit ( ast:: DUMMY_NODE_ID )
@@ -2322,7 +2282,7 @@ fn mk_opaque_struct(ctx: &GenCtx, name: &str, layout: &Layout) -> P<ast::Item> {
2322
2282
2323
2283
P ( ast:: Item {
2324
2284
ident : ctx. ext_cx . ident_of ( & name) ,
2325
- attrs : vec ! [ mk_repr_attr( ctx, layout. clone ( ) ) ] ,
2285
+ attrs : vec ! [ mk_repr_attr( ctx, layout) ] ,
2326
2286
id : ast:: DUMMY_NODE_ID ,
2327
2287
node : def,
2328
2288
vis : ast:: Visibility :: Public ,
0 commit comments