The Go Programming Language

Source file src/pkg/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	//
     8	package ast
     9	
    10	import (
    11		"go/token"
    12		"unicode"
    13		"utf8"
    14	)
    15	
    16	// ----------------------------------------------------------------------------
    17	// Interfaces
    18	//
    19	// There are 3 main classes of nodes: Expressions and type nodes,
    20	// statement nodes, and declaration nodes. The node names usually
    21	// match the corresponding Go spec production names to which they
    22	// correspond. The node fields correspond to the individual parts
    23	// of the respective productions.
    24	//
    25	// All nodes contain position information marking the beginning of
    26	// the corresponding source text segment; it is accessible via the
    27	// Pos accessor method. Nodes may contain additional position info
    28	// for language constructs where comments may be found between parts
    29	// of the construct (typically any larger, parenthesized subpart).
    30	// That position information is needed to properly position comments
    31	// when printing the construct.
    32	
    33	// All node types implement the Node interface.
    34	type Node interface {
    35		Pos() token.Pos // position of first character belonging to the node
    36		End() token.Pos // position of first character immediately after the node
    37	}
    38	
    39	// All expression nodes implement the Expr interface.
    40	type Expr interface {
    41		Node
    42		exprNode()
    43	}
    44	
    45	// All statement nodes implement the Stmt interface.
    46	type Stmt interface {
    47		Node
    48		stmtNode()
    49	}
    50	
    51	// All declaration nodes implement the Decl interface.
    52	type Decl interface {
    53		Node
    54		declNode()
    55	}
    56	
    57	// ----------------------------------------------------------------------------
    58	// Comments
    59	
    60	// A Comment node represents a single //-style or /*-style comment.
    61	type Comment struct {
    62		Slash token.Pos // position of "/" starting the comment
    63		Text  string    // comment text (excluding '\n' for //-style comments)
    64	}
    65	
    66	func (c *Comment) Pos() token.Pos { return c.Slash }
    67	func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) }
    68	
    69	// A CommentGroup represents a sequence of comments
    70	// with no other tokens and no empty lines between.
    71	//
    72	type CommentGroup struct {
    73		List []*Comment // len(List) > 0
    74	}
    75	
    76	func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() }
    77	func (g *CommentGroup) End() token.Pos { return g.List[len(g.List)-1].End() }
    78	
    79	// ----------------------------------------------------------------------------
    80	// Expressions and types
    81	
    82	// A Field represents a Field declaration list in a struct type,
    83	// a method list in an interface type, or a parameter/result declaration
    84	// in a signature.
    85	//
    86	type Field struct {
    87		Doc     *CommentGroup // associated documentation; or nil
    88		Names   []*Ident      // field/method/parameter names; or nil if anonymous field
    89		Type    Expr          // field/method/parameter type
    90		Tag     *BasicLit     // field tag; or nil
    91		Comment *CommentGroup // line comments; or nil
    92	}
    93	
    94	func (f *Field) Pos() token.Pos {
    95		if len(f.Names) > 0 {
    96			return f.Names[0].Pos()
    97		}
    98		return f.Type.Pos()
    99	}
   100	
   101	func (f *Field) End() token.Pos {
   102		if f.Tag != nil {
   103			return f.Tag.End()
   104		}
   105		return f.Type.End()
   106	}
   107	
   108	// A FieldList represents a list of Fields, enclosed by parentheses or braces.
   109	type FieldList struct {
   110		Opening token.Pos // position of opening parenthesis/brace, if any
   111		List    []*Field  // field list; or nil
   112		Closing token.Pos // position of closing parenthesis/brace, if any
   113	}
   114	
   115	func (f *FieldList) Pos() token.Pos {
   116		if f.Opening.IsValid() {
   117			return f.Opening
   118		}
   119		// the list should not be empty in this case;
   120		// be conservative and guard against bad ASTs
   121		if len(f.List) > 0 {
   122			return f.List[0].Pos()
   123		}
   124		return token.NoPos
   125	}
   126	
   127	func (f *FieldList) End() token.Pos {
   128		if f.Closing.IsValid() {
   129			return f.Closing + 1
   130		}
   131		// the list should not be empty in this case;
   132		// be conservative and guard against bad ASTs
   133		if n := len(f.List); n > 0 {
   134			return f.List[n-1].End()
   135		}
   136		return token.NoPos
   137	}
   138	
   139	// NumFields returns the number of (named and anonymous fields) in a FieldList.
   140	func (f *FieldList) NumFields() int {
   141		n := 0
   142		if f != nil {
   143			for _, g := range f.List {
   144				m := len(g.Names)
   145				if m == 0 {
   146					m = 1 // anonymous field
   147				}
   148				n += m
   149			}
   150		}
   151		return n
   152	}
   153	
   154	// An expression is represented by a tree consisting of one
   155	// or more of the following concrete expression nodes.
   156	//
   157	type (
   158		// A BadExpr node is a placeholder for expressions containing
   159		// syntax errors for which no correct expression nodes can be
   160		// created.
   161		//
   162		BadExpr struct {
   163			From, To token.Pos // position range of bad expression
   164		}
   165	
   166		// An Ident node represents an identifier.
   167		Ident struct {
   168			NamePos token.Pos // identifier position
   169			Name    string    // identifier name
   170			Obj     *Object   // denoted object; or nil
   171		}
   172	
   173		// An Ellipsis node stands for the "..." type in a
   174		// parameter list or the "..." length in an array type.
   175		//
   176		Ellipsis struct {
   177			Ellipsis token.Pos // position of "..."
   178			Elt      Expr      // ellipsis element type (parameter lists only); or nil
   179		}
   180	
   181		// A BasicLit node represents a literal of basic type.
   182		BasicLit struct {
   183			ValuePos token.Pos   // literal position
   184			Kind     token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
   185			Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
   186		}
   187	
   188		// A FuncLit node represents a function literal.
   189		FuncLit struct {
   190			Type *FuncType  // function type
   191			Body *BlockStmt // function body
   192		}
   193	
   194		// A CompositeLit node represents a composite literal.
   195		CompositeLit struct {
   196			Type   Expr      // literal type; or nil
   197			Lbrace token.Pos // position of "{"
   198			Elts   []Expr    // list of composite elements; or nil
   199			Rbrace token.Pos // position of "}"
   200		}
   201	
   202		// A ParenExpr node represents a parenthesized expression.
   203		ParenExpr struct {
   204			Lparen token.Pos // position of "("
   205			X      Expr      // parenthesized expression
   206			Rparen token.Pos // position of ")"
   207		}
   208	
   209		// A SelectorExpr node represents an expression followed by a selector.
   210		SelectorExpr struct {
   211			X   Expr   // expression
   212			Sel *Ident // field selector
   213		}
   214	
   215		// An IndexExpr node represents an expression followed by an index.
   216		IndexExpr struct {
   217			X      Expr      // expression
   218			Lbrack token.Pos // position of "["
   219			Index  Expr      // index expression
   220			Rbrack token.Pos // position of "]"
   221		}
   222	
   223		// An SliceExpr node represents an expression followed by slice indices.
   224		SliceExpr struct {
   225			X      Expr      // expression
   226			Lbrack token.Pos // position of "["
   227			Low    Expr      // begin of slice range; or nil
   228			High   Expr      // end of slice range; or nil
   229			Rbrack token.Pos // position of "]"
   230		}
   231	
   232		// A TypeAssertExpr node represents an expression followed by a
   233		// type assertion.
   234		//
   235		TypeAssertExpr struct {
   236			X    Expr // expression
   237			Type Expr // asserted type; nil means type switch X.(type)
   238		}
   239	
   240		// A CallExpr node represents an expression followed by an argument list.
   241		CallExpr struct {
   242			Fun      Expr      // function expression
   243			Lparen   token.Pos // position of "("
   244			Args     []Expr    // function arguments; or nil
   245			Ellipsis token.Pos // position of "...", if any
   246			Rparen   token.Pos // position of ")"
   247		}
   248	
   249		// A StarExpr node represents an expression of the form "*" Expression.
   250		// Semantically it could be a unary "*" expression, or a pointer type.
   251		//
   252		StarExpr struct {
   253			Star token.Pos // position of "*"
   254			X    Expr      // operand
   255		}
   256	
   257		// A UnaryExpr node represents a unary expression.
   258		// Unary "*" expressions are represented via StarExpr nodes.
   259		//
   260		UnaryExpr struct {
   261			OpPos token.Pos   // position of Op
   262			Op    token.Token // operator
   263			X     Expr        // operand
   264		}
   265	
   266		// A BinaryExpr node represents a binary expression.
   267		BinaryExpr struct {
   268			X     Expr        // left operand
   269			OpPos token.Pos   // position of Op
   270			Op    token.Token // operator
   271			Y     Expr        // right operand
   272		}
   273	
   274		// A KeyValueExpr node represents (key : value) pairs
   275		// in composite literals.
   276		//
   277		KeyValueExpr struct {
   278			Key   Expr
   279			Colon token.Pos // position of ":"
   280			Value Expr
   281		}
   282	)
   283	
   284	// The direction of a channel type is indicated by one
   285	// of the following constants.
   286	//
   287	type ChanDir int
   288	
   289	const (
   290		SEND ChanDir = 1 << iota
   291		RECV
   292	)
   293	
   294	// A type is represented by a tree consisting of one
   295	// or more of the following type-specific expression
   296	// nodes.
   297	//
   298	type (
   299		// An ArrayType node represents an array or slice type.
   300		ArrayType struct {
   301			Lbrack token.Pos // position of "["
   302			Len    Expr      // Ellipsis node for [...]T array types, nil for slice types
   303			Elt    Expr      // element type
   304		}
   305	
   306		// A StructType node represents a struct type.
   307		StructType struct {
   308			Struct     token.Pos  // position of "struct" keyword
   309			Fields     *FieldList // list of field declarations
   310			Incomplete bool       // true if (source) fields are missing in the Fields list
   311		}
   312	
   313		// Pointer types are represented via StarExpr nodes.
   314	
   315		// A FuncType node represents a function type.
   316		FuncType struct {
   317			Func    token.Pos  // position of "func" keyword
   318			Params  *FieldList // (incoming) parameters; or nil
   319			Results *FieldList // (outgoing) results; or nil
   320		}
   321	
   322		// An InterfaceType node represents an interface type.
   323		InterfaceType struct {
   324			Interface  token.Pos  // position of "interface" keyword
   325			Methods    *FieldList // list of methods
   326			Incomplete bool       // true if (source) methods are missing in the Methods list
   327		}
   328	
   329		// A MapType node represents a map type.
   330		MapType struct {
   331			Map   token.Pos // position of "map" keyword
   332			Key   Expr
   333			Value Expr
   334		}
   335	
   336		// A ChanType node represents a channel type.
   337		ChanType struct {
   338			Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
   339			Dir   ChanDir   // channel direction
   340			Value Expr      // value type
   341		}
   342	)
   343	
   344	// Pos and End implementations for expression/type nodes.
   345	//
   346	func (x *BadExpr) Pos() token.Pos  { return x.From }
   347	func (x *Ident) Pos() token.Pos    { return x.NamePos }
   348	func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
   349	func (x *BasicLit) Pos() token.Pos { return x.ValuePos }
   350	func (x *FuncLit) Pos() token.Pos  { return x.Type.Pos() }
   351	func (x *CompositeLit) Pos() token.Pos {
   352		if x.Type != nil {
   353			return x.Type.Pos()
   354		}
   355		return x.Lbrace
   356	}
   357	func (x *ParenExpr) Pos() token.Pos      { return x.Lparen }
   358	func (x *SelectorExpr) Pos() token.Pos   { return x.X.Pos() }
   359	func (x *IndexExpr) Pos() token.Pos      { return x.X.Pos() }
   360	func (x *SliceExpr) Pos() token.Pos      { return x.X.Pos() }
   361	func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
   362	func (x *CallExpr) Pos() token.Pos       { return x.Fun.Pos() }
   363	func (x *StarExpr) Pos() token.Pos       { return x.Star }
   364	func (x *UnaryExpr) Pos() token.Pos      { return x.OpPos }
   365	func (x *BinaryExpr) Pos() token.Pos     { return x.X.Pos() }
   366	func (x *KeyValueExpr) Pos() token.Pos   { return x.Key.Pos() }
   367	func (x *ArrayType) Pos() token.Pos      { return x.Lbrack }
   368	func (x *StructType) Pos() token.Pos     { return x.Struct }
   369	func (x *FuncType) Pos() token.Pos       { return x.Func }
   370	func (x *InterfaceType) Pos() token.Pos  { return x.Interface }
   371	func (x *MapType) Pos() token.Pos        { return x.Map }
   372	func (x *ChanType) Pos() token.Pos       { return x.Begin }
   373	
   374	func (x *BadExpr) End() token.Pos { return x.To }
   375	func (x *Ident) End() token.Pos   { return token.Pos(int(x.NamePos) + len(x.Name)) }
   376	func (x *Ellipsis) End() token.Pos {
   377		if x.Elt != nil {
   378			return x.Elt.End()
   379		}
   380		return x.Ellipsis + 3 // len("...")
   381	}
   382	func (x *BasicLit) End() token.Pos     { return token.Pos(int(x.ValuePos) + len(x.Value)) }
   383	func (x *FuncLit) End() token.Pos      { return x.Body.End() }
   384	func (x *CompositeLit) End() token.Pos { return x.Rbrace + 1 }
   385	func (x *ParenExpr) End() token.Pos    { return x.Rparen + 1 }
   386	func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
   387	func (x *IndexExpr) End() token.Pos    { return x.Rbrack + 1 }
   388	func (x *SliceExpr) End() token.Pos    { return x.Rbrack + 1 }
   389	func (x *TypeAssertExpr) End() token.Pos {
   390		if x.Type != nil {
   391			return x.Type.End()
   392		}
   393		return x.X.End()
   394	}
   395	func (x *CallExpr) End() token.Pos     { return x.Rparen + 1 }
   396	func (x *StarExpr) End() token.Pos     { return x.X.End() }
   397	func (x *UnaryExpr) End() token.Pos    { return x.X.End() }
   398	func (x *BinaryExpr) End() token.Pos   { return x.Y.End() }
   399	func (x *KeyValueExpr) End() token.Pos { return x.Value.End() }
   400	func (x *ArrayType) End() token.Pos    { return x.Elt.End() }
   401	func (x *StructType) End() token.Pos   { return x.Fields.End() }
   402	func (x *FuncType) End() token.Pos {
   403		if x.Results != nil {
   404			return x.Results.End()
   405		}
   406		return x.Params.End()
   407	}
   408	func (x *InterfaceType) End() token.Pos { return x.Methods.End() }
   409	func (x *MapType) End() token.Pos       { return x.Value.End() }
   410	func (x *ChanType) End() token.Pos      { return x.Value.End() }
   411	
   412	// exprNode() ensures that only expression/type nodes can be
   413	// assigned to an ExprNode.
   414	//
   415	func (x *BadExpr) exprNode()        {}
   416	func (x *Ident) exprNode()          {}
   417	func (x *Ellipsis) exprNode()       {}
   418	func (x *BasicLit) exprNode()       {}
   419	func (x *FuncLit) exprNode()        {}
   420	func (x *CompositeLit) exprNode()   {}
   421	func (x *ParenExpr) exprNode()      {}
   422	func (x *SelectorExpr) exprNode()   {}
   423	func (x *IndexExpr) exprNode()      {}
   424	func (x *SliceExpr) exprNode()      {}
   425	func (x *TypeAssertExpr) exprNode() {}
   426	func (x *CallExpr) exprNode()       {}
   427	func (x *StarExpr) exprNode()       {}
   428	func (x *UnaryExpr) exprNode()      {}
   429	func (x *BinaryExpr) exprNode()     {}
   430	func (x *KeyValueExpr) exprNode()   {}
   431	
   432	func (x *ArrayType) exprNode()     {}
   433	func (x *StructType) exprNode()    {}
   434	func (x *FuncType) exprNode()      {}
   435	func (x *InterfaceType) exprNode() {}
   436	func (x *MapType) exprNode()       {}
   437	func (x *ChanType) exprNode()      {}
   438	
   439	// ----------------------------------------------------------------------------
   440	// Convenience functions for Idents
   441	
   442	var noPos token.Pos
   443	
   444	// NewIdent creates a new Ident without position.
   445	// Useful for ASTs generated by code other than the Go parser.
   446	//
   447	func NewIdent(name string) *Ident { return &Ident{noPos, name, nil} }
   448	
   449	// IsExported returns whether name is an exported Go symbol
   450	// (i.e., whether it begins with an uppercase letter).
   451	//
   452	func IsExported(name string) bool {
   453		ch, _ := utf8.DecodeRuneInString(name)
   454		return unicode.IsUpper(ch)
   455	}
   456	
   457	// IsExported returns whether id is an exported Go symbol
   458	// (i.e., whether it begins with an uppercase letter).
   459	//
   460	func (id *Ident) IsExported() bool { return IsExported(id.Name) }
   461	
   462	func (id *Ident) String() string {
   463		if id != nil {
   464			return id.Name
   465		}
   466		return "<nil>"
   467	}
   468	
   469	// ----------------------------------------------------------------------------
   470	// Statements
   471	
   472	// A statement is represented by a tree consisting of one
   473	// or more of the following concrete statement nodes.
   474	//
   475	type (
   476		// A BadStmt node is a placeholder for statements containing
   477		// syntax errors for which no correct statement nodes can be
   478		// created.
   479		//
   480		BadStmt struct {
   481			From, To token.Pos // position range of bad statement
   482		}
   483	
   484		// A DeclStmt node represents a declaration in a statement list.
   485		DeclStmt struct {
   486			Decl Decl
   487		}
   488	
   489		// An EmptyStmt node represents an empty statement.
   490		// The "position" of the empty statement is the position
   491		// of the immediately preceding semicolon.
   492		//
   493		EmptyStmt struct {
   494			Semicolon token.Pos // position of preceding ";"
   495		}
   496	
   497		// A LabeledStmt node represents a labeled statement.
   498		LabeledStmt struct {
   499			Label *Ident
   500			Colon token.Pos // position of ":"
   501			Stmt  Stmt
   502		}
   503	
   504		// An ExprStmt node represents a (stand-alone) expression
   505		// in a statement list.
   506		//
   507		ExprStmt struct {
   508			X Expr // expression
   509		}
   510	
   511		// A SendStmt node represents a send statement.
   512		SendStmt struct {
   513			Chan  Expr
   514			Arrow token.Pos // position of "<-"
   515			Value Expr
   516		}
   517	
   518		// An IncDecStmt node represents an increment or decrement statement.
   519		IncDecStmt struct {
   520			X      Expr
   521			TokPos token.Pos   // position of Tok
   522			Tok    token.Token // INC or DEC
   523		}
   524	
   525		// An AssignStmt node represents an assignment or
   526		// a short variable declaration.
   527		//
   528		AssignStmt struct {
   529			Lhs    []Expr
   530			TokPos token.Pos   // position of Tok
   531			Tok    token.Token // assignment token, DEFINE
   532			Rhs    []Expr
   533		}
   534	
   535		// A GoStmt node represents a go statement.
   536		GoStmt struct {
   537			Go   token.Pos // position of "go" keyword
   538			Call *CallExpr
   539		}
   540	
   541		// A DeferStmt node represents a defer statement.
   542		DeferStmt struct {
   543			Defer token.Pos // position of "defer" keyword
   544			Call  *CallExpr
   545		}
   546	
   547		// A ReturnStmt node represents a return statement.
   548		ReturnStmt struct {
   549			Return  token.Pos // position of "return" keyword
   550			Results []Expr    // result expressions; or nil
   551		}
   552	
   553		// A BranchStmt node represents a break, continue, goto,
   554		// or fallthrough statement.
   555		//
   556		BranchStmt struct {
   557			TokPos token.Pos   // position of Tok
   558			Tok    token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
   559			Label  *Ident      // label name; or nil
   560		}
   561	
   562		// A BlockStmt node represents a braced statement list.
   563		BlockStmt struct {
   564			Lbrace token.Pos // position of "{"
   565			List   []Stmt
   566			Rbrace token.Pos // position of "}"
   567		}
   568	
   569		// An IfStmt node represents an if statement.
   570		IfStmt struct {
   571			If   token.Pos // position of "if" keyword
   572			Init Stmt      // initialization statement; or nil
   573			Cond Expr      // condition
   574			Body *BlockStmt
   575			Else Stmt // else branch; or nil
   576		}
   577	
   578		// A CaseClause represents a case of an expression or type switch statement.
   579		CaseClause struct {
   580			Case  token.Pos // position of "case" or "default" keyword
   581			List  []Expr    // list of expressions or types; nil means default case
   582			Colon token.Pos // position of ":"
   583			Body  []Stmt    // statement list; or nil
   584		}
   585	
   586		// A SwitchStmt node represents an expression switch statement.
   587		SwitchStmt struct {
   588			Switch token.Pos  // position of "switch" keyword
   589			Init   Stmt       // initialization statement; or nil
   590			Tag    Expr       // tag expression; or nil
   591			Body   *BlockStmt // CaseClauses only
   592		}
   593	
   594		// An TypeSwitchStmt node represents a type switch statement.
   595		TypeSwitchStmt struct {
   596			Switch token.Pos  // position of "switch" keyword
   597			Init   Stmt       // initialization statement; or nil
   598			Assign Stmt       // x := y.(type) or y.(type)
   599			Body   *BlockStmt // CaseClauses only
   600		}
   601	
   602		// A CommClause node represents a case of a select statement.
   603		CommClause struct {
   604			Case  token.Pos // position of "case" or "default" keyword
   605			Comm  Stmt      // send or receive statement; nil means default case
   606			Colon token.Pos // position of ":"
   607			Body  []Stmt    // statement list; or nil
   608		}
   609	
   610		// An SelectStmt node represents a select statement.
   611		SelectStmt struct {
   612			Select token.Pos  // position of "select" keyword
   613			Body   *BlockStmt // CommClauses only
   614		}
   615	
   616		// A ForStmt represents a for statement.
   617		ForStmt struct {
   618			For  token.Pos // position of "for" keyword
   619			Init Stmt      // initialization statement; or nil
   620			Cond Expr      // condition; or nil
   621			Post Stmt      // post iteration statement; or nil
   622			Body *BlockStmt
   623		}
   624	
   625		// A RangeStmt represents a for statement with a range clause.
   626		RangeStmt struct {
   627			For        token.Pos   // position of "for" keyword
   628			Key, Value Expr        // Value may be nil
   629			TokPos     token.Pos   // position of Tok
   630			Tok        token.Token // ASSIGN, DEFINE
   631			X          Expr        // value to range over
   632			Body       *BlockStmt
   633		}
   634	)
   635	
   636	// Pos and End implementations for statement nodes.
   637	//
   638	func (s *BadStmt) Pos() token.Pos        { return s.From }
   639	func (s *DeclStmt) Pos() token.Pos       { return s.Decl.Pos() }
   640	func (s *EmptyStmt) Pos() token.Pos      { return s.Semicolon }
   641	func (s *LabeledStmt) Pos() token.Pos    { return s.Label.Pos() }
   642	func (s *ExprStmt) Pos() token.Pos       { return s.X.Pos() }
   643	func (s *SendStmt) Pos() token.Pos       { return s.Chan.Pos() }
   644	func (s *IncDecStmt) Pos() token.Pos     { return s.X.Pos() }
   645	func (s *AssignStmt) Pos() token.Pos     { return s.Lhs[0].Pos() }
   646	func (s *GoStmt) Pos() token.Pos         { return s.Go }
   647	func (s *DeferStmt) Pos() token.Pos      { return s.Defer }
   648	func (s *ReturnStmt) Pos() token.Pos     { return s.Return }
   649	func (s *BranchStmt) Pos() token.Pos     { return s.TokPos }
   650	func (s *BlockStmt) Pos() token.Pos      { return s.Lbrace }
   651	func (s *IfStmt) Pos() token.Pos         { return s.If }
   652	func (s *CaseClause) Pos() token.Pos     { return s.Case }
   653	func (s *SwitchStmt) Pos() token.Pos     { return s.Switch }
   654	func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch }
   655	func (s *CommClause) Pos() token.Pos     { return s.Case }
   656	func (s *SelectStmt) Pos() token.Pos     { return s.Select }
   657	func (s *ForStmt) Pos() token.Pos        { return s.For }
   658	func (s *RangeStmt) Pos() token.Pos      { return s.For }
   659	
   660	func (s *BadStmt) End() token.Pos  { return s.To }
   661	func (s *DeclStmt) End() token.Pos { return s.Decl.End() }
   662	func (s *EmptyStmt) End() token.Pos {
   663		return s.Semicolon + 1 /* len(";") */
   664	}
   665	func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() }
   666	func (s *ExprStmt) End() token.Pos    { return s.X.End() }
   667	func (s *SendStmt) End() token.Pos    { return s.Value.End() }
   668	func (s *IncDecStmt) End() token.Pos {
   669		return s.TokPos + 2 /* len("++") */
   670	}
   671	func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() }
   672	func (s *GoStmt) End() token.Pos     { return s.Call.End() }
   673	func (s *DeferStmt) End() token.Pos  { return s.Call.End() }
   674	func (s *ReturnStmt) End() token.Pos {
   675		if n := len(s.Results); n > 0 {
   676			return s.Results[n-1].End()
   677		}
   678		return s.Return + 6 // len("return")
   679	}
   680	func (s *BranchStmt) End() token.Pos {
   681		if s.Label != nil {
   682			return s.Label.End()
   683		}
   684		return token.Pos(int(s.TokPos) + len(s.Tok.String()))
   685	}
   686	func (s *BlockStmt) End() token.Pos { return s.Rbrace + 1 }
   687	func (s *IfStmt) End() token.Pos {
   688		if s.Else != nil {
   689			return s.Else.End()
   690		}
   691		return s.Body.End()
   692	}
   693	func (s *CaseClause) End() token.Pos {
   694		if n := len(s.Body); n > 0 {
   695			return s.Body[n-1].End()
   696		}
   697		return s.Colon + 1
   698	}
   699	func (s *SwitchStmt) End() token.Pos     { return s.Body.End() }
   700	func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() }
   701	func (s *CommClause) End() token.Pos {
   702		if n := len(s.Body); n > 0 {
   703			return s.Body[n-1].End()
   704		}
   705		return s.Colon + 1
   706	}
   707	func (s *SelectStmt) End() token.Pos { return s.Body.End() }
   708	func (s *ForStmt) End() token.Pos    { return s.Body.End() }
   709	func (s *RangeStmt) End() token.Pos  { return s.Body.End() }
   710	
   711	// stmtNode() ensures that only statement nodes can be
   712	// assigned to a StmtNode.
   713	//
   714	func (s *BadStmt) stmtNode()        {}
   715	func (s *DeclStmt) stmtNode()       {}
   716	func (s *EmptyStmt) stmtNode()      {}
   717	func (s *LabeledStmt) stmtNode()    {}
   718	func (s *ExprStmt) stmtNode()       {}
   719	func (s *SendStmt) stmtNode()       {}
   720	func (s *IncDecStmt) stmtNode()     {}
   721	func (s *AssignStmt) stmtNode()     {}
   722	func (s *GoStmt) stmtNode()         {}
   723	func (s *DeferStmt) stmtNode()      {}
   724	func (s *ReturnStmt) stmtNode()     {}
   725	func (s *BranchStmt) stmtNode()     {}
   726	func (s *BlockStmt) stmtNode()      {}
   727	func (s *IfStmt) stmtNode()         {}
   728	func (s *CaseClause) stmtNode()     {}
   729	func (s *SwitchStmt) stmtNode()     {}
   730	func (s *TypeSwitchStmt) stmtNode() {}
   731	func (s *CommClause) stmtNode()     {}
   732	func (s *SelectStmt) stmtNode()     {}
   733	func (s *ForStmt) stmtNode()        {}
   734	func (s *RangeStmt) stmtNode()      {}
   735	
   736	// ----------------------------------------------------------------------------
   737	// Declarations
   738	
   739	// A Spec node represents a single (non-parenthesized) import,
   740	// constant, type, or variable declaration.
   741	//
   742	type (
   743		// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
   744		Spec interface {
   745			Node
   746			specNode()
   747		}
   748	
   749		// An ImportSpec node represents a single package import.
   750		ImportSpec struct {
   751			Doc     *CommentGroup // associated documentation; or nil
   752			Name    *Ident        // local package name (including "."); or nil
   753			Path    *BasicLit     // import path
   754			Comment *CommentGroup // line comments; or nil
   755		}
   756	
   757		// A ValueSpec node represents a constant or variable declaration
   758		// (ConstSpec or VarSpec production).
   759		//
   760		ValueSpec struct {
   761			Doc     *CommentGroup // associated documentation; or nil
   762			Names   []*Ident      // value names (len(Names) > 0)
   763			Type    Expr          // value type; or nil
   764			Values  []Expr        // initial values; or nil
   765			Comment *CommentGroup // line comments; or nil
   766		}
   767	
   768		// A TypeSpec node represents a type declaration (TypeSpec production).
   769		TypeSpec struct {
   770			Doc     *CommentGroup // associated documentation; or nil
   771			Name    *Ident        // type name
   772			Type    Expr          // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
   773			Comment *CommentGroup // line comments; or nil
   774		}
   775	)
   776	
   777	// Pos and End implementations for spec nodes.
   778	//
   779	func (s *ImportSpec) Pos() token.Pos {
   780		if s.Name != nil {
   781			return s.Name.Pos()
   782		}
   783		return s.Path.Pos()
   784	}
   785	func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
   786	func (s *TypeSpec) Pos() token.Pos  { return s.Name.Pos() }
   787	
   788	func (s *ImportSpec) End() token.Pos { return s.Path.End() }
   789	func (s *ValueSpec) End() token.Pos {
   790		if n := len(s.Values); n > 0 {
   791			return s.Values[n-1].End()
   792		}
   793		if s.Type != nil {
   794			return s.Type.End()
   795		}
   796		return s.Names[len(s.Names)-1].End()
   797	}
   798	func (s *TypeSpec) End() token.Pos { return s.Type.End() }
   799	
   800	// specNode() ensures that only spec nodes can be
   801	// assigned to a Spec.
   802	//
   803	func (s *ImportSpec) specNode() {}
   804	func (s *ValueSpec) specNode()  {}
   805	func (s *TypeSpec) specNode()   {}
   806	
   807	// A declaration is represented by one of the following declaration nodes.
   808	//
   809	type (
   810		// A BadDecl node is a placeholder for declarations containing
   811		// syntax errors for which no correct declaration nodes can be
   812		// created.
   813		//
   814		BadDecl struct {
   815			From, To token.Pos // position range of bad declaration
   816		}
   817	
   818		// A GenDecl node (generic declaration node) represents an import,
   819		// constant, type or variable declaration. A valid Lparen position
   820		// (Lparen.Line > 0) indicates a parenthesized declaration.
   821		//
   822		// Relationship between Tok value and Specs element type:
   823		//
   824		//	token.IMPORT  *ImportSpec
   825		//	token.CONST   *ValueSpec
   826		//	token.TYPE    *TypeSpec
   827		//	token.VAR     *ValueSpec
   828		//
   829		GenDecl struct {
   830			Doc    *CommentGroup // associated documentation; or nil
   831			TokPos token.Pos     // position of Tok
   832			Tok    token.Token   // IMPORT, CONST, TYPE, VAR
   833			Lparen token.Pos     // position of '(', if any
   834			Specs  []Spec
   835			Rparen token.Pos // position of ')', if any
   836		}
   837	
   838		// A FuncDecl node represents a function declaration.
   839		FuncDecl struct {
   840			Doc  *CommentGroup // associated documentation; or nil
   841			Recv *FieldList    // receiver (methods); or nil (functions)
   842			Name *Ident        // function/method name
   843			Type *FuncType     // position of Func keyword, parameters and results
   844			Body *BlockStmt    // function body; or nil (forward declaration)
   845		}
   846	)
   847	
   848	// Pos and End implementations for declaration nodes.
   849	//
   850	func (d *BadDecl) Pos() token.Pos  { return d.From }
   851	func (d *GenDecl) Pos() token.Pos  { return d.TokPos }
   852	func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
   853	
   854	func (d *BadDecl) End() token.Pos { return d.To }
   855	func (d *GenDecl) End() token.Pos {
   856		if d.Rparen.IsValid() {
   857			return d.Rparen + 1
   858		}
   859		return d.Specs[0].End()
   860	}
   861	func (d *FuncDecl) End() token.Pos {
   862		if d.Body != nil {
   863			return d.Body.End()
   864		}
   865		return d.Type.End()
   866	}
   867	
   868	// declNode() ensures that only declaration nodes can be
   869	// assigned to a DeclNode.
   870	//
   871	func (d *BadDecl) declNode()  {}
   872	func (d *GenDecl) declNode()  {}
   873	func (d *FuncDecl) declNode() {}
   874	
   875	// ----------------------------------------------------------------------------
   876	// Files and packages
   877	
   878	// A File node represents a Go source file.
   879	//
   880	// The Comments list contains all comments in the source file in order of
   881	// appearance, including the comments that are pointed to from other nodes
   882	// via Doc and Comment fields.
   883	//
   884	type File struct {
   885		Doc        *CommentGroup   // associated documentation; or nil
   886		Package    token.Pos       // position of "package" keyword
   887		Name       *Ident          // package name
   888		Decls      []Decl          // top-level declarations; or nil
   889		Scope      *Scope          // package scope (this file only)
   890		Imports    []*ImportSpec   // imports in this file
   891		Unresolved []*Ident        // unresolved identifiers in this file
   892		Comments   []*CommentGroup // list of all comments in the source file
   893	}
   894	
   895	func (f *File) Pos() token.Pos { return f.Package }
   896	func (f *File) End() token.Pos {
   897		if n := len(f.Decls); n > 0 {
   898			return f.Decls[n-1].End()
   899		}
   900		return f.Name.End()
   901	}
   902	
   903	// A Package node represents a set of source files
   904	// collectively building a Go package.
   905	//
   906	type Package struct {
   907		Name    string             // package name
   908		Scope   *Scope             // package scope across all files
   909		Imports map[string]*Object // map of package id -> package object
   910		Files   map[string]*File   // Go source files by filename
   911	}
   912	
   913	func (p *Package) Pos() token.Pos { return token.NoPos }
   914	func (p *Package) End() token.Pos { return token.NoPos }

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.