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