github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/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  		Bad   bool // true means the literal Value has syntax errors
   143  		expr
   144  	}
   145  
   146  	// Type { ElemList[0], ElemList[1], ... }
   147  	CompositeLit struct {
   148  		Type     Expr // nil means no literal type
   149  		ElemList []Expr
   150  		NKeys    int // number of elements with keys
   151  		Rbrace   Pos
   152  		expr
   153  	}
   154  
   155  	// Key: Value
   156  	KeyValueExpr struct {
   157  		Key, Value Expr
   158  		expr
   159  	}
   160  
   161  	// func Type { Body }
   162  	FuncLit struct {
   163  		Type *FuncType
   164  		Body *BlockStmt
   165  		expr
   166  	}
   167  
   168  	// (X)
   169  	ParenExpr struct {
   170  		X Expr
   171  		expr
   172  	}
   173  
   174  	// X.Sel
   175  	SelectorExpr struct {
   176  		X   Expr
   177  		Sel *Name
   178  		expr
   179  	}
   180  
   181  	// X[Index]
   182  	IndexExpr struct {
   183  		X     Expr
   184  		Index Expr
   185  		expr
   186  	}
   187  
   188  	// X[Index[0] : Index[1] : Index[2]]
   189  	SliceExpr struct {
   190  		X     Expr
   191  		Index [3]Expr
   192  		// Full indicates whether this is a simple or full slice expression.
   193  		// In a valid AST, this is equivalent to Index[2] != nil.
   194  		// TODO(mdempsky): This is only needed to report the "3-index
   195  		// slice of string" error when Index[2] is missing.
   196  		Full bool
   197  		expr
   198  	}
   199  
   200  	// X.(Type)
   201  	AssertExpr struct {
   202  		X    Expr
   203  		Type Expr
   204  		expr
   205  	}
   206  
   207  	// X.(type)
   208  	// Lhs := X.(type)
   209  	TypeSwitchGuard struct {
   210  		Lhs *Name // nil means no Lhs :=
   211  		X   Expr  // X.(type)
   212  		expr
   213  	}
   214  
   215  	Operation struct {
   216  		Op   Operator
   217  		X, Y Expr // Y == nil means unary expression
   218  		expr
   219  	}
   220  
   221  	// Fun(ArgList[0], ArgList[1], ...)
   222  	CallExpr struct {
   223  		Fun     Expr
   224  		ArgList []Expr // nil means no arguments
   225  		HasDots bool   // last argument is followed by ...
   226  		expr
   227  	}
   228  
   229  	// ElemList[0], ElemList[1], ...
   230  	ListExpr struct {
   231  		ElemList []Expr
   232  		expr
   233  	}
   234  
   235  	// [Len]Elem
   236  	ArrayType struct {
   237  		// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
   238  		Len  Expr // nil means Len is ...
   239  		Elem Expr
   240  		expr
   241  	}
   242  
   243  	// []Elem
   244  	SliceType struct {
   245  		Elem Expr
   246  		expr
   247  	}
   248  
   249  	// ...Elem
   250  	DotsType struct {
   251  		Elem Expr
   252  		expr
   253  	}
   254  
   255  	// struct { FieldList[0] TagList[0]; FieldList[1] TagList[1]; ... }
   256  	StructType struct {
   257  		FieldList []*Field
   258  		TagList   []*BasicLit // i >= len(TagList) || TagList[i] == nil means no tag for field i
   259  		expr
   260  	}
   261  
   262  	// Name Type
   263  	//      Type
   264  	Field struct {
   265  		Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded interface (interfaces)
   266  		Type Expr  // field names declared in a list share the same Type (identical pointers)
   267  		node
   268  	}
   269  
   270  	// interface { MethodList[0]; MethodList[1]; ... }
   271  	InterfaceType struct {
   272  		MethodList []*Field
   273  		expr
   274  	}
   275  
   276  	FuncType struct {
   277  		ParamList  []*Field
   278  		ResultList []*Field
   279  		expr
   280  	}
   281  
   282  	// map[Key]Value
   283  	MapType struct {
   284  		Key, Value Expr
   285  		expr
   286  	}
   287  
   288  	//   chan Elem
   289  	// <-chan Elem
   290  	// chan<- Elem
   291  	ChanType struct {
   292  		Dir  ChanDir // 0 means no direction
   293  		Elem Expr
   294  		expr
   295  	}
   296  )
   297  
   298  type expr struct{ node }
   299  
   300  func (*expr) aExpr() {}
   301  
   302  type ChanDir uint
   303  
   304  const (
   305  	_ ChanDir = iota
   306  	SendOnly
   307  	RecvOnly
   308  )
   309  
   310  // ----------------------------------------------------------------------------
   311  // Statements
   312  
   313  type (
   314  	Stmt interface {
   315  		Node
   316  		aStmt()
   317  	}
   318  
   319  	SimpleStmt interface {
   320  		Stmt
   321  		aSimpleStmt()
   322  	}
   323  
   324  	EmptyStmt struct {
   325  		simpleStmt
   326  	}
   327  
   328  	LabeledStmt struct {
   329  		Label *Name
   330  		Stmt  Stmt
   331  		stmt
   332  	}
   333  
   334  	BlockStmt struct {
   335  		List   []Stmt
   336  		Rbrace Pos
   337  		stmt
   338  	}
   339  
   340  	ExprStmt struct {
   341  		X Expr
   342  		simpleStmt
   343  	}
   344  
   345  	SendStmt struct {
   346  		Chan, Value Expr // Chan <- Value
   347  		simpleStmt
   348  	}
   349  
   350  	DeclStmt struct {
   351  		DeclList []Decl
   352  		stmt
   353  	}
   354  
   355  	AssignStmt struct {
   356  		Op       Operator // 0 means no operation
   357  		Lhs, Rhs Expr     // Rhs == ImplicitOne means Lhs++ (Op == Add) or Lhs-- (Op == Sub)
   358  		simpleStmt
   359  	}
   360  
   361  	BranchStmt struct {
   362  		Tok   token // Break, Continue, Fallthrough, or Goto
   363  		Label *Name
   364  		// Target is the continuation of the control flow after executing
   365  		// the branch; it is computed by the parser if CheckBranches is set.
   366  		// Target is a *LabeledStmt for gotos, and a *SwitchStmt, *SelectStmt,
   367  		// or *ForStmt for breaks and continues, depending on the context of
   368  		// the branch. Target is not set for fallthroughs.
   369  		Target Stmt
   370  		stmt
   371  	}
   372  
   373  	CallStmt struct {
   374  		Tok  token // Go or Defer
   375  		Call *CallExpr
   376  		stmt
   377  	}
   378  
   379  	ReturnStmt struct {
   380  		Results Expr // nil means no explicit return values
   381  		stmt
   382  	}
   383  
   384  	IfStmt struct {
   385  		Init SimpleStmt
   386  		Cond Expr
   387  		Then *BlockStmt
   388  		Else Stmt // either nil, *IfStmt, or *BlockStmt
   389  		stmt
   390  	}
   391  
   392  	ForStmt struct {
   393  		Init SimpleStmt // incl. *RangeClause
   394  		Cond Expr
   395  		Post SimpleStmt
   396  		Body *BlockStmt
   397  		stmt
   398  	}
   399  
   400  	SwitchStmt struct {
   401  		Init   SimpleStmt
   402  		Tag    Expr // incl. *TypeSwitchGuard
   403  		Body   []*CaseClause
   404  		Rbrace Pos
   405  		stmt
   406  	}
   407  
   408  	SelectStmt struct {
   409  		Body   []*CommClause
   410  		Rbrace Pos
   411  		stmt
   412  	}
   413  )
   414  
   415  type (
   416  	RangeClause struct {
   417  		Lhs Expr // nil means no Lhs = or Lhs :=
   418  		Def bool // means :=
   419  		X   Expr // range X
   420  		simpleStmt
   421  	}
   422  
   423  	CaseClause struct {
   424  		Cases Expr // nil means default clause
   425  		Body  []Stmt
   426  		Colon Pos
   427  		node
   428  	}
   429  
   430  	CommClause struct {
   431  		Comm  SimpleStmt // send or receive stmt; nil means default clause
   432  		Body  []Stmt
   433  		Colon Pos
   434  		node
   435  	}
   436  )
   437  
   438  type stmt struct{ node }
   439  
   440  func (stmt) aStmt() {}
   441  
   442  type simpleStmt struct {
   443  	stmt
   444  }
   445  
   446  func (simpleStmt) aSimpleStmt() {}
   447  
   448  // ----------------------------------------------------------------------------
   449  // Comments
   450  
   451  // TODO(gri) Consider renaming to CommentPos, CommentPlacement, etc.
   452  //           Kind = Above doesn't make much sense.
   453  type CommentKind uint
   454  
   455  const (
   456  	Above CommentKind = iota
   457  	Below
   458  	Left
   459  	Right
   460  )
   461  
   462  type Comment struct {
   463  	Kind CommentKind
   464  	Text string
   465  	Next *Comment
   466  }