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