github.com/pankona/gometalinter@v2.0.11+incompatible/_linters/src/golang.org/x/tools/go/ssa/ssa.go (about) 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 exact "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 exact value of the constant, independent of its 408 // Type(), using the same representation as package go/exact uses for 409 // constants, or nil for a typed nil value. 410 // 411 // Pos() returns token.NoPos. 412 // 413 // Example printed form: 414 // 42:int 415 // "hello":untyped string 416 // 3+4i:MyComplex 417 // 418 type Const struct { 419 typ types.Type 420 Value exact.Value 421 } 422 423 // A Global is a named Value holding the address of a package-level 424 // variable. 425 // 426 // Pos() returns the position of the ast.ValueSpec.Names[*] 427 // identifier. 428 // 429 type Global struct { 430 name string 431 object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard 432 typ types.Type 433 pos token.Pos 434 435 Pkg *Package 436 } 437 438 // A Builtin represents a specific use of a built-in function, e.g. len. 439 // 440 // Builtins are immutable values. Builtins do not have addresses. 441 // Builtins can only appear in CallCommon.Func. 442 // 443 // Name() indicates the function: one of the built-in functions from the 444 // Go spec (excluding "make" and "new") or one of these ssa-defined 445 // intrinsics: 446 // 447 // // wrapnilchk returns ptr if non-nil, panics otherwise. 448 // // (For use in indirection wrappers.) 449 // func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T 450 // 451 // Object() returns a *types.Builtin for built-ins defined by the spec, 452 // nil for others. 453 // 454 // Type() returns a *types.Signature representing the effective 455 // signature of the built-in for this call. 456 // 457 type Builtin struct { 458 name string 459 sig *types.Signature 460 } 461 462 // Value-defining instructions ---------------------------------------- 463 464 // The Alloc instruction reserves space for a variable of the given type, 465 // zero-initializes it, and yields its address. 466 // 467 // Alloc values are always addresses, and have pointer types, so the 468 // type of the allocated variable is actually 469 // Type().Underlying().(*types.Pointer).Elem(). 470 // 471 // If Heap is false, Alloc allocates space in the function's 472 // activation record (frame); we refer to an Alloc(Heap=false) as a 473 // "local" alloc. Each local Alloc returns the same address each time 474 // it is executed within the same activation; the space is 475 // re-initialized to zero. 476 // 477 // If Heap is true, Alloc allocates space in the heap; we 478 // refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc 479 // returns a different address each time it is executed. 480 // 481 // When Alloc is applied to a channel, map or slice type, it returns 482 // the address of an uninitialized (nil) reference of that kind; store 483 // the result of MakeSlice, MakeMap or MakeChan in that location to 484 // instantiate these types. 485 // 486 // Pos() returns the ast.CompositeLit.Lbrace for a composite literal, 487 // or the ast.CallExpr.Rparen for a call to new() or for a call that 488 // allocates a varargs slice. 489 // 490 // Example printed form: 491 // t0 = local int 492 // t1 = new int 493 // 494 type Alloc struct { 495 register 496 Comment string 497 Heap bool 498 index int // dense numbering; for lifting 499 } 500 501 // The Phi instruction represents an SSA φ-node, which combines values 502 // that differ across incoming control-flow edges and yields a new 503 // value. Within a block, all φ-nodes must appear before all non-φ 504 // nodes. 505 // 506 // Pos() returns the position of the && or || for short-circuit 507 // control-flow joins, or that of the *Alloc for φ-nodes inserted 508 // during SSA renaming. 509 // 510 // Example printed form: 511 // t2 = phi [0: t0, 1: t1] 512 // 513 type Phi struct { 514 register 515 Comment string // a hint as to its purpose 516 Edges []Value // Edges[i] is value for Block().Preds[i] 517 } 518 519 // The Call instruction represents a function or method call. 520 // 521 // The Call instruction yields the function result if there is exactly 522 // one. Otherwise it returns a tuple, the components of which are 523 // accessed via Extract. 524 // 525 // See CallCommon for generic function call documentation. 526 // 527 // Pos() returns the ast.CallExpr.Lparen, if explicit in the source. 528 // 529 // Example printed form: 530 // t2 = println(t0, t1) 531 // t4 = t3() 532 // t7 = invoke t5.Println(...t6) 533 // 534 type Call struct { 535 register 536 Call CallCommon 537 } 538 539 // The BinOp instruction yields the result of binary operation X Op Y. 540 // 541 // Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source. 542 // 543 // Example printed form: 544 // t1 = t0 + 1:int 545 // 546 type BinOp struct { 547 register 548 // One of: 549 // ADD SUB MUL QUO REM + - * / % 550 // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~ 551 // EQL LSS GTR NEQ LEQ GEQ == != < <= < >= 552 Op token.Token 553 X, Y Value 554 } 555 556 // The UnOp instruction yields the result of Op X. 557 // ARROW is channel receive. 558 // MUL is pointer indirection (load). 559 // XOR is bitwise complement. 560 // SUB is negation. 561 // NOT is logical negation. 562 // 563 // If CommaOk and Op=ARROW, the result is a 2-tuple of the value above 564 // and a boolean indicating the success of the receive. The 565 // components of the tuple are accessed using Extract. 566 // 567 // Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source. 568 // For receive operations (ARROW) implicit in ranging over a channel, 569 // Pos() returns the ast.RangeStmt.For. 570 // For implicit memory loads (STAR), Pos() returns the position of the 571 // most closely associated source-level construct; the details are not 572 // specified. 573 // 574 // Example printed form: 575 // t0 = *x 576 // t2 = <-t1,ok 577 // 578 type UnOp struct { 579 register 580 Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^ 581 X Value 582 CommaOk bool 583 } 584 585 // The ChangeType instruction applies to X a value-preserving type 586 // change to Type(). 587 // 588 // Type changes are permitted: 589 // - between a named type and its underlying type. 590 // - between two named types of the same underlying type. 591 // - between (possibly named) pointers to identical base types. 592 // - from a bidirectional channel to a read- or write-channel, 593 // optionally adding/removing a name. 594 // 595 // This operation cannot fail dynamically. 596 // 597 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 598 // from an explicit conversion in the source. 599 // 600 // Example printed form: 601 // t1 = changetype *int <- IntPtr (t0) 602 // 603 type ChangeType struct { 604 register 605 X Value 606 } 607 608 // The Convert instruction yields the conversion of value X to type 609 // Type(). One or both of those types is basic (but possibly named). 610 // 611 // A conversion may change the value and representation of its operand. 612 // Conversions are permitted: 613 // - between real numeric types. 614 // - between complex numeric types. 615 // - between string and []byte or []rune. 616 // - between pointers and unsafe.Pointer. 617 // - between unsafe.Pointer and uintptr. 618 // - from (Unicode) integer to (UTF-8) string. 619 // A conversion may imply a type name change also. 620 // 621 // This operation cannot fail dynamically. 622 // 623 // Conversions of untyped string/number/bool constants to a specific 624 // representation are eliminated during SSA construction. 625 // 626 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 627 // from an explicit conversion in the source. 628 // 629 // Example printed form: 630 // t1 = convert []byte <- string (t0) 631 // 632 type Convert struct { 633 register 634 X Value 635 } 636 637 // ChangeInterface constructs a value of one interface type from a 638 // value of another interface type known to be assignable to it. 639 // This operation cannot fail. 640 // 641 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from 642 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the 643 // instruction arose from an explicit e.(T) operation; or token.NoPos 644 // otherwise. 645 // 646 // Example printed form: 647 // t1 = change interface interface{} <- I (t0) 648 // 649 type ChangeInterface struct { 650 register 651 X Value 652 } 653 654 // MakeInterface constructs an instance of an interface type from a 655 // value of a concrete type. 656 // 657 // Use Program.MethodSets.MethodSet(X.Type()) to find the method-set 658 // of X, and Program.Method(m) to find the implementation of a method. 659 // 660 // To construct the zero value of an interface type T, use: 661 // NewConst(exact.MakeNil(), T, pos) 662 // 663 // Pos() returns the ast.CallExpr.Lparen, if the instruction arose 664 // from an explicit conversion in the source. 665 // 666 // Example printed form: 667 // t1 = make interface{} <- int (42:int) 668 // t2 = make Stringer <- t0 669 // 670 type MakeInterface struct { 671 register 672 X Value 673 } 674 675 // The MakeClosure instruction yields a closure value whose code is 676 // Fn and whose free variables' values are supplied by Bindings. 677 // 678 // Type() returns a (possibly named) *types.Signature. 679 // 680 // Pos() returns the ast.FuncLit.Type.Func for a function literal 681 // closure or the ast.SelectorExpr.Sel for a bound method closure. 682 // 683 // Example printed form: 684 // t0 = make closure anon@1.2 [x y z] 685 // t1 = make closure bound$(main.I).add [i] 686 // 687 type MakeClosure struct { 688 register 689 Fn Value // always a *Function 690 Bindings []Value // values for each free variable in Fn.FreeVars 691 } 692 693 // The MakeMap instruction creates a new hash-table-based map object 694 // and yields a value of kind map. 695 // 696 // Type() returns a (possibly named) *types.Map. 697 // 698 // Pos() returns the ast.CallExpr.Lparen, if created by make(map), or 699 // the ast.CompositeLit.Lbrack if created by a literal. 700 // 701 // Example printed form: 702 // t1 = make map[string]int t0 703 // t1 = make StringIntMap t0 704 // 705 type MakeMap struct { 706 register 707 Reserve Value // initial space reservation; nil => default 708 } 709 710 // The MakeChan instruction creates a new channel object and yields a 711 // value of kind chan. 712 // 713 // Type() returns a (possibly named) *types.Chan. 714 // 715 // Pos() returns the ast.CallExpr.Lparen for the make(chan) that 716 // created it. 717 // 718 // Example printed form: 719 // t0 = make chan int 0 720 // t0 = make IntChan 0 721 // 722 type MakeChan struct { 723 register 724 Size Value // int; size of buffer; zero => synchronous. 725 } 726 727 // The MakeSlice instruction yields a slice of length Len backed by a 728 // newly allocated array of length Cap. 729 // 730 // Both Len and Cap must be non-nil Values of integer type. 731 // 732 // (Alloc(types.Array) followed by Slice will not suffice because 733 // Alloc can only create arrays of constant length.) 734 // 735 // Type() returns a (possibly named) *types.Slice. 736 // 737 // Pos() returns the ast.CallExpr.Lparen for the make([]T) that 738 // created it. 739 // 740 // Example printed form: 741 // t1 = make []string 1:int t0 742 // t1 = make StringSlice 1:int t0 743 // 744 type MakeSlice struct { 745 register 746 Len Value 747 Cap Value 748 } 749 750 // The Slice instruction yields a slice of an existing string, slice 751 // or *array X between optional integer bounds Low and High. 752 // 753 // Dynamically, this instruction panics if X evaluates to a nil *array 754 // pointer. 755 // 756 // Type() returns string if the type of X was string, otherwise a 757 // *types.Slice with the same element type as X. 758 // 759 // Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice 760 // operation, the ast.CompositeLit.Lbrace if created by a literal, or 761 // NoPos if not explicit in the source (e.g. a variadic argument slice). 762 // 763 // Example printed form: 764 // t1 = slice t0[1:] 765 // 766 type Slice struct { 767 register 768 X Value // slice, string, or *array 769 Low, High, Max Value // each may be nil 770 } 771 772 // The FieldAddr instruction yields the address of Field of *struct X. 773 // 774 // The field is identified by its index within the field list of the 775 // struct type of X. 776 // 777 // Dynamically, this instruction panics if X evaluates to a nil 778 // pointer. 779 // 780 // Type() returns a (possibly named) *types.Pointer. 781 // 782 // Pos() returns the position of the ast.SelectorExpr.Sel for the 783 // field, if explicit in the source. 784 // 785 // Example printed form: 786 // t1 = &t0.name [#1] 787 // 788 type FieldAddr struct { 789 register 790 X Value // *struct 791 Field int // index into X.Type().Deref().(*types.Struct).Fields 792 } 793 794 // The Field instruction yields the Field of struct X. 795 // 796 // The field is identified by its index within the field list of the 797 // struct type of X; by using numeric indices we avoid ambiguity of 798 // package-local identifiers and permit compact representations. 799 // 800 // Pos() returns the position of the ast.SelectorExpr.Sel for the 801 // field, if explicit in the source. 802 // 803 // Example printed form: 804 // t1 = t0.name [#1] 805 // 806 type Field struct { 807 register 808 X Value // struct 809 Field int // index into X.Type().(*types.Struct).Fields 810 } 811 812 // The IndexAddr instruction yields the address of the element at 813 // index Index of collection X. Index is an integer expression. 814 // 815 // The elements of maps and strings are not addressable; use Lookup or 816 // MapUpdate instead. 817 // 818 // Dynamically, this instruction panics if X evaluates to a nil *array 819 // pointer. 820 // 821 // Type() returns a (possibly named) *types.Pointer. 822 // 823 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if 824 // explicit in the source. 825 // 826 // Example printed form: 827 // t2 = &t0[t1] 828 // 829 type IndexAddr struct { 830 register 831 X Value // slice or *array, 832 Index Value // numeric index 833 } 834 835 // The Index instruction yields element Index of array X. 836 // 837 // Pos() returns the ast.IndexExpr.Lbrack for the index operation, if 838 // explicit in the source. 839 // 840 // Example printed form: 841 // t2 = t0[t1] 842 // 843 type Index struct { 844 register 845 X Value // array 846 Index Value // integer index 847 } 848 849 // The Lookup instruction yields element Index of collection X, a map 850 // or string. Index is an integer expression if X is a string or the 851 // appropriate key type if X is a map. 852 // 853 // If CommaOk, the result is a 2-tuple of the value above and a 854 // boolean indicating the result of a map membership test for the key. 855 // The components of the tuple are accessed using Extract. 856 // 857 // Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. 858 // 859 // Example printed form: 860 // t2 = t0[t1] 861 // t5 = t3[t4],ok 862 // 863 type Lookup struct { 864 register 865 X Value // string or map 866 Index Value // numeric or key-typed index 867 CommaOk bool // return a value,ok pair 868 } 869 870 // SelectState is a helper for Select. 871 // It represents one goal state and its corresponding communication. 872 // 873 type SelectState struct { 874 Dir types.ChanDir // direction of case (SendOnly or RecvOnly) 875 Chan Value // channel to use (for send or receive) 876 Send Value // value to send (for send) 877 Pos token.Pos // position of token.ARROW 878 DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode] 879 } 880 881 // The Select instruction tests whether (or blocks until) one 882 // of the specified sent or received states is entered. 883 // 884 // Let n be the number of States for which Dir==RECV and T_i (0<=i<n) 885 // be the element type of each such state's Chan. 886 // Select returns an n+2-tuple 887 // (index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1) 888 // The tuple's components, described below, must be accessed via the 889 // Extract instruction. 890 // 891 // If Blocking, select waits until exactly one state holds, i.e. a 892 // channel becomes ready for the designated operation of sending or 893 // receiving; select chooses one among the ready states 894 // pseudorandomly, performs the send or receive operation, and sets 895 // 'index' to the index of the chosen channel. 896 // 897 // If !Blocking, select doesn't block if no states hold; instead it 898 // returns immediately with index equal to -1. 899 // 900 // If the chosen channel was used for a receive, the r_i component is 901 // set to the received value, where i is the index of that state among 902 // all n receive states; otherwise r_i has the zero value of type T_i. 903 // Note that the receive index i is not the same as the state 904 // index index. 905 // 906 // The second component of the triple, recvOk, is a boolean whose value 907 // is true iff the selected operation was a receive and the receive 908 // successfully yielded a value. 909 // 910 // Pos() returns the ast.SelectStmt.Select. 911 // 912 // Example printed form: 913 // t3 = select nonblocking [<-t0, t1<-t2] 914 // t4 = select blocking [] 915 // 916 type Select struct { 917 register 918 States []*SelectState 919 Blocking bool 920 } 921 922 // The Range instruction yields an iterator over the domain and range 923 // of X, which must be a string or map. 924 // 925 // Elements are accessed via Next. 926 // 927 // Type() returns an opaque and degenerate "rangeIter" type. 928 // 929 // Pos() returns the ast.RangeStmt.For. 930 // 931 // Example printed form: 932 // t0 = range "hello":string 933 // 934 type Range struct { 935 register 936 X Value // string or map 937 } 938 939 // The Next instruction reads and advances the (map or string) 940 // iterator Iter and returns a 3-tuple value (ok, k, v). If the 941 // iterator is not exhausted, ok is true and k and v are the next 942 // elements of the domain and range, respectively. Otherwise ok is 943 // false and k and v are undefined. 944 // 945 // Components of the tuple are accessed using Extract. 946 // 947 // The IsString field distinguishes iterators over strings from those 948 // over maps, as the Type() alone is insufficient: consider 949 // map[int]rune. 950 // 951 // Type() returns a *types.Tuple for the triple (ok, k, v). 952 // The types of k and/or v may be types.Invalid. 953 // 954 // Example printed form: 955 // t1 = next t0 956 // 957 type Next struct { 958 register 959 Iter Value 960 IsString bool // true => string iterator; false => map iterator. 961 } 962 963 // The TypeAssert instruction tests whether interface value X has type 964 // AssertedType. 965 // 966 // If !CommaOk, on success it returns v, the result of the conversion 967 // (defined below); on failure it panics. 968 // 969 // If CommaOk: on success it returns a pair (v, true) where v is the 970 // result of the conversion; on failure it returns (z, false) where z 971 // is AssertedType's zero value. The components of the pair must be 972 // accessed using the Extract instruction. 973 // 974 // If AssertedType is a concrete type, TypeAssert checks whether the 975 // dynamic type in interface X is equal to it, and if so, the result 976 // of the conversion is a copy of the value in the interface. 977 // 978 // If AssertedType is an interface, TypeAssert checks whether the 979 // dynamic type of the interface is assignable to it, and if so, the 980 // result of the conversion is a copy of the interface value X. 981 // If AssertedType is a superinterface of X.Type(), the operation will 982 // fail iff the operand is nil. (Contrast with ChangeInterface, which 983 // performs no nil-check.) 984 // 985 // Type() reflects the actual type of the result, possibly a 986 // 2-types.Tuple; AssertedType is the asserted type. 987 // 988 // Pos() returns the ast.CallExpr.Lparen if the instruction arose from 989 // an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the 990 // instruction arose from an explicit e.(T) operation; or the 991 // ast.CaseClause.Case if the instruction arose from a case of a 992 // type-switch statement. 993 // 994 // Example printed form: 995 // t1 = typeassert t0.(int) 996 // t3 = typeassert,ok t2.(T) 997 // 998 type TypeAssert struct { 999 register 1000 X Value 1001 AssertedType types.Type 1002 CommaOk bool 1003 } 1004 1005 // The Extract instruction yields component Index of Tuple. 1006 // 1007 // This is used to access the results of instructions with multiple 1008 // return values, such as Call, TypeAssert, Next, UnOp(ARROW) and 1009 // IndexExpr(Map). 1010 // 1011 // Example printed form: 1012 // t1 = extract t0 #1 1013 // 1014 type Extract struct { 1015 register 1016 Tuple Value 1017 Index int 1018 } 1019 1020 // Instructions executed for effect. They do not yield a value. -------------------- 1021 1022 // The Jump instruction transfers control to the sole successor of its 1023 // owning block. 1024 // 1025 // A Jump must be the last instruction of its containing BasicBlock. 1026 // 1027 // Pos() returns NoPos. 1028 // 1029 // Example printed form: 1030 // jump done 1031 // 1032 type Jump struct { 1033 anInstruction 1034 } 1035 1036 // The If instruction transfers control to one of the two successors 1037 // of its owning block, depending on the boolean Cond: the first if 1038 // true, the second if false. 1039 // 1040 // An If instruction must be the last instruction of its containing 1041 // BasicBlock. 1042 // 1043 // Pos() returns NoPos. 1044 // 1045 // Example printed form: 1046 // if t0 goto done else body 1047 // 1048 type If struct { 1049 anInstruction 1050 Cond Value 1051 } 1052 1053 // The Return instruction returns values and control back to the calling 1054 // function. 1055 // 1056 // len(Results) is always equal to the number of results in the 1057 // function's signature. 1058 // 1059 // If len(Results) > 1, Return returns a tuple value with the specified 1060 // components which the caller must access using Extract instructions. 1061 // 1062 // There is no instruction to return a ready-made tuple like those 1063 // returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or 1064 // a tail-call to a function with multiple result parameters. 1065 // 1066 // Return must be the last instruction of its containing BasicBlock. 1067 // Such a block has no successors. 1068 // 1069 // Pos() returns the ast.ReturnStmt.Return, if explicit in the source. 1070 // 1071 // Example printed form: 1072 // return 1073 // return nil:I, 2:int 1074 // 1075 type Return struct { 1076 anInstruction 1077 Results []Value 1078 pos token.Pos 1079 } 1080 1081 // The RunDefers instruction pops and invokes the entire stack of 1082 // procedure calls pushed by Defer instructions in this function. 1083 // 1084 // It is legal to encounter multiple 'rundefers' instructions in a 1085 // single control-flow path through a function; this is useful in 1086 // the combined init() function, for example. 1087 // 1088 // Pos() returns NoPos. 1089 // 1090 // Example printed form: 1091 // rundefers 1092 // 1093 type RunDefers struct { 1094 anInstruction 1095 } 1096 1097 // The Panic instruction initiates a panic with value X. 1098 // 1099 // A Panic instruction must be the last instruction of its containing 1100 // BasicBlock, which must have no successors. 1101 // 1102 // NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; 1103 // they are treated as calls to a built-in function. 1104 // 1105 // Pos() returns the ast.CallExpr.Lparen if this panic was explicit 1106 // in the source. 1107 // 1108 // Example printed form: 1109 // panic t0 1110 // 1111 type Panic struct { 1112 anInstruction 1113 X Value // an interface{} 1114 pos token.Pos 1115 } 1116 1117 // The Go instruction creates a new goroutine and calls the specified 1118 // function within it. 1119 // 1120 // See CallCommon for generic function call documentation. 1121 // 1122 // Pos() returns the ast.GoStmt.Go. 1123 // 1124 // Example printed form: 1125 // go println(t0, t1) 1126 // go t3() 1127 // go invoke t5.Println(...t6) 1128 // 1129 type Go struct { 1130 anInstruction 1131 Call CallCommon 1132 pos token.Pos 1133 } 1134 1135 // The Defer instruction pushes the specified call onto a stack of 1136 // functions to be called by a RunDefers instruction or by a panic. 1137 // 1138 // See CallCommon for generic function call documentation. 1139 // 1140 // Pos() returns the ast.DeferStmt.Defer. 1141 // 1142 // Example printed form: 1143 // defer println(t0, t1) 1144 // defer t3() 1145 // defer invoke t5.Println(...t6) 1146 // 1147 type Defer struct { 1148 anInstruction 1149 Call CallCommon 1150 pos token.Pos 1151 } 1152 1153 // The Send instruction sends X on channel Chan. 1154 // 1155 // Pos() returns the ast.SendStmt.Arrow, if explicit in the source. 1156 // 1157 // Example printed form: 1158 // send t0 <- t1 1159 // 1160 type Send struct { 1161 anInstruction 1162 Chan, X Value 1163 pos token.Pos 1164 } 1165 1166 // The Store instruction stores Val at address Addr. 1167 // Stores can be of arbitrary types. 1168 // 1169 // Pos() returns the position of the source-level construct most closely 1170 // associated with the memory store operation. 1171 // Since implicit memory stores are numerous and varied and depend upon 1172 // implementation choices, the details are not specified. 1173 // 1174 // Example printed form: 1175 // *x = y 1176 // 1177 type Store struct { 1178 anInstruction 1179 Addr Value 1180 Val Value 1181 pos token.Pos 1182 } 1183 1184 // The MapUpdate instruction updates the association of Map[Key] to 1185 // Value. 1186 // 1187 // Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack, 1188 // if explicit in the source. 1189 // 1190 // Example printed form: 1191 // t0[t1] = t2 1192 // 1193 type MapUpdate struct { 1194 anInstruction 1195 Map Value 1196 Key Value 1197 Value Value 1198 pos token.Pos 1199 } 1200 1201 // A DebugRef instruction maps a source-level expression Expr to the 1202 // SSA value X that represents the value (!IsAddr) or address (IsAddr) 1203 // of that expression. 1204 // 1205 // DebugRef is a pseudo-instruction: it has no dynamic effect. 1206 // 1207 // Pos() returns Expr.Pos(), the start position of the source-level 1208 // expression. This is not the same as the "designated" token as 1209 // documented at Value.Pos(). e.g. CallExpr.Pos() does not return the 1210 // position of the ("designated") Lparen token. 1211 // 1212 // If Expr is an *ast.Ident denoting a var or func, Object() returns 1213 // the object; though this information can be obtained from the type 1214 // checker, including it here greatly facilitates debugging. 1215 // For non-Ident expressions, Object() returns nil. 1216 // 1217 // DebugRefs are generated only for functions built with debugging 1218 // enabled; see Package.SetDebugMode() and the GlobalDebug builder 1219 // mode flag. 1220 // 1221 // DebugRefs are not emitted for ast.Idents referring to constants or 1222 // predeclared identifiers, since they are trivial and numerous. 1223 // Nor are they emitted for ast.ParenExprs. 1224 // 1225 // (By representing these as instructions, rather than out-of-band, 1226 // consistency is maintained during transformation passes by the 1227 // ordinary SSA renaming machinery.) 1228 // 1229 // Example printed form: 1230 // ; *ast.CallExpr @ 102:9 is t5 1231 // ; var x float64 @ 109:72 is x 1232 // ; address of *ast.CompositeLit @ 216:10 is t0 1233 // 1234 type DebugRef struct { 1235 anInstruction 1236 Expr ast.Expr // the referring expression (never *ast.ParenExpr) 1237 object types.Object // the identity of the source var/func 1238 IsAddr bool // Expr is addressable and X is the address it denotes 1239 X Value // the value or address of Expr 1240 } 1241 1242 // Embeddable mix-ins and helpers for common parts of other structs. ----------- 1243 1244 // register is a mix-in embedded by all SSA values that are also 1245 // instructions, i.e. virtual registers, and provides a uniform 1246 // implementation of most of the Value interface: Value.Name() is a 1247 // numbered register (e.g. "t0"); the other methods are field accessors. 1248 // 1249 // Temporary names are automatically assigned to each register on 1250 // completion of building a function in SSA form. 1251 // 1252 // Clients must not assume that the 'id' value (and the Name() derived 1253 // from it) is unique within a function. As always in this API, 1254 // semantics are determined only by identity; names exist only to 1255 // facilitate debugging. 1256 // 1257 type register struct { 1258 anInstruction 1259 num int // "name" of virtual register, e.g. "t0". Not guaranteed unique. 1260 typ types.Type // type of virtual register 1261 pos token.Pos // position of source expression, or NoPos 1262 referrers []Instruction 1263 } 1264 1265 // anInstruction is a mix-in embedded by all Instructions. 1266 // It provides the implementations of the Block and setBlock methods. 1267 type anInstruction struct { 1268 block *BasicBlock // the basic block of this instruction 1269 } 1270 1271 // CallCommon is contained by Go, Defer and Call to hold the 1272 // common parts of a function or method call. 1273 // 1274 // Each CallCommon exists in one of two modes, function call and 1275 // interface method invocation, or "call" and "invoke" for short. 1276 // 1277 // 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon 1278 // represents an ordinary function call of the value in Value, 1279 // which may be a *Builtin, a *Function or any other value of kind 1280 // 'func'. 1281 // 1282 // Value may be one of: 1283 // (a) a *Function, indicating a statically dispatched call 1284 // to a package-level function, an anonymous function, or 1285 // a method of a named type. 1286 // (b) a *MakeClosure, indicating an immediately applied 1287 // function literal with free variables. 1288 // (c) a *Builtin, indicating a statically dispatched call 1289 // to a built-in function. 1290 // (d) any other value, indicating a dynamically dispatched 1291 // function call. 1292 // StaticCallee returns the identity of the callee in cases 1293 // (a) and (b), nil otherwise. 1294 // 1295 // Args contains the arguments to the call. If Value is a method, 1296 // Args[0] contains the receiver parameter. 1297 // 1298 // Example printed form: 1299 // t2 = println(t0, t1) 1300 // go t3() 1301 // defer t5(...t6) 1302 // 1303 // 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon 1304 // represents a dynamically dispatched call to an interface method. 1305 // In this mode, Value is the interface value and Method is the 1306 // interface's abstract method. Note: an abstract method may be 1307 // shared by multiple interfaces due to embedding; Value.Type() 1308 // provides the specific interface used for this call. 1309 // 1310 // Value is implicitly supplied to the concrete method implementation 1311 // as the receiver parameter; in other words, Args[0] holds not the 1312 // receiver but the first true argument. 1313 // 1314 // Example printed form: 1315 // t1 = invoke t0.String() 1316 // go invoke t3.Run(t2) 1317 // defer invoke t4.Handle(...t5) 1318 // 1319 // For all calls to variadic functions (Signature().Variadic()), 1320 // the last element of Args is a slice. 1321 // 1322 type CallCommon struct { 1323 Value Value // receiver (invoke mode) or func value (call mode) 1324 Method *types.Func // abstract method (invoke mode) 1325 Args []Value // actual parameters (in static method call, includes receiver) 1326 pos token.Pos // position of CallExpr.Lparen, iff explicit in source 1327 } 1328 1329 // IsInvoke returns true if this call has "invoke" (not "call") mode. 1330 func (c *CallCommon) IsInvoke() bool { 1331 return c.Method != nil 1332 } 1333 1334 func (c *CallCommon) Pos() token.Pos { return c.pos } 1335 1336 // Signature returns the signature of the called function. 1337 // 1338 // For an "invoke"-mode call, the signature of the interface method is 1339 // returned. 1340 // 1341 // In either "call" or "invoke" mode, if the callee is a method, its 1342 // receiver is represented by sig.Recv, not sig.Params().At(0). 1343 // 1344 func (c *CallCommon) Signature() *types.Signature { 1345 if c.Method != nil { 1346 return c.Method.Type().(*types.Signature) 1347 } 1348 return c.Value.Type().Underlying().(*types.Signature) 1349 } 1350 1351 // StaticCallee returns the callee if this is a trivially static 1352 // "call"-mode call to a function. 1353 func (c *CallCommon) StaticCallee() *Function { 1354 switch fn := c.Value.(type) { 1355 case *Function: 1356 return fn 1357 case *MakeClosure: 1358 return fn.Fn.(*Function) 1359 } 1360 return nil 1361 } 1362 1363 // Description returns a description of the mode of this call suitable 1364 // for a user interface, e.g., "static method call". 1365 func (c *CallCommon) Description() string { 1366 switch fn := c.Value.(type) { 1367 case *Builtin: 1368 return "built-in function call" 1369 case *MakeClosure: 1370 return "static function closure call" 1371 case *Function: 1372 if fn.Signature.Recv() != nil { 1373 return "static method call" 1374 } 1375 return "static function call" 1376 } 1377 if c.IsInvoke() { 1378 return "dynamic method call" // ("invoke" mode) 1379 } 1380 return "dynamic function call" 1381 } 1382 1383 // The CallInstruction interface, implemented by *Go, *Defer and *Call, 1384 // exposes the common parts of function-calling instructions, 1385 // yet provides a way back to the Value defined by *Call alone. 1386 // 1387 type CallInstruction interface { 1388 Instruction 1389 Common() *CallCommon // returns the common parts of the call 1390 Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer) 1391 } 1392 1393 func (s *Call) Common() *CallCommon { return &s.Call } 1394 func (s *Defer) Common() *CallCommon { return &s.Call } 1395 func (s *Go) Common() *CallCommon { return &s.Call } 1396 1397 func (s *Call) Value() *Call { return s } 1398 func (s *Defer) Value() *Call { return nil } 1399 func (s *Go) Value() *Call { return nil } 1400 1401 func (v *Builtin) Type() types.Type { return v.sig } 1402 func (v *Builtin) Name() string { return v.name } 1403 func (*Builtin) Referrers() *[]Instruction { return nil } 1404 func (v *Builtin) Pos() token.Pos { return token.NoPos } 1405 func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) } 1406 func (v *Builtin) Parent() *Function { return nil } 1407 1408 func (v *FreeVar) Type() types.Type { return v.typ } 1409 func (v *FreeVar) Name() string { return v.name } 1410 func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers } 1411 func (v *FreeVar) Pos() token.Pos { return v.pos } 1412 func (v *FreeVar) Parent() *Function { return v.parent } 1413 1414 func (v *Global) Type() types.Type { return v.typ } 1415 func (v *Global) Name() string { return v.name } 1416 func (v *Global) Parent() *Function { return nil } 1417 func (v *Global) Pos() token.Pos { return v.pos } 1418 func (v *Global) Referrers() *[]Instruction { return nil } 1419 func (v *Global) Token() token.Token { return token.VAR } 1420 func (v *Global) Object() types.Object { return v.object } 1421 func (v *Global) String() string { return v.RelString(nil) } 1422 func (v *Global) Package() *Package { return v.Pkg } 1423 func (v *Global) RelString(from *types.Package) string { return relString(v, from) } 1424 1425 func (v *Function) Name() string { return v.name } 1426 func (v *Function) Type() types.Type { return v.Signature } 1427 func (v *Function) Pos() token.Pos { return v.pos } 1428 func (v *Function) Token() token.Token { return token.FUNC } 1429 func (v *Function) Object() types.Object { return v.object } 1430 func (v *Function) String() string { return v.RelString(nil) } 1431 func (v *Function) Package() *Package { return v.Pkg } 1432 func (v *Function) Parent() *Function { return v.parent } 1433 func (v *Function) Referrers() *[]Instruction { 1434 if v.parent != nil { 1435 return &v.referrers 1436 } 1437 return nil 1438 } 1439 1440 func (v *Parameter) Type() types.Type { return v.typ } 1441 func (v *Parameter) Name() string { return v.name } 1442 func (v *Parameter) Object() types.Object { return v.object } 1443 func (v *Parameter) Referrers() *[]Instruction { return &v.referrers } 1444 func (v *Parameter) Pos() token.Pos { return v.pos } 1445 func (v *Parameter) Parent() *Function { return v.parent } 1446 1447 func (v *Alloc) Type() types.Type { return v.typ } 1448 func (v *Alloc) Referrers() *[]Instruction { return &v.referrers } 1449 func (v *Alloc) Pos() token.Pos { return v.pos } 1450 1451 func (v *register) Type() types.Type { return v.typ } 1452 func (v *register) setType(typ types.Type) { v.typ = typ } 1453 func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) } 1454 func (v *register) setNum(num int) { v.num = num } 1455 func (v *register) Referrers() *[]Instruction { return &v.referrers } 1456 func (v *register) Pos() token.Pos { return v.pos } 1457 func (v *register) setPos(pos token.Pos) { v.pos = pos } 1458 1459 func (v *anInstruction) Parent() *Function { return v.block.parent } 1460 func (v *anInstruction) Block() *BasicBlock { return v.block } 1461 func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block } 1462 func (v *anInstruction) Referrers() *[]Instruction { return nil } 1463 1464 func (t *Type) Name() string { return t.object.Name() } 1465 func (t *Type) Pos() token.Pos { return t.object.Pos() } 1466 func (t *Type) Type() types.Type { return t.object.Type() } 1467 func (t *Type) Token() token.Token { return token.TYPE } 1468 func (t *Type) Object() types.Object { return t.object } 1469 func (t *Type) String() string { return t.RelString(nil) } 1470 func (t *Type) Package() *Package { return t.pkg } 1471 func (t *Type) RelString(from *types.Package) string { return relString(t, from) } 1472 1473 func (c *NamedConst) Name() string { return c.object.Name() } 1474 func (c *NamedConst) Pos() token.Pos { return c.object.Pos() } 1475 func (c *NamedConst) String() string { return c.RelString(nil) } 1476 func (c *NamedConst) Type() types.Type { return c.object.Type() } 1477 func (c *NamedConst) Token() token.Token { return token.CONST } 1478 func (c *NamedConst) Object() types.Object { return c.object } 1479 func (c *NamedConst) Package() *Package { return c.pkg } 1480 func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) } 1481 1482 // Func returns the package-level function of the specified name, 1483 // or nil if not found. 1484 // 1485 func (p *Package) Func(name string) (f *Function) { 1486 f, _ = p.Members[name].(*Function) 1487 return 1488 } 1489 1490 // Var returns the package-level variable of the specified name, 1491 // or nil if not found. 1492 // 1493 func (p *Package) Var(name string) (g *Global) { 1494 g, _ = p.Members[name].(*Global) 1495 return 1496 } 1497 1498 // Const returns the package-level constant of the specified name, 1499 // or nil if not found. 1500 // 1501 func (p *Package) Const(name string) (c *NamedConst) { 1502 c, _ = p.Members[name].(*NamedConst) 1503 return 1504 } 1505 1506 // Type returns the package-level type of the specified name, 1507 // or nil if not found. 1508 // 1509 func (p *Package) Type(name string) (t *Type) { 1510 t, _ = p.Members[name].(*Type) 1511 return 1512 } 1513 1514 func (v *Call) Pos() token.Pos { return v.Call.pos } 1515 func (s *Defer) Pos() token.Pos { return s.pos } 1516 func (s *Go) Pos() token.Pos { return s.pos } 1517 func (s *MapUpdate) Pos() token.Pos { return s.pos } 1518 func (s *Panic) Pos() token.Pos { return s.pos } 1519 func (s *Return) Pos() token.Pos { return s.pos } 1520 func (s *Send) Pos() token.Pos { return s.pos } 1521 func (s *Store) Pos() token.Pos { return s.pos } 1522 func (s *If) Pos() token.Pos { return token.NoPos } 1523 func (s *Jump) Pos() token.Pos { return token.NoPos } 1524 func (s *RunDefers) Pos() token.Pos { return token.NoPos } 1525 func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() } 1526 1527 // Operands. 1528 1529 func (v *Alloc) Operands(rands []*Value) []*Value { 1530 return rands 1531 } 1532 1533 func (v *BinOp) Operands(rands []*Value) []*Value { 1534 return append(rands, &v.X, &v.Y) 1535 } 1536 1537 func (c *CallCommon) Operands(rands []*Value) []*Value { 1538 rands = append(rands, &c.Value) 1539 for i := range c.Args { 1540 rands = append(rands, &c.Args[i]) 1541 } 1542 return rands 1543 } 1544 1545 func (s *Go) Operands(rands []*Value) []*Value { 1546 return s.Call.Operands(rands) 1547 } 1548 1549 func (s *Call) Operands(rands []*Value) []*Value { 1550 return s.Call.Operands(rands) 1551 } 1552 1553 func (s *Defer) Operands(rands []*Value) []*Value { 1554 return s.Call.Operands(rands) 1555 } 1556 1557 func (v *ChangeInterface) Operands(rands []*Value) []*Value { 1558 return append(rands, &v.X) 1559 } 1560 1561 func (v *ChangeType) Operands(rands []*Value) []*Value { 1562 return append(rands, &v.X) 1563 } 1564 1565 func (v *Convert) Operands(rands []*Value) []*Value { 1566 return append(rands, &v.X) 1567 } 1568 1569 func (s *DebugRef) Operands(rands []*Value) []*Value { 1570 return append(rands, &s.X) 1571 } 1572 1573 func (v *Extract) Operands(rands []*Value) []*Value { 1574 return append(rands, &v.Tuple) 1575 } 1576 1577 func (v *Field) Operands(rands []*Value) []*Value { 1578 return append(rands, &v.X) 1579 } 1580 1581 func (v *FieldAddr) Operands(rands []*Value) []*Value { 1582 return append(rands, &v.X) 1583 } 1584 1585 func (s *If) Operands(rands []*Value) []*Value { 1586 return append(rands, &s.Cond) 1587 } 1588 1589 func (v *Index) Operands(rands []*Value) []*Value { 1590 return append(rands, &v.X, &v.Index) 1591 } 1592 1593 func (v *IndexAddr) Operands(rands []*Value) []*Value { 1594 return append(rands, &v.X, &v.Index) 1595 } 1596 1597 func (*Jump) Operands(rands []*Value) []*Value { 1598 return rands 1599 } 1600 1601 func (v *Lookup) Operands(rands []*Value) []*Value { 1602 return append(rands, &v.X, &v.Index) 1603 } 1604 1605 func (v *MakeChan) Operands(rands []*Value) []*Value { 1606 return append(rands, &v.Size) 1607 } 1608 1609 func (v *MakeClosure) Operands(rands []*Value) []*Value { 1610 rands = append(rands, &v.Fn) 1611 for i := range v.Bindings { 1612 rands = append(rands, &v.Bindings[i]) 1613 } 1614 return rands 1615 } 1616 1617 func (v *MakeInterface) Operands(rands []*Value) []*Value { 1618 return append(rands, &v.X) 1619 } 1620 1621 func (v *MakeMap) Operands(rands []*Value) []*Value { 1622 return append(rands, &v.Reserve) 1623 } 1624 1625 func (v *MakeSlice) Operands(rands []*Value) []*Value { 1626 return append(rands, &v.Len, &v.Cap) 1627 } 1628 1629 func (v *MapUpdate) Operands(rands []*Value) []*Value { 1630 return append(rands, &v.Map, &v.Key, &v.Value) 1631 } 1632 1633 func (v *Next) Operands(rands []*Value) []*Value { 1634 return append(rands, &v.Iter) 1635 } 1636 1637 func (s *Panic) Operands(rands []*Value) []*Value { 1638 return append(rands, &s.X) 1639 } 1640 1641 func (v *Phi) Operands(rands []*Value) []*Value { 1642 for i := range v.Edges { 1643 rands = append(rands, &v.Edges[i]) 1644 } 1645 return rands 1646 } 1647 1648 func (v *Range) Operands(rands []*Value) []*Value { 1649 return append(rands, &v.X) 1650 } 1651 1652 func (s *Return) Operands(rands []*Value) []*Value { 1653 for i := range s.Results { 1654 rands = append(rands, &s.Results[i]) 1655 } 1656 return rands 1657 } 1658 1659 func (*RunDefers) Operands(rands []*Value) []*Value { 1660 return rands 1661 } 1662 1663 func (v *Select) Operands(rands []*Value) []*Value { 1664 for i := range v.States { 1665 rands = append(rands, &v.States[i].Chan, &v.States[i].Send) 1666 } 1667 return rands 1668 } 1669 1670 func (s *Send) Operands(rands []*Value) []*Value { 1671 return append(rands, &s.Chan, &s.X) 1672 } 1673 1674 func (v *Slice) Operands(rands []*Value) []*Value { 1675 return append(rands, &v.X, &v.Low, &v.High, &v.Max) 1676 } 1677 1678 func (s *Store) Operands(rands []*Value) []*Value { 1679 return append(rands, &s.Addr, &s.Val) 1680 } 1681 1682 func (v *TypeAssert) Operands(rands []*Value) []*Value { 1683 return append(rands, &v.X) 1684 } 1685 1686 func (v *UnOp) Operands(rands []*Value) []*Value { 1687 return append(rands, &v.X) 1688 } 1689 1690 // Non-Instruction Values: 1691 func (v *Builtin) Operands(rands []*Value) []*Value { return rands } 1692 func (v *FreeVar) Operands(rands []*Value) []*Value { return rands } 1693 func (v *Const) Operands(rands []*Value) []*Value { return rands } 1694 func (v *Function) Operands(rands []*Value) []*Value { return rands } 1695 func (v *Global) Operands(rands []*Value) []*Value { return rands } 1696 func (v *Parameter) Operands(rands []*Value) []*Value { return rands }