5
5
package gc
6
6
7
7
import (
8
+ "bytes"
8
9
"cmd/internal/obj"
9
10
"fmt"
10
11
"strconv"
@@ -529,21 +530,21 @@ var basicnames = []string{
529
530
TBLANK : "blank" ,
530
531
}
531
532
532
- func ( p * printer ) typefmt (t * Type , flag FmtFlag ) * printer {
533
+ func typefmt (t * Type , flag FmtFlag ) string {
533
534
if t == nil {
534
- return p . s ( "<T>" )
535
+ return "<T>"
535
536
}
536
537
537
538
if t == bytetype || t == runetype {
538
539
// in %-T mode collapse rune and byte with their originals.
539
540
if fmtmode != FTypeId {
540
- return p . s ( sconv (t .Sym , FmtShort ) )
541
+ return sconv (t .Sym , FmtShort )
541
542
}
542
543
t = Types [t .Etype ]
543
544
}
544
545
545
546
if t == errortype {
546
- return p . s ( "error" )
547
+ return "error"
547
548
}
548
549
549
550
// Unless the 'l' flag was specified, if the type has a name, just print that name.
@@ -552,190 +553,193 @@ func (p *printer) typefmt(t *Type, flag FmtFlag) *printer {
552
553
case FTypeId :
553
554
if flag & FmtShort != 0 {
554
555
if t .Vargen != 0 {
555
- p . f ("%v·%d" , sconv (t .Sym , FmtShort ), t .Vargen )
556
+ return fmt . Sprintf ("%v·%d" , sconv (t .Sym , FmtShort ), t .Vargen )
556
557
}
557
- return p . s ( sconv (t .Sym , FmtShort ) )
558
+ return sconv (t .Sym , FmtShort )
558
559
}
559
560
560
561
if flag & FmtUnsigned != 0 {
561
- return p . s ( sconv (t .Sym , FmtUnsigned ) )
562
+ return sconv (t .Sym , FmtUnsigned )
562
563
}
563
564
564
565
if t .Sym .Pkg == localpkg && t .Vargen != 0 {
565
- p . f ("%v·%d" , t .Sym , t .Vargen )
566
+ return fmt . Sprintf ("%v·%d" , t .Sym , t .Vargen )
566
567
}
567
568
}
568
569
569
- return p . s ( sconv (t .Sym , 0 ) )
570
+ return sconv (t .Sym , 0 )
570
571
}
571
572
572
573
if int (t .Etype ) < len (basicnames ) && basicnames [t .Etype ] != "" {
573
574
prefix := ""
574
575
if fmtmode == FErr && (t == idealbool || t == idealstring ) {
575
576
prefix = "untyped "
576
577
}
577
- return p . s ( prefix + basicnames [t .Etype ])
578
+ return prefix + basicnames [t .Etype ]
578
579
}
579
580
580
581
if fmtmode == FDbg {
581
582
fmtmode = 0
582
- p . s ( t .Etype .String ()). s ( "-" ). typefmt (t , flag )
583
+ str := t .Etype .String () + "-" + typefmt (t , flag )
583
584
fmtmode = FDbg
584
- return p
585
+ return str
585
586
}
586
587
587
588
switch t .Etype {
588
589
case TPTR32 , TPTR64 :
589
590
if fmtmode == FTypeId && (flag & FmtShort != 0 ) {
590
- return p . s ( "*" + Tconv (t .Elem (), FmtShort ) )
591
+ return "*" + Tconv (t .Elem (), FmtShort )
591
592
}
592
- return p . s ( "*" + t .Elem ().String () )
593
+ return "*" + t .Elem ().String ()
593
594
594
595
case TARRAY :
595
596
if t .isDDDArray () {
596
- return p . s ( "[...]" + t .Elem ().String () )
597
+ return "[...]" + t .Elem ().String ()
597
598
}
598
- p . f ("[%d]%v" , t .NumElem (), t .Elem ())
599
+ return fmt . Sprintf ("[%d]%v" , t .NumElem (), t .Elem ())
599
600
600
601
case TSLICE :
601
- return p . s ( "[]" + t .Elem ().String () )
602
+ return "[]" + t .Elem ().String ()
602
603
603
604
case TCHAN :
604
605
switch t .ChanDir () {
605
606
case Crecv :
606
- return p . s ( "<-chan " + t .Elem ().String () )
607
+ return "<-chan " + t .Elem ().String ()
607
608
608
609
case Csend :
609
- return p . s ( "chan<- " + t .Elem ().String () )
610
+ return "chan<- " + t .Elem ().String ()
610
611
}
611
612
612
613
if t .Elem () != nil && t .Elem ().IsChan () && t .Elem ().Sym == nil && t .Elem ().ChanDir () == Crecv {
613
- return p . s ( "chan (" + t .Elem ().String () + ")" )
614
+ return "chan (" + t .Elem ().String () + ")"
614
615
}
615
- return p . s ( "chan " + t .Elem ().String () )
616
+ return "chan " + t .Elem ().String ()
616
617
617
618
case TMAP :
618
- return p . s ( "map[" + t .Key ().String () + "]" + t .Val ().String () )
619
+ return "map[" + t .Key ().String () + "]" + t .Val ().String ()
619
620
620
621
case TINTER :
621
- p .s ("interface {" )
622
+ var buf bytes.Buffer
623
+ buf .WriteString ("interface {" )
622
624
for i , f := range t .Fields ().Slice () {
623
625
if i != 0 {
624
- p . s (";" )
626
+ buf . WriteString (";" )
625
627
}
626
- p . s (" " )
628
+ buf . WriteString (" " )
627
629
switch {
628
630
case f .Sym == nil :
629
631
// Check first that a symbol is defined for this type.
630
632
// Wrong interface definitions may have types lacking a symbol.
631
633
break
632
634
case exportname (f .Sym .Name ):
633
- p . s (sconv (f .Sym , FmtShort ))
635
+ buf . WriteString (sconv (f .Sym , FmtShort ))
634
636
default :
635
- p . s (sconv (f .Sym , FmtUnsigned ))
637
+ buf . WriteString (sconv (f .Sym , FmtUnsigned ))
636
638
}
637
- p . s (Tconv (f .Type , FmtShort ))
639
+ buf . WriteString (Tconv (f .Type , FmtShort ))
638
640
}
639
641
if t .NumFields () != 0 {
640
- p . s (" " )
642
+ buf . WriteString (" " )
641
643
}
642
- p . s ("}" )
643
- return p
644
+ buf . WriteString ("}" )
645
+ return buf . String ()
644
646
645
647
case TFUNC :
648
+ var buf bytes.Buffer
646
649
if flag & FmtShort != 0 {
647
650
// no leading func
648
651
} else {
649
652
if t .Recv () != nil {
650
- p . s ("method" )
651
- p . s (Tconv (t .Recvs (), 0 ))
652
- p . s (" " )
653
+ buf . WriteString ("method" )
654
+ buf . WriteString (Tconv (t .Recvs (), 0 ))
655
+ buf . WriteString (" " )
653
656
}
654
- p . s ("func" )
657
+ buf . WriteString ("func" )
655
658
}
656
- p . s (Tconv (t .Params (), 0 ))
659
+ buf . WriteString (Tconv (t .Params (), 0 ))
657
660
658
661
switch t .Results ().NumFields () {
659
662
case 0 :
660
663
// nothing to do
661
664
662
665
case 1 :
663
- p . s (" " )
664
- p . s (Tconv (t .Results ().Field (0 ).Type , 0 )) // struct->field->field's type
666
+ buf . WriteString (" " )
667
+ buf . WriteString (Tconv (t .Results ().Field (0 ).Type , 0 )) // struct->field->field's type
665
668
666
669
default :
667
- p . s (" " )
668
- p . s (Tconv (t .Results (), 0 ))
670
+ buf . WriteString (" " )
671
+ buf . WriteString (Tconv (t .Results (), 0 ))
669
672
}
670
- return p
673
+ return buf . String ()
671
674
672
675
case TSTRUCT :
673
676
if m := t .StructType ().Map ; m != nil {
674
677
mt := m .MapType ()
675
678
// Format the bucket struct for map[x]y as map.bucket[x]y.
676
679
// This avoids a recursive print that generates very long names.
677
680
if mt .Bucket == t {
678
- return p . s ( "map.bucket[" + m .Key ().String () + "]" + m .Val ().String () )
681
+ return "map.bucket[" + m .Key ().String () + "]" + m .Val ().String ()
679
682
}
680
683
681
684
if mt .Hmap == t {
682
- return p . s ( "map.hdr[" + m .Key ().String () + "]" + m .Val ().String () )
685
+ return "map.hdr[" + m .Key ().String () + "]" + m .Val ().String ()
683
686
}
684
687
685
688
if mt .Hiter == t {
686
- return p . s ( "map.iter[" + m .Key ().String () + "]" + m .Val ().String () )
689
+ return "map.iter[" + m .Key ().String () + "]" + m .Val ().String ()
687
690
}
688
691
689
692
Yyerror ("unknown internal map type" )
690
693
}
691
694
695
+ var buf bytes.Buffer
692
696
if t .IsFuncArgStruct () {
693
- p . s ("(" )
697
+ buf . WriteString ("(" )
694
698
var flag1 FmtFlag
695
699
if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
696
700
flag1 = FmtShort
697
701
}
698
702
for i , f := range t .Fields ().Slice () {
699
703
if i != 0 {
700
- p . s (", " )
704
+ buf . WriteString (", " )
701
705
}
702
- p . s (Fldconv (f , flag1 ))
706
+ buf . WriteString (Fldconv (f , flag1 ))
703
707
}
704
- p . s (")" )
708
+ buf . WriteString (")" )
705
709
} else {
706
- p . s ("struct {" )
710
+ buf . WriteString ("struct {" )
707
711
for i , f := range t .Fields ().Slice () {
708
712
if i != 0 {
709
- p . s (";" )
713
+ buf . WriteString (";" )
710
714
}
711
- p . s (" " )
712
- p . s (Fldconv (f , FmtLong ))
715
+ buf . WriteString (" " )
716
+ buf . WriteString (Fldconv (f , FmtLong ))
713
717
}
714
718
if t .NumFields () != 0 {
715
- p . s (" " )
719
+ buf . WriteString (" " )
716
720
}
717
- p . s ("}" )
721
+ buf . WriteString ("}" )
718
722
}
719
- return p
723
+ return buf . String ()
720
724
721
725
case TFORW :
722
726
if t .Sym != nil {
723
- return p . s ( "undefined " + t .Sym .String () )
727
+ return "undefined " + t .Sym .String ()
724
728
}
725
- return p . s ( "undefined" )
729
+ return "undefined"
726
730
727
731
case TUNSAFEPTR :
728
- return p . s ( "unsafe.Pointer" )
732
+ return "unsafe.Pointer"
729
733
730
734
case TDDDFIELD :
731
- p . f ("%v <%v> %v" , t .Etype , t .Sym , t .DDDField ())
735
+ return fmt . Sprintf ("%v <%v> %v" , t .Etype , t .Sym , t .DDDField ())
732
736
733
737
case Txxx :
734
- return p . s ( "Txxx" )
738
+ return "Txxx"
735
739
}
736
740
737
741
// Don't know how to handle - fall back to detailed prints.
738
- return p . f ("%v <%v> %v" , t .Etype , t .Sym , t .Elem ())
742
+ return fmt . Sprintf ("%v <%v> %v" , t .Etype , t .Sym , t .Elem ())
739
743
}
740
744
741
745
// Statements which may be rendered with a simplestmt as init.
@@ -1597,8 +1601,6 @@ func Fldconv(f *Field, flag FmtFlag) string {
1597
1601
// 'h' omit 'func' and receiver from function types, short type names
1598
1602
// 'u' package name, not prefix (FTypeId mode, sticky)
1599
1603
func Tconv (t * Type , flag FmtFlag ) string {
1600
- var p printer
1601
-
1602
1604
if t == nil {
1603
1605
return "<T>"
1604
1606
}
@@ -1618,7 +1620,7 @@ func Tconv(t *Type, flag FmtFlag) string {
1618
1620
flag |= FmtUnsigned
1619
1621
}
1620
1622
1621
- p . typefmt (t , flag )
1623
+ str := typefmt (t , flag )
1622
1624
1623
1625
if fmtmode == FTypeId && (sf & FmtUnsigned != 0 ) {
1624
1626
fmtpkgpfx --
@@ -1628,8 +1630,7 @@ func Tconv(t *Type, flag FmtFlag) string {
1628
1630
fmtbody = sb
1629
1631
fmtmode = sm
1630
1632
t .Trecur --
1631
-
1632
- return p .String ()
1633
+ return str
1633
1634
}
1634
1635
1635
1636
func (n * Node ) String () string {
0 commit comments