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