github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/cmd/compile/internal/gc/syntax.go (about)

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