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