mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: use printer in typefmt, Tconv
Change-Id: Ib3ac0177761af1edea6b7951ffbbea042fb836d2 Reviewed-on: https://go-review.googlesource.com/28055 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
d3134b6450
commit
0a7c73b5db
1 changed files with 66 additions and 69 deletions
|
|
@ -5,7 +5,6 @@
|
||||||
package gc
|
package gc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"cmd/internal/obj"
|
"cmd/internal/obj"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -530,21 +529,21 @@ var basicnames = []string{
|
||||||
TBLANK: "blank",
|
TBLANK: "blank",
|
||||||
}
|
}
|
||||||
|
|
||||||
func typefmt(t *Type, flag FmtFlag) string {
|
func (p *printer) typefmt(t *Type, flag FmtFlag) *printer {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return "<T>"
|
return p.s("<T>")
|
||||||
}
|
}
|
||||||
|
|
||||||
if t == bytetype || t == runetype {
|
if t == bytetype || t == runetype {
|
||||||
// in %-T mode collapse rune and byte with their originals.
|
// in %-T mode collapse rune and byte with their originals.
|
||||||
if fmtmode != FTypeId {
|
if fmtmode != FTypeId {
|
||||||
return sconv(t.Sym, FmtShort)
|
return p.s(sconv(t.Sym, FmtShort))
|
||||||
}
|
}
|
||||||
t = Types[t.Etype]
|
t = Types[t.Etype]
|
||||||
}
|
}
|
||||||
|
|
||||||
if t == errortype {
|
if t == errortype {
|
||||||
return "error"
|
return p.s("error")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unless the 'l' flag was specified, if the type has a name, just print that name.
|
// Unless the 'l' flag was specified, if the type has a name, just print that name.
|
||||||
|
|
@ -553,124 +552,120 @@ func typefmt(t *Type, flag FmtFlag) string {
|
||||||
case FTypeId:
|
case FTypeId:
|
||||||
if flag&FmtShort != 0 {
|
if flag&FmtShort != 0 {
|
||||||
if t.Vargen != 0 {
|
if t.Vargen != 0 {
|
||||||
return fmt.Sprintf("%v·%d", sconv(t.Sym, FmtShort), t.Vargen)
|
return p.f("%v·%d", sconv(t.Sym, FmtShort), t.Vargen)
|
||||||
}
|
}
|
||||||
return sconv(t.Sym, FmtShort)
|
return p.s(sconv(t.Sym, FmtShort))
|
||||||
}
|
}
|
||||||
|
|
||||||
if flag&FmtUnsigned != 0 {
|
if flag&FmtUnsigned != 0 {
|
||||||
return sconv(t.Sym, FmtUnsigned)
|
return p.s(sconv(t.Sym, FmtUnsigned))
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Sym.Pkg == localpkg && t.Vargen != 0 {
|
if t.Sym.Pkg == localpkg && t.Vargen != 0 {
|
||||||
return fmt.Sprintf("%v·%d", t.Sym, t.Vargen)
|
return p.f("%v·%d", t.Sym, t.Vargen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sconv(t.Sym, 0)
|
return p.s(sconv(t.Sym, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
if int(t.Etype) < len(basicnames) && basicnames[t.Etype] != "" {
|
if int(t.Etype) < len(basicnames) && basicnames[t.Etype] != "" {
|
||||||
prefix := ""
|
|
||||||
if fmtmode == FErr && (t == idealbool || t == idealstring) {
|
if fmtmode == FErr && (t == idealbool || t == idealstring) {
|
||||||
prefix = "untyped "
|
p.s("untyped ")
|
||||||
}
|
}
|
||||||
return prefix + basicnames[t.Etype]
|
return p.s(basicnames[t.Etype])
|
||||||
}
|
}
|
||||||
|
|
||||||
if fmtmode == FDbg {
|
if fmtmode == FDbg {
|
||||||
fmtmode = 0
|
fmtmode = 0
|
||||||
str := t.Etype.String() + "-" + typefmt(t, flag)
|
p.s(t.Etype.String()).s("-").typefmt(t, flag)
|
||||||
fmtmode = FDbg
|
fmtmode = FDbg
|
||||||
return str
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t.Etype {
|
switch t.Etype {
|
||||||
case TPTR32, TPTR64:
|
case TPTR32, TPTR64:
|
||||||
if fmtmode == FTypeId && (flag&FmtShort != 0) {
|
if fmtmode == FTypeId && (flag&FmtShort != 0) {
|
||||||
return "*" + Tconv(t.Elem(), FmtShort)
|
return p.s("*" + Tconv(t.Elem(), FmtShort))
|
||||||
}
|
}
|
||||||
return "*" + t.Elem().String()
|
return p.s("*" + t.Elem().String())
|
||||||
|
|
||||||
case TARRAY:
|
case TARRAY:
|
||||||
if t.isDDDArray() {
|
if t.isDDDArray() {
|
||||||
return "[...]" + t.Elem().String()
|
return p.s("[...]" + t.Elem().String())
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("[%d]%v", t.NumElem(), t.Elem())
|
return p.f("[%d]%v", t.NumElem(), t.Elem())
|
||||||
|
|
||||||
case TSLICE:
|
case TSLICE:
|
||||||
return "[]" + t.Elem().String()
|
return p.s("[]" + t.Elem().String())
|
||||||
|
|
||||||
case TCHAN:
|
case TCHAN:
|
||||||
switch t.ChanDir() {
|
switch t.ChanDir() {
|
||||||
case Crecv:
|
case Crecv:
|
||||||
return "<-chan " + t.Elem().String()
|
return p.s("<-chan " + t.Elem().String())
|
||||||
|
|
||||||
case Csend:
|
case Csend:
|
||||||
return "chan<- " + t.Elem().String()
|
return p.s("chan<- " + t.Elem().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv {
|
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv {
|
||||||
return "chan (" + t.Elem().String() + ")"
|
return p.s("chan (" + t.Elem().String() + ")")
|
||||||
}
|
}
|
||||||
return "chan " + t.Elem().String()
|
return p.s("chan " + t.Elem().String())
|
||||||
|
|
||||||
case TMAP:
|
case TMAP:
|
||||||
return "map[" + t.Key().String() + "]" + t.Val().String()
|
return p.s("map[" + t.Key().String() + "]" + t.Val().String())
|
||||||
|
|
||||||
case TINTER:
|
case TINTER:
|
||||||
var buf bytes.Buffer
|
p.s("interface {")
|
||||||
buf.WriteString("interface {")
|
|
||||||
for i, f := range t.Fields().Slice() {
|
for i, f := range t.Fields().Slice() {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
buf.WriteString(";")
|
p.s(";")
|
||||||
}
|
}
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
switch {
|
switch {
|
||||||
case f.Sym == nil:
|
case f.Sym == nil:
|
||||||
// Check first that a symbol is defined for this type.
|
// Check first that a symbol is defined for this type.
|
||||||
// Wrong interface definitions may have types lacking a symbol.
|
// Wrong interface definitions may have types lacking a symbol.
|
||||||
break
|
break
|
||||||
case exportname(f.Sym.Name):
|
case exportname(f.Sym.Name):
|
||||||
buf.WriteString(sconv(f.Sym, FmtShort))
|
p.s(sconv(f.Sym, FmtShort))
|
||||||
default:
|
default:
|
||||||
buf.WriteString(sconv(f.Sym, FmtUnsigned))
|
p.s(sconv(f.Sym, FmtUnsigned))
|
||||||
}
|
}
|
||||||
buf.WriteString(Tconv(f.Type, FmtShort))
|
p.s(Tconv(f.Type, FmtShort))
|
||||||
}
|
}
|
||||||
if t.NumFields() != 0 {
|
if t.NumFields() != 0 {
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
}
|
}
|
||||||
buf.WriteString("}")
|
return p.s("}")
|
||||||
return buf.String()
|
|
||||||
|
|
||||||
case TFUNC:
|
case TFUNC:
|
||||||
var buf bytes.Buffer
|
|
||||||
if flag&FmtShort != 0 {
|
if flag&FmtShort != 0 {
|
||||||
// no leading func
|
// no leading func
|
||||||
} else {
|
} else {
|
||||||
if t.Recv() != nil {
|
if t.Recv() != nil {
|
||||||
buf.WriteString("method")
|
p.s("method")
|
||||||
buf.WriteString(Tconv(t.Recvs(), 0))
|
p.s(Tconv(t.Recvs(), 0))
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
}
|
}
|
||||||
buf.WriteString("func")
|
p.s("func")
|
||||||
}
|
}
|
||||||
buf.WriteString(Tconv(t.Params(), 0))
|
p.s(Tconv(t.Params(), 0))
|
||||||
|
|
||||||
switch t.Results().NumFields() {
|
switch t.Results().NumFields() {
|
||||||
case 0:
|
case 0:
|
||||||
// nothing to do
|
// nothing to do
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
buf.WriteString(Tconv(t.Results().Field(0).Type, 0)) // struct->field->field's type
|
p.s(Tconv(t.Results().Field(0).Type, 0)) // struct->field->field's type
|
||||||
|
|
||||||
default:
|
default:
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
buf.WriteString(Tconv(t.Results(), 0))
|
p.s(Tconv(t.Results(), 0))
|
||||||
}
|
}
|
||||||
return buf.String()
|
return p
|
||||||
|
|
||||||
case TSTRUCT:
|
case TSTRUCT:
|
||||||
if m := t.StructType().Map; m != nil {
|
if m := t.StructType().Map; m != nil {
|
||||||
|
|
@ -678,68 +673,67 @@ func typefmt(t *Type, flag FmtFlag) string {
|
||||||
// Format the bucket struct for map[x]y as map.bucket[x]y.
|
// Format the bucket struct for map[x]y as map.bucket[x]y.
|
||||||
// This avoids a recursive print that generates very long names.
|
// This avoids a recursive print that generates very long names.
|
||||||
if mt.Bucket == t {
|
if mt.Bucket == t {
|
||||||
return "map.bucket[" + m.Key().String() + "]" + m.Val().String()
|
return p.s("map.bucket[" + m.Key().String() + "]" + m.Val().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if mt.Hmap == t {
|
if mt.Hmap == t {
|
||||||
return "map.hdr[" + m.Key().String() + "]" + m.Val().String()
|
return p.s("map.hdr[" + m.Key().String() + "]" + m.Val().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if mt.Hiter == t {
|
if mt.Hiter == t {
|
||||||
return "map.iter[" + m.Key().String() + "]" + m.Val().String()
|
return p.s("map.iter[" + m.Key().String() + "]" + m.Val().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
Yyerror("unknown internal map type")
|
Yyerror("unknown internal map type")
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if t.IsFuncArgStruct() {
|
if t.IsFuncArgStruct() {
|
||||||
buf.WriteString("(")
|
p.s("(")
|
||||||
var flag1 FmtFlag
|
var flag1 FmtFlag
|
||||||
if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
|
if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
|
||||||
flag1 = FmtShort
|
flag1 = FmtShort
|
||||||
}
|
}
|
||||||
for i, f := range t.Fields().Slice() {
|
for i, f := range t.Fields().Slice() {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
buf.WriteString(", ")
|
p.s(", ")
|
||||||
}
|
}
|
||||||
buf.WriteString(Fldconv(f, flag1))
|
p.s(Fldconv(f, flag1))
|
||||||
}
|
}
|
||||||
buf.WriteString(")")
|
p.s(")")
|
||||||
} else {
|
} else {
|
||||||
buf.WriteString("struct {")
|
p.s("struct {")
|
||||||
for i, f := range t.Fields().Slice() {
|
for i, f := range t.Fields().Slice() {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
buf.WriteString(";")
|
p.s(";")
|
||||||
}
|
}
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
buf.WriteString(Fldconv(f, FmtLong))
|
p.s(Fldconv(f, FmtLong))
|
||||||
}
|
}
|
||||||
if t.NumFields() != 0 {
|
if t.NumFields() != 0 {
|
||||||
buf.WriteString(" ")
|
p.s(" ")
|
||||||
}
|
}
|
||||||
buf.WriteString("}")
|
p.s("}")
|
||||||
}
|
}
|
||||||
return buf.String()
|
return p
|
||||||
|
|
||||||
case TFORW:
|
case TFORW:
|
||||||
if t.Sym != nil {
|
if t.Sym != nil {
|
||||||
return "undefined " + t.Sym.String()
|
return p.s("undefined " + t.Sym.String())
|
||||||
}
|
}
|
||||||
return "undefined"
|
return p.s("undefined")
|
||||||
|
|
||||||
case TUNSAFEPTR:
|
case TUNSAFEPTR:
|
||||||
return "unsafe.Pointer"
|
return p.s("unsafe.Pointer")
|
||||||
|
|
||||||
case TDDDFIELD:
|
case TDDDFIELD:
|
||||||
return fmt.Sprintf("%v <%v> %v", t.Etype, t.Sym, t.DDDField())
|
return p.f("%v <%v> %v", t.Etype, t.Sym, t.DDDField())
|
||||||
|
|
||||||
case Txxx:
|
case Txxx:
|
||||||
return "Txxx"
|
return p.s("Txxx")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't know how to handle - fall back to detailed prints.
|
// Don't know how to handle - fall back to detailed prints.
|
||||||
return fmt.Sprintf("%v <%v> %v", t.Etype, t.Sym, t.Elem())
|
return p.f("%v <%v> %v", t.Etype, t.Sym, t.Elem())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statements which may be rendered with a simplestmt as init.
|
// Statements which may be rendered with a simplestmt as init.
|
||||||
|
|
@ -1601,6 +1595,8 @@ func Fldconv(f *Field, flag FmtFlag) string {
|
||||||
// 'h' omit 'func' and receiver from function types, short type names
|
// 'h' omit 'func' and receiver from function types, short type names
|
||||||
// 'u' package name, not prefix (FTypeId mode, sticky)
|
// 'u' package name, not prefix (FTypeId mode, sticky)
|
||||||
func Tconv(t *Type, flag FmtFlag) string {
|
func Tconv(t *Type, flag FmtFlag) string {
|
||||||
|
var p printer
|
||||||
|
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return "<T>"
|
return "<T>"
|
||||||
}
|
}
|
||||||
|
|
@ -1620,7 +1616,7 @@ func Tconv(t *Type, flag FmtFlag) string {
|
||||||
flag |= FmtUnsigned
|
flag |= FmtUnsigned
|
||||||
}
|
}
|
||||||
|
|
||||||
str := typefmt(t, flag)
|
p.typefmt(t, flag)
|
||||||
|
|
||||||
if fmtmode == FTypeId && (sf&FmtUnsigned != 0) {
|
if fmtmode == FTypeId && (sf&FmtUnsigned != 0) {
|
||||||
fmtpkgpfx--
|
fmtpkgpfx--
|
||||||
|
|
@ -1630,7 +1626,8 @@ func Tconv(t *Type, flag FmtFlag) string {
|
||||||
fmtbody = sb
|
fmtbody = sb
|
||||||
fmtmode = sm
|
fmtmode = sm
|
||||||
t.Trecur--
|
t.Trecur--
|
||||||
return str
|
|
||||||
|
return p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node) String() string {
|
func (n *Node) String() string {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue