Source file src/go/ast/ast.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  // Package ast declares the types used to represent syntax trees for Go
     6  // packages.
     7  package ast
     8  
     9  import (
    10  	"go/token"
    11  	"strings"
    12  )
    13  
    14  // ----------------------------------------------------------------------------
    15  // Interfaces
    16  //
    17  // There are 3 main classes of nodes: Expressions and type nodes,
    18  // statement nodes, and declaration nodes. The node names usually
    19  // match the corresponding Go spec production names to which they
    20  // correspond. The node fields correspond to the individual parts
    21  // of the respective productions.
    22  //
    23  // All nodes contain position information marking the beginning of
    24  // the corresponding source text segment; it is accessible via the
    25  // Pos accessor method. Nodes may contain additional position info
    26  // for language constructs where comments may be found between parts
    27  // of the construct (typically any larger, parenthesized subpart).
    28  // That position information is needed to properly position comments
    29  // when printing the construct.
    30  
    31  // All node types implement the Node interface.
    32  type Node interface {
    33  	Pos() token.Pos // position of first character belonging to the node
    34  	End() token.Pos // position of first character immediately after the node
    35  }
    36  
    37  // All expression nodes implement the Expr interface.
    38  type Expr interface {
    39  	Node
    40  	exprNode()
    41  }
    42  
    43  // All statement nodes implement the Stmt interface.
    44  type Stmt interface {
    45  	Node
    46  	stmtNode()
    47  }
    48  
    49  // All declaration nodes implement the Decl interface.
    50  type Decl interface {
    51  	Node
    52  	declNode()
    53  }
    54  
    55  // ----------------------------------------------------------------------------
    56  // Comments
    57  
    58  // A Comment node represents a single //-style or /*-style comment.
    59  //
    60  // The Text field contains the comment text without carriage returns (\r) that
    61  // may have been present in the source. Because a comment's end position is
    62  // computed using len(Text), the position reported by [Comment.End] does not match the
    63  // true source end position for comments containing carriage returns.
    64  type Comment struct {
    65  	Slash token.Pos // position of "/" starting the comment
    66  	Text  string    // comment text (excluding '\n' for //-style comments)
    67  }
    68  
    69  func (c *Comment) Pos() token.Pos { return c.Slash }
    70  func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) }
    71  
    72  // A CommentGroup represents a sequence of comments
    73  // with no other tokens and no empty lines between.
    74  type CommentGroup struct {
    75  	List []*Comment // len(List) > 0
    76  }
    77  
    78  func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() }
    79  func (g *CommentGroup) End() token.Pos { return g.List[len(g.List)-1].End() }
    80  
    81  func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' }
    82  
    83  func stripTrailingWhitespace(s string) string {
    84  	i := len(s)
    85  	for i > 0 && isWhitespace(s[i-1]) {
    86  		i--
    87  	}
    88  	return s[0:i]
    89  }
    90  
    91  // Text returns the text of the comment.
    92  // Comment markers (//, /*, and */), the first space of a line comment, and
    93  // leading and trailing empty lines are removed.
    94  // Comment directives like "//line" and "//go:noinline" are also removed.
    95  // Multiple empty lines are reduced to one, and trailing space on lines is trimmed.
    96  // Unless the result is empty, it is newline-terminated.
    97  func (g *CommentGroup) Text() string {
    98  	if g == nil {
    99  		return ""
   100  	}
   101  	comments := make([]string, len(g.List))
   102  	for i, c := range g.List {
   103  		comments[i] = c.Text
   104  	}
   105  
   106  	lines := make([]string, 0, 10) // most comments are less than 10 lines
   107  	for _, c := range comments {
   108  		// Remove comment markers.
   109  		// The parser has given us exactly the comment text.
   110  		switch c[1] {
   111  		case '/':
   112  			//-style comment (no newline at the end)
   113  			c = c[2:]
   114  			if len(c) == 0 {
   115  				// empty line
   116  				break
   117  			}
   118  			if c[0] == ' ' {
   119  				// strip first space - required for Example tests
   120  				c = c[1:]
   121  				break
   122  			}
   123  			if isDirective(c) {
   124  				// Ignore //go:noinline, //line, and so on.
   125  				continue
   126  			}
   127  		case '*':
   128  			/*-style comment */
   129  			c = c[2 : len(c)-2]
   130  		}
   131  
   132  		// Split on newlines.
   133  		cl := strings.Split(c, "\n")
   134  
   135  		// Walk lines, stripping trailing white space and adding to list.
   136  		for _, l := range cl {
   137  			lines = append(lines, stripTrailingWhitespace(l))
   138  		}
   139  	}
   140  
   141  	// Remove leading blank lines; convert runs of
   142  	// interior blank lines to a single blank line.
   143  	n := 0
   144  	for _, line := range lines {
   145  		if line != "" || n > 0 && lines[n-1] != "" {
   146  			lines[n] = line
   147  			n++
   148  		}
   149  	}
   150  	lines = lines[0:n]
   151  
   152  	// Add final "" entry to get trailing newline from Join.
   153  	if n > 0 && lines[n-1] != "" {
   154  		lines = append(lines, "")
   155  	}
   156  
   157  	return strings.Join(lines, "\n")
   158  }
   159  
   160  // isDirective reports whether c is a comment directive.
   161  // This code is also in go/printer.
   162  func isDirective(c string) bool {
   163  	// "//line " is a line directive.
   164  	// "//extern " is for gccgo.
   165  	// "//export " is for cgo.
   166  	// (The // has been removed.)
   167  	if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") {
   168  		return true
   169  	}
   170  
   171  	// "//[a-z0-9]+:[a-z0-9]"
   172  	// (The // has been removed.)
   173  	colon := strings.Index(c, ":")
   174  	if colon <= 0 || colon+1 >= len(c) {
   175  		return false
   176  	}
   177  	for i := 0; i <= colon+1; i++ {
   178  		if i == colon {
   179  			continue
   180  		}
   181  		b := c[i]
   182  		if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') {
   183  			return false
   184  		}
   185  	}
   186  	return true
   187  }
   188  
   189  // ----------------------------------------------------------------------------
   190  // Expressions and types
   191  
   192  // A Field represents a Field declaration list in a struct type,
   193  // a method list in an interface type, or a parameter/result declaration
   194  // in a signature.
   195  // [Field.Names] is nil for unnamed parameters (parameter lists which only contain types)
   196  // and embedded struct fields. In the latter case, the field name is the type name.
   197  type Field struct {
   198  	Doc     *CommentGroup // associated documentation; or nil
   199  	Names   []*Ident      // field/method/(type) parameter names; or nil
   200  	Type    Expr          // field/method/parameter type; or nil
   201  	Tag     *BasicLit     // field tag; or nil
   202  	Comment *CommentGroup // line comments; or nil
   203  }
   204  
   205  func (f *Field) Pos() token.Pos {
   206  	if len(f.Names) > 0 {
   207  		return f.Names[0].Pos()
   208  	}
   209  	if f.Type != nil {
   210  		return f.Type.Pos()
   211  	}
   212  	return token.NoPos
   213  }
   214  
   215  func (f *Field) End() token.Pos {
   216  	if f.Tag != nil {
   217  		return f.Tag.End()
   218  	}
   219  	if f.Type != nil {
   220  		return f.Type.End()
   221  	}
   222  	if len(f.Names) > 0 {
   223  		return f.Names[len(f.Names)-1].End()
   224  	}
   225  	return token.NoPos
   226  }
   227  
   228  // A FieldList represents a list of Fields, enclosed by parentheses,
   229  // curly braces, or square brackets.
   230  type FieldList struct {
   231  	Opening token.Pos // position of opening parenthesis/brace/bracket, if any
   232  	List    []*Field  // field list; or nil
   233  	Closing token.Pos // position of closing parenthesis/brace/bracket, if any
   234  }
   235  
   236  func (f *FieldList) Pos() token.Pos {
   237  	if f.Opening.IsValid() {
   238  		return f.Opening
   239  	}
   240  	// the list should not be empty in this case;
   241  	// be conservative and guard against bad ASTs
   242  	if len(f.List) > 0 {
   243  		return f.List[0].Pos()
   244  	}
   245  	return token.NoPos
   246  }
   247  
   248  func (f *FieldList) End() token.Pos {
   249  	if f.Closing.IsValid() {
   250  		return f.Closing + 1
   251  	}
   252  	// the list should not be empty in this case;
   253  	// be conservative and guard against bad ASTs
   254  	if n := len(f.List); n > 0 {
   255  		return f.List[n-1].End()
   256  	}
   257  	return token.NoPos
   258  }
   259  
   260  // NumFields returns the number of parameters or struct fields represented by a [FieldList].
   261  func (f *FieldList) NumFields() int {
   262  	n := 0
   263  	if f != nil {
   264  		for _, g := range f.List {
   265  			m := len(g.Names)
   266  			if m == 0 {
   267  				m = 1
   268  			}
   269  			n += m
   270  		}
   271  	}
   272  	return n
   273  }
   274  
   275  // An expression is represented by a tree consisting of one
   276  // or more of the following concrete expression nodes.
   277  type (
   278  	// A BadExpr node is a placeholder for an expression containing
   279  	// syntax errors for which a correct expression node cannot be
   280  	// created.
   281  	//
   282  	BadExpr struct {
   283  		From, To token.Pos // position range of bad expression
   284  	}
   285  
   286  	// An Ident node represents an identifier.
   287  	Ident struct {
   288  		NamePos token.Pos // identifier position
   289  		Name    string    // identifier name
   290  		Obj     *Object   // denoted object, or nil. Deprecated: see Object.
   291  	}
   292  
   293  	// An Ellipsis node stands for the "..." type in a
   294  	// parameter list or the "..." length in an array type.
   295  	//
   296  	Ellipsis struct {
   297  		Ellipsis token.Pos // position of "..."
   298  		Elt      Expr      // ellipsis element type (parameter lists only); or nil
   299  	}
   300  
   301  	// A BasicLit node represents a literal of basic type.
   302  	//
   303  	// Note that for the CHAR and STRING kinds, the literal is stored
   304  	// with its quotes. For example, for a double-quoted STRING, the
   305  	// first and the last rune in the Value field will be ". The
   306  	// [strconv.Unquote] and [strconv.UnquoteChar] functions can be
   307  	// used to unquote STRING and CHAR values, respectively.
   308  	BasicLit struct {
   309  		ValuePos token.Pos   // literal position
   310  		Kind     token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
   311  		Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
   312  	}
   313  
   314  	// A FuncLit node represents a function literal.
   315  	FuncLit struct {
   316  		Type *FuncType  // function type
   317  		Body *BlockStmt // function body
   318  	}
   319  
   320  	// A CompositeLit node represents a composite literal.
   321  	CompositeLit struct {
   322  		Type       Expr      // literal type; or nil
   323  		Lbrace     token.Pos // position of "{"
   324  		Elts       []Expr    // list of composite elements; or nil
   325  		Rbrace     token.Pos // position of "}"
   326  		Incomplete bool      // true if (source) expressions are missing in the Elts list
   327  	}
   328  
   329  	// A ParenExpr node represents a parenthesized expression.
   330  	ParenExpr struct {
   331  		Lparen token.Pos // position of "("
   332  		X      Expr      // parenthesized expression
   333  		Rparen token.Pos // position of ")"
   334  	}
   335  
   336  	// A SelectorExpr node represents an expression followed by a selector.
   337  	SelectorExpr struct {
   338  		X   Expr   // expression
   339  		Sel *Ident // field selector
   340  	}
   341  
   342  	// An IndexExpr node represents an expression followed by an index.
   343  	IndexExpr struct {
   344  		X      Expr      // expression
   345  		Lbrack token.Pos // position of "["
   346  		Index  Expr      // index expression
   347  		Rbrack token.Pos // position of "]"
   348  	}
   349  
   350  	// An IndexListExpr node represents an expression followed by multiple
   351  	// indices.
   352  	IndexListExpr struct {
   353  		X       Expr      // expression
   354  		Lbrack  token.Pos // position of "["
   355  		Indices []Expr    // index expressions
   356  		Rbrack  token.Pos // position of "]"
   357  	}
   358  
   359  	// A SliceExpr node represents an expression followed by slice indices.
   360  	SliceExpr struct {
   361  		X      Expr      // expression
   362  		Lbrack token.Pos // position of "["
   363  		Low    Expr      // begin of slice range; or nil
   364  		High   Expr      // end of slice range; or nil
   365  		Max    Expr      // maximum capacity of slice; or nil
   366  		Slice3 bool      // true if 3-index slice (2 colons present)
   367  		Rbrack token.Pos // position of "]"
   368  	}
   369  
   370  	// A TypeAssertExpr node represents an expression followed by a
   371  	// type assertion.
   372  	//
   373  	TypeAssertExpr struct {
   374  		X      Expr      // expression
   375  		Lparen token.Pos // position of "("
   376  		Type   Expr      // asserted type; nil means type switch X.(type)
   377  		Rparen token.Pos // position of ")"
   378  	}
   379  
   380  	// A CallExpr node represents an expression followed by an argument list.
   381  	CallExpr struct {
   382  		Fun      Expr      // function expression
   383  		Lparen   token.Pos // position of "("
   384  		Args     []Expr    // function arguments; or nil
   385  		Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
   386  		Rparen   token.Pos // position of ")"
   387  	}
   388  
   389  	// A StarExpr node represents an expression of the form "*" Expression.
   390  	// Semantically it could be a unary "*" expression, or a pointer type.
   391  	//
   392  	StarExpr struct {
   393  		Star token.Pos // position of "*"
   394  		X    Expr      // operand
   395  	}
   396  
   397  	// A UnaryExpr node represents a unary expression.
   398  	// Unary "*" expressions are represented via StarExpr nodes.
   399  	//
   400  	UnaryExpr struct {
   401  		OpPos token.Pos   // position of Op
   402  		Op    token.Token // operator
   403  		X     Expr        // operand
   404  	}
   405  
   406  	// A BinaryExpr node represents a binary expression.
   407  	BinaryExpr struct {
   408  		X     Expr        // left operand
   409  		OpPos token.Pos   // position of Op
   410  		Op    token.Token // operator
   411  		Y     Expr        // right operand
   412  	}
   413  
   414  	// A KeyValueExpr node represents (key : value) pairs
   415  	// in composite literals.
   416  	//
   417  	KeyValueExpr struct {
   418  		Key   Expr
   419  		Colon token.Pos // position of ":"
   420  		Value Expr
   421  	}
   422  )
   423  
   424  // The direction of a channel type is indicated by a bit
   425  // mask including one or both of the following constants.
   426  type ChanDir int
   427  
   428  const (
   429  	SEND ChanDir = 1 << iota
   430  	RECV
   431  )
   432  
   433  // A type is represented by a tree consisting of one
   434  // or more of the following type-specific expression
   435  // nodes.
   436  type (
   437  	// An ArrayType node represents an array or slice type.
   438  	ArrayType struct {
   439  		Lbrack token.Pos // position of "["
   440  		Len    Expr      // Ellipsis node for [...]T array types, nil for slice types
   441  		Elt    Expr      // element type
   442  	}
   443  
   444  	// A StructType node represents a struct type.
   445  	StructType struct {
   446  		Struct     token.Pos  // position of "struct" keyword
   447  		Fields     *FieldList // list of field declarations
   448  		Incomplete bool       // true if (source) fields are missing in the Fields list
   449  	}
   450  
   451  	// Pointer types are represented via StarExpr nodes.
   452  
   453  	// A FuncType node represents a function type.
   454  	FuncType struct {
   455  		Func       token.Pos  // position of "func" keyword (token.NoPos if there is no "func")
   456  		TypeParams *FieldList // type parameters; or nil
   457  		Params     *FieldList // (incoming) parameters; non-nil
   458  		Results    *FieldList // (outgoing) results; or nil
   459  	}
   460  
   461  	// An InterfaceType node represents an interface type.
   462  	InterfaceType struct {
   463  		Interface  token.Pos  // position of "interface" keyword
   464  		Methods    *FieldList // list of embedded interfaces, methods, or types
   465  		Incomplete bool       // true if (source) methods or types are missing in the Methods list
   466  	}
   467  
   468  	// A MapType node represents a map type.
   469  	MapType struct {
   470  		Map   token.Pos // position of "map" keyword
   471  		Key   Expr
   472  		Value Expr
   473  	}
   474  
   475  	// A ChanType node represents a channel type.
   476  	ChanType struct {
   477  		Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
   478  		Arrow token.Pos // position of "<-" (token.NoPos if there is no "<-")
   479  		Dir   ChanDir   // channel direction
   480  		Value Expr      // value type
   481  	}
   482  )
   483  
   484  // Pos and End implementations for expression/type nodes.
   485  
   486  func (x *BadExpr) Pos() token.Pos  { return x.From }
   487  func (x *Ident) Pos() token.Pos    { return x.NamePos }
   488  func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
   489  func (x *BasicLit) Pos() token.Pos { return x.ValuePos }
   490  func (x *FuncLit) Pos() token.Pos  { return x.Type.Pos() }
   491  func (x *CompositeLit) Pos() token.Pos {
   492  	if x.Type != nil {
   493  		return x.Type.Pos()
   494  	}
   495  	return x.Lbrace
   496  }
   497  func (x *ParenExpr) Pos() token.Pos      { return x.Lparen }
   498  func (x *SelectorExpr) Pos() token.Pos   { return x.X.Pos() }
   499  func (x *IndexExpr) Pos() token.Pos      { return x.X.Pos() }
   500  func (x *IndexListExpr) Pos() token.Pos  { return x.X.Pos() }
   501  func (x *SliceExpr) Pos() token.Pos      { return x.X.Pos() }
   502  func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
   503  func (x *CallExpr) Pos() token.Pos       { return x.Fun.Pos() }
   504  func (x *StarExpr) Pos() token.Pos       { return x.Star }
   505  func (x *UnaryExpr) Pos() token.Pos      { return x.OpPos }
   506  func (x *BinaryExpr) Pos() token.Pos     { return x.X.Pos() }
   507  func (x *KeyValueExpr) Pos() token.Pos   { return x.Key.Pos() }
   508  func (x *ArrayType) Pos() token.Pos      { return x.Lbrack }
   509  func (x *StructType) Pos() token.Pos     { return x.Struct }
   510  func (x *FuncType) Pos() token.Pos {
   511  	if x.Func.IsValid() || x.Params == nil { // see issue 3870
   512  		return x.Func
   513  	}
   514  	return x.Params.Pos() // interface method declarations have no "func" keyword
   515  }
   516  func (x *InterfaceType) Pos() token.Pos { return x.Interface }
   517  func (x *MapType) Pos() token.Pos       { return x.Map }
   518  func (x *ChanType) Pos() token.Pos      { return x.Begin }
   519  
   520  func (x *BadExpr) End() token.Pos { return x.To }
   521  func (x *Ident) End() token.Pos   { return token.Pos(int(x.NamePos) + len(x.Name)) }
   522  func (x *Ellipsis) End() token.Pos {
   523  	if x.Elt != nil {
   524  		return x.Elt.End()
   525  	}
   526  	return x.Ellipsis + 3 // len("...")
   527  }
   528  func (x *BasicLit) End() token.Pos       { return token.Pos(int(x.ValuePos) + len(x.Value)) }
   529  func (x *FuncLit) End() token.Pos        { return x.Body.End() }
   530  func (x *CompositeLit) End() token.Pos   { return x.Rbrace + 1 }
   531  func (x *ParenExpr) End() token.Pos      { return x.Rparen + 1 }
   532  func (x *SelectorExpr) End() token.Pos   { return x.Sel.End() }
   533  func (x *IndexExpr) End() token.Pos      { return x.Rbrack + 1 }
   534  func (x *IndexListExpr) End() token.Pos  { return x.Rbrack + 1 }
   535  func (x *SliceExpr) End() token.Pos      { return x.Rbrack + 1 }
   536  func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 }
   537  func (x *CallExpr) End() token.Pos       { return x.Rparen + 1 }
   538  func (x *StarExpr) End() token.Pos       { return x.X.End() }
   539  func (x *UnaryExpr) End() token.Pos      { return x.X.End() }
   540  func (x *BinaryExpr) End() token.Pos     { return x.Y.End() }
   541  func (x *KeyValueExpr) End() token.Pos   { return x.Value.End() }
   542  func (x *ArrayType) End() token.Pos      { return x.Elt.End() }
   543  func (x *StructType) End() token.Pos     { return x.Fields.End() }
   544  func (x *FuncType) End() token.Pos {
   545  	if x.Results != nil {
   546  		return x.Results.End()
   547  	}
   548  	return x.Params.End()
   549  }
   550  func (x *InterfaceType) End() token.Pos { return x.Methods.End() }
   551  func (x *MapType) End() token.Pos       { return x.Value.End() }
   552  func (x *ChanType) End() token.Pos      { return x.Value.End() }
   553  
   554  // exprNode() ensures that only expression/type nodes can be
   555  // assigned to an Expr.
   556  func (*BadExpr) exprNode()        {}
   557  func (*Ident) exprNode()          {}
   558  func (*Ellipsis) exprNode()       {}
   559  func (*BasicLit) exprNode()       {}
   560  func (*FuncLit) exprNode()        {}
   561  func (*CompositeLit) exprNode()   {}
   562  func (*ParenExpr) exprNode()      {}
   563  func (*SelectorExpr) exprNode()   {}
   564  func (*IndexExpr) exprNode()      {}
   565  func (*IndexListExpr) exprNode()  {}
   566  func (*SliceExpr) exprNode()      {}
   567  func (*TypeAssertExpr) exprNode() {}
   568  func (*CallExpr) exprNode()       {}
   569  func (*StarExpr) exprNode()       {}
   570  func (*UnaryExpr) exprNode()      {}
   571  func (*BinaryExpr) exprNode()     {}
   572  func (*KeyValueExpr) exprNode()   {}
   573  
   574  func (*ArrayType) exprNode()     {}
   575  func (*StructType) exprNode()    {}
   576  func (*FuncType) exprNode()      {}
   577  func (*InterfaceType) exprNode() {}
   578  func (*MapType) exprNode()       {}
   579  func (*ChanType) exprNode()      {}
   580  
   581  // ----------------------------------------------------------------------------
   582  // Convenience functions for Idents
   583  
   584  // NewIdent creates a new [Ident] without position.
   585  // Useful for ASTs generated by code other than the Go parser.
   586  func NewIdent(name string) *Ident { return &Ident{token.NoPos, name, nil} }
   587  
   588  // IsExported reports whether name starts with an upper-case letter.
   589  func IsExported(name string) bool { return token.IsExported(name) }
   590  
   591  // IsExported reports whether id starts with an upper-case letter.
   592  func (id *Ident) IsExported() bool { return token.IsExported(id.Name) }
   593  
   594  func (id *Ident) String() string {
   595  	if id != nil {
   596  		return id.Name
   597  	}
   598  	return "<nil>"
   599  }
   600  
   601  // ----------------------------------------------------------------------------
   602  // Statements
   603  
   604  // A statement is represented by a tree consisting of one
   605  // or more of the following concrete statement nodes.
   606  type (
   607  	// A BadStmt node is a placeholder for statements containing
   608  	// syntax errors for which no correct statement nodes can be
   609  	// created.
   610  	//
   611  	BadStmt struct {
   612  		From, To token.Pos // position range of bad statement
   613  	}
   614  
   615  	// A DeclStmt node represents a declaration in a statement list.
   616  	DeclStmt struct {
   617  		Decl Decl // *GenDecl with CONST, TYPE, or VAR token
   618  	}
   619  
   620  	// An EmptyStmt node represents an empty statement.
   621  	// The "position" of the empty statement is the position
   622  	// of the immediately following (explicit or implicit) semicolon.
   623  	//
   624  	EmptyStmt struct {
   625  		Semicolon token.Pos // position of following ";"
   626  		Implicit  bool      // if set, ";" was omitted in the source
   627  	}
   628  
   629  	// A LabeledStmt node represents a labeled statement.
   630  	LabeledStmt struct {
   631  		Label *Ident
   632  		Colon token.Pos // position of ":"
   633  		Stmt  Stmt
   634  	}
   635  
   636  	// An ExprStmt node represents a (stand-alone) expression
   637  	// in a statement list.
   638  	//
   639  	ExprStmt struct {
   640  		X Expr // expression
   641  	}
   642  
   643  	// A SendStmt node represents a send statement.
   644  	SendStmt struct {
   645  		Chan  Expr
   646  		Arrow token.Pos // position of "<-"
   647  		Value Expr
   648  	}
   649  
   650  	// An IncDecStmt node represents an increment or decrement statement.
   651  	IncDecStmt struct {
   652  		X      Expr
   653  		TokPos token.Pos   // position of Tok
   654  		Tok    token.Token // INC or DEC
   655  	}
   656  
   657  	// An AssignStmt node represents an assignment or
   658  	// a short variable declaration.
   659  	//
   660  	AssignStmt struct {
   661  		Lhs    []Expr
   662  		TokPos token.Pos   // position of Tok
   663  		Tok    token.Token // assignment token, DEFINE
   664  		Rhs    []Expr
   665  	}
   666  
   667  	// A GoStmt node represents a go statement.
   668  	GoStmt struct {
   669  		Go   token.Pos // position of "go" keyword
   670  		Call *CallExpr
   671  	}
   672  
   673  	// A DeferStmt node represents a defer statement.
   674  	DeferStmt struct {
   675  		Defer token.Pos // position of "defer" keyword
   676  		Call  *CallExpr
   677  	}
   678  
   679  	// A ReturnStmt node represents a return statement.
   680  	ReturnStmt struct {
   681  		Return  token.Pos // position of "return" keyword
   682  		Results []Expr    // result expressions; or nil
   683  	}
   684  
   685  	// A BranchStmt node represents a break, continue, goto,
   686  	// or fallthrough statement.
   687  	//
   688  	BranchStmt struct {
   689  		TokPos token.Pos   // position of Tok
   690  		Tok    token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
   691  		Label  *Ident      // label name; or nil
   692  	}
   693  
   694  	// A BlockStmt node represents a braced statement list.
   695  	BlockStmt struct {
   696  		Lbrace token.Pos // position of "{"
   697  		List   []Stmt
   698  		Rbrace token.Pos // position of "}", if any (may be absent due to syntax error)
   699  	}
   700  
   701  	// An IfStmt node represents an if statement.
   702  	IfStmt struct {
   703  		If   token.Pos // position of "if" keyword
   704  		Init Stmt      // initialization statement; or nil
   705  		Cond Expr      // condition
   706  		Body *BlockStmt
   707  		Else Stmt // else branch; or nil
   708  	}
   709  
   710  	// A CaseClause represents a case of an expression or type switch statement.
   711  	CaseClause struct {
   712  		Case  token.Pos // position of "case" or "default" keyword
   713  		List  []Expr    // list of expressions or types; nil means default case
   714  		Colon token.Pos // position of ":"
   715  		Body  []Stmt    // statement list; or nil
   716  	}
   717  
   718  	// A SwitchStmt node represents an expression switch statement.
   719  	SwitchStmt struct {
   720  		Switch token.Pos  // position of "switch" keyword
   721  		Init   Stmt       // initialization statement; or nil
   722  		Tag    Expr       // tag expression; or nil
   723  		Body   *BlockStmt // CaseClauses only
   724  	}
   725  
   726  	// A TypeSwitchStmt node represents a type switch statement.
   727  	TypeSwitchStmt struct {
   728  		Switch token.Pos  // position of "switch" keyword
   729  		Init   Stmt       // initialization statement; or nil
   730  		Assign Stmt       // x := y.(type) or y.(type)
   731  		Body   *BlockStmt // CaseClauses only
   732  	}
   733  
   734  	// A CommClause node represents a case of a select statement.
   735  	CommClause struct {
   736  		Case  token.Pos // position of "case" or "default" keyword
   737  		Comm  Stmt      // send or receive statement; nil means default case
   738  		Colon token.Pos // position of ":"
   739  		Body  []Stmt    // statement list; or nil
   740  	}
   741  
   742  	// A SelectStmt node represents a select statement.
   743  	SelectStmt struct {
   744  		Select token.Pos  // position of "select" keyword
   745  		Body   *BlockStmt // CommClauses only
   746  	}
   747  
   748  	// A ForStmt represents a for statement.
   749  	ForStmt struct {
   750  		For  token.Pos // position of "for" keyword
   751  		Init Stmt      // initialization statement; or nil
   752  		Cond Expr      // condition; or nil
   753  		Post Stmt      // post iteration statement; or nil
   754  		Body *BlockStmt
   755  	}
   756  
   757  	// A RangeStmt represents a for statement with a range clause.
   758  	RangeStmt struct {
   759  		For        token.Pos   // position of "for" keyword
   760  		Key, Value Expr        // Key, Value may be nil
   761  		TokPos     token.Pos   // position of Tok; invalid if Key == nil
   762  		Tok        token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
   763  		Range      token.Pos   // position of "range" keyword
   764  		X          Expr        // value to range over
   765  		Body       *BlockStmt
   766  	}
   767  )
   768  
   769  // Pos and End implementations for statement nodes.
   770  
   771  func (s *BadStmt) Pos() token.Pos        { return s.From }
   772  func (s *DeclStmt) Pos() token.Pos       { return s.Decl.Pos() }
   773  func (s *EmptyStmt) Pos() token.Pos      { return s.Semicolon }
   774  func (s *LabeledStmt) Pos() token.Pos    { return s.Label.Pos() }
   775  func (s *ExprStmt) Pos() token.Pos       { return s.X.Pos() }
   776  func (s *SendStmt) Pos() token.Pos       { return s.Chan.Pos() }
   777  func (s *IncDecStmt) Pos() token.Pos     { return s.X.Pos() }
   778  func (s *AssignStmt) Pos() token.Pos     { return s.Lhs[0].Pos() }
   779  func (s *GoStmt) Pos() token.Pos         { return s.Go }
   780  func (s *DeferStmt) Pos() token.Pos      { return s.Defer }
   781  func (s *ReturnStmt) Pos() token.Pos     { return s.Return }
   782  func (s *BranchStmt) Pos() token.Pos     { return s.TokPos }
   783  func (s *BlockStmt) Pos() token.Pos      { return s.Lbrace }
   784  func (s *IfStmt) Pos() token.Pos         { return s.If }
   785  func (s *CaseClause) Pos() token.Pos     { return s.Case }
   786  func (s *SwitchStmt) Pos() token.Pos     { return s.Switch }
   787  func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch }
   788  func (s *CommClause) Pos() token.Pos     { return s.Case }
   789  func (s *SelectStmt) Pos() token.Pos     { return s.Select }
   790  func (s *ForStmt) Pos() token.Pos        { return s.For }
   791  func (s *RangeStmt) Pos() token.Pos      { return s.For }
   792  
   793  func (s *BadStmt) End() token.Pos  { return s.To }
   794  func (s *DeclStmt) End() token.Pos { return s.Decl.End() }
   795  func (s *EmptyStmt) End() token.Pos {
   796  	if s.Implicit {
   797  		return s.Semicolon
   798  	}
   799  	return s.Semicolon + 1 /* len(";") */
   800  }
   801  func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() }
   802  func (s *ExprStmt) End() token.Pos    { return s.X.End() }
   803  func (s *SendStmt) End() token.Pos    { return s.Value.End() }
   804  func (s *IncDecStmt) End() token.Pos {
   805  	return s.TokPos + 2 /* len("++") */
   806  }
   807  func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() }
   808  func (s *GoStmt) End() token.Pos     { return s.Call.End() }
   809  func (s *DeferStmt) End() token.Pos  { return s.Call.End() }
   810  func (s *ReturnStmt) End() token.Pos {
   811  	if n := len(s.Results); n > 0 {
   812  		return s.Results[n-1].End()
   813  	}
   814  	return s.Return + 6 // len("return")
   815  }
   816  func (s *BranchStmt) End() token.Pos {
   817  	if s.Label != nil {
   818  		return s.Label.End()
   819  	}
   820  	return token.Pos(int(s.TokPos) + len(s.Tok.String()))
   821  }
   822  func (s *BlockStmt) End() token.Pos {
   823  	if s.Rbrace.IsValid() {
   824  		return s.Rbrace + 1
   825  	}
   826  	if n := len(s.List); n > 0 {
   827  		return s.List[n-1].End()
   828  	}
   829  	return s.Lbrace + 1
   830  }
   831  func (s *IfStmt) End() token.Pos {
   832  	if s.Else != nil {
   833  		return s.Else.End()
   834  	}
   835  	return s.Body.End()
   836  }
   837  func (s *CaseClause) End() token.Pos {
   838  	if n := len(s.Body); n > 0 {
   839  		return s.Body[n-1].End()
   840  	}
   841  	return s.Colon + 1
   842  }
   843  func (s *SwitchStmt) End() token.Pos     { return s.Body.End() }
   844  func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() }
   845  func (s *CommClause) End() token.Pos {
   846  	if n := len(s.Body); n > 0 {
   847  		return s.Body[n-1].End()
   848  	}
   849  	return s.Colon + 1
   850  }
   851  func (s *SelectStmt) End() token.Pos { return s.Body.End() }
   852  func (s *ForStmt) End() token.Pos    { return s.Body.End() }
   853  func (s *RangeStmt) End() token.Pos  { return s.Body.End() }
   854  
   855  // stmtNode() ensures that only statement nodes can be
   856  // assigned to a Stmt.
   857  func (*BadStmt) stmtNode()        {}
   858  func (*DeclStmt) stmtNode()       {}
   859  func (*EmptyStmt) stmtNode()      {}
   860  func (*LabeledStmt) stmtNode()    {}
   861  func (*ExprStmt) stmtNode()       {}
   862  func (*SendStmt) stmtNode()       {}
   863  func (*IncDecStmt) stmtNode()     {}
   864  func (*AssignStmt) stmtNode()     {}
   865  func (*GoStmt) stmtNode()         {}
   866  func (*DeferStmt) stmtNode()      {}
   867  func (*ReturnStmt) stmtNode()     {}
   868  func (*BranchStmt) stmtNode()     {}
   869  func (*BlockStmt) stmtNode()      {}
   870  func (*IfStmt) stmtNode()         {}
   871  func (*CaseClause) stmtNode()     {}
   872  func (*SwitchStmt) stmtNode()     {}
   873  func (*TypeSwitchStmt) stmtNode() {}
   874  func (*CommClause) stmtNode()     {}
   875  func (*SelectStmt) stmtNode()     {}
   876  func (*ForStmt) stmtNode()        {}
   877  func (*RangeStmt) stmtNode()      {}
   878  
   879  // ----------------------------------------------------------------------------
   880  // Declarations
   881  
   882  // A Spec node represents a single (non-parenthesized) import,
   883  // constant, type, or variable declaration.
   884  type (
   885  	// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
   886  	Spec interface {
   887  		Node
   888  		specNode()
   889  	}
   890  
   891  	// An ImportSpec node represents a single package import.
   892  	ImportSpec struct {
   893  		Doc     *CommentGroup // associated documentation; or nil
   894  		Name    *Ident        // local package name (including "."); or nil
   895  		Path    *BasicLit     // import path
   896  		Comment *CommentGroup // line comments; or nil
   897  		EndPos  token.Pos     // end of spec (overrides Path.Pos if nonzero)
   898  	}
   899  
   900  	// A ValueSpec node represents a constant or variable declaration
   901  	// (ConstSpec or VarSpec production).
   902  	//
   903  	ValueSpec struct {
   904  		Doc     *CommentGroup // associated documentation; or nil
   905  		Names   []*Ident      // value names (len(Names) > 0)
   906  		Type    Expr          // value type; or nil
   907  		Values  []Expr        // initial values; or nil
   908  		Comment *CommentGroup // line comments; or nil
   909  	}
   910  
   911  	// A TypeSpec node represents a type declaration (TypeSpec production).
   912  	TypeSpec struct {
   913  		Doc        *CommentGroup // associated documentation; or nil
   914  		Name       *Ident        // type name
   915  		TypeParams *FieldList    // type parameters; or nil
   916  		Assign     token.Pos     // position of '=', if any
   917  		Type       Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
   918  		Comment    *CommentGroup // line comments; or nil
   919  	}
   920  )
   921  
   922  // Pos and End implementations for spec nodes.
   923  
   924  func (s *ImportSpec) Pos() token.Pos {
   925  	if s.Name != nil {
   926  		return s.Name.Pos()
   927  	}
   928  	return s.Path.Pos()
   929  }
   930  func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
   931  func (s *TypeSpec) Pos() token.Pos  { return s.Name.Pos() }
   932  
   933  func (s *ImportSpec) End() token.Pos {
   934  	if s.EndPos != 0 {
   935  		return s.EndPos
   936  	}
   937  	return s.Path.End()
   938  }
   939  
   940  func (s *ValueSpec) End() token.Pos {
   941  	if n := len(s.Values); n > 0 {
   942  		return s.Values[n-1].End()
   943  	}
   944  	if s.Type != nil {
   945  		return s.Type.End()
   946  	}
   947  	return s.Names[len(s.Names)-1].End()
   948  }
   949  func (s *TypeSpec) End() token.Pos { return s.Type.End() }
   950  
   951  // specNode() ensures that only spec nodes can be
   952  // assigned to a Spec.
   953  func (*ImportSpec) specNode() {}
   954  func (*ValueSpec) specNode()  {}
   955  func (*TypeSpec) specNode()   {}
   956  
   957  // A declaration is represented by one of the following declaration nodes.
   958  type (
   959  	// A BadDecl node is a placeholder for a declaration containing
   960  	// syntax errors for which a correct declaration node cannot be
   961  	// created.
   962  	//
   963  	BadDecl struct {
   964  		From, To token.Pos // position range of bad declaration
   965  	}
   966  
   967  	// A GenDecl node (generic declaration node) represents an import,
   968  	// constant, type or variable declaration. A valid Lparen position
   969  	// (Lparen.IsValid()) indicates a parenthesized declaration.
   970  	//
   971  	// Relationship between Tok value and Specs element type:
   972  	//
   973  	//	token.IMPORT  *ImportSpec
   974  	//	token.CONST   *ValueSpec
   975  	//	token.TYPE    *TypeSpec
   976  	//	token.VAR     *ValueSpec
   977  	//
   978  	GenDecl struct {
   979  		Doc    *CommentGroup // associated documentation; or nil
   980  		TokPos token.Pos     // position of Tok
   981  		Tok    token.Token   // IMPORT, CONST, TYPE, or VAR
   982  		Lparen token.Pos     // position of '(', if any
   983  		Specs  []Spec
   984  		Rparen token.Pos // position of ')', if any
   985  	}
   986  
   987  	// A FuncDecl node represents a function declaration.
   988  	FuncDecl struct {
   989  		Doc  *CommentGroup // associated documentation; or nil
   990  		Recv *FieldList    // receiver (methods); or nil (functions)
   991  		Name *Ident        // function/method name
   992  		Type *FuncType     // function signature: type and value parameters, results, and position of "func" keyword
   993  		Body *BlockStmt    // function body; or nil for external (non-Go) function
   994  	}
   995  )
   996  
   997  // Pos and End implementations for declaration nodes.
   998  
   999  func (d *BadDecl) Pos() token.Pos  { return d.From }
  1000  func (d *GenDecl) Pos() token.Pos  { return d.TokPos }
  1001  func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
  1002  
  1003  func (d *BadDecl) End() token.Pos { return d.To }
  1004  func (d *GenDecl) End() token.Pos {
  1005  	if d.Rparen.IsValid() {
  1006  		return d.Rparen + 1
  1007  	}
  1008  	return d.Specs[0].End()
  1009  }
  1010  func (d *FuncDecl) End() token.Pos {
  1011  	if d.Body != nil {
  1012  		return d.Body.End()
  1013  	}
  1014  	return d.Type.End()
  1015  }
  1016  
  1017  // declNode() ensures that only declaration nodes can be
  1018  // assigned to a Decl.
  1019  func (*BadDecl) declNode()  {}
  1020  func (*GenDecl) declNode()  {}
  1021  func (*FuncDecl) declNode() {}
  1022  
  1023  // ----------------------------------------------------------------------------
  1024  // Files and packages
  1025  
  1026  // A File node represents a Go source file.
  1027  //
  1028  // The Comments list contains all comments in the source file in order of
  1029  // appearance, including the comments that are pointed to from other nodes
  1030  // via Doc and Comment fields.
  1031  //
  1032  // For correct printing of source code containing comments (using packages
  1033  // go/format and go/printer), special care must be taken to update comments
  1034  // when a File's syntax tree is modified: For printing, comments are interspersed
  1035  // between tokens based on their position. If syntax tree nodes are
  1036  // removed or moved, relevant comments in their vicinity must also be removed
  1037  // (from the [File.Comments] list) or moved accordingly (by updating their
  1038  // positions). A [CommentMap] may be used to facilitate some of these operations.
  1039  //
  1040  // Whether and how a comment is associated with a node depends on the
  1041  // interpretation of the syntax tree by the manipulating program: except for Doc
  1042  // and [Comment] comments directly associated with nodes, the remaining comments
  1043  // are "free-floating" (see also issues [#18593], [#20744]).
  1044  //
  1045  // [#18593]: https://go.dev/issue/18593
  1046  // [#20744]: https://go.dev/issue/20744
  1047  type File struct {
  1048  	Doc     *CommentGroup // associated documentation; or nil
  1049  	Package token.Pos     // position of "package" keyword
  1050  	Name    *Ident        // package name
  1051  	Decls   []Decl        // top-level declarations; or nil
  1052  
  1053  	FileStart, FileEnd token.Pos       // start and end of entire file
  1054  	Scope              *Scope          // package scope (this file only). Deprecated: see Object
  1055  	Imports            []*ImportSpec   // imports in this file
  1056  	Unresolved         []*Ident        // unresolved identifiers in this file. Deprecated: see Object
  1057  	Comments           []*CommentGroup // list of all comments in the source file
  1058  	GoVersion          string          // minimum Go version required by //go:build or // +build directives
  1059  }
  1060  
  1061  // Pos returns the position of the package declaration.
  1062  // (Use FileStart for the start of the entire file.)
  1063  func (f *File) Pos() token.Pos { return f.Package }
  1064  
  1065  // End returns the end of the last declaration in the file.
  1066  // (Use FileEnd for the end of the entire file.)
  1067  func (f *File) End() token.Pos {
  1068  	if n := len(f.Decls); n > 0 {
  1069  		return f.Decls[n-1].End()
  1070  	}
  1071  	return f.Name.End()
  1072  }
  1073  
  1074  // A Package node represents a set of source files
  1075  // collectively building a Go package.
  1076  //
  1077  // Deprecated: use the type checker [go/types] instead; see [Object].
  1078  type Package struct {
  1079  	Name    string             // package name
  1080  	Scope   *Scope             // package scope across all files
  1081  	Imports map[string]*Object // map of package id -> package object
  1082  	Files   map[string]*File   // Go source files by filename
  1083  }
  1084  
  1085  func (p *Package) Pos() token.Pos { return token.NoPos }
  1086  func (p *Package) End() token.Pos { return token.NoPos }
  1087  
  1088  // IsGenerated reports whether the file was generated by a program,
  1089  // not handwritten, by detecting the special comment described
  1090  // at https://go.dev/s/generatedcode.
  1091  //
  1092  // The syntax tree must have been parsed with the [parser.ParseComments] flag.
  1093  // Example:
  1094  //
  1095  //	f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly)
  1096  //	if err != nil { ... }
  1097  //	gen := ast.IsGenerated(f)
  1098  func IsGenerated(file *File) bool {
  1099  	_, ok := generator(file)
  1100  	return ok
  1101  }
  1102  
  1103  func generator(file *File) (string, bool) {
  1104  	for _, group := range file.Comments {
  1105  		for _, comment := range group.List {
  1106  			if comment.Pos() > file.Package {
  1107  				break // after package declaration
  1108  			}
  1109  			// opt: check Contains first to avoid unnecessary array allocation in Split.
  1110  			const prefix = "// Code generated "
  1111  			if strings.Contains(comment.Text, prefix) {
  1112  				for _, line := range strings.Split(comment.Text, "\n") {
  1113  					if rest, ok := strings.CutPrefix(line, prefix); ok {
  1114  						if gen, ok := strings.CutSuffix(rest, " DO NOT EDIT."); ok {
  1115  							return gen, true
  1116  						}
  1117  					}
  1118  				}
  1119  			}
  1120  		}
  1121  	}
  1122  	return "", false
  1123  }
  1124  
  1125  // Unparen returns the expression with any enclosing parentheses removed.
  1126  func Unparen(e Expr) Expr {
  1127  	for {
  1128  		paren, ok := e.(*ParenExpr)
  1129  		if !ok {
  1130  			return e
  1131  		}
  1132  		e = paren.X
  1133  	}
  1134  }
  1135  

View as plain text