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