Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / go / ssa / ssa.go
1 // Copyright 2013 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 package ssa
6
7 // This package defines a high-level intermediate representation for
8 // Go programs using static single-assignment (SSA) form.
9
10 import (
11         "fmt"
12         "go/ast"
13         "go/constant"
14         "go/token"
15         "go/types"
16         "sync"
17
18         "golang.org/x/tools/go/types/typeutil"
19 )
20
21 // A Program is a partial or complete Go program converted to SSA form.
22 type Program struct {
23         Fset       *token.FileSet              // position information for the files of this Program
24         imported   map[string]*Package         // all importable Packages, keyed by import path
25         packages   map[*types.Package]*Package // all loaded Packages, keyed by object
26         mode       BuilderMode                 // set of mode bits for SSA construction
27         MethodSets typeutil.MethodSetCache     // cache of type-checker's method-sets
28
29         methodsMu    sync.Mutex                 // guards the following maps:
30         methodSets   typeutil.Map               // maps type to its concrete methodSet
31         runtimeTypes typeutil.Map               // types for which rtypes are needed
32         canon        typeutil.Map               // type canonicalization map
33         bounds       map[*types.Func]*Function  // bounds for curried x.Method closures
34         thunks       map[selectionKey]*Function // thunks for T.Method expressions
35 }
36
37 // A Package is a single analyzed Go package containing Members for
38 // all package-level functions, variables, constants and types it
39 // declares.  These may be accessed directly via Members, or via the
40 // type-specific accessor methods Func, Type, Var and Const.
41 //
42 // Members also contains entries for "init" (the synthetic package
43 // initializer) and "init#%d", the nth declared init function,
44 // and unspecified other things too.
45 //
46 type Package struct {
47         Prog    *Program               // the owning program
48         Pkg     *types.Package         // the corresponding go/types.Package
49         Members map[string]Member      // all package members keyed by name (incl. init and init#%d)
50         values  map[types.Object]Value // package members (incl. types and methods), keyed by object
51         init    *Function              // Func("init"); the package's init function
52         debug   bool                   // include full debug info in this package
53
54         // The following fields are set transiently, then cleared
55         // after building.
56         buildOnce sync.Once   // ensures package building occurs once
57         ninit     int32       // number of init functions
58         info      *types.Info // package type information
59         files     []*ast.File // package ASTs
60 }
61
62 // A Member is a member of a Go package, implemented by *NamedConst,
63 // *Global, *Function, or *Type; they are created by package-level
64 // const, var, func and type declarations respectively.
65 //
66 type Member interface {
67         Name() string                    // declared name of the package member
68         String() string                  // package-qualified name of the package member
69         RelString(*types.Package) string // like String, but relative refs are unqualified
70         Object() types.Object            // typechecker's object for this member, if any
71         Pos() token.Pos                  // position of member's declaration, if known
72         Type() types.Type                // type of the package member
73         Token() token.Token              // token.{VAR,FUNC,CONST,TYPE}
74         Package() *Package               // the containing package
75 }
76
77 // A Type is a Member of a Package representing a package-level named type.
78 type Type struct {
79         object *types.TypeName
80         pkg    *Package
81 }
82
83 // A NamedConst is a Member of a Package representing a package-level
84 // named constant.
85 //
86 // Pos() returns the position of the declaring ast.ValueSpec.Names[*]
87 // identifier.
88 //
89 // NB: a NamedConst is not a Value; it contains a constant Value, which
90 // it augments with the name and position of its 'const' declaration.
91 //
92 type NamedConst struct {
93         object *types.Const
94         Value  *Const
95         pkg    *Package
96 }
97
98 // A Value is an SSA value that can be referenced by an instruction.
99 type Value interface {
100         // Name returns the name of this value, and determines how
101         // this Value appears when used as an operand of an
102         // Instruction.
103         //
104         // This is the same as the source name for Parameters,
105         // Builtins, Functions, FreeVars, Globals.
106         // For constants, it is a representation of the constant's value
107         // and type.  For all other Values this is the name of the
108         // virtual register defined by the instruction.
109         //
110         // The name of an SSA Value is not semantically significant,
111         // and may not even be unique within a function.
112         Name() string
113
114         // If this value is an Instruction, String returns its
115         // disassembled form; otherwise it returns unspecified
116         // human-readable information about the Value, such as its
117         // kind, name and type.
118         String() string
119
120         // Type returns the type of this value.  Many instructions
121         // (e.g. IndexAddr) change their behaviour depending on the
122         // types of their operands.
123         Type() types.Type
124
125         // Parent returns the function to which this Value belongs.
126         // It returns nil for named Functions, Builtin, Const and Global.
127         Parent() *Function
128
129         // Referrers returns the list of instructions that have this
130         // value as one of their operands; it may contain duplicates
131         // if an instruction has a repeated operand.
132         //
133         // Referrers actually returns a pointer through which the
134         // caller may perform mutations to the object's state.
135         //
136         // Referrers is currently only defined if Parent()!=nil,
137         // i.e. for the function-local values FreeVar, Parameter,
138         // Functions (iff anonymous) and all value-defining instructions.
139         // It returns nil for named Functions, Builtin, Const and Global.
140         //
141         // Instruction.Operands contains the inverse of this relation.
142         Referrers() *[]Instruction
143
144         // Pos returns the location of the AST token most closely
145         // associated with the operation that gave rise to this value,
146         // or token.NoPos if it was not explicit in the source.
147         //
148         // For each ast.Node type, a particular token is designated as
149         // the closest location for the expression, e.g. the Lparen
150         // for an *ast.CallExpr.  This permits a compact but
151         // approximate mapping from Values to source positions for use
152         // in diagnostic messages, for example.
153         //
154         // (Do not use this position to determine which Value
155         // corresponds to an ast.Expr; use Function.ValueForExpr
156         // instead.  NB: it requires that the function was built with
157         // debug information.)
158         Pos() token.Pos
159 }
160
161 // An Instruction is an SSA instruction that computes a new Value or
162 // has some effect.
163 //
164 // An Instruction that defines a value (e.g. BinOp) also implements
165 // the Value interface; an Instruction that only has an effect (e.g. Store)
166 // does not.
167 //
168 type Instruction interface {
169         // String returns the disassembled form of this value.
170         //
171         // Examples of Instructions that are Values:
172         //       "x + y"     (BinOp)
173         //       "len([])"   (Call)
174         // Note that the name of the Value is not printed.
175         //
176         // Examples of Instructions that are not Values:
177         //       "return x"  (Return)
178         //       "*y = x"    (Store)
179         //
180         // (The separation Value.Name() from Value.String() is useful
181         // for some analyses which distinguish the operation from the
182         // value it defines, e.g., 'y = local int' is both an allocation
183         // of memory 'local int' and a definition of a pointer y.)
184         String() string
185
186         // Parent returns the function to which this instruction
187         // belongs.
188         Parent() *Function
189
190         // Block returns the basic block to which this instruction
191         // belongs.
192         Block() *BasicBlock
193
194         // setBlock sets the basic block to which this instruction belongs.
195         setBlock(*BasicBlock)
196
197         // Operands returns the operands of this instruction: the
198         // set of Values it references.
199         //
200         // Specifically, it appends their addresses to rands, a
201         // user-provided slice, and returns the resulting slice,
202         // permitting avoidance of memory allocation.
203         //
204         // The operands are appended in undefined order, but the order
205         // is consistent for a given Instruction; the addresses are
206         // always non-nil but may point to a nil Value.  Clients may
207         // store through the pointers, e.g. to effect a value
208         // renaming.
209         //
210         // Value.Referrers is a subset of the inverse of this
211         // relation.  (Referrers are not tracked for all types of
212         // Values.)
213         Operands(rands []*Value) []*Value
214
215         // Pos returns the location of the AST token most closely
216         // associated with the operation that gave rise to this
217         // instruction, or token.NoPos if it was not explicit in the
218         // source.
219         //
220         // For each ast.Node type, a particular token is designated as
221         // the closest location for the expression, e.g. the Go token
222         // for an *ast.GoStmt.  This permits a compact but approximate
223         // mapping from Instructions to source positions for use in
224         // diagnostic messages, for example.
225         //
226         // (Do not use this position to determine which Instruction
227         // corresponds to an ast.Expr; see the notes for Value.Pos.
228         // This position may be used to determine which non-Value
229         // Instruction corresponds to some ast.Stmts, but not all: If
230         // and Jump instructions have no Pos(), for example.)
231         Pos() token.Pos
232 }
233
234 // A Node is a node in the SSA value graph.  Every concrete type that
235 // implements Node is also either a Value, an Instruction, or both.
236 //
237 // Node contains the methods common to Value and Instruction, plus the
238 // Operands and Referrers methods generalized to return nil for
239 // non-Instructions and non-Values, respectively.
240 //
241 // Node is provided to simplify SSA graph algorithms.  Clients should
242 // use the more specific and informative Value or Instruction
243 // interfaces where appropriate.
244 //
245 type Node interface {
246         // Common methods:
247         String() string
248         Pos() token.Pos
249         Parent() *Function
250
251         // Partial methods:
252         Operands(rands []*Value) []*Value // nil for non-Instructions
253         Referrers() *[]Instruction        // nil for non-Values
254 }
255
256 // Function represents the parameters, results, and code of a function
257 // or method.
258 //
259 // If Blocks is nil, this indicates an external function for which no
260 // Go source code is available.  In this case, FreeVars and Locals
261 // are nil too.  Clients performing whole-program analysis must
262 // handle external functions specially.
263 //
264 // Blocks contains the function's control-flow graph (CFG).
265 // Blocks[0] is the function entry point; block order is not otherwise
266 // semantically significant, though it may affect the readability of
267 // the disassembly.
268 // To iterate over the blocks in dominance order, use DomPreorder().
269 //
270 // Recover is an optional second entry point to which control resumes
271 // after a recovered panic.  The Recover block may contain only a return
272 // statement, preceded by a load of the function's named return
273 // parameters, if any.
274 //
275 // A nested function (Parent()!=nil) that refers to one or more
276 // lexically enclosing local variables ("free variables") has FreeVars.
277 // Such functions cannot be called directly but require a
278 // value created by MakeClosure which, via its Bindings, supplies
279 // values for these parameters.
280 //
281 // If the function is a method (Signature.Recv() != nil) then the first
282 // element of Params is the receiver parameter.
283 //
284 // A Go package may declare many functions called "init".
285 // For each one, Object().Name() returns "init" but Name() returns
286 // "init#1", etc, in declaration order.
287 //
288 // Pos() returns the declaring ast.FuncLit.Type.Func or the position
289 // of the ast.FuncDecl.Name, if the function was explicit in the
290 // source.  Synthetic wrappers, for which Synthetic != "", may share
291 // the same position as the function they wrap.
292 // Syntax.Pos() always returns the position of the declaring "func" token.
293 //
294 // Type() returns the function's Signature.
295 //
296 type Function struct {
297         name      string
298         object    types.Object     // a declared *types.Func or one of its wrappers
299         method    *types.Selection // info about provenance of synthetic methods
300         Signature *types.Signature
301         pos       token.Pos
302
303         Synthetic string        // provenance of synthetic function; "" for true source functions
304         syntax    ast.Node      // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode
305         parent    *Function     // enclosing function if anon; nil if global
306         Pkg       *Package      // enclosing package; nil for shared funcs (wrappers and error.Error)
307         Prog      *Program      // enclosing program
308         Params    []*Parameter  // function parameters; for methods, includes receiver
309         FreeVars  []*FreeVar    // free variables whose values must be supplied by closure
310         Locals    []*Alloc      // local variables of this function
311         Blocks    []*BasicBlock // basic blocks of the function; nil => external
312         Recover   *BasicBlock   // optional; control transfers here after recovered panic
313         AnonFuncs []*Function   // anonymous functions directly beneath this one
314         referrers []Instruction // referring instructions (iff Parent() != nil)
315
316         // The following fields are set transiently during building,
317         // then cleared.
318         currentBlock *BasicBlock             // where to emit code
319         objects      map[types.Object]Value  // addresses of local variables
320         namedResults []*Alloc                // tuple of named results
321         targets      *targets                // linked stack of branch targets
322         lblocks      map[*ast.Object]*lblock // labelled blocks
323 }
324
325 // BasicBlock represents an SSA basic block.
326 //
327 // The final element of Instrs is always an explicit transfer of
328 // control (If, Jump, Return, or Panic).
329 //
330 // A block may contain no Instructions only if it is unreachable,
331 // i.e., Preds is nil.  Empty blocks are typically pruned.
332 //
333 // BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
334 // graph independent of the SSA Value graph: the control-flow graph or
335 // CFG.  It is illegal for multiple edges to exist between the same
336 // pair of blocks.
337 //
338 // Each BasicBlock is also a node in the dominator tree of the CFG.
339 // The tree may be navigated using Idom()/Dominees() and queried using
340 // Dominates().
341 //
342 // The order of Preds and Succs is significant (to Phi and If
343 // instructions, respectively).
344 //
345 type BasicBlock struct {
346         Index        int            // index of this block within Parent().Blocks
347         Comment      string         // optional label; no semantic significance
348         parent       *Function      // parent function
349         Instrs       []Instruction  // instructions in order
350         Preds, Succs []*BasicBlock  // predecessors and successors
351         succs2       [2]*BasicBlock // initial space for Succs
352         dom          domInfo        // dominator tree info
353         gaps         int            // number of nil Instrs (transient)
354         rundefers    int            // number of rundefers (transient)
355 }
356
357 // Pure values ----------------------------------------
358
359 // A FreeVar represents a free variable of the function to which it
360 // belongs.
361 //
362 // FreeVars are used to implement anonymous functions, whose free
363 // variables are lexically captured in a closure formed by
364 // MakeClosure.  The value of such a free var is an Alloc or another
365 // FreeVar and is considered a potentially escaping heap address, with
366 // pointer type.
367 //
368 // FreeVars are also used to implement bound method closures.  Such a
369 // free var represents the receiver value and may be of any type that
370 // has concrete methods.
371 //
372 // Pos() returns the position of the value that was captured, which
373 // belongs to an enclosing function.
374 //
375 type FreeVar struct {
376         name      string
377         typ       types.Type
378         pos       token.Pos
379         parent    *Function
380         referrers []Instruction
381
382         // Transiently needed during building.
383         outer Value // the Value captured from the enclosing context.
384 }
385
386 // A Parameter represents an input parameter of a function.
387 //
388 type Parameter struct {
389         name      string
390         object    types.Object // a *types.Var; nil for non-source locals
391         typ       types.Type
392         pos       token.Pos
393         parent    *Function
394         referrers []Instruction
395 }
396
397 // A Const represents the value of a constant expression.
398 //
399 // The underlying type of a constant may be any boolean, numeric, or
400 // string type.  In addition, a Const may represent the nil value of
401 // any reference type---interface, map, channel, pointer, slice, or
402 // function---but not "untyped nil".
403 //
404 // All source-level constant expressions are represented by a Const
405 // of the same type and value.
406 //
407 // Value holds the value of the constant, independent of its Type(),
408 // using go/constant representation, or nil for a typed nil value.
409 //
410 // Pos() returns token.NoPos.
411 //
412 // Example printed form:
413 //      42:int
414 //      "hello":untyped string
415 //      3+4i:MyComplex
416 //
417 type Const struct {
418         typ   types.Type
419         Value constant.Value
420 }
421
422 // A Global is a named Value holding the address of a package-level
423 // variable.
424 //
425 // Pos() returns the position of the ast.ValueSpec.Names[*]
426 // identifier.
427 //
428 type Global struct {
429         name   string
430         object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
431         typ    types.Type
432         pos    token.Pos
433
434         Pkg *Package
435 }
436
437 // A Builtin represents a specific use of a built-in function, e.g. len.
438 //
439 // Builtins are immutable values.  Builtins do not have addresses.
440 // Builtins can only appear in CallCommon.Func.
441 //
442 // Name() indicates the function: one of the built-in functions from the
443 // Go spec (excluding "make" and "new") or one of these ssa-defined
444 // intrinsics:
445 //
446 //   // wrapnilchk returns ptr if non-nil, panics otherwise.
447 //   // (For use in indirection wrappers.)
448 //   func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T
449 //
450 // Object() returns a *types.Builtin for built-ins defined by the spec,
451 // nil for others.
452 //
453 // Type() returns a *types.Signature representing the effective
454 // signature of the built-in for this call.
455 //
456 type Builtin struct {
457         name string
458         sig  *types.Signature
459 }
460
461 // Value-defining instructions  ----------------------------------------
462
463 // The Alloc instruction reserves space for a variable of the given type,
464 // zero-initializes it, and yields its address.
465 //
466 // Alloc values are always addresses, and have pointer types, so the
467 // type of the allocated variable is actually
468 // Type().Underlying().(*types.Pointer).Elem().
469 //
470 // If Heap is false, Alloc allocates space in the function's
471 // activation record (frame); we refer to an Alloc(Heap=false) as a
472 // "local" alloc.  Each local Alloc returns the same address each time
473 // it is executed within the same activation; the space is
474 // re-initialized to zero.
475 //
476 // If Heap is true, Alloc allocates space in the heap; we
477 // refer to an Alloc(Heap=true) as a "new" alloc.  Each new Alloc
478 // returns a different address each time it is executed.
479 //
480 // When Alloc is applied to a channel, map or slice type, it returns
481 // the address of an uninitialized (nil) reference of that kind; store
482 // the result of MakeSlice, MakeMap or MakeChan in that location to
483 // instantiate these types.
484 //
485 // Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
486 // or the ast.CallExpr.Rparen for a call to new() or for a call that
487 // allocates a varargs slice.
488 //
489 // Example printed form:
490 //      t0 = local int
491 //      t1 = new int
492 //
493 type Alloc struct {
494         register
495         Comment string
496         Heap    bool
497         index   int // dense numbering; for lifting
498 }
499
500 // The Phi instruction represents an SSA Ï†-node, which combines values
501 // that differ across incoming control-flow edges and yields a new
502 // value.  Within a block, all Ï†-nodes must appear before all non-φ
503 // nodes.
504 //
505 // Pos() returns the position of the && or || for short-circuit
506 // control-flow joins, or that of the *Alloc for Ï†-nodes inserted
507 // during SSA renaming.
508 //
509 // Example printed form:
510 //      t2 = phi [0: t0, 1: t1]
511 //
512 type Phi struct {
513         register
514         Comment string  // a hint as to its purpose
515         Edges   []Value // Edges[i] is value for Block().Preds[i]
516 }
517
518 // The Call instruction represents a function or method call.
519 //
520 // The Call instruction yields the function result if there is exactly
521 // one.  Otherwise it returns a tuple, the components of which are
522 // accessed via Extract.
523 //
524 // See CallCommon for generic function call documentation.
525 //
526 // Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
527 //
528 // Example printed form:
529 //      t2 = println(t0, t1)
530 //      t4 = t3()
531 //      t7 = invoke t5.Println(...t6)
532 //
533 type Call struct {
534         register
535         Call CallCommon
536 }
537
538 // The BinOp instruction yields the result of binary operation X Op Y.
539 //
540 // Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
541 //
542 // Example printed form:
543 //      t1 = t0 + 1:int
544 //
545 type BinOp struct {
546         register
547         // One of:
548         // ADD SUB MUL QUO REM          + - * / %
549         // AND OR XOR SHL SHR AND_NOT   & | ^ << >> &^
550         // EQL NEQ LSS LEQ GTR GEQ      == != < <= < >=
551         Op   token.Token
552         X, Y Value
553 }
554
555 // The UnOp instruction yields the result of Op X.
556 // ARROW is channel receive.
557 // MUL is pointer indirection (load).
558 // XOR is bitwise complement.
559 // SUB is negation.
560 // NOT is logical negation.
561 //
562 // If CommaOk and Op=ARROW, the result is a 2-tuple of the value above
563 // and a boolean indicating the success of the receive.  The
564 // components of the tuple are accessed using Extract.
565 //
566 // Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source.
567 // For receive operations (ARROW) implicit in ranging over a channel,
568 // Pos() returns the ast.RangeStmt.For.
569 // For implicit memory loads (STAR), Pos() returns the position of the
570 // most closely associated source-level construct; the details are not
571 // specified.
572 //
573 // Example printed form:
574 //      t0 = *x
575 //      t2 = <-t1,ok
576 //
577 type UnOp struct {
578         register
579         Op      token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^
580         X       Value
581         CommaOk bool
582 }
583
584 // The ChangeType instruction applies to X a value-preserving type
585 // change to Type().
586 //
587 // Type changes are permitted:
588 //    - between a named type and its underlying type.
589 //    - between two named types of the same underlying type.
590 //    - between (possibly named) pointers to identical base types.
591 //    - from a bidirectional channel to a read- or write-channel,
592 //      optionally adding/removing a name.
593 //
594 // This operation cannot fail dynamically.
595 //
596 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose
597 // from an explicit conversion in the source.
598 //
599 // Example printed form:
600 //      t1 = changetype *int <- IntPtr (t0)
601 //
602 type ChangeType struct {
603         register
604         X Value
605 }
606
607 // The Convert instruction yields the conversion of value X to type
608 // Type().  One or both of those types is basic (but possibly named).
609 //
610 // A conversion may change the value and representation of its operand.
611 // Conversions are permitted:
612 //    - between real numeric types.
613 //    - between complex numeric types.
614 //    - between string and []byte or []rune.
615 //    - between pointers and unsafe.Pointer.
616 //    - between unsafe.Pointer and uintptr.
617 //    - from (Unicode) integer to (UTF-8) string.
618 // A conversion may imply a type name change also.
619 //
620 // This operation cannot fail dynamically.
621 //
622 // Conversions of untyped string/number/bool constants to a specific
623 // representation are eliminated during SSA construction.
624 //
625 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose
626 // from an explicit conversion in the source.
627 //
628 // Example printed form:
629 //      t1 = convert []byte <- string (t0)
630 //
631 type Convert struct {
632         register
633         X Value
634 }
635
636 // ChangeInterface constructs a value of one interface type from a
637 // value of another interface type known to be assignable to it.
638 // This operation cannot fail.
639 //
640 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from
641 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
642 // instruction arose from an explicit e.(T) operation; or token.NoPos
643 // otherwise.
644 //
645 // Example printed form:
646 //      t1 = change interface interface{} <- I (t0)
647 //
648 type ChangeInterface struct {
649         register
650         X Value
651 }
652
653 // MakeInterface constructs an instance of an interface type from a
654 // value of a concrete type.
655 //
656 // Use Program.MethodSets.MethodSet(X.Type()) to find the method-set
657 // of X, and Program.MethodValue(m) to find the implementation of a method.
658 //
659 // To construct the zero value of an interface type T, use:
660 //      NewConst(constant.MakeNil(), T, pos)
661 //
662 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose
663 // from an explicit conversion in the source.
664 //
665 // Example printed form:
666 //      t1 = make interface{} <- int (42:int)
667 //      t2 = make Stringer <- t0
668 //
669 type MakeInterface struct {
670         register
671         X Value
672 }
673
674 // The MakeClosure instruction yields a closure value whose code is
675 // Fn and whose free variables' values are supplied by Bindings.
676 //
677 // Type() returns a (possibly named) *types.Signature.
678 //
679 // Pos() returns the ast.FuncLit.Type.Func for a function literal
680 // closure or the ast.SelectorExpr.Sel for a bound method closure.
681 //
682 // Example printed form:
683 //      t0 = make closure anon@1.2 [x y z]
684 //      t1 = make closure bound$(main.I).add [i]
685 //
686 type MakeClosure struct {
687         register
688         Fn       Value   // always a *Function
689         Bindings []Value // values for each free variable in Fn.FreeVars
690 }
691
692 // The MakeMap instruction creates a new hash-table-based map object
693 // and yields a value of kind map.
694 //
695 // Type() returns a (possibly named) *types.Map.
696 //
697 // Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
698 // the ast.CompositeLit.Lbrack if created by a literal.
699 //
700 // Example printed form:
701 //      t1 = make map[string]int t0
702 //      t1 = make StringIntMap t0
703 //
704 type MakeMap struct {
705         register
706         Reserve Value // initial space reservation; nil => default
707 }
708
709 // The MakeChan instruction creates a new channel object and yields a
710 // value of kind chan.
711 //
712 // Type() returns a (possibly named) *types.Chan.
713 //
714 // Pos() returns the ast.CallExpr.Lparen for the make(chan) that
715 // created it.
716 //
717 // Example printed form:
718 //      t0 = make chan int 0
719 //      t0 = make IntChan 0
720 //
721 type MakeChan struct {
722         register
723         Size Value // int; size of buffer; zero => synchronous.
724 }
725
726 // The MakeSlice instruction yields a slice of length Len backed by a
727 // newly allocated array of length Cap.
728 //
729 // Both Len and Cap must be non-nil Values of integer type.
730 //
731 // (Alloc(types.Array) followed by Slice will not suffice because
732 // Alloc can only create arrays of constant length.)
733 //
734 // Type() returns a (possibly named) *types.Slice.
735 //
736 // Pos() returns the ast.CallExpr.Lparen for the make([]T) that
737 // created it.
738 //
739 // Example printed form:
740 //      t1 = make []string 1:int t0
741 //      t1 = make StringSlice 1:int t0
742 //
743 type MakeSlice struct {
744         register
745         Len Value
746         Cap Value
747 }
748
749 // The Slice instruction yields a slice of an existing string, slice
750 // or *array X between optional integer bounds Low and High.
751 //
752 // Dynamically, this instruction panics if X evaluates to a nil *array
753 // pointer.
754 //
755 // Type() returns string if the type of X was string, otherwise a
756 // *types.Slice with the same element type as X.
757 //
758 // Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
759 // operation, the ast.CompositeLit.Lbrace if created by a literal, or
760 // NoPos if not explicit in the source (e.g. a variadic argument slice).
761 //
762 // Example printed form:
763 //      t1 = slice t0[1:]
764 //
765 type Slice struct {
766         register
767         X              Value // slice, string, or *array
768         Low, High, Max Value // each may be nil
769 }
770
771 // The FieldAddr instruction yields the address of Field of *struct X.
772 //
773 // The field is identified by its index within the field list of the
774 // struct type of X.
775 //
776 // Dynamically, this instruction panics if X evaluates to a nil
777 // pointer.
778 //
779 // Type() returns a (possibly named) *types.Pointer.
780 //
781 // Pos() returns the position of the ast.SelectorExpr.Sel for the
782 // field, if explicit in the source.
783 //
784 // Example printed form:
785 //      t1 = &t0.name [#1]
786 //
787 type FieldAddr struct {
788         register
789         X     Value // *struct
790         Field int   // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field)
791 }
792
793 // The Field instruction yields the Field of struct X.
794 //
795 // The field is identified by its index within the field list of the
796 // struct type of X; by using numeric indices we avoid ambiguity of
797 // package-local identifiers and permit compact representations.
798 //
799 // Pos() returns the position of the ast.SelectorExpr.Sel for the
800 // field, if explicit in the source.
801 //
802 // Example printed form:
803 //      t1 = t0.name [#1]
804 //
805 type Field struct {
806         register
807         X     Value // struct
808         Field int   // index into X.Type().(*types.Struct).Fields
809 }
810
811 // The IndexAddr instruction yields the address of the element at
812 // index Index of collection X.  Index is an integer expression.
813 //
814 // The elements of maps and strings are not addressable; use Lookup or
815 // MapUpdate instead.
816 //
817 // Dynamically, this instruction panics if X evaluates to a nil *array
818 // pointer.
819 //
820 // Type() returns a (possibly named) *types.Pointer.
821 //
822 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
823 // explicit in the source.
824 //
825 // Example printed form:
826 //      t2 = &t0[t1]
827 //
828 type IndexAddr struct {
829         register
830         X     Value // slice or *array,
831         Index Value // numeric index
832 }
833
834 // The Index instruction yields element Index of array X.
835 //
836 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
837 // explicit in the source.
838 //
839 // Example printed form:
840 //      t2 = t0[t1]
841 //
842 type Index struct {
843         register
844         X     Value // array
845         Index Value // integer index
846 }
847
848 // The Lookup instruction yields element Index of collection X, a map
849 // or string.  Index is an integer expression if X is a string or the
850 // appropriate key type if X is a map.
851 //
852 // If CommaOk, the result is a 2-tuple of the value above and a
853 // boolean indicating the result of a map membership test for the key.
854 // The components of the tuple are accessed using Extract.
855 //
856 // Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
857 //
858 // Example printed form:
859 //      t2 = t0[t1]
860 //      t5 = t3[t4],ok
861 //
862 type Lookup struct {
863         register
864         X       Value // string or map
865         Index   Value // numeric or key-typed index
866         CommaOk bool  // return a value,ok pair
867 }
868
869 // SelectState is a helper for Select.
870 // It represents one goal state and its corresponding communication.
871 //
872 type SelectState struct {
873         Dir       types.ChanDir // direction of case (SendOnly or RecvOnly)
874         Chan      Value         // channel to use (for send or receive)
875         Send      Value         // value to send (for send)
876         Pos       token.Pos     // position of token.ARROW
877         DebugNode ast.Node      // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
878 }
879
880 // The Select instruction tests whether (or blocks until) one
881 // of the specified sent or received states is entered.
882 //
883 // Let n be the number of States for which Dir==RECV and T_i (0<=i<n)
884 // be the element type of each such state's Chan.
885 // Select returns an n+2-tuple
886 //    (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)
887 // The tuple's components, described below, must be accessed via the
888 // Extract instruction.
889 //
890 // If Blocking, select waits until exactly one state holds, i.e. a
891 // channel becomes ready for the designated operation of sending or
892 // receiving; select chooses one among the ready states
893 // pseudorandomly, performs the send or receive operation, and sets
894 // 'index' to the index of the chosen channel.
895 //
896 // If !Blocking, select doesn't block if no states hold; instead it
897 // returns immediately with index equal to -1.
898 //
899 // If the chosen channel was used for a receive, the r_i component is
900 // set to the received value, where i is the index of that state among
901 // all n receive states; otherwise r_i has the zero value of type T_i.
902 // Note that the receive index i is not the same as the state
903 // index index.
904 //
905 // The second component of the triple, recvOk, is a boolean whose value
906 // is true iff the selected operation was a receive and the receive
907 // successfully yielded a value.
908 //
909 // Pos() returns the ast.SelectStmt.Select.
910 //
911 // Example printed form:
912 //      t3 = select nonblocking [<-t0, t1<-t2]
913 //      t4 = select blocking []
914 //
915 type Select struct {
916         register
917         States   []*SelectState
918         Blocking bool
919 }
920
921 // The Range instruction yields an iterator over the domain and range
922 // of X, which must be a string or map.
923 //
924 // Elements are accessed via Next.
925 //
926 // Type() returns an opaque and degenerate "rangeIter" type.
927 //
928 // Pos() returns the ast.RangeStmt.For.
929 //
930 // Example printed form:
931 //      t0 = range "hello":string
932 //
933 type Range struct {
934         register
935         X Value // string or map
936 }
937
938 // The Next instruction reads and advances the (map or string)
939 // iterator Iter and returns a 3-tuple value (ok, k, v).  If the
940 // iterator is not exhausted, ok is true and k and v are the next
941 // elements of the domain and range, respectively.  Otherwise ok is
942 // false and k and v are undefined.
943 //
944 // Components of the tuple are accessed using Extract.
945 //
946 // The IsString field distinguishes iterators over strings from those
947 // over maps, as the Type() alone is insufficient: consider
948 // map[int]rune.
949 //
950 // Type() returns a *types.Tuple for the triple (ok, k, v).
951 // The types of k and/or v may be types.Invalid.
952 //
953 // Example printed form:
954 //      t1 = next t0
955 //
956 type Next struct {
957         register
958         Iter     Value
959         IsString bool // true => string iterator; false => map iterator.
960 }
961
962 // The TypeAssert instruction tests whether interface value X has type
963 // AssertedType.
964 //
965 // If !CommaOk, on success it returns v, the result of the conversion
966 // (defined below); on failure it panics.
967 //
968 // If CommaOk: on success it returns a pair (v, true) where v is the
969 // result of the conversion; on failure it returns (z, false) where z
970 // is AssertedType's zero value.  The components of the pair must be
971 // accessed using the Extract instruction.
972 //
973 // If AssertedType is a concrete type, TypeAssert checks whether the
974 // dynamic type in interface X is equal to it, and if so, the result
975 // of the conversion is a copy of the value in the interface.
976 //
977 // If AssertedType is an interface, TypeAssert checks whether the
978 // dynamic type of the interface is assignable to it, and if so, the
979 // result of the conversion is a copy of the interface value X.
980 // If AssertedType is a superinterface of X.Type(), the operation will
981 // fail iff the operand is nil.  (Contrast with ChangeInterface, which
982 // performs no nil-check.)
983 //
984 // Type() reflects the actual type of the result, possibly a
985 // 2-types.Tuple; AssertedType is the asserted type.
986 //
987 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from
988 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
989 // instruction arose from an explicit e.(T) operation; or the
990 // ast.CaseClause.Case if the instruction arose from a case of a
991 // type-switch statement.
992 //
993 // Example printed form:
994 //      t1 = typeassert t0.(int)
995 //      t3 = typeassert,ok t2.(T)
996 //
997 type TypeAssert struct {
998         register
999         X            Value
1000         AssertedType types.Type
1001         CommaOk      bool
1002 }
1003
1004 // The Extract instruction yields component Index of Tuple.
1005 //
1006 // This is used to access the results of instructions with multiple
1007 // return values, such as Call, TypeAssert, Next, UnOp(ARROW) and
1008 // IndexExpr(Map).
1009 //
1010 // Example printed form:
1011 //      t1 = extract t0 #1
1012 //
1013 type Extract struct {
1014         register
1015         Tuple Value
1016         Index int
1017 }
1018
1019 // Instructions executed for effect.  They do not yield a value. --------------------
1020
1021 // The Jump instruction transfers control to the sole successor of its
1022 // owning block.
1023 //
1024 // A Jump must be the last instruction of its containing BasicBlock.
1025 //
1026 // Pos() returns NoPos.
1027 //
1028 // Example printed form:
1029 //      jump done
1030 //
1031 type Jump struct {
1032         anInstruction
1033 }
1034
1035 // The If instruction transfers control to one of the two successors
1036 // of its owning block, depending on the boolean Cond: the first if
1037 // true, the second if false.
1038 //
1039 // An If instruction must be the last instruction of its containing
1040 // BasicBlock.
1041 //
1042 // Pos() returns NoPos.
1043 //
1044 // Example printed form:
1045 //      if t0 goto done else body
1046 //
1047 type If struct {
1048         anInstruction
1049         Cond Value
1050 }
1051
1052 // The Return instruction returns values and control back to the calling
1053 // function.
1054 //
1055 // len(Results) is always equal to the number of results in the
1056 // function's signature.
1057 //
1058 // If len(Results) > 1, Return returns a tuple value with the specified
1059 // components which the caller must access using Extract instructions.
1060 //
1061 // There is no instruction to return a ready-made tuple like those
1062 // returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or
1063 // a tail-call to a function with multiple result parameters.
1064 //
1065 // Return must be the last instruction of its containing BasicBlock.
1066 // Such a block has no successors.
1067 //
1068 // Pos() returns the ast.ReturnStmt.Return, if explicit in the source.
1069 //
1070 // Example printed form:
1071 //      return
1072 //      return nil:I, 2:int
1073 //
1074 type Return struct {
1075         anInstruction
1076         Results []Value
1077         pos     token.Pos
1078 }
1079
1080 // The RunDefers instruction pops and invokes the entire stack of
1081 // procedure calls pushed by Defer instructions in this function.
1082 //
1083 // It is legal to encounter multiple 'rundefers' instructions in a
1084 // single control-flow path through a function; this is useful in
1085 // the combined init() function, for example.
1086 //
1087 // Pos() returns NoPos.
1088 //
1089 // Example printed form:
1090 //      rundefers
1091 //
1092 type RunDefers struct {
1093         anInstruction
1094 }
1095
1096 // The Panic instruction initiates a panic with value X.
1097 //
1098 // A Panic instruction must be the last instruction of its containing
1099 // BasicBlock, which must have no successors.
1100 //
1101 // NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction;
1102 // they are treated as calls to a built-in function.
1103 //
1104 // Pos() returns the ast.CallExpr.Lparen if this panic was explicit
1105 // in the source.
1106 //
1107 // Example printed form:
1108 //      panic t0
1109 //
1110 type Panic struct {
1111         anInstruction
1112         X   Value // an interface{}
1113         pos token.Pos
1114 }
1115
1116 // The Go instruction creates a new goroutine and calls the specified
1117 // function within it.
1118 //
1119 // See CallCommon for generic function call documentation.
1120 //
1121 // Pos() returns the ast.GoStmt.Go.
1122 //
1123 // Example printed form:
1124 //      go println(t0, t1)
1125 //      go t3()
1126 //      go invoke t5.Println(...t6)
1127 //
1128 type Go struct {
1129         anInstruction
1130         Call CallCommon
1131         pos  token.Pos
1132 }
1133
1134 // The Defer instruction pushes the specified call onto a stack of
1135 // functions to be called by a RunDefers instruction or by a panic.
1136 //
1137 // See CallCommon for generic function call documentation.
1138 //
1139 // Pos() returns the ast.DeferStmt.Defer.
1140 //
1141 // Example printed form:
1142 //      defer println(t0, t1)
1143 //      defer t3()
1144 //      defer invoke t5.Println(...t6)
1145 //
1146 type Defer struct {
1147         anInstruction
1148         Call CallCommon
1149         pos  token.Pos
1150 }
1151
1152 // The Send instruction sends X on channel Chan.
1153 //
1154 // Pos() returns the ast.SendStmt.Arrow, if explicit in the source.
1155 //
1156 // Example printed form:
1157 //      send t0 <- t1
1158 //
1159 type Send struct {
1160         anInstruction
1161         Chan, X Value
1162         pos     token.Pos
1163 }
1164
1165 // The Store instruction stores Val at address Addr.
1166 // Stores can be of arbitrary types.
1167 //
1168 // Pos() returns the position of the source-level construct most closely
1169 // associated with the memory store operation.
1170 // Since implicit memory stores are numerous and varied and depend upon
1171 // implementation choices, the details are not specified.
1172 //
1173 // Example printed form:
1174 //      *x = y
1175 //
1176 type Store struct {
1177         anInstruction
1178         Addr Value
1179         Val  Value
1180         pos  token.Pos
1181 }
1182
1183 // The MapUpdate instruction updates the association of Map[Key] to
1184 // Value.
1185 //
1186 // Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
1187 // if explicit in the source.
1188 //
1189 // Example printed form:
1190 //      t0[t1] = t2
1191 //
1192 type MapUpdate struct {
1193         anInstruction
1194         Map   Value
1195         Key   Value
1196         Value Value
1197         pos   token.Pos
1198 }
1199
1200 // A DebugRef instruction maps a source-level expression Expr to the
1201 // SSA value X that represents the value (!IsAddr) or address (IsAddr)
1202 // of that expression.
1203 //
1204 // DebugRef is a pseudo-instruction: it has no dynamic effect.
1205 //
1206 // Pos() returns Expr.Pos(), the start position of the source-level
1207 // expression.  This is not the same as the "designated" token as
1208 // documented at Value.Pos(). e.g. CallExpr.Pos() does not return the
1209 // position of the ("designated") Lparen token.
1210 //
1211 // If Expr is an *ast.Ident denoting a var or func, Object() returns
1212 // the object; though this information can be obtained from the type
1213 // checker, including it here greatly facilitates debugging.
1214 // For non-Ident expressions, Object() returns nil.
1215 //
1216 // DebugRefs are generated only for functions built with debugging
1217 // enabled; see Package.SetDebugMode() and the GlobalDebug builder
1218 // mode flag.
1219 //
1220 // DebugRefs are not emitted for ast.Idents referring to constants or
1221 // predeclared identifiers, since they are trivial and numerous.
1222 // Nor are they emitted for ast.ParenExprs.
1223 //
1224 // (By representing these as instructions, rather than out-of-band,
1225 // consistency is maintained during transformation passes by the
1226 // ordinary SSA renaming machinery.)
1227 //
1228 // Example printed form:
1229 //      ; *ast.CallExpr @ 102:9 is t5
1230 //      ; var x float64 @ 109:72 is x
1231 //      ; address of *ast.CompositeLit @ 216:10 is t0
1232 //
1233 type DebugRef struct {
1234         anInstruction
1235         Expr   ast.Expr     // the referring expression (never *ast.ParenExpr)
1236         object types.Object // the identity of the source var/func
1237         IsAddr bool         // Expr is addressable and X is the address it denotes
1238         X      Value        // the value or address of Expr
1239 }
1240
1241 // Embeddable mix-ins and helpers for common parts of other structs. -----------
1242
1243 // register is a mix-in embedded by all SSA values that are also
1244 // instructions, i.e. virtual registers, and provides a uniform
1245 // implementation of most of the Value interface: Value.Name() is a
1246 // numbered register (e.g. "t0"); the other methods are field accessors.
1247 //
1248 // Temporary names are automatically assigned to each register on
1249 // completion of building a function in SSA form.
1250 //
1251 // Clients must not assume that the 'id' value (and the Name() derived
1252 // from it) is unique within a function.  As always in this API,
1253 // semantics are determined only by identity; names exist only to
1254 // facilitate debugging.
1255 //
1256 type register struct {
1257         anInstruction
1258         num       int        // "name" of virtual register, e.g. "t0".  Not guaranteed unique.
1259         typ       types.Type // type of virtual register
1260         pos       token.Pos  // position of source expression, or NoPos
1261         referrers []Instruction
1262 }
1263
1264 // anInstruction is a mix-in embedded by all Instructions.
1265 // It provides the implementations of the Block and setBlock methods.
1266 type anInstruction struct {
1267         block *BasicBlock // the basic block of this instruction
1268 }
1269
1270 // CallCommon is contained by Go, Defer and Call to hold the
1271 // common parts of a function or method call.
1272 //
1273 // Each CallCommon exists in one of two modes, function call and
1274 // interface method invocation, or "call" and "invoke" for short.
1275 //
1276 // 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon
1277 // represents an ordinary function call of the value in Value,
1278 // which may be a *Builtin, a *Function or any other value of kind
1279 // 'func'.
1280 //
1281 // Value may be one of:
1282 //    (a) a *Function, indicating a statically dispatched call
1283 //        to a package-level function, an anonymous function, or
1284 //        a method of a named type.
1285 //    (b) a *MakeClosure, indicating an immediately applied
1286 //        function literal with free variables.
1287 //    (c) a *Builtin, indicating a statically dispatched call
1288 //        to a built-in function.
1289 //    (d) any other value, indicating a dynamically dispatched
1290 //        function call.
1291 // StaticCallee returns the identity of the callee in cases
1292 // (a) and (b), nil otherwise.
1293 //
1294 // Args contains the arguments to the call.  If Value is a method,
1295 // Args[0] contains the receiver parameter.
1296 //
1297 // Example printed form:
1298 //      t2 = println(t0, t1)
1299 //      go t3()
1300 //      defer t5(...t6)
1301 //
1302 // 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon
1303 // represents a dynamically dispatched call to an interface method.
1304 // In this mode, Value is the interface value and Method is the
1305 // interface's abstract method.  Note: an abstract method may be
1306 // shared by multiple interfaces due to embedding; Value.Type()
1307 // provides the specific interface used for this call.
1308 //
1309 // Value is implicitly supplied to the concrete method implementation
1310 // as the receiver parameter; in other words, Args[0] holds not the
1311 // receiver but the first true argument.
1312 //
1313 // Example printed form:
1314 //      t1 = invoke t0.String()
1315 //      go invoke t3.Run(t2)
1316 //      defer invoke t4.Handle(...t5)
1317 //
1318 // For all calls to variadic functions (Signature().Variadic()),
1319 // the last element of Args is a slice.
1320 //
1321 type CallCommon struct {
1322         Value  Value       // receiver (invoke mode) or func value (call mode)
1323         Method *types.Func // abstract method (invoke mode)
1324         Args   []Value     // actual parameters (in static method call, includes receiver)
1325         pos    token.Pos   // position of CallExpr.Lparen, iff explicit in source
1326 }
1327
1328 // IsInvoke returns true if this call has "invoke" (not "call") mode.
1329 func (c *CallCommon) IsInvoke() bool {
1330         return c.Method != nil
1331 }
1332
1333 func (c *CallCommon) Pos() token.Pos { return c.pos }
1334
1335 // Signature returns the signature of the called function.
1336 //
1337 // For an "invoke"-mode call, the signature of the interface method is
1338 // returned.
1339 //
1340 // In either "call" or "invoke" mode, if the callee is a method, its
1341 // receiver is represented by sig.Recv, not sig.Params().At(0).
1342 //
1343 func (c *CallCommon) Signature() *types.Signature {
1344         if c.Method != nil {
1345                 return c.Method.Type().(*types.Signature)
1346         }
1347         return c.Value.Type().Underlying().(*types.Signature)
1348 }
1349
1350 // StaticCallee returns the callee if this is a trivially static
1351 // "call"-mode call to a function.
1352 func (c *CallCommon) StaticCallee() *Function {
1353         switch fn := c.Value.(type) {
1354         case *Function:
1355                 return fn
1356         case *MakeClosure:
1357                 return fn.Fn.(*Function)
1358         }
1359         return nil
1360 }
1361
1362 // Description returns a description of the mode of this call suitable
1363 // for a user interface, e.g., "static method call".
1364 func (c *CallCommon) Description() string {
1365         switch fn := c.Value.(type) {
1366         case *Builtin:
1367                 return "built-in function call"
1368         case *MakeClosure:
1369                 return "static function closure call"
1370         case *Function:
1371                 if fn.Signature.Recv() != nil {
1372                         return "static method call"
1373                 }
1374                 return "static function call"
1375         }
1376         if c.IsInvoke() {
1377                 return "dynamic method call" // ("invoke" mode)
1378         }
1379         return "dynamic function call"
1380 }
1381
1382 // The CallInstruction interface, implemented by *Go, *Defer and *Call,
1383 // exposes the common parts of function-calling instructions,
1384 // yet provides a way back to the Value defined by *Call alone.
1385 //
1386 type CallInstruction interface {
1387         Instruction
1388         Common() *CallCommon // returns the common parts of the call
1389         Value() *Call        // returns the result value of the call (*Call) or nil (*Go, *Defer)
1390 }
1391
1392 func (s *Call) Common() *CallCommon  { return &s.Call }
1393 func (s *Defer) Common() *CallCommon { return &s.Call }
1394 func (s *Go) Common() *CallCommon    { return &s.Call }
1395
1396 func (s *Call) Value() *Call  { return s }
1397 func (s *Defer) Value() *Call { return nil }
1398 func (s *Go) Value() *Call    { return nil }
1399
1400 func (v *Builtin) Type() types.Type        { return v.sig }
1401 func (v *Builtin) Name() string            { return v.name }
1402 func (*Builtin) Referrers() *[]Instruction { return nil }
1403 func (v *Builtin) Pos() token.Pos          { return token.NoPos }
1404 func (v *Builtin) Object() types.Object    { return types.Universe.Lookup(v.name) }
1405 func (v *Builtin) Parent() *Function       { return nil }
1406
1407 func (v *FreeVar) Type() types.Type          { return v.typ }
1408 func (v *FreeVar) Name() string              { return v.name }
1409 func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers }
1410 func (v *FreeVar) Pos() token.Pos            { return v.pos }
1411 func (v *FreeVar) Parent() *Function         { return v.parent }
1412
1413 func (v *Global) Type() types.Type                     { return v.typ }
1414 func (v *Global) Name() string                         { return v.name }
1415 func (v *Global) Parent() *Function                    { return nil }
1416 func (v *Global) Pos() token.Pos                       { return v.pos }
1417 func (v *Global) Referrers() *[]Instruction            { return nil }
1418 func (v *Global) Token() token.Token                   { return token.VAR }
1419 func (v *Global) Object() types.Object                 { return v.object }
1420 func (v *Global) String() string                       { return v.RelString(nil) }
1421 func (v *Global) Package() *Package                    { return v.Pkg }
1422 func (v *Global) RelString(from *types.Package) string { return relString(v, from) }
1423
1424 func (v *Function) Name() string         { return v.name }
1425 func (v *Function) Type() types.Type     { return v.Signature }
1426 func (v *Function) Pos() token.Pos       { return v.pos }
1427 func (v *Function) Token() token.Token   { return token.FUNC }
1428 func (v *Function) Object() types.Object { return v.object }
1429 func (v *Function) String() string       { return v.RelString(nil) }
1430 func (v *Function) Package() *Package    { return v.Pkg }
1431 func (v *Function) Parent() *Function    { return v.parent }
1432 func (v *Function) Referrers() *[]Instruction {
1433         if v.parent != nil {
1434                 return &v.referrers
1435         }
1436         return nil
1437 }
1438
1439 func (v *Parameter) Type() types.Type          { return v.typ }
1440 func (v *Parameter) Name() string              { return v.name }
1441 func (v *Parameter) Object() types.Object      { return v.object }
1442 func (v *Parameter) Referrers() *[]Instruction { return &v.referrers }
1443 func (v *Parameter) Pos() token.Pos            { return v.pos }
1444 func (v *Parameter) Parent() *Function         { return v.parent }
1445
1446 func (v *Alloc) Type() types.Type          { return v.typ }
1447 func (v *Alloc) Referrers() *[]Instruction { return &v.referrers }
1448 func (v *Alloc) Pos() token.Pos            { return v.pos }
1449
1450 func (v *register) Type() types.Type          { return v.typ }
1451 func (v *register) setType(typ types.Type)    { v.typ = typ }
1452 func (v *register) Name() string              { return fmt.Sprintf("t%d", v.num) }
1453 func (v *register) setNum(num int)            { v.num = num }
1454 func (v *register) Referrers() *[]Instruction { return &v.referrers }
1455 func (v *register) Pos() token.Pos            { return v.pos }
1456 func (v *register) setPos(pos token.Pos)      { v.pos = pos }
1457
1458 func (v *anInstruction) Parent() *Function          { return v.block.parent }
1459 func (v *anInstruction) Block() *BasicBlock         { return v.block }
1460 func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block }
1461 func (v *anInstruction) Referrers() *[]Instruction  { return nil }
1462
1463 func (t *Type) Name() string                         { return t.object.Name() }
1464 func (t *Type) Pos() token.Pos                       { return t.object.Pos() }
1465 func (t *Type) Type() types.Type                     { return t.object.Type() }
1466 func (t *Type) Token() token.Token                   { return token.TYPE }
1467 func (t *Type) Object() types.Object                 { return t.object }
1468 func (t *Type) String() string                       { return t.RelString(nil) }
1469 func (t *Type) Package() *Package                    { return t.pkg }
1470 func (t *Type) RelString(from *types.Package) string { return relString(t, from) }
1471
1472 func (c *NamedConst) Name() string                         { return c.object.Name() }
1473 func (c *NamedConst) Pos() token.Pos                       { return c.object.Pos() }
1474 func (c *NamedConst) String() string                       { return c.RelString(nil) }
1475 func (c *NamedConst) Type() types.Type                     { return c.object.Type() }
1476 func (c *NamedConst) Token() token.Token                   { return token.CONST }
1477 func (c *NamedConst) Object() types.Object                 { return c.object }
1478 func (c *NamedConst) Package() *Package                    { return c.pkg }
1479 func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) }
1480
1481 func (d *DebugRef) Object() types.Object { return d.object }
1482
1483 // Func returns the package-level function of the specified name,
1484 // or nil if not found.
1485 //
1486 func (p *Package) Func(name string) (f *Function) {
1487         f, _ = p.Members[name].(*Function)
1488         return
1489 }
1490
1491 // Var returns the package-level variable of the specified name,
1492 // or nil if not found.
1493 //
1494 func (p *Package) Var(name string) (g *Global) {
1495         g, _ = p.Members[name].(*Global)
1496         return
1497 }
1498
1499 // Const returns the package-level constant of the specified name,
1500 // or nil if not found.
1501 //
1502 func (p *Package) Const(name string) (c *NamedConst) {
1503         c, _ = p.Members[name].(*NamedConst)
1504         return
1505 }
1506
1507 // Type returns the package-level type of the specified name,
1508 // or nil if not found.
1509 //
1510 func (p *Package) Type(name string) (t *Type) {
1511         t, _ = p.Members[name].(*Type)
1512         return
1513 }
1514
1515 func (v *Call) Pos() token.Pos      { return v.Call.pos }
1516 func (s *Defer) Pos() token.Pos     { return s.pos }
1517 func (s *Go) Pos() token.Pos        { return s.pos }
1518 func (s *MapUpdate) Pos() token.Pos { return s.pos }
1519 func (s *Panic) Pos() token.Pos     { return s.pos }
1520 func (s *Return) Pos() token.Pos    { return s.pos }
1521 func (s *Send) Pos() token.Pos      { return s.pos }
1522 func (s *Store) Pos() token.Pos     { return s.pos }
1523 func (s *If) Pos() token.Pos        { return token.NoPos }
1524 func (s *Jump) Pos() token.Pos      { return token.NoPos }
1525 func (s *RunDefers) Pos() token.Pos { return token.NoPos }
1526 func (s *DebugRef) Pos() token.Pos  { return s.Expr.Pos() }
1527
1528 // Operands.
1529
1530 func (v *Alloc) Operands(rands []*Value) []*Value {
1531         return rands
1532 }
1533
1534 func (v *BinOp) Operands(rands []*Value) []*Value {
1535         return append(rands, &v.X, &v.Y)
1536 }
1537
1538 func (c *CallCommon) Operands(rands []*Value) []*Value {
1539         rands = append(rands, &c.Value)
1540         for i := range c.Args {
1541                 rands = append(rands, &c.Args[i])
1542         }
1543         return rands
1544 }
1545
1546 func (s *Go) Operands(rands []*Value) []*Value {
1547         return s.Call.Operands(rands)
1548 }
1549
1550 func (s *Call) Operands(rands []*Value) []*Value {
1551         return s.Call.Operands(rands)
1552 }
1553
1554 func (s *Defer) Operands(rands []*Value) []*Value {
1555         return s.Call.Operands(rands)
1556 }
1557
1558 func (v *ChangeInterface) Operands(rands []*Value) []*Value {
1559         return append(rands, &v.X)
1560 }
1561
1562 func (v *ChangeType) Operands(rands []*Value) []*Value {
1563         return append(rands, &v.X)
1564 }
1565
1566 func (v *Convert) Operands(rands []*Value) []*Value {
1567         return append(rands, &v.X)
1568 }
1569
1570 func (s *DebugRef) Operands(rands []*Value) []*Value {
1571         return append(rands, &s.X)
1572 }
1573
1574 func (v *Extract) Operands(rands []*Value) []*Value {
1575         return append(rands, &v.Tuple)
1576 }
1577
1578 func (v *Field) Operands(rands []*Value) []*Value {
1579         return append(rands, &v.X)
1580 }
1581
1582 func (v *FieldAddr) Operands(rands []*Value) []*Value {
1583         return append(rands, &v.X)
1584 }
1585
1586 func (s *If) Operands(rands []*Value) []*Value {
1587         return append(rands, &s.Cond)
1588 }
1589
1590 func (v *Index) Operands(rands []*Value) []*Value {
1591         return append(rands, &v.X, &v.Index)
1592 }
1593
1594 func (v *IndexAddr) Operands(rands []*Value) []*Value {
1595         return append(rands, &v.X, &v.Index)
1596 }
1597
1598 func (*Jump) Operands(rands []*Value) []*Value {
1599         return rands
1600 }
1601
1602 func (v *Lookup) Operands(rands []*Value) []*Value {
1603         return append(rands, &v.X, &v.Index)
1604 }
1605
1606 func (v *MakeChan) Operands(rands []*Value) []*Value {
1607         return append(rands, &v.Size)
1608 }
1609
1610 func (v *MakeClosure) Operands(rands []*Value) []*Value {
1611         rands = append(rands, &v.Fn)
1612         for i := range v.Bindings {
1613                 rands = append(rands, &v.Bindings[i])
1614         }
1615         return rands
1616 }
1617
1618 func (v *MakeInterface) Operands(rands []*Value) []*Value {
1619         return append(rands, &v.X)
1620 }
1621
1622 func (v *MakeMap) Operands(rands []*Value) []*Value {
1623         return append(rands, &v.Reserve)
1624 }
1625
1626 func (v *MakeSlice) Operands(rands []*Value) []*Value {
1627         return append(rands, &v.Len, &v.Cap)
1628 }
1629
1630 func (v *MapUpdate) Operands(rands []*Value) []*Value {
1631         return append(rands, &v.Map, &v.Key, &v.Value)
1632 }
1633
1634 func (v *Next) Operands(rands []*Value) []*Value {
1635         return append(rands, &v.Iter)
1636 }
1637
1638 func (s *Panic) Operands(rands []*Value) []*Value {
1639         return append(rands, &s.X)
1640 }
1641
1642 func (v *Phi) Operands(rands []*Value) []*Value {
1643         for i := range v.Edges {
1644                 rands = append(rands, &v.Edges[i])
1645         }
1646         return rands
1647 }
1648
1649 func (v *Range) Operands(rands []*Value) []*Value {
1650         return append(rands, &v.X)
1651 }
1652
1653 func (s *Return) Operands(rands []*Value) []*Value {
1654         for i := range s.Results {
1655                 rands = append(rands, &s.Results[i])
1656         }
1657         return rands
1658 }
1659
1660 func (*RunDefers) Operands(rands []*Value) []*Value {
1661         return rands
1662 }
1663
1664 func (v *Select) Operands(rands []*Value) []*Value {
1665         for i := range v.States {
1666                 rands = append(rands, &v.States[i].Chan, &v.States[i].Send)
1667         }
1668         return rands
1669 }
1670
1671 func (s *Send) Operands(rands []*Value) []*Value {
1672         return append(rands, &s.Chan, &s.X)
1673 }
1674
1675 func (v *Slice) Operands(rands []*Value) []*Value {
1676         return append(rands, &v.X, &v.Low, &v.High, &v.Max)
1677 }
1678
1679 func (s *Store) Operands(rands []*Value) []*Value {
1680         return append(rands, &s.Addr, &s.Val)
1681 }
1682
1683 func (v *TypeAssert) Operands(rands []*Value) []*Value {
1684         return append(rands, &v.X)
1685 }
1686
1687 func (v *UnOp) Operands(rands []*Value) []*Value {
1688         return append(rands, &v.X)
1689 }
1690
1691 // Non-Instruction Values:
1692 func (v *Builtin) Operands(rands []*Value) []*Value   { return rands }
1693 func (v *FreeVar) Operands(rands []*Value) []*Value   { return rands }
1694 func (v *Const) Operands(rands []*Value) []*Value     { return rands }
1695 func (v *Function) Operands(rands []*Value) []*Value  { return rands }
1696 func (v *Global) Operands(rands []*Value) []*Value    { return rands }
1697 func (v *Parameter) Operands(rands []*Value) []*Value { return rands }