@@ -2153,61 +2153,8 @@ impl ToRustTy for FunctionSig {
2153
2153
2154
2154
fn to_rust_ty ( & self , ctx : & BindgenContext , _item : & Item ) -> P < ast:: Ty > {
2155
2155
// TODO: we might want to consider ignoring the reference return value.
2156
- let return_item = ctx. resolve_item ( self . return_type ( ) ) ;
2157
- let ret =
2158
- if let TypeKind :: Void = * return_item. kind ( ) . expect_type ( ) . kind ( ) {
2159
- ast:: FunctionRetTy :: Default ( ctx. span ( ) )
2160
- } else {
2161
- ast:: FunctionRetTy :: Ty ( return_item. to_rust_ty ( ctx) )
2162
- } ;
2163
-
2164
- let mut unnamed_arguments = 0 ;
2165
- let arguments = self . argument_types ( ) . iter ( ) . map ( |& ( ref name, ty) | {
2166
- let arg_item = ctx. resolve_item ( ty) ;
2167
- let arg_ty = arg_item. kind ( ) . expect_type ( ) ;
2168
-
2169
- // From the C90 standard[1]:
2170
- //
2171
- // A declaration of a parameter as "array of type" shall be
2172
- // adjusted to "qualified pointer to type", where the type
2173
- // qualifiers (if any) are those specified within the [ and ] of
2174
- // the array type derivation.
2175
- //
2176
- // [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
2177
- let arg_ty = match * arg_ty. canonical_type ( ctx) . kind ( ) {
2178
- TypeKind :: Array ( t, _) => {
2179
- t. to_rust_ty ( ctx) . to_ptr ( arg_ty. is_const ( ) , ctx. span ( ) )
2180
- } ,
2181
- TypeKind :: Pointer ( inner) => {
2182
- let inner = ctx. resolve_item ( inner) ;
2183
- let inner_ty = inner. expect_type ( ) ;
2184
- if let TypeKind :: ObjCInterface ( _) = * inner_ty. canonical_type ( ctx) . kind ( ) {
2185
- quote_ty ! ( ctx. ext_cx( ) , id)
2186
- } else {
2187
- arg_item. to_rust_ty ( ctx)
2188
- }
2189
- } ,
2190
- _ => {
2191
- arg_item. to_rust_ty ( ctx)
2192
- }
2193
- } ;
2194
-
2195
- let arg_name = match * name {
2196
- Some ( ref name) => ctx. rust_mangle ( name) . into_owned ( ) ,
2197
- None => {
2198
- unnamed_arguments += 1 ;
2199
- format ! ( "arg{}" , unnamed_arguments)
2200
- }
2201
- } ;
2202
-
2203
- assert ! ( !arg_name. is_empty( ) ) ;
2204
-
2205
- ast:: Arg {
2206
- ty : arg_ty,
2207
- pat : aster:: AstBuilder :: new ( ) . pat ( ) . id ( arg_name) ,
2208
- id : ast:: DUMMY_NODE_ID ,
2209
- }
2210
- } ) . collect :: < Vec < _ > > ( ) ;
2156
+ let ret = utils:: fnsig_return_ty ( ctx, & self ) ;
2157
+ let arguments = utils:: fnsig_arguments ( ctx, & self ) ;
2211
2158
2212
2159
let decl = P ( ast:: FnDecl {
2213
2160
inputs : arguments,
@@ -2217,7 +2164,7 @@ impl ToRustTy for FunctionSig {
2217
2164
2218
2165
let fnty = ast:: TyKind :: BareFn ( P ( ast:: BareFnTy {
2219
2166
unsafety : ast:: Unsafety :: Unsafe ,
2220
- abi : self . abi ( ) ,
2167
+ abi : self . abi ( ) . expect ( "Invalid abi for function!" ) ,
2221
2168
lifetimes : vec ! [ ] ,
2222
2169
decl : decl,
2223
2170
} ) ) ;
@@ -2297,7 +2244,8 @@ impl CodeGenerator for Function {
2297
2244
vis : ast:: Visibility :: Public ,
2298
2245
} ;
2299
2246
2300
- let item = ForeignModBuilder :: new ( signature. abi ( ) )
2247
+ let item = ForeignModBuilder :: new ( signature. abi ( )
2248
+ . expect ( "Invalid abi for function!" ) )
2301
2249
. with_foreign_item ( foreign_item)
2302
2250
. build ( ctx) ;
2303
2251
@@ -2316,9 +2264,36 @@ impl CodeGenerator for ObjCInterface {
2316
2264
let mut trait_items = vec ! [ ] ;
2317
2265
2318
2266
for method in self . methods ( ) {
2319
- let method_name = ctx. rust_ident ( method. name ( ) ) ;
2267
+ let signature = method. signature ( ) ;
2268
+ let fn_args = utils:: fnsig_arguments ( ctx, signature) ;
2269
+ let fn_ret = utils:: fnsig_return_ty ( ctx, signature) ;
2270
+ let sig = aster:: AstBuilder :: new ( )
2271
+ . method_sig ( )
2272
+ . unsafe_ ( )
2273
+ . fn_decl ( )
2274
+ . self_ ( )
2275
+ . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2276
+ . with_args ( fn_args. clone ( ) )
2277
+ . build ( fn_ret) ;
2278
+
2279
+ // Collect the actual used argument names
2280
+ let arg_names: Vec < _ > = fn_args. iter ( )
2281
+ . map ( |ref arg| {
2282
+ match arg. pat . node {
2283
+ ast:: PatKind :: Ident ( _, ref spanning, _) => {
2284
+ spanning. node . name . as_str ( ) . to_string ( )
2285
+ }
2286
+ _ => {
2287
+ panic ! ( "odd argument!" ) ;
2288
+ }
2289
+ }
2290
+ } )
2291
+ . collect ( ) ;
2320
2292
2321
- let body = quote_stmt ! ( ctx. ext_cx( ) , msg_send![ self , $method_name] )
2293
+ let methods_and_args =
2294
+ ctx. rust_ident ( & method. format_method_call ( & arg_names) ) ;
2295
+ let body =
2296
+ quote_stmt ! ( ctx. ext_cx( ) , msg_send![ self , $methods_and_args] )
2322
2297
. unwrap ( ) ;
2323
2298
let block = ast:: Block {
2324
2299
stmts : vec ! [ body] ,
@@ -2327,13 +2302,6 @@ impl CodeGenerator for ObjCInterface {
2327
2302
span : ctx. span ( ) ,
2328
2303
} ;
2329
2304
2330
- let sig = aster:: AstBuilder :: new ( )
2331
- . method_sig ( )
2332
- . unsafe_ ( )
2333
- . fn_decl ( )
2334
- . self_ ( )
2335
- . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2336
- . build ( ast:: FunctionRetTy :: Default ( ctx. span ( ) ) ) ;
2337
2305
let attrs = vec ! [ ] ;
2338
2306
2339
2307
let impl_item = ast:: ImplItem {
@@ -2697,4 +2665,69 @@ mod utils {
2697
2665
_ => panic ! ( "How did this happen exactly?" ) ,
2698
2666
}
2699
2667
}
2668
+
2669
+ pub fn fnsig_return_ty ( ctx : & BindgenContext ,
2670
+ sig : & super :: FunctionSig )
2671
+ -> ast:: FunctionRetTy {
2672
+ let return_item = ctx. resolve_item ( sig. return_type ( ) ) ;
2673
+ if let TypeKind :: Void = * return_item. kind ( ) . expect_type ( ) . kind ( ) {
2674
+ ast:: FunctionRetTy :: Default ( ctx. span ( ) )
2675
+ } else {
2676
+ ast:: FunctionRetTy :: Ty ( return_item. to_rust_ty ( ctx) )
2677
+ }
2678
+ }
2679
+
2680
+ pub fn fnsig_arguments ( ctx : & BindgenContext ,
2681
+ sig : & super :: FunctionSig )
2682
+ -> Vec < ast:: Arg > {
2683
+ use super :: ToPtr ;
2684
+ let mut unnamed_arguments = 0 ;
2685
+ sig. argument_types ( ) . iter ( ) . map ( |& ( ref name, ty) | {
2686
+ let arg_item = ctx. resolve_item ( ty) ;
2687
+ let arg_ty = arg_item. kind ( ) . expect_type ( ) ;
2688
+
2689
+ // From the C90 standard[1]:
2690
+ //
2691
+ // A declaration of a parameter as "array of type" shall be
2692
+ // adjusted to "qualified pointer to type", where the type
2693
+ // qualifiers (if any) are those specified within the [ and ] of
2694
+ // the array type derivation.
2695
+ //
2696
+ // [1]: http://c0x.coding-guidelines.com/6.7.5.3.html
2697
+ let arg_ty = match * arg_ty. canonical_type ( ctx) . kind ( ) {
2698
+ TypeKind :: Array ( t, _) => {
2699
+ t. to_rust_ty ( ctx) . to_ptr ( arg_ty. is_const ( ) , ctx. span ( ) )
2700
+ } ,
2701
+ TypeKind :: Pointer ( inner) => {
2702
+ let inner = ctx. resolve_item ( inner) ;
2703
+ let inner_ty = inner. expect_type ( ) ;
2704
+ if let TypeKind :: ObjCInterface ( _) = * inner_ty. canonical_type ( ctx) . kind ( ) {
2705
+ quote_ty ! ( ctx. ext_cx( ) , id)
2706
+ } else {
2707
+ arg_item. to_rust_ty ( ctx)
2708
+ }
2709
+ } ,
2710
+ _ => {
2711
+ arg_item. to_rust_ty ( ctx)
2712
+ }
2713
+ } ;
2714
+
2715
+ let arg_name = match * name {
2716
+ Some ( ref name) => ctx. rust_mangle ( name) . into_owned ( ) ,
2717
+ None => {
2718
+ unnamed_arguments += 1 ;
2719
+ format ! ( "arg{}" , unnamed_arguments)
2720
+ }
2721
+ } ;
2722
+
2723
+ assert ! ( !arg_name. is_empty( ) ) ;
2724
+
2725
+ ast:: Arg {
2726
+ ty : arg_ty,
2727
+ pat : aster:: AstBuilder :: new ( ) . pat ( ) . id ( arg_name) ,
2728
+ id : ast:: DUMMY_NODE_ID ,
2729
+ }
2730
+ } ) . collect :: < Vec < _ > > ( )
2731
+ }
2732
+
2700
2733
}
0 commit comments