@@ -48,6 +48,21 @@ pub fn item_signature(item: &ast::Item, scx: &SaveContext) -> Option<Signature>
48
48
item. make ( 0 , None , scx) . ok ( )
49
49
}
50
50
51
+ pub fn foreign_item_signature ( item : & ast:: ForeignItem , scx : & SaveContext ) -> Option < Signature > {
52
+ item. make ( 0 , None , scx) . ok ( )
53
+ }
54
+
55
+ /// Signature for a struct or tuple field declaration.
56
+ /// Does not include a trailing comma.
57
+ pub fn field_signature ( field : & ast:: StructField , scx : & SaveContext ) -> Option < Signature > {
58
+ field. make ( 0 , None , scx) . ok ( )
59
+ }
60
+
61
+ /// Does not include a trailing comma.
62
+ pub fn variant_signature ( variant : & ast:: Variant , scx : & SaveContext ) -> Option < Signature > {
63
+ variant. node . make ( 0 , None , scx) . ok ( )
64
+ }
65
+
51
66
type Result = :: std:: result:: Result < Signature , & ' static str > ;
52
67
53
68
trait Sig {
@@ -255,20 +270,6 @@ impl Sig for ast::Item {
255
270
fn make ( & self , offset : usize , _parent_id : Option < NodeId > , scx : & SaveContext ) -> Result {
256
271
let id = Some ( self . id ) ;
257
272
258
- let name_and_generics = |mut text : String , generics : & ast:: Generics | -> Result {
259
- let name = self . ident . to_string ( ) ;
260
- let def = SigElement {
261
- id : id_from_node_id ( self . id , scx) ,
262
- start : offset + text. len ( ) ,
263
- end : offset + text. len ( ) + name. len ( ) ,
264
- } ;
265
- text. push_str ( & name) ;
266
- let generics: Signature = generics. make ( offset + text. len ( ) , id, scx) ?;
267
- // FIXME where clause
268
- let text = format ! ( "{}{}" , text, generics. text) ;
269
- Ok ( extend_sig ( generics, text, vec ! [ def] , vec ! [ ] ) )
270
- } ;
271
-
272
273
match self . node {
273
274
ast:: ItemKind :: Static ( ref ty, m, ref expr) => {
274
275
let mut text = "static " . to_owned ( ) ;
@@ -330,7 +331,12 @@ impl Sig for ast::Item {
330
331
}
331
332
text. push_str ( "fn " ) ;
332
333
333
- let mut sig = name_and_generics ( text, generics) ?;
334
+ let mut sig = name_and_generics ( text,
335
+ offset,
336
+ generics,
337
+ self . id ,
338
+ self . ident ,
339
+ scx) ?;
334
340
335
341
sig. text . push ( '(' ) ;
336
342
for i in & decl. inputs {
@@ -352,6 +358,7 @@ impl Sig for ast::Item {
352
358
sig. defs . extend ( nested. defs . into_iter ( ) ) ;
353
359
sig. refs . extend ( nested. refs . into_iter ( ) ) ;
354
360
}
361
+ sig. text . push_str ( " {}" ) ;
355
362
356
363
Ok ( sig)
357
364
}
@@ -375,7 +382,12 @@ impl Sig for ast::Item {
375
382
}
376
383
ast:: ItemKind :: Ty ( ref ty, ref generics) => {
377
384
let text = "type " . to_owned ( ) ;
378
- let mut sig = name_and_generics ( text, generics) ?;
385
+ let mut sig = name_and_generics ( text,
386
+ offset,
387
+ generics,
388
+ self . id ,
389
+ self . ident ,
390
+ scx) ?;
379
391
380
392
sig. text . push_str ( " = " ) ;
381
393
let ty = ty. make ( offset + sig. text . len ( ) , id, scx) ?;
@@ -386,19 +398,34 @@ impl Sig for ast::Item {
386
398
}
387
399
ast:: ItemKind :: Enum ( _, ref generics) => {
388
400
let text = "enum " . to_owned ( ) ;
389
- let mut sig = name_and_generics ( text, generics) ?;
401
+ let mut sig = name_and_generics ( text,
402
+ offset,
403
+ generics,
404
+ self . id ,
405
+ self . ident ,
406
+ scx) ?;
390
407
sig. text . push_str ( " {}" ) ;
391
408
Ok ( sig)
392
409
}
393
410
ast:: ItemKind :: Struct ( _, ref generics) => {
394
411
let text = "struct " . to_owned ( ) ;
395
- let mut sig = name_and_generics ( text, generics) ?;
412
+ let mut sig = name_and_generics ( text,
413
+ offset,
414
+ generics,
415
+ self . id ,
416
+ self . ident ,
417
+ scx) ?;
396
418
sig. text . push_str ( " {}" ) ;
397
419
Ok ( sig)
398
420
}
399
421
ast:: ItemKind :: Union ( _, ref generics) => {
400
422
let text = "union " . to_owned ( ) ;
401
- let mut sig = name_and_generics ( text, generics) ?;
423
+ let mut sig = name_and_generics ( text,
424
+ offset,
425
+ generics,
426
+ self . id ,
427
+ self . ident ,
428
+ scx) ?;
402
429
sig. text . push_str ( " {}" ) ;
403
430
Ok ( sig)
404
431
}
@@ -408,7 +435,12 @@ impl Sig for ast::Item {
408
435
text. push_str ( "unsafe " ) ;
409
436
}
410
437
text. push_str ( "trait " ) ;
411
- let mut sig = name_and_generics ( text, generics) ?;
438
+ let mut sig = name_and_generics ( text,
439
+ offset,
440
+ generics,
441
+ self . id ,
442
+ self . ident ,
443
+ scx) ?;
412
444
413
445
if !bounds. is_empty ( ) {
414
446
sig. text . push_str ( ": " ) ;
@@ -485,10 +517,6 @@ impl Sig for ast::Item {
485
517
486
518
impl Sig for ast:: Path {
487
519
fn make ( & self , offset : usize , id : Option < NodeId > , scx : & SaveContext ) -> Result {
488
- // if generated_code(span) {
489
- // return Err("Generated code");
490
- // }
491
-
492
520
let def = scx. get_path_def ( id. ok_or ( "Missing id for Path" ) ?) ;
493
521
494
522
let ( name, start, end) = match def {
@@ -583,4 +611,177 @@ impl Sig for ast::Generics {
583
611
}
584
612
}
585
613
586
- // TODO impl items, trait items, fields, extern items, enum variant
614
+ impl Sig for ast:: StructField {
615
+ fn make ( & self , offset : usize , _parent_id : Option < NodeId > , scx : & SaveContext ) -> Result {
616
+ let mut text = String :: new ( ) ;
617
+ let mut defs = None ;
618
+ if let Some ( ref ident) = self . ident {
619
+ text. push_str ( & ident. to_string ( ) ) ;
620
+ defs = Some ( SigElement {
621
+ id : id_from_node_id ( self . id , scx) ,
622
+ start : offset,
623
+ end : offset + text. len ( ) ,
624
+ } ) ;
625
+ text. push_str ( ": " ) ;
626
+ }
627
+
628
+ let mut ty_sig = self . ty . make ( offset + text. len ( ) , Some ( self . id ) , scx) ?;
629
+ text. push_str ( & ty_sig. text ) ;
630
+ ty_sig. text = text;
631
+ ty_sig. defs . extend ( defs. into_iter ( ) ) ;
632
+ Ok ( ty_sig)
633
+ }
634
+ }
635
+
636
+
637
+ impl Sig for ast:: Variant_ {
638
+ fn make ( & self , offset : usize , _parent_id : Option < NodeId > , scx : & SaveContext ) -> Result {
639
+ let mut text = self . name . to_string ( ) ;
640
+ match self . data {
641
+ ast:: VariantData :: Struct ( ref fields, id) => {
642
+ let name_def = SigElement {
643
+ id : id_from_node_id ( id, scx) ,
644
+ start : offset,
645
+ end : offset + text. len ( ) ,
646
+ } ;
647
+ text. push_str ( " { " ) ;
648
+ let mut defs = vec ! [ name_def] ;
649
+ let mut refs = vec ! [ ] ;
650
+ for f in fields {
651
+ let field_sig = f. make ( offset + text. len ( ) , Some ( id) , scx) ?;
652
+ text. push_str ( & field_sig. text ) ;
653
+ text. push_str ( ", " ) ;
654
+ defs. extend ( field_sig. defs . into_iter ( ) ) ;
655
+ refs. extend ( field_sig. refs . into_iter ( ) ) ;
656
+ }
657
+ text. push ( '}' ) ;
658
+ Ok ( Signature {
659
+ text,
660
+ defs : defs,
661
+ refs : refs,
662
+ } )
663
+ }
664
+ ast:: VariantData :: Tuple ( ref fields, id) => {
665
+ let name_def = SigElement {
666
+ id : id_from_node_id ( id, scx) ,
667
+ start : offset,
668
+ end : offset + text. len ( ) ,
669
+ } ;
670
+ text. push ( '(' ) ;
671
+ let mut defs = vec ! [ name_def] ;
672
+ let mut refs = vec ! [ ] ;
673
+ for f in fields {
674
+ let field_sig = f. make ( offset + text. len ( ) , Some ( id) , scx) ?;
675
+ text. push_str ( & field_sig. text ) ;
676
+ text. push_str ( ", " ) ;
677
+ defs. extend ( field_sig. defs . into_iter ( ) ) ;
678
+ refs. extend ( field_sig. refs . into_iter ( ) ) ;
679
+ }
680
+ text. push ( ')' ) ;
681
+ Ok ( Signature {
682
+ text,
683
+ defs : defs,
684
+ refs : refs,
685
+ } )
686
+ }
687
+ ast:: VariantData :: Unit ( id) => {
688
+ let name_def = SigElement {
689
+ id : id_from_node_id ( id, scx) ,
690
+ start : offset,
691
+ end : offset + text. len ( ) ,
692
+ } ;
693
+ Ok ( Signature {
694
+ text,
695
+ defs : vec ! [ name_def] ,
696
+ refs : vec ! [ ] ,
697
+ } )
698
+ }
699
+ }
700
+ }
701
+ }
702
+
703
+ impl Sig for ast:: ForeignItem {
704
+ fn make ( & self , offset : usize , _parent_id : Option < NodeId > , scx : & SaveContext ) -> Result {
705
+ let id = Some ( self . id ) ;
706
+ match self . node {
707
+ ast:: ForeignItemKind :: Fn ( ref decl, ref generics) => {
708
+ let mut text = String :: new ( ) ;
709
+ text. push_str ( "fn " ) ;
710
+
711
+ let mut sig = name_and_generics ( text,
712
+ offset,
713
+ generics,
714
+ self . id ,
715
+ self . ident ,
716
+ scx) ?;
717
+
718
+ sig. text . push ( '(' ) ;
719
+ for i in & decl. inputs {
720
+ // FIXME should descend into patterns to add defs.
721
+ sig. text . push_str ( & pprust:: pat_to_string ( & i. pat ) ) ;
722
+ sig. text . push_str ( ": " ) ;
723
+ let nested = i. ty . make ( offset + sig. text . len ( ) , Some ( i. id ) , scx) ?;
724
+ sig. text . push_str ( & nested. text ) ;
725
+ sig. text . push ( ',' ) ;
726
+ sig. defs . extend ( nested. defs . into_iter ( ) ) ;
727
+ sig. refs . extend ( nested. refs . into_iter ( ) ) ;
728
+ }
729
+ sig. text . push ( ')' ) ;
730
+
731
+ if let ast:: FunctionRetTy :: Ty ( ref t) = decl. output {
732
+ sig. text . push_str ( " -> " ) ;
733
+ let nested = t. make ( offset + sig. text . len ( ) , None , scx) ?;
734
+ sig. text . push_str ( & nested. text ) ;
735
+ sig. defs . extend ( nested. defs . into_iter ( ) ) ;
736
+ sig. refs . extend ( nested. refs . into_iter ( ) ) ;
737
+ }
738
+ sig. text . push ( ';' ) ;
739
+
740
+ Ok ( sig)
741
+ }
742
+ ast:: ForeignItemKind :: Static ( ref ty, m) => {
743
+ let mut text = "static " . to_owned ( ) ;
744
+ if m {
745
+ text. push_str ( "mut " ) ;
746
+ }
747
+ let name = self . ident . to_string ( ) ;
748
+ let defs = vec ! [ SigElement {
749
+ id: id_from_node_id( self . id, scx) ,
750
+ start: offset + text. len( ) ,
751
+ end: offset + text. len( ) + name. len( ) ,
752
+ } ] ;
753
+ text. push_str ( & name) ;
754
+ text. push_str ( ": " ) ;
755
+
756
+ let ty_sig = ty. make ( offset + text. len ( ) , id, scx) ?;
757
+ text. push ( ';' ) ;
758
+
759
+ Ok ( extend_sig ( ty_sig, text, defs, vec ! [ ] ) )
760
+ }
761
+ }
762
+ }
763
+ }
764
+
765
+ fn name_and_generics ( mut text : String ,
766
+ offset : usize ,
767
+ generics : & ast:: Generics ,
768
+ id : NodeId ,
769
+ name : ast:: Ident ,
770
+ scx : & SaveContext )
771
+ -> Result {
772
+ let name = name. to_string ( ) ;
773
+ let def = SigElement {
774
+ id : id_from_node_id ( id, scx) ,
775
+ start : offset + text. len ( ) ,
776
+ end : offset + text. len ( ) + name. len ( ) ,
777
+ } ;
778
+ text. push_str ( & name) ;
779
+ let generics: Signature = generics. make ( offset + text. len ( ) , Some ( id) , scx) ?;
780
+ // FIXME where clause
781
+ let text = format ! ( "{}{}" , text, generics. text) ;
782
+ Ok ( extend_sig ( generics, text, vec ! [ def] , vec ! [ ] ) )
783
+ }
784
+
785
+
786
+ // TODO impl items, trait items
787
+ // for impl/trait sigs - function for each kind, rather than use trait.
0 commit comments