1 // Copyright 2009 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.
4 5 // This file implements printing of AST nodes; specifically
6 // expressions, statements, declarations, and files. It uses
7 // the print functionality implemented in printer.go.
8 9 package printer
10 11 import (
12 "go/ast"
13 "go/token"
14 "math"
15 "strconv"
16 "bytes"
17 "unicode"
18 "unicode/utf8"
19 )
20 21 // Formatting issues:
22 // - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
23 // when the comment spans multiple lines; if such a comment is just two lines, formatting is
24 // not idempotent
25 // - formatting of expression lists
26 // - should use blank instead of tab to separate one-line function bodies from
27 // the function header unless there is a group of consecutive one-liners
28 29 // ----------------------------------------------------------------------------
30 // Common AST nodes.
31 32 // Print as many newlines as necessary (but at least min newlines) to get to
33 // the current line. ws is printed before the first line break. If newSection
34 // is set, the first line break is printed as formfeed. Returns 0 if no line
35 // breaks were printed, returns 1 if there was exactly one newline printed,
36 // and returns a value > 1 if there was a formfeed or more than one newline
37 // printed.
38 //
39 // TODO(gri): linebreak may add too many lines if the next statement at "line"
40 // is preceded by comments because the computation of n assumes
41 // the current position before the comment and the target position
42 // after the comment. Thus, after interspersing such comments, the
43 // space taken up by them is not considered to reduce the number of
44 // linebreaks. At the moment there is no easy way to know about
45 // future (not yet interspersed) comments in this function.
46 func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (nbreaks int) {
47 n := max(nlimit(line-p.pos.Line), min)
48 if n > 0 {
49 p.print(ws)
50 if newSection {
51 p.print(formfeed)
52 n--
53 nbreaks = 2
54 }
55 nbreaks += n
56 for ; n > 0; n-- {
57 p.print(newline)
58 }
59 }
60 return
61 }
62 63 // setComment sets g as the next comment if g != nil and if node comments
64 // are enabled - this mode is used when printing source code fragments such
65 // as exports only. It assumes that there is no pending comment in p.comments
66 // and at most one pending comment in the p.comment cache.
67 func (p *printer) setComment(g *ast.CommentGroup) {
68 if g == nil || !p.useNodeComments {
69 return
70 }
71 if p.comments == nil {
72 // initialize p.comments lazily
73 p.comments = []*ast.CommentGroup{:1}
74 } else if p.cindex < len(p.comments) {
75 // for some reason there are pending comments; this
76 // should never happen - handle gracefully and flush
77 // all comments up to g, ignore anything after that
78 p.flush(p.posFor(g.List[0].Pos()), token.ILLEGAL)
79 p.comments = p.comments[0:1]
80 // in debug mode, report error
81 p.internalError("setComment found pending comments")
82 }
83 p.comments[0] = g
84 p.cindex = 0
85 // don't overwrite any pending comment in the p.comment cache
86 // (there may be a pending comment when a line comment is
87 // immediately followed by a lead comment with no other
88 // tokens between)
89 if p.commentOffset == infinity {
90 p.nextComment() // get comment ready for use
91 }
92 }
93 94 type exprListMode uint
95 96 const (
97 commaTerm exprListMode = 1 << iota // list is optionally terminated by a comma
98 noIndent // no extra indentation in multi-line lists
99 )
100 101 // If indent is set, a multi-line identifier list is indented after the
102 // first linebreak encountered.
103 func (p *printer) identList(list []*ast.Ident, indent bool) {
104 // convert into an expression list so we can re-use exprList formatting
105 xlist := []ast.Expr{:len(list)}
106 for i, x := range list {
107 xlist[i] = x
108 }
109 var mode exprListMode
110 if !indent {
111 mode = noIndent
112 }
113 p.exprList(token.NoPos, xlist, 1, mode, token.NoPos, false)
114 }
115 116 const filteredMsg = "contains filtered or unexported fields"
117 118 // Print a list of expressions. If the list spans multiple
119 // source lines, the original line breaks are respected between
120 // expressions.
121 //
122 // TODO(gri) Consider rewriting this to be independent of []ast.Expr
123 // so that we can use the algorithm for any kind of list
124 //
125 // (e.g., pass list via a channel over which to range).
126 func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exprListMode, next0 token.Pos, isIncomplete bool) {
127 if len(list) == 0 {
128 if isIncomplete {
129 prev := p.posFor(prev0)
130 next := p.posFor(next0)
131 if prev.IsValid() && prev.Line == next.Line {
132 p.print("/* " + filteredMsg + " */")
133 } else {
134 p.print(newline)
135 p.print(indent, "// "+filteredMsg, unindent, newline)
136 }
137 }
138 return
139 }
140 141 prev := p.posFor(prev0)
142 next := p.posFor(next0)
143 line := p.lineFor(list[0].Pos())
144 endLine := p.lineFor(list[len(list)-1].End())
145 146 if prev.IsValid() && prev.Line == line && line == endLine {
147 // all list entries on a single line
148 for i, x := range list {
149 if i > 0 {
150 // use position of expression following the comma as
151 // comma position for correct comment placement
152 p.setPos(x.Pos())
153 p.print(token.COMMA, blank)
154 }
155 p.expr0(x, depth)
156 }
157 if isIncomplete {
158 p.print(token.COMMA, blank, "/* "+filteredMsg+" */")
159 }
160 return
161 }
162 163 // list entries span multiple lines;
164 // use source code positions to guide line breaks
165 166 // Don't add extra indentation if noIndent is set;
167 // i.e., pretend that the first line is already indented.
168 ws := ignore
169 if mode&noIndent == 0 {
170 ws = indent
171 }
172 173 // The first linebreak is always a formfeed since this section must not
174 // depend on any previous formatting.
175 prevBreak := -1 // index of last expression that was followed by a linebreak
176 if prev.IsValid() && prev.Line < line && p.linebreak(line, 0, ws, true) > 0 {
177 ws = ignore
178 prevBreak = 0
179 }
180 181 // initialize expression/key size: a zero value indicates expr/key doesn't fit on a single line
182 size := 0
183 184 // We use the ratio between the geometric mean of the previous key sizes and
185 // the current size to determine if there should be a break in the alignment.
186 // To compute the geometric mean we accumulate the ln(size) values (lnsum)
187 // and the number of sizes included (count).
188 lnsum := 0.0
189 count := 0
190 191 // print all list elements
192 prevLine := prev.Line
193 for i, x := range list {
194 line = p.lineFor(x.Pos())
195 196 // Determine if the next linebreak, if any, needs to use formfeed:
197 // in general, use the entire node size to make the decision; for
198 // key:value expressions, use the key size.
199 // TODO(gri) for a better result, should probably incorporate both
200 // the key and the node size into the decision process
201 useFF := true
202 203 // Determine element size: All bets are off if we don't have
204 // position information for the previous and next token (likely
205 // generated code - simply ignore the size in this case by setting
206 // it to 0).
207 prevSize := size
208 const infinity = 1e6 // larger than any source line
209 size = p.nodeSize(x, infinity)
210 pair, isPair := x.(*ast.KeyValueExpr)
211 if size <= infinity && prev.IsValid() && next.IsValid() {
212 // x fits on a single line
213 if isPair {
214 size = p.nodeSize(pair.Key, infinity) // size <= infinity
215 }
216 } else {
217 // size too large or we don't have good layout information
218 size = 0
219 }
220 221 // If the previous line and the current line had single-
222 // line-expressions and the key sizes are small or the
223 // ratio between the current key and the geometric mean
224 // if the previous key sizes does not exceed a threshold,
225 // align columns and do not use formfeed.
226 if prevSize > 0 && size > 0 {
227 const smallSize = 40
228 if count == 0 || prevSize <= smallSize && size <= smallSize {
229 useFF = false
230 } else {
231 const r = 2.5 // threshold
232 geomean := math.Exp(lnsum / float64(count)) // count > 0
233 ratio := float64(size) / geomean
234 useFF = r*ratio <= 1 || r <= ratio
235 }
236 }
237 238 needsLinebreak := 0 < prevLine && prevLine < line
239 if i > 0 {
240 // Use position of expression following the comma as
241 // comma position for correct comment placement, but
242 // only if the expression is on the same line.
243 if !needsLinebreak {
244 p.setPos(x.Pos())
245 }
246 p.print(token.COMMA)
247 needsBlank := true
248 if needsLinebreak {
249 // Lines are broken using newlines so comments remain aligned
250 // unless useFF is set or there are multiple expressions on
251 // the same line in which case formfeed is used.
252 nbreaks := p.linebreak(line, 0, ws, useFF || prevBreak+1 < i)
253 if nbreaks > 0 {
254 ws = ignore
255 prevBreak = i
256 needsBlank = false // we got a line break instead
257 }
258 // If there was a new section or more than one new line
259 // (which means that the tabwriter will implicitly break
260 // the section), reset the geomean variables since we are
261 // starting a new group of elements with the next element.
262 if nbreaks > 1 {
263 lnsum = 0
264 count = 0
265 }
266 }
267 if needsBlank {
268 p.print(blank)
269 }
270 }
271 272 if len(list) > 1 && isPair && size > 0 && needsLinebreak {
273 // We have a key:value expression that fits onto one line
274 // and it's not on the same line as the prior expression:
275 // Use a column for the key such that consecutive entries
276 // can align if possible.
277 // (needsLinebreak is set if we started a new line before)
278 p.expr(pair.Key)
279 p.setPos(pair.Colon)
280 p.print(token.COLON, vtab)
281 p.expr(pair.Value)
282 } else {
283 p.expr0(x, depth)
284 }
285 286 if size > 0 {
287 lnsum += math.Log(float64(size))
288 count++
289 }
290 291 prevLine = line
292 }
293 294 if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
295 // Print a terminating comma if the next token is on a new line.
296 p.print(token.COMMA)
297 if isIncomplete {
298 p.print(newline)
299 p.print("// " + filteredMsg)
300 }
301 if ws == ignore && mode&noIndent == 0 {
302 // unindent if we indented
303 p.print(unindent)
304 }
305 p.print(formfeed) // terminating comma needs a line break to look good
306 return
307 }
308 309 if isIncomplete {
310 p.print(token.COMMA, newline)
311 p.print("// "+filteredMsg, newline)
312 }
313 314 if ws == ignore && mode&noIndent == 0 {
315 // unindent if we indented
316 p.print(unindent)
317 }
318 }
319 320 type paramMode int
321 322 const (
323 funcParam paramMode = iota
324 funcTParam
325 typeTParam
326 )
327 328 func (p *printer) parameters(fields *ast.FieldList, mode paramMode) {
329 openTok, closeTok := token.LPAREN, token.RPAREN
330 if mode != funcParam {
331 openTok, closeTok = token.LBRACK, token.RBRACK
332 }
333 p.setPos(fields.Opening)
334 p.print(openTok)
335 if len(fields.List) > 0 {
336 prevLine := p.lineFor(fields.Opening)
337 ws := indent
338 for i, par := range fields.List {
339 // determine par begin and end line (may be different
340 // if there are multiple parameter names for this par
341 // or the type is on a separate line)
342 parLineBeg := p.lineFor(par.Pos())
343 parLineEnd := p.lineFor(par.End())
344 // separating "," if needed
345 needsLinebreak := 0 < prevLine && prevLine < parLineBeg
346 if i > 0 {
347 // use position of parameter following the comma as
348 // comma position for correct comma placement, but
349 // only if the next parameter is on the same line
350 if !needsLinebreak {
351 p.setPos(par.Pos())
352 }
353 p.print(token.COMMA)
354 }
355 // separator if needed (linebreak or blank)
356 if needsLinebreak && p.linebreak(parLineBeg, 0, ws, true) > 0 {
357 // break line if the opening "(" or previous parameter ended on a different line
358 ws = ignore
359 } else if i > 0 {
360 p.print(blank)
361 }
362 // parameter names
363 if len(par.Names) > 0 {
364 // Very subtle: If we indented before (ws == ignore), identList
365 // won't indent again. If we didn't (ws == indent), identList will
366 // indent if the identList spans multiple lines, and it will outdent
367 // again at the end (and still ws == indent). Thus, a subsequent indent
368 // by a linebreak call after a type, or in the next multi-line identList
369 // will do the right thing.
370 p.identList(par.Names, ws == indent)
371 p.print(blank)
372 }
373 // parameter type
374 p.expr(stripParensAlways(par.Type))
375 prevLine = parLineEnd
376 }
377 378 // if the closing ")" is on a separate line from the last parameter,
379 // print an additional "," and line break
380 if closing := p.lineFor(fields.Closing); 0 < prevLine && prevLine < closing {
381 p.print(token.COMMA)
382 p.linebreak(closing, 0, ignore, true)
383 } else if mode == typeTParam && fields.NumFields() == 1 && combinesWithName(stripParensAlways(fields.List[0].Type)) {
384 // A type parameter list [P T] where the name P and the type expression T syntactically
385 // combine to another valid (value) expression requires a trailing comma, as in [P *T,]
386 // (or an enclosing interface as in [P interface(*T)]), so that the type parameter list
387 // is not parsed as an array length [P*T].
388 p.print(token.COMMA)
389 }
390 391 // unindent if we indented
392 if ws == ignore {
393 p.print(unindent)
394 }
395 }
396 397 p.setPos(fields.Closing)
398 p.print(closeTok)
399 }
400 401 // combinesWithName reports whether a name followed by the expression x
402 // syntactically combines to another valid (value) expression. For instance
403 // using *T for x, "name *T" syntactically appears as the expression x*T.
404 // On the other hand, using P|Q or *P|~Q for x, "name P|Q" or "name *P|~Q"
405 // cannot be combined into a valid (value) expression.
406 func combinesWithName(x ast.Expr) bool {
407 switch x := x.(type) {
408 case *ast.StarExpr:
409 // name *x.X combines to name*x.X if x.X is not a type element
410 return !isTypeElem(x.X)
411 case *ast.BinaryExpr:
412 return combinesWithName(x.X) && !isTypeElem(x.Y)
413 case *ast.ParenExpr:
414 return !isTypeElem(x.X)
415 }
416 return false
417 }
418 419 // isTypeElem reports whether x is a (possibly parenthesized) type element expression.
420 // The result is false if x could be a type element OR an ordinary (value) expression.
421 func isTypeElem(x ast.Expr) bool {
422 switch x := x.(type) {
423 case *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.ChanType:
424 return true
425 case *ast.UnaryExpr:
426 return x.Op == token.TILDE
427 case *ast.BinaryExpr:
428 return isTypeElem(x.X) || isTypeElem(x.Y)
429 case *ast.ParenExpr:
430 return isTypeElem(x.X)
431 }
432 return false
433 }
434 435 func (p *printer) signature(sig *ast.FuncType) {
436 if sig.TypeParams != nil {
437 p.parameters(sig.TypeParams, funcTParam)
438 }
439 if sig.Params != nil {
440 p.parameters(sig.Params, funcParam)
441 } else {
442 p.print(token.LPAREN, token.RPAREN)
443 }
444 res := sig.Results
445 n := res.NumFields()
446 if n > 0 {
447 // res != nil
448 p.print(blank)
449 if n == 1 && res.List[0].Names == nil {
450 // single anonymous res; no ()'s
451 p.expr(stripParensAlways(res.List[0].Type))
452 return
453 }
454 p.parameters(res, funcParam)
455 }
456 }
457 458 func identListSize(list []*ast.Ident, maxSize int) (size int) {
459 for i, x := range list {
460 if i > 0 {
461 size += len(", ")
462 }
463 size += utf8.RuneCountInString(x.Name)
464 if size >= maxSize {
465 break
466 }
467 }
468 return
469 }
470 471 func (p *printer) isOneLineFieldList(list []*ast.Field) bool {
472 if len(list) != 1 {
473 return false // allow only one field
474 }
475 f := list[0]
476 if f.Tag != nil || f.Comment != nil {
477 return false // don't allow tags or comments
478 }
479 // only name(s) and type
480 const maxSize = 30 // adjust as appropriate, this is an approximate value
481 namesSize := identListSize(f.Names, maxSize)
482 if namesSize > 0 {
483 namesSize = 1 // blank between names and types
484 }
485 typeSize := p.nodeSize(f.Type, maxSize)
486 return namesSize+typeSize <= maxSize
487 }
488 489 func (p *printer) setLineComment(text string) {
490 p.setComment(&ast.CommentGroup{List: []*ast.Comment{{Slash: token.NoPos, Text: text}}})
491 }
492 493 func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) {
494 lbrace := fields.Opening
495 list := fields.List
496 rbrace := fields.Closing
497 hasComments := isIncomplete || p.commentBefore(p.posFor(rbrace))
498 srcIsOneLine := lbrace.IsValid() && rbrace.IsValid() && p.lineFor(lbrace) == p.lineFor(rbrace)
499 500 if !hasComments && srcIsOneLine {
501 // possibly a one-line struct/interface
502 if len(list) == 0 {
503 // no blank between keyword and {} in this case
504 p.setPos(lbrace)
505 p.print(token.LBRACE)
506 p.setPos(rbrace)
507 p.print(token.RBRACE)
508 return
509 } else if p.isOneLineFieldList(list) {
510 // small enough - print on one line
511 // (don't use identList and ignore source line breaks)
512 p.setPos(lbrace)
513 p.print(token.LBRACE, blank)
514 f := list[0]
515 if isStruct {
516 for i, x := range f.Names {
517 if i > 0 {
518 // no comments so no need for comma position
519 p.print(token.COMMA, blank)
520 }
521 p.expr(x)
522 }
523 if len(f.Names) > 0 {
524 p.print(blank)
525 }
526 p.expr(f.Type)
527 } else { // interface
528 if len(f.Names) > 0 {
529 name := f.Names[0] // method name
530 p.expr(name)
531 p.signature(f.Type.(*ast.FuncType)) // don't print "func"
532 } else {
533 // embedded interface
534 p.expr(f.Type)
535 }
536 }
537 p.print(blank)
538 p.setPos(rbrace)
539 p.print(token.RBRACE)
540 return
541 }
542 }
543 // hasComments || !srcIsOneLine
544 545 p.print(blank)
546 p.setPos(lbrace)
547 p.print(token.LBRACE, indent)
548 if hasComments || len(list) > 0 {
549 p.print(formfeed)
550 }
551 552 if isStruct {
553 554 sep := vtab
555 if len(list) == 1 {
556 sep = blank
557 }
558 var line int
559 for i, f := range list {
560 if i > 0 {
561 p.linebreak(p.lineFor(f.Pos()), 1, ignore, p.linesFrom(line) > 0)
562 }
563 extraTabs := 0
564 p.setComment(f.Doc)
565 p.recordLine(&line)
566 if len(f.Names) > 0 {
567 // named fields
568 p.identList(f.Names, false)
569 p.print(sep)
570 p.expr(f.Type)
571 extraTabs = 1
572 } else {
573 // anonymous field
574 p.expr(f.Type)
575 extraTabs = 2
576 }
577 if f.Tag != nil {
578 if len(f.Names) > 0 && sep == vtab {
579 p.print(sep)
580 }
581 p.print(sep)
582 p.expr(f.Tag)
583 extraTabs = 0
584 }
585 if f.Comment != nil {
586 for ; extraTabs > 0; extraTabs-- {
587 p.print(sep)
588 }
589 p.setComment(f.Comment)
590 }
591 }
592 if isIncomplete {
593 if len(list) > 0 {
594 p.print(formfeed)
595 }
596 p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
597 p.setLineComment("// " + filteredMsg)
598 }
599 600 } else { // interface
601 602 var line int
603 var prev *ast.Ident // previous "type" identifier
604 for i, f := range list {
605 var name *ast.Ident // first name, or nil
606 if len(f.Names) > 0 {
607 name = f.Names[0]
608 }
609 if i > 0 {
610 // don't do a line break (min == 0) if we are printing a list of types
611 // TODO(gri) this doesn't work quite right if the list of types is
612 // spread across multiple lines
613 min := 1
614 if prev != nil && name == prev {
615 min = 0
616 }
617 p.linebreak(p.lineFor(f.Pos()), min, ignore, p.linesFrom(line) > 0)
618 }
619 p.setComment(f.Doc)
620 p.recordLine(&line)
621 if name != nil {
622 // method
623 p.expr(name)
624 p.signature(f.Type.(*ast.FuncType)) // don't print "func"
625 prev = nil
626 } else {
627 // embedded interface
628 p.expr(f.Type)
629 prev = nil
630 }
631 p.setComment(f.Comment)
632 }
633 if isIncomplete {
634 if len(list) > 0 {
635 p.print(formfeed)
636 }
637 p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
638 p.setLineComment("// contains filtered or unexported methods")
639 }
640 641 }
642 p.print(unindent, formfeed)
643 p.setPos(rbrace)
644 p.print(token.RBRACE)
645 }
646 647 // ----------------------------------------------------------------------------
648 // Expressions
649 650 func walkBinary(e *ast.BinaryExpr) (has4, has5 bool, maxProblem int) {
651 switch e.Op.Precedence() {
652 case 4:
653 has4 = true
654 case 5:
655 has5 = true
656 }
657 658 switch l := e.X.(type) {
659 case *ast.BinaryExpr:
660 if l.Op.Precedence() < e.Op.Precedence() {
661 // parens will be inserted.
662 // pretend this is an *ast.ParenExpr and do nothing.
663 break
664 }
665 h4, h5, mp := walkBinary(l)
666 has4 = has4 || h4
667 has5 = has5 || h5
668 maxProblem = max(maxProblem, mp)
669 }
670 671 switch r := e.Y.(type) {
672 case *ast.BinaryExpr:
673 if r.Op.Precedence() <= e.Op.Precedence() {
674 // parens will be inserted.
675 // pretend this is an *ast.ParenExpr and do nothing.
676 break
677 }
678 h4, h5, mp := walkBinary(r)
679 has4 = has4 || h4
680 has5 = has5 || h5
681 maxProblem = max(maxProblem, mp)
682 683 case *ast.StarExpr:
684 if e.Op == token.QUO { // `*/`
685 maxProblem = 5
686 }
687 688 case *ast.UnaryExpr:
689 switch e.Op.String() + r.Op.String() {
690 case "/*", "&&", "&^":
691 maxProblem = 5
692 case "++", "--":
693 maxProblem = max(maxProblem, 4)
694 }
695 }
696 return
697 }
698 699 func cutoff(e *ast.BinaryExpr, depth int) int {
700 has4, has5, maxProblem := walkBinary(e)
701 if maxProblem > 0 {
702 return maxProblem + 1
703 }
704 if has4 && has5 {
705 if depth == 1 {
706 return 5
707 }
708 return 4
709 }
710 if depth == 1 {
711 return 6
712 }
713 return 4
714 }
715 716 func diffPrec(expr ast.Expr, prec int) int {
717 x, ok := expr.(*ast.BinaryExpr)
718 if !ok || prec != x.Op.Precedence() {
719 return 1
720 }
721 return 0
722 }
723 724 func reduceDepth(depth int) int {
725 depth--
726 if depth < 1 {
727 depth = 1
728 }
729 return depth
730 }
731 732 // Format the binary expression: decide the cutoff and then format.
733 // Let's call depth == 1 Normal mode, and depth > 1 Compact mode.
734 // (Algorithm suggestion by Russ Cox.)
735 //
736 // The precedences are:
737 //
738 // 5 * / % << >> & &^
739 // 4 + - | ^
740 // 3 == != < <= > >=
741 // 2 &&
742 // 1 ||
743 //
744 // The only decision is whether there will be spaces around levels 4 and 5.
745 // There are never spaces at level 6 (unary), and always spaces at levels 3 and below.
746 //
747 // To choose the cutoff, look at the whole expression but excluding primary
748 // expressions (function calls, parenthesized exprs), and apply these rules:
749 //
750 // 1. If there is a binary operator with a right side unary operand
751 // that would clash without a space, the cutoff must be (in order):
752 //
753 // /* 6
754 // && 6
755 // &^ 6
756 // ++ 5
757 // -- 5
758 //
759 // (Comparison operators always have spaces around them.)
760 //
761 // 2. If there is a mix of level 5 and level 4 operators, then the cutoff
762 // is 5 (use spaces to distinguish precedence) in Normal mode
763 // and 4 (never use spaces) in Compact mode.
764 //
765 // 3. If there are no level 4 operators or no level 5 operators, then the
766 // cutoff is 6 (always use spaces) in Normal mode
767 // and 4 (never use spaces) in Compact mode.
768 func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int) {
769 prec := x.Op.Precedence()
770 if prec < prec1 {
771 // parenthesis needed
772 // Note: The parser inserts an ast.ParenExpr node; thus this case
773 // can only occur if the AST is created in a different way.
774 p.print(token.LPAREN)
775 p.expr0(x, reduceDepth(depth)) // parentheses undo one level of depth
776 p.print(token.RPAREN)
777 return
778 }
779 780 printBlank := prec < cutoff
781 782 ws := indent
783 p.expr1(x.X, prec, depth+diffPrec(x.X, prec))
784 if printBlank {
785 p.print(blank)
786 }
787 xline := p.pos.Line // before the operator (it may be on the next line!)
788 yline := p.lineFor(x.Y.Pos())
789 p.setPos(x.OpPos)
790 p.print(x.Op)
791 if xline != yline && xline > 0 && yline > 0 {
792 // at least one line break, but respect an extra empty line
793 // in the source
794 if p.linebreak(yline, 1, ws, true) > 0 {
795 ws = ignore
796 printBlank = false // no blank after line break
797 }
798 }
799 if printBlank {
800 p.print(blank)
801 }
802 p.expr1(x.Y, prec+1, depth+1)
803 if ws == ignore {
804 p.print(unindent)
805 }
806 }
807 808 func isBinary(expr ast.Expr) bool {
809 _, ok := expr.(*ast.BinaryExpr)
810 return ok
811 }
812 813 func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
814 p.setPos(expr.Pos())
815 816 switch x := expr.(type) {
817 case *ast.BadExpr:
818 p.print("BadExpr")
819 820 case *ast.Ident:
821 p.print(x)
822 823 case *ast.BinaryExpr:
824 if depth < 1 {
825 p.internalError("depth < 1:", depth)
826 depth = 1
827 }
828 p.binaryExpr(x, prec1, cutoff(x, depth), depth)
829 830 case *ast.KeyValueExpr:
831 p.expr(x.Key)
832 p.setPos(x.Colon)
833 p.print(token.COLON, blank)
834 p.expr(x.Value)
835 836 case *ast.StarExpr:
837 const prec = token.UnaryPrec
838 if prec < prec1 {
839 // parenthesis needed
840 p.print(token.LPAREN)
841 p.print(token.MUL)
842 p.expr(x.X)
843 p.print(token.RPAREN)
844 } else {
845 // no parenthesis needed
846 p.print(token.MUL)
847 p.expr(x.X)
848 }
849 850 case *ast.UnaryExpr:
851 const prec = token.UnaryPrec
852 if prec < prec1 {
853 // parenthesis needed
854 p.print(token.LPAREN)
855 p.expr(x)
856 p.print(token.RPAREN)
857 } else {
858 // no parenthesis needed
859 p.print(x.Op)
860 if x.Op == token.RANGE {
861 // TODO(gri) Remove this code if it cannot be reached.
862 p.print(blank)
863 }
864 p.expr1(x.X, prec, depth)
865 }
866 867 case *ast.BasicLit:
868 if p.Config.Mode&normalizeNumbers != 0 {
869 x = normalizedNumber(x)
870 }
871 p.print(x)
872 873 case *ast.FuncLit:
874 p.setPos(x.Type.Pos())
875 p.print(token.FUNC)
876 // See the comment in funcDecl about how the header size is computed.
877 startCol := p.out.Column - len("func")
878 p.signature(x.Type)
879 p.funcBody(p.distanceFrom(x.Type.Pos(), startCol), blank, x.Body)
880 881 case *ast.ParenExpr:
882 if _, hasParens := x.X.(*ast.ParenExpr); hasParens {
883 // don't print parentheses around an already parenthesized expression
884 // TODO(gri) consider making this more general and incorporate precedence levels
885 p.expr0(x.X, depth)
886 } else {
887 p.print(token.LPAREN)
888 p.expr0(x.X, reduceDepth(depth)) // parentheses undo one level of depth
889 p.setPos(x.Rparen)
890 p.print(token.RPAREN)
891 }
892 893 case *ast.SelectorExpr:
894 p.selectorExpr(x, depth, false)
895 896 case *ast.TypeAssertExpr:
897 p.expr1(x.X, token.HighestPrec, depth)
898 p.print(token.PERIOD)
899 p.setPos(x.Lparen)
900 p.print(token.LPAREN)
901 if x.Type != nil {
902 p.expr(x.Type)
903 } else {
904 p.print(token.TYPE)
905 }
906 p.setPos(x.Rparen)
907 p.print(token.RPAREN)
908 909 case *ast.IndexExpr:
910 // TODO(gri): should treat[] like parentheses and undo one level of depth
911 p.expr1(x.X, token.HighestPrec, 1)
912 p.setPos(x.Lbrack)
913 p.print(token.LBRACK)
914 p.expr0(x.Index, depth+1)
915 p.setPos(x.Rbrack)
916 p.print(token.RBRACK)
917 918 case *ast.IndexListExpr:
919 // TODO(gri): as for IndexExpr, should treat [] like parentheses and undo
920 // one level of depth
921 p.expr1(x.X, token.HighestPrec, 1)
922 p.setPos(x.Lbrack)
923 p.print(token.LBRACK)
924 p.exprList(x.Lbrack, x.Indices, depth+1, commaTerm, x.Rbrack, false)
925 p.setPos(x.Rbrack)
926 p.print(token.RBRACK)
927 928 case *ast.SliceExpr:
929 // TODO(gri): should treat[] like parentheses and undo one level of depth
930 p.expr1(x.X, token.HighestPrec, 1)
931 p.setPos(x.Lbrack)
932 p.print(token.LBRACK)
933 indices := []ast.Expr{x.Low, x.High}
934 if x.Max != nil {
935 indices = append(indices, x.Max)
936 }
937 // determine if we need extra blanks around ':'
938 var needsBlanks bool
939 if depth <= 1 {
940 var indexCount int
941 var hasBinaries bool
942 for _, x := range indices {
943 if x != nil {
944 indexCount++
945 if isBinary(x) {
946 hasBinaries = true
947 }
948 }
949 }
950 if indexCount > 1 && hasBinaries {
951 needsBlanks = true
952 }
953 }
954 for i, x := range indices {
955 if i > 0 {
956 if indices[i-1] != nil && needsBlanks {
957 p.print(blank)
958 }
959 p.print(token.COLON)
960 if x != nil && needsBlanks {
961 p.print(blank)
962 }
963 }
964 if x != nil {
965 p.expr0(x, depth+1)
966 }
967 }
968 p.setPos(x.Rbrack)
969 p.print(token.RBRACK)
970 971 case *ast.CallExpr:
972 if len(x.Args) > 1 {
973 depth++
974 }
975 976 // Conversions to literal function types or <-chan
977 // types require parentheses around the type.
978 paren := false
979 switch t := x.Fun.(type) {
980 case *ast.FuncType:
981 paren = true
982 case *ast.ChanType:
983 paren = t.Dir == ast.RECV
984 }
985 if paren {
986 p.print(token.LPAREN)
987 }
988 wasIndented := p.possibleSelectorExpr(x.Fun, token.HighestPrec, depth)
989 if paren {
990 p.print(token.RPAREN)
991 }
992 993 p.setPos(x.Lparen)
994 p.print(token.LPAREN)
995 if x.Ellipsis.IsValid() {
996 p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis, false)
997 p.setPos(x.Ellipsis)
998 p.print(token.ELLIPSIS)
999 if x.Rparen.IsValid() && p.lineFor(x.Ellipsis) < p.lineFor(x.Rparen) {
1000 p.print(token.COMMA, formfeed)
1001 }
1002 } else {
1003 p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen, false)
1004 }
1005 p.setPos(x.Rparen)
1006 p.print(token.RPAREN)
1007 if wasIndented {
1008 p.print(unindent)
1009 }
1010 1011 case *ast.CompositeLit:
1012 // composite literal elements that are composite literals themselves may have the type omitted
1013 if x.Type != nil {
1014 p.expr1(x.Type, token.HighestPrec, depth)
1015 }
1016 p.level++
1017 p.setPos(x.Lbrace)
1018 p.print(token.LBRACE)
1019 p.exprList(x.Lbrace, x.Elts, 1, commaTerm, x.Rbrace, x.Incomplete)
1020 // do not insert extra line break following a /*-style comment
1021 // before the closing '}' as it might break the code if there
1022 // is no trailing ','
1023 mode := noExtraLinebreak
1024 // do not insert extra blank following a /*-style comment
1025 // before the closing '}' unless the literal is empty
1026 if len(x.Elts) > 0 {
1027 mode |= noExtraBlank
1028 }
1029 // need the initial indent to print lone comments with
1030 // the proper level of indentation
1031 p.print(indent, unindent, mode)
1032 p.setPos(x.Rbrace)
1033 p.print(token.RBRACE, mode)
1034 p.level--
1035 1036 case *ast.Ellipsis:
1037 p.print(token.ELLIPSIS)
1038 if x.Elt != nil {
1039 p.expr(x.Elt)
1040 }
1041 1042 case *ast.ArrayType:
1043 p.print(token.LBRACK)
1044 if x.Len != nil {
1045 p.expr(x.Len)
1046 }
1047 p.print(token.RBRACK)
1048 p.expr(x.Elt)
1049 1050 case *ast.StructType:
1051 p.print(token.STRUCT)
1052 p.fieldList(x.Fields, true, x.Incomplete)
1053 1054 case *ast.FuncType:
1055 p.print(token.FUNC)
1056 p.signature(x)
1057 1058 case *ast.InterfaceType:
1059 p.print(token.INTERFACE)
1060 p.fieldList(x.Methods, false, x.Incomplete)
1061 1062 case *ast.MapType:
1063 p.print(token.MAP, token.LBRACK)
1064 p.expr(x.Key)
1065 p.print(token.RBRACK)
1066 p.expr(x.Value)
1067 1068 case *ast.ChanType:
1069 switch x.Dir {
1070 case ast.SEND | ast.RECV:
1071 p.print(token.CHAN)
1072 case ast.RECV:
1073 p.print(token.ARROW, token.CHAN) // x.Arrow and x.Pos() are the same
1074 case ast.SEND:
1075 p.print(token.CHAN)
1076 p.setPos(x.Arrow)
1077 p.print(token.ARROW)
1078 }
1079 p.print(blank)
1080 p.expr(x.Value)
1081 1082 default:
1083 panic("unreachable")
1084 }
1085 }
1086 1087 // normalizedNumber rewrites base prefixes and exponents
1088 // of numbers to use lower-case letters (0X123 to 0x123 and 1.2E3 to 1.2e3),
1089 // and removes leading 0's from integer imaginary literals (0765i to 765i).
1090 // It leaves hexadecimal digits alone.
1091 //
1092 // normalizedNumber doesn't modify the ast.BasicLit value lit points to.
1093 // If lit is not a number or a number in canonical format already,
1094 // lit is returned as is. Otherwise a new ast.BasicLit is created.
1095 func normalizedNumber(lit *ast.BasicLit) *ast.BasicLit {
1096 if lit.Kind != token.INT && lit.Kind != token.FLOAT && lit.Kind != token.IMAG {
1097 return lit // not a number - nothing to do
1098 }
1099 if len(lit.Value) < 2 {
1100 return lit // only one digit (common case) - nothing to do
1101 }
1102 // len(lit.Value) >= 2
1103 1104 // We ignore lit.Kind because for lit.Kind == token.IMAG the literal may be an integer
1105 // or floating-point value, decimal or not. Instead, just consider the literal pattern.
1106 x := lit.Value
1107 switch x[:2] {
1108 default:
1109 // 0-prefix octal, decimal int, or float (possibly with 'i' suffix)
1110 if i := bytes.LastIndexByte(x, 'E'); i >= 0 {
1111 x = x[:i] + "e" + x[i+1:]
1112 break
1113 }
1114 // remove leading 0's from integer (but not floating-point) imaginary literals
1115 if x[len(x)-1] == 'i' && !bytes.ContainsAny(x, ".e") {
1116 x = bytes.TrimLeft(x, "0_")
1117 if x == "i" {
1118 x = "0i"
1119 }
1120 }
1121 case "0X":
1122 x = "0x" + x[2:]
1123 // possibly a hexadecimal float
1124 if i := bytes.LastIndexByte(x, 'P'); i >= 0 {
1125 x = x[:i] + "p" + x[i+1:]
1126 }
1127 case "0x":
1128 // possibly a hexadecimal float
1129 i := bytes.LastIndexByte(x, 'P')
1130 if i == -1 {
1131 return lit // nothing to do
1132 }
1133 x = x[:i] + "p" + x[i+1:]
1134 case "0O":
1135 x = "0o" + x[2:]
1136 case "0o":
1137 return lit // nothing to do
1138 case "0B":
1139 x = "0b" + x[2:]
1140 case "0b":
1141 return lit // nothing to do
1142 }
1143 1144 return &ast.BasicLit{ValuePos: lit.ValuePos, Kind: lit.Kind, Value: x}
1145 }
1146 1147 func (p *printer) possibleSelectorExpr(expr ast.Expr, prec1, depth int) bool {
1148 if x, ok := expr.(*ast.SelectorExpr); ok {
1149 return p.selectorExpr(x, depth, true)
1150 }
1151 p.expr1(expr, prec1, depth)
1152 return false
1153 }
1154 1155 // selectorExpr handles an *ast.SelectorExpr node and reports whether x spans
1156 // multiple lines.
1157 func (p *printer) selectorExpr(x *ast.SelectorExpr, depth int, isMethod bool) bool {
1158 p.expr1(x.X, token.HighestPrec, depth)
1159 p.print(token.PERIOD)
1160 if line := p.lineFor(x.Sel.Pos()); p.pos.IsValid() && p.pos.Line < line {
1161 p.print(indent, newline)
1162 p.setPos(x.Sel.Pos())
1163 p.print(x.Sel)
1164 if !isMethod {
1165 p.print(unindent)
1166 }
1167 return true
1168 }
1169 p.setPos(x.Sel.Pos())
1170 p.print(x.Sel)
1171 return false
1172 }
1173 1174 func (p *printer) expr0(x ast.Expr, depth int) {
1175 p.expr1(x, token.LowestPrec, depth)
1176 }
1177 1178 func (p *printer) expr(x ast.Expr) {
1179 const depth = 1
1180 p.expr1(x, token.LowestPrec, depth)
1181 }
1182 1183 // ----------------------------------------------------------------------------
1184 // Statements
1185 1186 // Print the statement list indented, but without a newline after the last statement.
1187 // Extra line breaks between statements in the source are respected but at most one
1188 // empty line is printed between statements.
1189 func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
1190 if nindent > 0 {
1191 p.print(indent)
1192 }
1193 var line int
1194 i := 0
1195 for _, s := range list {
1196 // ignore empty statements (was issue 3466)
1197 if _, isEmpty := s.(*ast.EmptyStmt); !isEmpty {
1198 // nindent == 0 only for lists of switch/select case clauses;
1199 // in those cases each clause is a new section
1200 if len(p.output) > 0 {
1201 // only print line break if we are not at the beginning of the output
1202 // (i.e., we are not printing only a partial program)
1203 p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || nindent == 0 || p.linesFrom(line) > 0)
1204 }
1205 p.recordLine(&line)
1206 p.stmt(s, nextIsRBrace && i == len(list)-1)
1207 // labeled statements put labels on a separate line, but here
1208 // we only care about the start line of the actual statement
1209 // without label - correct line for each label
1210 for t := s; ; {
1211 lt, _ := t.(*ast.LabeledStmt)
1212 if lt == nil {
1213 break
1214 }
1215 line++
1216 t = lt.Stmt
1217 }
1218 i++
1219 }
1220 }
1221 if nindent > 0 {
1222 p.print(unindent)
1223 }
1224 }
1225 1226 // block prints an *ast.BlockStmt; it always spans at least two lines.
1227 func (p *printer) block(b *ast.BlockStmt, nindent int) {
1228 p.setPos(b.Lbrace)
1229 p.print(token.LBRACE)
1230 p.stmtList(b.List, nindent, true)
1231 p.linebreak(p.lineFor(b.Rbrace), 1, ignore, true)
1232 p.setPos(b.Rbrace)
1233 p.print(token.RBRACE)
1234 }
1235 1236 func isTypeName(x ast.Expr) bool {
1237 switch t := x.(type) {
1238 case *ast.Ident:
1239 return true
1240 case *ast.SelectorExpr:
1241 return isTypeName(t.X)
1242 }
1243 return false
1244 }
1245 1246 func stripParens(x ast.Expr) ast.Expr {
1247 if px, strip := x.(*ast.ParenExpr); strip {
1248 // parentheses must not be stripped if there are any
1249 // unparenthesized composite literals starting with
1250 // a type name
1251 ast.Inspect(px.X, func(node ast.Node) bool {
1252 switch x := node.(type) {
1253 case *ast.ParenExpr:
1254 // parentheses protect enclosed composite literals
1255 return false
1256 case *ast.CompositeLit:
1257 if isTypeName(x.Type) {
1258 strip = false // do not strip parentheses
1259 }
1260 return false
1261 }
1262 // in all other cases, keep inspecting
1263 return true
1264 })
1265 if strip {
1266 return stripParens(px.X)
1267 }
1268 }
1269 return x
1270 }
1271 1272 func stripParensAlways(x ast.Expr) ast.Expr {
1273 if x, ok := x.(*ast.ParenExpr); ok {
1274 return stripParensAlways(x.X)
1275 }
1276 return x
1277 }
1278 1279 func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
1280 p.print(blank)
1281 needsBlank := false
1282 if init == nil && post == nil {
1283 // no semicolons required
1284 if expr != nil {
1285 p.expr(stripParens(expr))
1286 needsBlank = true
1287 }
1288 } else {
1289 // all semicolons required
1290 // (they are not separators, print them explicitly)
1291 if init != nil {
1292 p.stmt(init, false)
1293 }
1294 p.print(token.SEMICOLON, blank)
1295 if expr != nil {
1296 p.expr(stripParens(expr))
1297 needsBlank = true
1298 }
1299 if isForStmt {
1300 p.print(token.SEMICOLON, blank)
1301 needsBlank = false
1302 if post != nil {
1303 p.stmt(post, false)
1304 needsBlank = true
1305 }
1306 }
1307 }
1308 if needsBlank {
1309 p.print(blank)
1310 }
1311 }
1312 1313 // indentList reports whether an expression list would look better if it
1314 // were indented wholesale (starting with the very first element, rather
1315 // than starting at the first line break).
1316 func (p *printer) indentList(list []ast.Expr) bool {
1317 // Heuristic: indentList reports whether there are more than one multi-
1318 // line element in the list, or if there is any element that is not
1319 // starting on the same line as the previous one ends.
1320 if len(list) >= 2 {
1321 var b = p.lineFor(list[0].Pos())
1322 var e = p.lineFor(list[len(list)-1].End())
1323 if 0 < b && b < e {
1324 // list spans multiple lines
1325 n := 0 // multi-line element count
1326 line := b
1327 for _, x := range list {
1328 xb := p.lineFor(x.Pos())
1329 xe := p.lineFor(x.End())
1330 if line < xb {
1331 // x is not starting on the same
1332 // line as the previous one ended
1333 return true
1334 }
1335 if xb < xe {
1336 // x is a multi-line element
1337 n++
1338 }
1339 line = xe
1340 }
1341 return n > 1
1342 }
1343 }
1344 return false
1345 }
1346 1347 func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1348 p.setPos(stmt.Pos())
1349 1350 switch s := stmt.(type) {
1351 case *ast.BadStmt:
1352 p.print("BadStmt")
1353 1354 case *ast.DeclStmt:
1355 p.decl(s.Decl)
1356 1357 case *ast.EmptyStmt:
1358 // nothing to do
1359 1360 case *ast.LabeledStmt:
1361 // a "correcting" unindent immediately following a line break
1362 // is applied before the line break if there is no comment
1363 // between (see writeWhitespace)
1364 p.print(unindent)
1365 p.expr(s.Label)
1366 p.setPos(s.Colon)
1367 p.print(token.COLON, indent)
1368 if e, isEmpty := s.Stmt.(*ast.EmptyStmt); isEmpty {
1369 if !nextIsRBrace {
1370 p.print(newline)
1371 p.setPos(e.Pos())
1372 p.print(token.SEMICOLON)
1373 break
1374 }
1375 } else {
1376 p.linebreak(p.lineFor(s.Stmt.Pos()), 1, ignore, true)
1377 }
1378 p.stmt(s.Stmt, nextIsRBrace)
1379 1380 case *ast.ExprStmt:
1381 const depth = 1
1382 p.expr0(s.X, depth)
1383 1384 case *ast.SendStmt:
1385 const depth = 1
1386 p.expr0(s.Chan, depth)
1387 p.print(blank)
1388 p.setPos(s.Arrow)
1389 p.print(token.ARROW, blank)
1390 p.expr0(s.Value, depth)
1391 1392 case *ast.IncDecStmt:
1393 const depth = 1
1394 p.expr0(s.X, depth+1)
1395 p.setPos(s.TokPos)
1396 p.print(s.Tok)
1397 1398 case *ast.AssignStmt:
1399 var depth = 1
1400 if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
1401 depth++
1402 }
1403 p.exprList(s.Pos(), s.Lhs, depth, 0, s.TokPos, false)
1404 p.print(blank)
1405 p.setPos(s.TokPos)
1406 p.print(s.Tok, blank)
1407 p.exprList(s.TokPos, s.Rhs, depth, 0, token.NoPos, false)
1408 1409 case *ast.GoStmt:
1410 p.print(token.GO, blank)
1411 p.expr(s.Call)
1412 1413 case *ast.DeferStmt:
1414 p.print(token.DEFER, blank)
1415 p.expr(s.Call)
1416 1417 case *ast.ReturnStmt:
1418 p.print(token.RETURN)
1419 if s.Results != nil {
1420 p.print(blank)
1421 // Use indentList heuristic to make corner cases look
1422 // better (issue 1207). A more systematic approach would
1423 // always indent, but this would cause significant
1424 // reformatting of the code base and not necessarily
1425 // lead to more nicely formatted code in general.
1426 if p.indentList(s.Results) {
1427 p.print(indent)
1428 // Use NoPos so that a newline never goes before
1429 // the results (see issue #32854).
1430 p.exprList(token.NoPos, s.Results, 1, noIndent, token.NoPos, false)
1431 p.print(unindent)
1432 } else {
1433 p.exprList(token.NoPos, s.Results, 1, 0, token.NoPos, false)
1434 }
1435 }
1436 1437 case *ast.BranchStmt:
1438 p.print(s.Tok)
1439 if s.Label != nil {
1440 p.print(blank)
1441 p.expr(s.Label)
1442 }
1443 1444 case *ast.BlockStmt:
1445 p.block(s, 1)
1446 1447 case *ast.IfStmt:
1448 p.print(token.IF)
1449 p.controlClause(false, s.Init, s.Cond, nil)
1450 p.block(s.Body, 1)
1451 if s.Else != nil {
1452 p.print(blank, token.ELSE, blank)
1453 switch s.Else.(type) {
1454 case *ast.BlockStmt, *ast.IfStmt:
1455 p.stmt(s.Else, nextIsRBrace)
1456 default:
1457 // This can only happen with an incorrectly
1458 // constructed AST. Permit it but print so
1459 // that it can be parsed without errors.
1460 p.print(token.LBRACE, indent, formfeed)
1461 p.stmt(s.Else, true)
1462 p.print(unindent, formfeed, token.RBRACE)
1463 }
1464 }
1465 1466 case *ast.CaseClause:
1467 if s.List != nil {
1468 p.print(token.CASE, blank)
1469 p.exprList(s.Pos(), s.List, 1, 0, s.Colon, false)
1470 } else {
1471 p.print(token.DEFAULT)
1472 }
1473 p.setPos(s.Colon)
1474 p.print(token.COLON)
1475 p.stmtList(s.Body, 1, nextIsRBrace)
1476 1477 case *ast.SwitchStmt:
1478 p.print(token.SWITCH)
1479 p.controlClause(false, s.Init, s.Tag, nil)
1480 p.block(s.Body, 0)
1481 1482 case *ast.TypeSwitchStmt:
1483 p.print(token.SWITCH)
1484 if s.Init != nil {
1485 p.print(blank)
1486 p.stmt(s.Init, false)
1487 p.print(token.SEMICOLON)
1488 }
1489 p.print(blank)
1490 p.stmt(s.Assign, false)
1491 p.print(blank)
1492 p.block(s.Body, 0)
1493 1494 case *ast.CommClause:
1495 if s.Comm != nil {
1496 p.print(token.CASE, blank)
1497 p.stmt(s.Comm, false)
1498 } else {
1499 p.print(token.DEFAULT)
1500 }
1501 p.setPos(s.Colon)
1502 p.print(token.COLON)
1503 p.stmtList(s.Body, 1, nextIsRBrace)
1504 1505 case *ast.SelectStmt:
1506 p.print(token.SELECT, blank)
1507 body := s.Body
1508 if len(body.List) == 0 && !p.commentBefore(p.posFor(body.Rbrace)) {
1509 // print empty select statement w/o comments on one line
1510 p.setPos(body.Lbrace)
1511 p.print(token.LBRACE)
1512 p.setPos(body.Rbrace)
1513 p.print(token.RBRACE)
1514 } else {
1515 p.block(body, 0)
1516 }
1517 1518 case *ast.ForStmt:
1519 p.print(token.FOR)
1520 p.controlClause(true, s.Init, s.Cond, s.Post)
1521 p.block(s.Body, 1)
1522 1523 case *ast.RangeStmt:
1524 p.print(token.FOR, blank)
1525 if s.Key != nil {
1526 p.expr(s.Key)
1527 if s.Value != nil {
1528 // use position of value following the comma as
1529 // comma position for correct comment placement
1530 p.setPos(s.Value.Pos())
1531 p.print(token.COMMA, blank)
1532 p.expr(s.Value)
1533 }
1534 p.print(blank)
1535 p.setPos(s.TokPos)
1536 p.print(s.Tok, blank)
1537 }
1538 p.print(token.RANGE, blank)
1539 p.expr(stripParens(s.X))
1540 p.print(blank)
1541 p.block(s.Body, 1)
1542 1543 default:
1544 panic("unreachable")
1545 }
1546 }
1547 1548 // ----------------------------------------------------------------------------
1549 // Declarations
1550 1551 // The keepTypeColumn function determines if the type column of a series of
1552 // consecutive const or var declarations must be kept, or if initialization
1553 // values (V) can be placed in the type column (T) instead. The i'th entry
1554 // in the result slice is true if the type column in spec[i] must be kept.
1555 //
1556 // For example, the declaration:
1557 //
1558 // const (
1559 // foobar int = 42 // comment
1560 // x = 7 // comment
1561 // foo
1562 // bar = 991
1563 // )
1564 //
1565 // leads to the type/values matrix below. A run of value columns (V) can
1566 // be moved into the type column if there is no type for any of the values
1567 // in that column (we only move entire columns so that they align properly).
1568 //
1569 // matrix formatted result
1570 // matrix
1571 // T V -> T V -> true there is a T and so the type
1572 // - V - V true column must be kept
1573 // - - - - false
1574 // - V V - false V is moved into T column
1575 func keepTypeColumn(specs []ast.Spec) []bool {
1576 m := []bool{:len(specs)}
1577 1578 populate := func(i, j int, keepType bool) {
1579 if keepType {
1580 for ; i < j; i++ {
1581 m[i] = true
1582 }
1583 }
1584 }
1585 1586 i0 := -1 // if i0 >= 0 we are in a run and i0 is the start of the run
1587 var keepType bool
1588 for i, s := range specs {
1589 t := s.(*ast.ValueSpec)
1590 if t.Values != nil {
1591 if i0 < 0 {
1592 // start of a run of ValueSpecs with non-nil Values
1593 i0 = i
1594 keepType = false
1595 }
1596 } else {
1597 if i0 >= 0 {
1598 // end of a run
1599 populate(i0, i, keepType)
1600 i0 = -1
1601 }
1602 }
1603 if t.Type != nil {
1604 keepType = true
1605 }
1606 }
1607 if i0 >= 0 {
1608 // end of a run
1609 populate(i0, len(specs), keepType)
1610 }
1611 1612 return m
1613 }
1614 1615 func (p *printer) valueSpec(s *ast.ValueSpec, keepType bool) {
1616 p.setComment(s.Doc)
1617 p.identList(s.Names, false) // always present
1618 extraTabs := 3
1619 if s.Type != nil || keepType {
1620 p.print(vtab)
1621 extraTabs--
1622 }
1623 if s.Type != nil {
1624 p.expr(s.Type)
1625 }
1626 if s.Values != nil {
1627 p.print(vtab, token.ASSIGN, blank)
1628 p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false)
1629 extraTabs--
1630 }
1631 if s.Comment != nil {
1632 for ; extraTabs > 0; extraTabs-- {
1633 p.print(vtab)
1634 }
1635 p.setComment(s.Comment)
1636 }
1637 }
1638 1639 func sanitizeImportPath(lit *ast.BasicLit) *ast.BasicLit {
1640 // Note: An unmodified AST generated by go/parser will already
1641 // contain a backward- or double-quoted path string that does
1642 // not contain any invalid characters, and most of the work
1643 // here is not needed. However, a modified or generated AST
1644 // may possibly contain non-canonical paths. Do the work in
1645 // all cases since it's not too hard and not speed-critical.
1646 1647 // if we don't have a proper string, be conservative and return whatever we have
1648 if lit.Kind != token.STRING {
1649 return lit
1650 }
1651 s, err := strconv.Unquote(lit.Value)
1652 if err != nil {
1653 return lit
1654 }
1655 1656 // if the string is an invalid path, return whatever we have
1657 //
1658 // spec: "Implementation restriction: A compiler may restrict
1659 // ImportPaths to non-empty strings using only characters belonging
1660 // to Unicode's L, M, N, P, and S general categories (the Graphic
1661 // characters without spaces) and may also exclude the characters
1662 // !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character
1663 // U+FFFD."
1664 if s == "" {
1665 return lit
1666 }
1667 const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
1668 for _, r := range s {
1669 if !unicode.IsGraphic(r) || unicode.IsSpace(r) || bytes.ContainsRune(illegalChars, r) {
1670 return lit
1671 }
1672 }
1673 1674 // otherwise, return the double-quoted path
1675 s = strconv.Quote(s)
1676 if s == lit.Value {
1677 return lit // nothing wrong with lit
1678 }
1679 return &ast.BasicLit{ValuePos: lit.ValuePos, Kind: token.STRING, Value: s}
1680 }
1681 1682 // The parameter n is the number of specs in the group. If doIndent is set,
1683 // multi-line identifier lists in the spec are indented when the first
1684 // linebreak is encountered.
1685 func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
1686 switch s := spec.(type) {
1687 case *ast.ImportSpec:
1688 p.setComment(s.Doc)
1689 if s.Name != nil {
1690 p.expr(s.Name)
1691 p.print(blank)
1692 }
1693 p.expr(sanitizeImportPath(s.Path))
1694 p.setComment(s.Comment)
1695 p.setPos(s.EndPos)
1696 1697 case *ast.ValueSpec:
1698 if n != 1 {
1699 p.internalError("expected n = 1; got", n)
1700 }
1701 p.setComment(s.Doc)
1702 p.identList(s.Names, doIndent) // always present
1703 if s.Type != nil {
1704 p.print(blank)
1705 p.expr(s.Type)
1706 }
1707 if s.Values != nil {
1708 p.print(blank, token.ASSIGN, blank)
1709 p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false)
1710 }
1711 p.setComment(s.Comment)
1712 1713 case *ast.TypeSpec:
1714 p.setComment(s.Doc)
1715 p.expr(s.Name)
1716 if s.TypeParams != nil {
1717 p.parameters(s.TypeParams, typeTParam)
1718 }
1719 if n == 1 {
1720 p.print(blank)
1721 } else {
1722 p.print(vtab)
1723 }
1724 if s.Assign.IsValid() {
1725 p.print(token.ASSIGN, blank)
1726 }
1727 p.expr(s.Type)
1728 p.setComment(s.Comment)
1729 1730 default:
1731 panic("unreachable")
1732 }
1733 }
1734 1735 func (p *printer) genDecl(d *ast.GenDecl) {
1736 p.setComment(d.Doc)
1737 p.setPos(d.Pos())
1738 p.print(d.Tok, blank)
1739 1740 if d.Lparen.IsValid() || len(d.Specs) != 1 {
1741 // group of parenthesized declarations
1742 p.setPos(d.Lparen)
1743 p.print(token.LPAREN)
1744 if n := len(d.Specs); n > 0 {
1745 p.print(indent, formfeed)
1746 if n > 1 && (d.Tok == token.CONST || d.Tok == token.VAR) {
1747 // two or more grouped const/var declarations:
1748 // determine if the type column must be kept
1749 keepType := keepTypeColumn(d.Specs)
1750 var line int
1751 for i, s := range d.Specs {
1752 if i > 0 {
1753 p.linebreak(p.lineFor(s.Pos()), 1, ignore, p.linesFrom(line) > 0)
1754 }
1755 p.recordLine(&line)
1756 p.valueSpec(s.(*ast.ValueSpec), keepType[i])
1757 }
1758 } else {
1759 var line int
1760 for i, s := range d.Specs {
1761 if i > 0 {
1762 p.linebreak(p.lineFor(s.Pos()), 1, ignore, p.linesFrom(line) > 0)
1763 }
1764 p.recordLine(&line)
1765 p.spec(s, n, false)
1766 }
1767 }
1768 p.print(unindent, formfeed)
1769 }
1770 p.setPos(d.Rparen)
1771 p.print(token.RPAREN)
1772 1773 } else if len(d.Specs) > 0 {
1774 // single declaration
1775 p.spec(d.Specs[0], 1, true)
1776 }
1777 }
1778 1779 // sizeCounter is an io.Writer which counts the number of bytes written,
1780 // as well as whether a newline character was seen.
1781 type sizeCounter struct {
1782 hasNewline bool
1783 size int
1784 }
1785 1786 func (c *sizeCounter) Write(p []byte) (int, error) {
1787 if !c.hasNewline {
1788 for _, b := range p {
1789 if b == '\n' || b == '\f' {
1790 c.hasNewline = true
1791 break
1792 }
1793 }
1794 }
1795 c.size += len(p)
1796 return len(p), nil
1797 }
1798 1799 // nodeSize determines the size of n in chars after formatting.
1800 // The result is <= maxSize if the node fits on one line with at
1801 // most maxSize chars and the formatted output doesn't contain
1802 // any control chars. Otherwise, the result is > maxSize.
1803 func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) {
1804 // nodeSize invokes the printer, which may invoke nodeSize
1805 // recursively. For deep composite literal nests, this can
1806 // lead to an exponential algorithm. Remember previous
1807 // results to prune the recursion (was issue 1628).
1808 if size, found := p.nodeSizes[n]; found {
1809 return size
1810 }
1811 1812 size = maxSize + 1 // assume n doesn't fit
1813 p.nodeSizes[n] = size
1814 1815 // nodeSize computation must be independent of particular
1816 // style so that we always get the same decision; print
1817 // in RawFormat
1818 cfg := Config{Mode: RawFormat}
1819 var counter sizeCounter
1820 if err := cfg.fprint(&counter, p.fset, n, p.nodeSizes); err != nil {
1821 return
1822 }
1823 if counter.size <= maxSize && !counter.hasNewline {
1824 // n fits in a single line
1825 size = counter.size
1826 p.nodeSizes[n] = size
1827 }
1828 return
1829 }
1830 1831 // numLines returns the number of lines spanned by node n in the original source.
1832 func (p *printer) numLines(n ast.Node) int {
1833 if from := n.Pos(); from.IsValid() {
1834 if to := n.End(); to.IsValid() {
1835 return p.lineFor(to) - p.lineFor(from) + 1
1836 }
1837 }
1838 return infinity
1839 }
1840 1841 // bodySize is like nodeSize but it is specialized for *ast.BlockStmt's.
1842 func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int {
1843 pos1 := b.Pos()
1844 pos2 := b.Rbrace
1845 if pos1.IsValid() && pos2.IsValid() && p.lineFor(pos1) != p.lineFor(pos2) {
1846 // opening and closing brace are on different lines - don't make it a one-liner
1847 return maxSize + 1
1848 }
1849 if len(b.List) > 5 {
1850 // too many statements - don't make it a one-liner
1851 return maxSize + 1
1852 }
1853 // otherwise, estimate body size
1854 bodySize := p.commentSizeBefore(p.posFor(pos2))
1855 for i, s := range b.List {
1856 if bodySize > maxSize {
1857 break // no need to continue
1858 }
1859 if i > 0 {
1860 bodySize += 2 // space for a semicolon and blank
1861 }
1862 bodySize += p.nodeSize(s, maxSize)
1863 }
1864 return bodySize
1865 }
1866 1867 // funcBody prints a function body following a function header of given headerSize.
1868 // If the header's and block's size are "small enough" and the block is "simple enough",
1869 // the block is printed on the current line, without line breaks, spaced from the header
1870 // by sep. Otherwise the block's opening "{" is printed on the current line, followed by
1871 // lines for the block's statements and its closing "}".
1872 func (p *printer) funcBody(headerSize int, sep whiteSpace, b *ast.BlockStmt) {
1873 if b == nil {
1874 return
1875 }
1876 1877 // save/restore composite literal nesting level
1878 defer func(level int) {
1879 p.level = level
1880 }(p.level)
1881 p.level = 0
1882 1883 const maxSize = 100
1884 if headerSize+p.bodySize(b, maxSize) <= maxSize {
1885 p.print(sep)
1886 p.setPos(b.Lbrace)
1887 p.print(token.LBRACE)
1888 if len(b.List) > 0 {
1889 p.print(blank)
1890 for i, s := range b.List {
1891 if i > 0 {
1892 p.print(token.SEMICOLON, blank)
1893 }
1894 p.stmt(s, i == len(b.List)-1)
1895 }
1896 p.print(blank)
1897 }
1898 p.print(noExtraLinebreak)
1899 p.setPos(b.Rbrace)
1900 p.print(token.RBRACE, noExtraLinebreak)
1901 return
1902 }
1903 1904 if sep != ignore {
1905 p.print(blank) // always use blank
1906 }
1907 p.block(b, 1)
1908 }
1909 1910 // distanceFrom returns the column difference between p.out (the current output
1911 // position) and startOutCol. If the start position is on a different line from
1912 // the current position (or either is unknown), the result is infinity.
1913 func (p *printer) distanceFrom(startPos token.Pos, startOutCol int) int {
1914 if startPos.IsValid() && p.pos.IsValid() && p.posFor(startPos).Line == p.pos.Line {
1915 return p.out.Column - startOutCol
1916 }
1917 return infinity
1918 }
1919 1920 func (p *printer) funcDecl(d *ast.FuncDecl) {
1921 p.setComment(d.Doc)
1922 p.setPos(d.Pos())
1923 p.print(token.FUNC, blank)
1924 // We have to save startCol only after emitting FUNC; otherwise it can be on a
1925 // different line (all whitespace preceding the FUNC is emitted only when the
1926 // FUNC is emitted).
1927 startCol := p.out.Column - len("func ")
1928 if d.Recv != nil {
1929 p.parameters(d.Recv, funcParam) // method: print receiver
1930 p.print(blank)
1931 }
1932 p.expr(d.Name)
1933 p.signature(d.Type)
1934 p.funcBody(p.distanceFrom(d.Pos(), startCol), vtab, d.Body)
1935 }
1936 1937 func (p *printer) decl(decl ast.Decl) {
1938 switch d := decl.(type) {
1939 case *ast.BadDecl:
1940 p.setPos(d.Pos())
1941 p.print("BadDecl")
1942 case *ast.GenDecl:
1943 p.genDecl(d)
1944 case *ast.FuncDecl:
1945 p.funcDecl(d)
1946 default:
1947 panic("unreachable")
1948 }
1949 }
1950 1951 // ----------------------------------------------------------------------------
1952 // Files
1953 1954 func declToken(decl ast.Decl) (tok token.Token) {
1955 tok = token.ILLEGAL
1956 switch d := decl.(type) {
1957 case *ast.GenDecl:
1958 tok = d.Tok
1959 case *ast.FuncDecl:
1960 tok = token.FUNC
1961 }
1962 return
1963 }
1964 1965 func (p *printer) declList(list []ast.Decl) {
1966 tok := token.ILLEGAL
1967 for _, d := range list {
1968 prev := tok
1969 tok = declToken(d)
1970 // If the declaration token changed (e.g., from CONST to TYPE)
1971 // or the next declaration has documentation associated with it,
1972 // print an empty line between top-level declarations.
1973 // (because p.linebreak is called with the position of d, which
1974 // is past any documentation, the minimum requirement is satisfied
1975 // even w/o the extra getDoc(d) nil-check - leave it in case the
1976 // linebreak logic improves - there's already a TODO).
1977 if len(p.output) > 0 {
1978 // only print line break if we are not at the beginning of the output
1979 // (i.e., we are not printing only a partial program)
1980 min := 1
1981 if prev != tok || getDoc(d) != nil {
1982 min = 2
1983 }
1984 // start a new section if the next declaration is a function
1985 // that spans multiple lines (see also issue #19544)
1986 p.linebreak(p.lineFor(d.Pos()), min, ignore, tok == token.FUNC && p.numLines(d) > 1)
1987 }
1988 p.decl(d)
1989 }
1990 }
1991 1992 func (p *printer) file(src *ast.File) {
1993 p.setComment(src.Doc)
1994 p.setPos(src.Pos())
1995 p.print(token.PACKAGE, blank)
1996 p.expr(src.Name)
1997 p.declList(src.Decls)
1998 p.print(newline)
1999 }
2000