1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 // %L int Line numbers
20 // %E int etype values (aka 'Kind')
22 // %O int Node Opcodes
23 // Flags: "%#O": print go syntax. (automatic unless fmtmode == FDbg)
25 // %J Node* Node details
26 // Flags: "%hJ" suppresses things not relevant until walk.
28 // %V Val* Constant values
31 // Flags: +,- #: mode (see below)
32 // "%hS" unqualified identifier in any mode
33 // "%hhS" in export mode: unqualified identifier if exported, qualified if not
36 // Flags: +,- #: mode (see below)
37 // 'l' definition instead of name.
38 // 'h' omit "func" and receiver in function types
39 // 'u' (only in -/Sym mode) print type identifiers wit package name instead of prefix.
42 // Flags: +,- #: mode (see below)
43 // 'h' (only in +/debug mode) suppress recursion
44 // 'l' (only in Error mode) print "foo (type Bar)"
46 // %H NodeList* NodeLists
48 // ',' separate items with ',' instead of ';'
50 // In mparith2.go and mparith3.go:
51 // %B Mpint* Big integers
52 // %F Mpflt* Big floats
54 // %S, %T and %N obey use the following flags to set the format mode:
62 var fmtmode int = FErr
64 var fmtpkgpfx int // %uT stickyness
67 // E.g. for %S: %+S %#S %-S print an identifier properly qualified for debug/export/internal mode.
69 // The mode flags +, - and # are sticky, meaning they persist through
70 // recursions of %N, %T and %S, but not the h and l flags. The u flag is
71 // sticky only on %T recursions and only used in %-/Sym mode.
74 // Useful format combinations:
76 // %+N %+H multiline recursive debug dump of node/nodelist
77 // %+hN %+hH non recursive debug dump
79 // %#N %#T export format
80 // %#lT type definition instead of name
81 // %#hT omit"func" and receiver in function signature
83 // %lN "foo (type Bar)" for error messages
85 // %-T type identifiers
86 // %-hT type identifiers without "func" and arg names in type signatures (methodsym)
87 // %-uT type identifiers with package name instead of prefix (typesym, dcommontype, typehash)
90 func setfmode(flags *int) int {
92 if *flags&obj.FmtSign != 0 {
94 } else if *flags&obj.FmtSharp != 0 {
96 } else if *flags&obj.FmtLeft != 0 {
100 *flags &^= (obj.FmtSharp | obj.FmtLeft | obj.FmtSign)
104 // Fmt "%L": Linenumbers
106 var goopnames = []string{
117 OCALL: "function call", // not actual syntax
123 OCONTINUE: "continue",
130 OFALL: "fallthrough",
169 // Fmt "%O": Node opcodes
170 func Oconv(o int, flag int) string {
171 if (flag&obj.FmtSharp != 0) || fmtmode != FDbg {
172 if o >= 0 && o < len(goopnames) && goopnames[o] != "" {
177 if o >= 0 && o < len(opnames) && opnames[o] != "" {
181 return fmt.Sprintf("O-%d", o)
184 var classnames = []string{
194 // Fmt "%J": Node details.
195 func Jconv(n *Node, flag int) string {
198 c := flag & obj.FmtShort
200 if c == 0 && n.Ullman != 0 {
201 fmt.Fprintf(&buf, " u(%d)", n.Ullman)
204 if c == 0 && n.Addable {
205 fmt.Fprintf(&buf, " a(%v)", n.Addable)
208 if c == 0 && n.Name != nil && n.Name.Vargen != 0 {
209 fmt.Fprintf(&buf, " g(%d)", n.Name.Vargen)
213 fmt.Fprintf(&buf, " l(%d)", n.Lineno)
216 if c == 0 && n.Xoffset != BADWIDTH {
217 fmt.Fprintf(&buf, " x(%d%+d)", n.Xoffset, stkdelta[n])
222 if n.Class&PHEAP != 0 {
225 if int(n.Class&^PHEAP) < len(classnames) {
226 fmt.Fprintf(&buf, " class(%s%s)", classnames[n.Class&^PHEAP], s)
228 fmt.Fprintf(&buf, " class(%d?%s)", n.Class&^PHEAP, s)
233 fmt.Fprintf(&buf, " colas(%v)", n.Colas)
236 if n.Name != nil && n.Name.Funcdepth != 0 {
237 fmt.Fprintf(&buf, " f(%d)", n.Name.Funcdepth)
239 if n.Func != nil && n.Func.Depth != 0 {
240 fmt.Fprintf(&buf, " ff(%d)", n.Func.Depth)
248 buf.WriteString(" esc(h)")
251 buf.WriteString(" esc(s)")
254 buf.WriteString(" esc(no)")
258 buf.WriteString(" esc(N)")
262 fmt.Fprintf(&buf, " esc(%d)", n.Esc)
265 if e, ok := n.Opt().(*NodeEscState); ok && e.Escloopdepth != 0 {
266 fmt.Fprintf(&buf, " ld(%d)", e.Escloopdepth)
269 if c == 0 && n.Typecheck != 0 {
270 fmt.Fprintf(&buf, " tc(%d)", n.Typecheck)
273 if c == 0 && n.Dodata != 0 {
274 fmt.Fprintf(&buf, " dd(%d)", n.Dodata)
278 fmt.Fprintf(&buf, " isddd(%v)", n.Isddd)
282 fmt.Fprintf(&buf, " implicit(%v)", n.Implicit)
286 fmt.Fprintf(&buf, " embedded(%d)", n.Embedded)
290 buf.WriteString(" addrtaken")
294 buf.WriteString(" assigned")
297 if c == 0 && n.Used {
298 fmt.Fprintf(&buf, " used(%v)", n.Used)
304 func Vconv(v Val, flag int) string {
307 if (flag&obj.FmtSharp != 0) || fmtmode == FExp {
308 return Bconv(v.U.(*Mpint), obj.FmtSharp)
310 return Bconv(v.U.(*Mpint), 0)
313 x := Mpgetfix(v.U.(*Mpint))
314 if ' ' <= x && x < 0x80 && x != '\\' && x != '\'' {
315 return fmt.Sprintf("'%c'", int(x))
317 if 0 <= x && x < 1<<16 {
318 return fmt.Sprintf("'\\u%04x'", uint(int(x)))
320 if 0 <= x && x <= utf8.MaxRune {
321 return fmt.Sprintf("'\\U%08x'", uint64(x))
323 return fmt.Sprintf("('\\x00' + %v)", v.U.(*Mpint))
326 if (flag&obj.FmtSharp != 0) || fmtmode == FExp {
327 return Fconv(v.U.(*Mpflt), 0)
329 return Fconv(v.U.(*Mpflt), obj.FmtSharp)
332 if (flag&obj.FmtSharp != 0) || fmtmode == FExp {
333 return fmt.Sprintf("(%v+%vi)", &v.U.(*Mpcplx).Real, &v.U.(*Mpcplx).Imag)
335 if mpcmpfltc(&v.U.(*Mpcplx).Real, 0) == 0 {
336 return fmt.Sprintf("%vi", Fconv(&v.U.(*Mpcplx).Imag, obj.FmtSharp))
338 if mpcmpfltc(&v.U.(*Mpcplx).Imag, 0) == 0 {
339 return Fconv(&v.U.(*Mpcplx).Real, obj.FmtSharp)
341 if mpcmpfltc(&v.U.(*Mpcplx).Imag, 0) < 0 {
342 return fmt.Sprintf("(%v%vi)", Fconv(&v.U.(*Mpcplx).Real, obj.FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, obj.FmtSharp))
344 return fmt.Sprintf("(%v+%vi)", Fconv(&v.U.(*Mpcplx).Real, obj.FmtSharp), Fconv(&v.U.(*Mpcplx).Imag, obj.FmtSharp))
347 return strconv.Quote(v.U.(string))
359 return fmt.Sprintf("<ctype=%d>", v.Ctype())
371 var etnames = []string{
385 TCOMPLEX64: "COMPLEX64",
386 TCOMPLEX128: "COMPLEX128",
399 TUNSAFEPTR: "TUNSAFEPTR",
404 func Econv(et EType) string {
405 if int(et) < len(etnames) && etnames[et] != "" {
408 return fmt.Sprintf("E-%d", et)
412 func symfmt(s *Sym, flag int) string {
413 if s.Pkg != nil && flag&obj.FmtShort == 0 {
415 case FErr: // This is for the user
416 if s.Pkg == localpkg {
420 // If the name was used by multiple packages, display the full path,
421 if s.Pkg.Name != "" && numImport[s.Pkg.Name] > 1 {
422 return fmt.Sprintf("%q.%s", s.Pkg.Path, s.Name)
424 return fmt.Sprintf("%s.%s", s.Pkg.Name, s.Name)
427 return fmt.Sprintf("%s.%s", s.Pkg.Name, s.Name)
430 if flag&obj.FmtUnsigned != 0 {
431 return fmt.Sprintf("%s.%s", s.Pkg.Name, s.Name) // dcommontype, typehash
433 return fmt.Sprintf("%s.%s", s.Pkg.Prefix, s.Name) // (methodsym), typesym, weaksym
436 if s.Name != "" && s.Name[0] == '.' {
437 Fatalf("exporting synthetic symbol %s", s.Name)
439 if s.Pkg != builtinpkg {
440 return fmt.Sprintf("@%q.%s", s.Pkg.Path, s.Name)
445 if flag&obj.FmtByte != 0 {
446 // FmtByte (hh) implies FmtShort (h)
447 // skip leading "type." in method name
449 if i := strings.LastIndex(s.Name, "."); i >= 0 {
453 // exportname needs to see the name without the prefix too.
454 if (fmtmode == FExp && !exportname(p)) || fmtmode == FDbg {
455 return fmt.Sprintf("@%q.%s", s.Pkg.Path, p)
464 var basicnames = []string{
478 TCOMPLEX64: "complex64",
479 TCOMPLEX128: "complex128",
484 TIDEAL: "untyped number",
488 func typefmt(t *Type, flag int) string {
493 if t == bytetype || t == runetype {
494 // in %-T mode collapse rune and byte with their originals.
495 if fmtmode != FTypeId {
496 return Sconv(t.Sym, obj.FmtShort)
505 // Unless the 'l' flag was specified, if the type has a name, just print that name.
506 if flag&obj.FmtLong == 0 && t.Sym != nil && t.Etype != TFIELD && t != Types[t.Etype] {
509 if flag&obj.FmtShort != 0 {
511 return fmt.Sprintf("%v·%d", Sconv(t.Sym, obj.FmtShort), t.Vargen)
513 return Sconv(t.Sym, obj.FmtShort)
516 if flag&obj.FmtUnsigned != 0 {
517 return Sconv(t.Sym, obj.FmtUnsigned)
522 if t.Sym.Pkg == localpkg && t.Vargen != 0 {
523 return fmt.Sprintf("%v·%d", t.Sym, t.Vargen)
527 return Sconv(t.Sym, 0)
530 if int(t.Etype) < len(basicnames) && basicnames[t.Etype] != "" {
532 if fmtmode == FErr && (t == idealbool || t == idealstring) {
535 return prefix + basicnames[t.Etype]
540 str := Econv(t.Etype) + "-" + typefmt(t, flag)
547 if fmtmode == FTypeId && (flag&obj.FmtShort != 0) {
548 return fmt.Sprintf("*%v", Tconv(t.Type, obj.FmtShort))
550 return fmt.Sprintf("*%v", t.Type)
554 return fmt.Sprintf("[%d]%v", t.Bound, t.Type)
557 return fmt.Sprintf("[...]%v", t.Type)
559 return fmt.Sprintf("[]%v", t.Type)
564 return fmt.Sprintf("<-chan %v", t.Type)
567 return fmt.Sprintf("chan<- %v", t.Type)
570 if t.Type != nil && t.Type.Etype == TCHAN && t.Type.Sym == nil && t.Type.Chan == Crecv {
571 return fmt.Sprintf("chan (%v)", t.Type)
573 return fmt.Sprintf("chan %v", t.Type)
576 return fmt.Sprintf("map[%v]%v", t.Down, t.Type)
580 buf.WriteString("interface {")
581 for t1 := t.Type; t1 != nil; t1 = t1.Down {
583 if exportname(t1.Sym.Name) {
584 buf.WriteString(Sconv(t1.Sym, obj.FmtShort))
586 buf.WriteString(Sconv(t1.Sym, obj.FmtUnsigned))
588 buf.WriteString(Tconv(t1.Type, obj.FmtShort))
601 if flag&obj.FmtShort != 0 {
604 if t.Thistuple != 0 {
605 buf.WriteString("method")
606 buf.WriteString(Tconv(getthisx(t), 0))
609 buf.WriteString("func")
611 buf.WriteString(Tconv(getinargx(t), 0))
620 buf.WriteString(Tconv(getoutargx(t).Type.Type, 0)) // struct->field->field's type
627 buf.WriteString(Tconv(getoutargx(t), 0))
633 // Format the bucket struct for map[x]y as map.bucket[x]y.
634 // This avoids a recursive print that generates very long names.
635 if t.Map.Bucket == t {
636 return fmt.Sprintf("map.bucket[%v]%v", t.Map.Down, t.Map.Type)
640 return fmt.Sprintf("map.hdr[%v]%v", t.Map.Down, t.Map.Type)
643 if t.Map.Hiter == t {
644 return fmt.Sprintf("map.iter[%v]%v", t.Map.Down, t.Map.Type)
647 Yyerror("unknown internal map type")
653 if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
654 for t1 := t.Type; t1 != nil; t1 = t1.Down {
655 buf.WriteString(Tconv(t1, obj.FmtShort))
657 buf.WriteString(", ")
661 for t1 := t.Type; t1 != nil; t1 = t1.Down {
662 buf.WriteString(Tconv(t1, 0))
664 buf.WriteString(", ")
670 buf.WriteString("struct {")
671 for t1 := t.Type; t1 != nil; t1 = t1.Down {
673 buf.WriteString(Tconv(t1, obj.FmtLong))
687 if flag&obj.FmtShort == 0 {
690 // Take the name from the original, lest we substituted it with ~r%d or ~b%d.
691 // ~r%d is a (formerly) unnamed result.
692 if (fmtmode == FErr || fmtmode == FExp) && t.Nname != nil {
693 if t.Nname.Orig != nil {
695 if s != nil && s.Name[0] == '~' {
696 if s.Name[1] == 'r' { // originally an unnamed result
698 } else if s.Name[1] == 'b' { // originally the blank identifier _
707 if s != nil && t.Embedded == 0 {
709 name = Nconv(t.Nname, 0)
710 } else if flag&obj.FmtLong != 0 {
711 name = Sconv(s, obj.FmtShort|obj.FmtByte) // qualify non-exported names (used on structs, not on funarg)
715 } else if fmtmode == FExp {
716 // TODO(rsc) this breaks on the eliding of unused arguments in the backend
717 // when this is fixed, the special case in dcl.go checkarglist can go.
719 // fmtstrcpy(fp, "_ ");
721 if t.Embedded != 0 && s.Pkg != nil && len(s.Pkg.Path) > 0 {
722 name = fmt.Sprintf("@%q.?", s.Pkg.Path)
731 typ = "..." + Tconv(t.Type.Type, 0)
733 typ = Tconv(t.Type, 0)
738 str = name + " " + typ
740 if flag&obj.FmtShort == 0 && t.Note != nil {
741 str += " " + strconv.Quote(*t.Note)
747 return fmt.Sprintf("undefined %v", t.Sym)
753 return "@\"unsafe\".Pointer"
755 return "unsafe.Pointer"
759 Fatalf("missing %v case during export", Econv(t.Etype))
762 // Don't know how to handle - fall back to detailed prints.
763 return fmt.Sprintf("%v <%v> %v", Econv(t.Etype), t.Sym, t.Type)
766 // Statements which may be rendered with a simplestmt as init.
767 func stmtwithinit(op Op) bool {
769 case OIF, OFOR, OSWITCH:
776 func stmtfmt(n *Node) string {
779 // some statements allow for an init, but at most one,
780 // but we may have an arbitrary number added, eg by typecheck
781 // and inlining. If it doesn't fit the syntax, emit an enclosing
782 // block starting with the init statements.
784 // if we can just say "for" n->ninit; ... then do so
785 simpleinit := n.Ninit != nil && n.Ninit.Next == nil && n.Ninit.N.Ninit == nil && stmtwithinit(n.Op)
787 // otherwise, print the inits as separate statements
788 complexinit := n.Ninit != nil && !simpleinit && (fmtmode != FErr)
790 // but if it was for if/for/switch, put in an extra surrounding block to limit the scope
791 extrablock := complexinit && stmtwithinit(n.Op)
798 f += fmt.Sprintf(" %v; ", n.Ninit)
804 switch n.Left.Class &^ PHEAP {
805 case PPARAM, PPARAMOUT, PAUTO:
806 f += fmt.Sprintf("var %v %v", n.Left, n.Left.Type)
811 f += fmt.Sprintf("var %v %v", n.Left.Sym, n.Left.Type)
815 f += fmt.Sprintf("%v %v", n.Left, n.Right)
817 f += Nconv(n.Right, 0)
820 // Don't export "v = <N>" initializing statements, hope they're always
821 // preceded by the DCL which will be re-parsed and typecheck to reproduce
822 // the "v = <N>" again.
824 if fmtmode == FExp && n.Right == nil {
828 if n.Colas && !complexinit {
829 f += fmt.Sprintf("%v := %v", n.Left, n.Right)
831 f += fmt.Sprintf("%v = %v", n.Left, n.Right)
836 if Op(n.Etype) == OADD {
837 f += fmt.Sprintf("%v++", n.Left)
839 f += fmt.Sprintf("%v--", n.Left)
844 f += fmt.Sprintf("%v %v= %v", n.Left, Oconv(int(n.Etype), obj.FmtSharp), n.Right)
847 if n.Colas && !complexinit {
848 f += fmt.Sprintf("%v := %v", Hconv(n.List, obj.FmtComma), Hconv(n.Rlist, obj.FmtComma))
853 case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV:
854 f += fmt.Sprintf("%v = %v", Hconv(n.List, obj.FmtComma), Hconv(n.Rlist, obj.FmtComma))
857 f += fmt.Sprintf("return %v", Hconv(n.List, obj.FmtComma))
860 f += fmt.Sprintf("retjmp %v", n.Sym)
863 f += fmt.Sprintf("go %v", n.Left)
866 f += fmt.Sprintf("defer %v", n.Left)
870 f += fmt.Sprintf("if %v; %v { %v }", n.Ninit.N, n.Left, n.Nbody)
872 f += fmt.Sprintf("if %v { %v }", n.Left, n.Nbody)
875 f += fmt.Sprintf(" else { %v }", n.Rlist)
879 if fmtmode == FErr { // TODO maybe only if FmtShort, same below
886 f += fmt.Sprintf(" %v;", n.Ninit.N)
887 } else if n.Right != nil {
892 f += fmt.Sprintf(" %v", n.Left)
896 f += fmt.Sprintf("; %v", n.Right)
897 } else if simpleinit {
901 f += fmt.Sprintf(" { %v }", n.Nbody)
910 f += fmt.Sprintf("for range %v { %v }", n.Right, n.Nbody)
914 f += fmt.Sprintf("for %v = range %v { %v }", Hconv(n.List, obj.FmtComma), n.Right, n.Nbody)
916 case OSELECT, OSWITCH:
918 f += fmt.Sprintf("%v statement", Oconv(int(n.Op), 0))
922 f += Oconv(int(n.Op), obj.FmtSharp)
924 f += fmt.Sprintf(" %v;", n.Ninit.N)
927 f += Nconv(n.Left, 0)
930 f += fmt.Sprintf(" { %v }", n.List)
934 f += fmt.Sprintf("case %v: %v", Hconv(n.List, obj.FmtComma), n.Nbody)
936 f += fmt.Sprintf("default: %v", n.Nbody)
945 f += fmt.Sprintf("%v %v", Oconv(int(n.Op), obj.FmtSharp), n.Left)
947 f += Oconv(int(n.Op), obj.FmtSharp)
954 f += fmt.Sprintf("%v: ", n.Left)
1049 // Statements handled by stmtfmt
1079 func exprfmt(n *Node, prec int) string {
1080 for n != nil && n.Implicit && (n.Op == OIND || n.Op == OADDR) {
1088 nprec := opprec[n.Op]
1089 if n.Op == OTYPE && n.Sym != nil {
1094 return fmt.Sprintf("(%v)", n)
1099 return fmt.Sprintf("(%v)", n.Left)
1102 return "... argument"
1105 return obj.Rconv(int(n.Reg))
1107 case OLITERAL: // this is a bit of a mess
1108 if fmtmode == FErr {
1109 if n.Orig != nil && n.Orig != n {
1110 return exprfmt(n.Orig, prec)
1113 return Sconv(n.Sym, 0)
1116 if n.Val().Ctype() == CTNIL && n.Orig != nil && n.Orig != n {
1117 return exprfmt(n.Orig, prec)
1119 if n.Type != nil && n.Type != Types[n.Type.Etype] && n.Type != idealbool && n.Type != idealstring {
1120 // Need parens when type begins with what might
1121 // be misinterpreted as a unary operator: * or <-.
1122 if Isptr[n.Type.Etype] || (n.Type.Etype == TCHAN && n.Type.Chan == Crecv) {
1123 return fmt.Sprintf("(%v)(%v)", n.Type, Vconv(n.Val(), 0))
1125 return fmt.Sprintf("%v(%v)", n.Type, Vconv(n.Val(), 0))
1129 return Vconv(n.Val(), 0)
1131 // Special case: name used as local variable in export.
1132 // _ becomes ~b%d internally; print as _ for export
1134 if (fmtmode == FExp || fmtmode == FErr) && n.Sym != nil && n.Sym.Name[0] == '~' && n.Sym.Name[1] == 'b' {
1137 if fmtmode == FExp && n.Sym != nil && !isblank(n) && n.Name.Vargen > 0 {
1138 return fmt.Sprintf("%v·%d", n.Sym, n.Name.Vargen)
1141 // Special case: explicit name of func (*T) method(...) is turned into pkg.(*T).method,
1142 // but for export, this should be rendered as (*pkg.T).meth.
1143 // These nodes have the special property that they are names with a left OTYPE and a right ONAME.
1144 if fmtmode == FExp && n.Left != nil && n.Left.Op == OTYPE && n.Right != nil && n.Right.Op == ONAME {
1145 if Isptr[n.Left.Type.Etype] {
1146 return fmt.Sprintf("(%v).%v", n.Left.Type, Sconv(n.Right.Sym, obj.FmtShort|obj.FmtByte))
1148 return fmt.Sprintf("%v.%v", n.Left.Type, Sconv(n.Right.Sym, obj.FmtShort|obj.FmtByte))
1153 case OPACK, ONONAME:
1154 return Sconv(n.Sym, 0)
1157 if n.Type == nil && n.Sym != nil {
1158 return Sconv(n.Sym, 0)
1160 return Tconv(n.Type, 0)
1164 return fmt.Sprintf("[]%v", n.Left)
1167 f += fmt.Sprintf("[]%v", n.Right)
1168 return f // happens before typecheck
1171 return fmt.Sprintf("map[%v]%v", n.Left, n.Right)
1176 return fmt.Sprintf("<-chan %v", n.Left)
1179 return fmt.Sprintf("chan<- %v", n.Left)
1182 if n.Left != nil && n.Left.Op == OTCHAN && n.Left.Sym == nil && n.Left.Etype == Crecv {
1183 return fmt.Sprintf("chan (%v)", n.Left)
1185 return fmt.Sprintf("chan %v", n.Left)
1199 if fmtmode == FErr {
1200 return "func literal"
1203 return fmt.Sprintf("%v { %v }", n.Type, n.Nbody)
1205 return fmt.Sprintf("%v { %v }", n.Type, n.Name.Param.Closure.Nbody)
1208 ptrlit := n.Right != nil && n.Right.Implicit && n.Right.Type != nil && Isptr[n.Right.Type.Etype]
1209 if fmtmode == FErr {
1210 if n.Right != nil && n.Right.Type != nil && !n.Implicit {
1212 return fmt.Sprintf("&%v literal", n.Right.Type.Type)
1214 return fmt.Sprintf("%v literal", n.Right.Type)
1218 return "composite literal"
1221 if fmtmode == FExp && ptrlit {
1222 // typecheck has overwritten OIND by OTYPE with pointer type.
1223 return fmt.Sprintf("(&%v{ %v })", n.Right.Type.Type, Hconv(n.List, obj.FmtComma))
1226 return fmt.Sprintf("(%v{ %v })", n.Right, Hconv(n.List, obj.FmtComma))
1229 if fmtmode == FExp && n.Left.Implicit {
1230 return Nconv(n.Left, 0)
1232 return fmt.Sprintf("&%v", n.Left)
1235 if fmtmode == FExp { // requires special handling of field names
1240 f += fmt.Sprintf("(%v{", n.Type)
1242 for l := n.List; l != nil; l = l.Next {
1243 f += fmt.Sprintf(" %v:%v", Sconv(l.N.Left.Sym, obj.FmtShort|obj.FmtByte), l.N.Right)
1261 case OARRAYLIT, OMAPLIT:
1262 if fmtmode == FErr {
1263 return fmt.Sprintf("%v literal", n.Type)
1265 if fmtmode == FExp && n.Implicit {
1266 return fmt.Sprintf("{ %v }", Hconv(n.List, obj.FmtComma))
1268 return fmt.Sprintf("(%v{ %v })", n.Type, Hconv(n.List, obj.FmtComma))
1271 if n.Left != nil && n.Right != nil {
1272 if fmtmode == FExp && n.Left.Type != nil && n.Left.Type.Etype == TFIELD {
1273 // requires special handling of field names
1274 return fmt.Sprintf("%v:%v", Sconv(n.Left.Sym, obj.FmtShort|obj.FmtByte), n.Right)
1276 return fmt.Sprintf("%v:%v", n.Left, n.Right)
1280 if n.Left == nil && n.Right != nil {
1281 return fmt.Sprintf(":%v", n.Right)
1283 if n.Left != nil && n.Right == nil {
1284 return fmt.Sprintf("%v:", n.Left)
1295 f += exprfmt(n.Left, nprec)
1296 if n.Right == nil || n.Right.Sym == nil {
1300 f += fmt.Sprintf(".%v", Sconv(n.Right.Sym, obj.FmtShort|obj.FmtByte))
1303 case ODOTTYPE, ODOTTYPE2:
1305 f += exprfmt(n.Left, nprec)
1307 f += fmt.Sprintf(".(%v)", n.Right)
1310 f += fmt.Sprintf(".(%v)", n.Type)
1321 f += exprfmt(n.Left, nprec)
1322 f += fmt.Sprintf("[%v]", n.Right)
1325 case OCOPY, OCOMPLEX:
1326 return fmt.Sprintf("%v(%v, %v)", Oconv(int(n.Op), obj.FmtSharp), n.Left, n.Right)
1336 if n.Type == nil || n.Type.Sym == nil {
1337 return fmt.Sprintf("(%v)(%v)", n.Type, n.Left)
1340 return fmt.Sprintf("%v(%v)", n.Type, n.Left)
1342 return fmt.Sprintf("%v(%v)", n.Type, Hconv(n.List, obj.FmtComma))
1358 return fmt.Sprintf("%v(%v)", Oconv(int(n.Op), obj.FmtSharp), n.Left)
1361 return fmt.Sprintf("%v(%v...)", Oconv(int(n.Op), obj.FmtSharp), Hconv(n.List, obj.FmtComma))
1363 return fmt.Sprintf("%v(%v)", Oconv(int(n.Op), obj.FmtSharp), Hconv(n.List, obj.FmtComma))
1365 case OCALL, OCALLFUNC, OCALLINTER, OCALLMETH, OGETG:
1367 f += exprfmt(n.Left, nprec)
1369 f += fmt.Sprintf("(%v...)", Hconv(n.List, obj.FmtComma))
1372 f += fmt.Sprintf("(%v)", Hconv(n.List, obj.FmtComma))
1375 case OMAKEMAP, OMAKECHAN, OMAKESLICE:
1376 if n.List != nil { // pre-typecheck
1377 return fmt.Sprintf("make(%v, %v)", n.Type, Hconv(n.List, obj.FmtComma))
1380 return fmt.Sprintf("make(%v, %v, %v)", n.Type, n.Left, n.Right)
1382 if n.Left != nil && (n.Op == OMAKESLICE || !isideal(n.Left.Type)) {
1383 return fmt.Sprintf("make(%v, %v)", n.Type, n.Left)
1385 return fmt.Sprintf("make(%v)", n.Type)
1396 if n.Left.Op == n.Op {
1397 f += fmt.Sprintf("%v ", Oconv(int(n.Op), obj.FmtSharp))
1399 f += Oconv(int(n.Op), obj.FmtSharp)
1401 f += exprfmt(n.Left, nprec+1)
1426 f += exprfmt(n.Left, nprec)
1428 f += fmt.Sprintf(" %v ", Oconv(int(n.Op), obj.FmtSharp))
1429 f += exprfmt(n.Right, nprec+1)
1434 for l := n.List; l != nil; l = l.Next {
1438 f += exprfmt(l.N, nprec)
1443 case OCMPSTR, OCMPIFACE:
1445 f += exprfmt(n.Left, nprec)
1446 // TODO(marvin): Fix Node.EType type union.
1447 f += fmt.Sprintf(" %v ", Oconv(int(n.Etype), obj.FmtSharp))
1448 f += exprfmt(n.Right, nprec+1)
1452 return fmt.Sprintf("<node %v>", Oconv(int(n.Op), 0))
1455 func nodefmt(n *Node, flag int) string {
1458 // we almost always want the original, except in export mode for literals
1459 // this saves the importer some work, and avoids us having to redo some
1460 // special casing for package unsafe
1461 if (fmtmode != FExp || n.Op != OLITERAL) && n.Orig != nil {
1465 if flag&obj.FmtLong != 0 && t != nil {
1466 if t.Etype == TNIL {
1469 return fmt.Sprintf("%v (type %v)", n, t)
1473 // TODO inlining produces expressions with ninits. we can't print these yet.
1475 if opprec[n.Op] < 0 {
1479 return exprfmt(n, 0)
1484 func indent(buf *bytes.Buffer) {
1485 buf.WriteString("\n")
1486 for i := 0; i < dumpdepth; i++ {
1487 buf.WriteString(". ")
1491 func nodedump(n *Node, flag int) string {
1496 recur := flag&obj.FmtShort == 0
1498 var buf bytes.Buffer
1502 buf.WriteString("...")
1507 fmt.Fprintf(&buf, "%v-init%v", Oconv(int(n.Op), 0), n.Ninit)
1514 fmt.Fprintf(&buf, "%v%v", Oconv(int(n.Op), 0), Jconv(n, 0))
1516 case OREGISTER, OINDREG:
1517 fmt.Fprintf(&buf, "%v-%v%v", Oconv(int(n.Op), 0), obj.Rconv(int(n.Reg)), Jconv(n, 0))
1520 fmt.Fprintf(&buf, "%v-%v%v", Oconv(int(n.Op), 0), Vconv(n.Val(), 0), Jconv(n, 0))
1522 case ONAME, ONONAME:
1524 fmt.Fprintf(&buf, "%v-%v%v", Oconv(int(n.Op), 0), n.Sym, Jconv(n, 0))
1526 fmt.Fprintf(&buf, "%v%v", Oconv(int(n.Op), 0), Jconv(n, 0))
1528 if recur && n.Type == nil && n.Name.Param.Ntype != nil {
1530 fmt.Fprintf(&buf, "%v-ntype%v", Oconv(int(n.Op), 0), n.Name.Param.Ntype)
1534 fmt.Fprintf(&buf, "%v-%v%v", Oconv(int(n.Op), 0), Oconv(int(n.Etype), 0), Jconv(n, 0))
1537 fmt.Fprintf(&buf, "%v %v%v type=%v", Oconv(int(n.Op), 0), n.Sym, Jconv(n, 0), n.Type)
1538 if recur && n.Type == nil && n.Name.Param.Ntype != nil {
1540 fmt.Fprintf(&buf, "%v-ntype%v", Oconv(int(n.Op), 0), n.Name.Param.Ntype)
1544 if n.Sym != nil && n.Op != ONAME {
1545 fmt.Fprintf(&buf, " %v", n.Sym)
1549 fmt.Fprintf(&buf, " %v", n.Type)
1554 buf.WriteString(Nconv(n.Left, 0))
1557 buf.WriteString(Nconv(n.Right, 0))
1561 fmt.Fprintf(&buf, "%v-list%v", Oconv(int(n.Op), 0), n.List)
1566 fmt.Fprintf(&buf, "%v-rlist%v", Oconv(int(n.Op), 0), n.Rlist)
1571 fmt.Fprintf(&buf, "%v-body%v", Oconv(int(n.Op), 0), n.Nbody)
1578 func (s *Sym) String() string {
1583 // Flags: "%hS" suppresses qualifying with package
1584 func Sconv(s *Sym, flag int) string {
1585 if flag&obj.FmtLong != 0 {
1598 sm := setfmode(&flag)
1599 str := symfmt(s, flag)
1605 func (t *Type) String() string {
1610 // Flags: 'l' print definition, not name
1611 // 'h' omit 'func' and receiver from function types, short type names
1612 // 'u' package name, not prefix (FTypeId mode, sticky)
1613 func Tconv(t *Type, flag int) string {
1624 sm := setfmode(&flag)
1626 if fmtmode == FTypeId && (sf&obj.FmtUnsigned != 0) {
1630 flag |= obj.FmtUnsigned
1633 str := typefmt(t, flag)
1635 if fmtmode == FTypeId && (sf&obj.FmtUnsigned != 0) {
1645 func (n *Node) String() string {
1650 // Flags: 'l' suffix with "(type %T)" where possible
1651 // '+h' in debug mode, don't recurse, no multiline output
1652 func Nconv(n *Node, flag int) string {
1657 sm := setfmode(&flag)
1662 str = nodefmt(n, flag)
1666 str = nodedump(n, flag)
1670 Fatalf("unhandled %%N mode")
1678 func (l *NodeList) String() string {
1682 // Fmt '%H': NodeList.
1683 // Flags: all those of %N plus ',': separate with comma's instead of semicolons.
1684 func Hconv(l *NodeList, flag int) string {
1685 if l == nil && fmtmode == FDbg {
1690 sm := setfmode(&flag)
1692 if fmtmode == FDbg {
1694 } else if flag&obj.FmtComma != 0 {
1698 var buf bytes.Buffer
1699 for ; l != nil; l = l.Next {
1700 buf.WriteString(Nconv(l.N, 0))
1702 buf.WriteString(sep)
1711 func dumplist(s string, l *NodeList) {
1712 fmt.Printf("%s%v\n", s, Hconv(l, obj.FmtSign))
1715 func Dump(s string, n *Node) {
1716 fmt.Printf("%s [%p]%v\n", s, n, Nconv(n, obj.FmtSign))