Source file src/cmd/compile/internal/syntax/nodes.go

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

View as plain text