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