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.
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.
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
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
30 // ----------------------------------------------------------------------------
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
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.
48 func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (nbreaks int) {
49 n := nlimit(line - p.pos.Line)
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 {
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")
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
94 if p.commentOffset == infinity {
95 p.nextComment() // get comment ready for use
99 type exprListMode uint
102 commaTerm exprListMode = 1 << iota // list is optionally terminated by a comma
103 noIndent // no extra indentation in multi-line lists
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 {
114 var mode exprListMode
118 p.exprList(token.NoPos, xlist, 1, mode, token.NoPos, false)
121 const filteredMsg = "contains filtered or unexported fields"
123 // Print a list of expressions. If the list spans multiple
124 // source lines, the original line breaks are respected between
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) {
133 prev := p.posFor(prev0)
134 next := p.posFor(next0)
135 if prev.IsValid() && prev.Line == next.Line {
136 p.print("/* " + filteredMsg + " */")
139 p.print(indent, "// "+filteredMsg, unindent, newline)
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())
150 if prev.IsValid() && prev.Line == line && line == endLine {
151 // all list entries on a single line
152 for i, x := range list {
154 // use position of expression following the comma as
155 // comma position for correct comment placement
156 p.print(x.Pos(), token.COMMA, blank)
161 p.print(token.COMMA, blank, "/* "+filteredMsg+" */")
166 // list entries span multiple lines;
167 // use source code positions to guide line breaks
169 // Don't add extra indentation if noIndent is set;
170 // i.e., pretend that the first line is already indented.
172 if mode&noIndent == 0 {
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 {
184 // initialize expression/key size: a zero value indicates expr/key doesn't fit on a single line
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).
194 // print all list elements
195 prevLine := prev.Line
196 for i, x := range list {
197 line = p.lineFor(x.Pos())
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
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
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
217 size = p.nodeSize(pair.Key, infinity) // size <= infinity
220 // size too large or we don't have good layout information
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 {
231 if count == 0 || prevSize <= smallSize && size <= smallSize {
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
241 needsLinebreak := 0 < prevLine && prevLine < line
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.
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)
259 needsBlank = false // we got a line break instead
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.
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)
282 p.print(pair.Colon, token.COLON, vtab)
289 lnsum += math.Log(float64(size))
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.
301 p.print("// " + filteredMsg)
303 if ws == ignore && mode&noIndent == 0 {
304 // unindent if we indented
307 p.print(formfeed) // terminating comma needs a line break to look good
312 p.print(token.COMMA, newline)
313 p.print("// "+filteredMsg, newline)
316 if ws == ignore && mode&noIndent == 0 {
317 // unindent if we indented
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)
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)
332 if len(par.Names) > 0 {
333 parLineBeg = p.lineFor(par.Names[0].Pos())
335 parLineBeg = p.lineFor(par.Type.Pos())
337 var parLineEnd = p.lineFor(par.Type.End())
338 // separating "," if needed
339 needsLinebreak := 0 < prevLine && prevLine < parLineBeg
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
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
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)
368 p.expr(stripParensAlways(par.Type))
369 prevLine = parLineEnd
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 {
375 p.linebreak(closing, 0, ignore, true)
377 // unindent if we indented
382 p.print(fields.Closing, token.RPAREN)
385 func (p *printer) signature(params, result *ast.FieldList) {
389 p.print(token.LPAREN, token.RPAREN)
391 n := result.NumFields()
395 if n == 1 && result.List[0].Names == nil {
396 // single anonymous result; no ()'s
397 p.expr(stripParensAlways(result.List[0].Type))
404 func identListSize(list []*ast.Ident, maxSize int) (size int) {
405 for i, x := range list {
409 size += utf8.RuneCountInString(x.Name)
417 func (p *printer) isOneLineFieldList(list []*ast.Field) bool {
419 return false // allow only one field
422 if f.Tag != nil || f.Comment != nil {
423 return false // don't allow tags or comments
425 // only name(s) and type
426 const maxSize = 30 // adjust as appropriate, this is an approximate value
427 namesSize := identListSize(f.Names, maxSize)
429 namesSize = 1 // blank between names and types
431 typeSize := p.nodeSize(f.Type, maxSize)
432 return namesSize+typeSize <= maxSize
435 func (p *printer) setLineComment(text string) {
436 p.setComment(&ast.CommentGroup{List: []*ast.Comment{{Slash: token.NoPos, Text: text}}})
439 func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) {
440 lbrace := fields.Opening
442 rbrace := fields.Closing
443 hasComments := isIncomplete || p.commentBefore(p.posFor(rbrace))
444 srcIsOneLine := lbrace.IsValid() && rbrace.IsValid() && p.lineFor(lbrace) == p.lineFor(rbrace)
446 if !hasComments && srcIsOneLine {
447 // possibly a one-line struct/interface
449 // no blank between keyword and {} in this case
450 p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
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)
458 for i, x := range f.Names {
460 // no comments so no need for comma position
461 p.print(token.COMMA, blank)
465 if len(f.Names) > 0 {
469 } else { // interface
470 if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
473 p.signature(ftyp.Params, ftyp.Results)
475 // embedded interface
479 p.print(blank, rbrace, token.RBRACE)
483 // hasComments || !srcIsOneLine
485 p.print(blank, lbrace, token.LBRACE, indent)
486 if hasComments || len(list) > 0 {
497 for i, f := range list {
499 p.linebreak(p.lineFor(f.Pos()), 1, ignore, p.linesFrom(line) > 0)
504 if len(f.Names) > 0 {
506 p.identList(f.Names, false)
516 if len(f.Names) > 0 && sep == vtab {
523 if f.Comment != nil {
524 for ; extraTabs > 0; extraTabs-- {
527 p.setComment(f.Comment)
534 p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment
535 p.setLineComment("// " + filteredMsg)
538 } else { // interface
541 for i, f := range list {
543 p.linebreak(p.lineFor(f.Pos()), 1, ignore, p.linesFrom(line) > 0)
547 if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
550 p.signature(ftyp.Params, ftyp.Results)
552 // embedded interface
555 p.setComment(f.Comment)
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")
566 p.print(unindent, formfeed, rbrace, token.RBRACE)
569 // ----------------------------------------------------------------------------
572 func walkBinary(e *ast.BinaryExpr) (has4, has5 bool, maxProblem int) {
573 switch e.Op.Precedence() {
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.
587 h4, h5, mp := walkBinary(l)
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.
602 h4, h5, mp := walkBinary(r)
610 if e.Op == token.QUO { // `*/`
615 switch e.Op.String() + r.Op.String() {
616 case "/*", "&&", "&^":
627 func cutoff(e *ast.BinaryExpr, depth int) int {
628 has4, has5, maxProblem := walkBinary(e)
630 return maxProblem + 1
644 func diffPrec(expr ast.Expr, prec int) int {
645 x, ok := expr.(*ast.BinaryExpr)
646 if !ok || prec != x.Op.Precedence() {
652 func reduceDepth(depth int) int {
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.)
664 // The precedences are:
665 // 5 * / % << >> & &^
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.
674 // To choose the cutoff, look at the whole expression but excluding primary
675 // expressions (function calls, parenthesized exprs), and apply these rules:
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):
686 // (Comparison operators always have spaces around them.)
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.
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.
696 func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int) {
697 prec := x.Op.Precedence()
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)
708 printBlank := prec < cutoff
711 p.expr1(x.X, prec, depth+diffPrec(x.X, prec))
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
721 if p.linebreak(yline, 1, ws, true) > 0 {
723 printBlank = false // no blank after line break
729 p.expr1(x.Y, prec+1, depth+1)
735 func isBinary(expr ast.Expr) bool {
736 _, ok := expr.(*ast.BinaryExpr)
740 func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
743 switch x := expr.(type) {
750 case *ast.BinaryExpr:
752 p.internalError("depth < 1:", depth)
755 p.binaryExpr(x, prec1, cutoff(x, depth), depth)
757 case *ast.KeyValueExpr:
759 p.print(x.Colon, token.COLON, blank)
763 const prec = token.UnaryPrec
765 // parenthesis needed
766 p.print(token.LPAREN)
769 p.print(token.RPAREN)
771 // no parenthesis needed
777 const prec = token.UnaryPrec
779 // parenthesis needed
780 p.print(token.LPAREN)
782 p.print(token.RPAREN)
784 // no parenthesis needed
786 if x.Op == token.RANGE {
787 // TODO(gri) Remove this code if it cannot be reached.
790 p.expr1(x.X, prec, depth)
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)
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
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)
814 case *ast.SelectorExpr:
815 p.selectorExpr(x, depth, false)
817 case *ast.TypeAssertExpr:
818 p.expr1(x.X, token.HighestPrec, depth)
819 p.print(token.PERIOD, x.Lparen, token.LPAREN)
825 p.print(x.Rparen, token.RPAREN)
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)
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}
840 indices = append(indices, x.Max)
842 // determine if we need extra blanks around ':'
847 for _, x := range indices {
855 if indexCount > 1 && hasBinaries {
859 for i, x := range indices {
861 if indices[i-1] != nil && needsBlanks {
865 if x != nil && needsBlanks {
873 p.print(x.Rbrack, token.RBRACK)
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)
886 wasIndented = p.possibleSelectorExpr(x.Fun, token.HighestPrec, depth)
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)
896 p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen, false)
898 p.print(x.Rparen, token.RPAREN)
903 case *ast.CompositeLit:
904 // composite literal elements that are composite literals themselves may have the type omitted
906 p.expr1(x.Type, token.HighestPrec, depth)
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
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)
926 p.print(token.ELLIPSIS)
932 p.print(token.LBRACK)
936 p.print(token.RBRACK)
939 case *ast.StructType:
940 p.print(token.STRUCT)
941 p.fieldList(x.Fields, true, x.Incomplete)
945 p.signature(x.Params, x.Results)
947 case *ast.InterfaceType:
948 p.print(token.INTERFACE)
949 p.fieldList(x.Methods, false, x.Incomplete)
952 p.print(token.MAP, token.LBRACK)
954 p.print(token.RBRACK)
959 case ast.SEND | ast.RECV:
962 p.print(token.ARROW, token.CHAN) // x.Arrow and x.Pos() are the same
964 p.print(token.CHAN, x.Arrow, token.ARROW)
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)
978 p.expr1(expr, prec1, depth)
982 // selectorExpr handles an *ast.SelectorExpr node and reports whether x spans
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)
994 p.print(x.Sel.Pos(), x.Sel)
998 func (p *printer) expr0(x ast.Expr, depth int) {
999 p.expr1(x, token.LowestPrec, depth)
1002 func (p *printer) expr(x ast.Expr) {
1004 p.expr1(x, token.LowestPrec, depth)
1007 // ----------------------------------------------------------------------------
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) {
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)
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
1035 lt, _ := t.(*ast.LabeledStmt)
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)
1058 func isTypeName(x ast.Expr) bool {
1059 switch t := x.(type) {
1062 case *ast.SelectorExpr:
1063 return isTypeName(t.X)
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
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
1078 case *ast.CompositeLit:
1079 if isTypeName(x.Type) {
1080 strip = false // do not strip parentheses
1084 // in all other cases, keep inspecting
1088 return stripParens(px.X)
1094 func stripParensAlways(x ast.Expr) ast.Expr {
1095 if x, ok := x.(*ast.ParenExpr); ok {
1096 return stripParensAlways(x.X)
1101 func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
1104 if init == nil && post == nil {
1105 // no semicolons required
1107 p.expr(stripParens(expr))
1111 // all semicolons required
1112 // (they are not separators, print them explicitly)
1116 p.print(token.SEMICOLON, blank)
1118 p.expr(stripParens(expr))
1122 p.print(token.SEMICOLON, blank)
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).
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.
1144 var b = p.lineFor(list[0].Pos())
1145 var e = p.lineFor(list[len(list)-1].End())
1147 // list spans multiple lines
1148 n := 0 // multi-line element count
1150 for _, x := range list {
1151 xb := p.lineFor(x.Pos())
1152 xe := p.lineFor(x.End())
1154 // x is not starting on the same
1155 // line as the previous one ended
1159 // x is a multi-line element
1170 func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
1173 switch s := stmt.(type) {
1180 case *ast.EmptyStmt:
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)
1189 p.print(s.Colon, token.COLON, indent)
1190 if e, isEmpty := s.Stmt.(*ast.EmptyStmt); isEmpty {
1192 p.print(newline, e.Pos(), token.SEMICOLON)
1196 p.linebreak(p.lineFor(s.Stmt.Pos()), 1, ignore, true)
1198 p.stmt(s.Stmt, nextIsRBrace)
1206 p.expr0(s.Chan, depth)
1207 p.print(blank, s.Arrow, token.ARROW, blank)
1208 p.expr0(s.Value, depth)
1210 case *ast.IncDecStmt:
1212 p.expr0(s.X, depth+1)
1213 p.print(s.TokPos, s.Tok)
1215 case *ast.AssignStmt:
1217 if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
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)
1225 p.print(token.GO, blank)
1228 case *ast.DeferStmt:
1229 p.print(token.DEFER, blank)
1232 case *ast.ReturnStmt:
1233 p.print(token.RETURN)
1234 if s.Results != nil {
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) {
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)
1248 p.exprList(token.NoPos, s.Results, 1, 0, token.NoPos, false)
1252 case *ast.BranchStmt:
1259 case *ast.BlockStmt:
1264 p.controlClause(false, s.Init, s.Cond, 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)
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)
1281 case *ast.CaseClause:
1283 p.print(token.CASE, blank)
1284 p.exprList(s.Pos(), s.List, 1, 0, s.Colon, false)
1286 p.print(token.DEFAULT)
1288 p.print(s.Colon, token.COLON)
1289 p.stmtList(s.Body, 1, nextIsRBrace)
1291 case *ast.SwitchStmt:
1292 p.print(token.SWITCH)
1293 p.controlClause(false, s.Init, s.Tag, nil)
1296 case *ast.TypeSwitchStmt:
1297 p.print(token.SWITCH)
1300 p.stmt(s.Init, false)
1301 p.print(token.SEMICOLON)
1304 p.stmt(s.Assign, false)
1308 case *ast.CommClause:
1310 p.print(token.CASE, blank)
1311 p.stmt(s.Comm, false)
1313 p.print(token.DEFAULT)
1315 p.print(s.Colon, token.COLON)
1316 p.stmtList(s.Body, 1, nextIsRBrace)
1318 case *ast.SelectStmt:
1319 p.print(token.SELECT, blank)
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)
1330 p.controlClause(true, s.Init, s.Cond, s.Post)
1333 case *ast.RangeStmt:
1334 p.print(token.FOR, blank)
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)
1343 p.print(blank, s.TokPos, s.Tok, blank)
1345 p.print(token.RANGE, blank)
1346 p.expr(stripParens(s.X))
1351 panic("unreachable")
1355 // ----------------------------------------------------------------------------
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.
1363 // For example, the declaration:
1366 // foobar int = 42 // comment
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).
1376 // matrix formatted result
1378 // T V -> T V -> true there is a T and so the type
1379 // - V - V true column must be kept
1381 // - V V - false V is moved into T column
1383 func keepTypeColumn(specs []ast.Spec) []bool {
1384 m := make([]bool, len(specs))
1386 populate := func(i, j int, keepType bool) {
1394 i0 := -1 // if i0 >= 0 we are in a run and i0 is the start of the run
1396 for i, s := range specs {
1397 t := s.(*ast.ValueSpec)
1398 if t.Values != nil {
1400 // start of a run of ValueSpecs with non-nil Values
1407 populate(i0, i, keepType)
1417 populate(i0, len(specs), keepType)
1423 func (p *printer) valueSpec(s *ast.ValueSpec, keepType bool) {
1425 p.identList(s.Names, false) // always present
1427 if s.Type != nil || keepType {
1434 if s.Values != nil {
1435 p.print(vtab, token.ASSIGN, blank)
1436 p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false)
1439 if s.Comment != nil {
1440 for ; extraTabs > 0; extraTabs-- {
1443 p.setComment(s.Comment)
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.
1455 // if we don't have a proper string, be conservative and return whatever we have
1456 if lit.Kind != token.STRING {
1459 s, err := strconv.Unquote(lit.Value)
1464 // if the string is an invalid path, return whatever we have
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
1475 const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
1476 for _, r := range s {
1477 if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
1482 // otherwise, return the double-quoted path
1483 s = strconv.Quote(s)
1485 return lit // nothing wrong with lit
1487 return &ast.BasicLit{ValuePos: lit.ValuePos, Kind: token.STRING, Value: s}
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.
1494 func (p *printer) spec(spec ast.Spec, n int, doIndent bool) {
1495 switch s := spec.(type) {
1496 case *ast.ImportSpec:
1502 p.expr(sanitizeImportPath(s.Path))
1503 p.setComment(s.Comment)
1506 case *ast.ValueSpec:
1508 p.internalError("expected n = 1; got", n)
1511 p.identList(s.Names, doIndent) // always present
1516 if s.Values != nil {
1517 p.print(blank, token.ASSIGN, blank)
1518 p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false)
1520 p.setComment(s.Comment)
1530 if s.Assign.IsValid() {
1531 p.print(token.ASSIGN, blank)
1534 p.setComment(s.Comment)
1537 panic("unreachable")
1541 func (p *printer) genDecl(d *ast.GenDecl) {
1543 p.print(d.Pos(), d.Tok, blank)
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)
1555 for i, s := range d.Specs {
1557 p.linebreak(p.lineFor(s.Pos()), 1, ignore, p.linesFrom(line) > 0)
1560 p.valueSpec(s.(*ast.ValueSpec), keepType[i])
1564 for i, s := range d.Specs {
1566 p.linebreak(p.lineFor(s.Pos()), 1, ignore, p.linesFrom(line) > 0)
1572 p.print(unindent, formfeed)
1574 p.print(d.Rparen, token.RPAREN)
1576 } else if len(d.Specs) > 0 {
1577 // single declaration
1578 p.spec(d.Specs[0], 1, true)
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.
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 {
1596 size = maxSize + 1 // assume n doesn't fit
1597 p.nodeSizes[n] = size
1599 // nodeSize computation must be independent of particular
1600 // style so that we always get the same decision; print
1602 cfg := Config{Mode: RawFormat}
1603 var buf bytes.Buffer
1604 if err := cfg.fprint(&buf, p.fset, n, p.nodeSizes); err != nil {
1607 if buf.Len() <= maxSize {
1608 for _, ch := range buf.Bytes() {
1613 size = buf.Len() // n fits
1614 p.nodeSizes[n] = size
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
1629 // bodySize is like nodeSize but it is specialized for *ast.BlockStmt's.
1630 func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int {
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
1637 if len(b.List) > 5 {
1638 // too many statements - don't make it a one-liner
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
1648 bodySize += 2 // space for a semicolon and blank
1650 bodySize += p.nodeSize(s, maxSize)
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 "}".
1661 func (p *printer) funcBody(headerSize int, sep whiteSpace, b *ast.BlockStmt) {
1666 // save/restore composite literal nesting level
1667 defer func(level int) {
1673 if headerSize+p.bodySize(b, maxSize) <= maxSize {
1674 p.print(sep, b.Lbrace, token.LBRACE)
1675 if len(b.List) > 0 {
1677 for i, s := range b.List {
1679 p.print(token.SEMICOLON, blank)
1681 p.stmt(s, i == len(b.List)-1)
1685 p.print(noExtraLinebreak, b.Rbrace, token.RBRACE, noExtraLinebreak)
1690 p.print(blank) // always use blank
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
1705 func (p *printer) funcDecl(d *ast.FuncDecl) {
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 ")
1713 p.parameters(d.Recv) // method: print receiver
1717 p.signature(d.Type.Params, d.Type.Results)
1718 p.funcBody(p.distanceFrom(d.Pos(), startCol), vtab, d.Body)
1721 func (p *printer) decl(decl ast.Decl) {
1722 switch d := decl.(type) {
1724 p.print(d.Pos(), "BadDecl")
1730 panic("unreachable")
1734 // ----------------------------------------------------------------------------
1737 func declToken(decl ast.Decl) (tok token.Token) {
1739 switch d := decl.(type) {
1748 func (p *printer) declList(list []ast.Decl) {
1749 tok := token.ILLEGAL
1750 for _, d := range list {
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)
1764 if prev != tok || getDoc(d) != nil {
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)
1775 func (p *printer) file(src *ast.File) {
1776 p.setComment(src.Doc)
1777 p.print(src.Pos(), token.PACKAGE, blank)
1779 p.declList(src.Decls)