-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmt.go
1820 lines (1571 loc) · 37.8 KB
/
fmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import (
"bytes"
"fmt"
"github.com/glycerine/golang-repl/cmd/avail/obj"
"strconv"
"strings"
"unicode/utf8"
)
// A FmtFlag value is a set of flags (or 0).
// They control how the Xconv functions format their values.
// See the respective function's documentation for details.
type FmtFlag int
const (
FmtWidth FmtFlag = 1 << iota
FmtLeft // "-"
FmtSharp // "#"
FmtSign // "+"
FmtUnsigned // "u"
FmtShort // "h"
FmtLong // "l"
FmtComma // ","
FmtByte // "hh"
FmtBody // for printing export bodies
)
//
// Format conversions
// %L int Line numbers
//
// %E int etype values (aka 'Kind')
//
// %O int Node Opcodes
// Flags: "%#O": print go syntax. (automatic unless fmtmode == FDbg)
//
// %J Node* Node details
// Flags: "%hJ" suppresses things not relevant until walk.
//
// %V Val* Constant values
//
// %S Sym* Symbols
// Flags: +,- #: mode (see below)
// "%hS" unqualified identifier in any mode
// "%hhS" in export mode: unqualified identifier if exported, qualified if not
//
// %T Type* Types
// Flags: +,- #: mode (see below)
// 'l' definition instead of name.
// 'h' omit "func" and receiver in function types
// 'u' (only in -/Sym mode) print type identifiers wit package name instead of prefix.
//
// %N Node* Nodes
// Flags: +,- #: mode (see below)
// 'h' (only in +/debug mode) suppress recursion
// 'l' (only in Error mode) print "foo (type Bar)"
//
// %H Nodes Nodes
// Flags: those of %N
// ',' separate items with ',' instead of ';'
//
// In mparith2.go and mparith3.go:
// %B Mpint* Big integers
// %F Mpflt* Big floats
//
// %S, %T and %N obey use the following flags to set the format mode:
const (
FErr = iota
FDbg
FExp
FTypeId
)
var fmtmode int = FErr
var fmtpkgpfx int // %uT stickyness
var fmtbody bool
//
// E.g. for %S: %+S %#S %-S print an identifier properly qualified for debug/export/internal mode.
//
// The mode flags +, - and # are sticky, meaning they persist through
// recursions of %N, %T and %S, but not the h and l flags. The u flag is
// sticky only on %T recursions and only used in %-/Sym mode.
//
// Useful format combinations:
//
// %+N %+H multiline recursive debug dump of node/nodelist
// %+hN %+hH non recursive debug dump
//
// %#N %#T export format
// %#lT type definition instead of name
// %#hT omit"func" and receiver in function signature
//
// %lN "foo (type Bar)" for error messages
//
// %-T type identifiers
// %-hT type identifiers without "func" and arg names in type signatures (methodsym)
// %-uT type identifiers with package name instead of prefix (typesym, dcommontype, typehash)
//
func setfmode(flags *FmtFlag) (fm int, fb bool) {
fm = fmtmode
fb = fmtbody
if *flags&FmtSign != 0 {
fmtmode = FDbg
} else if *flags&FmtSharp != 0 {
fmtmode = FExp
} else if *flags&FmtLeft != 0 {
fmtmode = FTypeId
}
if *flags&FmtBody != 0 {
fmtbody = true
}
*flags &^= (FmtSharp | FmtLeft | FmtSign | FmtBody)
return
}
// Fmt "%L": Linenumbers
var goopnames = []string{
OADDR: "&",
OADD: "+",
OADDSTR: "+",
OANDAND: "&&",
OANDNOT: "&^",
OAND: "&",
OAPPEND: "append",
OAS: "=",
OAS2: "=",
OBREAK: "break",
OCALL: "function call", // not actual syntax
OCAP: "cap",
OCASE: "case",
OCLOSE: "close",
OCOMPLEX: "complex",
OCOM: "^",
OCONTINUE: "continue",
OCOPY: "copy",
ODEC: "--",
ODELETE: "delete",
ODEFER: "defer",
ODIV: "/",
OEQ: "==",
OFALL: "fallthrough",
OFOR: "for",
OGE: ">=",
OGOTO: "goto",
OGT: ">",
OIF: "if",
OIMAG: "imag",
OINC: "++",
OIND: "*",
OLEN: "len",
OLE: "<=",
OLSH: "<<",
OLT: "<",
OMAKE: "make",
OMINUS: "-",
OMOD: "%",
OMUL: "*",
ONEW: "new",
ONE: "!=",
ONOT: "!",
OOROR: "||",
OOR: "|",
OPANIC: "panic",
OPLUS: "+",
OPRINTN: "println",
OPRINT: "print",
ORANGE: "range",
OREAL: "real",
ORECV: "<-",
ORECOVER: "recover",
ORETURN: "return",
ORSH: ">>",
OSELECT: "select",
OSEND: "<-",
OSUB: "-",
OSWITCH: "switch",
OXOR: "^",
OXFALL: "fallthrough",
}
func (o Op) String() string {
return oconv(o, 0)
}
func (o Op) GoString() string {
return oconv(o, FmtSharp)
}
func oconv(o Op, flag FmtFlag) string {
if (flag&FmtSharp != 0) || fmtmode != FDbg {
if o >= 0 && int(o) < len(goopnames) && goopnames[o] != "" {
return goopnames[o]
}
}
if o >= 0 && int(o) < len(opnames) && opnames[o] != "" {
return opnames[o]
}
return fmt.Sprintf("O-%d", o)
}
var classnames = []string{
"Pxxx",
"PEXTERN",
"PAUTO",
"PAUTOHEAP",
"PPARAM",
"PPARAMOUT",
"PFUNC",
}
// Fmt "%J": Node details.
func jconv(n *Node, flag FmtFlag) string {
var buf bytes.Buffer
c := flag & FmtShort
if c == 0 && n.Ullman != 0 {
fmt.Fprintf(&buf, " u(%d)", n.Ullman)
}
if c == 0 && n.Addable {
fmt.Fprintf(&buf, " a(%v)", n.Addable)
}
if c == 0 && n.Name != nil && n.Name.Vargen != 0 {
fmt.Fprintf(&buf, " g(%d)", n.Name.Vargen)
}
if n.Lineno != 0 {
fmt.Fprintf(&buf, " l(%d)", n.Lineno)
}
if c == 0 && n.Xoffset != BADWIDTH {
//jea fmt.Fprintf(&buf, " x(%d%+d)", n.Xoffset, stkdelta[n])
}
if n.Class != 0 {
if int(n.Class) < len(classnames) {
fmt.Fprintf(&buf, " class(%s)", classnames[n.Class])
} else {
fmt.Fprintf(&buf, " class(%d?)", n.Class)
}
}
if n.Colas {
fmt.Fprintf(&buf, " colas(%v)", n.Colas)
}
if n.Name != nil && n.Name.Funcdepth != 0 {
fmt.Fprintf(&buf, " f(%d)", n.Name.Funcdepth)
}
if n.Func != nil && n.Func.Depth != 0 {
fmt.Fprintf(&buf, " ff(%d)", n.Func.Depth)
}
/* jea
switch n.Esc {
case EscUnknown:
break
case EscHeap:
buf.WriteString(" esc(h)")
case EscScope:
buf.WriteString(" esc(s)")
case EscNone:
buf.WriteString(" esc(no)")
case EscNever:
if c == 0 {
buf.WriteString(" esc(N)")
}
default:
fmt.Fprintf(&buf, " esc(%d)", n.Esc)
}
if e, ok := n.Opt().(*NodeEscState); ok && e.Escloopdepth != 0 {
fmt.Fprintf(&buf, " ld(%d)", e.Escloopdepth)
}
*/
if c == 0 && n.Typecheck != 0 {
fmt.Fprintf(&buf, " tc(%d)", n.Typecheck)
}
if c == 0 && n.Dodata != 0 {
fmt.Fprintf(&buf, " dd(%d)", n.Dodata)
}
if n.Isddd {
fmt.Fprintf(&buf, " isddd(%v)", n.Isddd)
}
if n.Implicit {
fmt.Fprintf(&buf, " implicit(%v)", n.Implicit)
}
if n.Embedded != 0 {
fmt.Fprintf(&buf, " embedded(%d)", n.Embedded)
}
if n.Addrtaken {
buf.WriteString(" addrtaken")
}
if n.Assigned {
buf.WriteString(" assigned")
}
if n.Bounded {
buf.WriteString(" bounded")
}
if n.NonNil {
buf.WriteString(" nonnil")
}
if c == 0 && n.Used {
fmt.Fprintf(&buf, " used(%v)", n.Used)
}
return buf.String()
}
// Fmt "%V": Values
func vconv(v Val, flag FmtFlag) string {
switch u := v.U.(type) {
case *Mpint:
if !u.Rune {
if (flag&FmtSharp != 0) || fmtmode == FExp {
return bconv(u, FmtSharp)
}
return bconv(u, 0)
}
x := u.Int64()
if ' ' <= x && x < utf8.RuneSelf && x != '\\' && x != '\'' {
return fmt.Sprintf("'%c'", int(x))
}
if 0 <= x && x < 1<<16 {
return fmt.Sprintf("'\\u%04x'", uint(int(x)))
}
if 0 <= x && x <= utf8.MaxRune {
return fmt.Sprintf("'\\U%08x'", uint64(x))
}
return fmt.Sprintf("('\\x00' + %v)", u)
case *Mpflt:
if (flag&FmtSharp != 0) || fmtmode == FExp {
return fconv(u, 0)
}
return fconv(u, FmtSharp)
case *Mpcplx:
if (flag&FmtSharp != 0) || fmtmode == FExp {
return fmt.Sprintf("(%v+%vi)", &u.Real, &u.Imag)
}
if v.U.(*Mpcplx).Real.CmpFloat64(0) == 0 {
return fmt.Sprintf("%vi", fconv(&u.Imag, FmtSharp))
}
if v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0 {
return fconv(&u.Real, FmtSharp)
}
if v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0 {
return fmt.Sprintf("(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
}
return fmt.Sprintf("(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
case string:
return strconv.Quote(u)
case bool:
if u {
return "true"
}
return "false"
case *NilVal:
return "nil"
}
return fmt.Sprintf("<ctype=%d>", v.Ctype())
}
/*
s%,%,\n%g
s%\n+%\n%g
s%^[ ]*T%%g
s%,.*%%g
s%.+% [T&] = "&",%g
s%^ ........*\]%&~%g
s%~ %%g
*/
var etnames = []string{
Txxx: "Txxx",
TINT: "INT",
TUINT: "UINT",
TINT8: "INT8",
TUINT8: "UINT8",
TINT16: "INT16",
TUINT16: "UINT16",
TINT32: "INT32",
TUINT32: "UINT32",
TINT64: "INT64",
TUINT64: "UINT64",
TUINTPTR: "UINTPTR",
TFLOAT32: "FLOAT32",
TFLOAT64: "FLOAT64",
TCOMPLEX64: "COMPLEX64",
TCOMPLEX128: "COMPLEX128",
TBOOL: "BOOL",
TPTR32: "PTR32",
TPTR64: "PTR64",
TFUNC: "FUNC",
TARRAY: "ARRAY",
TSLICE: "SLICE",
TSTRUCT: "STRUCT",
TCHAN: "CHAN",
TMAP: "MAP",
TINTER: "INTER",
TFORW: "FORW",
TSTRING: "STRING",
TUNSAFEPTR: "TUNSAFEPTR",
TANY: "ANY",
TIDEAL: "TIDEAL",
TNIL: "TNIL",
TBLANK: "TBLANK",
TFUNCARGS: "TFUNCARGS",
TCHANARGS: "TCHANARGS",
TINTERMETH: "TINTERMETH",
TDDDFIELD: "TDDDFIELD",
}
func (et EType) String() string {
if int(et) < len(etnames) && etnames[et] != "" {
return etnames[et]
}
return fmt.Sprintf("E-%d", et)
}
// Fmt "%S": syms
func symfmt(s *Sym, flag FmtFlag) string {
if s.Pkg != nil && flag&FmtShort == 0 {
switch fmtmode {
case FErr: // This is for the user
if s.Pkg == builtinpkg || s.Pkg == localpkg {
return s.Name
}
// If the name was used by multiple packages, display the full path,
if s.Pkg.Name != "" { //jea && numImport[s.Pkg.Name] > 1 {
return fmt.Sprintf("%q.%s", s.Pkg.Path, s.Name)
}
return s.Pkg.Name + "." + s.Name
case FDbg:
return s.Pkg.Name + "." + s.Name
case FTypeId:
if flag&FmtUnsigned != 0 {
return s.Pkg.Name + "." + s.Name // dcommontype, typehash
}
return s.Pkg.Prefix + "." + s.Name // (methodsym), typesym, weaksym
case FExp:
if s.Name != "" && s.Name[0] == '.' {
Fatalf("exporting synthetic symbol %s", s.Name)
}
if s.Pkg != builtinpkg {
return fmt.Sprintf("@%q.%s", s.Pkg.Path, s.Name)
}
}
}
if flag&FmtByte != 0 {
// FmtByte (hh) implies FmtShort (h)
// skip leading "type." in method name
p := s.Name
if i := strings.LastIndex(s.Name, "."); i >= 0 {
p = s.Name[i+1:]
}
// exportname needs to see the name without the prefix too.
if (fmtmode == FExp && !exportname(p)) || fmtmode == FDbg {
return fmt.Sprintf("@%q.%s", s.Pkg.Path, p)
}
return p
}
return s.Name
}
var basicnames = []string{
TINT: "int",
TUINT: "uint",
TINT8: "int8",
TUINT8: "uint8",
TINT16: "int16",
TUINT16: "uint16",
TINT32: "int32",
TUINT32: "uint32",
TINT64: "int64",
TUINT64: "uint64",
TUINTPTR: "uintptr",
TFLOAT32: "float32",
TFLOAT64: "float64",
TCOMPLEX64: "complex64",
TCOMPLEX128: "complex128",
TBOOL: "bool",
TANY: "any",
TSTRING: "string",
TNIL: "nil",
TIDEAL: "untyped number",
TBLANK: "blank",
}
func typefmt(t *Type, flag FmtFlag) string {
if t == nil {
return "<T>"
}
if t == bytetype || t == runetype {
// in %-T mode collapse rune and byte with their originals.
if fmtmode != FTypeId {
return sconv(t.Sym, FmtShort)
}
t = Types[t.Etype]
}
if t == errortype {
return "error"
}
// Unless the 'l' flag was specified, if the type has a name, just print that name.
if flag&FmtLong == 0 && t.Sym != nil && t != Types[t.Etype] {
switch fmtmode {
case FTypeId:
if flag&FmtShort != 0 {
if t.Vargen != 0 {
return fmt.Sprintf("%v·%d", sconv(t.Sym, FmtShort), t.Vargen)
}
return sconv(t.Sym, FmtShort)
}
if flag&FmtUnsigned != 0 {
return sconv(t.Sym, FmtUnsigned)
}
fallthrough
case FExp:
if t.Sym.Pkg == localpkg && t.Vargen != 0 {
return fmt.Sprintf("%v·%d", t.Sym, t.Vargen)
}
}
return sconv(t.Sym, 0)
}
if int(t.Etype) < len(basicnames) && basicnames[t.Etype] != "" {
prefix := ""
if fmtmode == FErr && (t == idealbool || t == idealstring) {
prefix = "untyped "
}
return prefix + basicnames[t.Etype]
}
if fmtmode == FDbg {
fmtmode = 0
str := t.Etype.String() + "-" + typefmt(t, flag)
fmtmode = FDbg
return str
}
switch t.Etype {
case TPTR32, TPTR64:
if fmtmode == FTypeId && (flag&FmtShort != 0) {
return "*" + Tconv(t.Elem(), FmtShort)
}
return "*" + t.Elem().String()
case TARRAY:
if t.isDDDArray() {
return "[...]" + t.Elem().String()
}
return fmt.Sprintf("[%d]%v", t.NumElem(), t.Elem())
case TSLICE:
return "[]" + t.Elem().String()
case TCHAN:
switch t.ChanDir() {
case Crecv:
return "<-chan " + t.Elem().String()
case Csend:
return "chan<- " + t.Elem().String()
}
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv {
return "chan (" + t.Elem().String() + ")"
}
return "chan " + t.Elem().String()
case TMAP:
return "map[" + t.Key().String() + "]" + t.Val().String()
case TINTER:
var buf bytes.Buffer
buf.WriteString("interface {")
for i, f := range t.Fields().Slice() {
if i != 0 {
buf.WriteString(";")
}
buf.WriteString(" ")
switch {
case f.Sym == nil:
// Check first that a symbol is defined for this type.
// Wrong interface definitions may have types lacking a symbol.
break
case exportname(f.Sym.Name):
buf.WriteString(sconv(f.Sym, FmtShort))
default:
buf.WriteString(sconv(f.Sym, FmtUnsigned))
}
buf.WriteString(Tconv(f.Type, FmtShort))
}
if t.NumFields() != 0 {
buf.WriteString(" ")
}
buf.WriteString("}")
return buf.String()
case TFUNC:
var buf bytes.Buffer
if flag&FmtShort != 0 {
// no leading func
} else {
if t.Recv() != nil {
buf.WriteString("method")
buf.WriteString(Tconv(t.Recvs(), 0))
buf.WriteString(" ")
}
buf.WriteString("func")
}
buf.WriteString(Tconv(t.Params(), 0))
switch t.Results().NumFields() {
case 0:
break
case 1:
if fmtmode != FExp {
buf.WriteString(" ")
buf.WriteString(Tconv(t.Results().Field(0).Type, 0)) // struct->field->field's type
break
}
fallthrough
default:
buf.WriteString(" ")
buf.WriteString(Tconv(t.Results(), 0))
}
return buf.String()
case TSTRUCT:
if m := t.StructType().Map; m != nil {
mt := m.MapType()
// Format the bucket struct for map[x]y as map.bucket[x]y.
// This avoids a recursive print that generates very long names.
if mt.Bucket == t {
return "map.bucket[" + m.Key().String() + "]" + m.Val().String()
}
if mt.Hmap == t {
return "map.hdr[" + m.Key().String() + "]" + m.Val().String()
}
if mt.Hiter == t {
return "map.iter[" + m.Key().String() + "]" + m.Val().String()
}
Yyerror("unknown internal map type")
}
var buf bytes.Buffer
if t.IsFuncArgStruct() {
buf.WriteString("(")
var flag1 FmtFlag
if fmtmode == FTypeId || fmtmode == FErr { // no argument names on function signature, and no "noescape"/"nosplit" tags
flag1 = FmtShort
}
for i, f := range t.Fields().Slice() {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(Fldconv(f, flag1))
}
buf.WriteString(")")
} else {
buf.WriteString("struct {")
for i, f := range t.Fields().Slice() {
if i != 0 {
buf.WriteString(";")
}
buf.WriteString(" ")
buf.WriteString(Fldconv(f, FmtLong))
}
if t.NumFields() != 0 {
buf.WriteString(" ")
}
buf.WriteString("}")
}
return buf.String()
case TFORW:
if t.Sym != nil {
return "undefined " + t.Sym.String()
}
return "undefined"
case TUNSAFEPTR:
if fmtmode == FExp {
return "@\"unsafe\".Pointer"
}
return "unsafe.Pointer"
case TDDDFIELD:
if fmtmode == FExp {
Fatalf("cannot use TDDDFIELD with old exporter")
}
return fmt.Sprintf("%v <%v> %v", t.Etype, t.Sym, t.DDDField())
case Txxx:
return "Txxx"
}
if fmtmode == FExp {
Fatalf("missing %v case during export", t.Etype)
}
// Don't know how to handle - fall back to detailed prints.
return fmt.Sprintf("%v <%v> %v", t.Etype, t.Sym, t.Elem())
}
// Statements which may be rendered with a simplestmt as init.
func stmtwithinit(op Op) bool {
switch op {
case OIF, OFOR, OSWITCH:
return true
}
return false
}
func stmtfmt(n *Node) string {
var f string
// some statements allow for an init, but at most one,
// but we may have an arbitrary number added, eg by typecheck
// and inlining. If it doesn't fit the syntax, emit an enclosing
// block starting with the init statements.
// if we can just say "for" n->ninit; ... then do so
simpleinit := n.Ninit.Len() == 1 && n.Ninit.First().Ninit.Len() == 0 && stmtwithinit(n.Op)
// otherwise, print the inits as separate statements
complexinit := n.Ninit.Len() != 0 && !simpleinit && (fmtmode != FErr)
// but if it was for if/for/switch, put in an extra surrounding block to limit the scope
extrablock := complexinit && stmtwithinit(n.Op)
if extrablock {
f += "{"
}
if complexinit {
f += fmt.Sprintf(" %v; ", n.Ninit)
}
switch n.Op {
case ODCL:
if fmtmode == FExp {
switch n.Left.Class {
case PPARAM, PPARAMOUT, PAUTO, PAUTOHEAP:
f += fmt.Sprintf("var %v %v", n.Left, n.Left.Type)
goto ret
}
}
f += fmt.Sprintf("var %v %v", n.Left.Sym, n.Left.Type)
case ODCLFIELD:
if n.Left != nil {
f += fmt.Sprintf("%v %v", n.Left, n.Right)
} else {
f += Nconv(n.Right, 0)
}
// Don't export "v = <N>" initializing statements, hope they're always
// preceded by the DCL which will be re-parsed and typechecked to reproduce
// the "v = <N>" again.
case OAS, OASWB:
if fmtmode == FExp && n.Right == nil {
break
}
if n.Colas && !complexinit {
f += fmt.Sprintf("%v := %v", n.Left, n.Right)
} else {
f += fmt.Sprintf("%v = %v", n.Left, n.Right)
}
case OASOP:
if n.Implicit {
if Op(n.Etype) == OADD {
f += fmt.Sprintf("%v++", n.Left)
} else {
f += fmt.Sprintf("%v--", n.Left)
}
break
}
f += fmt.Sprintf("%v %#v= %v", n.Left, Op(n.Etype), n.Right)
case OAS2:
if n.Colas && !complexinit {
f += fmt.Sprintf("%v := %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
break
}
fallthrough
case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV:
f += fmt.Sprintf("%v = %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
case ORETURN:
f += fmt.Sprintf("return %v", hconv(n.List, FmtComma))
case ORETJMP:
f += fmt.Sprintf("retjmp %v", n.Sym)
case OPROC:
f += fmt.Sprintf("go %v", n.Left)
case ODEFER:
f += fmt.Sprintf("defer %v", n.Left)
case OIF:
if simpleinit {
f += fmt.Sprintf("if %v; %v { %v }", n.Ninit.First(), n.Left, n.Nbody)
} else {
f += fmt.Sprintf("if %v { %v }", n.Left, n.Nbody)
}
if n.Rlist.Len() != 0 {
f += fmt.Sprintf(" else { %v }", n.Rlist)
}
case OFOR:
if fmtmode == FErr { // TODO maybe only if FmtShort, same below
f += "for loop"
break
}
f += "for"
if simpleinit {
f += fmt.Sprintf(" %v;", n.Ninit.First())
} else if n.Right != nil {
f += " ;"
}
if n.Left != nil {
f += fmt.Sprintf(" %v", n.Left)
}
if n.Right != nil {
f += fmt.Sprintf("; %v", n.Right)
} else if simpleinit {
f += ";"
}
f += fmt.Sprintf(" { %v }", n.Nbody)
case ORANGE:
if fmtmode == FErr {
f += "for loop"
break
}
if n.List.Len() == 0 {
f += fmt.Sprintf("for range %v { %v }", n.Right, n.Nbody)
break
}
f += fmt.Sprintf("for %v = range %v { %v }", hconv(n.List, FmtComma), n.Right, n.Nbody)
case OSELECT, OSWITCH:
if fmtmode == FErr {
f += fmt.Sprintf("%v statement", n.Op)
break
}
f += n.Op.GoString() // %#v
if simpleinit {
f += fmt.Sprintf(" %v;", n.Ninit.First())
}
if n.Left != nil {
f += fmt.Sprintf(" %s ", Nconv(n.Left, 0))
}
f += fmt.Sprintf(" { %v }", n.List)
case OCASE, OXCASE:
if n.List.Len() != 0 {
f += fmt.Sprintf("case %v: %v", hconv(n.List, FmtComma), n.Nbody)
} else {
f += fmt.Sprintf("default: %v", n.Nbody)
}
case OBREAK,
OCONTINUE,
OGOTO,
OFALL,
OXFALL:
if n.Left != nil {
f += fmt.Sprintf("%#v %v", n.Op, n.Left)
} else {
f += n.Op.GoString() // %#v
}
case OEMPTY:
break
case OLABEL:
f += fmt.Sprintf("%v: ", n.Left)
}
ret:
if extrablock {
f += "}"
}
return f
}
var opprec = []int{
OAPPEND: 8,
OARRAYBYTESTR: 8,
OARRAYLIT: 8,
OARRAYRUNESTR: 8,
OCALLFUNC: 8,
OCALLINTER: 8,
OCALLMETH: 8,
OCALL: 8,
OCAP: 8,
OCLOSE: 8,
OCONVIFACE: 8,
OCONVNOP: 8,
OCONV: 8,
OCOPY: 8,
ODELETE: 8,
OGETG: 8,
OLEN: 8,
OLITERAL: 8,
OMAKESLICE: 8,
OMAKE: 8,
OMAPLIT: 8,
ONAME: 8,
ONEW: 8,
ONONAME: 8,
OPACK: 8,
OPANIC: 8,
OPAREN: 8,
OPRINTN: 8,
OPRINT: 8,
ORUNESTR: 8,
OSTRARRAYBYTE: 8,
OSTRARRAYRUNE: 8,
OSTRUCTLIT: 8,
OTARRAY: 8,
OTCHAN: 8,
OTFUNC: 8,
OTINTER: 8,
OTMAP: 8,
OTSTRUCT: 8,
OINDEXMAP: 8,
OINDEX: 8,
OSLICE: 8,