github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/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 if n == nil || n.Func == nil || n.Func.Nname == nil { 218 return "<nil>" 219 } 220 return n.Func.Nname.Sym.Name 221 } 222 223 // Name holds Node fields used only by named nodes (ONAME, OTYPE, OPACK, OLABEL, some OLITERAL). 224 type Name struct { 225 Pack *Node // real package for import . names 226 Pkg *types.Pkg // pkg for OPACK nodes 227 Defn *Node // initializing assignment 228 Curfn *Node // function for local variables 229 Param *Param // additional fields for ONAME, OTYPE 230 Decldepth int32 // declaration loop depth, increased for every loop or label 231 Vargen int32 // unique name for ONAME within a function. Function outputs are numbered starting at one. 232 Funcdepth int32 233 234 used bool // for variable declared and not used error 235 flags bitset8 236 } 237 238 const ( 239 nameCaptured = 1 << iota // is the variable captured by a closure 240 nameReadonly 241 nameByval // is the variable captured by value or by reference 242 nameNeedzero // if it contains pointers, needs to be zeroed on function entry 243 nameKeepalive // mark value live across unknown assembly call 244 nameAutoTemp // is the variable a temporary (implies no dwarf info. reset if escapes to heap) 245 ) 246 247 func (n *Name) Captured() bool { return n.flags&nameCaptured != 0 } 248 func (n *Name) Readonly() bool { return n.flags&nameReadonly != 0 } 249 func (n *Name) Byval() bool { return n.flags&nameByval != 0 } 250 func (n *Name) Needzero() bool { return n.flags&nameNeedzero != 0 } 251 func (n *Name) Keepalive() bool { return n.flags&nameKeepalive != 0 } 252 func (n *Name) AutoTemp() bool { return n.flags&nameAutoTemp != 0 } 253 func (n *Name) Used() bool { return n.used } 254 255 func (n *Name) SetCaptured(b bool) { n.flags.set(nameCaptured, b) } 256 func (n *Name) SetReadonly(b bool) { n.flags.set(nameReadonly, b) } 257 func (n *Name) SetByval(b bool) { n.flags.set(nameByval, b) } 258 func (n *Name) SetNeedzero(b bool) { n.flags.set(nameNeedzero, b) } 259 func (n *Name) SetKeepalive(b bool) { n.flags.set(nameKeepalive, b) } 260 func (n *Name) SetAutoTemp(b bool) { n.flags.set(nameAutoTemp, b) } 261 func (n *Name) SetUsed(b bool) { n.used = b } 262 263 type Param struct { 264 Ntype *Node 265 Heapaddr *Node // temp holding heap address of param 266 267 // ONAME PAUTOHEAP 268 Stackcopy *Node // the PPARAM/PPARAMOUT on-stack slot (moved func params only) 269 270 // ONAME PPARAM 271 Field *types.Field // TFIELD in arg struct 272 273 // ONAME closure linkage 274 // Consider: 275 // 276 // func f() { 277 // x := 1 // x1 278 // func() { 279 // use(x) // x2 280 // func() { 281 // use(x) // x3 282 // --- parser is here --- 283 // }() 284 // }() 285 // } 286 // 287 // There is an original declaration of x and then a chain of mentions of x 288 // leading into the current function. Each time x is mentioned in a new closure, 289 // we create a variable representing x for use in that specific closure, 290 // since the way you get to x is different in each closure. 291 // 292 // Let's number the specific variables as shown in the code: 293 // x1 is the original x, x2 is when mentioned in the closure, 294 // and x3 is when mentioned in the closure in the closure. 295 // 296 // We keep these linked (assume N > 1): 297 // 298 // - x1.Defn = original declaration statement for x (like most variables) 299 // - x1.Innermost = current innermost closure x (in this case x3), or nil for none 300 // - x1.IsClosureVar() = false 301 // 302 // - xN.Defn = x1, N > 1 303 // - xN.IsClosureVar() = true, N > 1 304 // - x2.Outer = nil 305 // - xN.Outer = x(N-1), N > 2 306 // 307 // 308 // When we look up x in the symbol table, we always get x1. 309 // Then we can use x1.Innermost (if not nil) to get the x 310 // for the innermost known closure function, 311 // but the first reference in a closure will find either no x1.Innermost 312 // or an x1.Innermost with .Funcdepth < Funcdepth. 313 // In that case, a new xN must be created, linked in with: 314 // 315 // xN.Defn = x1 316 // xN.Outer = x1.Innermost 317 // x1.Innermost = xN 318 // 319 // When we finish the function, we'll process its closure variables 320 // and find xN and pop it off the list using: 321 // 322 // x1 := xN.Defn 323 // x1.Innermost = xN.Outer 324 // 325 // We leave xN.Innermost set so that we can still get to the original 326 // variable quickly. Not shown here, but once we're 327 // done parsing a function and no longer need xN.Outer for the 328 // lexical x reference links as described above, closurebody 329 // recomputes xN.Outer as the semantic x reference link tree, 330 // even filling in x in intermediate closures that might not 331 // have mentioned it along the way to inner closures that did. 332 // See closurebody for details. 333 // 334 // During the eventual compilation, then, for closure variables we have: 335 // 336 // xN.Defn = original variable 337 // xN.Outer = variable captured in next outward scope 338 // to make closure where xN appears 339 // 340 // Because of the sharding of pieces of the node, x.Defn means x.Name.Defn 341 // and x.Innermost/Outer means x.Name.Param.Innermost/Outer. 342 Innermost *Node 343 Outer *Node 344 345 // OTYPE 346 // 347 // TODO: Should Func pragmas also be stored on the Name? 348 Pragma syntax.Pragma 349 Alias bool // node is alias for Ntype (only used when type-checking ODCLTYPE) 350 } 351 352 // Func holds Node fields used only with function-like nodes. 353 type Func struct { 354 Shortname *types.Sym 355 Enter Nodes // for example, allocate and initialize memory for escaping parameters 356 Exit Nodes 357 Cvars Nodes // closure params 358 Dcl []*Node // autodcl for this func/closure 359 Inldcl Nodes // copy of dcl for use in inlining 360 361 // Parents records the parent scope of each scope within a 362 // function. The root scope (0) has no parent, so the i'th 363 // scope's parent is stored at Parents[i-1]. 364 Parents []ScopeID 365 366 // Marks records scope boundary changes. 367 Marks []Mark 368 369 Closgen int 370 Outerfunc *Node // outer function (for closure) 371 FieldTrack map[*types.Sym]struct{} 372 Ntype *Node // signature 373 Top int // top context (Ecall, Eproc, etc) 374 Closure *Node // OCLOSURE <-> ODCLFUNC 375 Nname *Node 376 lsym *obj.LSym 377 378 Inl Nodes // copy of the body for use in inlining 379 InlCost int32 380 Depth int32 381 382 Label int32 // largest auto-generated label in this function 383 384 Endlineno src.XPos 385 WBPos src.XPos // position of first write barrier 386 387 Pragma syntax.Pragma // go:xxx function annotations 388 389 flags bitset8 390 } 391 392 // A Mark represents a scope boundary. 393 type Mark struct { 394 // Pos is the position of the token that marks the scope 395 // change. 396 Pos src.XPos 397 398 // Scope identifies the innermost scope to the right of Pos. 399 Scope ScopeID 400 } 401 402 // A ScopeID represents a lexical scope within a function. 403 type ScopeID int32 404 405 const ( 406 funcDupok = 1 << iota // duplicate definitions ok 407 funcWrapper // is method wrapper 408 funcNeedctxt // function uses context register (has closure variables) 409 funcReflectMethod // function calls reflect.Type.Method or MethodByName 410 funcIsHiddenClosure 411 funcNoFramePointer // Must not use a frame pointer for this function 412 funcHasDefer // contains a defer statement 413 funcNilCheckDisabled // disable nil checks when compiling this function 414 ) 415 416 func (f *Func) Dupok() bool { return f.flags&funcDupok != 0 } 417 func (f *Func) Wrapper() bool { return f.flags&funcWrapper != 0 } 418 func (f *Func) Needctxt() bool { return f.flags&funcNeedctxt != 0 } 419 func (f *Func) ReflectMethod() bool { return f.flags&funcReflectMethod != 0 } 420 func (f *Func) IsHiddenClosure() bool { return f.flags&funcIsHiddenClosure != 0 } 421 func (f *Func) NoFramePointer() bool { return f.flags&funcNoFramePointer != 0 } 422 func (f *Func) HasDefer() bool { return f.flags&funcHasDefer != 0 } 423 func (f *Func) NilCheckDisabled() bool { return f.flags&funcNilCheckDisabled != 0 } 424 425 func (f *Func) SetDupok(b bool) { f.flags.set(funcDupok, b) } 426 func (f *Func) SetWrapper(b bool) { f.flags.set(funcWrapper, b) } 427 func (f *Func) SetNeedctxt(b bool) { f.flags.set(funcNeedctxt, b) } 428 func (f *Func) SetReflectMethod(b bool) { f.flags.set(funcReflectMethod, b) } 429 func (f *Func) SetIsHiddenClosure(b bool) { f.flags.set(funcIsHiddenClosure, b) } 430 func (f *Func) SetNoFramePointer(b bool) { f.flags.set(funcNoFramePointer, b) } 431 func (f *Func) SetHasDefer(b bool) { f.flags.set(funcHasDefer, b) } 432 func (f *Func) SetNilCheckDisabled(b bool) { f.flags.set(funcNilCheckDisabled, b) } 433 434 type Op uint8 435 436 // Node ops. 437 const ( 438 OXXX = Op(iota) 439 440 // names 441 ONAME // var, const or func name 442 ONONAME // unnamed arg or return value: f(int, string) (int, error) { etc } 443 OTYPE // type name 444 OPACK // import 445 OLITERAL // literal 446 447 // expressions 448 OADD // Left + Right 449 OSUB // Left - Right 450 OOR // Left | Right 451 OXOR // Left ^ Right 452 OADDSTR // +{List} (string addition, list elements are strings) 453 OADDR // &Left 454 OANDAND // Left && Right 455 OAPPEND // append(List); after walk, Left may contain elem type descriptor 456 OARRAYBYTESTR // Type(Left) (Type is string, Left is a []byte) 457 OARRAYBYTESTRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral) 458 OARRAYRUNESTR // Type(Left) (Type is string, Left is a []rune) 459 OSTRARRAYBYTE // Type(Left) (Type is []byte, Left is a string) 460 OSTRARRAYBYTETMP // Type(Left) (Type is []byte, Left is a string, ephemeral) 461 OSTRARRAYRUNE // Type(Left) (Type is []rune, Left is a string) 462 OAS // Left = Right or (if Colas=true) Left := Right 463 OAS2 // List = Rlist (x, y, z = a, b, c) 464 OAS2FUNC // List = Rlist (x, y = f()) 465 OAS2RECV // List = Rlist (x, ok = <-c) 466 OAS2MAPR // List = Rlist (x, ok = m["foo"]) 467 OAS2DOTTYPE // List = Rlist (x, ok = I.(int)) 468 OASOP // Left Etype= Right (x += y) 469 OCALL // Left(List) (function call, method call or type conversion) 470 OCALLFUNC // Left(List) (function call f(args)) 471 OCALLMETH // Left(List) (direct method call x.Method(args)) 472 OCALLINTER // Left(List) (interface method call x.Method(args)) 473 OCALLPART // Left.Right (method expression x.Method, not called) 474 OCAP // cap(Left) 475 OCLOSE // close(Left) 476 OCLOSURE // func Type { Body } (func literal) 477 OCMPIFACE // Left Etype Right (interface comparison, x == y or x != y) 478 OCMPSTR // Left Etype Right (string comparison, x == y, x < y, etc) 479 OCOMPLIT // Right{List} (composite literal, not yet lowered to specific form) 480 OMAPLIT // Type{List} (composite literal, Type is map) 481 OSTRUCTLIT // Type{List} (composite literal, Type is struct) 482 OARRAYLIT // Type{List} (composite literal, Type is array) 483 OSLICELIT // Type{List} (composite literal, Type is slice) 484 OPTRLIT // &Left (left is composite literal) 485 OCONV // Type(Left) (type conversion) 486 OCONVIFACE // Type(Left) (type conversion, to interface) 487 OCONVNOP // Type(Left) (type conversion, no effect) 488 OCOPY // copy(Left, Right) 489 ODCL // var Left (declares Left of type Left.Type) 490 491 // Used during parsing but don't last. 492 ODCLFUNC // func f() or func (r) f() 493 ODCLFIELD // struct field, interface field, or func/method argument/return value. 494 ODCLCONST // const pi = 3.14 495 ODCLTYPE // type Int int or type Int = int 496 497 ODELETE // delete(Left, Right) 498 ODOT // Left.Sym (Left is of struct type) 499 ODOTPTR // Left.Sym (Left is of pointer to struct type) 500 ODOTMETH // Left.Sym (Left is non-interface, Right is method name) 501 ODOTINTER // Left.Sym (Left is interface, Right is method name) 502 OXDOT // Left.Sym (before rewrite to one of the preceding) 503 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 504 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 505 OEQ // Left == Right 506 ONE // Left != Right 507 OLT // Left < Right 508 OLE // Left <= Right 509 OGE // Left >= Right 510 OGT // Left > Right 511 OIND // *Left 512 OINDEX // Left[Right] (index of array or slice) 513 OINDEXMAP // Left[Right] (index of map) 514 OKEY // Left:Right (key:value in struct/array/map literal) 515 OSTRUCTKEY // Sym:Left (key:value in struct literal, after type checking) 516 OLEN // len(Left) 517 OMAKE // make(List) (before type checking converts to one of the following) 518 OMAKECHAN // make(Type, Left) (type is chan) 519 OMAKEMAP // make(Type, Left) (type is map) 520 OMAKESLICE // make(Type, Left, Right) (type is slice) 521 OMUL // Left * Right 522 ODIV // Left / Right 523 OMOD // Left % Right 524 OLSH // Left << Right 525 ORSH // Left >> Right 526 OAND // Left & Right 527 OANDNOT // Left &^ Right 528 ONEW // new(Left) 529 ONOT // !Left 530 OCOM // ^Left 531 OPLUS // +Left 532 OMINUS // -Left 533 OOROR // Left || Right 534 OPANIC // panic(Left) 535 OPRINT // print(List) 536 OPRINTN // println(List) 537 OPAREN // (Left) 538 OSEND // Left <- Right 539 OSLICE // Left[List[0] : List[1]] (Left is untypechecked or slice) 540 OSLICEARR // Left[List[0] : List[1]] (Left is array) 541 OSLICESTR // Left[List[0] : List[1]] (Left is string) 542 OSLICE3 // Left[List[0] : List[1] : List[2]] (Left is untypedchecked or slice) 543 OSLICE3ARR // Left[List[0] : List[1] : List[2]] (Left is array) 544 ORECOVER // recover() 545 ORECV // <-Left 546 ORUNESTR // Type(Left) (Type is string, Left is rune) 547 OSELRECV // Left = <-Right.Left: (appears as .Left of OCASE; Right.Op == ORECV) 548 OSELRECV2 // List = <-Right.Left: (apperas as .Left of OCASE; count(List) == 2, Right.Op == ORECV) 549 OIOTA // iota 550 OREAL // real(Left) 551 OIMAG // imag(Left) 552 OCOMPLEX // complex(Left, Right) 553 OALIGNOF // unsafe.Alignof(Left) 554 OOFFSETOF // unsafe.Offsetof(Left) 555 OSIZEOF // unsafe.Sizeof(Left) 556 557 // statements 558 OBLOCK // { List } (block of code) 559 OBREAK // break 560 OCASE // case Left or List[0]..List[1]: Nbody (select case after processing; Left==nil and List==nil means default) 561 OXCASE // case List: Nbody (select case before processing; List==nil means default) 562 OCONTINUE // continue 563 ODEFER // defer Left (Left must be call) 564 OEMPTY // no-op (empty statement) 565 OFALL // fallthrough (after processing) 566 OXFALL // fallthrough (before processing) 567 OFOR // for Ninit; Left; Right { Nbody } 568 OFORUNTIL // for Ninit; Left; Right { Nbody } ; test applied after executing body, not before 569 OGOTO // goto Left 570 OIF // if Ninit; Left { Nbody } else { Rlist } 571 OLABEL // Left: 572 OPROC // go Left (Left must be call) 573 ORANGE // for List = range Right { Nbody } 574 ORETURN // return List 575 OSELECT // select { List } (List is list of OXCASE or OCASE) 576 OSWITCH // switch Ninit; Left { List } (List is a list of OXCASE or OCASE) 577 OTYPESW // List = Left.(type) (appears as .Left of OSWITCH) 578 579 // types 580 OTCHAN // chan int 581 OTMAP // map[string]int 582 OTSTRUCT // struct{} 583 OTINTER // interface{} 584 OTFUNC // func() 585 OTARRAY // []int, [8]int, [N]int or [...]int 586 587 // misc 588 ODDD // func f(args ...int) or f(l...) or var a = [...]int{0, 1, 2}. 589 ODDDARG // func f(args ...int), introduced by escape analysis. 590 OINLCALL // intermediary representation of an inlined call. 591 OEFACE // itable and data words of an empty-interface value. 592 OITAB // itable word of an interface value. 593 OIDATA // data word of an interface value in Left 594 OSPTR // base pointer of a slice or string. 595 OCLOSUREVAR // variable reference at beginning of closure function 596 OCFUNC // reference to c function pointer (not go func value) 597 OCHECKNIL // emit code to ensure pointer/interface not nil 598 OVARKILL // variable is dead 599 OVARLIVE // variable is alive 600 OINDREGSP // offset plus indirect of REGSP, such as 8(SP). 601 602 // arch-specific opcodes 603 ORETJMP // return to other function 604 OGETG // runtime.getg() (read g pointer) 605 606 OEND 607 ) 608 609 // Nodes is a pointer to a slice of *Node. 610 // For fields that are not used in most nodes, this is used instead of 611 // a slice to save space. 612 type Nodes struct{ slice *[]*Node } 613 614 // Slice returns the entries in Nodes as a slice. 615 // Changes to the slice entries (as in s[i] = n) will be reflected in 616 // the Nodes. 617 func (n Nodes) Slice() []*Node { 618 if n.slice == nil { 619 return nil 620 } 621 return *n.slice 622 } 623 624 // Len returns the number of entries in Nodes. 625 func (n Nodes) Len() int { 626 if n.slice == nil { 627 return 0 628 } 629 return len(*n.slice) 630 } 631 632 // Index returns the i'th element of Nodes. 633 // It panics if n does not have at least i+1 elements. 634 func (n Nodes) Index(i int) *Node { 635 return (*n.slice)[i] 636 } 637 638 // First returns the first element of Nodes (same as n.Index(0)). 639 // It panics if n has no elements. 640 func (n Nodes) First() *Node { 641 return (*n.slice)[0] 642 } 643 644 // Second returns the second element of Nodes (same as n.Index(1)). 645 // It panics if n has fewer than two elements. 646 func (n Nodes) Second() *Node { 647 return (*n.slice)[1] 648 } 649 650 // Set sets n to a slice. 651 // This takes ownership of the slice. 652 func (n *Nodes) Set(s []*Node) { 653 if len(s) == 0 { 654 n.slice = nil 655 } else { 656 // Copy s and take address of t rather than s to avoid 657 // allocation in the case where len(s) == 0 (which is 658 // over 3x more common, dynamically, for make.bash). 659 t := s 660 n.slice = &t 661 } 662 } 663 664 // Set1 sets n to a slice containing a single node. 665 func (n *Nodes) Set1(n1 *Node) { 666 n.slice = &[]*Node{n1} 667 } 668 669 // Set2 sets n to a slice containing two nodes. 670 func (n *Nodes) Set2(n1, n2 *Node) { 671 n.slice = &[]*Node{n1, n2} 672 } 673 674 // Set3 sets n to a slice containing three nodes. 675 func (n *Nodes) Set3(n1, n2, n3 *Node) { 676 n.slice = &[]*Node{n1, n2, n3} 677 } 678 679 // MoveNodes sets n to the contents of n2, then clears n2. 680 func (n *Nodes) MoveNodes(n2 *Nodes) { 681 n.slice = n2.slice 682 n2.slice = nil 683 } 684 685 // SetIndex sets the i'th element of Nodes to node. 686 // It panics if n does not have at least i+1 elements. 687 func (n Nodes) SetIndex(i int, node *Node) { 688 (*n.slice)[i] = node 689 } 690 691 // SetFirst sets the first element of Nodes to node. 692 // It panics if n does not have at least one elements. 693 func (n Nodes) SetFirst(node *Node) { 694 (*n.slice)[0] = node 695 } 696 697 // SetSecond sets the second element of Nodes to node. 698 // It panics if n does not have at least two elements. 699 func (n Nodes) SetSecond(node *Node) { 700 (*n.slice)[1] = node 701 } 702 703 // Addr returns the address of the i'th element of Nodes. 704 // It panics if n does not have at least i+1 elements. 705 func (n Nodes) Addr(i int) **Node { 706 return &(*n.slice)[i] 707 } 708 709 // Append appends entries to Nodes. 710 func (n *Nodes) Append(a ...*Node) { 711 if len(a) == 0 { 712 return 713 } 714 if n.slice == nil { 715 s := make([]*Node, len(a)) 716 copy(s, a) 717 n.slice = &s 718 return 719 } 720 *n.slice = append(*n.slice, a...) 721 } 722 723 // Prepend prepends entries to Nodes. 724 // If a slice is passed in, this will take ownership of it. 725 func (n *Nodes) Prepend(a ...*Node) { 726 if len(a) == 0 { 727 return 728 } 729 if n.slice == nil { 730 n.slice = &a 731 } else { 732 *n.slice = append(a, *n.slice...) 733 } 734 } 735 736 // AppendNodes appends the contents of *n2 to n, then clears n2. 737 func (n *Nodes) AppendNodes(n2 *Nodes) { 738 switch { 739 case n2.slice == nil: 740 case n.slice == nil: 741 n.slice = n2.slice 742 default: 743 *n.slice = append(*n.slice, *n2.slice...) 744 } 745 n2.slice = nil 746 }