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