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

View as plain text