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