@@ -2198,61 +2198,8 @@ impl ToRustTy for FunctionSig {
2198
2198
2199
2199
fn to_rust_ty ( & self , ctx : & BindgenContext , _item : & Item ) -> P < ast:: Ty > {
2200
2200
// TODO: we might want to consider ignoring the reference return value.
2201
- let return_item = ctx. resolve_item ( self . return_type ( ) ) ;
2202
- let ret =
2203
- if let TypeKind :: Void = * return_item. kind ( ) . expect_type ( ) . kind ( ) {
2204
- ast:: FunctionRetTy :: Default ( ctx. span ( ) )
2205
- } else {
2206
- ast:: FunctionRetTy :: Ty ( return_item. to_rust_ty ( ctx) )
2207
- } ;
2208
-
2209
- let mut unnamed_arguments = 0 ;
2210
- let arguments = self . argument_types ( ) . iter ( ) . map ( |& ( ref name, ty) | {
2211
- let arg_item = ctx. resolve_item ( ty) ;
2212
- let arg_ty = arg_item. kind ( ) . expect_type ( ) ;
2213
-
2214
- // From the C90 standard[1]:
2215
- //
2216
- // A declaration of a parameter as "array of type" shall be
2217
- // adjusted to "qualified pointer to type", where the type
2218
- // qualifiers (if any) are those specified within the [ and ] of
2219
- // the array type derivation.
2220
- //
2221
- // [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
2222
- let arg_ty = match * arg_ty. canonical_type ( ctx) . kind ( ) {
2223
- TypeKind :: Array ( t, _) => {
2224
- t. to_rust_ty ( ctx) . to_ptr ( arg_ty. is_const ( ) , ctx. span ( ) )
2225
- } ,
2226
- TypeKind :: Pointer ( inner) => {
2227
- let inner = ctx. resolve_item ( inner) ;
2228
- let inner_ty = inner. expect_type ( ) ;
2229
- if let TypeKind :: ObjCInterface ( _) = * inner_ty. canonical_type ( ctx) . kind ( ) {
2230
- quote_ty ! ( ctx. ext_cx( ) , id)
2231
- } else {
2232
- arg_item. to_rust_ty ( ctx)
2233
- }
2234
- } ,
2235
- _ => {
2236
- arg_item. to_rust_ty ( ctx)
2237
- }
2238
- } ;
2239
-
2240
- let arg_name = match * name {
2241
- Some ( ref name) => ctx. rust_mangle ( name) . into_owned ( ) ,
2242
- None => {
2243
- unnamed_arguments += 1 ;
2244
- format ! ( "arg{}" , unnamed_arguments)
2245
- }
2246
- } ;
2247
-
2248
- assert ! ( !arg_name. is_empty( ) ) ;
2249
-
2250
- ast:: Arg {
2251
- ty : arg_ty,
2252
- pat : aster:: AstBuilder :: new ( ) . pat ( ) . id ( arg_name) ,
2253
- id : ast:: DUMMY_NODE_ID ,
2254
- }
2255
- } ) . collect :: < Vec < _ > > ( ) ;
2201
+ let ret = utils:: fnsig_return_ty ( ctx, & self ) ;
2202
+ let arguments = utils:: fnsig_arguments ( ctx, & self ) ;
2256
2203
2257
2204
let decl = P ( ast:: FnDecl {
2258
2205
inputs : arguments,
@@ -2262,7 +2209,7 @@ impl ToRustTy for FunctionSig {
2262
2209
2263
2210
let fnty = ast:: TyKind :: BareFn ( P ( ast:: BareFnTy {
2264
2211
unsafety : ast:: Unsafety :: Unsafe ,
2265
- abi : self . abi ( ) ,
2212
+ abi : self . abi ( ) . expect ( "Invalid abi for function!" ) ,
2266
2213
lifetimes : vec ! [ ] ,
2267
2214
decl : decl,
2268
2215
} ) ) ;
@@ -2342,7 +2289,8 @@ impl CodeGenerator for Function {
2342
2289
vis : ast:: Visibility :: Public ,
2343
2290
} ;
2344
2291
2345
- let item = ForeignModBuilder :: new ( signature. abi ( ) )
2292
+ let item = ForeignModBuilder :: new ( signature. abi ( )
2293
+ . expect ( "Invalid abi for function!" ) )
2346
2294
. with_foreign_item ( foreign_item)
2347
2295
. build ( ctx) ;
2348
2296
@@ -2361,9 +2309,36 @@ impl CodeGenerator for ObjCInterface {
2361
2309
let mut trait_items = vec ! [ ] ;
2362
2310
2363
2311
for method in self . methods ( ) {
2364
- let method_name = ctx. rust_ident ( method. name ( ) ) ;
2312
+ let signature = method. signature ( ) ;
2313
+ let fn_args = utils:: fnsig_arguments ( ctx, signature) ;
2314
+ let fn_ret = utils:: fnsig_return_ty ( ctx, signature) ;
2315
+ let sig = aster:: AstBuilder :: new ( )
2316
+ . method_sig ( )
2317
+ . unsafe_ ( )
2318
+ . fn_decl ( )
2319
+ . self_ ( )
2320
+ . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2321
+ . with_args ( fn_args. clone ( ) )
2322
+ . build ( fn_ret) ;
2323
+
2324
+ // Collect the actual used argument names
2325
+ let arg_names: Vec < _ > = fn_args. iter ( )
2326
+ . map ( |ref arg| {
2327
+ match arg. pat . node {
2328
+ ast:: PatKind :: Ident ( _, ref spanning, _) => {
2329
+ spanning. node . name . as_str ( ) . to_string ( )
2330
+ }
2331
+ _ => {
2332
+ panic ! ( "odd argument!" ) ;
2333
+ }
2334
+ }
2335
+ } )
2336
+ . collect ( ) ;
2365
2337
2366
- let body = quote_stmt ! ( ctx. ext_cx( ) , msg_send![ self , $method_name] )
2338
+ let methods_and_args =
2339
+ ctx. rust_ident ( & method. format_method_call ( & arg_names) ) ;
2340
+ let body =
2341
+ quote_stmt ! ( ctx. ext_cx( ) , msg_send![ self , $methods_and_args] )
2367
2342
. unwrap ( ) ;
2368
2343
let block = ast:: Block {
2369
2344
stmts : vec ! [ body] ,
@@ -2372,13 +2347,6 @@ impl CodeGenerator for ObjCInterface {
2372
2347
span : ctx. span ( ) ,
2373
2348
} ;
2374
2349
2375
- let sig = aster:: AstBuilder :: new ( )
2376
- . method_sig ( )
2377
- . unsafe_ ( )
2378
- . fn_decl ( )
2379
- . self_ ( )
2380
- . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2381
- . build ( ast:: FunctionRetTy :: Default ( ctx. span ( ) ) ) ;
2382
2350
let attrs = vec ! [ ] ;
2383
2351
2384
2352
let impl_item = ast:: ImplItem {
@@ -2743,4 +2711,69 @@ mod utils {
2743
2711
_ => panic ! ( "How did this happen exactly?" ) ,
2744
2712
}
2745
2713
}
2714
+
2715
+ pub fn fnsig_return_ty ( ctx : & BindgenContext ,
2716
+ sig : & super :: FunctionSig )
2717
+ -> ast:: FunctionRetTy {
2718
+ let return_item = ctx. resolve_item ( sig. return_type ( ) ) ;
2719
+ if let TypeKind :: Void = * return_item. kind ( ) . expect_type ( ) . kind ( ) {
2720
+ ast:: FunctionRetTy :: Default ( ctx. span ( ) )
2721
+ } else {
2722
+ ast:: FunctionRetTy :: Ty ( return_item. to_rust_ty ( ctx) )
2723
+ }
2724
+ }
2725
+
2726
+ pub fn fnsig_arguments ( ctx : & BindgenContext ,
2727
+ sig : & super :: FunctionSig )
2728
+ -> Vec < ast:: Arg > {
2729
+ use super :: ToPtr ;
2730
+ let mut unnamed_arguments = 0 ;
2731
+ sig. argument_types ( ) . iter ( ) . map ( |& ( ref name, ty) | {
2732
+ let arg_item = ctx. resolve_item ( ty) ;
2733
+ let arg_ty = arg_item. kind ( ) . expect_type ( ) ;
2734
+
2735
+ // From the C90 standard[1]:
2736
+ //
2737
+ // A declaration of a parameter as "array of type" shall be
2738
+ // adjusted to "qualified pointer to type", where the type
2739
+ // qualifiers (if any) are those specified within the [ and ] of
2740
+ // the array type derivation.
2741
+ //
2742
+ // [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
2743
+ let arg_ty = match * arg_ty. canonical_type ( ctx) . kind ( ) {
2744
+ TypeKind :: Array ( t, _) => {
2745
+ t. to_rust_ty ( ctx) . to_ptr ( arg_ty. is_const ( ) , ctx. span ( ) )
2746
+ } ,
2747
+ TypeKind :: Pointer ( inner) => {
2748
+ let inner = ctx. resolve_item ( inner) ;
2749
+ let inner_ty = inner. expect_type ( ) ;
2750
+ if let TypeKind :: ObjCInterface ( _) = * inner_ty. canonical_type ( ctx) . kind ( ) {
2751
+ quote_ty ! ( ctx. ext_cx( ) , id)
2752
+ } else {
2753
+ arg_item. to_rust_ty ( ctx)
2754
+ }
2755
+ } ,
2756
+ _ => {
2757
+ arg_item. to_rust_ty ( ctx)
2758
+ }
2759
+ } ;
2760
+
2761
+ let arg_name = match * name {
2762
+ Some ( ref name) => ctx. rust_mangle ( name) . into_owned ( ) ,
2763
+ None => {
2764
+ unnamed_arguments += 1 ;
2765
+ format ! ( "arg{}" , unnamed_arguments)
2766
+ }
2767
+ } ;
2768
+
2769
+ assert ! ( !arg_name. is_empty( ) ) ;
2770
+
2771
+ ast:: Arg {
2772
+ ty : arg_ty,
2773
+ pat : aster:: AstBuilder :: new ( ) . pat ( ) . id ( arg_name) ,
2774
+ id : ast:: DUMMY_NODE_ID ,
2775
+ }
2776
+ } ) . collect :: < Vec < _ > > ( )
2777
+ }
2778
+
2746
2779
}
0 commit comments