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