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>
diff --git a/src/cmd/compile/internal/gc/bexport.go b/src/cmd/compile/internal/gc/bexport.go
index 5fb918f..3c8fb9a 100644
--- a/src/cmd/compile/internal/gc/bexport.go
+++ b/src/cmd/compile/internal/gc/bexport.go
@@ -396,7 +396,7 @@
 			// function has inlineable body:
 			// write index and body
 			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.stmtList(f.Inl)
diff --git a/src/cmd/compile/internal/gc/fmt.go b/src/cmd/compile/internal/gc/fmt.go
index 9b8bb65..d58199b 100644
--- a/src/cmd/compile/internal/gc/fmt.go
+++ b/src/cmd/compile/internal/gc/fmt.go
@@ -893,16 +893,16 @@
 
 	case OAS2:
 		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
 		}
 		fallthrough
 
 	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:
-		fmt.Fprintf(s, "return %v", hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "return %.v", n.List)
 
 	case ORETJMP:
 		fmt.Fprintf(s, "retjmp %v", n.Sym)
@@ -959,7 +959,7 @@
 			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:
 		if fmtmode == FErr {
@@ -979,7 +979,7 @@
 
 	case OXCASE:
 		if n.List.Len() != 0 {
-			fmt.Fprintf(s, "case %v", hconv(n.List, FmtComma))
+			fmt.Fprintf(s, "case %.v", n.List)
 		} else {
 			fmt.Fprint(s, "default")
 		}
@@ -1296,7 +1296,7 @@
 			return
 		}
 
-		fmt.Fprintf(s, "(%v{ %v })", n.Right, hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "(%v{ %.v })", n.Right, n.List)
 		return
 
 	case OPTRLIT:
@@ -1308,7 +1308,7 @@
 			fmt.Fprintf(s, "%v literal", n.Type)
 			return
 		}
-		fmt.Fprintf(s, "(%v{ %v })", n.Type, hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "(%v{ %.v })", n.Type, n.List)
 		return
 
 	case OKEY:
@@ -1400,7 +1400,7 @@
 			fmt.Fprintf(s, "%v(%v)", n.Type, n.Left)
 			return
 		}
-		fmt.Fprintf(s, "%v(%v)", n.Type, hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "%v(%.v)", n.Type, n.List)
 		return
 
 	case OREAL,
@@ -1421,24 +1421,24 @@
 			return
 		}
 		if n.Isddd {
-			fmt.Fprintf(s, "%#v(%v...)", n.Op, hconv(n.List, FmtComma))
+			fmt.Fprintf(s, "%#v(%.v...)", n.Op, n.List)
 			return
 		}
-		fmt.Fprintf(s, "%#v(%v)", n.Op, hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "%#v(%.v)", n.Op, n.List)
 		return
 
 	case OCALL, OCALLFUNC, OCALLINTER, OCALLMETH, OGETG:
 		n.Left.exprfmt(s, nprec)
 		if n.Isddd {
-			fmt.Fprintf(s, "(%v...)", hconv(n.List, FmtComma))
+			fmt.Fprintf(s, "(%.v...)", n.List)
 			return
 		}
-		fmt.Fprintf(s, "(%v)", hconv(n.List, FmtComma))
+		fmt.Fprintf(s, "(%.v)", n.List)
 		return
 
 	case OMAKEMAP, OMAKECHAN, OMAKESLICE:
 		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
 		}
 		if n.Right != nil {
@@ -1831,25 +1831,28 @@
 	fmtmode = sm
 }
 
-func (n Nodes) Print(p *printer) {
-	p.hconv(n, 0)
+func (l Nodes) Format(s fmt.State, format rune) {
+	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 {
-	return hconv(n, 0)
+	return fmt.Sprint(n)
 }
 
 // Fmt '%H': Nodes.
 // Flags: all those of %N plus ',': separate with comma's instead of semicolons.
-func hconv(l Nodes, flag FmtFlag) string {
-	return new(printer).hconv(l, flag).String()
-}
+func (l Nodes) hconv(s fmt.State) {
+	flag := fmtFlag(s)
 
-func (p *printer) hconv(l Nodes, flag FmtFlag) *printer {
 	if l.Len() == 0 && fmtmode == FDbg {
-		return p.s("<nil>")
+		fmt.Fprint(s, "<nil>")
+		return
 	}
 
 	sf := flag
@@ -1862,20 +1865,18 @@
 	}
 
 	for i, n := range l.Slice() {
-		p.f("%v", n)
+		fmt.Fprint(s, n)
 		if i+1 < l.Len() {
-			p.s(sep)
+			fmt.Fprint(s, sep)
 		}
 	}
 
 	flag = sf
 	fmtmode = sm
-
-	return p
 }
 
 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) {
diff --git a/src/cmd/compile/internal/gc/inl.go b/src/cmd/compile/internal/gc/inl.go
index 4b85fd3..8043ac8 100644
--- a/src/cmd/compile/internal/gc/inl.go
+++ b/src/cmd/compile/internal/gc/inl.go
@@ -65,7 +65,7 @@
 	}
 
 	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
@@ -165,7 +165,7 @@
 	fn.Type.SetNname(n)
 
 	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 {
 		fmt.Printf("%v: can inline %v\n", fn.Line(), n)
 	}
@@ -556,7 +556,7 @@
 
 	// Bingo, we have a function node, and it has an inlineable body
 	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 {
 		fmt.Printf("%v: inlining call to %v\n", n.Line(), fn)
 	}
@@ -752,7 +752,7 @@
 		}
 
 		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)
 		}
 	}
 
diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go
index 58ab4c6..9a6b426 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -1745,7 +1745,7 @@
 		var nln, nrn Nodes
 		nln.Set(nl)
 		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
 }