...

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

Documentation: cmd/compile/internal/syntax

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

View as plain text