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