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  	//
   309  	// For raw string literals (Kind == token.STRING && Value[0] == '`'),
   310  	// the Value field contains the string text without carriage returns (\r) that
   311  	// may have been present in the source. Because the end position is
   312  	// computed using len(Value), the position reported by [BasicLit.End] does not match the
   313  	// true source end position for raw string literals containing carriage returns.
   314  	BasicLit struct {
   315  		ValuePos token.Pos   // literal position
   316  		Kind     token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
   317  		Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
   318  	}
   319  
   320  	// A FuncLit node represents a function literal.
   321  	FuncLit struct {
   322  		Type *FuncType  // function type
   323  		Body *BlockStmt // function body
   324  	}
   325  
   326  	// A CompositeLit node represents a composite literal.
   327  	CompositeLit struct {
   328  		Type       Expr      // literal type; or nil
   329  		Lbrace     token.Pos // position of "{"
   330  		Elts       []Expr    // list of composite elements; or nil
   331  		Rbrace     token.Pos // position of "}"
   332  		Incomplete bool      // true if (source) expressions are missing in the Elts list
   333  	}
   334  
   335  	// A ParenExpr node represents a parenthesized expression.
   336  	ParenExpr struct {
   337  		Lparen token.Pos // position of "("
   338  		X      Expr      // parenthesized expression
   339  		Rparen token.Pos // position of ")"
   340  	}
   341  
   342  	// A SelectorExpr node represents an expression followed by a selector.
   343  	SelectorExpr struct {
   344  		X   Expr   // expression
   345  		Sel *Ident // field selector
   346  	}
   347  
   348  	// An IndexExpr node represents an expression followed by an index.
   349  	IndexExpr struct {
   350  		X      Expr      // expression
   351  		Lbrack token.Pos // position of "["
   352  		Index  Expr      // index expression
   353  		Rbrack token.Pos // position of "]"
   354  	}
   355  
   356  	// An IndexListExpr node represents an expression followed by multiple
   357  	// indices.
   358  	IndexListExpr struct {
   359  		X       Expr      // expression
   360  		Lbrack  token.Pos // position of "["
   361  		Indices []Expr    // index expressions
   362  		Rbrack  token.Pos // position of "]"
   363  	}
   364  
   365  	// A SliceExpr node represents an expression followed by slice indices.
   366  	SliceExpr struct {
   367  		X      Expr      // expression
   368  		Lbrack token.Pos // position of "["
   369  		Low    Expr      // begin of slice range; or nil
   370  		High   Expr      // end of slice range; or nil
   371  		Max    Expr      // maximum capacity of slice; or nil
   372  		Slice3 bool      // true if 3-index slice (2 colons present)
   373  		Rbrack token.Pos // position of "]"
   374  	}
   375  
   376  	// A TypeAssertExpr node represents an expression followed by a
   377  	// type assertion.
   378  	//
   379  	TypeAssertExpr struct {
   380  		X      Expr      // expression
   381  		Lparen token.Pos // position of "("
   382  		Type   Expr      // asserted type; nil means type switch X.(type)
   383  		Rparen token.Pos // position of ")"
   384  	}
   385  
   386  	// A CallExpr node represents an expression followed by an argument list.
   387  	CallExpr struct {
   388  		Fun      Expr      // function expression
   389  		Lparen   token.Pos // position of "("
   390  		Args     []Expr    // function arguments; or nil
   391  		Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
   392  		Rparen   token.Pos // position of ")"
   393  	}
   394  
   395  	// A StarExpr node represents an expression of the form "*" Expression.
   396  	// Semantically it could be a unary "*" expression, or a pointer type.
   397  	//
   398  	StarExpr struct {
   399  		Star token.Pos // position of "*"
   400  		X    Expr      // operand
   401  	}
   402  
   403  	// A UnaryExpr node represents a unary expression.
   404  	// Unary "*" expressions are represented via StarExpr nodes.
   405  	//
   406  	UnaryExpr struct {
   407  		OpPos token.Pos   // position of Op
   408  		Op    token.Token // operator
   409  		X     Expr        // operand
   410  	}
   411  
   412  	// A BinaryExpr node represents a binary expression.
   413  	BinaryExpr struct {
   414  		X     Expr        // left operand
   415  		OpPos token.Pos   // position of Op
   416  		Op    token.Token // operator
   417  		Y     Expr        // right operand
   418  	}
   419  
   420  	// A KeyValueExpr node represents (key : value) pairs
   421  	// in composite literals.
   422  	//
   423  	KeyValueExpr struct {
   424  		Key   Expr
   425  		Colon token.Pos // position of ":"
   426  		Value Expr
   427  	}
   428  )
   429  
   430  // The direction of a channel type is indicated by a bit
   431  // mask including one or both of the following constants.
   432  type ChanDir int
   433  
   434  const (
   435  	SEND ChanDir = 1 << iota
   436  	RECV
   437  )
   438  
   439  // A type is represented by a tree consisting of one
   440  // or more of the following type-specific expression
   441  // nodes.
   442  type (
   443  	// An ArrayType node represents an array or slice type.
   444  	ArrayType struct {
   445  		Lbrack token.Pos // position of "["
   446  		Len    Expr      // Ellipsis node for [...]T array types, nil for slice types
   447  		Elt    Expr      // element type
   448  	}
   449  
   450  	// A StructType node represents a struct type.
   451  	StructType struct {
   452  		Struct     token.Pos  // position of "struct" keyword
   453  		Fields     *FieldList // list of field declarations
   454  		Incomplete bool       // true if (source) fields are missing in the Fields list
   455  	}
   456  
   457  	// Pointer types are represented via StarExpr nodes.
   458  
   459  	// A FuncType node represents a function type.
   460  	FuncType struct {
   461  		Func       token.Pos  // position of "func" keyword (token.NoPos if there is no "func")
   462  		TypeParams *FieldList // type parameters; or nil
   463  		Params     *FieldList // (incoming) parameters; non-nil
   464  		Results    *FieldList // (outgoing) results; or nil
   465  	}
   466  
   467  	// An InterfaceType node represents an interface type.
   468  	InterfaceType struct {
   469  		Interface  token.Pos  // position of "interface" keyword
   470  		Methods    *FieldList // list of embedded interfaces, methods, or types
   471  		Incomplete bool       // true if (source) methods or types are missing in the Methods list
   472  	}
   473  
   474  	// A MapType node represents a map type.
   475  	MapType struct {
   476  		Map   token.Pos // position of "map" keyword
   477  		Key   Expr
   478  		Value Expr
   479  	}
   480  
   481  	// A ChanType node represents a channel type.
   482  	ChanType struct {
   483  		Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
   484  		Arrow token.Pos // position of "<-" (token.NoPos if there is no "<-")
   485  		Dir   ChanDir   // channel direction
   486  		Value Expr      // value type
   487  	}
   488  )
   489  
   490  // Pos and End implementations for expression/type nodes.
   491  
   492  func (x *BadExpr) Pos() token.Pos  { return x.From }
   493  func (x *Ident) Pos() token.Pos    { return x.NamePos }
   494  func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
   495  func (x *BasicLit) Pos() token.Pos { return x.ValuePos }
   496  func (x *FuncLit) Pos() token.Pos  { return x.Type.Pos() }
   497  func (x *CompositeLit) Pos() token.Pos {
   498  	if x.Type != nil {
   499  		return x.Type.Pos()
   500  	}
   501  	return x.Lbrace
   502  }
   503  func (x *ParenExpr) Pos() token.Pos      { return x.Lparen }
   504  func (x *SelectorExpr) Pos() token.Pos   { return x.X.Pos() }
   505  func (x *IndexExpr) Pos() token.Pos      { return x.X.Pos() }
   506  func (x *IndexListExpr) Pos() token.Pos  { return x.X.Pos() }
   507  func (x *SliceExpr) Pos() token.Pos      { return x.X.Pos() }
   508  func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
   509  func (x *CallExpr) Pos() token.Pos       { return x.Fun.Pos() }
   510  func (x *StarExpr) Pos() token.Pos       { return x.Star }
   511  func (x *UnaryExpr) Pos() token.Pos      { return x.OpPos }
   512  func (x *BinaryExpr) Pos() token.Pos     { return x.X.Pos() }
   513  func (x *KeyValueExpr) Pos() token.Pos   { return x.Key.Pos() }
   514  func (x *ArrayType) Pos() token.Pos      { return x.Lbrack }
   515  func (x *StructType) Pos() token.Pos     { return x.Struct }
   516  func (x *FuncType) Pos() token.Pos {
   517  	if x.Func.IsValid() || x.Params == nil { // see issue 3870
   518  		return x.Func
   519  	}
   520  	return x.Params.Pos() // interface method declarations have no "func" keyword
   521  }
   522  func (x *InterfaceType) Pos() token.Pos { return x.Interface }
   523  func (x *MapType) Pos() token.Pos       { return x.Map }
   524  func (x *ChanType) Pos() token.Pos      { return x.Begin }
   525  
   526  func (x *BadExpr) End() token.Pos { return x.To }
   527  func (x *Ident) End() token.Pos   { return token.Pos(int(x.NamePos) + len(x.Name)) }
   528  func (x *Ellipsis) End() token.Pos {
   529  	if x.Elt != nil {
   530  		return x.Elt.End()
   531  	}
   532  	return x.Ellipsis + 3 // len("...")
   533  }
   534  func (x *BasicLit) End() token.Pos       { return token.Pos(int(x.ValuePos) + len(x.Value)) }
   535  func (x *FuncLit) End() token.Pos        { return x.Body.End() }
   536  func (x *CompositeLit) End() token.Pos   { return x.Rbrace + 1 }
   537  func (x *ParenExpr) End() token.Pos      { return x.Rparen + 1 }
   538  func (x *SelectorExpr) End() token.Pos   { return x.Sel.End() }
   539  func (x *IndexExpr) End() token.Pos      { return x.Rbrack + 1 }
   540  func (x *IndexListExpr) End() token.Pos  { return x.Rbrack + 1 }
   541  func (x *SliceExpr) End() token.Pos      { return x.Rbrack + 1 }
   542  func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 }
   543  func (x *CallExpr) End() token.Pos       { return x.Rparen + 1 }
   544  func (x *StarExpr) End() token.Pos       { return x.X.End() }
   545  func (x *UnaryExpr) End() token.Pos      { return x.X.End() }
   546  func (x *BinaryExpr) End() token.Pos     { return x.Y.End() }
   547  func (x *KeyValueExpr) End() token.Pos   { return x.Value.End() }
   548  func (x *ArrayType) End() token.Pos      { return x.Elt.End() }
   549  func (x *StructType) End() token.Pos     { return x.Fields.End() }
   550  func (x *FuncType) End() token.Pos {
   551  	if x.Results != nil {
   552  		return x.Results.End()
   553  	}
   554  	return x.Params.End()
   555  }
   556  func (x *InterfaceType) End() token.Pos { return x.Methods.End() }
   557  func (x *MapType) End() token.Pos       { return x.Value.End() }
   558  func (x *ChanType) End() token.Pos      { return x.Value.End() }
   559  
   560  // exprNode() ensures that only expression/type nodes can be
   561  // assigned to an Expr.
   562  func (*BadExpr) exprNode()        {}
   563  func (*Ident) exprNode()          {}
   564  func (*Ellipsis) exprNode()       {}
   565  func (*BasicLit) exprNode()       {}
   566  func (*FuncLit) exprNode()        {}
   567  func (*CompositeLit) exprNode()   {}
   568  func (*ParenExpr) exprNode()      {}
   569  func (*SelectorExpr) exprNode()   {}
   570  func (*IndexExpr) exprNode()      {}
   571  func (*IndexListExpr) exprNode()  {}
   572  func (*SliceExpr) exprNode()      {}
   573  func (*TypeAssertExpr) exprNode() {}
   574  func (*CallExpr) exprNode()       {}
   575  func (*StarExpr) exprNode()       {}
   576  func (*UnaryExpr) exprNode()      {}
   577  func (*BinaryExpr) exprNode()     {}
   578  func (*KeyValueExpr) exprNode()   {}
   579  
   580  func (*ArrayType) exprNode()     {}
   581  func (*StructType) exprNode()    {}
   582  func (*FuncType) exprNode()      {}
   583  func (*InterfaceType) exprNode() {}
   584  func (*MapType) exprNode()       {}
   585  func (*ChanType) exprNode()      {}
   586  
   587  // ----------------------------------------------------------------------------
   588  // Convenience functions for Idents
   589  
   590  // NewIdent creates a new [Ident] without position.
   591  // Useful for ASTs generated by code other than the Go parser.
   592  func NewIdent(name string) *Ident { return &Ident{token.NoPos, name, nil} }
   593  
   594  // IsExported reports whether name starts with an upper-case letter.
   595  func IsExported(name string) bool { return token.IsExported(name) }
   596  
   597  // IsExported reports whether id starts with an upper-case letter.
   598  func (id *Ident) IsExported() bool { return token.IsExported(id.Name) }
   599  
   600  func (id *Ident) String() string {
   601  	if id != nil {
   602  		return id.Name
   603  	}
   604  	return "<nil>"
   605  }
   606  
   607  // ----------------------------------------------------------------------------
   608  // Statements
   609  
   610  // A statement is represented by a tree consisting of one
   611  // or more of the following concrete statement nodes.
   612  type (
   613  	// A BadStmt node is a placeholder for statements containing
   614  	// syntax errors for which no correct statement nodes can be
   615  	// created.
   616  	//
   617  	BadStmt struct {
   618  		From, To token.Pos // position range of bad statement
   619  	}
   620  
   621  	// A DeclStmt node represents a declaration in a statement list.
   622  	DeclStmt struct {
   623  		Decl Decl // *GenDecl with CONST, TYPE, or VAR token
   624  	}
   625  
   626  	// An EmptyStmt node represents an empty statement.
   627  	// The "position" of the empty statement is the position
   628  	// of the immediately following (explicit or implicit) semicolon.
   629  	//
   630  	EmptyStmt struct {
   631  		Semicolon token.Pos // position of following ";"
   632  		Implicit  bool      // if set, ";" was omitted in the source
   633  	}
   634  
   635  	// A LabeledStmt node represents a labeled statement.
   636  	LabeledStmt struct {
   637  		Label *Ident
   638  		Colon token.Pos // position of ":"
   639  		Stmt  Stmt
   640  	}
   641  
   642  	// An ExprStmt node represents a (stand-alone) expression
   643  	// in a statement list.
   644  	//
   645  	ExprStmt struct {
   646  		X Expr // expression
   647  	}
   648  
   649  	// A SendStmt node represents a send statement.
   650  	SendStmt struct {
   651  		Chan  Expr
   652  		Arrow token.Pos // position of "<-"
   653  		Value Expr
   654  	}
   655  
   656  	// An IncDecStmt node represents an increment or decrement statement.
   657  	IncDecStmt struct {
   658  		X      Expr
   659  		TokPos token.Pos   // position of Tok
   660  		Tok    token.Token // INC or DEC
   661  	}
   662  
   663  	// An AssignStmt node represents an assignment or
   664  	// a short variable declaration.
   665  	//
   666  	AssignStmt struct {
   667  		Lhs    []Expr
   668  		TokPos token.Pos   // position of Tok
   669  		Tok    token.Token // assignment token, DEFINE
   670  		Rhs    []Expr
   671  	}
   672  
   673  	// A GoStmt node represents a go statement.
   674  	GoStmt struct {
   675  		Go   token.Pos // position of "go" keyword
   676  		Call *CallExpr
   677  	}
   678  
   679  	// A DeferStmt node represents a defer statement.
   680  	DeferStmt struct {
   681  		Defer token.Pos // position of "defer" keyword
   682  		Call  *CallExpr
   683  	}
   684  
   685  	// A ReturnStmt node represents a return statement.
   686  	ReturnStmt struct {
   687  		Return  token.Pos // position of "return" keyword
   688  		Results []Expr    // result expressions; or nil
   689  	}
   690  
   691  	// A BranchStmt node represents a break, continue, goto,
   692  	// or fallthrough statement.
   693  	//
   694  	BranchStmt struct {
   695  		TokPos token.Pos   // position of Tok
   696  		Tok    token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
   697  		Label  *Ident      // label name; or nil
   698  	}
   699  
   700  	// A BlockStmt node represents a braced statement list.
   701  	BlockStmt struct {
   702  		Lbrace token.Pos // position of "{"
   703  		List   []Stmt
   704  		Rbrace token.Pos // position of "}", if any (may be absent due to syntax error)
   705  	}
   706  
   707  	// An IfStmt node represents an if statement.
   708  	IfStmt struct {
   709  		If   token.Pos // position of "if" keyword
   710  		Init Stmt      // initialization statement; or nil
   711  		Cond Expr      // condition
   712  		Body *BlockStmt
   713  		Else Stmt // else branch; or nil
   714  	}
   715  
   716  	// A CaseClause represents a case of an expression or type switch statement.
   717  	CaseClause struct {
   718  		Case  token.Pos // position of "case" or "default" keyword
   719  		List  []Expr    // list of expressions or types; nil means default case
   720  		Colon token.Pos // position of ":"
   721  		Body  []Stmt    // statement list; or nil
   722  	}
   723  
   724  	// A SwitchStmt node represents an expression switch statement.
   725  	SwitchStmt struct {
   726  		Switch token.Pos  // position of "switch" keyword
   727  		Init   Stmt       // initialization statement; or nil
   728  		Tag    Expr       // tag expression; or nil
   729  		Body   *BlockStmt // CaseClauses only
   730  	}
   731  
   732  	// A TypeSwitchStmt node represents a type switch statement.
   733  	TypeSwitchStmt struct {
   734  		Switch token.Pos  // position of "switch" keyword
   735  		Init   Stmt       // initialization statement; or nil
   736  		Assign Stmt       // x := y.(type) or y.(type)
   737  		Body   *BlockStmt // CaseClauses only
   738  	}
   739  
   740  	// A CommClause node represents a case of a select statement.
   741  	CommClause struct {
   742  		Case  token.Pos // position of "case" or "default" keyword
   743  		Comm  Stmt      // send or receive statement; nil means default case
   744  		Colon token.Pos // position of ":"
   745  		Body  []Stmt    // statement list; or nil
   746  	}
   747  
   748  	// A SelectStmt node represents a select statement.
   749  	SelectStmt struct {
   750  		Select token.Pos  // position of "select" keyword
   751  		Body   *BlockStmt // CommClauses only
   752  	}
   753  
   754  	// A ForStmt represents a for statement.
   755  	ForStmt struct {
   756  		For  token.Pos // position of "for" keyword
   757  		Init Stmt      // initialization statement; or nil
   758  		Cond Expr      // condition; or nil
   759  		Post Stmt      // post iteration statement; or nil
   760  		Body *BlockStmt
   761  	}
   762  
   763  	// A RangeStmt represents a for statement with a range clause.
   764  	RangeStmt struct {
   765  		For        token.Pos   // position of "for" keyword
   766  		Key, Value Expr        // Key, Value may be nil
   767  		TokPos     token.Pos   // position of Tok; invalid if Key == nil
   768  		Tok        token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE
   769  		Range      token.Pos   // position of "range" keyword
   770  		X          Expr        // value to range over
   771  		Body       *BlockStmt
   772  	}
   773  )
   774  
   775  // Pos and End implementations for statement nodes.
   776  
   777  func (s *BadStmt) Pos() token.Pos        { return s.From }
   778  func (s *DeclStmt) Pos() token.Pos       { return s.Decl.Pos() }
   779  func (s *EmptyStmt) Pos() token.Pos      { return s.Semicolon }
   780  func (s *LabeledStmt) Pos() token.Pos    { return s.Label.Pos() }
   781  func (s *ExprStmt) Pos() token.Pos       { return s.X.Pos() }
   782  func (s *SendStmt) Pos() token.Pos       { return s.Chan.Pos() }
   783  func (s *IncDecStmt) Pos() token.Pos     { return s.X.Pos() }
   784  func (s *AssignStmt) Pos() token.Pos     { return s.Lhs[0].Pos() }
   785  func (s *GoStmt) Pos() token.Pos         { return s.Go }
   786  func (s *DeferStmt) Pos() token.Pos      { return s.Defer }
   787  func (s *ReturnStmt) Pos() token.Pos     { return s.Return }
   788  func (s *BranchStmt) Pos() token.Pos     { return s.TokPos }
   789  func (s *BlockStmt) Pos() token.Pos      { return s.Lbrace }
   790  func (s *IfStmt) Pos() token.Pos         { return s.If }
   791  func (s *CaseClause) Pos() token.Pos     { return s.Case }
   792  func (s *SwitchStmt) Pos() token.Pos     { return s.Switch }
   793  func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch }
   794  func (s *CommClause) Pos() token.Pos     { return s.Case }
   795  func (s *SelectStmt) Pos() token.Pos     { return s.Select }
   796  func (s *ForStmt) Pos() token.Pos        { return s.For }
   797  func (s *RangeStmt) Pos() token.Pos      { return s.For }
   798  
   799  func (s *BadStmt) End() token.Pos  { return s.To }
   800  func (s *DeclStmt) End() token.Pos { return s.Decl.End() }
   801  func (s *EmptyStmt) End() token.Pos {
   802  	if s.Implicit {
   803  		return s.Semicolon
   804  	}
   805  	return s.Semicolon + 1 /* len(";") */
   806  }
   807  func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() }
   808  func (s *ExprStmt) End() token.Pos    { return s.X.End() }
   809  func (s *SendStmt) End() token.Pos    { return s.Value.End() }
   810  func (s *IncDecStmt) End() token.Pos {
   811  	return s.TokPos + 2 /* len("++") */
   812  }
   813  func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() }
   814  func (s *GoStmt) End() token.Pos     { return s.Call.End() }
   815  func (s *DeferStmt) End() token.Pos  { return s.Call.End() }
   816  func (s *ReturnStmt) End() token.Pos {
   817  	if n := len(s.Results); n > 0 {
   818  		return s.Results[n-1].End()
   819  	}
   820  	return s.Return + 6 // len("return")
   821  }
   822  func (s *BranchStmt) End() token.Pos {
   823  	if s.Label != nil {
   824  		return s.Label.End()
   825  	}
   826  	return token.Pos(int(s.TokPos) + len(s.Tok.String()))
   827  }
   828  func (s *BlockStmt) End() token.Pos {
   829  	if s.Rbrace.IsValid() {
   830  		return s.Rbrace + 1
   831  	}
   832  	if n := len(s.List); n > 0 {
   833  		return s.List[n-1].End()
   834  	}
   835  	return s.Lbrace + 1
   836  }
   837  func (s *IfStmt) End() token.Pos {
   838  	if s.Else != nil {
   839  		return s.Else.End()
   840  	}
   841  	return s.Body.End()
   842  }
   843  func (s *CaseClause) End() token.Pos {
   844  	if n := len(s.Body); n > 0 {
   845  		return s.Body[n-1].End()
   846  	}
   847  	return s.Colon + 1
   848  }
   849  func (s *SwitchStmt) End() token.Pos     { return s.Body.End() }
   850  func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() }
   851  func (s *CommClause) End() token.Pos {
   852  	if n := len(s.Body); n > 0 {
   853  		return s.Body[n-1].End()
   854  	}
   855  	return s.Colon + 1
   856  }
   857  func (s *SelectStmt) End() token.Pos { return s.Body.End() }
   858  func (s *ForStmt) End() token.Pos    { return s.Body.End() }
   859  func (s *RangeStmt) End() token.Pos  { return s.Body.End() }
   860  
   861  // stmtNode() ensures that only statement nodes can be
   862  // assigned to a Stmt.
   863  func (*BadStmt) stmtNode()        {}
   864  func (*DeclStmt) stmtNode()       {}
   865  func (*EmptyStmt) stmtNode()      {}
   866  func (*LabeledStmt) stmtNode()    {}
   867  func (*ExprStmt) stmtNode()       {}
   868  func (*SendStmt) stmtNode()       {}
   869  func (*IncDecStmt) stmtNode()     {}
   870  func (*AssignStmt) stmtNode()     {}
   871  func (*GoStmt) stmtNode()         {}
   872  func (*DeferStmt) stmtNode()      {}
   873  func (*ReturnStmt) stmtNode()     {}
   874  func (*BranchStmt) stmtNode()     {}
   875  func (*BlockStmt) stmtNode()      {}
   876  func (*IfStmt) stmtNode()         {}
   877  func (*CaseClause) stmtNode()     {}
   878  func (*SwitchStmt) stmtNode()     {}
   879  func (*TypeSwitchStmt) stmtNode() {}
   880  func (*CommClause) stmtNode()     {}
   881  func (*SelectStmt) stmtNode()     {}
   882  func (*ForStmt) stmtNode()        {}
   883  func (*RangeStmt) stmtNode()      {}
   884  
   885  // ----------------------------------------------------------------------------
   886  // Declarations
   887  
   888  // A Spec node represents a single (non-parenthesized) import,
   889  // constant, type, or variable declaration.
   890  type (
   891  	// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
   892  	Spec interface {
   893  		Node
   894  		specNode()
   895  	}
   896  
   897  	// An ImportSpec node represents a single package import.
   898  	ImportSpec struct {
   899  		Doc     *CommentGroup // associated documentation; or nil
   900  		Name    *Ident        // local package name (including "."); or nil
   901  		Path    *BasicLit     // import path
   902  		Comment *CommentGroup // line comments; or nil
   903  		EndPos  token.Pos     // end of spec (overrides Path.Pos if nonzero)
   904  	}
   905  
   906  	// A ValueSpec node represents a constant or variable declaration
   907  	// (ConstSpec or VarSpec production).
   908  	//
   909  	ValueSpec struct {
   910  		Doc     *CommentGroup // associated documentation; or nil
   911  		Names   []*Ident      // value names (len(Names) > 0)
   912  		Type    Expr          // value type; or nil
   913  		Values  []Expr        // initial values; or nil
   914  		Comment *CommentGroup // line comments; or nil
   915  	}
   916  
   917  	// A TypeSpec node represents a type declaration (TypeSpec production).
   918  	TypeSpec struct {
   919  		Doc        *CommentGroup // associated documentation; or nil
   920  		Name       *Ident        // type name
   921  		TypeParams *FieldList    // type parameters; or nil
   922  		Assign     token.Pos     // position of '=', if any
   923  		Type       Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
   924  		Comment    *CommentGroup // line comments; or nil
   925  	}
   926  )
   927  
   928  // Pos and End implementations for spec nodes.
   929  
   930  func (s *ImportSpec) Pos() token.Pos {
   931  	if s.Name != nil {
   932  		return s.Name.Pos()
   933  	}
   934  	return s.Path.Pos()
   935  }
   936  func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
   937  func (s *TypeSpec) Pos() token.Pos  { return s.Name.Pos() }
   938  
   939  func (s *ImportSpec) End() token.Pos {
   940  	if s.EndPos != 0 {
   941  		return s.EndPos
   942  	}
   943  	return s.Path.End()
   944  }
   945  
   946  func (s *ValueSpec) End() token.Pos {
   947  	if n := len(s.Values); n > 0 {
   948  		return s.Values[n-1].End()
   949  	}
   950  	if s.Type != nil {
   951  		return s.Type.End()
   952  	}
   953  	return s.Names[len(s.Names)-1].End()
   954  }
   955  func (s *TypeSpec) End() token.Pos { return s.Type.End() }
   956  
   957  // specNode() ensures that only spec nodes can be
   958  // assigned to a Spec.
   959  func (*ImportSpec) specNode() {}
   960  func (*ValueSpec) specNode()  {}
   961  func (*TypeSpec) specNode()   {}
   962  
   963  // A declaration is represented by one of the following declaration nodes.
   964  type (
   965  	// A BadDecl node is a placeholder for a declaration containing
   966  	// syntax errors for which a correct declaration node cannot be
   967  	// created.
   968  	//
   969  	BadDecl struct {
   970  		From, To token.Pos // position range of bad declaration
   971  	}
   972  
   973  	// A GenDecl node (generic declaration node) represents an import,
   974  	// constant, type or variable declaration. A valid Lparen position
   975  	// (Lparen.IsValid()) indicates a parenthesized declaration.
   976  	//
   977  	// Relationship between Tok value and Specs element type:
   978  	//
   979  	//	token.IMPORT  *ImportSpec
   980  	//	token.CONST   *ValueSpec
   981  	//	token.TYPE    *TypeSpec
   982  	//	token.VAR     *ValueSpec
   983  	//
   984  	GenDecl struct {
   985  		Doc    *CommentGroup // associated documentation; or nil
   986  		TokPos token.Pos     // position of Tok
   987  		Tok    token.Token   // IMPORT, CONST, TYPE, or VAR
   988  		Lparen token.Pos     // position of '(', if any
   989  		Specs  []Spec
   990  		Rparen token.Pos // position of ')', if any
   991  	}
   992  
   993  	// A FuncDecl node represents a function declaration.
   994  	FuncDecl struct {
   995  		Doc  *CommentGroup // associated documentation; or nil
   996  		Recv *FieldList    // receiver (methods); or nil (functions)
   997  		Name *Ident        // function/method name
   998  		Type *FuncType     // function signature: type and value parameters, results, and position of "func" keyword
   999  		Body *BlockStmt    // function body; or nil for external (non-Go) function
  1000  	}
  1001  )
  1002  
  1003  // Pos and End implementations for declaration nodes.
  1004  
  1005  func (d *BadDecl) Pos() token.Pos  { return d.From }
  1006  func (d *GenDecl) Pos() token.Pos  { return d.TokPos }
  1007  func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
  1008  
  1009  func (d *BadDecl) End() token.Pos { return d.To }
  1010  func (d *GenDecl) End() token.Pos {
  1011  	if d.Rparen.IsValid() {
  1012  		return d.Rparen + 1
  1013  	}
  1014  	return d.Specs[0].End()
  1015  }
  1016  func (d *FuncDecl) End() token.Pos {
  1017  	if d.Body != nil {
  1018  		return d.Body.End()
  1019  	}
  1020  	return d.Type.End()
  1021  }
  1022  
  1023  // declNode() ensures that only declaration nodes can be
  1024  // assigned to a Decl.
  1025  func (*BadDecl) declNode()  {}
  1026  func (*GenDecl) declNode()  {}
  1027  func (*FuncDecl) declNode() {}
  1028  
  1029  // ----------------------------------------------------------------------------
  1030  // Files and packages
  1031  
  1032  // A File node represents a Go source file.
  1033  //
  1034  // The Comments list contains all comments in the source file in order of
  1035  // appearance, including the comments that are pointed to from other nodes
  1036  // via Doc and Comment fields.
  1037  //
  1038  // For correct printing of source code containing comments (using packages
  1039  // go/format and go/printer), special care must be taken to update comments
  1040  // when a File's syntax tree is modified: For printing, comments are interspersed
  1041  // between tokens based on their position. If syntax tree nodes are
  1042  // removed or moved, relevant comments in their vicinity must also be removed
  1043  // (from the [File.Comments] list) or moved accordingly (by updating their
  1044  // positions). A [CommentMap] may be used to facilitate some of these operations.
  1045  //
  1046  // Whether and how a comment is associated with a node depends on the
  1047  // interpretation of the syntax tree by the manipulating program: except for Doc
  1048  // and [Comment] comments directly associated with nodes, the remaining comments
  1049  // are "free-floating" (see also issues [#18593], [#20744]).
  1050  //
  1051  // [#18593]: https://go.dev/issue/18593
  1052  // [#20744]: https://go.dev/issue/20744
  1053  type File struct {
  1054  	Doc     *CommentGroup // associated documentation; or nil
  1055  	Package token.Pos     // position of "package" keyword
  1056  	Name    *Ident        // package name
  1057  	Decls   []Decl        // top-level declarations; or nil
  1058  
  1059  	FileStart, FileEnd token.Pos       // start and end of entire file
  1060  	Scope              *Scope          // package scope (this file only). Deprecated: see Object
  1061  	Imports            []*ImportSpec   // imports in this file
  1062  	Unresolved         []*Ident        // unresolved identifiers in this file. Deprecated: see Object
  1063  	Comments           []*CommentGroup // list of all comments in the source file
  1064  	GoVersion          string          // minimum Go version required by //go:build or // +build directives
  1065  }
  1066  
  1067  // Pos returns the position of the package declaration.
  1068  // It may be invalid, for example in an empty file.
  1069  //
  1070  // (Use FileStart for the start of the entire file. It is always valid.)
  1071  func (f *File) Pos() token.Pos { return f.Package }
  1072  
  1073  // End returns the end of the last declaration in the file.
  1074  // It may be invalid, for example in an empty file.
  1075  //
  1076  // (Use FileEnd for the end of the entire file. It is always valid.)
  1077  func (f *File) End() token.Pos {
  1078  	if n := len(f.Decls); n > 0 {
  1079  		return f.Decls[n-1].End()
  1080  	}
  1081  	return f.Name.End()
  1082  }
  1083  
  1084  // A Package node represents a set of source files
  1085  // collectively building a Go package.
  1086  //
  1087  // Deprecated: use the type checker [go/types] instead; see [Object].
  1088  type Package struct {
  1089  	Name    string             // package name
  1090  	Scope   *Scope             // package scope across all files
  1091  	Imports map[string]*Object // map of package id -> package object
  1092  	Files   map[string]*File   // Go source files by filename
  1093  }
  1094  
  1095  func (p *Package) Pos() token.Pos { return token.NoPos }
  1096  func (p *Package) End() token.Pos { return token.NoPos }
  1097  
  1098  // IsGenerated reports whether the file was generated by a program,
  1099  // not handwritten, by detecting the special comment described
  1100  // at https://go.dev/s/generatedcode.
  1101  //
  1102  // The syntax tree must have been parsed with the [parser.ParseComments] flag.
  1103  // Example:
  1104  //
  1105  //	f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly)
  1106  //	if err != nil { ... }
  1107  //	gen := ast.IsGenerated(f)
  1108  func IsGenerated(file *File) bool {
  1109  	_, ok := generator(file)
  1110  	return ok
  1111  }
  1112  
  1113  func generator(file *File) (string, bool) {
  1114  	for _, group := range file.Comments {
  1115  		for _, comment := range group.List {
  1116  			if comment.Pos() > file.Package {
  1117  				break // after package declaration
  1118  			}
  1119  			// opt: check Contains first to avoid unnecessary array allocation in Split.
  1120  			const prefix = "// Code generated "
  1121  			if strings.Contains(comment.Text, prefix) {
  1122  				for _, line := range strings.Split(comment.Text, "\n") {
  1123  					if rest, ok := strings.CutPrefix(line, prefix); ok {
  1124  						if gen, ok := strings.CutSuffix(rest, " DO NOT EDIT."); ok {
  1125  							return gen, true
  1126  						}
  1127  					}
  1128  				}
  1129  			}
  1130  		}
  1131  	}
  1132  	return "", false
  1133  }
  1134  
  1135  // Unparen returns the expression with any enclosing parentheses removed.
  1136  func Unparen(e Expr) Expr {
  1137  	for {
  1138  		paren, ok := e.(*ParenExpr)
  1139  		if !ok {
  1140  			return e
  1141  		}
  1142  		e = paren.X
  1143  	}
  1144  }
  1145  

View as plain text