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