1
1
mod helpers;
2
2
3
+
3
4
use aster;
5
+
4
6
use ir:: annotations:: FieldAccessorKind ;
5
7
use ir:: comp:: { CompInfo , CompKind , Field , Method } ;
6
-
7
8
use ir:: context:: BindgenContext ;
8
9
use ir:: enum_ty:: Enum ;
9
10
use ir:: function:: { Function , FunctionSig } ;
@@ -16,13 +17,12 @@ use ir::ty::{Type, TypeKind};
16
17
use ir:: type_collector:: { ItemSet , TypeCollector } ;
17
18
use ir:: var:: Var ;
18
19
use self :: helpers:: { BlobTyBuilder , attributes} ;
20
+
19
21
use std:: borrow:: Cow ;
20
22
use std:: collections:: HashSet ;
21
23
use std:: collections:: hash_map:: { Entry , HashMap } ;
22
24
use std:: mem;
23
-
24
25
use std:: ops;
25
-
26
26
use syntax:: abi:: Abi ;
27
27
use syntax:: ast;
28
28
use syntax:: codemap:: { Span , respan} ;
@@ -40,8 +40,7 @@ struct CodegenResult {
40
40
saw_union : bool ,
41
41
items_seen : HashSet < ItemId > ,
42
42
/// The set of generated function/var names, needed because in C/C++ is
43
- /// legal to
44
- /// do something like:
43
+ /// legal to do something like:
45
44
///
46
45
/// ```c++
47
46
/// extern "C" {
@@ -400,7 +399,8 @@ impl CodeGenerator for Type {
400
399
if template_arg. is_named ( ) {
401
400
let name = template_arg. name ( ) . unwrap ( ) ;
402
401
if name. contains ( "typename " ) {
403
- error ! ( "Item contained `typename`'d template param: {:?}" , item) ;
402
+ error ! ( "Item contained `typename`'d template \
403
+ parameter: {:?}", item) ;
404
404
return ;
405
405
}
406
406
generics =
@@ -541,18 +541,25 @@ impl<'a> Bitfield<'a> {
541
541
pub fn $getter_name( & self ) -> $field_type {
542
542
unsafe {
543
543
:: std:: mem:: transmute(
544
- ( ( self . $field_ident & ( $mask as $bitfield_type) ) >> $offset)
545
- as $int_type)
544
+ (
545
+ ( self . $field_ident &
546
+ ( $mask as $bitfield_type) )
547
+ >> $offset
548
+ ) as $int_type
549
+ )
546
550
}
547
551
}
548
552
549
553
#[ inline]
550
554
pub fn $setter_name( & mut self , val: $field_type) {
551
555
self . $field_ident &= !( $mask as $bitfield_type) ;
552
- self . $field_ident |= ( val as $int_type as $bitfield_type << $offset) & ( $mask as $bitfield_type) ;
556
+ self . $field_ident |=
557
+ ( val as $int_type as $bitfield_type << $offset) &
558
+ ( $mask as $bitfield_type) ;
553
559
}
554
560
}
555
- ) . unwrap ( ) ;
561
+ )
562
+ . unwrap ( ) ;
556
563
557
564
let items = match item. unwrap ( ) . node {
558
565
ast:: ItemKind :: Impl ( _, _, _, _, _, items) => items,
@@ -639,6 +646,7 @@ impl CodeGenerator for CompInfo {
639
646
} ;
640
647
641
648
// Generate the vtable from the method list if appropriate.
649
+ //
642
650
// TODO: I don't know how this could play with virtual methods that are
643
651
// not in the list of methods found by us, we'll see. Also, could the
644
652
// order of the vtable pointers vary?
@@ -677,8 +685,9 @@ impl CodeGenerator for CompInfo {
677
685
continue ;
678
686
}
679
687
680
- for ( i, ty) in applicable_template_args. iter ( ) . enumerate ( ) {
681
- if base_ty. signature_contains_named_type ( ctx, ctx. resolve_type ( * ty) ) {
688
+ for ( i, ty_id) in applicable_template_args. iter ( ) . enumerate ( ) {
689
+ let template_arg_ty = ctx. resolve_type ( * ty_id) ;
690
+ if base_ty. signature_contains_named_type ( ctx, template_arg_ty) {
682
691
template_args_used[ i] = true ;
683
692
}
684
693
}
@@ -758,8 +767,9 @@ impl CodeGenerator for CompInfo {
758
767
continue ;
759
768
}
760
769
761
- for ( i, ty) in applicable_template_args. iter ( ) . enumerate ( ) {
762
- if field_ty. signature_contains_named_type ( ctx, ctx. resolve_type ( * ty) ) {
770
+ for ( i, ty_id) in applicable_template_args. iter ( ) . enumerate ( ) {
771
+ let template_arg = ctx. resolve_type ( * ty_id) ;
772
+ if field_ty. signature_contains_named_type ( ctx, template_arg) {
763
773
template_args_used[ i] = true ;
764
774
}
765
775
}
@@ -841,7 +851,8 @@ impl CodeGenerator for CompInfo {
841
851
}
842
852
843
853
#[ inline]
844
- pub unsafe fn $mutable_getter_name( & mut self ) -> & mut $ty {
854
+ pub unsafe fn $mutable_getter_name( & mut self )
855
+ -> & mut $ty {
845
856
& mut self . $field_name
846
857
}
847
858
}
@@ -931,9 +942,12 @@ impl CodeGenerator for CompInfo {
931
942
if !template_args_used[ i] {
932
943
let name = ctx. resolve_type ( * ty) . name ( ) . unwrap ( ) ;
933
944
let ident = ctx. rust_ident ( name) ;
945
+ let phantom = quote_ty ! ( ctx. ext_cx( ) ,
946
+ :: std:: marker:: PhantomData <$ident>) ;
934
947
let field =
935
- StructFieldBuilder :: named ( format ! ( "_phantom_{}" , i) ) . pub_ ( )
936
- . build_ty ( quote_ty ! ( ctx. ext_cx( ) , :: std:: marker:: PhantomData <$ident>) ) ;
948
+ StructFieldBuilder :: named ( format ! ( "_phantom_{}" , i) )
949
+ . pub_ ( )
950
+ . build_ty ( phantom) ;
937
951
fields. push ( field)
938
952
}
939
953
}
@@ -972,16 +986,18 @@ impl CodeGenerator for CompInfo {
972
986
// affect layout, so we're bad and pray to the gods for avoid sending
973
987
// all the tests to shit when parsing things like max_align_t.
974
988
if self . found_unknown_attr ( ) {
975
- warn ! ( "Type {} has an unkown attribute that may affect layout" , canonical_name) ;
989
+ warn ! ( "Type {} has an unkown attribute that may affect layout" ,
990
+ canonical_name) ;
976
991
}
992
+
977
993
if applicable_template_args. is_empty ( ) && !self . found_unknown_attr ( ) {
978
994
for var in self . inner_vars ( ) {
979
995
ctx. resolve_item ( * var) . codegen ( ctx, result, & ( ) ) ;
980
996
}
981
997
982
998
if let Some ( layout) = layout {
983
- let fn_name =
984
- ctx. rust_ident_raw ( & format ! ( "bindgen_test_layout_{}" , canonical_name ) ) ;
999
+ let fn_name = format ! ( "bindgen_test_layout_{}" , canonical_name ) ;
1000
+ let fn_name = ctx. rust_ident_raw ( & fn_name ) ;
985
1001
let ident = ctx. rust_ident_raw ( & canonical_name) ;
986
1002
let size_of_expr =
987
1003
quote_expr ! ( ctx. ext_cx( ) , :: std:: mem:: size_of:: <$ident>( ) ) ;
@@ -1107,6 +1123,7 @@ impl MethodCodegen for Method {
1107
1123
} ;
1108
1124
1109
1125
assert ! ( !fndecl. inputs. is_empty( ) ) ;
1126
+
1110
1127
// FIXME: use aster here.
1111
1128
fndecl. inputs [ 0 ] = ast:: Arg {
1112
1129
ty : P ( ast:: Ty {
@@ -1123,9 +1140,11 @@ impl MethodCodegen for Method {
1123
1140
} ) ,
1124
1141
pat : P ( ast:: Pat {
1125
1142
id : ast:: DUMMY_NODE_ID ,
1126
- node : ast:: PatKind :: Ident ( ast:: BindingMode :: ByValue ( ast:: Mutability :: Immutable ) ,
1127
- respan ( ctx. span ( ) , ctx. ext_cx ( ) . ident_of ( "self" ) ) ,
1128
- None ) ,
1143
+ node : ast:: PatKind :: Ident (
1144
+ ast:: BindingMode :: ByValue ( ast:: Mutability :: Immutable ) ,
1145
+ respan ( ctx. span ( ) , ctx. ext_cx ( ) . ident_of ( "self" ) ) ,
1146
+ None
1147
+ ) ,
1129
1148
span : ctx. span ( ) ,
1130
1149
} ) ,
1131
1150
id : ast:: DUMMY_NODE_ID ,
@@ -1223,7 +1242,8 @@ impl CodeGenerator for Enum {
1223
1242
}
1224
1243
}
1225
1244
None => {
1226
- warn ! ( "Guessing type of enum! Forward declarations of enums shouldn't be legal!" ) ;
1245
+ warn ! ( "Guessing type of enum! Forward declarations of enums \
1246
+ shouldn't be legal!") ;
1227
1247
IntKind :: Int
1228
1248
}
1229
1249
} ;
@@ -1272,8 +1292,7 @@ impl CodeGenerator for Enum {
1272
1292
// Only to avoid recomputing every time.
1273
1293
enum_canonical_name : & str ,
1274
1294
// May be the same as "variant" if it's because the
1275
- // enum
1276
- // is unnamed and we still haven't seen the value.
1295
+ // enum is unnamed and we still haven't seen the value.
1277
1296
variant_name : & str ,
1278
1297
referenced_name : & str ,
1279
1298
enum_rust_ty : P < ast:: Ty > ,
@@ -1342,9 +1361,11 @@ impl CodeGenerator for Enum {
1342
1361
. canonical_name ( ctx) ) ;
1343
1362
}
1344
1363
1364
+ let parent_name = parent_canonical_name. as_ref ( )
1365
+ . unwrap ( ) ;
1366
+
1345
1367
Cow :: Owned (
1346
- format ! ( "{}_{}" , parent_canonical_name. as_ref( ) . unwrap( ) ,
1347
- variant_name) )
1368
+ format ! ( "{}_{}" , parent_name, variant_name) )
1348
1369
} ;
1349
1370
1350
1371
add_constant ( enum_ty,
@@ -1464,13 +1485,15 @@ impl ToRustTy for Type {
1464
1485
let mut inner_ty = inner. to_rust_ty ( ctx) . unwrap ( ) ;
1465
1486
1466
1487
if let ast:: TyKind :: Path ( _, ref mut path) = inner_ty. node {
1488
+ let template_args = template_args. iter ( )
1489
+ . map ( |arg| arg. to_rust_ty ( ctx) )
1490
+ . collect ( ) ;
1491
+
1467
1492
path. segments . last_mut ( ) . unwrap ( ) . parameters =
1468
1493
ast:: PathParameters :: AngleBracketed (
1469
1494
ast:: AngleBracketedParameterData {
1470
1495
lifetimes : vec ! [ ] ,
1471
- types : P :: from_vec ( template_args. iter ( ) . map ( |arg| {
1472
- arg. to_rust_ty ( ctx)
1473
- } ) . collect ( ) ) ,
1496
+ types : P :: from_vec ( template_args) ,
1474
1497
bindings : P :: from_vec ( vec ! [ ] ) ,
1475
1498
}
1476
1499
) ;
@@ -1498,7 +1521,8 @@ impl ToRustTy for Type {
1498
1521
Some ( layout) => BlobTyBuilder :: new ( layout) . build ( ) ,
1499
1522
None => {
1500
1523
warn ! ( "Couldn't compute layout for a type with non \
1501
- type template params or opaque, expect dragons!") ;
1524
+ type template params or opaque, expect \
1525
+ dragons!") ;
1502
1526
aster:: AstBuilder :: new ( ) . ty ( ) . unit ( )
1503
1527
}
1504
1528
} ;
@@ -1558,11 +1582,14 @@ impl ToRustTy for FunctionSig {
1558
1582
let arg_item = ctx. resolve_item ( ty) ;
1559
1583
let arg_ty = arg_item. kind ( ) . expect_type ( ) ;
1560
1584
1561
- // From the C90 standard (http://c0x.coding-guidelines.com/6.7.5.3.html)
1562
- // 1598 - A declaration of a parameter as “array of type” shall be
1563
- // adjusted to “qualified pointer to type”, where the type qualifiers
1564
- // (if any) are those specified within the [ and ] of the array type
1565
- // derivation.
1585
+ // From the C90 standard[1]:
1586
+ //
1587
+ // A declaration of a parameter as "array of type" shall be
1588
+ // adjusted to "qualified pointer to type", where the type
1589
+ // qualifiers (if any) are those specified within the [ and ] of
1590
+ // the array type derivation.
1591
+ //
1592
+ // [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
1566
1593
let arg_ty = if let TypeKind :: Array ( t, _) = * arg_ty. kind ( ) {
1567
1594
t. to_rust_ty ( ctx) . to_ptr ( arg_ty. is_const ( ) , ctx. span ( ) )
1568
1595
} else {
@@ -1678,8 +1705,7 @@ pub fn codegen(context: &mut BindgenContext) -> Vec<P<ast::Item>> {
1678
1705
context. options ( ) . whitelisted_vars . is_empty ( ) {
1679
1706
for ( _item_id, item) in context. items ( ) {
1680
1707
// Non-toplevel item parents are the responsible one for
1681
- // generating
1682
- // them.
1708
+ // generating them.
1683
1709
if item. is_toplevel ( context) {
1684
1710
item. codegen ( context, & mut result, & ( ) ) ;
1685
1711
}
0 commit comments