/pkg/go/ast/ast.go

http://github.com/border/golang-china · Go · 739 lines · 422 code · 122 blank · 195 comment · 12 complexity · d16f79d20cfd656630d0bac55317a078 MD5 · raw file

  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. // The AST package declares the types used to represent
  5. // syntax trees for Go packages.
  6. //
  7. package ast
  8. import (
  9. "go/token"
  10. "unicode"
  11. "utf8"
  12. )
  13. // ----------------------------------------------------------------------------
  14. // Interfaces
  15. //
  16. // There are 3 main classes of nodes: Expressions and type nodes,
  17. // statement nodes, and declaration nodes. The node names usually
  18. // match the corresponding Go spec production names to which they
  19. // correspond. The node fields correspond to the individual parts
  20. // of the respective productions.
  21. //
  22. // All nodes contain position information marking the beginning of
  23. // the corresponding source text segment; it is accessible via the
  24. // Pos accessor method. Nodes may contain additional position info
  25. // for language constructs where comments may be found between parts
  26. // of the construct (typically any larger, parenthesized subpart).
  27. // That position information is needed to properly position comments
  28. // when printing the construct.
  29. // All node types implement the Node interface.
  30. type Node interface {
  31. // Pos returns the (beginning) position of the node.
  32. Pos() token.Position
  33. }
  34. // All expression nodes implement the Expr interface.
  35. type Expr interface {
  36. Node
  37. exprNode()
  38. }
  39. // All statement nodes implement the Stmt interface.
  40. type Stmt interface {
  41. Node
  42. stmtNode()
  43. }
  44. // All declaration nodes implement the Decl interface.
  45. type Decl interface {
  46. Node
  47. declNode()
  48. }
  49. // ----------------------------------------------------------------------------
  50. // Comments
  51. // A Comment node represents a single //-style or /*-style comment.
  52. type Comment struct {
  53. token.Position // beginning position of the comment
  54. Text []byte // comment text (excluding '\n' for //-style comments)
  55. }
  56. // A CommentGroup represents a sequence of comments
  57. // with no other tokens and no empty lines between.
  58. //
  59. type CommentGroup struct {
  60. List []*Comment
  61. }
  62. // ----------------------------------------------------------------------------
  63. // Expressions and types
  64. // A Field represents a Field declaration list in a struct type,
  65. // a method list in an interface type, or a parameter/result declaration
  66. // in a signature.
  67. //
  68. type Field struct {
  69. Doc *CommentGroup // associated documentation; or nil
  70. Names []*Ident // field/method/parameter names; or nil if anonymous field
  71. Type Expr // field/method/parameter type
  72. Tag *BasicLit // field tag; or nil
  73. Comment *CommentGroup // line comments; or nil
  74. }
  75. func (f *Field) Pos() token.Position {
  76. if len(f.Names) > 0 {
  77. return f.Names[0].Pos()
  78. }
  79. return f.Type.Pos()
  80. }
  81. // A FieldList represents a list of Fields, enclosed by parentheses or braces.
  82. type FieldList struct {
  83. Opening token.Position // position of opening parenthesis/brace
  84. List []*Field // field list
  85. Closing token.Position // position of closing parenthesis/brace
  86. }
  87. // NumFields returns the number of (named and anonymous fields) in a FieldList.
  88. func (f *FieldList) NumFields() int {
  89. n := 0
  90. if f != nil {
  91. for _, g := range f.List {
  92. m := len(g.Names)
  93. if m == 0 {
  94. m = 1 // anonymous field
  95. }
  96. n += m
  97. }
  98. }
  99. return n
  100. }
  101. // An expression is represented by a tree consisting of one
  102. // or more of the following concrete expression nodes.
  103. //
  104. type (
  105. // A BadExpr node is a placeholder for expressions containing
  106. // syntax errors for which no correct expression nodes can be
  107. // created.
  108. //
  109. BadExpr struct {
  110. token.Position // beginning position of bad expression
  111. }
  112. // An Ident node represents an identifier.
  113. Ident struct {
  114. token.Position // identifier position
  115. Obj *Object // denoted object
  116. }
  117. // An Ellipsis node stands for the "..." type in a
  118. // parameter list or the "..." length in an array type.
  119. //
  120. Ellipsis struct {
  121. token.Position // position of "..."
  122. Elt Expr // ellipsis element type (parameter lists only)
  123. }
  124. // A BasicLit node represents a literal of basic type.
  125. BasicLit struct {
  126. token.Position // literal position
  127. Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
  128. Value []byte // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 'a', '\x7f', "foo" or `\m\n\o`
  129. }
  130. // A FuncLit node represents a function literal.
  131. FuncLit struct {
  132. Type *FuncType // function type
  133. Body *BlockStmt // function body
  134. }
  135. // A CompositeLit node represents a composite literal.
  136. //
  137. CompositeLit struct {
  138. Type Expr // literal type
  139. Lbrace token.Position // position of "{"
  140. Elts []Expr // list of composite elements
  141. Rbrace token.Position // position of "}"
  142. }
  143. // A ParenExpr node represents a parenthesized expression.
  144. ParenExpr struct {
  145. token.Position // position of "("
  146. X Expr // parenthesized expression
  147. Rparen token.Position // position of ")"
  148. }
  149. // A SelectorExpr node represents an expression followed by a selector.
  150. SelectorExpr struct {
  151. X Expr // expression
  152. Sel *Ident // field selector
  153. }
  154. // An IndexExpr node represents an expression followed by an index.
  155. IndexExpr struct {
  156. X Expr // expression
  157. Index Expr // index expression
  158. }
  159. // An SliceExpr node represents an expression followed by slice indices.
  160. SliceExpr struct {
  161. X Expr // expression
  162. Index Expr // beginning of slice range
  163. End Expr // end of slice range; or nil
  164. }
  165. // A TypeAssertExpr node represents an expression followed by a
  166. // type assertion.
  167. //
  168. TypeAssertExpr struct {
  169. X Expr // expression
  170. Type Expr // asserted type; nil means type switch X.(type)
  171. }
  172. // A CallExpr node represents an expression followed by an argument list.
  173. CallExpr struct {
  174. Fun Expr // function expression
  175. Lparen token.Position // position of "("
  176. Args []Expr // function arguments
  177. Rparen token.Position // positions of ")"
  178. }
  179. // A StarExpr node represents an expression of the form "*" Expression.
  180. // Semantically it could be a unary "*" expression, or a pointer type.
  181. StarExpr struct {
  182. token.Position // position of "*"
  183. X Expr // operand
  184. }
  185. // A UnaryExpr node represents a unary expression.
  186. // Unary "*" expressions are represented via StarExpr nodes.
  187. //
  188. UnaryExpr struct {
  189. token.Position // position of Op
  190. Op token.Token // operator
  191. X Expr // operand
  192. }
  193. // A BinaryExpr node represents a binary expression.
  194. //
  195. BinaryExpr struct {
  196. X Expr // left operand
  197. OpPos token.Position // position of Op
  198. Op token.Token // operator
  199. Y Expr // right operand
  200. }
  201. // A KeyValueExpr node represents (key : value) pairs
  202. // in composite literals.
  203. //
  204. KeyValueExpr struct {
  205. Key Expr
  206. Colon token.Position // position of ":"
  207. Value Expr
  208. }
  209. )
  210. // The direction of a channel type is indicated by one
  211. // of the following constants.
  212. //
  213. type ChanDir int
  214. const (
  215. SEND ChanDir = 1 << iota
  216. RECV
  217. )
  218. // A type is represented by a tree consisting of one
  219. // or more of the following type-specific expression
  220. // nodes.
  221. //
  222. type (
  223. // An ArrayType node represents an array or slice type.
  224. ArrayType struct {
  225. token.Position // position of "["
  226. Len Expr // Ellipsis node for [...]T array types, nil for slice types
  227. Elt Expr // element type
  228. }
  229. // A StructType node represents a struct type.
  230. StructType struct {
  231. token.Position // position of "struct" keyword
  232. Fields *FieldList // list of field declarations
  233. Incomplete bool // true if (source) fields are missing in the Fields list
  234. }
  235. // Pointer types are represented via StarExpr nodes.
  236. // A FuncType node represents a function type.
  237. FuncType struct {
  238. token.Position // position of "func" keyword
  239. Params *FieldList // (incoming) parameters
  240. Results *FieldList // (outgoing) results
  241. }
  242. // An InterfaceType node represents an interface type.
  243. InterfaceType struct {
  244. token.Position // position of "interface" keyword
  245. Methods *FieldList // list of methods
  246. Incomplete bool // true if (source) methods are missing in the Methods list
  247. }
  248. // A MapType node represents a map type.
  249. MapType struct {
  250. token.Position // position of "map" keyword
  251. Key Expr
  252. Value Expr
  253. }
  254. // A ChanType node represents a channel type.
  255. ChanType struct {
  256. token.Position // position of "chan" keyword or "<-" (whichever comes first)
  257. Dir ChanDir // channel direction
  258. Value Expr // value type
  259. }
  260. )
  261. // Pos() implementations for expression/type where the position
  262. // corresponds to the position of a sub-node.
  263. //
  264. func (x *FuncLit) Pos() token.Position { return x.Type.Pos() }
  265. func (x *CompositeLit) Pos() token.Position { return x.Type.Pos() }
  266. func (x *SelectorExpr) Pos() token.Position { return x.X.Pos() }
  267. func (x *IndexExpr) Pos() token.Position { return x.X.Pos() }
  268. func (x *SliceExpr) Pos() token.Position { return x.X.Pos() }
  269. func (x *TypeAssertExpr) Pos() token.Position { return x.X.Pos() }
  270. func (x *CallExpr) Pos() token.Position { return x.Fun.Pos() }
  271. func (x *BinaryExpr) Pos() token.Position { return x.X.Pos() }
  272. func (x *KeyValueExpr) Pos() token.Position { return x.Key.Pos() }
  273. // exprNode() ensures that only expression/type nodes can be
  274. // assigned to an ExprNode.
  275. func (x *BadExpr) exprNode() {}
  276. func (x *Ident) exprNode() {}
  277. func (x *Ellipsis) exprNode() {}
  278. func (x *BasicLit) exprNode() {}
  279. func (x *FuncLit) exprNode() {}
  280. func (x *CompositeLit) exprNode() {}
  281. func (x *ParenExpr) exprNode() {}
  282. func (x *SelectorExpr) exprNode() {}
  283. func (x *IndexExpr) exprNode() {}
  284. func (x *SliceExpr) exprNode() {}
  285. func (x *TypeAssertExpr) exprNode() {}
  286. func (x *CallExpr) exprNode() {}
  287. func (x *StarExpr) exprNode() {}
  288. func (x *UnaryExpr) exprNode() {}
  289. func (x *BinaryExpr) exprNode() {}
  290. func (x *KeyValueExpr) exprNode() {}
  291. func (x *ArrayType) exprNode() {}
  292. func (x *StructType) exprNode() {}
  293. func (x *FuncType) exprNode() {}
  294. func (x *InterfaceType) exprNode() {}
  295. func (x *MapType) exprNode() {}
  296. func (x *ChanType) exprNode() {}
  297. // ----------------------------------------------------------------------------
  298. // Convenience functions for Idents
  299. var noPos token.Position
  300. // NewIdent creates a new Ident without position and minimal object
  301. // information. Useful for ASTs generated by code other than the Go
  302. // parser.
  303. //
  304. func NewIdent(name string) *Ident { return &Ident{noPos, NewObj(Err, noPos, name)} }
  305. // IsExported returns whether name is an exported Go symbol
  306. // (i.e., whether it begins with an uppercase letter).
  307. func IsExported(name string) bool {
  308. ch, _ := utf8.DecodeRuneInString(name)
  309. return unicode.IsUpper(ch)
  310. }
  311. // IsExported returns whether id is an exported Go symbol
  312. // (i.e., whether it begins with an uppercase letter).
  313. func (id *Ident) IsExported() bool { return id.Obj.IsExported() }
  314. // Name returns an identifier's name.
  315. func (id *Ident) Name() string { return id.Obj.Name }
  316. func (id *Ident) String() string {
  317. if id != nil && id.Obj != nil {
  318. return id.Obj.Name
  319. }
  320. return "<nil>"
  321. }
  322. // ----------------------------------------------------------------------------
  323. // Statements
  324. // A statement is represented by a tree consisting of one
  325. // or more of the following concrete statement nodes.
  326. //
  327. type (
  328. // A BadStmt node is a placeholder for statements containing
  329. // syntax errors for which no correct statement nodes can be
  330. // created.
  331. //
  332. BadStmt struct {
  333. token.Position // beginning position of bad statement
  334. }
  335. // A DeclStmt node represents a declaration in a statement list.
  336. DeclStmt struct {
  337. Decl Decl
  338. }
  339. // An EmptyStmt node represents an empty statement.
  340. // The "position" of the empty statement is the position
  341. // of the immediately preceeding semicolon.
  342. //
  343. EmptyStmt struct {
  344. token.Position // position of preceeding ";"
  345. }
  346. // A LabeledStmt node represents a labeled statement.
  347. LabeledStmt struct {
  348. Label *Ident
  349. Stmt Stmt
  350. }
  351. // An ExprStmt node represents a (stand-alone) expression
  352. // in a statement list.
  353. //
  354. ExprStmt struct {
  355. X Expr // expression
  356. }
  357. // An IncDecStmt node represents an increment or decrement statement.
  358. IncDecStmt struct {
  359. X Expr
  360. Tok token.Token // INC or DEC
  361. }
  362. // An AssignStmt node represents an assignment or
  363. // a short variable declaration.
  364. AssignStmt struct {
  365. Lhs []Expr
  366. TokPos token.Position // position of Tok
  367. Tok token.Token // assignment token, DEFINE
  368. Rhs []Expr
  369. }
  370. // A GoStmt node represents a go statement.
  371. GoStmt struct {
  372. token.Position // position of "go" keyword
  373. Call *CallExpr
  374. }
  375. // A DeferStmt node represents a defer statement.
  376. DeferStmt struct {
  377. token.Position // position of "defer" keyword
  378. Call *CallExpr
  379. }
  380. // A ReturnStmt node represents a return statement.
  381. ReturnStmt struct {
  382. token.Position // position of "return" keyword
  383. Results []Expr
  384. }
  385. // A BranchStmt node represents a break, continue, goto,
  386. // or fallthrough statement.
  387. //
  388. BranchStmt struct {
  389. token.Position // position of Tok
  390. Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
  391. Label *Ident
  392. }
  393. // A BlockStmt node represents a braced statement list.
  394. BlockStmt struct {
  395. token.Position // position of "{"
  396. List []Stmt
  397. Rbrace token.Position // position of "}"
  398. }
  399. // An IfStmt node represents an if statement.
  400. IfStmt struct {
  401. token.Position // position of "if" keyword
  402. Init Stmt
  403. Cond Expr
  404. Body *BlockStmt
  405. Else Stmt
  406. }
  407. // A CaseClause represents a case of an expression switch statement.
  408. CaseClause struct {
  409. token.Position // position of "case" or "default" keyword
  410. Values []Expr // nil means default case
  411. Colon token.Position // position of ":"
  412. Body []Stmt // statement list; or nil
  413. }
  414. // A SwitchStmt node represents an expression switch statement.
  415. SwitchStmt struct {
  416. token.Position // position of "switch" keyword
  417. Init Stmt
  418. Tag Expr
  419. Body *BlockStmt // CaseClauses only
  420. }
  421. // A TypeCaseClause represents a case of a type switch statement.
  422. TypeCaseClause struct {
  423. token.Position // position of "case" or "default" keyword
  424. Types []Expr // nil means default case
  425. Colon token.Position // position of ":"
  426. Body []Stmt // statement list; or nil
  427. }
  428. // An TypeSwitchStmt node represents a type switch statement.
  429. TypeSwitchStmt struct {
  430. token.Position // position of "switch" keyword
  431. Init Stmt
  432. Assign Stmt // x := y.(type)
  433. Body *BlockStmt // TypeCaseClauses only
  434. }
  435. // A CommClause node represents a case of a select statement.
  436. CommClause struct {
  437. token.Position // position of "case" or "default" keyword
  438. Tok token.Token // ASSIGN or DEFINE (valid only if Lhs != nil)
  439. Lhs, Rhs Expr // Rhs == nil means default case
  440. Colon token.Position // position of ":"
  441. Body []Stmt // statement list; or nil
  442. }
  443. // An SelectStmt node represents a select statement.
  444. SelectStmt struct {
  445. token.Position // position of "select" keyword
  446. Body *BlockStmt // CommClauses only
  447. }
  448. // A ForStmt represents a for statement.
  449. ForStmt struct {
  450. token.Position // position of "for" keyword
  451. Init Stmt
  452. Cond Expr
  453. Post Stmt
  454. Body *BlockStmt
  455. }
  456. // A RangeStmt represents a for statement with a range clause.
  457. RangeStmt struct {
  458. token.Position // position of "for" keyword
  459. Key, Value Expr // Value may be nil
  460. TokPos token.Position // position of Tok
  461. Tok token.Token // ASSIGN, DEFINE
  462. X Expr // value to range over
  463. Body *BlockStmt
  464. }
  465. )
  466. // Pos() implementations for statement nodes where the position
  467. // corresponds to the position of a sub-node.
  468. //
  469. func (s *DeclStmt) Pos() token.Position { return s.Decl.Pos() }
  470. func (s *LabeledStmt) Pos() token.Position { return s.Label.Pos() }
  471. func (s *ExprStmt) Pos() token.Position { return s.X.Pos() }
  472. func (s *IncDecStmt) Pos() token.Position { return s.X.Pos() }
  473. func (s *AssignStmt) Pos() token.Position { return s.Lhs[0].Pos() }
  474. // stmtNode() ensures that only statement nodes can be
  475. // assigned to a StmtNode.
  476. //
  477. func (s *BadStmt) stmtNode() {}
  478. func (s *DeclStmt) stmtNode() {}
  479. func (s *EmptyStmt) stmtNode() {}
  480. func (s *LabeledStmt) stmtNode() {}
  481. func (s *ExprStmt) stmtNode() {}
  482. func (s *IncDecStmt) stmtNode() {}
  483. func (s *AssignStmt) stmtNode() {}
  484. func (s *GoStmt) stmtNode() {}
  485. func (s *DeferStmt) stmtNode() {}
  486. func (s *ReturnStmt) stmtNode() {}
  487. func (s *BranchStmt) stmtNode() {}
  488. func (s *BlockStmt) stmtNode() {}
  489. func (s *IfStmt) stmtNode() {}
  490. func (s *CaseClause) stmtNode() {}
  491. func (s *SwitchStmt) stmtNode() {}
  492. func (s *TypeCaseClause) stmtNode() {}
  493. func (s *TypeSwitchStmt) stmtNode() {}
  494. func (s *CommClause) stmtNode() {}
  495. func (s *SelectStmt) stmtNode() {}
  496. func (s *ForStmt) stmtNode() {}
  497. func (s *RangeStmt) stmtNode() {}
  498. // ----------------------------------------------------------------------------
  499. // Declarations
  500. // A Spec node represents a single (non-parenthesized) import,
  501. // constant, type, or variable declaration.
  502. //
  503. type (
  504. // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
  505. Spec interface {
  506. Node
  507. specNode()
  508. }
  509. // An ImportSpec node represents a single package import.
  510. ImportSpec struct {
  511. Doc *CommentGroup // associated documentation; or nil
  512. Name *Ident // local package name (including "."); or nil
  513. Path *BasicLit // package path
  514. Comment *CommentGroup // line comments; or nil
  515. }
  516. // A ValueSpec node represents a constant or variable declaration
  517. // (ConstSpec or VarSpec production).
  518. ValueSpec struct {
  519. Doc *CommentGroup // associated documentation; or nil
  520. Names []*Ident // value names
  521. Type Expr // value type; or nil
  522. Values []Expr // initial values; or nil
  523. Comment *CommentGroup // line comments; or nil
  524. }
  525. // A TypeSpec node represents a type declaration (TypeSpec production).
  526. TypeSpec struct {
  527. Doc *CommentGroup // associated documentation; or nil
  528. Name *Ident // type name
  529. Type Expr // *ArrayType, *StructType, *FuncType, *InterfaceType, *MapType, *ChanType or *Ident
  530. Comment *CommentGroup // line comments; or nil
  531. }
  532. )
  533. // Pos() implementations for spec nodes.
  534. //
  535. func (s *ImportSpec) Pos() token.Position {
  536. if s.Name != nil {
  537. return s.Name.Pos()
  538. }
  539. return s.Path.Pos()
  540. }
  541. func (s *ValueSpec) Pos() token.Position { return s.Names[0].Pos() }
  542. func (s *TypeSpec) Pos() token.Position { return s.Name.Pos() }
  543. // specNode() ensures that only spec nodes can be
  544. // assigned to a Spec.
  545. //
  546. func (s *ImportSpec) specNode() {}
  547. func (s *ValueSpec) specNode() {}
  548. func (s *TypeSpec) specNode() {}
  549. // A declaration is represented by one of the following declaration nodes.
  550. //
  551. type (
  552. // A BadDecl node is a placeholder for declarations containing
  553. // syntax errors for which no correct declaration nodes can be
  554. // created.
  555. //
  556. BadDecl struct {
  557. token.Position // beginning position of bad declaration
  558. }
  559. // A GenDecl node (generic declaration node) represents an import,
  560. // constant, type or variable declaration. A valid Lparen position
  561. // (Lparen.Line > 0) indicates a parenthesized declaration.
  562. //
  563. // Relationship between Tok value and Specs element type:
  564. //
  565. // token.IMPORT *ImportSpec
  566. // token.CONST *ValueSpec
  567. // token.TYPE *TypeSpec
  568. // token.VAR *ValueSpec
  569. //
  570. GenDecl struct {
  571. Doc *CommentGroup // associated documentation; or nil
  572. token.Position // position of Tok
  573. Tok token.Token // IMPORT, CONST, TYPE, VAR
  574. Lparen token.Position // position of '(', if any
  575. Specs []Spec
  576. Rparen token.Position // position of ')', if any
  577. }
  578. // A FuncDecl node represents a function declaration.
  579. FuncDecl struct {
  580. Doc *CommentGroup // associated documentation; or nil
  581. Recv *FieldList // receiver (methods); or nil (functions)
  582. Name *Ident // function/method name
  583. Type *FuncType // position of Func keyword, parameters and results
  584. Body *BlockStmt // function body; or nil (forward declaration)
  585. }
  586. )
  587. // The position of a FuncDecl node is the position of its function type.
  588. func (d *FuncDecl) Pos() token.Position { return d.Type.Pos() }
  589. // declNode() ensures that only declaration nodes can be
  590. // assigned to a DeclNode.
  591. //
  592. func (d *BadDecl) declNode() {}
  593. func (d *GenDecl) declNode() {}
  594. func (d *FuncDecl) declNode() {}
  595. // ----------------------------------------------------------------------------
  596. // Files and packages
  597. // A File node represents a Go source file.
  598. //
  599. // The Comments list contains all comments in the source file in order of
  600. // appearance, including the comments that are pointed to from other nodes
  601. // via Doc and Comment fields.
  602. //
  603. type File struct {
  604. Doc *CommentGroup // associated documentation; or nil
  605. token.Position // position of "package" keyword
  606. Name *Ident // package name
  607. Decls []Decl // top-level declarations
  608. Comments []*CommentGroup // list of all comments in the source file
  609. }
  610. // A Package node represents a set of source files
  611. // collectively building a Go package.
  612. //
  613. type Package struct {
  614. Name string // package name
  615. Scope *Scope // package scope
  616. Files map[string]*File // Go source files by filename
  617. }