@@ -23,31 +23,21 @@ use syntax::codemap::{Span, respan};
23
23
use syntax:: ptr:: P ;
24
24
use aster;
25
25
26
+ // FIXME: This is shame, but we need to do this here since quasi eats the
27
+ // includes in the "helpers" mod otherwise.
28
+ mod helpers {
29
+ include ! ( "helpers.rs" ) ;
30
+ }
31
+
32
+ use self :: helpers:: { attributes, ArrayTyBuilder , BlobTyBuilder } ;
33
+
26
34
fn root_import ( ctx : & BindgenContext ) -> P < ast:: Item > {
27
35
assert ! ( ctx. options( ) . enable_cxx_namespaces, "Somebody messed it up" ) ;
28
36
let root = ctx. root_module ( ) . canonical_name ( ctx) ;
29
37
let root_ident = ctx. rust_ident ( & root) ;
30
38
quote_item ! ( ctx. ext_cx( ) , use $root_ident; ) . unwrap ( )
31
39
}
32
40
33
- macro_rules! repr {
34
- ( $which: expr) => { {
35
- aster:: AstBuilder :: new( ) . attr( ) . list( "repr" ) . words( & [ $which] ) . build( )
36
- } }
37
- }
38
-
39
- macro_rules! doc {
40
- ( $comment: expr) => { {
41
- aster:: AstBuilder :: new( ) . attr( ) . doc( $comment)
42
- } }
43
- }
44
-
45
- macro_rules! link_name {
46
- ( $name: expr) => { {
47
- aster:: AstBuilder :: new( ) . attr( ) . name_value( "link_name" ) . str ( $name)
48
- } }
49
- }
50
-
51
41
struct CodegenResult {
52
42
items : Vec < P < ast:: Item > > ,
53
43
saw_union : bool ,
@@ -166,83 +156,6 @@ impl ForeignModBuilder {
166
156
}
167
157
}
168
158
169
- struct ArrayTyBuilder {
170
- len : usize ,
171
- }
172
-
173
- // TODO: Aster seem to lack a array builder?
174
- impl ArrayTyBuilder {
175
- fn new ( ) -> Self {
176
- ArrayTyBuilder {
177
- len : 0 ,
178
- }
179
- }
180
-
181
- fn with_len ( mut self , len : usize ) -> Self {
182
- self . len = len;
183
- self
184
- }
185
-
186
- fn build ( self , ty : P < ast:: Ty > ) -> P < ast:: Ty > {
187
- use syntax:: codemap:: DUMMY_SP ;
188
- let size =
189
- ast:: LitKind :: Int ( self . len as u64 ,
190
- ast:: LitIntType :: Unsigned ( ast:: UintTy :: Us ) ) ;
191
- let size = ast:: ExprKind :: Lit ( P ( respan ( DUMMY_SP , size) ) ) ;
192
- let array_kind = ast:: TyKind :: FixedLengthVec ( ty,
193
- P ( ast:: Expr {
194
- id : ast:: DUMMY_NODE_ID ,
195
- node : size,
196
- span : DUMMY_SP ,
197
- attrs : ast:: ThinVec :: new ( ) ,
198
- } )
199
- ) ;
200
-
201
- P ( ast:: Ty {
202
- id : ast:: DUMMY_NODE_ID ,
203
- node : array_kind,
204
- span : DUMMY_SP ,
205
- } )
206
- }
207
- }
208
-
209
- /// Generates a proper type for a field or type with a given `Layout`, that is,
210
- /// a type with the correct size and alignment restrictions.
211
- struct BlobTyBuilder {
212
- layout : Layout ,
213
- }
214
-
215
- impl BlobTyBuilder {
216
- fn new ( layout : Layout ) -> Self {
217
- BlobTyBuilder {
218
- layout : layout,
219
- }
220
- }
221
-
222
- fn build ( self ) -> P < ast:: Ty > {
223
- use std:: cmp;
224
-
225
- let ty_name = match self . layout . align {
226
- 8 => "u64" ,
227
- 4 => "u32" ,
228
- 2 => "u16" ,
229
- 1 | _ => "u8" ,
230
- } ;
231
- let data_len = if ty_name == "u8" {
232
- self . layout . size
233
- } else {
234
- self . layout . size / cmp:: max ( self . layout . align , 1 )
235
- } ;
236
-
237
- let inner_ty = aster:: AstBuilder :: new ( ) . ty ( ) . path ( ) . id ( ty_name) . build ( ) ;
238
- if data_len == 1 {
239
- inner_ty
240
- } else {
241
- ArrayTyBuilder :: new ( ) . with_len ( data_len) . build ( inner_ty)
242
- }
243
- }
244
- }
245
-
246
159
/// A trait to convert a rust type into a pointer, optionally const, to the same
247
160
/// type.
248
161
///
@@ -366,9 +279,9 @@ impl CodeGenerator for Var {
366
279
} else {
367
280
let mut attrs = vec ! [ ] ;
368
281
if let Some ( mangled) = self . mangled_name ( ) {
369
- attrs. push ( link_name ! ( mangled) ) ;
282
+ attrs. push ( attributes :: link_name ( mangled) ) ;
370
283
} else if name != self . name ( ) {
371
- attrs. push ( link_name ! ( self . name( ) ) ) ;
284
+ attrs. push ( attributes :: link_name ( self . name ( ) ) ) ;
372
285
}
373
286
374
287
let item = ast:: ForeignItem {
@@ -501,7 +414,7 @@ impl<'a> CodeGenerator for Vtable<'a> {
501
414
// For now, generate an empty struct, later we should generate function
502
415
// pointers and whatnot.
503
416
let vtable = aster:: AstBuilder :: new ( ) . item ( ) . pub_ ( )
504
- . with_attr ( repr ! ( "C" ) )
417
+ . with_attr ( attributes :: repr ( "C" ) )
505
418
. struct_ ( self . canonical_name ( ctx) )
506
419
. build ( ) ;
507
420
result. push ( vtable) ;
@@ -628,16 +541,12 @@ impl CodeGenerator for CompInfo {
628
541
let mut attributes = vec ! [ ] ;
629
542
let mut needs_clone_impl = false ;
630
543
if let Some ( comment) = item. comment ( ) {
631
- attributes. push ( doc ! ( comment) ) ;
544
+ attributes. push ( attributes :: doc ( comment) ) ;
632
545
}
633
546
if self . packed ( ) {
634
- // TODO: reuse the repr! macro?
635
- let c_packed =
636
- aster:: AstBuilder :: new ( ) . attr ( ) . list ( "repr" )
637
- . words ( & [ "C" , "packed" ] ) . build ( ) ;
638
- attributes. push ( c_packed) ;
547
+ attributes. push ( attributes:: repr_list ( & [ "C" , "packed" ] ) ) ;
639
548
} else {
640
- attributes. push ( repr ! ( "C" ) ) ;
549
+ attributes. push ( attributes :: repr ( "C" ) ) ;
641
550
}
642
551
643
552
let mut derives = vec ! [ ] ;
@@ -662,9 +571,7 @@ impl CodeGenerator for CompInfo {
662
571
}
663
572
664
573
if !derives. is_empty ( ) {
665
- let derives = aster:: AstBuilder :: new ( ) . attr ( ) . list ( "derive" )
666
- . words ( & derives) . build ( ) ;
667
- attributes. push ( derives)
574
+ attributes. push ( attributes:: derives ( & derives) )
668
575
}
669
576
670
577
let mut template_args_used = vec ! [ false ; applicable_template_args. len( ) ] ;
@@ -809,7 +716,7 @@ impl CodeGenerator for CompInfo {
809
716
810
717
let mut attrs = vec ! [ ] ;
811
718
if let Some ( comment) = field. comment ( ) {
812
- attrs. push ( doc ! ( comment) ) ;
719
+ attrs. push ( attributes :: doc ( comment) ) ;
813
720
}
814
721
let field_name = match field. name ( ) {
815
722
Some ( name) => ctx. rust_mangle ( name) . into_owned ( ) ,
@@ -1198,7 +1105,7 @@ impl MethodCodegen for Method {
1198
1105
} ;
1199
1106
1200
1107
let mut attrs = vec ! [ ] ;
1201
- attrs. push ( aster :: AstBuilder :: new ( ) . attr ( ) . word ( " inline" ) ) ;
1108
+ attrs. push ( attributes :: inline ( ) ) ;
1202
1109
1203
1110
let item = ast:: ImplItem {
1204
1111
id : ast:: DUMMY_NODE_ID ,
@@ -1260,17 +1167,16 @@ impl CodeGenerator for Enum {
1260
1167
// FIXME: Rust forbids repr with empty enums. Remove this condition when
1261
1168
// this is allowed.
1262
1169
if !self . variants ( ) . is_empty ( ) {
1263
- builder = builder. with_attr ( repr ! ( repr_name) ) ;
1170
+ builder = builder. with_attr ( attributes :: repr ( repr_name) ) ;
1264
1171
}
1265
1172
1266
1173
if let Some ( comment) = item. comment ( ) {
1267
- builder = builder. with_attr ( doc ! ( comment) ) ;
1174
+ builder = builder. with_attr ( attributes :: doc ( comment) ) ;
1268
1175
}
1269
1176
1270
1177
let derives =
1271
- aster:: AstBuilder :: new ( ) . attr ( ) . list ( "derive" )
1272
- . words ( & [ "Debug" , "Copy" , "Clone" , "PartialEq" , "Eq" , "Hash" ] )
1273
- . build ( ) ;
1178
+ attributes:: derives ( & [ "Debug" , "Copy" , "Clone" , "PartialEq" , "Eq" , "Hash" ] ) ;
1179
+
1274
1180
builder = builder. with_attr ( derives) ;
1275
1181
1276
1182
let mut builder = builder. enum_ ( & name) ;
@@ -1586,13 +1492,13 @@ impl CodeGenerator for Function {
1586
1492
let mut attributes = vec ! [ ] ;
1587
1493
1588
1494
if let Some ( comment) = item. comment ( ) {
1589
- attributes. push ( doc ! ( comment) ) ;
1495
+ attributes. push ( attributes :: doc ( comment) ) ;
1590
1496
}
1591
1497
1592
1498
if let Some ( mangled) = self . mangled_name ( ) {
1593
- attributes. push ( link_name ! ( mangled) ) ;
1499
+ attributes. push ( attributes :: link_name ( mangled) ) ;
1594
1500
} else if name != canonical_name {
1595
- attributes. push ( link_name ! ( name) ) ;
1501
+ attributes. push ( attributes :: link_name ( name) ) ;
1596
1502
}
1597
1503
1598
1504
let foreign_item_kind =
0 commit comments