1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package ast declares the types used to represent syntax trees for Go
16 // ----------------------------------------------------------------------------
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.
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.
33 // All node types implement the 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
39 // All expression nodes implement the Expr interface.
45 // All statement nodes implement the Stmt interface.
51 // All declaration nodes implement the Decl interface.
57 // ----------------------------------------------------------------------------
60 // A Comment node represents a single //-style or /*-style comment.
62 Slash token.Pos // position of "/" starting the comment
63 Text string // comment text (excluding '\n' for //-style comments)
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)) }
69 // A CommentGroup represents a sequence of comments
70 // with no other tokens and no empty lines between.
72 type CommentGroup struct {
73 List []*Comment // len(List) > 0
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() }
79 // ----------------------------------------------------------------------------
80 // Expressions and types
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
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
94 func (f *Field) Pos() token.Pos {
96 return f.Names[0].Pos()
101 func (f *Field) End() token.Pos {
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
115 func (f *FieldList) Pos() token.Pos {
116 if f.Opening.IsValid() {
119 // the list should not be empty in this case;
120 // be conservative and guard against bad ASTs
122 return f.List[0].Pos()
127 func (f *FieldList) End() token.Pos {
128 if f.Closing.IsValid() {
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()
139 // NumFields returns the number of (named and anonymous fields) in a FieldList.
140 func (f *FieldList) NumFields() int {
143 for _, g := range f.List {
146 m = 1 // anonymous field
154 // An expression is represented by a tree consisting of one
155 // or more of the following concrete expression nodes.
158 // A BadExpr node is a placeholder for expressions containing
159 // syntax errors for which no correct expression nodes can be
163 From, To token.Pos // position range of bad expression
166 // An Ident node represents an identifier.
168 NamePos token.Pos // identifier position
169 Name string // identifier name
170 Obj *Object // denoted object; or nil
173 // An Ellipsis node stands for the "..." type in a
174 // parameter list or the "..." length in an array type.
177 Ellipsis token.Pos // position of "..."
178 Elt Expr // ellipsis element type (parameter lists only); or nil
181 // A BasicLit node represents a literal of basic type.
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`
188 // A FuncLit node represents a function literal.
190 Type *FuncType // function type
191 Body *BlockStmt // function body
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 "}"
202 // A ParenExpr node represents a parenthesized expression.
204 Lparen token.Pos // position of "("
205 X Expr // parenthesized expression
206 Rparen token.Pos // position of ")"
209 // A SelectorExpr node represents an expression followed by a selector.
210 SelectorExpr struct {
212 Sel *Ident // field selector
215 // An IndexExpr node represents an expression followed by an index.
218 Lbrack token.Pos // position of "["
219 Index Expr // index expression
220 Rbrack token.Pos // position of "]"
223 // An SliceExpr node represents an expression followed by slice indices.
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 "]"
232 // A TypeAssertExpr node represents an expression followed by a
235 TypeAssertExpr struct {
237 Type Expr // asserted type; nil means type switch X.(type)
240 // A CallExpr node represents an expression followed by an argument list.
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 ")"
249 // A StarExpr node represents an expression of the form "*" Expression.
250 // Semantically it could be a unary "*" expression, or a pointer type.
253 Star token.Pos // position of "*"
257 // A UnaryExpr node represents a unary expression.
258 // Unary "*" expressions are represented via StarExpr nodes.
261 OpPos token.Pos // position of Op
262 Op token.Token // operator
266 // A BinaryExpr node represents a binary expression.
268 X Expr // left operand
269 OpPos token.Pos // position of Op
270 Op token.Token // operator
271 Y Expr // right operand
274 // A KeyValueExpr node represents (key : value) pairs
275 // in composite literals.
277 KeyValueExpr struct {
279 Colon token.Pos // position of ":"
284 // The direction of a channel type is indicated by one
285 // of the following constants.
290 SEND ChanDir = 1 << iota
294 // A type is represented by a tree consisting of one
295 // or more of the following type-specific expression
299 // An ArrayType node represents an array or slice type.
301 Lbrack token.Pos // position of "["
302 Len Expr // Ellipsis node for [...]T array types, nil for slice types
303 Elt Expr // element type
306 // A StructType node represents a struct type.
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
313 // Pointer types are represented via StarExpr nodes.
315 // A FuncType node represents a function type.
317 Func token.Pos // position of "func" keyword
318 Params *FieldList // (incoming) parameters; or nil
319 Results *FieldList // (outgoing) results; or nil
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
329 // A MapType node represents a map type.
331 Map token.Pos // position of "map" keyword
336 // A ChanType node represents a channel type.
338 Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
339 Dir ChanDir // channel direction
340 Value Expr // value type
344 // Pos and End implementations for expression/type nodes.
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 {
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 }
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 {
380 return x.Ellipsis + 3 // len("...")
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 {
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()
406 return x.Params.End()
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() }
412 // exprNode() ensures that only expression/type nodes can be
413 // assigned to an ExprNode.
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() {}
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() {}
439 // ----------------------------------------------------------------------------
440 // Convenience functions for Idents
444 // NewIdent creates a new Ident without position.
445 // Useful for ASTs generated by code other than the Go parser.
447 func NewIdent(name string) *Ident { return &Ident{noPos, name, nil} }
449 // IsExported returns whether name is an exported Go symbol
450 // (i.e., whether it begins with an uppercase letter).
452 func IsExported(name string) bool {
453 ch, _ := utf8.DecodeRuneInString(name)
454 return unicode.IsUpper(ch)
457 // IsExported returns whether id is an exported Go symbol
458 // (i.e., whether it begins with an uppercase letter).
460 func (id *Ident) IsExported() bool { return IsExported(id.Name) }
462 func (id *Ident) String() string {
469 // ----------------------------------------------------------------------------
472 // A statement is represented by a tree consisting of one
473 // or more of the following concrete statement nodes.
476 // A BadStmt node is a placeholder for statements containing
477 // syntax errors for which no correct statement nodes can be
481 From, To token.Pos // position range of bad statement
484 // A DeclStmt node represents a declaration in a statement list.
489 // An EmptyStmt node represents an empty statement.
490 // The "position" of the empty statement is the position
491 // of the immediately preceding semicolon.
494 Semicolon token.Pos // position of preceding ";"
497 // A LabeledStmt node represents a labeled statement.
500 Colon token.Pos // position of ":"
504 // An ExprStmt node represents a (stand-alone) expression
505 // in a statement list.
511 // A SendStmt node represents a send statement.
514 Arrow token.Pos // position of "<-"
518 // An IncDecStmt node represents an increment or decrement statement.
521 TokPos token.Pos // position of Tok
522 Tok token.Token // INC or DEC
525 // An AssignStmt node represents an assignment or
526 // a short variable declaration.
530 TokPos token.Pos // position of Tok
531 Tok token.Token // assignment token, DEFINE
535 // A GoStmt node represents a go statement.
537 Go token.Pos // position of "go" keyword
541 // A DeferStmt node represents a defer statement.
543 Defer token.Pos // position of "defer" keyword
547 // A ReturnStmt node represents a return statement.
549 Return token.Pos // position of "return" keyword
550 Results []Expr // result expressions; or nil
553 // A BranchStmt node represents a break, continue, goto,
554 // or fallthrough statement.
557 TokPos token.Pos // position of Tok
558 Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
559 Label *Ident // label name; or nil
562 // A BlockStmt node represents a braced statement list.
564 Lbrace token.Pos // position of "{"
566 Rbrace token.Pos // position of "}"
569 // An IfStmt node represents an if statement.
571 If token.Pos // position of "if" keyword
572 Init Stmt // initialization statement; or nil
573 Cond Expr // condition
575 Else Stmt // else branch; or nil
578 // A CaseClause represents a case of an expression or type switch statement.
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
586 // A SwitchStmt node represents an expression switch statement.
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
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
602 // A CommClause node represents a case of a select statement.
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
610 // An SelectStmt node represents a select statement.
612 Select token.Pos // position of "select" keyword
613 Body *BlockStmt // CommClauses only
616 // A ForStmt represents a for statement.
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
625 // A RangeStmt represents a for statement with a range clause.
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
636 // Pos and End implementations for statement nodes.
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 }
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(";") */
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("++") */
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()
678 return s.Return + 6 // len("return")
680 func (s *BranchStmt) End() token.Pos {
684 return token.Pos(int(s.TokPos) + len(s.Tok.String()))
686 func (s *BlockStmt) End() token.Pos { return s.Rbrace + 1 }
687 func (s *IfStmt) End() token.Pos {
693 func (s *CaseClause) End() token.Pos {
694 if n := len(s.Body); n > 0 {
695 return s.Body[n-1].End()
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()
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() }
711 // stmtNode() ensures that only statement nodes can be
712 // assigned to a StmtNode.
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() {}
736 // ----------------------------------------------------------------------------
739 // A Spec node represents a single (non-parenthesized) import,
740 // constant, type, or variable declaration.
743 // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
749 // An ImportSpec node represents a single package import.
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 EndPos token.Pos // end of spec (overrides Path.Pos if nonzero)
758 // A ValueSpec node represents a constant or variable declaration
759 // (ConstSpec or VarSpec production).
762 Doc *CommentGroup // associated documentation; or nil
763 Names []*Ident // value names (len(Names) > 0)
764 Type Expr // value type; or nil
765 Values []Expr // initial values; or nil
766 Comment *CommentGroup // line comments; or nil
769 // A TypeSpec node represents a type declaration (TypeSpec production).
771 Doc *CommentGroup // associated documentation; or nil
772 Name *Ident // type name
773 Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
774 Comment *CommentGroup // line comments; or nil
778 // Pos and End implementations for spec nodes.
780 func (s *ImportSpec) Pos() token.Pos {
786 func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
787 func (s *TypeSpec) Pos() token.Pos { return s.Name.Pos() }
789 func (s *ImportSpec) End() token.Pos {
796 func (s *ValueSpec) End() token.Pos {
797 if n := len(s.Values); n > 0 {
798 return s.Values[n-1].End()
803 return s.Names[len(s.Names)-1].End()
805 func (s *TypeSpec) End() token.Pos { return s.Type.End() }
807 // specNode() ensures that only spec nodes can be
808 // assigned to a Spec.
810 func (s *ImportSpec) specNode() {}
811 func (s *ValueSpec) specNode() {}
812 func (s *TypeSpec) specNode() {}
814 // A declaration is represented by one of the following declaration nodes.
817 // A BadDecl node is a placeholder for declarations containing
818 // syntax errors for which no correct declaration nodes can be
822 From, To token.Pos // position range of bad declaration
825 // A GenDecl node (generic declaration node) represents an import,
826 // constant, type or variable declaration. A valid Lparen position
827 // (Lparen.Line > 0) indicates a parenthesized declaration.
829 // Relationship between Tok value and Specs element type:
831 // token.IMPORT *ImportSpec
832 // token.CONST *ValueSpec
833 // token.TYPE *TypeSpec
834 // token.VAR *ValueSpec
837 Doc *CommentGroup // associated documentation; or nil
838 TokPos token.Pos // position of Tok
839 Tok token.Token // IMPORT, CONST, TYPE, VAR
840 Lparen token.Pos // position of '(', if any
842 Rparen token.Pos // position of ')', if any
845 // A FuncDecl node represents a function declaration.
847 Doc *CommentGroup // associated documentation; or nil
848 Recv *FieldList // receiver (methods); or nil (functions)
849 Name *Ident // function/method name
850 Type *FuncType // position of Func keyword, parameters and results
851 Body *BlockStmt // function body; or nil (forward declaration)
855 // Pos and End implementations for declaration nodes.
857 func (d *BadDecl) Pos() token.Pos { return d.From }
858 func (d *GenDecl) Pos() token.Pos { return d.TokPos }
859 func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
861 func (d *BadDecl) End() token.Pos { return d.To }
862 func (d *GenDecl) End() token.Pos {
863 if d.Rparen.IsValid() {
866 return d.Specs[0].End()
868 func (d *FuncDecl) End() token.Pos {
875 // declNode() ensures that only declaration nodes can be
876 // assigned to a DeclNode.
878 func (d *BadDecl) declNode() {}
879 func (d *GenDecl) declNode() {}
880 func (d *FuncDecl) declNode() {}
882 // ----------------------------------------------------------------------------
883 // Files and packages
885 // A File node represents a Go source file.
887 // The Comments list contains all comments in the source file in order of
888 // appearance, including the comments that are pointed to from other nodes
889 // via Doc and Comment fields.
892 Doc *CommentGroup // associated documentation; or nil
893 Package token.Pos // position of "package" keyword
894 Name *Ident // package name
895 Decls []Decl // top-level declarations; or nil
896 Scope *Scope // package scope (this file only)
897 Imports []*ImportSpec // imports in this file
898 Unresolved []*Ident // unresolved identifiers in this file
899 Comments []*CommentGroup // list of all comments in the source file
902 func (f *File) Pos() token.Pos { return f.Package }
903 func (f *File) End() token.Pos {
904 if n := len(f.Decls); n > 0 {
905 return f.Decls[n-1].End()
910 // A Package node represents a set of source files
911 // collectively building a Go package.
913 type Package struct {
914 Name string // package name
915 Scope *Scope // package scope across all files
916 Imports map[string]*Object // map of package id -> package object
917 Files map[string]*File // Go source files by filename
920 func (p *Package) Pos() token.Pos { return token.NoPos }
921 func (p *Package) End() token.Pos { return token.NoPos }