github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/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  type Node interface {
     8  	Pos() Pos
     9  	SetPos(Pos)
    10  	aNode()
    11  }
    12  
    13  // package PkgName; DeclList[0], DeclList[1], ...
    14  type File struct {
    15  	Pragma    Pragma
    16  	PkgName   *Name
    17  	DeclList  []Decl
    18  	EOF       Pos
    19  	GoVersion string
    20  	node
    21  }
    22  
    23  type (
    24  	Decl interface {
    25  		Node
    26  		aDecl()
    27  	}
    28  
    29  	//              Path
    30  	// LocalPkgName Path
    31  	ImportDecl struct {
    32  		Group        *Group
    33  		Pragma       Pragma
    34  		LocalPkgName *Name
    35  		Path         *BasicLit
    36  		decl
    37  	}
    38  
    39  	// NameList
    40  	// NameList      = Values
    41  	// NameList Type = Values
    42  	ConstDecl struct {
    43  		Group    *Group
    44  		Pragma   Pragma
    45  		NameList []*Name
    46  		Type     Expr
    47  		Values   Expr
    48  		decl
    49  	}
    50  
    51  	// Name Type
    52  	TypeDecl struct {
    53  		Group      *Group
    54  		Pragma     Pragma
    55  		Name       *Name
    56  		TParamList []*Field
    57  		Alias      bool
    58  		Type       Expr
    59  		decl
    60  	}
    61  
    62  	// NameList Type
    63  	// NameList Type = Values
    64  	// NameList      = Values
    65  	VarDecl struct {
    66  		Group    *Group
    67  		Pragma   Pragma
    68  		NameList []*Name
    69  		Type     Expr
    70  		Values   Expr
    71  		decl
    72  	}
    73  
    74  	// func          Name Type { Body }
    75  	// func          Name Type
    76  	// func Receiver Name Type { Body }
    77  	// func Receiver Name Type
    78  	FuncDecl struct {
    79  		Pragma     Pragma
    80  		Recv       *Field
    81  		Name       *Name
    82  		TParamList []*Field
    83  		Type       *FuncType
    84  		Body       *BlockStmt
    85  		decl
    86  	}
    87  )
    88  
    89  // All declarations belonging to the same group point to the same Group node.
    90  type Group struct {
    91  	_ int
    92  }
    93  
    94  func NewName(pos Pos, value string) *Name
    95  
    96  type (
    97  	Expr interface {
    98  		Node
    99  		typeInfo
   100  		aExpr()
   101  	}
   102  
   103  	// Placeholder for an expression that failed to parse
   104  	// correctly and where we can't provide a better node.
   105  	BadExpr struct {
   106  		expr
   107  	}
   108  
   109  	// Value
   110  	Name struct {
   111  		Value string
   112  		expr
   113  	}
   114  
   115  	// Value
   116  	BasicLit struct {
   117  		Value string
   118  		Kind  LitKind
   119  		Bad   bool
   120  		expr
   121  	}
   122  
   123  	// Type { ElemList[0], ElemList[1], ... }
   124  	CompositeLit struct {
   125  		Type     Expr
   126  		ElemList []Expr
   127  		NKeys    int
   128  		Rbrace   Pos
   129  		expr
   130  	}
   131  
   132  	// Key: Value
   133  	KeyValueExpr struct {
   134  		Key, Value Expr
   135  		expr
   136  	}
   137  
   138  	// func Type { Body }
   139  	FuncLit struct {
   140  		Type *FuncType
   141  		Body *BlockStmt
   142  		expr
   143  	}
   144  
   145  	// (X)
   146  	ParenExpr struct {
   147  		X Expr
   148  		expr
   149  	}
   150  
   151  	// X.Sel
   152  	SelectorExpr struct {
   153  		X   Expr
   154  		Sel *Name
   155  		expr
   156  	}
   157  
   158  	// X[Index]
   159  	// X[T1, T2, ...] (with Ti = Index.(*ListExpr).ElemList[i])
   160  	IndexExpr struct {
   161  		X     Expr
   162  		Index Expr
   163  		expr
   164  	}
   165  
   166  	// X[Index[0] : Index[1] : Index[2]]
   167  	SliceExpr struct {
   168  		X     Expr
   169  		Index [3]Expr
   170  		// Full indicates whether this is a simple or full slice expression.
   171  		// In a valid AST, this is equivalent to Index[2] != nil.
   172  		// TODO(mdempsky): This is only needed to report the "3-index
   173  		// slice of string" error when Index[2] is missing.
   174  		Full bool
   175  		expr
   176  	}
   177  
   178  	// X.(Type)
   179  	AssertExpr struct {
   180  		X    Expr
   181  		Type Expr
   182  		expr
   183  	}
   184  
   185  	// X.(type)
   186  	// Lhs := X.(type)
   187  	TypeSwitchGuard struct {
   188  		Lhs *Name
   189  		X   Expr
   190  		expr
   191  	}
   192  
   193  	Operation struct {
   194  		Op   Operator
   195  		X, Y Expr
   196  		expr
   197  	}
   198  
   199  	// Fun(ArgList[0], ArgList[1], ...)
   200  	CallExpr struct {
   201  		Fun     Expr
   202  		ArgList []Expr
   203  		HasDots bool
   204  		expr
   205  	}
   206  
   207  	// ElemList[0], ElemList[1], ...
   208  	ListExpr struct {
   209  		ElemList []Expr
   210  		expr
   211  	}
   212  
   213  	// [Len]Elem
   214  	ArrayType struct {
   215  		// TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments)
   216  		Len  Expr
   217  		Elem Expr
   218  		expr
   219  	}
   220  
   221  	// []Elem
   222  	SliceType struct {
   223  		Elem Expr
   224  		expr
   225  	}
   226  
   227  	// ...Elem
   228  	DotsType struct {
   229  		Elem Expr
   230  		expr
   231  	}
   232  
   233  	// struct { FieldList[0] TagList[0]; FieldList[1] TagList[1]; ... }
   234  	StructType struct {
   235  		FieldList []*Field
   236  		TagList   []*BasicLit
   237  		expr
   238  	}
   239  
   240  	// Name Type
   241  	//      Type
   242  	Field struct {
   243  		Name *Name
   244  		Type Expr
   245  		node
   246  	}
   247  
   248  	// interface { MethodList[0]; MethodList[1]; ... }
   249  	InterfaceType struct {
   250  		MethodList []*Field
   251  		expr
   252  	}
   253  
   254  	FuncType struct {
   255  		ParamList  []*Field
   256  		ResultList []*Field
   257  		expr
   258  	}
   259  
   260  	// map[Key]Value
   261  	MapType struct {
   262  		Key, Value Expr
   263  		expr
   264  	}
   265  
   266  	//   chan Elem
   267  	// <-chan Elem
   268  	// chan<- Elem
   269  	ChanType struct {
   270  		Dir  ChanDir
   271  		Elem Expr
   272  		expr
   273  	}
   274  )
   275  
   276  type ChanDir uint
   277  
   278  const (
   279  	_ ChanDir = iota
   280  	SendOnly
   281  	RecvOnly
   282  )
   283  
   284  type (
   285  	Stmt interface {
   286  		Node
   287  		aStmt()
   288  	}
   289  
   290  	SimpleStmt interface {
   291  		Stmt
   292  		aSimpleStmt()
   293  	}
   294  
   295  	EmptyStmt struct {
   296  		simpleStmt
   297  	}
   298  
   299  	LabeledStmt struct {
   300  		Label *Name
   301  		Stmt  Stmt
   302  		stmt
   303  	}
   304  
   305  	BlockStmt struct {
   306  		List   []Stmt
   307  		Rbrace Pos
   308  		stmt
   309  	}
   310  
   311  	ExprStmt struct {
   312  		X Expr
   313  		simpleStmt
   314  	}
   315  
   316  	SendStmt struct {
   317  		Chan, Value Expr
   318  		simpleStmt
   319  	}
   320  
   321  	DeclStmt struct {
   322  		DeclList []Decl
   323  		stmt
   324  	}
   325  
   326  	AssignStmt struct {
   327  		Op       Operator
   328  		Lhs, Rhs Expr
   329  		simpleStmt
   330  	}
   331  
   332  	BranchStmt struct {
   333  		Tok   token
   334  		Label *Name
   335  		// Target is the continuation of the control flow after executing
   336  		// the branch; it is computed by the parser if CheckBranches is set.
   337  		// Target is a *LabeledStmt for gotos, and a *SwitchStmt, *SelectStmt,
   338  		// or *ForStmt for breaks and continues, depending on the context of
   339  		// the branch. Target is not set for fallthroughs.
   340  		Target Stmt
   341  		stmt
   342  	}
   343  
   344  	CallStmt struct {
   345  		Tok     token
   346  		Call    Expr
   347  		DeferAt Expr
   348  		stmt
   349  	}
   350  
   351  	ReturnStmt struct {
   352  		Results Expr
   353  		stmt
   354  	}
   355  
   356  	IfStmt struct {
   357  		Init SimpleStmt
   358  		Cond Expr
   359  		Then *BlockStmt
   360  		Else Stmt
   361  		stmt
   362  	}
   363  
   364  	ForStmt struct {
   365  		Init SimpleStmt
   366  		Cond Expr
   367  		Post SimpleStmt
   368  		Body *BlockStmt
   369  		stmt
   370  	}
   371  
   372  	SwitchStmt struct {
   373  		Init   SimpleStmt
   374  		Tag    Expr
   375  		Body   []*CaseClause
   376  		Rbrace Pos
   377  		stmt
   378  	}
   379  
   380  	SelectStmt struct {
   381  		Body   []*CommClause
   382  		Rbrace Pos
   383  		stmt
   384  	}
   385  )
   386  
   387  type (
   388  	RangeClause struct {
   389  		Lhs Expr
   390  		Def bool
   391  		X   Expr
   392  		simpleStmt
   393  	}
   394  
   395  	CaseClause struct {
   396  		Cases Expr
   397  		Body  []Stmt
   398  		Colon Pos
   399  		node
   400  	}
   401  
   402  	CommClause struct {
   403  		Comm  SimpleStmt
   404  		Body  []Stmt
   405  		Colon Pos
   406  		node
   407  	}
   408  )
   409  
   410  // TODO(gri) Consider renaming to CommentPos, CommentPlacement, etc.
   411  // Kind = Above doesn't make much sense.
   412  type CommentKind uint
   413  
   414  const (
   415  	Above CommentKind = iota
   416  	Below
   417  	Left
   418  	Right
   419  )
   420  
   421  type Comment struct {
   422  	Kind CommentKind
   423  	Text string
   424  	Next *Comment
   425  }