Commit ebdc8faf authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: use fmt.State in nodefmt and stmtfmt

Change-Id: Iac87007af4ee268b45f11ec05bf4757f2e7eedd8
Reviewed-on: https://go-review.googlesource.com/28336Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent ff046d2e
...@@ -835,7 +835,7 @@ func stmtwithinit(op Op) bool { ...@@ -835,7 +835,7 @@ func stmtwithinit(op Op) bool {
return false return false
} }
func (p *printer) stmtfmt(n *Node) *printer { func (n *Node) stmtfmt(s fmt.State) {
// some statements allow for an init, but at most one, // some statements allow for an init, but at most one,
// but we may have an arbitrary number added, eg by typecheck // but we may have an arbitrary number added, eg by typecheck
// and inlining. If it doesn't fit the syntax, emit an enclosing // and inlining. If it doesn't fit the syntax, emit an enclosing
...@@ -851,22 +851,22 @@ func (p *printer) stmtfmt(n *Node) *printer { ...@@ -851,22 +851,22 @@ func (p *printer) stmtfmt(n *Node) *printer {
extrablock := complexinit && stmtwithinit(n.Op) extrablock := complexinit && stmtwithinit(n.Op)
if extrablock { if extrablock {
p.s("{") fmt.Fprint(s, "{")
} }
if complexinit { if complexinit {
p.f(" %v; ", n.Ninit) fmt.Fprintf(s, " %v; ", n.Ninit)
} }
switch n.Op { switch n.Op {
case ODCL: case ODCL:
p.f("var %v %v", n.Left.Sym, n.Left.Type) fmt.Fprintf(s, "var %v %v", n.Left.Sym, n.Left.Type)
case ODCLFIELD: case ODCLFIELD:
if n.Left != nil { if n.Left != nil {
p.f("%v %v", n.Left, n.Right) fmt.Fprintf(s, "%v %v", n.Left, n.Right)
} else { } else {
p.f("%v", n.Right) fmt.Fprintf(s, "%v", n.Right)
} }
// Don't export "v = <N>" initializing statements, hope they're always // Don't export "v = <N>" initializing statements, hope they're always
...@@ -874,132 +874,132 @@ func (p *printer) stmtfmt(n *Node) *printer { ...@@ -874,132 +874,132 @@ func (p *printer) stmtfmt(n *Node) *printer {
// the "v = <N>" again. // the "v = <N>" again.
case OAS, OASWB: case OAS, OASWB:
if n.Colas && !complexinit { if n.Colas && !complexinit {
p.f("%v := %v", n.Left, n.Right) fmt.Fprintf(s, "%v := %v", n.Left, n.Right)
} else { } else {
p.f("%v = %v", n.Left, n.Right) fmt.Fprintf(s, "%v = %v", n.Left, n.Right)
} }
case OASOP: case OASOP:
if n.Implicit { if n.Implicit {
if Op(n.Etype) == OADD { if Op(n.Etype) == OADD {
p.f("%v++", n.Left) fmt.Fprintf(s, "%v++", n.Left)
} else { } else {
p.f("%v--", n.Left) fmt.Fprintf(s, "%v--", n.Left)
} }
break break
} }
p.f("%v %#v= %v", n.Left, Op(n.Etype), n.Right) fmt.Fprintf(s, "%v %#v= %v", n.Left, Op(n.Etype), n.Right)
case OAS2: case OAS2:
if n.Colas && !complexinit { if n.Colas && !complexinit {
p.f("%v := %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma)) fmt.Fprintf(s, "%v := %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
break break
} }
fallthrough fallthrough
case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV: case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV:
p.f("%v = %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma)) fmt.Fprintf(s, "%v = %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
case ORETURN: case ORETURN:
p.f("return %v", hconv(n.List, FmtComma)) fmt.Fprintf(s, "return %v", hconv(n.List, FmtComma))
case ORETJMP: case ORETJMP:
p.f("retjmp %v", n.Sym) fmt.Fprintf(s, "retjmp %v", n.Sym)
case OPROC: case OPROC:
p.f("go %v", n.Left) fmt.Fprintf(s, "go %v", n.Left)
case ODEFER: case ODEFER:
p.f("defer %v", n.Left) fmt.Fprintf(s, "defer %v", n.Left)
case OIF: case OIF:
if simpleinit { if simpleinit {
p.f("if %v; %v { %v }", n.Ninit.First(), n.Left, n.Nbody) fmt.Fprintf(s, "if %v; %v { %v }", n.Ninit.First(), n.Left, n.Nbody)
} else { } else {
p.f("if %v { %v }", n.Left, n.Nbody) fmt.Fprintf(s, "if %v { %v }", n.Left, n.Nbody)
} }
if n.Rlist.Len() != 0 { if n.Rlist.Len() != 0 {
p.f(" else { %v }", n.Rlist) fmt.Fprintf(s, " else { %v }", n.Rlist)
} }
case OFOR: case OFOR:
if fmtmode == FErr { // TODO maybe only if FmtShort, same below if fmtmode == FErr { // TODO maybe only if FmtShort, same below
p.s("for loop") fmt.Fprint(s, "for loop")
break break
} }
p.s("for") fmt.Fprint(s, "for")
if simpleinit { if simpleinit {
p.f(" %v;", n.Ninit.First()) fmt.Fprintf(s, " %v;", n.Ninit.First())
} else if n.Right != nil { } else if n.Right != nil {
p.s(" ;") fmt.Fprint(s, " ;")
} }
if n.Left != nil { if n.Left != nil {
p.f(" %v", n.Left) fmt.Fprintf(s, " %v", n.Left)
} }
if n.Right != nil { if n.Right != nil {
p.f("; %v", n.Right) fmt.Fprintf(s, "; %v", n.Right)
} else if simpleinit { } else if simpleinit {
p.s(";") fmt.Fprint(s, ";")
} }
p.f(" { %v }", n.Nbody) fmt.Fprintf(s, " { %v }", n.Nbody)
case ORANGE: case ORANGE:
if fmtmode == FErr { if fmtmode == FErr {
p.s("for loop") fmt.Fprint(s, "for loop")
break break
} }
if n.List.Len() == 0 { if n.List.Len() == 0 {
p.f("for range %v { %v }", n.Right, n.Nbody) fmt.Fprintf(s, "for range %v { %v }", n.Right, n.Nbody)
break break
} }
p.f("for %v = range %v { %v }", hconv(n.List, FmtComma), n.Right, n.Nbody) fmt.Fprintf(s, "for %v = range %v { %v }", hconv(n.List, FmtComma), n.Right, n.Nbody)
case OSELECT, OSWITCH: case OSELECT, OSWITCH:
if fmtmode == FErr { if fmtmode == FErr {
p.f("%v statement", n.Op) fmt.Fprintf(s, "%v statement", n.Op)
break break
} }
p.s(n.Op.GoString()) // %#v fmt.Fprint(s, n.Op.GoString()) // %#v
if simpleinit { if simpleinit {
p.f(" %v;", n.Ninit.First()) fmt.Fprintf(s, " %v;", n.Ninit.First())
} }
if n.Left != nil { if n.Left != nil {
p.f(" %v ", n.Left) fmt.Fprintf(s, " %v ", n.Left)
} }
p.f(" { %v }", n.List) fmt.Fprintf(s, " { %v }", n.List)
case OXCASE: case OXCASE:
if n.List.Len() != 0 { if n.List.Len() != 0 {
p.f("case %v", hconv(n.List, FmtComma)) fmt.Fprintf(s, "case %v", hconv(n.List, FmtComma))
} else { } else {
p.s("default") fmt.Fprint(s, "default")
} }
p.f(": %v", n.Nbody) fmt.Fprintf(s, ": %v", n.Nbody)
case OCASE: case OCASE:
switch { switch {
case n.Left != nil: case n.Left != nil:
// single element // single element
p.f("case %v", n.Left) fmt.Fprintf(s, "case %v", n.Left)
case n.List.Len() > 0: case n.List.Len() > 0:
// range // range
if n.List.Len() != 2 { if n.List.Len() != 2 {
Fatalf("bad OCASE list length %d", n.List.Len()) Fatalf("bad OCASE list length %d", n.List.Len())
} }
p.f("case %v..%v", n.List.First(), n.List.Second()) fmt.Fprintf(s, "case %v..%v", n.List.First(), n.List.Second())
default: default:
p.s("default") fmt.Fprint(s, "default")
} }
p.f(": %v", n.Nbody) fmt.Fprintf(s, ": %v", n.Nbody)
case OBREAK, case OBREAK,
OCONTINUE, OCONTINUE,
...@@ -1007,23 +1007,21 @@ func (p *printer) stmtfmt(n *Node) *printer { ...@@ -1007,23 +1007,21 @@ func (p *printer) stmtfmt(n *Node) *printer {
OFALL, OFALL,
OXFALL: OXFALL:
if n.Left != nil { if n.Left != nil {
p.f("%#v %v", n.Op, n.Left) fmt.Fprintf(s, "%#v %v", n.Op, n.Left)
} else { } else {
p.s(n.Op.GoString()) // %#v fmt.Fprint(s, n.Op.GoString()) // %#v
} }
case OEMPTY: case OEMPTY:
break break
case OLABEL: case OLABEL:
p.f("%v: ", n.Left) fmt.Fprintf(s, "%v: ", n.Left)
} }
if extrablock { if extrablock {
p.s("}") fmt.Fprint(s, "}")
} }
return p
} }
var opprec = []int{ var opprec = []int{
...@@ -1455,7 +1453,7 @@ func (p *printer) exprfmt(n *Node, prec int) *printer { ...@@ -1455,7 +1453,7 @@ func (p *printer) exprfmt(n *Node, prec int) *printer {
return p.f("<node %v>", n.Op) return p.f("<node %v>", n.Op)
} }
func (p *printer) nodefmt(n *Node, flag FmtFlag) *printer { func (n *Node) nodefmt(s fmt.State, flag FmtFlag) {
t := n.Type t := n.Type
// we almost always want the original, except in export mode for literals // we almost always want the original, except in export mode for literals
...@@ -1467,19 +1465,21 @@ func (p *printer) nodefmt(n *Node, flag FmtFlag) *printer { ...@@ -1467,19 +1465,21 @@ func (p *printer) nodefmt(n *Node, flag FmtFlag) *printer {
if flag&FmtLong != 0 && t != nil { if flag&FmtLong != 0 && t != nil {
if t.Etype == TNIL { if t.Etype == TNIL {
return p.s("nil") fmt.Fprint(s, "nil")
} else { } else {
return p.f("%v (type %v)", n, t) fmt.Fprintf(s, "%v (type %v)", n, t)
} }
return
} }
// TODO inlining produces expressions with ninits. we can't print these yet. // TODO inlining produces expressions with ninits. we can't print these yet.
if opprec[n.Op] < 0 { if opprec[n.Op] < 0 {
return p.stmtfmt(n) n.stmtfmt(s)
return
} }
return p.exprfmt(n, 0) fmt.Fprint(s, new(printer).exprfmt(n, 0).String())
} }
func (p *printer) nodedump(n *Node, flag FmtFlag) *printer { func (p *printer) nodedump(n *Node, flag FmtFlag) *printer {
...@@ -1755,7 +1755,7 @@ func (n *Node) Nconv(s fmt.State) { ...@@ -1755,7 +1755,7 @@ func (n *Node) Nconv(s fmt.State) {
switch fmtmode { switch fmtmode {
case FErr: case FErr:
fmt.Fprint(s, new(printer).nodefmt(n, flag).String()) n.nodefmt(s, flag)
case FDbg: case FDbg:
dumpdepth++ dumpdepth++
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment