github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/go/ast/ast.go (about) 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 "strings" 13 "unicode" 14 "unicode/utf8" 15 ) 16 17 // ---------------------------------------------------------------------------- 18 // Interfaces 19 // 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. 25 // 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. 33 34 // All node types implement the Node interface. 35 type 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 38 } 39 40 // All expression nodes implement the Expr interface. 41 type Expr interface { 42 Node 43 exprNode() 44 } 45 46 // All statement nodes implement the Stmt interface. 47 type Stmt interface { 48 Node 49 stmtNode() 50 } 51 52 // All declaration nodes implement the Decl interface. 53 type Decl interface { 54 Node 55 declNode() 56 } 57 58 // ---------------------------------------------------------------------------- 59 // Comments 60 61 // A Comment node represents a single //-style or /*-style comment. 62 type Comment struct { 63 Slash token.Pos // position of "/" starting the comment 64 Text string // comment text (excluding '\n' for //-style comments) 65 } 66 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)) } 69 70 // A CommentGroup represents a sequence of comments 71 // with no other tokens and no empty lines between. 72 // 73 type CommentGroup struct { 74 List []*Comment // len(List) > 0 75 } 76 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() } 79 80 func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' } 81 82 func stripTrailingWhitespace(s string) string { 83 i := len(s) 84 for i > 0 && isWhitespace(s[i-1]) { 85 i-- 86 } 87 return s[0:i] 88 } 89 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. 95 // 96 func (g *CommentGroup) Text() string { 97 if g == nil { 98 return "" 99 } 100 comments := make([]string, len(g.List)) 101 for i, c := range g.List { 102 comments[i] = string(c.Text) 103 } 104 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. 109 switch c[1] { 110 case '/': 111 //-style comment (no newline at the end) 112 c = c[2:] 113 // strip first space - required for Example tests 114 if len(c) > 0 && c[0] == ' ' { 115 c = c[1:] 116 } 117 case '*': 118 /*-style comment */ 119 c = c[2 : len(c)-2] 120 } 121 122 // Split on newlines. 123 cl := strings.Split(c, "\n") 124 125 // Walk lines, stripping trailing white space and adding to list. 126 for _, l := range cl { 127 lines = append(lines, stripTrailingWhitespace(l)) 128 } 129 } 130 131 // Remove leading blank lines; convert runs of 132 // interior blank lines to a single blank line. 133 n := 0 134 for _, line := range lines { 135 if line != "" || n > 0 && lines[n-1] != "" { 136 lines[n] = line 137 n++ 138 } 139 } 140 lines = lines[0:n] 141 142 // Add final "" entry to get trailing newline from Join. 143 if n > 0 && lines[n-1] != "" { 144 lines = append(lines, "") 145 } 146 147 return strings.Join(lines, "\n") 148 } 149 150 // ---------------------------------------------------------------------------- 151 // Expressions and types 152 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 155 // in a signature. 156 // 157 type Field struct { 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 163 } 164 165 func (f *Field) Pos() token.Pos { 166 if len(f.Names) > 0 { 167 return f.Names[0].Pos() 168 } 169 return f.Type.Pos() 170 } 171 172 func (f *Field) End() token.Pos { 173 if f.Tag != nil { 174 return f.Tag.End() 175 } 176 return f.Type.End() 177 } 178 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 184 } 185 186 func (f *FieldList) Pos() token.Pos { 187 if f.Opening.IsValid() { 188 return f.Opening 189 } 190 // the list should not be empty in this case; 191 // be conservative and guard against bad ASTs 192 if len(f.List) > 0 { 193 return f.List[0].Pos() 194 } 195 return token.NoPos 196 } 197 198 func (f *FieldList) End() token.Pos { 199 if f.Closing.IsValid() { 200 return f.Closing + 1 201 } 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() 206 } 207 return token.NoPos 208 } 209 210 // NumFields returns the number of (named and anonymous fields) in a FieldList. 211 func (f *FieldList) NumFields() int { 212 n := 0 213 if f != nil { 214 for _, g := range f.List { 215 m := len(g.Names) 216 if m == 0 { 217 m = 1 // anonymous field 218 } 219 n += m 220 } 221 } 222 return n 223 } 224 225 // An expression is represented by a tree consisting of one 226 // or more of the following concrete expression nodes. 227 // 228 type ( 229 // A BadExpr node is a placeholder for expressions containing 230 // syntax errors for which no correct expression nodes can be 231 // created. 232 // 233 BadExpr struct { 234 From, To token.Pos // position range of bad expression 235 } 236 237 // An Ident node represents an identifier. 238 Ident struct { 239 NamePos token.Pos // identifier position 240 Name string // identifier name 241 Obj *Object // denoted object; or nil 242 } 243 244 // An Ellipsis node stands for the "..." type in a 245 // parameter list or the "..." length in an array type. 246 // 247 Ellipsis struct { 248 Ellipsis token.Pos // position of "..." 249 Elt Expr // ellipsis element type (parameter lists only); or nil 250 } 251 252 // A BasicLit node represents a literal of basic type. 253 BasicLit struct { 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` 257 } 258 259 // A FuncLit node represents a function literal. 260 FuncLit struct { 261 Type *FuncType // function type 262 Body *BlockStmt // function body 263 } 264 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 "}" 271 } 272 273 // A ParenExpr node represents a parenthesized expression. 274 ParenExpr struct { 275 Lparen token.Pos // position of "(" 276 X Expr // parenthesized expression 277 Rparen token.Pos // position of ")" 278 } 279 280 // A SelectorExpr node represents an expression followed by a selector. 281 SelectorExpr struct { 282 X Expr // expression 283 Sel *Ident // field selector 284 } 285 286 // An IndexExpr node represents an expression followed by an index. 287 IndexExpr struct { 288 X Expr // expression 289 Lbrack token.Pos // position of "[" 290 Index Expr // index expression 291 Rbrack token.Pos // position of "]" 292 } 293 294 // An SliceExpr node represents an expression followed by slice indices. 295 SliceExpr struct { 296 X Expr // expression 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 "]" 301 } 302 303 // A TypeAssertExpr node represents an expression followed by a 304 // type assertion. 305 // 306 TypeAssertExpr struct { 307 X Expr // expression 308 Type Expr // asserted type; nil means type switch X.(type) 309 } 310 311 // A CallExpr node represents an expression followed by an argument list. 312 CallExpr struct { 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 ")" 318 } 319 320 // A StarExpr node represents an expression of the form "*" Expression. 321 // Semantically it could be a unary "*" expression, or a pointer type. 322 // 323 StarExpr struct { 324 Star token.Pos // position of "*" 325 X Expr // operand 326 } 327 328 // A UnaryExpr node represents a unary expression. 329 // Unary "*" expressions are represented via StarExpr nodes. 330 // 331 UnaryExpr struct { 332 OpPos token.Pos // position of Op 333 Op token.Token // operator 334 X Expr // operand 335 } 336 337 // A BinaryExpr node represents a binary expression. 338 BinaryExpr struct { 339 X Expr // left operand 340 OpPos token.Pos // position of Op 341 Op token.Token // operator 342 Y Expr // right operand 343 } 344 345 // A KeyValueExpr node represents (key : value) pairs 346 // in composite literals. 347 // 348 KeyValueExpr struct { 349 Key Expr 350 Colon token.Pos // position of ":" 351 Value Expr 352 } 353 ) 354 355 // The direction of a channel type is indicated by one 356 // of the following constants. 357 // 358 type ChanDir int 359 360 const ( 361 SEND ChanDir = 1 << iota 362 RECV 363 ) 364 365 // A type is represented by a tree consisting of one 366 // or more of the following type-specific expression 367 // nodes. 368 // 369 type ( 370 // An ArrayType node represents an array or slice type. 371 ArrayType struct { 372 Lbrack token.Pos // position of "[" 373 Len Expr // Ellipsis node for [...]T array types, nil for slice types 374 Elt Expr // element type 375 } 376 377 // A StructType node represents a struct type. 378 StructType struct { 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 382 } 383 384 // Pointer types are represented via StarExpr nodes. 385 386 // A FuncType node represents a function type. 387 FuncType struct { 388 Func token.Pos // position of "func" keyword 389 Params *FieldList // (incoming) parameters; or nil 390 Results *FieldList // (outgoing) results; or nil 391 } 392 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 398 } 399 400 // A MapType node represents a map type. 401 MapType struct { 402 Map token.Pos // position of "map" keyword 403 Key Expr 404 Value Expr 405 } 406 407 // A ChanType node represents a channel type. 408 ChanType struct { 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 413 } 414 ) 415 416 // Pos and End implementations for expression/type nodes. 417 // 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 { 424 if x.Type != nil { 425 return x.Type.Pos() 426 } 427 return x.Lbrace 428 } 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 } 445 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 { 449 if x.Elt != nil { 450 return x.Elt.End() 451 } 452 return x.Ellipsis + 3 // len("...") 453 } 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 { 462 if x.Type != nil { 463 return x.Type.End() 464 } 465 return x.X.End() 466 } 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() 477 } 478 return x.Params.End() 479 } 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() } 483 484 // exprNode() ensures that only expression/type nodes can be 485 // assigned to an ExprNode. 486 // 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() {} 503 504 func (*ArrayType) exprNode() {} 505 func (*StructType) exprNode() {} 506 func (*FuncType) exprNode() {} 507 func (*InterfaceType) exprNode() {} 508 func (*MapType) exprNode() {} 509 func (*ChanType) exprNode() {} 510 511 // ---------------------------------------------------------------------------- 512 // Convenience functions for Idents 513 514 var noPos token.Pos 515 516 // NewIdent creates a new Ident without position. 517 // Useful for ASTs generated by code other than the Go parser. 518 // 519 func NewIdent(name string) *Ident { return &Ident{noPos, name, nil} } 520 521 // IsExported returns whether name is an exported Go symbol 522 // (i.e., whether it begins with an uppercase letter). 523 // 524 func IsExported(name string) bool { 525 ch, _ := utf8.DecodeRuneInString(name) 526 return unicode.IsUpper(ch) 527 } 528 529 // IsExported returns whether id is an exported Go symbol 530 // (i.e., whether it begins with an uppercase letter). 531 // 532 func (id *Ident) IsExported() bool { return IsExported(id.Name) } 533 534 func (id *Ident) String() string { 535 if id != nil { 536 return id.Name 537 } 538 return "<nil>" 539 } 540 541 // ---------------------------------------------------------------------------- 542 // Statements 543 544 // A statement is represented by a tree consisting of one 545 // or more of the following concrete statement nodes. 546 // 547 type ( 548 // A BadStmt node is a placeholder for statements containing 549 // syntax errors for which no correct statement nodes can be 550 // created. 551 // 552 BadStmt struct { 553 From, To token.Pos // position range of bad statement 554 } 555 556 // A DeclStmt node represents a declaration in a statement list. 557 DeclStmt struct { 558 Decl Decl // *GenDecl with CONST, TYPE, or VAR token 559 } 560 561 // An EmptyStmt node represents an empty statement. 562 // The "position" of the empty statement is the position 563 // of the immediately preceding semicolon. 564 // 565 EmptyStmt struct { 566 Semicolon token.Pos // position of preceding ";" 567 } 568 569 // A LabeledStmt node represents a labeled statement. 570 LabeledStmt struct { 571 Label *Ident 572 Colon token.Pos // position of ":" 573 Stmt Stmt 574 } 575 576 // An ExprStmt node represents a (stand-alone) expression 577 // in a statement list. 578 // 579 ExprStmt struct { 580 X Expr // expression 581 } 582 583 // A SendStmt node represents a send statement. 584 SendStmt struct { 585 Chan Expr 586 Arrow token.Pos // position of "<-" 587 Value Expr 588 } 589 590 // An IncDecStmt node represents an increment or decrement statement. 591 IncDecStmt struct { 592 X Expr 593 TokPos token.Pos // position of Tok 594 Tok token.Token // INC or DEC 595 } 596 597 // An AssignStmt node represents an assignment or 598 // a short variable declaration. 599 // 600 AssignStmt struct { 601 Lhs []Expr 602 TokPos token.Pos // position of Tok 603 Tok token.Token // assignment token, DEFINE 604 Rhs []Expr 605 } 606 607 // A GoStmt node represents a go statement. 608 GoStmt struct { 609 Go token.Pos // position of "go" keyword 610 Call *CallExpr 611 } 612 613 // A DeferStmt node represents a defer statement. 614 DeferStmt struct { 615 Defer token.Pos // position of "defer" keyword 616 Call *CallExpr 617 } 618 619 // A ReturnStmt node represents a return statement. 620 ReturnStmt struct { 621 Return token.Pos // position of "return" keyword 622 Results []Expr // result expressions; or nil 623 } 624 625 // A BranchStmt node represents a break, continue, goto, 626 // or fallthrough statement. 627 // 628 BranchStmt struct { 629 TokPos token.Pos // position of Tok 630 Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) 631 Label *Ident // label name; or nil 632 } 633 634 // A BlockStmt node represents a braced statement list. 635 BlockStmt struct { 636 Lbrace token.Pos // position of "{" 637 List []Stmt 638 Rbrace token.Pos // position of "}" 639 } 640 641 // An IfStmt node represents an if statement. 642 IfStmt struct { 643 If token.Pos // position of "if" keyword 644 Init Stmt // initialization statement; or nil 645 Cond Expr // condition 646 Body *BlockStmt 647 Else Stmt // else branch; or nil 648 } 649 650 // A CaseClause represents a case of an expression or type switch statement. 651 CaseClause struct { 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 656 } 657 658 // A SwitchStmt node represents an expression switch statement. 659 SwitchStmt struct { 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 664 } 665 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 672 } 673 674 // A CommClause node represents a case of a select statement. 675 CommClause struct { 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 680 } 681 682 // An SelectStmt node represents a select statement. 683 SelectStmt struct { 684 Select token.Pos // position of "select" keyword 685 Body *BlockStmt // CommClauses only 686 } 687 688 // A ForStmt represents a for statement. 689 ForStmt struct { 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 694 Body *BlockStmt 695 } 696 697 // A RangeStmt represents a for statement with a range clause. 698 RangeStmt struct { 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 704 Body *BlockStmt 705 } 706 ) 707 708 // Pos and End implementations for statement nodes. 709 // 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 } 731 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(";") */ 736 } 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("++") */ 742 } 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() 749 } 750 return s.Return + 6 // len("return") 751 } 752 func (s *BranchStmt) End() token.Pos { 753 if s.Label != nil { 754 return s.Label.End() 755 } 756 return token.Pos(int(s.TokPos) + len(s.Tok.String())) 757 } 758 func (s *BlockStmt) End() token.Pos { return s.Rbrace + 1 } 759 func (s *IfStmt) End() token.Pos { 760 if s.Else != nil { 761 return s.Else.End() 762 } 763 return s.Body.End() 764 } 765 func (s *CaseClause) End() token.Pos { 766 if n := len(s.Body); n > 0 { 767 return s.Body[n-1].End() 768 } 769 return s.Colon + 1 770 } 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() 776 } 777 return s.Colon + 1 778 } 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() } 782 783 // stmtNode() ensures that only statement nodes can be 784 // assigned to a StmtNode. 785 // 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() {} 807 808 // ---------------------------------------------------------------------------- 809 // Declarations 810 811 // A Spec node represents a single (non-parenthesized) import, 812 // constant, type, or variable declaration. 813 // 814 type ( 815 // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec. 816 Spec interface { 817 Node 818 specNode() 819 } 820 821 // An ImportSpec node represents a single package import. 822 ImportSpec struct { 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) 828 } 829 830 // A ValueSpec node represents a constant or variable declaration 831 // (ConstSpec or VarSpec production). 832 // 833 ValueSpec struct { 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 839 } 840 841 // A TypeSpec node represents a type declaration (TypeSpec production). 842 TypeSpec struct { 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 847 } 848 ) 849 850 // Pos and End implementations for spec nodes. 851 // 852 func (s *ImportSpec) Pos() token.Pos { 853 if s.Name != nil { 854 return s.Name.Pos() 855 } 856 return s.Path.Pos() 857 } 858 func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() } 859 func (s *TypeSpec) Pos() token.Pos { return s.Name.Pos() } 860 861 func (s *ImportSpec) End() token.Pos { 862 if s.EndPos != 0 { 863 return s.EndPos 864 } 865 return s.Path.End() 866 } 867 868 func (s *ValueSpec) End() token.Pos { 869 if n := len(s.Values); n > 0 { 870 return s.Values[n-1].End() 871 } 872 if s.Type != nil { 873 return s.Type.End() 874 } 875 return s.Names[len(s.Names)-1].End() 876 } 877 func (s *TypeSpec) End() token.Pos { return s.Type.End() } 878 879 // specNode() ensures that only spec nodes can be 880 // assigned to a Spec. 881 // 882 func (*ImportSpec) specNode() {} 883 func (*ValueSpec) specNode() {} 884 func (*TypeSpec) specNode() {} 885 886 // A declaration is represented by one of the following declaration nodes. 887 // 888 type ( 889 // A BadDecl node is a placeholder for declarations containing 890 // syntax errors for which no correct declaration nodes can be 891 // created. 892 // 893 BadDecl struct { 894 From, To token.Pos // position range of bad declaration 895 } 896 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. 900 // 901 // Relationship between Tok value and Specs element type: 902 // 903 // token.IMPORT *ImportSpec 904 // token.CONST *ValueSpec 905 // token.TYPE *TypeSpec 906 // token.VAR *ValueSpec 907 // 908 GenDecl struct { 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 913 Specs []Spec 914 Rparen token.Pos // position of ')', if any 915 } 916 917 // A FuncDecl node represents a function declaration. 918 FuncDecl struct { 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) 924 } 925 ) 926 927 // Pos and End implementations for declaration nodes. 928 // 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() } 932 933 func (d *BadDecl) End() token.Pos { return d.To } 934 func (d *GenDecl) End() token.Pos { 935 if d.Rparen.IsValid() { 936 return d.Rparen + 1 937 } 938 return d.Specs[0].End() 939 } 940 func (d *FuncDecl) End() token.Pos { 941 if d.Body != nil { 942 return d.Body.End() 943 } 944 return d.Type.End() 945 } 946 947 // declNode() ensures that only declaration nodes can be 948 // assigned to a DeclNode. 949 // 950 func (*BadDecl) declNode() {} 951 func (*GenDecl) declNode() {} 952 func (*FuncDecl) declNode() {} 953 954 // ---------------------------------------------------------------------------- 955 // Files and packages 956 957 // A File node represents a Go source file. 958 // 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. 962 // 963 type File struct { 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 972 } 973 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() 978 } 979 return f.Name.End() 980 } 981 982 // A Package node represents a set of source files 983 // collectively building a Go package. 984 // 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 990 } 991 992 func (p *Package) Pos() token.Pos { return token.NoPos } 993 func (p *Package) End() token.Pos { return token.NoPos }