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