@@ -19,7 +19,7 @@ use ir::item::{Item, ItemAncestors, ItemCanonicalName, ItemCanonicalPath,
19
19
use ir:: item_kind:: ItemKind ;
20
20
use ir:: layout:: Layout ;
21
21
use ir:: module:: Module ;
22
- use ir:: objc:: ObjCInterface ;
22
+ use ir:: objc:: { ObjCInterface , ObjCMethod } ;
23
23
use ir:: template:: { AsNamed , TemplateInstantiation } ;
24
24
use ir:: ty:: { TemplateDeclaration , Type , TypeKind } ;
25
25
use ir:: var:: Var ;
@@ -2434,8 +2434,92 @@ impl CodeGenerator for Function {
2434
2434
}
2435
2435
}
2436
2436
2437
+
2438
+ fn objc_method_codegen ( ctx : & BindgenContext ,
2439
+ method : & ObjCMethod ,
2440
+ class_name : Option < & str > )
2441
+ -> ( ast:: ImplItem , ast:: TraitItem ) {
2442
+ let signature = method. signature ( ) ;
2443
+ let fn_args = utils:: fnsig_arguments ( ctx, signature) ;
2444
+ let fn_ret = utils:: fnsig_return_ty ( ctx, signature) ;
2445
+
2446
+ let sig = if method. is_class_method ( ) {
2447
+ aster:: AstBuilder :: new ( )
2448
+ . method_sig ( )
2449
+ . unsafe_ ( )
2450
+ . fn_decl ( )
2451
+ . with_args ( fn_args. clone ( ) )
2452
+ . build ( fn_ret)
2453
+ } else {
2454
+ aster:: AstBuilder :: new ( )
2455
+ . method_sig ( )
2456
+ . unsafe_ ( )
2457
+ . fn_decl ( )
2458
+ . self_ ( )
2459
+ . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2460
+ . with_args ( fn_args. clone ( ) )
2461
+ . build ( fn_ret)
2462
+ } ;
2463
+
2464
+ // Collect the actual used argument names
2465
+ let arg_names: Vec < _ > = fn_args. iter ( )
2466
+ . map ( |ref arg| match arg. pat . node {
2467
+ ast:: PatKind :: Ident ( _, ref spanning, _) => {
2468
+ spanning. node . name . as_str ( ) . to_string ( )
2469
+ }
2470
+ _ => {
2471
+ panic ! ( "odd argument!" ) ;
2472
+ }
2473
+ } )
2474
+ . collect ( ) ;
2475
+
2476
+ let methods_and_args =
2477
+ ctx. rust_ident ( & method. format_method_call ( & arg_names) ) ;
2478
+
2479
+ let body = if method. is_class_method ( ) {
2480
+ let class_name =
2481
+ class_name. expect ( "Generating a class method without class name?" )
2482
+ . to_owned ( ) ;
2483
+ let expect_msg = format ! ( "Couldn't find {}" , class_name) ;
2484
+ quote_stmt ! ( ctx. ext_cx( ) ,
2485
+ msg_send![ objc:: runtime:: Class :: get( $class_name) . expect( $expect_msg) , $methods_and_args] )
2486
+ . unwrap ( )
2487
+ } else {
2488
+ quote_stmt ! ( ctx. ext_cx( ) , msg_send![ self , $methods_and_args] ) . unwrap ( )
2489
+ } ;
2490
+ let block = ast:: Block {
2491
+ stmts : vec ! [ body] ,
2492
+ id : ast:: DUMMY_NODE_ID ,
2493
+ rules : ast:: BlockCheckMode :: Default ,
2494
+ span : ctx. span ( ) ,
2495
+ } ;
2496
+
2497
+ let attrs = vec ! [ ] ;
2498
+
2499
+ let impl_item = ast:: ImplItem {
2500
+ id : ast:: DUMMY_NODE_ID ,
2501
+ ident : ctx. rust_ident ( method. rust_name ( ) ) ,
2502
+ vis : ast:: Visibility :: Inherited , // Public,
2503
+ attrs : attrs. clone ( ) ,
2504
+ node : ast:: ImplItemKind :: Method ( sig. clone ( ) , P ( block) ) ,
2505
+ defaultness : ast:: Defaultness :: Final ,
2506
+ span : ctx. span ( ) ,
2507
+ } ;
2508
+
2509
+ let trait_item = ast:: TraitItem {
2510
+ id : ast:: DUMMY_NODE_ID ,
2511
+ ident : ctx. rust_ident ( method. rust_name ( ) ) ,
2512
+ attrs : attrs,
2513
+ node : ast:: TraitItemKind :: Method ( sig, None ) ,
2514
+ span : ctx. span ( ) ,
2515
+ } ;
2516
+
2517
+ ( impl_item, trait_item)
2518
+ }
2519
+
2437
2520
impl CodeGenerator for ObjCInterface {
2438
2521
type Extra = Item ;
2522
+
2439
2523
fn codegen < ' a > ( & self ,
2440
2524
ctx : & BindgenContext ,
2441
2525
result : & mut CodegenResult < ' a > ,
@@ -2445,66 +2529,18 @@ impl CodeGenerator for ObjCInterface {
2445
2529
let mut trait_items = vec ! [ ] ;
2446
2530
2447
2531
for method in self . methods ( ) {
2448
- let signature = method. signature ( ) ;
2449
- let fn_args = utils:: fnsig_arguments ( ctx, signature) ;
2450
- let fn_ret = utils:: fnsig_return_ty ( ctx, signature) ;
2451
- let sig = aster:: AstBuilder :: new ( )
2452
- . method_sig ( )
2453
- . unsafe_ ( )
2454
- . fn_decl ( )
2455
- . self_ ( )
2456
- . build ( ast:: SelfKind :: Value ( ast:: Mutability :: Immutable ) )
2457
- . with_args ( fn_args. clone ( ) )
2458
- . build ( fn_ret) ;
2459
-
2460
- // Collect the actual used argument names
2461
- let arg_names: Vec < _ > = fn_args. iter ( )
2462
- . map ( |ref arg| match arg. pat . node {
2463
- ast:: PatKind :: Ident ( _, ref spanning, _) => {
2464
- spanning. node . name . as_str ( ) . to_string ( )
2465
- }
2466
- _ => {
2467
- panic ! ( "odd argument!" ) ;
2468
- }
2469
- } )
2470
- . collect ( ) ;
2471
-
2472
- let methods_and_args =
2473
- ctx. rust_ident ( & method. format_method_call ( & arg_names) ) ;
2474
- let body = quote_stmt ! ( ctx. ext_cx( ) ,
2475
- msg_send![ self , $methods_and_args] )
2476
- . unwrap ( ) ;
2477
- let block = ast:: Block {
2478
- stmts : vec ! [ body] ,
2479
- id : ast:: DUMMY_NODE_ID ,
2480
- rules : ast:: BlockCheckMode :: Default ,
2481
- span : ctx. span ( ) ,
2482
- } ;
2483
-
2484
- let attrs = vec ! [ ] ;
2485
-
2486
- let impl_item = ast:: ImplItem {
2487
- id : ast:: DUMMY_NODE_ID ,
2488
- ident : ctx. rust_ident ( method. rust_name ( ) ) ,
2489
- vis : ast:: Visibility :: Inherited , // Public,
2490
- attrs : attrs. clone ( ) ,
2491
- node : ast:: ImplItemKind :: Method ( sig. clone ( ) , P ( block) ) ,
2492
- defaultness : ast:: Defaultness :: Final ,
2493
- span : ctx. span ( ) ,
2494
- } ;
2495
-
2496
- let trait_item = ast:: TraitItem {
2497
- id : ast:: DUMMY_NODE_ID ,
2498
- ident : ctx. rust_ident ( method. rust_name ( ) ) ,
2499
- attrs : attrs,
2500
- node : ast:: TraitItemKind :: Method ( sig, None ) ,
2501
- span : ctx. span ( ) ,
2502
- } ;
2503
-
2532
+ let ( impl_item, trait_item) =
2533
+ objc_method_codegen ( ctx, method, None ) ;
2504
2534
impl_items. push ( impl_item) ;
2505
2535
trait_items. push ( trait_item)
2506
2536
}
2507
2537
2538
+ for class_method in self . class_methods ( ) {
2539
+ let ( impl_item, trait_item) =
2540
+ objc_method_codegen ( ctx, class_method, Some ( self . name ( ) ) ) ;
2541
+ impl_items. push ( impl_item) ;
2542
+ trait_items. push ( trait_item)
2543
+ }
2508
2544
2509
2545
let trait_name = self . rust_name ( ) ;
2510
2546
0 commit comments