github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/cmd/compile/internal/gc/syntax.go (about) 1 // Copyright 2009 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 // “Abstract” syntax representation. 6 7 package gc 8 9 import ( 10 "cmd/compile/internal/ssa" 11 "cmd/compile/internal/syntax" 12 "cmd/compile/internal/types" 13 "cmd/internal/obj" 14 "cmd/internal/src" 15 ) 16 17 // A Node is a single node in the syntax tree. 18 // Actually the syntax tree is a syntax DAG, because there is only one 19 // node with Op=ONAME for a given instance of a variable x. 20 // The same is true for Op=OTYPE and Op=OLITERAL. See Node.mayBeShared. 21 type Node struct { 22 // Tree structure. 23 // Generic recursive walks should follow these fields. 24 Left *Node 25 Right *Node 26 Ninit Nodes 27 Nbody Nodes 28 List Nodes 29 Rlist Nodes 30 31 // most nodes 32 Type *types.Type 33 Orig *Node // original form, for printing, and tracking copies of ONAMEs 34 35 // func 36 Func *Func 37 38 // ONAME, OTYPE, OPACK, OLABEL, some OLITERAL 39 Name *Name 40 41 Sym *types.Sym // various 42 E interface{} // Opt or Val, see methods below 43 44 // Various. Usually an offset into a struct. For example: 45 // - ONAME nodes that refer to local variables use it to identify their stack frame position. 46 // - ODOT, ODOTPTR, and OINDREGSP use it to indicate offset relative to their base address. 47 // - OSTRUCTKEY uses it to store the named field's offset. 48 // - Named OLITERALs use it to to store their ambient iota value. 49 // Possibly still more uses. If you find any, document them. 50 Xoffset int64 51 52 Pos src.XPos 53 54 flags bitset32 55 56 Esc uint16 // EscXXX 57 58 Op Op 59 Etype types.EType // op for OASOP, etype for OTYPE, exclam for export, 6g saved reg, ChanDir for OTCHAN, for OINDEXMAP 1=LHS,0=RHS 60 } 61 62 // IsAutoTmp indicates if n was created by the compiler as a temporary, 63 // based on the setting of the .AutoTemp flag in n's Name. 64 func (n *Node) IsAutoTmp() bool { 65 if n == nil || n.Op != ONAME { 66 return false 67 } 68 return n.Name.AutoTemp() 69 } 70 71 const ( 72 nodeClass, _ = iota, 1 << iota // PPARAM, PAUTO, PEXTERN, etc; three bits; first in the list because frequently accessed 73 _, _ // second nodeClass bit 74 _, _ // third nodeClass bit 75 nodeWalkdef, _ // tracks state during typecheckdef; 2 == loop detected; two bits 76 _, _ // second nodeWalkdef bit 77 nodeTypecheck, _ // tracks state during typechecking; 2 == loop detected; two bits 78 _, _ // second nodeTypecheck bit 79 nodeInitorder, _ // tracks state during init1; two bits 80 _, _ // second nodeInitorder bit 81 _, nodeHasBreak 82 _, nodeIsClosureVar 83 _, nodeIsOutputParamHeapAddr 84 _, nodeNoInline // used internally by inliner to indicate that a function call should not be inlined; set for OCALLFUNC and OCALLMETH only 85 _, nodeAssigned // is the variable ever assigned to 86 _, nodeAddrtaken // address taken, even if not moved to heap 87 _, nodeImplicit 88 _, nodeIsddd // is the argument variadic 89 _, nodeDiag // already printed error about this 90 _, nodeColas // OAS resulting from := 91 _, nodeNonNil // guaranteed to be non-nil 92 _, nodeNoescape // func arguments do not escape; TODO(rsc): move Noescape to Func struct (see CL 7360) 93 _, nodeBounded // bounds check unnecessary 94 _, nodeAddable // addressable 95 _, nodeHasCall // expression contains a function call 96 _, nodeLikely // if statement condition likely 97 _, nodeHasVal // node.E contains a Val 98 _, nodeHasOpt // node.E contains an Opt 99 _, nodeEmbedded // ODCLFIELD embedded type 100 ) 101 102 func (n *Node) Class() Class { return Class(n.flags.get3(nodeClass)) } 103 func (n *Node) Walkdef() uint8 { return n.flags.get2(nodeWalkdef) } 104 func (n *Node) Typecheck() uint8 { return n.flags.get2(nodeTypecheck) } 105 func (n *Node) Initorder() uint8 { return n.flags.get2(nodeInitorder) } 106 107 func (n *Node) HasBreak() bool { return n.flags&nodeHasBreak != 0 } 108 func (n *Node) IsClosureVar() bool { return n.flags&nodeIsClosureVar != 0 } 109 func (n *Node) NoInline() bool { return n.flags&nodeNoInline != 0 } 110 func (n *Node) IsOutputParamHeapAddr() bool { return n.flags&nodeIsOutputParamHeapAddr != 0 } 111 func (n *Node) Assigned() bool { return n.flags&nodeAssigned != 0 } 112 func (n *Node) Addrtaken() bool { return n.flags&nodeAddrtaken != 0 } 113 func (n *Node) Implicit() bool { return n.flags&nodeImplicit != 0 } 114 func (n *Node) Isddd() bool { return n.flags&nodeIsddd != 0 } 115 func (n *Node) Diag() bool { return n.flags&nodeDiag != 0 } 116 func (n *Node) Colas() bool { return n.flags&nodeColas != 0 } 117 func (n *Node) NonNil() bool { return n.flags&nodeNonNil != 0 } 118 func (n *Node) Noescape() bool { return n.flags&nodeNoescape != 0 } 119 func (n *Node) Bounded() bool { return n.flags&nodeBounded != 0 } 120 func (n *Node) Addable() bool { return n.flags&nodeAddable != 0 } 121 func (n *Node) HasCall() bool { return n.flags&nodeHasCall != 0 } 122 func (n *Node) Likely() bool { return n.flags&nodeLikely != 0 } 123 func (n *Node) HasVal() bool { return n.flags&nodeHasVal != 0 } 124 func (n *Node) HasOpt() bool { return n.flags&nodeHasOpt != 0 } 125 func (n *Node) Embedded() bool { return n.flags&nodeEmbedded != 0 } 126 127 func (n *Node) SetClass(b Class) { n.flags.set3(nodeClass, uint8(b)) } 128 func (n *Node) SetWalkdef(b uint8) { n.flags.set2(nodeWalkdef, b) } 129 func (n *Node) SetTypecheck(b uint8) { n.flags.set2(nodeTypecheck, b) } 130 func (n *Node) SetInitorder(b uint8) { n.flags.set2(nodeInitorder, b) } 131 132 func (n *Node) SetHasBreak(b bool) { n.flags.set(nodeHasBreak, b) } 133 func (n *Node) SetIsClosureVar(b bool) { n.flags.set(nodeIsClosureVar, b) } 134 func (n *Node) SetNoInline(b bool) { n.flags.set(nodeNoInline, b) } 135 func (n *Node) SetIsOutputParamHeapAddr(b bool) { n.flags.set(nodeIsOutputParamHeapAddr, b) } 136 func (n *Node) SetAssigned(b bool) { n.flags.set(nodeAssigned, b) } 137 func (n *Node) SetAddrtaken(b bool) { n.flags.set(nodeAddrtaken, b) } 138 func (n *Node) SetImplicit(b bool) { n.flags.set(nodeImplicit, b) } 139 func (n *Node) SetIsddd(b bool) { n.flags.set(nodeIsddd, b) } 140 func (n *Node) SetDiag(b bool) { n.flags.set(nodeDiag, b) } 141 func (n *Node) SetColas(b bool) { n.flags.set(nodeColas, b) } 142 func (n *Node) SetNonNil(b bool) { n.flags.set(nodeNonNil, b) } 143 func (n *Node) SetNoescape(b bool) { n.flags.set(nodeNoescape, b) } 144 func (n *Node) SetBounded(b bool) { n.flags.set(nodeBounded, b) } 145 func (n *Node) SetAddable(b bool) { n.flags.set(nodeAddable, b) } 146 func (n *Node) SetHasCall(b bool) { n.flags.set(nodeHasCall, b) } 147 func (n *Node) SetLikely(b bool) { n.flags.set(nodeLikely, b) } 148 func (n *Node) SetHasVal(b bool) { n.flags.set(nodeHasVal, b) } 149 func (n *Node) SetHasOpt(b bool) { n.flags.set(nodeHasOpt, b) } 150 func (n *Node) SetEmbedded(b bool) { n.flags.set(nodeEmbedded, b) } 151 152 // Val returns the Val for the node. 153 func (n *Node) Val() Val { 154 if !n.HasVal() { 155 return Val{} 156 } 157 return Val{n.E} 158 } 159 160 // SetVal sets the Val for the node, which must not have been used with SetOpt. 161 func (n *Node) SetVal(v Val) { 162 if n.HasOpt() { 163 Debug['h'] = 1 164 Dump("have Opt", n) 165 Fatalf("have Opt") 166 } 167 n.SetHasVal(true) 168 n.E = v.U 169 } 170 171 // Opt returns the optimizer data for the node. 172 func (n *Node) Opt() interface{} { 173 if !n.HasOpt() { 174 return nil 175 } 176 return n.E 177 } 178 179 // SetOpt sets the optimizer data for the node, which must not have been used with SetVal. 180 // SetOpt(nil) is ignored for Vals to simplify call sites that are clearing Opts. 181 func (n *Node) SetOpt(x interface{}) { 182 if x == nil && n.HasVal() { 183 return 184 } 185 if n.HasVal() { 186 Debug['h'] = 1 187 Dump("have Val", n) 188 Fatalf("have Val") 189 } 190 n.SetHasOpt(true) 191 n.E = x 192 } 193 194 func (n *Node) Iota() int64 { 195 return n.Xoffset 196 } 197 198 func (n *Node) SetIota(x int64) { 199 n.Xoffset = x 200 } 201 202 // mayBeShared reports whether n may occur in multiple places in the AST. 203 // Extra care must be taken when mutating such a node. 204 func (n *Node) mayBeShared() bool { 205 switch n.Op { 206 case ONAME, OLITERAL, OTYPE: 207 return true 208 } 209 return false 210 } 211 212 // isMethodExpression reports whether n represents a method expression T.M. 213 func (n *Node) isMethodExpression() bool { 214 return n.Op == ONAME && n.Left != nil && n.Left.Op == OTYPE && n.Right != nil && n.Right.Op == ONAME 215 } 216 217 // funcname returns the name of the function n. 218 func (n *Node) funcname() string { 219 if n == nil || n.Func == nil || n.Func.Nname == nil { 220 return "<nil>" 221 } 222 return n.Func.Nname.Sym.Name 223 } 224 225 // Name holds Node fields used only by named nodes (ONAME, OTYPE, OPACK, OLABEL, some OLITERAL). 226 type Name struct { 227 Pack *Node // real package for import . names 228 Pkg *types.Pkg // pkg for OPACK nodes 229 Defn *Node // initializing assignment 230 Curfn *Node // function for local variables 231 Param *Param // additional fields for ONAME, OTYPE 232 Decldepth int32 // declaration loop depth, increased for every loop or label 233 Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one. 234 Funcdepth int32 235 236 used bool // for variable declared and not used error 237 flags bitset8 238 } 239 240 const ( 241 nameCaptured = 1 << iota // is the variable captured by a closure 242 nameReadonly 243 nameByval // is the variable captured by value or by reference 244 nameNeedzero // if it contains pointers, needs to be zeroed on function entry 245 nameKeepalive // mark value live across unknown assembly call 246 nameAutoTemp // is the variable a temporary (implies no dwarf info. reset if escapes to heap) 247 ) 248 249 func (n *Name) Captured() bool { return n.flags&nameCaptured != 0 } 250 func (n *Name) Readonly() bool { return n.flags&nameReadonly != 0 } 251 func (n *Name) Byval() bool { return n.flags&nameByval != 0 } 252 func (n *Name) Needzero() bool { return n.flags&nameNeedzero != 0 } 253 func (n *Name) Keepalive() bool { return n.flags&nameKeepalive != 0 } 254 func (n *Name) AutoTemp() bool { return n.flags&nameAutoTemp != 0 } 255 func (n *Name) Used() bool { return n.used } 256 257 func (n *Name) SetCaptured(b bool) { n.flags.set(nameCaptured, b) } 258 func (n *Name) SetReadonly(b bool) { n.flags.set(nameReadonly, b) } 259 func (n *Name) SetByval(b bool) { n.flags.set(nameByval, b) } 260 func (n *Name) SetNeedzero(b bool) { n.flags.set(nameNeedzero, b) } 261 func (n *Name) SetKeepalive(b bool) { n.flags.set(nameKeepalive, b) } 262 func (n *Name) SetAutoTemp(b bool) { n.flags.set(nameAutoTemp, b) } 263 func (n *Name) SetUsed(b bool) { n.used = b } 264 265 type Param struct { 266 Ntype *Node 267 Heapaddr *Node // temp holding heap address of param 268 269 // ONAME PAUTOHEAP 270 Stackcopy *Node // the PPARAM/PPARAMOUT on-stack slot (moved func params only) 271 272 // ONAME PPARAM 273 Field *types.Field // TFIELD in arg struct 274 275 // ONAME closure linkage 276 // Consider: 277 // 278 // func f() { 279 // x := 1 // x1 280 // func() { 281 // use(x) // x2 282 // func() { 283 // use(x) // x3 284 // --- parser is here --- 285 // }() 286 // }() 287 // } 288 // 289 // There is an original declaration of x and then a chain of mentions of x 290 // leading into the current function. Each time x is mentioned in a new closure, 291 // we create a variable representing x for use in that specific closure, 292 // since the way you get to x is different in each closure. 293 // 294 // Let's number the specific variables as shown in the code: 295 // x1 is the original x, x2 is when mentioned in the closure, 296 // and x3 is when mentioned in the closure in the closure. 297 // 298 // We keep these linked (assume N > 1): 299 // 300 // - x1.Defn = original declaration statement for x (like most variables) 301 // - x1.Innermost = current innermost closure x (in this case x3), or nil for none 302 // - x1.IsClosureVar() = false 303 // 304 // - xN.Defn = x1, N > 1 305 // - xN.IsClosureVar() = true, N > 1 306 // - x2.Outer = nil 307 // - xN.Outer = x(N-1), N > 2 308 // 309 // 310 // When we look up x in the symbol table, we always get x1. 311 // Then we can use x1.Innermost (if not nil) to get the x 312 // for the innermost known closure function, 313 // but the first reference in a closure will find either no x1.Innermost 314 // or an x1.Innermost with .Funcdepth < Funcdepth. 315 // In that case, a new xN must be created, linked in with: 316 // 317 // xN.Defn = x1 318 // xN.Outer = x1.Innermost 319 // x1.Innermost = xN 320 // 321 // When we finish the function, we'll process its closure variables 322 // and find xN and pop it off the list using: 323 // 324 // x1 := xN.Defn 325 // x1.Innermost = xN.Outer 326 // 327 // We leave xN.Innermost set so that we can still get to the original 328 // variable quickly. Not shown here, but once we're 329 // done parsing a function and no longer need xN.Outer for the 330 // lexical x reference links as described above, closurebody 331 // recomputes xN.Outer as the semantic x reference link tree, 332 // even filling in x in intermediate closures that might not 333 // have mentioned it along the way to inner closures that did. 334 // See closurebody for details. 335 // 336 // During the eventual compilation, then, for closure variables we have: 337 // 338 // xN.Defn = original variable 339 // xN.Outer = variable captured in next outward scope 340 // to make closure where xN appears 341 // 342 // Because of the sharding of pieces of the node, x.Defn means x.Name.Defn 343 // and x.Innermost/Outer means x.Name.Param.Innermost/Outer. 344 Innermost *Node 345 Outer *Node 346 347 // OTYPE 348 // 349 // TODO: Should Func pragmas also be stored on the Name? 350 Pragma syntax.Pragma 351 Alias bool // node is alias for Ntype (only used when type-checking ODCLTYPE) 352 } 353 354 // Functions 355 // 356 // A simple function declaration is represented as an ODCLFUNC node f 357 // and an ONAME node n. They're linked to one another through 358 // f.Func.Nname == n and n.Name.Defn == f. When functions are 359 // referenced by name in an expression, the function's ONAME node is 360 // used directly. 361 // 362 // Function names have n.Class() == PFUNC. This distinguishes them 363 // from variables of function type. 364 // 365 // Confusingly, n.Func and f.Func both exist, but commonly point to 366 // different Funcs. (Exception: an OCALLPART's Func does point to its 367 // ODCLFUNC's Func.) 368 // 369 // A method declaration is represented like functions, except n.Sym 370 // will be the qualified method name (e.g., "T.m") and 371 // f.Func.Shortname is the bare method name (e.g., "m"). 372 // 373 // Method expressions are represented as ONAME/PFUNC nodes like 374 // function names, but their Left and Right fields still point to the 375 // type and method, respectively. They can be distinguished from 376 // normal functions with isMethodExpression. Also, unlike function 377 // name nodes, method expression nodes exist for each method 378 // expression. The declaration ONAME can be accessed with 379 // x.Type.Nname(), where x is the method expression ONAME node. 380 // 381 // Method values are represented by ODOTMETH/ODOTINTER when called 382 // immediately, and OCALLPART otherwise. They are like method 383 // expressions, except that for ODOTMETH/ODOTINTER the method name is 384 // stored in Sym instead of Right. 385 // 386 // Closures are represented by OCLOSURE node c. They link back and 387 // forth with the ODCLFUNC via Func.Closure; that is, c.Func.Closure 388 // == f and f.Func.Closure == c. 389 // 390 // Function bodies are stored in f.Nbody, and inline function bodies 391 // are stored in n.Func.Inl. Pragmas are stored in f.Func.Pragma. 392 // 393 // Imported functions skip the ODCLFUNC, so n.Name.Defn is nil. They 394 // also use Dcl instead of Inldcl. 395 396 // Func holds Node fields used only with function-like nodes. 397 type Func struct { 398 Shortname *types.Sym 399 Enter Nodes // for example, allocate and initialize memory for escaping parameters 400 Exit Nodes 401 Cvars Nodes // closure params 402 Dcl []*Node // autodcl for this func/closure 403 Inldcl Nodes // copy of dcl for use in inlining 404 405 // Parents records the parent scope of each scope within a 406 // function. The root scope (0) has no parent, so the i'th 407 // scope's parent is stored at Parents[i-1]. 408 Parents []ScopeID 409 410 // Marks records scope boundary changes. 411 Marks []Mark 412 413 Closgen int 414 Outerfunc *Node // outer function (for closure) 415 FieldTrack map[*types.Sym]struct{} 416 DebugInfo *ssa.FuncDebug 417 Ntype *Node // signature 418 Top int // top context (Ecall, Eproc, etc) 419 Closure *Node // OCLOSURE <-> ODCLFUNC 420 Nname *Node 421 lsym *obj.LSym 422 423 Inl Nodes // copy of the body for use in inlining 424 InlCost int32 425 Depth int32 426 427 Label int32 // largest auto-generated label in this function 428 429 Endlineno src.XPos 430 WBPos src.XPos // position of first write barrier; see SetWBPos 431 432 Pragma syntax.Pragma // go:xxx function annotations 433 434 flags bitset16 435 436 // nwbrCalls records the LSyms of functions called by this 437 // function for go:nowritebarrierrec analysis. Only filled in 438 // if nowritebarrierrecCheck != nil. 439 nwbrCalls *[]nowritebarrierrecCallSym 440 } 441 442 // A Mark represents a scope boundary. 443 type Mark struct { 444 // Pos is the position of the token that marks the scope 445 // change. 446 Pos src.XPos 447 448 // Scope identifies the innermost scope to the right of Pos. 449 Scope ScopeID 450 } 451 452 // A ScopeID represents a lexical scope within a function. 453 type ScopeID int32 454 455 const ( 456 funcDupok = 1 << iota // duplicate definitions ok 457 funcWrapper // is method wrapper 458 funcNeedctxt // function uses context register (has closure variables) 459 funcReflectMethod // function calls reflect.Type.Method or MethodByName 460 funcIsHiddenClosure 461 funcNoFramePointer // Must not use a frame pointer for this function 462 funcHasDefer // contains a defer statement 463 funcNilCheckDisabled // disable nil checks when compiling this function 464 funcInlinabilityChecked // inliner has already determined whether the function is inlinable 465 funcExportInline // include inline body in export data 466 ) 467 468 func (f *Func) Dupok() bool { return f.flags&funcDupok != 0 } 469 func (f *Func) Wrapper() bool { return f.flags&funcWrapper != 0 } 470 func (f *Func) Needctxt() bool { return f.flags&funcNeedctxt != 0 } 471 func (f *Func) ReflectMethod() bool { return f.flags&funcReflectMethod != 0 } 472 func (f *Func) IsHiddenClosure() bool { return f.flags&funcIsHiddenClosure != 0 } 473 func (f *Func) NoFramePointer() bool { return f.flags&funcNoFramePointer != 0 } 474 func (f *Func) HasDefer() bool { return f.flags&funcHasDefer != 0 } 475 func (f *Func) NilCheckDisabled() bool { return f.flags&funcNilCheckDisabled != 0 } 476 func (f *Func) InlinabilityChecked() bool { return f.flags&funcInlinabilityChecked != 0 } 477 func (f *Func) ExportInline() bool { return f.flags&funcExportInline != 0 } 478 479 func (f *Func) SetDupok(b bool) { f.flags.set(funcDupok, b) } 480 func (f *Func) SetWrapper(b bool) { f.flags.set(funcWrapper, b) } 481 func (f *Func) SetNeedctxt(b bool) { f.flags.set(funcNeedctxt, b) } 482 func (f *Func) SetReflectMethod(b bool) { f.flags.set(funcReflectMethod, b) } 483 func (f *Func) SetIsHiddenClosure(b bool) { f.flags.set(funcIsHiddenClosure, b) } 484 func (f *Func) SetNoFramePointer(b bool) { f.flags.set(funcNoFramePointer, b) } 485 func (f *Func) SetHasDefer(b bool) { f.flags.set(funcHasDefer, b) } 486 func (f *Func) SetNilCheckDisabled(b bool) { f.flags.set(funcNilCheckDisabled, b) } 487 func (f *Func) SetInlinabilityChecked(b bool) { f.flags.set(funcInlinabilityChecked, b) } 488 func (f *Func) SetExportInline(b bool) { f.flags.set(funcExportInline, b) } 489 490 func (f *Func) setWBPos(pos src.XPos) { 491 if Debug_wb != 0 { 492 Warnl(pos, "write barrier") 493 } 494 if !f.WBPos.IsKnown() { 495 f.WBPos = pos 496 } 497 } 498 499 //go:generate stringer -type=Op -trimprefix=O 500 501 type Op uint8 502 503 // Node ops. 504 const ( 505 OXXX Op = iota 506 507 // names 508 ONAME // var, const or func name 509 ONONAME // unnamed arg or return value: f(int, string) (int, error) { etc } 510 OTYPE // type name 511 OPACK // import 512 OLITERAL // literal 513 514 // expressions 515 OADD // Left + Right 516 OSUB // Left - Right 517 OOR // Left | Right 518 OXOR // Left ^ Right 519 OADDSTR // +{List} (string addition, list elements are strings) 520 OADDR // &Left 521 OANDAND // Left && Right 522 OAPPEND // append(List); after walk, Left may contain elem type descriptor 523 OARRAYBYTESTR // Type(Left) (Type is string, Left is a []byte) 524 OARRAYBYTESTRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral) 525 OARRAYRUNESTR // Type(Left) (Type is string, Left is a []rune) 526 OSTRARRAYBYTE // Type(Left) (Type is []byte, Left is a string) 527 OSTRARRAYBYTETMP // Type(Left) (Type is []byte, Left is a string, ephemeral) 528 OSTRARRAYRUNE // Type(Left) (Type is []rune, Left is a string) 529 OAS // Left = Right or (if Colas=true) Left := Right 530 OAS2 // List = Rlist (x, y, z = a, b, c) 531 OAS2FUNC // List = Rlist (x, y = f()) 532 OAS2RECV // List = Rlist (x, ok = <-c) 533 OAS2MAPR // List = Rlist (x, ok = m["foo"]) 534 OAS2DOTTYPE // List = Rlist (x, ok = I.(int)) 535 OASOP // Left Etype= Right (x += y) 536 OCALL // Left(List) (function call, method call or type conversion) 537 OCALLFUNC // Left(List) (function call f(args)) 538 OCALLMETH // Left(List) (direct method call x.Method(args)) 539 OCALLINTER // Left(List) (interface method call x.Method(args)) 540 OCALLPART // Left.Right (method expression x.Method, not called) 541 OCAP // cap(Left) 542 OCLOSE // close(Left) 543 OCLOSURE // func Type { Body } (func literal) 544 OCMPIFACE // Left Etype Right (interface comparison, x == y or x != y) 545 OCMPSTR // Left Etype Right (string comparison, x == y, x < y, etc) 546 OCOMPLIT // Right{List} (composite literal, not yet lowered to specific form) 547 OMAPLIT // Type{List} (composite literal, Type is map) 548 OSTRUCTLIT // Type{List} (composite literal, Type is struct) 549 OARRAYLIT // Type{List} (composite literal, Type is array) 550 OSLICELIT // Type{List} (composite literal, Type is slice) 551 OPTRLIT // &Left (left is composite literal) 552 OCONV // Type(Left) (type conversion) 553 OCONVIFACE // Type(Left) (type conversion, to interface) 554 OCONVNOP // Type(Left) (type conversion, no effect) 555 OCOPY // copy(Left, Right) 556 ODCL // var Left (declares Left of type Left.Type) 557 558 // Used during parsing but don't last. 559 ODCLFUNC // func f() or func (r) f() 560 ODCLFIELD // struct field, interface field, or func/method argument/return value. 561 ODCLCONST // const pi = 3.14 562 ODCLTYPE // type Int int or type Int = int 563 564 ODELETE // delete(Left, Right) 565 ODOT // Left.Sym (Left is of struct type) 566 ODOTPTR // Left.Sym (Left is of pointer to struct type) 567 ODOTMETH // Left.Sym (Left is non-interface, Right is method name) 568 ODOTINTER // Left.Sym (Left is interface, Right is method name) 569 OXDOT // Left.Sym (before rewrite to one of the preceding) 570 ODOTTYPE // Left.Right or Left.Type (.Right during parsing, .Type once resolved); after walk, .Right contains address of interface type descriptor and .Right.Right contains address of concrete type descriptor 571 ODOTTYPE2 // Left.Right or Left.Type (.Right during parsing, .Type once resolved; on rhs of OAS2DOTTYPE); after walk, .Right contains address of interface type descriptor 572 OEQ // Left == Right 573 ONE // Left != Right 574 OLT // Left < Right 575 OLE // Left <= Right 576 OGE // Left >= Right 577 OGT // Left > Right 578 OIND // *Left 579 OINDEX // Left[Right] (index of array or slice) 580 OINDEXMAP // Left[Right] (index of map) 581 OKEY // Left:Right (key:value in struct/array/map literal) 582 OSTRUCTKEY // Sym:Left (key:value in struct literal, after type checking) 583 OLEN // len(Left) 584 OMAKE // make(List) (before type checking converts to one of the following) 585 OMAKECHAN // make(Type, Left) (type is chan) 586 OMAKEMAP // make(Type, Left) (type is map) 587 OMAKESLICE // make(Type, Left, Right) (type is slice) 588 OMUL // Left * Right 589 ODIV // Left / Right 590 OMOD // Left % Right 591 OLSH // Left << Right 592 ORSH // Left >> Right 593 OAND // Left & Right 594 OANDNOT // Left &^ Right 595 ONEW // new(Left) 596 ONOT // !Left 597 OCOM // ^Left 598 OPLUS // +Left 599 OMINUS // -Left 600 OOROR // Left || Right 601 OPANIC // panic(Left) 602 OPRINT // print(List) 603 OPRINTN // println(List) 604 OPAREN // (Left) 605 OSEND // Left <- Right 606 OSLICE // Left[List[0] : List[1]] (Left is untypechecked or slice) 607 OSLICEARR // Left[List[0] : List[1]] (Left is array) 608 OSLICESTR // Left[List[0] : List[1]] (Left is string) 609 OSLICE3 // Left[List[0] : List[1] : List[2]] (Left is untypedchecked or slice) 610 OSLICE3ARR // Left[List[0] : List[1] : List[2]] (Left is array) 611 ORECOVER // recover() 612 ORECV // <-Left 613 ORUNESTR // Type(Left) (Type is string, Left is rune) 614 OSELRECV // Left = <-Right.Left: (appears as .Left of OCASE; Right.Op == ORECV) 615 OSELRECV2 // List = <-Right.Left: (apperas as .Left of OCASE; count(List) == 2, Right.Op == ORECV) 616 OIOTA // iota 617 OREAL // real(Left) 618 OIMAG // imag(Left) 619 OCOMPLEX // complex(Left, Right) 620 OALIGNOF // unsafe.Alignof(Left) 621 OOFFSETOF // unsafe.Offsetof(Left) 622 OSIZEOF // unsafe.Sizeof(Left) 623 624 // statements 625 OBLOCK // { List } (block of code) 626 OBREAK // break 627 OCASE // case Left or List[0]..List[1]: Nbody (select case after processing; Left==nil and List==nil means default) 628 OXCASE // case List: Nbody (select case before processing; List==nil means default) 629 OCONTINUE // continue 630 ODEFER // defer Left (Left must be call) 631 OEMPTY // no-op (empty statement) 632 OFALL // fallthrough 633 OFOR // for Ninit; Left; Right { Nbody } 634 OFORUNTIL // for Ninit; Left; Right { Nbody } ; test applied after executing body, not before 635 OGOTO // goto Left 636 OIF // if Ninit; Left { Nbody } else { Rlist } 637 OLABEL // Left: 638 OPROC // go Left (Left must be call) 639 ORANGE // for List = range Right { Nbody } 640 ORETURN // return List 641 OSELECT // select { List } (List is list of OXCASE or OCASE) 642 OSWITCH // switch Ninit; Left { List } (List is a list of OXCASE or OCASE) 643 OTYPESW // Left = Right.(type) (appears as .Left of OSWITCH) 644 645 // types 646 OTCHAN // chan int 647 OTMAP // map[string]int 648 OTSTRUCT // struct{} 649 OTINTER // interface{} 650 OTFUNC // func() 651 OTARRAY // []int, [8]int, [N]int or [...]int 652 653 // misc 654 ODDD // func f(args ...int) or f(l...) or var a = [...]int{0, 1, 2}. 655 ODDDARG // func f(args ...int), introduced by escape analysis. 656 OINLCALL // intermediary representation of an inlined call. 657 OEFACE // itable and data words of an empty-interface value. 658 OITAB // itable word of an interface value. 659 OIDATA // data word of an interface value in Left 660 OSPTR // base pointer of a slice or string. 661 OCLOSUREVAR // variable reference at beginning of closure function 662 OCFUNC // reference to c function pointer (not go func value) 663 OCHECKNIL // emit code to ensure pointer/interface not nil 664 OVARKILL // variable is dead 665 OVARLIVE // variable is alive 666 OINDREGSP // offset plus indirect of REGSP, such as 8(SP). 667 668 // arch-specific opcodes 669 ORETJMP // return to other function 670 OGETG // runtime.getg() (read g pointer) 671 672 OEND 673 ) 674 675 // Nodes is a pointer to a slice of *Node. 676 // For fields that are not used in most nodes, this is used instead of 677 // a slice to save space. 678 type Nodes struct{ slice *[]*Node } 679 680 // Slice returns the entries in Nodes as a slice. 681 // Changes to the slice entries (as in s[i] = n) will be reflected in 682 // the Nodes. 683 func (n Nodes) Slice() []*Node { 684 if n.slice == nil { 685 return nil 686 } 687 return *n.slice 688 } 689 690 // Len returns the number of entries in Nodes. 691 func (n Nodes) Len() int { 692 if n.slice == nil { 693 return 0 694 } 695 return len(*n.slice) 696 } 697 698 // Index returns the i'th element of Nodes. 699 // It panics if n does not have at least i+1 elements. 700 func (n Nodes) Index(i int) *Node { 701 return (*n.slice)[i] 702 } 703 704 // First returns the first element of Nodes (same as n.Index(0)). 705 // It panics if n has no elements. 706 func (n Nodes) First() *Node { 707 return (*n.slice)[0] 708 } 709 710 // Second returns the second element of Nodes (same as n.Index(1)). 711 // It panics if n has fewer than two elements. 712 func (n Nodes) Second() *Node { 713 return (*n.slice)[1] 714 } 715 716 // Set sets n to a slice. 717 // This takes ownership of the slice. 718 func (n *Nodes) Set(s []*Node) { 719 if len(s) == 0 { 720 n.slice = nil 721 } else { 722 // Copy s and take address of t rather than s to avoid 723 // allocation in the case where len(s) == 0 (which is 724 // over 3x more common, dynamically, for make.bash). 725 t := s 726 n.slice = &t 727 } 728 } 729 730 // Set1 sets n to a slice containing a single node. 731 func (n *Nodes) Set1(n1 *Node) { 732 n.slice = &[]*Node{n1} 733 } 734 735 // Set2 sets n to a slice containing two nodes. 736 func (n *Nodes) Set2(n1, n2 *Node) { 737 n.slice = &[]*Node{n1, n2} 738 } 739 740 // Set3 sets n to a slice containing three nodes. 741 func (n *Nodes) Set3(n1, n2, n3 *Node) { 742 n.slice = &[]*Node{n1, n2, n3} 743 } 744 745 // MoveNodes sets n to the contents of n2, then clears n2. 746 func (n *Nodes) MoveNodes(n2 *Nodes) { 747 n.slice = n2.slice 748 n2.slice = nil 749 } 750 751 // SetIndex sets the i'th element of Nodes to node. 752 // It panics if n does not have at least i+1 elements. 753 func (n Nodes) SetIndex(i int, node *Node) { 754 (*n.slice)[i] = node 755 } 756 757 // SetFirst sets the first element of Nodes to node. 758 // It panics if n does not have at least one elements. 759 func (n Nodes) SetFirst(node *Node) { 760 (*n.slice)[0] = node 761 } 762 763 // SetSecond sets the second element of Nodes to node. 764 // It panics if n does not have at least two elements. 765 func (n Nodes) SetSecond(node *Node) { 766 (*n.slice)[1] = node 767 } 768 769 // Addr returns the address of the i'th element of Nodes. 770 // It panics if n does not have at least i+1 elements. 771 func (n Nodes) Addr(i int) **Node { 772 return &(*n.slice)[i] 773 } 774 775 // Append appends entries to Nodes. 776 func (n *Nodes) Append(a ...*Node) { 777 if len(a) == 0 { 778 return 779 } 780 if n.slice == nil { 781 s := make([]*Node, len(a)) 782 copy(s, a) 783 n.slice = &s 784 return 785 } 786 *n.slice = append(*n.slice, a...) 787 } 788 789 // Prepend prepends entries to Nodes. 790 // If a slice is passed in, this will take ownership of it. 791 func (n *Nodes) Prepend(a ...*Node) { 792 if len(a) == 0 { 793 return 794 } 795 if n.slice == nil { 796 n.slice = &a 797 } else { 798 *n.slice = append(a, *n.slice...) 799 } 800 } 801 802 // AppendNodes appends the contents of *n2 to n, then clears n2. 803 func (n *Nodes) AppendNodes(n2 *Nodes) { 804 switch { 805 case n2.slice == nil: 806 case n.slice == nil: 807 n.slice = n2.slice 808 default: 809 *n.slice = append(*n.slice, *n2.slice...) 810 } 811 n2.slice = nil 812 } 813 814 // inspect invokes f on each node in an AST in depth-first order. 815 // If f(n) returns false, inspect skips visiting n's children. 816 func inspect(n *Node, f func(*Node) bool) { 817 if n == nil || !f(n) { 818 return 819 } 820 inspectList(n.Ninit, f) 821 inspect(n.Left, f) 822 inspect(n.Right, f) 823 inspectList(n.List, f) 824 inspectList(n.Nbody, f) 825 inspectList(n.Rlist, f) 826 } 827 828 func inspectList(l Nodes, f func(*Node) bool) { 829 for _, n := range l.Slice() { 830 inspect(n, f) 831 } 832 } 833 834 // nodeQueue is a FIFO queue of *Node. The zero value of nodeQueue is 835 // a ready-to-use empty queue. 836 type nodeQueue struct { 837 ring []*Node 838 head, tail int 839 } 840 841 // empty returns true if q contains no Nodes. 842 func (q *nodeQueue) empty() bool { 843 return q.head == q.tail 844 } 845 846 // pushRight appends n to the right of the queue. 847 func (q *nodeQueue) pushRight(n *Node) { 848 if len(q.ring) == 0 { 849 q.ring = make([]*Node, 16) 850 } else if q.head+len(q.ring) == q.tail { 851 // Grow the ring. 852 nring := make([]*Node, len(q.ring)*2) 853 // Copy the old elements. 854 part := q.ring[q.head%len(q.ring):] 855 if q.tail-q.head <= len(part) { 856 part = part[:q.tail-q.head] 857 copy(nring, part) 858 } else { 859 pos := copy(nring, part) 860 copy(nring[pos:], q.ring[:q.tail%len(q.ring)]) 861 } 862 q.ring, q.head, q.tail = nring, 0, q.tail-q.head 863 } 864 865 q.ring[q.tail%len(q.ring)] = n 866 q.tail++ 867 } 868 869 // popLeft pops a node from the left of the queue. It panics if q is 870 // empty. 871 func (q *nodeQueue) popLeft() *Node { 872 if q.empty() { 873 panic("dequeue empty") 874 } 875 n := q.ring[q.head%len(q.ring)] 876 q.head++ 877 return n 878 }