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
17 // ----------------------------------------------------------------------------
20 // There are 3 main classes of nodes: Expressions and type nodes,
21 // statement nodes, and declaration nodes. The node names usually
22 // match the corresponding Go spec production names to which they
23 // correspond. The node fields correspond to the individual parts
24 // of the respective productions.
26 // All nodes contain position information marking the beginning of
27 // the corresponding source text segment; it is accessible via the
28 // Pos accessor method. Nodes may contain additional position info
29 // for language constructs where comments may be found between parts
30 // of the construct (typically any larger, parenthesized subpart).
31 // That position information is needed to properly position comments
32 // when printing the construct.
34 // All node types implement the Node interface.
36 Pos() token.Pos // position of first character belonging to the node
37 End() token.Pos // position of first character immediately after the node
40 // All expression nodes implement the Expr interface.
46 // All statement nodes implement the Stmt interface.
52 // All declaration nodes implement the Decl interface.
58 // ----------------------------------------------------------------------------
61 // A Comment node represents a single //-style or /*-style comment.
63 Slash token.Pos // position of "/" starting the comment
64 Text string // comment text (excluding '\n' for //-style comments)
67 func (c *Comment) Pos() token.Pos { return c.Slash }
68 func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) }
70 // A CommentGroup represents a sequence of comments
71 // with no other tokens and no empty lines between.
73 type CommentGroup struct {
74 List []*Comment // len(List) > 0
77 func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() }
78 func (g *CommentGroup) End() token.Pos { return g.List[len(g.List)-1].End() }
80 func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' }
82 func stripTrailingWhitespace(s string) string {
84 for i > 0 && isWhitespace(s[i-1]) {
90 // Text returns the text of the comment.
91 // Comment markers (//, /*, and */), the first space of a line comment, and
92 // leading and trailing empty lines are removed. Multiple empty lines are
93 // reduced to one, and trailing space on lines is trimmed. Unless the result
94 // is empty, it is newline-terminated.
96 func (g *CommentGroup) Text() string {
100 comments := make([]string, len(g.List))
101 for i, c := range g.List {
102 comments[i] = string(c.Text)
105 lines := make([]string, 0, 10) // most comments are less than 10 lines
106 for _, c := range comments {
107 // Remove comment markers.
108 // The parser has given us exactly the comment text.
111 //-style comment (no newline at the end)
113 // strip first space - required for Example tests
114 if len(c) > 0 && c[0] == ' ' {
122 // Split on newlines.
123 cl := strings.Split(c, "\n")
125 // Walk lines, stripping trailing white space and adding to list.
126 for _, l := range cl {
127 lines = append(lines, stripTrailingWhitespace(l))
131 // Remove leading blank lines; convert runs of
132 // interior blank lines to a single blank line.
134 for _, line := range lines {
135 if line != "" || n > 0 && lines[n-1] != "" {
142 // Add final "" entry to get trailing newline from Join.
143 if n > 0 && lines[n-1] != "" {
144 lines = append(lines, "")
147 return strings.Join(lines, "\n")
150 // ----------------------------------------------------------------------------
151 // Expressions and types
153 // A Field represents a Field declaration list in a struct type,
154 // a method list in an interface type, or a parameter/result declaration
158 Doc *CommentGroup // associated documentation; or nil
159 Names []*Ident // field/method/parameter names; or nil if anonymous field
160 Type Expr // field/method/parameter type
161 Tag *BasicLit // field tag; or nil
162 Comment *CommentGroup // line comments; or nil
165 func (f *Field) Pos() token.Pos {
166 if len(f.Names) > 0 {
167 return f.Names[0].Pos()
172 func (f *Field) End() token.Pos {
179 // A FieldList represents a list of Fields, enclosed by parentheses or braces.
180 type FieldList struct {
181 Opening token.Pos // position of opening parenthesis/brace, if any
182 List []*Field // field list; or nil
183 Closing token.Pos // position of closing parenthesis/brace, if any
186 func (f *FieldList) Pos() token.Pos {
187 if f.Opening.IsValid() {
190 // the list should not be empty in this case;
191 // be conservative and guard against bad ASTs
193 return f.List[0].Pos()
198 func (f *FieldList) End() token.Pos {
199 if f.Closing.IsValid() {
202 // the list should not be empty in this case;
203 // be conservative and guard against bad ASTs
204 if n := len(f.List); n > 0 {
205 return f.List[n-1].End()
210 // NumFields returns the number of (named and anonymous fields) in a FieldList.
211 func (f *FieldList) NumFields() int {
214 for _, g := range f.List {
217 m = 1 // anonymous field
225 // An expression is represented by a tree consisting of one
226 // or more of the following concrete expression nodes.
229 // A BadExpr node is a placeholder for expressions containing
230 // syntax errors for which no correct expression nodes can be
234 From, To token.Pos // position range of bad expression
237 // An Ident node represents an identifier.
239 NamePos token.Pos // identifier position
240 Name string // identifier name
241 Obj *Object // denoted object; or nil
244 // An Ellipsis node stands for the "..." type in a
245 // parameter list or the "..." length in an array type.
248 Ellipsis token.Pos // position of "..."
249 Elt Expr // ellipsis element type (parameter lists only); or nil
252 // A BasicLit node represents a literal of basic type.
254 ValuePos token.Pos // literal position
255 Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
256 Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
259 // A FuncLit node represents a function literal.
261 Type *FuncType // function type
262 Body *BlockStmt // function body
265 // A CompositeLit node represents a composite literal.
266 CompositeLit struct {
267 Type Expr // literal type; or nil
268 Lbrace token.Pos // position of "{"
269 Elts []Expr // list of composite elements; or nil
270 Rbrace token.Pos // position of "}"
273 // A ParenExpr node represents a parenthesized expression.
275 Lparen token.Pos // position of "("
276 X Expr // parenthesized expression
277 Rparen token.Pos // position of ")"
280 // A SelectorExpr node represents an expression followed by a selector.
281 SelectorExpr struct {
283 Sel *Ident // field selector
286 // An IndexExpr node represents an expression followed by an index.
289 Lbrack token.Pos // position of "["
290 Index Expr // index expression
291 Rbrack token.Pos // position of "]"
294 // An SliceExpr node represents an expression followed by slice indices.
297 Lbrack token.Pos // position of "["
298 Low Expr // begin of slice range; or nil
299 High Expr // end of slice range; or nil
300 Rbrack token.Pos // position of "]"
303 // A TypeAssertExpr node represents an expression followed by a
306 TypeAssertExpr struct {
308 Type Expr // asserted type; nil means type switch X.(type)
311 // A CallExpr node represents an expression followed by an argument list.
313 Fun Expr // function expression
314 Lparen token.Pos // position of "("
315 Args []Expr // function arguments; or nil
316 Ellipsis token.Pos // position of "...", if any
317 Rparen token.Pos // position of ")"
320 // A StarExpr node represents an expression of the form "*" Expression.
321 // Semantically it could be a unary "*" expression, or a pointer type.
324 Star token.Pos // position of "*"
328 // A UnaryExpr node represents a unary expression.
329 // Unary "*" expressions are represented via StarExpr nodes.
332 OpPos token.Pos // position of Op
333 Op token.Token // operator
337 // A BinaryExpr node represents a binary expression.
339 X Expr // left operand
340 OpPos token.Pos // position of Op
341 Op token.Token // operator
342 Y Expr // right operand
345 // A KeyValueExpr node represents (key : value) pairs
346 // in composite literals.
348 KeyValueExpr struct {
350 Colon token.Pos // position of ":"
355 // The direction of a channel type is indicated by one
356 // of the following constants.
361 SEND ChanDir = 1 << iota
365 // A type is represented by a tree consisting of one
366 // or more of the following type-specific expression
370 // An ArrayType node represents an array or slice type.
372 Lbrack token.Pos // position of "["
373 Len Expr // Ellipsis node for [...]T array types, nil for slice types
374 Elt Expr // element type
377 // A StructType node represents a struct type.
379 Struct token.Pos // position of "struct" keyword
380 Fields *FieldList // list of field declarations
381 Incomplete bool // true if (source) fields are missing in the Fields list
384 // Pointer types are represented via StarExpr nodes.
386 // A FuncType node represents a function type.
388 Func token.Pos // position of "func" keyword
389 Params *FieldList // (incoming) parameters; or nil
390 Results *FieldList // (outgoing) results; or nil
393 // An InterfaceType node represents an interface type.
394 InterfaceType struct {
395 Interface token.Pos // position of "interface" keyword
396 Methods *FieldList // list of methods
397 Incomplete bool // true if (source) methods are missing in the Methods list
400 // A MapType node represents a map type.
402 Map token.Pos // position of "map" keyword
407 // A ChanType node represents a channel type.
409 Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first)
410 Arrow token.Pos // position of "<-" (noPos if there is no "<-")
411 Dir ChanDir // channel direction
412 Value Expr // value type
416 // Pos and End implementations for expression/type nodes.
418 func (x *BadExpr) Pos() token.Pos { return x.From }
419 func (x *Ident) Pos() token.Pos { return x.NamePos }
420 func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis }
421 func (x *BasicLit) Pos() token.Pos { return x.ValuePos }
422 func (x *FuncLit) Pos() token.Pos { return x.Type.Pos() }
423 func (x *CompositeLit) Pos() token.Pos {
429 func (x *ParenExpr) Pos() token.Pos { return x.Lparen }
430 func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() }
431 func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() }
432 func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() }
433 func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() }
434 func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() }
435 func (x *StarExpr) Pos() token.Pos { return x.Star }
436 func (x *UnaryExpr) Pos() token.Pos { return x.OpPos }
437 func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() }
438 func (x *KeyValueExpr) Pos() token.Pos { return x.Key.Pos() }
439 func (x *ArrayType) Pos() token.Pos { return x.Lbrack }
440 func (x *StructType) Pos() token.Pos { return x.Struct }
441 func (x *FuncType) Pos() token.Pos { return x.Func }
442 func (x *InterfaceType) Pos() token.Pos { return x.Interface }
443 func (x *MapType) Pos() token.Pos { return x.Map }
444 func (x *ChanType) Pos() token.Pos { return x.Begin }
446 func (x *BadExpr) End() token.Pos { return x.To }
447 func (x *Ident) End() token.Pos { return token.Pos(int(x.NamePos) + len(x.Name)) }
448 func (x *Ellipsis) End() token.Pos {
452 return x.Ellipsis + 3 // len("...")
454 func (x *BasicLit) End() token.Pos { return token.Pos(int(x.ValuePos) + len(x.Value)) }
455 func (x *FuncLit) End() token.Pos { return x.Body.End() }
456 func (x *CompositeLit) End() token.Pos { return x.Rbrace + 1 }
457 func (x *ParenExpr) End() token.Pos { return x.Rparen + 1 }
458 func (x *SelectorExpr) End() token.Pos { return x.Sel.End() }
459 func (x *IndexExpr) End() token.Pos { return x.Rbrack + 1 }
460 func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 }
461 func (x *TypeAssertExpr) End() token.Pos {
467 func (x *CallExpr) End() token.Pos { return x.Rparen + 1 }
468 func (x *StarExpr) End() token.Pos { return x.X.End() }
469 func (x *UnaryExpr) End() token.Pos { return x.X.End() }
470 func (x *BinaryExpr) End() token.Pos { return x.Y.End() }
471 func (x *KeyValueExpr) End() token.Pos { return x.Value.End() }
472 func (x *ArrayType) End() token.Pos { return x.Elt.End() }
473 func (x *StructType) End() token.Pos { return x.Fields.End() }
474 func (x *FuncType) End() token.Pos {
475 if x.Results != nil {
476 return x.Results.End()
478 return x.Params.End()
480 func (x *InterfaceType) End() token.Pos { return x.Methods.End() }
481 func (x *MapType) End() token.Pos { return x.Value.End() }
482 func (x *ChanType) End() token.Pos { return x.Value.End() }
484 // exprNode() ensures that only expression/type nodes can be
485 // assigned to an ExprNode.
487 func (*BadExpr) exprNode() {}
488 func (*Ident) exprNode() {}
489 func (*Ellipsis) exprNode() {}
490 func (*BasicLit) exprNode() {}
491 func (*FuncLit) exprNode() {}
492 func (*CompositeLit) exprNode() {}
493 func (*ParenExpr) exprNode() {}
494 func (*SelectorExpr) exprNode() {}
495 func (*IndexExpr) exprNode() {}
496 func (*SliceExpr) exprNode() {}
497 func (*TypeAssertExpr) exprNode() {}
498 func (*CallExpr) exprNode() {}
499 func (*StarExpr) exprNode() {}
500 func (*UnaryExpr) exprNode() {}
501 func (*BinaryExpr) exprNode() {}
502 func (*KeyValueExpr) exprNode() {}
504 func (*ArrayType) exprNode() {}
505 func (*StructType) exprNode() {}
506 func (*FuncType) exprNode() {}
507 func (*InterfaceType) exprNode() {}
508 func (*MapType) exprNode() {}
509 func (*ChanType) exprNode() {}
511 // ----------------------------------------------------------------------------
512 // Convenience functions for Idents
516 // NewIdent creates a new Ident without position.
517 // Useful for ASTs generated by code other than the Go parser.
519 func NewIdent(name string) *Ident { return &Ident{noPos, name, nil} }
521 // IsExported returns whether name is an exported Go symbol
522 // (i.e., whether it begins with an uppercase letter).
524 func IsExported(name string) bool {
525 ch, _ := utf8.DecodeRuneInString(name)
526 return unicode.IsUpper(ch)
529 // IsExported returns whether id is an exported Go symbol
530 // (i.e., whether it begins with an uppercase letter).
532 func (id *Ident) IsExported() bool { return IsExported(id.Name) }
534 func (id *Ident) String() string {
541 // ----------------------------------------------------------------------------
544 // A statement is represented by a tree consisting of one
545 // or more of the following concrete statement nodes.
548 // A BadStmt node is a placeholder for statements containing
549 // syntax errors for which no correct statement nodes can be
553 From, To token.Pos // position range of bad statement
556 // A DeclStmt node represents a declaration in a statement list.
561 // An EmptyStmt node represents an empty statement.
562 // The "position" of the empty statement is the position
563 // of the immediately preceding semicolon.
566 Semicolon token.Pos // position of preceding ";"
569 // A LabeledStmt node represents a labeled statement.
572 Colon token.Pos // position of ":"
576 // An ExprStmt node represents a (stand-alone) expression
577 // in a statement list.
583 // A SendStmt node represents a send statement.
586 Arrow token.Pos // position of "<-"
590 // An IncDecStmt node represents an increment or decrement statement.
593 TokPos token.Pos // position of Tok
594 Tok token.Token // INC or DEC
597 // An AssignStmt node represents an assignment or
598 // a short variable declaration.
602 TokPos token.Pos // position of Tok
603 Tok token.Token // assignment token, DEFINE
607 // A GoStmt node represents a go statement.
609 Go token.Pos // position of "go" keyword
613 // A DeferStmt node represents a defer statement.
615 Defer token.Pos // position of "defer" keyword
619 // A ReturnStmt node represents a return statement.
621 Return token.Pos // position of "return" keyword
622 Results []Expr // result expressions; or nil
625 // A BranchStmt node represents a break, continue, goto,
626 // or fallthrough statement.
629 TokPos token.Pos // position of Tok
630 Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
631 Label *Ident // label name; or nil
634 // A BlockStmt node represents a braced statement list.
636 Lbrace token.Pos // position of "{"
638 Rbrace token.Pos // position of "}"
641 // An IfStmt node represents an if statement.
643 If token.Pos // position of "if" keyword
644 Init Stmt // initialization statement; or nil
645 Cond Expr // condition
647 Else Stmt // else branch; or nil
650 // A CaseClause represents a case of an expression or type switch statement.
652 Case token.Pos // position of "case" or "default" keyword
653 List []Expr // list of expressions or types; nil means default case
654 Colon token.Pos // position of ":"
655 Body []Stmt // statement list; or nil
658 // A SwitchStmt node represents an expression switch statement.
660 Switch token.Pos // position of "switch" keyword
661 Init Stmt // initialization statement; or nil
662 Tag Expr // tag expression; or nil
663 Body *BlockStmt // CaseClauses only
666 // An TypeSwitchStmt node represents a type switch statement.
667 TypeSwitchStmt struct {
668 Switch token.Pos // position of "switch" keyword
669 Init Stmt // initialization statement; or nil
670 Assign Stmt // x := y.(type) or y.(type)
671 Body *BlockStmt // CaseClauses only
674 // A CommClause node represents a case of a select statement.
676 Case token.Pos // position of "case" or "default" keyword
677 Comm Stmt // send or receive statement; nil means default case
678 Colon token.Pos // position of ":"
679 Body []Stmt // statement list; or nil
682 // An SelectStmt node represents a select statement.
684 Select token.Pos // position of "select" keyword
685 Body *BlockStmt // CommClauses only
688 // A ForStmt represents a for statement.
690 For token.Pos // position of "for" keyword
691 Init Stmt // initialization statement; or nil
692 Cond Expr // condition; or nil
693 Post Stmt // post iteration statement; or nil
697 // A RangeStmt represents a for statement with a range clause.
699 For token.Pos // position of "for" keyword
700 Key, Value Expr // Value may be nil
701 TokPos token.Pos // position of Tok
702 Tok token.Token // ASSIGN, DEFINE
703 X Expr // value to range over
708 // Pos and End implementations for statement nodes.
710 func (s *BadStmt) Pos() token.Pos { return s.From }
711 func (s *DeclStmt) Pos() token.Pos { return s.Decl.Pos() }
712 func (s *EmptyStmt) Pos() token.Pos { return s.Semicolon }
713 func (s *LabeledStmt) Pos() token.Pos { return s.Label.Pos() }
714 func (s *ExprStmt) Pos() token.Pos { return s.X.Pos() }
715 func (s *SendStmt) Pos() token.Pos { return s.Chan.Pos() }
716 func (s *IncDecStmt) Pos() token.Pos { return s.X.Pos() }
717 func (s *AssignStmt) Pos() token.Pos { return s.Lhs[0].Pos() }
718 func (s *GoStmt) Pos() token.Pos { return s.Go }
719 func (s *DeferStmt) Pos() token.Pos { return s.Defer }
720 func (s *ReturnStmt) Pos() token.Pos { return s.Return }
721 func (s *BranchStmt) Pos() token.Pos { return s.TokPos }
722 func (s *BlockStmt) Pos() token.Pos { return s.Lbrace }
723 func (s *IfStmt) Pos() token.Pos { return s.If }
724 func (s *CaseClause) Pos() token.Pos { return s.Case }
725 func (s *SwitchStmt) Pos() token.Pos { return s.Switch }
726 func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch }
727 func (s *CommClause) Pos() token.Pos { return s.Case }
728 func (s *SelectStmt) Pos() token.Pos { return s.Select }
729 func (s *ForStmt) Pos() token.Pos { return s.For }
730 func (s *RangeStmt) Pos() token.Pos { return s.For }
732 func (s *BadStmt) End() token.Pos { return s.To }
733 func (s *DeclStmt) End() token.Pos { return s.Decl.End() }
734 func (s *EmptyStmt) End() token.Pos {
735 return s.Semicolon + 1 /* len(";") */
737 func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() }
738 func (s *ExprStmt) End() token.Pos { return s.X.End() }
739 func (s *SendStmt) End() token.Pos { return s.Value.End() }
740 func (s *IncDecStmt) End() token.Pos {
741 return s.TokPos + 2 /* len("++") */
743 func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() }
744 func (s *GoStmt) End() token.Pos { return s.Call.End() }
745 func (s *DeferStmt) End() token.Pos { return s.Call.End() }
746 func (s *ReturnStmt) End() token.Pos {
747 if n := len(s.Results); n > 0 {
748 return s.Results[n-1].End()
750 return s.Return + 6 // len("return")
752 func (s *BranchStmt) End() token.Pos {
756 return token.Pos(int(s.TokPos) + len(s.Tok.String()))
758 func (s *BlockStmt) End() token.Pos { return s.Rbrace + 1 }
759 func (s *IfStmt) End() token.Pos {
765 func (s *CaseClause) End() token.Pos {
766 if n := len(s.Body); n > 0 {
767 return s.Body[n-1].End()
771 func (s *SwitchStmt) End() token.Pos { return s.Body.End() }
772 func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() }
773 func (s *CommClause) End() token.Pos {
774 if n := len(s.Body); n > 0 {
775 return s.Body[n-1].End()
779 func (s *SelectStmt) End() token.Pos { return s.Body.End() }
780 func (s *ForStmt) End() token.Pos { return s.Body.End() }
781 func (s *RangeStmt) End() token.Pos { return s.Body.End() }
783 // stmtNode() ensures that only statement nodes can be
784 // assigned to a StmtNode.
786 func (*BadStmt) stmtNode() {}
787 func (*DeclStmt) stmtNode() {}
788 func (*EmptyStmt) stmtNode() {}
789 func (*LabeledStmt) stmtNode() {}
790 func (*ExprStmt) stmtNode() {}
791 func (*SendStmt) stmtNode() {}
792 func (*IncDecStmt) stmtNode() {}
793 func (*AssignStmt) stmtNode() {}
794 func (*GoStmt) stmtNode() {}
795 func (*DeferStmt) stmtNode() {}
796 func (*ReturnStmt) stmtNode() {}
797 func (*BranchStmt) stmtNode() {}
798 func (*BlockStmt) stmtNode() {}
799 func (*IfStmt) stmtNode() {}
800 func (*CaseClause) stmtNode() {}
801 func (*SwitchStmt) stmtNode() {}
802 func (*TypeSwitchStmt) stmtNode() {}
803 func (*CommClause) stmtNode() {}
804 func (*SelectStmt) stmtNode() {}
805 func (*ForStmt) stmtNode() {}
806 func (*RangeStmt) stmtNode() {}
808 // ----------------------------------------------------------------------------
811 // A Spec node represents a single (non-parenthesized) import,
812 // constant, type, or variable declaration.
815 // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
821 // An ImportSpec node represents a single package import.
823 Doc *CommentGroup // associated documentation; or nil
824 Name *Ident // local package name (including "."); or nil
825 Path *BasicLit // import path
826 Comment *CommentGroup // line comments; or nil
827 EndPos token.Pos // end of spec (overrides Path.Pos if nonzero)
830 // A ValueSpec node represents a constant or variable declaration
831 // (ConstSpec or VarSpec production).
834 Doc *CommentGroup // associated documentation; or nil
835 Names []*Ident // value names (len(Names) > 0)
836 Type Expr // value type; or nil
837 Values []Expr // initial values; or nil
838 Comment *CommentGroup // line comments; or nil
841 // A TypeSpec node represents a type declaration (TypeSpec production).
843 Doc *CommentGroup // associated documentation; or nil
844 Name *Ident // type name
845 Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes
846 Comment *CommentGroup // line comments; or nil
850 // Pos and End implementations for spec nodes.
852 func (s *ImportSpec) Pos() token.Pos {
858 func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() }
859 func (s *TypeSpec) Pos() token.Pos { return s.Name.Pos() }
861 func (s *ImportSpec) End() token.Pos {
868 func (s *ValueSpec) End() token.Pos {
869 if n := len(s.Values); n > 0 {
870 return s.Values[n-1].End()
875 return s.Names[len(s.Names)-1].End()
877 func (s *TypeSpec) End() token.Pos { return s.Type.End() }
879 // specNode() ensures that only spec nodes can be
880 // assigned to a Spec.
882 func (*ImportSpec) specNode() {}
883 func (*ValueSpec) specNode() {}
884 func (*TypeSpec) specNode() {}
886 // A declaration is represented by one of the following declaration nodes.
889 // A BadDecl node is a placeholder for declarations containing
890 // syntax errors for which no correct declaration nodes can be
894 From, To token.Pos // position range of bad declaration
897 // A GenDecl node (generic declaration node) represents an import,
898 // constant, type or variable declaration. A valid Lparen position
899 // (Lparen.Line > 0) indicates a parenthesized declaration.
901 // Relationship between Tok value and Specs element type:
903 // token.IMPORT *ImportSpec
904 // token.CONST *ValueSpec
905 // token.TYPE *TypeSpec
906 // token.VAR *ValueSpec
909 Doc *CommentGroup // associated documentation; or nil
910 TokPos token.Pos // position of Tok
911 Tok token.Token // IMPORT, CONST, TYPE, VAR
912 Lparen token.Pos // position of '(', if any
914 Rparen token.Pos // position of ')', if any
917 // A FuncDecl node represents a function declaration.
919 Doc *CommentGroup // associated documentation; or nil
920 Recv *FieldList // receiver (methods); or nil (functions)
921 Name *Ident // function/method name
922 Type *FuncType // position of Func keyword, parameters and results
923 Body *BlockStmt // function body; or nil (forward declaration)
927 // Pos and End implementations for declaration nodes.
929 func (d *BadDecl) Pos() token.Pos { return d.From }
930 func (d *GenDecl) Pos() token.Pos { return d.TokPos }
931 func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() }
933 func (d *BadDecl) End() token.Pos { return d.To }
934 func (d *GenDecl) End() token.Pos {
935 if d.Rparen.IsValid() {
938 return d.Specs[0].End()
940 func (d *FuncDecl) End() token.Pos {
947 // declNode() ensures that only declaration nodes can be
948 // assigned to a DeclNode.
950 func (*BadDecl) declNode() {}
951 func (*GenDecl) declNode() {}
952 func (*FuncDecl) declNode() {}
954 // ----------------------------------------------------------------------------
955 // Files and packages
957 // A File node represents a Go source file.
959 // The Comments list contains all comments in the source file in order of
960 // appearance, including the comments that are pointed to from other nodes
961 // via Doc and Comment fields.
964 Doc *CommentGroup // associated documentation; or nil
965 Package token.Pos // position of "package" keyword
966 Name *Ident // package name
967 Decls []Decl // top-level declarations; or nil
968 Scope *Scope // package scope (this file only)
969 Imports []*ImportSpec // imports in this file
970 Unresolved []*Ident // unresolved identifiers in this file
971 Comments []*CommentGroup // list of all comments in the source file
974 func (f *File) Pos() token.Pos { return f.Package }
975 func (f *File) End() token.Pos {
976 if n := len(f.Decls); n > 0 {
977 return f.Decls[n-1].End()
982 // A Package node represents a set of source files
983 // collectively building a Go package.
985 type Package struct {
986 Name string // package name
987 Scope *Scope // package scope across all files
988 Imports map[string]*Object // map of package id -> package object
989 Files map[string]*File // Go source files by filename
992 func (p *Package) Pos() token.Pos { return token.NoPos }
993 func (p *Package) End() token.Pos { return token.NoPos }