1
0
mirror of https://github.com/golang/go synced 2024-11-23 08:20:05 -07:00

cmd/compile: implement fmt.Formatter for Nodes formats %s, %v

Change-Id: Iac3a72cb6c5394f3c1a49f39125b0256d570e006
Reviewed-on: https://go-review.googlesource.com/28339
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2016-08-31 16:19:50 -07:00
parent daf6179733
commit 266c6223df
4 changed files with 35 additions and 34 deletions

View File

@ -396,7 +396,7 @@ func export(out *bufio.Writer, trace bool) int {
// function has inlineable body: // function has inlineable body:
// write index and body // write index and body
if p.trace { if p.trace {
p.tracef("\n----\nfunc { %s }\n", hconv(f.Inl, FmtSharp)) p.tracef("\n----\nfunc { %#s }\n", f.Inl)
} }
p.int(i) p.int(i)
p.stmtList(f.Inl) p.stmtList(f.Inl)

View File

@ -893,16 +893,16 @@ func (n *Node) stmtfmt(s fmt.State) {
case OAS2: case OAS2:
if n.Colas && !complexinit { if n.Colas && !complexinit {
fmt.Fprintf(s, "%v := %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma)) fmt.Fprintf(s, "%.v := %.v", n.List, n.Rlist)
break break
} }
fallthrough fallthrough
case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV: case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV:
fmt.Fprintf(s, "%v = %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma)) fmt.Fprintf(s, "%.v = %.v", n.List, n.Rlist)
case ORETURN: case ORETURN:
fmt.Fprintf(s, "return %v", hconv(n.List, FmtComma)) fmt.Fprintf(s, "return %.v", n.List)
case ORETJMP: case ORETJMP:
fmt.Fprintf(s, "retjmp %v", n.Sym) fmt.Fprintf(s, "retjmp %v", n.Sym)
@ -959,7 +959,7 @@ func (n *Node) stmtfmt(s fmt.State) {
break break
} }
fmt.Fprintf(s, "for %v = range %v { %v }", hconv(n.List, FmtComma), n.Right, n.Nbody) fmt.Fprintf(s, "for %.v = range %v { %v }", n.List, n.Right, n.Nbody)
case OSELECT, OSWITCH: case OSELECT, OSWITCH:
if fmtmode == FErr { if fmtmode == FErr {
@ -979,7 +979,7 @@ func (n *Node) stmtfmt(s fmt.State) {
case OXCASE: case OXCASE:
if n.List.Len() != 0 { if n.List.Len() != 0 {
fmt.Fprintf(s, "case %v", hconv(n.List, FmtComma)) fmt.Fprintf(s, "case %.v", n.List)
} else { } else {
fmt.Fprint(s, "default") fmt.Fprint(s, "default")
} }
@ -1296,7 +1296,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
return return
} }
fmt.Fprintf(s, "(%v{ %v })", n.Right, hconv(n.List, FmtComma)) fmt.Fprintf(s, "(%v{ %.v })", n.Right, n.List)
return return
case OPTRLIT: case OPTRLIT:
@ -1308,7 +1308,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
fmt.Fprintf(s, "%v literal", n.Type) fmt.Fprintf(s, "%v literal", n.Type)
return return
} }
fmt.Fprintf(s, "(%v{ %v })", n.Type, hconv(n.List, FmtComma)) fmt.Fprintf(s, "(%v{ %.v })", n.Type, n.List)
return return
case OKEY: case OKEY:
@ -1400,7 +1400,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
fmt.Fprintf(s, "%v(%v)", n.Type, n.Left) fmt.Fprintf(s, "%v(%v)", n.Type, n.Left)
return return
} }
fmt.Fprintf(s, "%v(%v)", n.Type, hconv(n.List, FmtComma)) fmt.Fprintf(s, "%v(%.v)", n.Type, n.List)
return return
case OREAL, case OREAL,
@ -1421,24 +1421,24 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
return return
} }
if n.Isddd { if n.Isddd {
fmt.Fprintf(s, "%#v(%v...)", n.Op, hconv(n.List, FmtComma)) fmt.Fprintf(s, "%#v(%.v...)", n.Op, n.List)
return return
} }
fmt.Fprintf(s, "%#v(%v)", n.Op, hconv(n.List, FmtComma)) fmt.Fprintf(s, "%#v(%.v)", n.Op, n.List)
return return
case OCALL, OCALLFUNC, OCALLINTER, OCALLMETH, OGETG: case OCALL, OCALLFUNC, OCALLINTER, OCALLMETH, OGETG:
n.Left.exprfmt(s, nprec) n.Left.exprfmt(s, nprec)
if n.Isddd { if n.Isddd {
fmt.Fprintf(s, "(%v...)", hconv(n.List, FmtComma)) fmt.Fprintf(s, "(%.v...)", n.List)
return return
} }
fmt.Fprintf(s, "(%v)", hconv(n.List, FmtComma)) fmt.Fprintf(s, "(%.v)", n.List)
return return
case OMAKEMAP, OMAKECHAN, OMAKESLICE: case OMAKEMAP, OMAKECHAN, OMAKESLICE:
if n.List.Len() != 0 { // pre-typecheck if n.List.Len() != 0 { // pre-typecheck
fmt.Fprintf(s, "make(%v, %v)", n.Type, hconv(n.List, FmtComma)) fmt.Fprintf(s, "make(%v, %.v)", n.Type, n.List)
return return
} }
if n.Right != nil { if n.Right != nil {
@ -1831,25 +1831,28 @@ func (n *Node) Nconv(s fmt.State) {
fmtmode = sm fmtmode = sm
} }
func (n Nodes) Print(p *printer) { func (l Nodes) Format(s fmt.State, format rune) {
p.hconv(n, 0) switch format {
case 's', 'v':
l.hconv(s)
default:
fmt.Fprintf(s, "%%!%c(Nodes)", format)
}
} }
var _ Printable = Nodes{} // verify that Nodes implements Printable
func (n Nodes) String() string { func (n Nodes) String() string {
return hconv(n, 0) return fmt.Sprint(n)
} }
// Fmt '%H': Nodes. // Fmt '%H': Nodes.
// Flags: all those of %N plus ',': separate with comma's instead of semicolons. // Flags: all those of %N plus ',': separate with comma's instead of semicolons.
func hconv(l Nodes, flag FmtFlag) string { func (l Nodes) hconv(s fmt.State) {
return new(printer).hconv(l, flag).String() flag := fmtFlag(s)
}
func (p *printer) hconv(l Nodes, flag FmtFlag) *printer {
if l.Len() == 0 && fmtmode == FDbg { if l.Len() == 0 && fmtmode == FDbg {
return p.s("<nil>") fmt.Fprint(s, "<nil>")
return
} }
sf := flag sf := flag
@ -1862,20 +1865,18 @@ func (p *printer) hconv(l Nodes, flag FmtFlag) *printer {
} }
for i, n := range l.Slice() { for i, n := range l.Slice() {
p.f("%v", n) fmt.Fprint(s, n)
if i+1 < l.Len() { if i+1 < l.Len() {
p.s(sep) fmt.Fprint(s, sep)
} }
} }
flag = sf flag = sf
fmtmode = sm fmtmode = sm
return p
} }
func dumplist(s string, l Nodes) { func dumplist(s string, l Nodes) {
fmt.Printf("%s%v\n", s, hconv(l, FmtSign)) fmt.Printf("%s%+v\n", s, l)
} }
func Dump(s string, n *Node) { func Dump(s string, n *Node) {

View File

@ -65,7 +65,7 @@ func typecheckinl(fn *Node) {
} }
if Debug['m'] > 2 || Debug_export != 0 { if Debug['m'] > 2 || Debug_export != 0 {
fmt.Printf("typecheck import [%v] %2v { %v }\n", fn.Sym, fn, hconv(fn.Func.Inl, FmtSharp)) fmt.Printf("typecheck import [%v] %2v { %#v }\n", fn.Sym, fn, fn.Func.Inl)
} }
save_safemode := safemode save_safemode := safemode
@ -165,7 +165,7 @@ func caninl(fn *Node) {
fn.Type.SetNname(n) fn.Type.SetNname(n)
if Debug['m'] > 1 { if Debug['m'] > 1 {
fmt.Printf("%v: can inline %#v as: %#v { %v }\n", fn.Line(), n, fn.Type, hconv(n.Func.Inl, FmtSharp)) fmt.Printf("%v: can inline %#v as: %#v { %#v }\n", fn.Line(), n, fn.Type, n.Func.Inl)
} else if Debug['m'] != 0 { } else if Debug['m'] != 0 {
fmt.Printf("%v: can inline %v\n", fn.Line(), n) fmt.Printf("%v: can inline %v\n", fn.Line(), n)
} }
@ -556,7 +556,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
// Bingo, we have a function node, and it has an inlineable body // Bingo, we have a function node, and it has an inlineable body
if Debug['m'] > 1 { if Debug['m'] > 1 {
fmt.Printf("%v: inlining call to %v %#v { %v }\n", n.Line(), fn.Sym, fn.Type, hconv(fn.Func.Inl, FmtSharp)) fmt.Printf("%v: inlining call to %v %#v { %#v }\n", n.Line(), fn.Sym, fn.Type, fn.Func.Inl)
} else if Debug['m'] != 0 { } else if Debug['m'] != 0 {
fmt.Printf("%v: inlining call to %v\n", n.Line(), fn) fmt.Printf("%v: inlining call to %v\n", n.Line(), fn)
} }
@ -752,7 +752,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
} }
if li < n.List.Len() || t != nil { if li < n.List.Len() || t != nil {
Fatalf("arg count mismatch: %#v vs %v\n", fn.Type.Params(), hconv(n.List, FmtComma)) Fatalf("arg count mismatch: %#v vs %.v\n", fn.Type.Params(), n.List)
} }
} }

View File

@ -1745,7 +1745,7 @@ func ascompatee(op Op, nl, nr []*Node, init *Nodes) []*Node {
var nln, nrn Nodes var nln, nrn Nodes
nln.Set(nl) nln.Set(nl)
nrn.Set(nr) nrn.Set(nr)
Yyerror("error in shape across %v %v %v / %d %d [%s]", hconv(nln, FmtSign), op, hconv(nrn, FmtSign), len(nl), len(nr), Curfn.Func.Nname.Sym.Name) Yyerror("error in shape across %+v %v %+v / %d %d [%s]", nln, op, nrn, len(nl), len(nr), Curfn.Func.Nname.Sym.Name)
} }
return nn return nn
} }