github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/compile/internal/syntax/nodes.go (about)

     1  // Copyright 2016 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 syntax
     6  
     7  import "cmd/internal/src"
     8  
     9  // ----------------------------------------------------------------------------
    10  // Nodes
    11  
    12  type Node interface {
    13  	// Pos() returns the position associated with the node as follows:
    14  	// 1) The position of a node representing a terminal syntax production
    15  	//    (Name, BasicLit, etc.) is the position of the respective production
    16  	//    in the source.
    17  	// 2) The position of a node representing a non-terminal production
    18  	//    (IndexExpr, IfStmt, etc.) is the position of a token uniquely
    19  	//    associated with that production; usually the left-most one
    20  	//    ('[' for IndexExpr, 'if' for IfStmt, etc.)
    21  	Pos() src.Pos
    22  	aNode()
    23  }
    24  
    25  type node struct {
    26  	// commented out for now since not yet used
    27  	// doc  *Comment // nil means no comment(s) attached
    28  	pos src.Pos
    29  }
    30  
    31  func (n *node) Pos() src.Pos { return n.pos }
    32  func (*node) aNode()         {}
    33  
    34  // ----------------------------------------------------------------------------
    35  // Files
    36  
    37  // package PkgName; DeclList[0], DeclList[1], ...
    38  type File struct {
    39  	PkgName  *Name
    40  	DeclList []Decl
    41  	Lines    uint
    42  	node
    43  }
    44  
    45  // ----------------------------------------------------------------------------
    46  // Declarations
    47  
    48  type (
    49  	Decl interface {
    50  		Node
    51  		aDecl()
    52  	}
    53  
    54  	//              Path
    55  	// LocalPkgName Path
    56  	ImportDecl struct {
    57  		LocalPkgName *Name // including "."; nil means no rename present
    58  		Path         *BasicLit
    59  		Group        *Group // nil means not part of a group
    60  		decl
    61  	}
    62  
    63  	// NameList
    64  	// NameList      = Values
    65  	// NameList Type = Values
    66  	ConstDecl struct {
    67  		NameList []*Name
    68  		Type     Expr   // nil means no type
    69  		Values   Expr   // nil means no values
    70  		Group    *Group // nil means not part of a group
    71  		decl
    72  	}
    73  
    74  	// Name Type
    75  	TypeDecl struct {
    76  		Name   *Name
    77  		Alias  bool
    78  		Type   Expr
    79  		Group  *Group // nil means not part of a group
    80  		Pragma Pragma
    81  		decl
    82  	}
    83  
    84  	// NameList Type
    85  	// NameList Type = Values
    86  	// NameList      = Values
    87  	VarDecl struct {
    88  		NameList []*Name
    89  		Type     Expr   // nil means no type
    90  		Values   Expr   // nil means no values
    91  		Group    *Group // nil means not part of a group
    92  		decl
    93  	}
    94  
    95  	// func          Name Type { Body }
    96  	// func          Name Type
    97  	// func Receiver Name Type { Body }
    98  	// func Receiver Name Type
    99  	FuncDecl struct {
   100  		Attr   map[string]bool // go:attr map
   101  		Recv   *Field          // nil means regular function
   102  		Name   *Name
   103  		Type   *FuncType
   104  		Body   *BlockStmt // nil means no body (forward declaration)
   105  		Pragma Pragma     // TODO(mdempsky): Cleaner solution.
   106  		decl
   107  	}
   108  )
   109  
   110  type decl struct{ node }
   111  
   112  func (*decl) aDecl() {}
   113  
   114  // All declarations belonging to the same group point to the same Group node.
   115  type Group struct {
   116  	dummy int // not empty so we are guaranteed different Group instances
   117  }
   118  
   119  // ----------------------------------------------------------------------------
   120  // Expressions
   121  
   122  type (
   123  	Expr interface {
   124  		Node
   125  		aExpr()
   126  	}
   127  
   128  	// Placeholder for an expression that failed to parse
   129  	// correctly and where we can't provide a better node.
   130  	BadExpr struct {
   131  		expr
   132  	}
   133  
   134  	// Value
   135  	Name struct {
   136  		Value string
   137  		expr
   138  	}
   139  
   140  	// Value
   141  	BasicLit struct {
   142  		Value string
   143  		Kind  LitKind
   144  		expr
   145  	}
   146  
   147  	// Type { ElemList[0], ElemList[1], ... }
   148  	CompositeLit struct {
   149  		Type     Expr // nil means no literal type
   150  		ElemList []Expr
   151  		NKeys    int // number of elements with keys
   152  		Rbrace   src.Pos
   153  		expr
   154  	}
   155  
   156  	// Key: Value
   157  	KeyValueExpr struct {
   158  		Key, Value Expr
   159  		expr
   160  	}
   161  
   162  	// func Type { Body }
   163  	FuncLit struct {
   164  		Type *FuncType
   165  		Body *BlockStmt
   166  		expr
   167  	}
   168  
   169  	// (X)
   170  	ParenExpr struct {
   171  		X Expr
   172  		expr
   173  	}
   174  
   175  	// X.Sel
   176  	SelectorExpr struct {
   177  		X   Expr
   178  		Sel *Name
   179  		expr
   180  	}
   181  
   182  	// X[Index]
   183  	IndexExpr struct {
   184  		X     Expr
   185  		Index Expr
   186  		expr
   187  	}
   188  
   189  	// X[Index[0] : Index[1] : Index[2]]
   190  	SliceExpr struct {
   191  		X     Expr
   192  		Index [3]Expr
   193  		// Full indicates whether this is a simple or full slice expression.
   194  		// In a valid AST, this is equivalent to Index[2] != nil.
   195  		// TODO(mdempsky): This is only needed to report the "3-index
   196  		// slice of string" error when Index[2] is missing.
   197  		Full bool
   198  		expr
   199  	}
   200  
   201  	// X.(Type)
   202  	AssertExpr struct {
   203  		X Expr
   204  		// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
   205  		Type Expr
   206  		expr
   207  	}
   208  
   209  	Operation struct {
   210  		Op   Operator
   211  		X, Y Expr // Y == nil means unary expression
   212  		expr
   213  	}
   214  
   215  	// Fun(ArgList[0], ArgList[1], ...)
   216  	CallExpr struct {
   217  		Fun     Expr
   218  		ArgList []Expr
   219  		HasDots bool // last argument is followed by ...
   220  		expr
   221  	}
   222  
   223  	// ElemList[0], ElemList[1], ...
   224  	ListExpr struct {
   225  		ElemList []Expr
   226  		expr
   227  	}
   228  
   229  	// [Len]Elem
   230  	ArrayType struct {
   231  		// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
   232  		Len  Expr // nil means Len is ...
   233  		Elem Expr
   234  		expr
   235  	}
   236  
   237  	// []Elem
   238  	SliceType struct {
   239  		Elem Expr
   240  		expr
   241  	}
   242  
   243  	// ...Elem
   244  	DotsType struct {
   245  		Elem Expr
   246  		expr
   247  	}
   248  
   249  	// struct { FieldList[0] TagList[0]; FieldList[1] TagList[1]; ... }
   250  	StructType struct {
   251  		FieldList []*Field
   252  		TagList   []*BasicLit // i >= len(TagList) || TagList[i] == nil means no tag for field i
   253  		expr
   254  	}
   255  
   256  	// Name Type
   257  	//      Type
   258  	Field struct {
   259  		Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded interface (interfaces)
   260  		Type Expr  // field names declared in a list share the same Type (identical pointers)
   261  		node
   262  	}
   263  
   264  	// interface { MethodList[0]; MethodList[1]; ... }
   265  	InterfaceType struct {
   266  		MethodList []*Field
   267  		expr
   268  	}
   269  
   270  	FuncType struct {
   271  		ParamList  []*Field
   272  		ResultList []*Field
   273  		expr
   274  	}
   275  
   276  	// map[Key]Value
   277  	MapType struct {
   278  		Key   Expr
   279  		Value Expr
   280  		expr
   281  	}
   282  
   283  	//   chan Elem
   284  	// <-chan Elem
   285  	// chan<- Elem
   286  	ChanType struct {
   287  		Dir  ChanDir // 0 means no direction
   288  		Elem Expr
   289  		expr
   290  	}
   291  )
   292  
   293  type expr struct{ node }
   294  
   295  func (*expr) aExpr() {}
   296  
   297  type ChanDir uint
   298  
   299  const (
   300  	_ ChanDir = iota
   301  	SendOnly
   302  	RecvOnly
   303  )
   304  
   305  // ----------------------------------------------------------------------------
   306  // Statements
   307  
   308  type (
   309  	Stmt interface {
   310  		Node
   311  		aStmt()
   312  	}
   313  
   314  	SimpleStmt interface {
   315  		Stmt
   316  		aSimpleStmt()
   317  	}
   318  
   319  	EmptyStmt struct {
   320  		simpleStmt
   321  	}
   322  
   323  	LabeledStmt struct {
   324  		Label *Name
   325  		Stmt  Stmt
   326  		stmt
   327  	}
   328  
   329  	BlockStmt struct {
   330  		List   []Stmt
   331  		Rbrace src.Pos
   332  		stmt
   333  	}
   334  
   335  	ExprStmt struct {
   336  		X Expr
   337  		simpleStmt
   338  	}
   339  
   340  	SendStmt struct {
   341  		Chan, Value Expr // Chan <- Value
   342  		simpleStmt
   343  	}
   344  
   345  	DeclStmt struct {
   346  		DeclList []Decl
   347  		stmt
   348  	}
   349  
   350  	AssignStmt struct {
   351  		Op       Operator // 0 means no operation
   352  		Lhs, Rhs Expr     // Rhs == ImplicitOne means Lhs++ (Op == Add) or Lhs-- (Op == Sub)
   353  		simpleStmt
   354  	}
   355  
   356  	BranchStmt struct {
   357  		Tok   token // Break, Continue, Fallthrough, or Goto
   358  		Label *Name
   359  		stmt
   360  	}
   361  
   362  	CallStmt struct {
   363  		Tok  token // Go or Defer
   364  		Call *CallExpr
   365  		stmt
   366  	}
   367  
   368  	ReturnStmt struct {
   369  		Results Expr // nil means no explicit return values
   370  		stmt
   371  	}
   372  
   373  	IfStmt struct {
   374  		Init SimpleStmt
   375  		Cond Expr
   376  		Then *BlockStmt
   377  		Else Stmt // either *IfStmt or *BlockStmt
   378  		stmt
   379  	}
   380  
   381  	ForStmt struct {
   382  		Init SimpleStmt // incl. *RangeClause
   383  		Cond Expr
   384  		Post SimpleStmt
   385  		Body *BlockStmt
   386  		stmt
   387  	}
   388  
   389  	SwitchStmt struct {
   390  		Init   SimpleStmt
   391  		Tag    Expr
   392  		Body   []*CaseClause
   393  		Rbrace src.Pos
   394  		stmt
   395  	}
   396  
   397  	SelectStmt struct {
   398  		Body   []*CommClause
   399  		Rbrace src.Pos
   400  		stmt
   401  	}
   402  )
   403  
   404  type (
   405  	RangeClause struct {
   406  		Lhs Expr // nil means no Lhs = or Lhs :=
   407  		Def bool // means :=
   408  		X   Expr // range X
   409  		simpleStmt
   410  	}
   411  
   412  	TypeSwitchGuard struct {
   413  		// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
   414  		Lhs *Name // nil means no Lhs :=
   415  		X   Expr  // X.(type)
   416  		expr
   417  	}
   418  
   419  	CaseClause struct {
   420  		Cases Expr // nil means default clause
   421  		Body  []Stmt
   422  		Colon src.Pos
   423  		node
   424  	}
   425  
   426  	CommClause struct {
   427  		Comm  SimpleStmt // send or receive stmt; nil means default clause
   428  		Body  []Stmt
   429  		Colon src.Pos
   430  		node
   431  	}
   432  )
   433  
   434  type stmt struct{ node }
   435  
   436  func (stmt) aStmt() {}
   437  
   438  type simpleStmt struct {
   439  	stmt
   440  }
   441  
   442  func (simpleStmt) aSimpleStmt() {}
   443  
   444  // ----------------------------------------------------------------------------
   445  // Comments
   446  
   447  // TODO(gri) Consider renaming to CommentPos, CommentPlacement, etc.
   448  //           Kind = Above doesn't make much sense.
   449  type CommentKind uint
   450  
   451  const (
   452  	Above CommentKind = iota
   453  	Below
   454  	Left
   455  	Right
   456  )
   457  
   458  type Comment struct {
   459  	Kind CommentKind
   460  	Text string
   461  	Next *Comment
   462  }