types

package
v0.0.0-...-74d226f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

Package types declares the data types and implements the algorithms for type-checking of Go packages. Use Config.Check to invoke the type checker for a package. Alternatively, create a new type checker with NewChecker and invoke it incrementally by calling Checker.Files.

Type-checking consists of several interdependent phases:

Name resolution maps each identifier (syntax.Name) in the program to the language object (Object) it denotes. Use Info.{Defs,Uses,Implicits} for the results of name resolution.

Constant folding computes the exact constant value (constant.Value) for every expression (syntax.Expr) that is a compile-time constant. Use Info.Types[expr].Value for the results of constant folding.

Type inference computes the type (Type) of every expression (syntax.Expr) and checks for compliance with the language specification. Use Info.Types[expr].Type for the results of type inference.

Package ssa defines a representation of the elements of Go programs (packages, types, functions, variables and constants) using a static single-assignment (SSA) form intermediate representation (IR) for the bodies of functions.

THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE.

For an introduction to SSA form, see http://en.wikipedia.org/wiki/Static_single_assignment_form. This page provides a broader reading list: http://www.dcs.gla.ac.uk/~jsinger/ssa.html.

The level of abstraction of the SSA form is intentionally close to the source language to facilitate construction of source analysis tools. It is not intended for machine code generation.

All looping, branching and switching constructs are replaced with unstructured control flow. Higher-level control flow constructs such as multi-way branch can be reconstructed as needed; see ssautil.Switches() for an example.

The simplest way to create the SSA representation of a package is to load typed syntax trees using golang.org/x/tools/go/packages, then invoke the ssautil.Packages helper function. See ExampleLoadPackages and ExampleWholeProgram for examples. The resulting ssa.Program contains all the packages and their members, but SSA code is not created for function bodies until a subsequent call to (*Package).Build or (*Program).Build.

The builder initially builds a naive SSA form in which all local variables are addresses of stack locations with explicit loads and stores. Registerisation of eligible locals and φ-node insertion using dominance and dataflow are then performed as a second pass called "lifting" to improve the accuracy and performance of subsequent analyses; this pass can be skipped by setting the NaiveForm builder flag.

The primary interfaces of this package are:

  • Member: a named member of a Go package.
  • Value: an expression that yields a value.
  • Instruction: a statement that consumes values and performs computation.
  • Node: a Value or Instruction (emphasizing its membership in the SSA value graph)

A computation that yields a result implements both the Value and Instruction interfaces. The following table shows for each concrete type which of these interfaces it implements.

                   Value?          Instruction?      Member?
*Alloc                ✔               ✔
*BinOp                ✔               ✔
*Builtin              ✔
*Call                 ✔               ✔
*ChangeInterface      ✔               ✔
*ChangeType           ✔               ✔
*Const                ✔
*Convert              ✔               ✔
*DebugRef                             ✔
*Defer                                ✔
*Extract              ✔               ✔
*Field                ✔               ✔
*FieldAddr            ✔               ✔
*FreeVar              ✔
*Function             ✔                               ✔ (func)
*Global               ✔                               ✔ (var)
*Go                                   ✔
*If                                   ✔
*Index                ✔               ✔
*IndexAddr            ✔               ✔
*Jump                                 ✔
*Lookup               ✔               ✔
*MakeChan             ✔               ✔
*MakeClosure          ✔               ✔
*MakeInterface        ✔               ✔
*MakeMap              ✔               ✔
*MakeSlice            ✔               ✔
*MapUpdate                            ✔
*NamedConst                                           ✔ (const)
*Next                 ✔               ✔
*Panic                                ✔
*Parameter            ✔
*Phi                  ✔               ✔
*Range                ✔               ✔
*Return                               ✔
*RunDefers                            ✔
*Select               ✔               ✔
*Send                                 ✔
*Slice                ✔               ✔
*SliceToArrayPointer  ✔               ✔
*Store                                ✔
*Type                                                 ✔ (type)
*TypeAssert           ✔               ✔
*UnOp                 ✔               ✔

Other key types in this package include: Program, Package, Function and BasicBlock.

The program representation constructed by this package is fully resolved internally, i.e. it does not rely on the names of Values, Packages, Functions, Types or BasicBlocks for the correct interpretation of the program. Only the identities of objects and the topology of the SSA and type graphs are semantically significant. (There is one exception: Ids, used to identify field and method names, contain strings.) Avoidance of name-based operations simplifies the implementation of subsequent passes and can make them very efficient. Many objects are nonetheless named to aid in debugging, but it is not essential that the names be either accurate or unambiguous. The public API exposes a number of name-based maps for client convenience.

The ssa/ssautil package provides various utilities that depend only on the public API of this package.

TODO(adonovan): Consider the exceptional control-flow implications of defer and recover().

TODO(adonovan): write a how-to document for all the various cases of trying to determine corresponding elements across the four domains of source locations, syntax.Nodes, types.Objects, ssa.Values/Instructions.

Package typeutil defines various utilities for types, such as Map, a mapping from types.Type to interface{} values.

Example (BuildPackage)

This program demonstrates how to run the SSA builder on a single package of one or more already-parsed files. Its dependencies are loaded from compiler export data. This is what you'd typically use for a compiler; it does not depend on golang.org/x/tools/go/loader.

It shows the printed representation of packages, functions, and instructions. Within the function listing, the name of each BasicBlock such as ".0.entry" is printed left-aligned, followed by the block's Instructions.

For each instruction that defines an SSA virtual register (i.e. implements Value), the type of that value is shown in the right column.

Build and run the ssadump.go program if you want a standalone tool with similar functionality. It is located at golang.org/x/tools/cmd/ssadump.

// Parse the source files.
f, err := ParseString("hello.go", hello)
if err != nil {
	fmt.Print(err) // parse error
	return
}
files := []*File{f}

// Create the type-checker's package.
pkg := NewPackage("hello", "")

// Type-check the package, load dependencies.
// Create and build the SSA program.
_, hello, _, err := ssautil.BuildPackage(
	&Config{Importer: importer.Default()}, pkg, files, types.SanityCheckFunctions)
if err != nil {
	fmt.Print(err) // type error in some package
	return
}

// Print out the package.
hello.WriteTo(os.Stdout)

// Print out the package-level functions.
hello.InitFunc.WriteTo(os.Stdout)
hello.Func("main").WriteTo(os.Stdout)
Output:


package hello:
  func  init       func()
  var   init$guard bool
  func  main       func()

# Name: hello.init
# Package: hello
# Synthetic: package initializer
func init():
0:                                                                entry P:0 S:2
	t0 = *init$guard                                                   bool
	if t0 goto 2 else 1
1:                                                           init.start P:1 S:1
	*init$guard = true:bool
	t1 = fmt.init()                                                      ()
	jump 2
2:                                                            init.done P:2 S:0
	return

# Name: hello.main
# Package: hello
# Location: hello.go:8:6
func main():
0:                                                                entry P:0 S:0
	t0 = new [1]any (varargs)                                       *[1]any
	t1 = &t0[0:int]                                                    *any
	t2 = make any <- string ("Hello, World!":string)                    any
	*t1 = t2
	t3 = slice t0[:]                                                  []any
	t4 = fmt.Println(t3...)                              (n int, err error)
	return
Example (LoadPackages)

This example builds SSA code for a set of packages using the x/tools/go/packages API. This is what you would typically use for a analysis capable of operating on a single package.

// Load, parse, and type-check the initial packages.
cfg := &packages.Config{Mode: packages.LoadSyntax}
initial, err := packages.Load(cfg, "fmt", "net/http")
if err != nil {
	log.Fatal(err)
}

// Stop if any package had errors.
// This step is optional; without it, the next step
// will create SSA for only a subset of packages.
if packages.PrintErrors(initial) > 0 {
	log.Fatalf("packages contain errors")
}

// Create SSA packages for all well-typed packages.
prog, pkgs := ssautil.Packages(initial, types.PrintPackages)
_ = prog

// Build SSA code for the well-typed initial packages.
for _, p := range pkgs {
	if p != nil {
		p.Build(prog)
	}
}
Output:

Example (LoadWholeProgram)

This example builds SSA code for a set of packages plus all their dependencies, using the x/tools/go/packages API. This is what you'd typically use for a whole-program analysis.

// Load, parse, and type-check the whole program.
cfg := packages.Config{Mode: packages.LoadAllSyntax}
initial, err := packages.Load(&cfg, "fmt", "net/http")
if err != nil {
	log.Fatal(err)
}

// Create SSA packages for well-typed packages and their dependencies.
prog, pkgs := ssautil.AllPackages(initial, types.PrintPackages)
_ = pkgs

// Build SSA code for the whole program.
prog.Build()
Output:

Index

Examples

Constants

View Source
const BuilderModeDoc = `` /* 504-byte string literal not displayed */

Variables

View Source
var Typ = [...]*Basic{
	Invalid: {Invalid, 0, "invalid type"},

	Bool:          {Bool, IsBoolean, "bool"},
	Int:           {Int, IsInteger, "int"},
	Int8:          {Int8, IsInteger, "int8"},
	Int16:         {Int16, IsInteger, "int16"},
	Int32:         {Int32, IsInteger, "int32"},
	Int64:         {Int64, IsInteger, "int64"},
	Uint:          {Uint, IsInteger | IsUnsigned, "uint"},
	Uint8:         {Uint8, IsInteger | IsUnsigned, "uint8"},
	Uint16:        {Uint16, IsInteger | IsUnsigned, "uint16"},
	Uint32:        {Uint32, IsInteger | IsUnsigned, "uint32"},
	Uint64:        {Uint64, IsInteger | IsUnsigned, "uint64"},
	Uintptr:       {Uintptr, IsInteger | IsUnsigned, "uintptr"},
	Float32:       {Float32, IsFloat, "float32"},
	Float64:       {Float64, IsFloat, "float64"},
	Complex64:     {Complex64, IsComplex, "complex64"},
	Complex128:    {Complex128, IsComplex, "complex128"},
	String:        {String, IsString, "string"},
	UnsafePointer: {UnsafePointer, 0, "Pointer"},

	UntypedBool:    {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
	UntypedInt:     {UntypedInt, IsInteger | IsUntyped, "untyped int"},
	UntypedRune:    {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
	UntypedFloat:   {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
	UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
	UntypedString:  {UntypedString, IsString | IsUntyped, "untyped string"},
	UntypedNil:     {UntypedNil, IsUntyped, "untyped nil"},
}

Typ contains the predeclared *Basic types indexed by their corresponding BasicKind.

The *Basic type for Typ[Byte] will have the name "uint8". Use Universe.Lookup("byte").Type() to obtain the specific alias basic type named "byte" (and analogous for "rune").

Functions

func AssertableTo

func AssertableTo(V *Interface, T Type) bool

AssertableTo reports whether a value of type V can be asserted to have type T.

The behavior of AssertableTo is undefined in two cases:

  • if V is a generalized interface; i.e., an interface that may only be used as a type constraint in Go code
  • if T is an uninstantiated generic type

func AssignableTo

func AssignableTo(V, T Type) bool

AssignableTo reports whether a value of type V is assignable to a variable of type T.

The behavior of AssignableTo is undefined if V or T is an uninstantiated generic type.

func CheckExpr

func CheckExpr(pkg *Package, pos Pos, expr Expr, info *Info) (err error)

CheckExpr type checks the expression expr as if it had appeared at position pos of package pkg. Type information about the expression is recorded in info. The expression may be an identifier denoting an uninstantiated generic function or type.

If pkg == nil, the Universe scope is used and the provided position pos is ignored. If pkg != nil, and pos is invalid, the package scope is used. Otherwise, pos must belong to the package.

An error is returned if pos is not within the package or if the node cannot be type-checked.

Note: Eval and CheckExpr should not be used instead of running Check to compute types and values, but in addition to Check, as these functions ignore the context in which an expression is used (e.g., an assignment). Thus, top-level untyped constants will return an untyped type rather then the respective context-specific type.

func Comparable

func Comparable(T Type) bool

Comparable reports whether values of type T are comparable.

func ConvertibleTo

func ConvertibleTo(V, T Type) bool

ConvertibleTo reports whether a value of type V is convertible to a value of type T.

The behavior of ConvertibleTo is undefined if V or T is an uninstantiated generic type.

func DefPredeclaredTestFuncs

func DefPredeclaredTestFuncs()

DefPredeclaredTestFuncs defines the assert and trace built-ins. These built-ins are intended for debugging and testing of this package only.

func Id

func Id(pkg *Package, name string) string

Id returns name if it is exported, otherwise it returns the name qualified with the package path.

func Identical

func Identical(x, y Type) bool

Identical reports whether x and y are identical types. Receivers of Signature types are ignored.

func IdenticalIgnoreTags

func IdenticalIgnoreTags(x, y Type) bool

IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored. Receivers of Signature types are ignored.

func Implements

func Implements(V Type, T *Interface) bool

Implements reports whether type V implements interface T.

The behavior of Implements is undefined if V is an uninstantiated generic type.

func IsInterface

func IsInterface(t Type) bool

IsInterface reports whether t is an interface type.

func ObjectString

func ObjectString(obj Object, qf Qualifier) string

ObjectString returns the string form of obj. The Qualifier controls the printing of package-level objects, and may be nil.

func SelectionString

func SelectionString(s *Selection, qf Qualifier) string

SelectionString returns the string form of s. The Qualifier controls the printing of package-level objects, and may be nil.

Examples:

"field (T) f int"
"method (T) f(X) Y"
"method expr (T) f(X) Y"

func TypeString

func TypeString(typ Type, qf Qualifier) string

TypeString returns the string representation of typ. The Qualifier controls the printing of package-level objects, and may be nil.

func WriteFunction

func WriteFunction(buf *bytes.Buffer, f *Function)

WriteFunction writes to buf a human-readable "disassembly" of f.

func WritePackage

func WritePackage(buf *bytes.Buffer, p *SSAPackage)

WritePackage writes to buf a human-readable summary of p.

func WriteSignature

func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier)

WriteSignature writes the representation of the signature sig to buf, without a leading "func" keyword. The Qualifier controls the printing of package-level objects, and may be nil.

func WriteType

func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier)

WriteType writes the string representation of typ to buf. The Qualifier controls the printing of package-level objects, and may be nil.

Types

type Alloc

type Alloc struct {
	Comment string
	Heap    bool
	// contains filtered or unexported fields
}

The Alloc instruction reserves space for a variable of the given type, zero-initializes it, and yields its address.

Alloc values are always addresses, and have pointer types, so the type of the allocated variable is actually Type().Underlying().(*types.Pointer).Elem().

If Heap is false, Alloc allocates space in the function's activation record (frame); we refer to an Alloc(Heap=false) as a "local" alloc. Each local Alloc returns the same address each time it is executed within the same activation; the space is re-initialized to zero.

If Heap is true, Alloc allocates space in the heap; we refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc returns a different address each time it is executed.

When Alloc is applied to a channel, map or slice type, it returns the address of an uninitialized (nil) reference of that kind; store the result of MakeSlice, MakeMap or MakeChan in that location to instantiate these types.

Pos() returns the syntax.CompositeLit.Lbrace for a composite literal, or the syntax.CallExpr.Rparen for a call to new() or for a call that allocates a varargs slice.

Example printed form:

t0 = local int
t1 = new int

func (*Alloc) Name

func (v *Alloc) Name() string

func (*Alloc) Operands

func (v *Alloc) Operands(rands []*Value) []*Value

func (*Alloc) Pos

func (v *Alloc) Pos() Pos

func (*Alloc) Referrers

func (v *Alloc) Referrers() *[]Instruction

func (*Alloc) String

func (v *Alloc) String() string

func (*Alloc) Type

func (v *Alloc) Type() Type

type ArgumentError

type ArgumentError struct {
	Index int
	Err   error
}

An ArgumentError holds an error associated with an argument index.

func (*ArgumentError) Error

func (e *ArgumentError) Error() string

func (*ArgumentError) Unwrap

func (e *ArgumentError) Unwrap() error

type Array

type Array struct {
	// contains filtered or unexported fields
}

An Array represents an array type.

func NewArray

func NewArray(elem Type, len int64) *Array

NewArray returns a new array type for the given element type and length. A negative length indicates an unknown length.

func (*Array) Elem

func (a *Array) Elem() Type

Elem returns element type of array a.

func (*Array) Len

func (a *Array) Len() int64

Len returns the length of array a. A negative result indicates an unknown length.

func (*Array) String

func (a *Array) String() string

func (*Array) Underlying

func (a *Array) Underlying() Type

type Basic

type Basic struct {
	// contains filtered or unexported fields
}

A Basic represents a basic type.

func (*Basic) Info

func (b *Basic) Info() BasicInfo

Info returns information about properties of basic type b.

func (*Basic) Kind

func (b *Basic) Kind() BasicKind

Kind returns the kind of basic type b.

func (*Basic) Name

func (b *Basic) Name() string

Name returns the name of basic type b.

func (*Basic) String

func (b *Basic) String() string

func (*Basic) Underlying

func (b *Basic) Underlying() Type

type BasicBlock

type BasicBlock struct {
	Index   int    // index of this block within Parent().Blocks
	Comment string // optional label; no semantic significance

	Instrs       []Instruction // instructions in order
	Preds, Succs []*BasicBlock // predecessors and successors
	// contains filtered or unexported fields
}

BasicBlock represents an SSA basic block.

The final element of Instrs is always an explicit transfer of control (If, Jump, Return, or Panic).

A block may contain no Instructions only if it is unreachable, i.e., Preds is nil. Empty blocks are typically pruned.

BasicBlocks and their Preds/Succs relation form a (possibly cyclic) graph independent of the SSA Value graph: the control-flow graph or CFG. It is illegal for multiple edges to exist between the same pair of blocks.

Each BasicBlock is also a node in the dominator tree of the CFG. The tree may be navigated using Idom()/Dominees() and queried using Dominates().

The order of Preds and Succs is significant (to Phi and If instructions, respectively).

func (*BasicBlock) Dominates

func (b *BasicBlock) Dominates(c *BasicBlock) bool

Dominates reports whether b dominates c.

func (*BasicBlock) Dominees

func (b *BasicBlock) Dominees() []*BasicBlock

Dominees returns the list of blocks that b immediately dominates: its children in the dominator tree.

func (*BasicBlock) Idom

func (b *BasicBlock) Idom() *BasicBlock

Idom returns the block that immediately dominates b: its parent in the dominator tree, if any. Neither the entry node (b.Index==0) nor recover node (b==b.Parent().Recover()) have a parent.

func (*BasicBlock) Parent

func (b *BasicBlock) Parent() *Function

Parent returns the function that contains block b.

func (*BasicBlock) String

func (b *BasicBlock) String() string

String returns a human-readable label of this block. It is not guaranteed unique within the function.

type BasicInfo

type BasicInfo int

BasicInfo is a set of flags describing properties of a basic type.

const (
	IsBoolean BasicInfo = 1 << iota
	IsInteger
	IsUnsigned
	IsFloat
	IsComplex
	IsString
	IsUntyped

	IsOrdered   = IsInteger | IsFloat | IsString
	IsNumeric   = IsInteger | IsFloat | IsComplex
	IsConstType = IsBoolean | IsNumeric | IsString
)

Properties of basic types.

type BasicKind

type BasicKind int

BasicKind describes the kind of basic type.

const (
	Invalid BasicKind = iota // type is invalid

	// predeclared types
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	String
	UnsafePointer

	// types for untyped values
	UntypedBool
	UntypedInt
	UntypedRune
	UntypedFloat
	UntypedComplex
	UntypedString
	UntypedNil

	// aliases
	Byte = Uint8
	Rune = Int32
)

type BinOp

type BinOp struct {

	// One of:
	// ADD SUB MUL QUO REM          + - * / %
	// AND OR XOR SHL SHR AND_NOT   & | ^ << >> &^
	// EQL NEQ LSS LEQ GTR GEQ      == != < <= < >=
	Op   Operator
	X, Y Value
	// contains filtered or unexported fields
}

The BinOp instruction yields the result of binary operation X Op Y.

Pos() returns the syntax.BinaryExpr.OpPos, if explicit in the source.

Example printed form:

t1 = t0 + 1:int

func (*BinOp) Name

func (v *BinOp) Name() string

func (*BinOp) Operands

func (v *BinOp) Operands(rands []*Value) []*Value

func (*BinOp) Pos

func (v *BinOp) Pos() Pos

func (*BinOp) Referrers

func (v *BinOp) Referrers() *[]Instruction

func (*BinOp) String

func (v *BinOp) String() string

func (*BinOp) Type

func (v *BinOp) Type() Type

type BuilderMode

type BuilderMode uint

BuilderMode is a bitmask of options for diagnostics and checking.

*BuilderMode satisfies the flag.Value interface. Example:

var mode = ssa.BuilderMode(0)
func init() { flag.Var(&mode, "build", ssa.BuilderModeDoc) }
const (
	PrintPackages        BuilderMode = 1 << iota // Print package inventory to stdout
	PrintFunctions                               // Print function SSA code to stdout
	LogSource                                    // Log source locations as SSA builder progresses
	SanityCheckFunctions                         // Perform sanity checking of function bodies
	NaiveForm                                    // Build naïve SSA form: don't replace local loads/stores with registers
	BuildSerially                                // Build packages serially, not in parallel.
	GlobalDebug                                  // Enable debug info for all packages
	BareInits                                    // Build init functions without guards or calls to dependent inits
)

func (BuilderMode) Get

func (m BuilderMode) Get() interface{}

Get returns m.

func (*BuilderMode) Set

func (m *BuilderMode) Set(s string) error

Set parses the flag characters in s and updates *m.

func (BuilderMode) String

func (m BuilderMode) String() string

type Builtin

type Builtin struct {
	// contains filtered or unexported fields
}

A Builtin represents a built-in function. Builtins don't have a valid type.

func (*Builtin) Exported

func (obj *Builtin) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Builtin) Id

func (obj *Builtin) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Builtin) Name

func (obj *Builtin) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Builtin) Parent

func (obj *Builtin) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Builtin) Pkg

func (obj *Builtin) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Builtin) Pos

func (obj *Builtin) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Builtin) String

func (obj *Builtin) String() string

func (*Builtin) Type

func (obj *Builtin) Type() Type

Type returns the object's type.

type Call

type Call struct {
	Call CallCommon
	// contains filtered or unexported fields
}

The Call instruction represents a function or method call.

The Call instruction yields the function result if there is exactly one. Otherwise it returns a tuple, the components of which are accessed via Extract.

See CallCommon for generic function call documentation.

Pos() returns the syntax.CallExpr.Lparen, if explicit in the source.

Example printed form:

t2 = println(t0, t1)
t4 = t3()
t7 = invoke t5.Println(...t6)

func (*Call) Common

func (s *Call) Common() *CallCommon

func (*Call) Name

func (v *Call) Name() string

func (*Call) Operands

func (s *Call) Operands(rands []*Value) []*Value

func (*Call) Pos

func (v *Call) Pos() Pos

func (*Call) Referrers

func (v *Call) Referrers() *[]Instruction

func (*Call) String

func (v *Call) String() string

func (*Call) Type

func (v *Call) Type() Type

func (*Call) Value

func (s *Call) Value() *Call

type CallCommon

type CallCommon struct {
	Value  Value   // receiver (invoke mode) or func value (call mode)
	Method *Func   // abstract method (invoke mode)
	Args   []Value // actual parameters (in static method call, includes receiver)
	// contains filtered or unexported fields
}

CallCommon is contained by Go, Defer and Call to hold the common parts of a function or method call.

Each CallCommon exists in one of two modes, function call and interface method invocation, or "call" and "invoke" for short.

1. "call" mode: when Method is nil (!IsInvoke), a CallCommon represents an ordinary function call of the value in Value, which may be a *Builtin, a *Function or any other value of kind 'func'.

Value may be one of:

(a) a *Function, indicating a statically dispatched call
    to a package-level function, an anonymous function, or
    a method of a named type.
(b) a *MakeClosure, indicating an immediately applied
    function literal with free variables.
(c) a *Builtin, indicating a statically dispatched call
    to a built-in function.
(d) any other value, indicating a dynamically dispatched
    function call.

StaticCallee returns the identity of the callee in cases (a) and (b), nil otherwise.

Args contains the arguments to the call. If Value is a method, Args[0] contains the receiver parameter.

Example printed form:

t2 = println(t0, t1)
go t3()
defer t5(...t6)

2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon represents a dynamically dispatched call to an interface method. In this mode, Value is the interface value and Method is the interface's abstract method. Note: an abstract method may be shared by multiple interfaces due to embedding; Value.Type() provides the specific interface used for this call.

Value is implicitly supplied to the concrete method implementation as the receiver parameter; in other words, Args[0] holds not the receiver but the first true argument.

Example printed form:

t1 = invoke t0.String()
go invoke t3.Run(t2)
defer invoke t4.Handle(...t5)

For all calls to variadic functions (Signature().Variadic()), the last element of Args is a slice.

func (*CallCommon) Description

func (c *CallCommon) Description() string

Description returns a description of the mode of this call suitable for a user interface, e.g., "static method call".

func (*CallCommon) IsInvoke

func (c *CallCommon) IsInvoke() bool

IsInvoke returns true if this call has "invoke" (not "call") mode.

func (*CallCommon) Operands

func (c *CallCommon) Operands(rands []*Value) []*Value

func (*CallCommon) Pos

func (c *CallCommon) Pos() Pos

func (*CallCommon) Signature

func (c *CallCommon) Signature() *Signature

Signature returns the signature of the called function.

For an "invoke"-mode call, the signature of the interface method is returned.

In either "call" or "invoke" mode, if the callee is a method, its receiver is represented by sig.Recv, not sig.Params().At(0).

func (*CallCommon) StaticCallee

func (c *CallCommon) StaticCallee() *Function

StaticCallee returns the callee if this is a trivially static "call"-mode call to a function.

func (*CallCommon) String

func (c *CallCommon) String() string

type CallInstruction

type CallInstruction interface {
	Instruction
	Common() *CallCommon // returns the common parts of the call
	Value() *Call        // returns the result value of the call (*Call) or nil (*Go, *Defer)
}

The CallInstruction interface, implemented by *Go, *Defer and *Call, exposes the common parts of function-calling instructions, yet provides a way back to the Value defined by *Call alone.

type Chan

type Chan struct {
	// contains filtered or unexported fields
}

A Chan represents a channel type.

func NewChan

func NewChan(dir ChanDir, elem Type) *Chan

NewChan returns a new channel type for the given direction and element type.

func (*Chan) Dir

func (c *Chan) Dir() ChanDir

Dir returns the direction of channel c.

func (*Chan) Elem

func (c *Chan) Elem() Type

Elem returns the element type of channel c.

func (*Chan) String

func (c *Chan) String() string

func (*Chan) Underlying

func (c *Chan) Underlying() Type

type ChangeInterface

type ChangeInterface struct {
	X Value
	// contains filtered or unexported fields
}

ChangeInterface constructs a value of one interface type from a value of another interface type known to be assignable to it. This operation cannot fail.

Pos() returns the syntax.CallExpr.Lparen if the instruction arose from an explicit T(e) conversion; the syntax.AssertExpr.Lparen if the instruction arose from an explicit e.(T) operation; or syntax.NoPos otherwise.

Example printed form:

t1 = change interface interface{} <- I (t0)

func (*ChangeInterface) Name

func (v *ChangeInterface) Name() string

func (*ChangeInterface) Operands

func (v *ChangeInterface) Operands(rands []*Value) []*Value

func (*ChangeInterface) Pos

func (v *ChangeInterface) Pos() Pos

func (*ChangeInterface) Referrers

func (v *ChangeInterface) Referrers() *[]Instruction

func (*ChangeInterface) String

func (v *ChangeInterface) String() string

func (*ChangeInterface) Type

func (v *ChangeInterface) Type() Type

type ChangeType

type ChangeType struct {
	X Value
	// contains filtered or unexported fields
}

The ChangeType instruction applies to X a value-preserving type change to Type().

Type changes are permitted:

  • between a named type and its underlying type.
  • between two named types of the same underlying type.
  • between (possibly named) pointers to identical base types.
  • from a bidirectional channel to a read- or write-channel, optionally adding/removing a name.

This operation cannot fail dynamically.

Pos() returns the syntax.CallExpr.Lparen, if the instruction arose from an explicit conversion in the source.

Example printed form:

t1 = changetype *int <- IntPtr (t0)

func (*ChangeType) Name

func (v *ChangeType) Name() string

func (*ChangeType) Operands

func (v *ChangeType) Operands(rands []*Value) []*Value

func (*ChangeType) Pos

func (v *ChangeType) Pos() Pos

func (*ChangeType) Referrers

func (v *ChangeType) Referrers() *[]Instruction

func (*ChangeType) String

func (v *ChangeType) String() string

func (*ChangeType) Type

func (v *ChangeType) Type() Type

type Checker

type Checker struct {
	*Info
	// contains filtered or unexported fields
}

A Checker maintains the state of the type checker. It must be created with NewChecker.

func NewChecker

func NewChecker(conf *Config, pkg *Package, info *Info) *Checker

NewChecker returns a new Checker instance for a given package. Package files may be added incrementally via checker.Files.

func (*Checker) Files

func (check *Checker) Files(files []*File) error

Files checks the provided files as part of the checker's package.

type Config

type Config struct {
	// Context is the context used for resolving global identifiers. If nil, the
	// type checker will initialize this field with a newly created context.
	Context *Context

	// Prog is the SSA program to use for creating SSA packages.
	// If nil, then SSA packages are not created.
	Prog *Program

	// GoVersion describes the accepted Go language version. The string
	// must follow the format "go%d.%d" (e.g. "go1.12") or ist must be
	// empty; an empty string indicates the latest language version.
	// If the format is invalid, invoking the type checker will cause a
	// panic.
	GoVersion string

	// If IgnoreFuncBodies is set, function bodies are not
	// type-checked.
	IgnoreFuncBodies bool

	// If FakeImportC is set, `import "C"` (for packages requiring Cgo)
	// declares an empty "C" package and errors are omitted for qualified
	// identifiers referring to package C (which won't find an object).
	// This feature is intended for the standard library cmd/api tool.
	//
	// Caution: Effects may be unpredictable due to follow-on errors.
	//          Do not use casually!
	FakeImportC bool

	// If IgnoreLabels is set, correct label use is not checked.
	// TODO(gri) Consolidate label checking and remove this flag.
	IgnoreLabels bool

	// If CompilerErrorMessages is set, errors are reported using
	// cmd/compile error strings to match $GOROOT/test errors.
	// TODO(gri) Consolidate error messages and remove this flag.
	CompilerErrorMessages bool

	// If UsesCgo is set, the type checker expects the
	// _cgo_gotypes.go file generated by running cmd/cgo to be
	// provided as a package source file. Qualified identifiers
	// referring to package C will be resolved to cgo-provided
	// declarations within _cgo_gotypes.go.
	//
	// It is an error to set both FakeImportC and UsesCgo.
	UsesCgo bool

	// If Trace is set, a debug trace is printed to stdout.
	Trace bool

	// If Error != nil, it is called with each error found
	// during type checking; err has dynamic type Error.
	// Secondary errors (for instance, to enumerate all types
	// involved in an invalid recursive type declaration) have
	// error strings that start with a '\t' character.
	// If Error == nil, type-checking stops with the first
	// error found.
	Error func(err error)

	// An importer is used to import packages referred to from
	// import declarations.
	// If the installed importer implements ImporterFrom, the type
	// checker calls ImportFrom instead of Import.
	// The type checker reports an error if an importer is needed
	// but none was installed.
	Importer Importer

	// If Sizes != nil, it provides the sizing functions for package unsafe.
	// Otherwise SizesFor("gc", "amd64") is used instead.
	Sizes Sizes

	// If DisableUnusedImportCheck is set, packages are not checked
	// for unused imports.
	DisableUnusedImportCheck bool
}

A Config specifies the configuration for type checking. The zero value for Config is a ready-to-use default configuration.

func (*Config) Check

func (conf *Config) Check(path string, files []*File, info *Info) (*Package, error)

Check type-checks a package and returns the resulting package object and the first error if any. Additionally, if info != nil, Check populates each of the non-nil maps in the Info struct.

The package is marked as complete if no errors occurred, otherwise it is incomplete. See Config.Error for controlling behavior in the presence of errors.

The package is specified by a list of *syntax.Files and corresponding file set, and the package path the package is identified with. The clean path must not be empty or dot (".").

type Const

type Const struct {
	// contains filtered or unexported fields
}

A Const represents a declared constant.

func NewConst

func NewConst(pos Pos, pkg *Package, name string, typ Type, val constant.Value) *Const

NewConst returns a new constant with value val. The remaining arguments set the attributes found with all Objects.

func (*Const) Exported

func (obj *Const) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Const) Id

func (obj *Const) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Const) Name

func (obj *Const) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Const) Parent

func (obj *Const) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Const) Pkg

func (obj *Const) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Const) Pos

func (obj *Const) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Const) String

func (obj *Const) String() string

func (*Const) Type

func (obj *Const) Type() Type

Type returns the object's type.

func (*Const) Val

func (obj *Const) Val() constant.Value

Val returns the constant's value.

type Context

type Context struct {
	// contains filtered or unexported fields
}

An Context is an opaque type checking context. It may be used to share identical type instances across type-checked packages or calls to Instantiate.

It is safe for concurrent use.

func NewContext

func NewContext() *Context

NewContext creates a new Context.

type Convert

type Convert struct {
	X Value
	// contains filtered or unexported fields
}

The Convert instruction yields the conversion of value X to type Type(). One or both of those types is basic (but possibly named).

A conversion may change the value and representation of its operand. Conversions are permitted:

  • between real numeric types.
  • between complex numeric types.
  • between string and []byte or []rune.
  • between pointers and unsafe.Pointer.
  • between unsafe.Pointer and uintptr.
  • from (Unicode) integer to (UTF-8) string.

A conversion may imply a type name change also.

This operation cannot fail dynamically.

Conversions of untyped string/number/bool constants to a specific representation are eliminated during SSA construction.

Pos() returns the syntax.CallExpr.Lparen, if the instruction arose from an explicit conversion in the source.

Example printed form:

t1 = convert []byte <- string (t0)

func (*Convert) Name

func (v *Convert) Name() string

func (*Convert) Operands

func (v *Convert) Operands(rands []*Value) []*Value

func (*Convert) Pos

func (v *Convert) Pos() Pos

func (*Convert) Referrers

func (v *Convert) Referrers() *[]Instruction

func (*Convert) String

func (v *Convert) String() string

func (*Convert) Type

func (v *Convert) Type() Type

type DebugRef

type DebugRef struct {
	Expr Expr // the referring expression (never *syntax.ParenExpr)

	IsAddr bool  // Expr is addressable and X is the address it denotes
	X      Value // the value or address of Expr
	// contains filtered or unexported fields
}

A DebugRef instruction maps a source-level expression Expr to the SSA value X that represents the value (!IsAddr) or address (IsAddr) of that expression.

DebugRef is a pseudo-instruction: it has no dynamic effect.

Pos() returns Expr.Pos(), the start position of the source-level expression. This is not the same as the "designated" token as documented at Value.Pos(). e.g. CallExpr.Pos() does not return the position of the ("designated") Lparen token.

If Expr is an *syntax.Name denoting a var or func, Object() returns the object; though this information can be obtained from the type checker, including it here greatly facilitates debugging. For non-Ident expressions, Object() returns nil.

DebugRefs are generated only for functions built with debugging enabled; see Package.SetDebugMode() and the GlobalDebug builder mode flag.

DebugRefs are not emitted for syntax.Idents referring to constants or predeclared identifiers, since they are trivial and numerous. Nor are they emitted for syntax.ParenExprs.

(By representing these as instructions, rather than out-of-band, consistency is maintained during transformation passes by the ordinary SSA renaming machinery.)

Example printed form:

; *syntax.CallExpr @ 102:9 is t5
; var x float64 @ 109:72 is x
; address of *syntax.CompositeLit @ 216:10 is t0

func (*DebugRef) Block

func (v *DebugRef) Block() *BasicBlock

func (*DebugRef) Object

func (d *DebugRef) Object() Object

func (*DebugRef) Operands

func (s *DebugRef) Operands(rands []*Value) []*Value

func (*DebugRef) Parent

func (v *DebugRef) Parent() *Function

func (*DebugRef) Pos

func (s *DebugRef) Pos() Pos

func (*DebugRef) Referrers

func (v *DebugRef) Referrers() *[]Instruction

func (*DebugRef) String

func (s *DebugRef) String() string

type Extract

type Extract struct {
	Tuple Value
	Index int
	// contains filtered or unexported fields
}

The Extract instruction yields component Index of Tuple.

This is used to access the results of instructions with multiple return values, such as Call, TypeAssert, Next, UnOp(ARROW) and IndexExpr(Map).

Example printed form:

t1 = extract t0 #1

func (*Extract) Name

func (v *Extract) Name() string

func (*Extract) Operands

func (v *Extract) Operands(rands []*Value) []*Value

func (*Extract) Pos

func (v *Extract) Pos() Pos

func (*Extract) Referrers

func (v *Extract) Referrers() *[]Instruction

func (*Extract) String

func (v *Extract) String() string

func (*Extract) Type

func (v *Extract) Type() Type

type FieldAddr

type FieldAddr struct {
	X     Value // *struct
	Field int   // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field)
	// contains filtered or unexported fields
}

The FieldAddr instruction yields the address of Field of *struct X.

The field is identified by its index within the field list of the struct type of X.

Dynamically, this instruction panics if X evaluates to a nil pointer.

Type() returns a (possibly named) *types.Pointer.

Pos() returns the position of the syntax.SelectorExpr.Sel for the field, if explicit in the source.

Example printed form:

t1 = &t0.name [#1]

func (*FieldAddr) Name

func (v *FieldAddr) Name() string

func (*FieldAddr) Operands

func (v *FieldAddr) Operands(rands []*Value) []*Value

func (*FieldAddr) Pos

func (v *FieldAddr) Pos() Pos

func (*FieldAddr) Referrers

func (v *FieldAddr) Referrers() *[]Instruction

func (*FieldAddr) String

func (v *FieldAddr) String() string

func (*FieldAddr) Type

func (v *FieldAddr) Type() Type

type FreeVar

type FreeVar struct {
	// contains filtered or unexported fields
}

A FreeVar represents a free variable of the function to which it belongs.

FreeVars are used to implement anonymous functions, whose free variables are lexically captured in a closure formed by MakeClosure. The value of such a free var is an Alloc or another FreeVar and is considered a potentially escaping heap address, with pointer type.

FreeVars are also used to implement bound method closures. Such a free var represents the receiver value and may be of any type that has concrete methods.

Pos() returns the position of the value that was captured, which belongs to an enclosing function.

func (*FreeVar) Name

func (v *FreeVar) Name() string

func (*FreeVar) Operands

func (v *FreeVar) Operands(rands []*Value) []*Value

func (*FreeVar) Parent

func (v *FreeVar) Parent() *Function

func (*FreeVar) Pos

func (v *FreeVar) Pos() Pos

func (*FreeVar) Referrers

func (v *FreeVar) Referrers() *[]Instruction

func (*FreeVar) String

func (v *FreeVar) String() string

func (*FreeVar) Type

func (v *FreeVar) Type() Type

type Func

type Func struct {
	// contains filtered or unexported fields
}

A Func represents a declared function, concrete method, or abstract (interface) method. Its Type() is always a *Signature. An abstract method may belong to many interfaces due to embedding.

func MissingMethod

func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool)

MissingMethod returns (nil, false) if V implements T, otherwise it returns a missing method required by T and whether it is missing or just has the wrong type.

For non-interface types V, or if static is set, V implements T if all methods of T are present in V. Otherwise (V is an interface and static is not set), MissingMethod only checks that methods of T which are also present in V have matching types (e.g., for a type assertion x.(T) where x is of interface type V).

func NewFunc

func NewFunc(pos Pos, pkg *Package, name string, sig *Signature) *Func

NewFunc returns a new function with the given signature, representing the function's type.

func NewFuncLit

func NewFuncLit(pos Pos, pkg *Package, sig *Signature) *Func

NewFuncLit returns a new function representing a function literal.

func StaticCallee

func StaticCallee(info *Info, call *CallExpr) *Func

StaticCallee returns the target (function or method) of a static function call, if any. It returns nil for calls to builtins.

func (*Func) Exported

func (obj *Func) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Func) FullName

func (obj *Func) FullName() string

FullName returns the package- or receiver-type-qualified name of function or method obj.

func (*Func) Id

func (obj *Func) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Func) Name

func (obj *Func) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Func) Parent

func (obj *Func) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Func) Pkg

func (obj *Func) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Func) Pos

func (obj *Func) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Func) Scope

func (obj *Func) Scope() *Scope

Scope returns the scope of the function's body block. The result is nil for imported or instantiated functions and methods (but there is also no mechanism to get to an instantiated function).

func (*Func) String

func (obj *Func) String() string

func (*Func) Type

func (obj *Func) Type() Type

Type returns the object's type.

type Function

type Function struct {
	Signature *Signature

	Synthetic string // provenance of synthetic function; "" for true source functions

	Pkg       *SSAPackage   // enclosing package; nil for shared funcs (wrappers and error.Error)
	Params    []*Parameter  // function parameters; for methods, includes receiver
	FreeVars  []*FreeVar    // free variables whose values must be supplied by closure
	Locals    []*Alloc      // local variables of this function
	Blocks    []*BasicBlock // basic blocks of the function; nil => external
	Recover   *BasicBlock   // optional; control transfers here after recovered panic
	AnonFuncs []*Function   // anonymous functions directly beneath this one
	// contains filtered or unexported fields
}

Function represents the parameters, results, and code of a function or method.

If Blocks is nil, this indicates an external function for which no Go source code is available. In this case, FreeVars and Locals are nil too. Clients performing whole-program analysis must handle external functions specially.

Blocks contains the function's control-flow graph (CFG). Blocks[0] is the function entry point; block order is not otherwise semantically significant, though it may affect the readability of the disassembly. To iterate over the blocks in dominance order, use DomPreorder().

Recover is an optional second entry point to which control resumes after a recovered panic. The Recover block may contain only a return statement, preceded by a load of the function's named return parameters, if any.

A nested function (Parent()!=nil) that refers to one or more lexically enclosing local variables ("free variables") has FreeVars. Such functions cannot be called directly but require a value created by MakeClosure which, via its Bindings, supplies values for these parameters.

If the function is a method (Signature.Recv() != nil) then the first element of Params is the receiver parameter.

A Go package may declare many functions called "init". For each one, Object().Name() returns "init" but Name() returns "init#1", etc, in declaration order.

Pos() returns the declaring syntax.FuncLit.Type.Func or the position of the syntax.FuncDecl.Name, if the function was explicit in the source. Synthetic wrappers, for which Synthetic != "", may share the same position as the function they wrap. Syntax.Pos() always returns the position of the declaring "func" token.

Type() returns the function's Signature.

func (*Function) DomPreorder

func (f *Function) DomPreorder() []*BasicBlock

DomPreorder returns a new slice containing the blocks of f in dominator tree preorder.

func (*Function) Name

func (v *Function) Name() string

func (*Function) Object

func (v *Function) Object() Object

func (*Function) Operands

func (v *Function) Operands(rands []*Value) []*Value

func (*Function) Parent

func (v *Function) Parent() *Function

func (*Function) Pos

func (v *Function) Pos() Pos

func (*Function) Referrers

func (v *Function) Referrers() *[]Instruction

func (*Function) RelString

func (f *Function) RelString(from *Package) string

RelString returns the full name of this function, qualified by package name, receiver type, etc.

The specific formatting rules are not guaranteed and may change.

Examples:

"math.IsNaN"                  // a package-level function
"(*bytes.Buffer).Bytes"       // a declared method or a wrapper
"(*bytes.Buffer).Bytes$thunk" // thunk (func wrapping method; receiver is param 0)
"(*bytes.Buffer).Bytes$bound" // bound (func wrapping method; receiver supplied by closure)
"main.main$1"                 // an anonymous function in main
"main.init#1"                 // a declared init function
"main.init"                   // the synthesized package initializer

When these functions are referred to from within the same package (i.e. from == f.Pkg.Object), they are rendered without the package path. For example: "IsNaN", "(*Buffer).Bytes", etc.

All non-synthetic functions have distinct package-qualified names. (But two methods may have the same name "(T).f" if one is a synthetic wrapper promoting a non-exported method "f" from another package; in that case, the strings are equal but the identifiers "f" are distinct.)

func (*Function) String

func (v *Function) String() string

func (*Function) Type

func (v *Function) Type() Type

func (*Function) WriteTo

func (f *Function) WriteTo(w io.Writer) (int64, error)

type Global

type Global struct {
	// contains filtered or unexported fields
}

A Global is a named Value holding the address of a package-level variable.

Pos() returns the position of the syntax.ValueSpec.Names[*] identifier.

func (*Global) Name

func (v *Global) Name() string

func (*Global) Object

func (v *Global) Object() Object

func (*Global) Operands

func (v *Global) Operands(rands []*Value) []*Value

func (*Global) Parent

func (v *Global) Parent() *Function

func (*Global) Pos

func (v *Global) Pos() Pos

func (*Global) Referrers

func (v *Global) Referrers() *[]Instruction

func (*Global) RelString

func (v *Global) RelString(from *Package) string

func (*Global) String

func (v *Global) String() string

func (*Global) Type

func (v *Global) Type() Type

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

A Hasher maps each type to its hash value. For efficiency, a hasher uses memoization; thus its memory footprint grows monotonically over time. Hashers are not thread-safe. Hashers have reference semantics. Call MakeHasher to create a Hasher.

func MakeHasher

func MakeHasher() Hasher

MakeHasher returns a new Hasher instance.

func (Hasher) Hash

func (h Hasher) Hash(t Type) uint32

Hash computes a hash value for the given type t such that Identical(t, t') => Hash(t) == Hash(t').

type If

type If struct {
	Cond Value
	// contains filtered or unexported fields
}

The If instruction transfers control to one of the two successors of its owning block, depending on the boolean Cond: the first if true, the second if false.

An If instruction must be the last instruction of its containing BasicBlock.

Pos() returns NoPos.

Example printed form:

if t0 goto done else body

func (*If) Block

func (v *If) Block() *BasicBlock

func (*If) Operands

func (s *If) Operands(rands []*Value) []*Value

func (*If) Parent

func (v *If) Parent() *Function

func (*If) Pos

func (s *If) Pos() Pos

func (*If) Referrers

func (v *If) Referrers() *[]Instruction

func (*If) String

func (s *If) String() string

type Importer

type Importer interface {
	// Import returns the imported package for the given import
	// path when imported by a package file located in srcDir.
	// If the import failed, besides returning an error, ImportFrom
	// is encouraged to cache and return a package anyway, if one
	// was created. This will reduce package inconsistencies and
	// follow-on type checker errors due to the missing package.
	// Two calls to ImportFrom with the same path and dir must
	// return the same package.
	Import(path, srcDir string) (*Package, error)
}

An Importer resolves import paths to Packages.

type Index

type Index struct {
	X     Value // array
	Index Value // integer index
	// contains filtered or unexported fields
}

The Index instruction yields element Index of array X.

Pos() returns the syntax.IndexExpr.Lbrack for the index operation, if explicit in the source.

Example printed form:

t2 = t0[t1]

func (*Index) Name

func (v *Index) Name() string

func (*Index) Operands

func (v *Index) Operands(rands []*Value) []*Value

func (*Index) Pos

func (v *Index) Pos() Pos

func (*Index) Referrers

func (v *Index) Referrers() *[]Instruction

func (*Index) String

func (v *Index) String() string

func (*Index) Type

func (v *Index) Type() Type

type IndexAddr

type IndexAddr struct {
	X     Value // slice or *array,
	Index Value // integer index
	// contains filtered or unexported fields
}

The IndexAddr instruction yields the address of the element at index Index of collection X. Index is an integer expression.

The elements of maps and strings are not addressable; use Lookup or MapUpdate instead.

Dynamically, this instruction panics if X evaluates to a nil *array pointer.

Type() returns a (possibly named) *types.Pointer.

Pos() returns the syntax.IndexExpr.Lbrack for the index operation, if explicit in the source.

Example printed form:

t2 = &t0[t1]

func (*IndexAddr) Name

func (v *IndexAddr) Name() string

func (*IndexAddr) Operands

func (v *IndexAddr) Operands(rands []*Value) []*Value

func (*IndexAddr) Pos

func (v *IndexAddr) Pos() Pos

func (*IndexAddr) Referrers

func (v *IndexAddr) Referrers() *[]Instruction

func (*IndexAddr) String

func (v *IndexAddr) String() string

func (*IndexAddr) Type

func (v *IndexAddr) Type() Type

type Info

type Info struct {
	// Types maps expressions to their types, and for constant
	// expressions, also their values. Invalid expressions are
	// omitted.
	//
	// For (possibly parenthesized) identifiers denoting built-in
	// functions, the recorded signatures are call-site specific:
	// if the call result is not a constant, the recorded type is
	// an argument-specific signature. Otherwise, the recorded type
	// is invalid.
	//
	// The Types map does not record the type of every identifier,
	// only those that appear where an arbitrary expression is
	// permitted. For instance, the identifier f in a selector
	// expression x.f is found only in the Selections map, the
	// identifier z in a variable declaration 'var z int' is found
	// only in the Defs map, and identifiers denoting packages in
	// qualified identifiers are collected in the Uses map.
	Types map[Expr]TypeAndValue

	// Instances maps identifiers denoting generic types or functions to their
	// type arguments and instantiated type.
	//
	// For example, Instances will map the identifier for 'T' in the type
	// instantiation T[int, string] to the type arguments [int, string] and
	// resulting instantiated *Named type. Given a generic function
	// func F[A any](A), Instances will map the identifier for 'F' in the call
	// expression F(int(1)) to the inferred type arguments [int], and resulting
	// instantiated *Signature.
	//
	// Invariant: Instantiating Uses[id].Type() with Instances[id].TypeArgs
	// results in an equivalent of Instances[id].Type.
	Instances map[*Name]Instance

	// Defs maps identifiers to the objects they define (including
	// package names, dots "." of dot-imports, and blank "_" identifiers).
	// For identifiers that do not denote objects (e.g., the package name
	// in package clauses, or symbolic variables t in t := x.(type) of
	// type switch headers), the corresponding objects are nil.
	//
	// For an embedded field, Defs returns the field *Var it defines.
	//
	// Invariant: Defs[id] == nil || Defs[id].Pos() == id.Pos()
	Defs map[*Name]Object

	// Uses maps identifiers to the objects they denote.
	//
	// For an embedded field, Uses returns the *TypeName it denotes.
	//
	// Invariant: Uses[id].Pos() != id.Pos()
	Uses map[*Name]Object

	// Implicits maps nodes to their implicitly declared objects, if any.
	// The following node and object types may appear:
	//
	//     node               declared object
	//
	//     *syntax.ImportDecl    *PkgName for imports without renames
	//     *syntax.CaseClause    type-specific *Var for each type switch case clause (incl. default)
	//     *syntax.Field         anonymous parameter *Var (incl. unnamed results)
	//     *syntax.FuncLit       anonymous *Func
	//
	Implicits map[Node]Object

	// Selections maps selector expressions (excluding qualified identifiers)
	// to their corresponding selections.
	Selections map[*SelectorExpr]*Selection

	// Scopes maps syntax.Nodes to the scopes they define. Package scopes are not
	// associated with a specific node but with all files belonging to a package.
	// Thus, the package scope can be found in the type-checked Package object.
	// Scopes nest, with the Universe scope being the outermost scope, enclosing
	// the package scope, which contains (one or more) files scopes, which enclose
	// function scopes which in turn enclose statement and function literal scopes.
	// Note that even though package-level functions are declared in the package
	// scope, the function scopes are embedded in the file scope of the file
	// containing the function declaration.
	//
	// The following node types may appear in Scopes:
	//
	//     *syntax.File
	//     *syntax.FuncType
	//     *syntax.TypeDecl
	//     *syntax.BlockStmt
	//     *syntax.IfStmt
	//     *syntax.SwitchStmt
	//     *syntax.CaseClause
	//     *syntax.CommClause
	//     *syntax.ForStmt
	//
	Scopes map[Node]*Scope

	// InitOrder is the list of package-level initializers in the order in which
	// they must be executed. Initializers referring to variables related by an
	// initialization dependency appear in topological order, the others appear
	// in source order. Variables without an initialization expression do not
	// appear in this list.
	InitOrder []*Initializer
}

Info holds result type information for a type-checked package. Only the information for which a map is provided is collected. If the package has type errors, the collected information may be incomplete.

Example

ExampleInfo prints various facts recorded by the type checker in a types.Info struct: definitions of and references to each named object, and the type, value, and mode of every expression in the package.

// Parse a single source file.
const input = `
package fib

type S string

var a, b, c = len(b), S(c), "hello"

func fib(x int) int {
	if x < 2 {
		return x
	}
	return fib(x-1) - fib(x-2)
}`
f, err := parseSrc("fib.go", input)
if err != nil {
	log.Fatal(err)
}

// Type-check the package.
// We create an empty map for each kind of input
// we're interested in, and Check populates them.
info := types.Info{
	Types: make(map[Expr]types.TypeAndValue),
	Defs:  make(map[*Name]types.Object),
	Uses:  make(map[*Name]types.Object),
}
var conf types.Config
pkg, err := conf.Check("fib", []*File{f}, &info)
if err != nil {
	log.Fatal(err)
}

// Print package-level variables in initialization order.
fmt.Printf("InitOrder: %v\n\n", info.InitOrder)

// For each named object, print the line and
// column of its definition and each of its uses.
fmt.Println("Defs and Uses of each named object:")
usesByObj := make(map[types.Object][]string)
for id, obj := range info.Uses {
	posn := id.Pos()
	lineCol := fmt.Sprintf("%d:%d", posn.Line(), posn.Col())
	usesByObj[obj] = append(usesByObj[obj], lineCol)
}
var items []string
for obj, uses := range usesByObj {
	sort.Strings(uses)
	item := fmt.Sprintf("%s:\n  defined at %s\n  used at %s",
		types.ObjectString(obj, types.RelativeTo(pkg)),
		obj.Pos(),
		strings.Join(uses, ", "))
	items = append(items, item)
}
sort.Strings(items) // sort by line:col, in effect
fmt.Println(strings.Join(items, "\n"))
fmt.Println()

// TODO(gri) Enable once positions are updated/verified
// fmt.Println("Types and Values of each expression:")
// items = nil
// for expr, tv := range info.Types {
// 	var buf bytes.Buffer
// 	posn := expr.Pos()
// 	tvstr := tv.Type.String()
// 	if tv.Value != nil {
// 		tvstr += " = " + tv.Value.String()
// 	}
// 	// line:col | expr | mode : type = value
// 	fmt.Fprintf(&buf, "%2d:%2d | %-19s | %-7s : %s",
// 		posn.Line(), posn.Col(), types.ExprString(expr),
// 		mode(tv), tvstr)
// 	items = append(items, buf.String())
// }
// sort.Strings(items)
// fmt.Println(strings.Join(items, "\n"))
Output:

InitOrder: [c = "hello" b = S(c) a = len(b)]

Defs and Uses of each named object:
builtin len:
  defined at <unknown position>
  used at 6:15
func fib(x int) int:
  defined at fib.go:8:6
  used at 12:20, 12:9
type S string:
  defined at fib.go:4:6
  used at 6:23
type int:
  defined at <unknown position>
  used at 8:12, 8:17
type string:
  defined at <unknown position>
  used at 4:8
var b S:
  defined at fib.go:6:8
  used at 6:19
var c string:
  defined at fib.go:6:11
  used at 6:25
var x int:
  defined at fib.go:8:10
  used at 10:10, 12:13, 12:24, 9:5

func (*Info) ObjectOf

func (info *Info) ObjectOf(id *Name) Object

ObjectOf returns the object denoted by the specified id, or nil if not found.

If id is an embedded struct field, ObjectOf returns the field (*Var) it defines, not the type (*TypeName) it uses.

Precondition: the Uses and Defs maps are populated.

func (*Info) TypeOf

func (info *Info) TypeOf(e Expr) Type

TypeOf returns the type of expression e, or nil if not found. Precondition: the Types, Uses and Defs maps are populated.

type Initializer

type Initializer struct {
	Lhs []*Var // var Lhs = Rhs
	Rhs Expr
}

An Initializer describes a package-level variable, or a list of variables in case of a multi-valued initialization expression, and the corresponding initialization expression.

func (*Initializer) String

func (init *Initializer) String() string

type Instance

type Instance struct {
	TypeArgs *TypeList
	Type     Type
}

Instance reports the type arguments and instantiated type for type and function instantiations. For type instantiations, Type will be of dynamic type *Named. For function instantiations, Type will be of dynamic type *Signature.

type Instruction

type Instruction interface {
	// String returns the disassembled form of this value.
	//
	// Examples of Instructions that are Values:
	//       "x + y"     (BinOp)
	//       "len([])"   (Call)
	// Note that the name of the Value is not printed.
	//
	// Examples of Instructions that are not Values:
	//       "return x"  (Return)
	//       "*y = x"    (Store)
	//
	// (The separation Value.Name() from Value.String() is useful
	// for some analyses which distinguish the operation from the
	// value it defines, e.g., 'y = local int' is both an allocation
	// of memory 'local int' and a definition of a pointer y.)
	String() string

	// Parent returns the function to which this instruction
	// belongs.
	Parent() *Function

	// Block returns the basic block to which this instruction
	// belongs.
	Block() *BasicBlock

	// Operands returns the operands of this instruction: the
	// set of Values it references.
	//
	// Specifically, it appends their addresses to rands, a
	// user-provided slice, and returns the resulting slice,
	// permitting avoidance of memory allocation.
	//
	// The operands are appended in undefined order, but the order
	// is consistent for a given Instruction; the addresses are
	// always non-nil but may point to a nil Value.  Clients may
	// store through the pointers, e.g. to effect a value
	// renaming.
	//
	// Value.Referrers is a subset of the inverse of this
	// relation.  (Referrers are not tracked for all types of
	// Values.)
	Operands(rands []*Value) []*Value

	// Pos returns the location of the AST token most closely
	// associated with the operation that gave rise to this
	// instruction, or syntax.NoPos if it was not explicit in the
	// source.
	//
	// For each syntax.Node type, a particular token is designated as
	// the closest location for the expression, e.g. the Go token
	// for an *syntax.GoStmt.  This permits a compact but approximate
	// mapping from Instructions to source positions for use in
	// diagnostic messages, for example.
	//
	// (Do not use this position to determine which Instruction
	// corresponds to an syntax.Expr; see the notes for Value.Pos.
	// This position may be used to determine which non-Value
	// Instruction corresponds to some syntax.Stmts, but not all: If
	// and Jump instructions have no Pos(), for example.)
	Pos() Pos
	// contains filtered or unexported methods
}

An Instruction is an SSA instruction that computes a new Value or has some effect.

An Instruction that defines a value (e.g. BinOp) also implements the Value interface; an Instruction that only has an effect (e.g. Store) does not.

type Interface

type Interface struct {
	// contains filtered or unexported fields
}

An Interface represents an interface type.

func NewInterfaceType

func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface

NewInterfaceType returns a new interface for the given methods and embedded types. NewInterfaceType takes ownership of the provided methods and may modify their types by setting missing receivers.

func (*Interface) EmbeddedType

func (t *Interface) EmbeddedType(i int) Type

EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().

func (*Interface) Empty

func (t *Interface) Empty() bool

Empty reports whether t is the empty interface.

func (*Interface) ExplicitMethod

func (t *Interface) ExplicitMethod(i int) *Func

ExplicitMethod returns the i'th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods(). The methods are ordered by their unique Id.

func (*Interface) IsComparable

func (t *Interface) IsComparable() bool

IsComparable reports whether each type in interface t's type set is comparable.

func (*Interface) IsImplicit

func (t *Interface) IsImplicit() bool

IsImplicit reports whether the interface t is a wrapper for a type set literal.

func (*Interface) IsMethodSet

func (t *Interface) IsMethodSet() bool

IsMethodSet reports whether the interface t is fully described by its method set.

func (*Interface) MarkImplicit

func (t *Interface) MarkImplicit()

MarkImplicit marks the interface t as implicit, meaning this interface corresponds to a constraint literal such as ~T or A|B without explicit interface embedding. MarkImplicit should be called before any concurrent use of implicit interfaces.

func (*Interface) Method

func (t *Interface) Method(i int) *Func

Method returns the i'th method of interface t for 0 <= i < t.NumMethods(). The methods are ordered by their unique Id.

func (*Interface) NumEmbeddeds

func (t *Interface) NumEmbeddeds() int

NumEmbeddeds returns the number of embedded types in interface t.

func (*Interface) NumExplicitMethods

func (t *Interface) NumExplicitMethods() int

NumExplicitMethods returns the number of explicitly declared methods of interface t.

func (*Interface) NumMethods

func (t *Interface) NumMethods() int

NumMethods returns the total number of methods of interface t.

func (*Interface) String

func (t *Interface) String() string

func (*Interface) Underlying

func (t *Interface) Underlying() Type

type Jump

type Jump struct {
	// contains filtered or unexported fields
}

The Jump instruction transfers control to the sole successor of its owning block.

A Jump must be the last instruction of its containing BasicBlock.

Pos() returns NoPos.

Example printed form:

jump done

func (*Jump) Block

func (v *Jump) Block() *BasicBlock

func (*Jump) Operands

func (*Jump) Operands(rands []*Value) []*Value

func (*Jump) Parent

func (v *Jump) Parent() *Function

func (*Jump) Pos

func (s *Jump) Pos() Pos

func (*Jump) Referrers

func (v *Jump) Referrers() *[]Instruction

func (*Jump) String

func (s *Jump) String() string

type Label

type Label struct {
	// contains filtered or unexported fields
}

A Label represents a declared label. Labels don't have a type.

func NewLabel

func NewLabel(pos Pos, pkg *Package, name string) *Label

NewLabel returns a new label.

func (*Label) Exported

func (obj *Label) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Label) Id

func (obj *Label) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Label) Name

func (obj *Label) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Label) Parent

func (obj *Label) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Label) Pkg

func (obj *Label) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Label) Pos

func (obj *Label) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Label) String

func (obj *Label) String() string

func (*Label) Type

func (obj *Label) Type() Type

Type returns the object's type.

type Lookup

type Lookup struct {
	X       Value // string or map
	Index   Value // integer or key-typed index
	CommaOk bool  // return a value,ok pair
	// contains filtered or unexported fields
}

The Lookup instruction yields element Index of collection X, a map or string. Index is an integer expression if X is a string or the appropriate key type if X is a map.

If CommaOk, the result is a 2-tuple of the value above and a boolean indicating the result of a map membership test for the key. The components of the tuple are accessed using Extract.

Pos() returns the syntax.IndexExpr.Lbrack, if explicit in the source.

Example printed form:

t2 = t0[t1]
t5 = t3[t4],ok

func (*Lookup) Name

func (v *Lookup) Name() string

func (*Lookup) Operands

func (v *Lookup) Operands(rands []*Value) []*Value

func (*Lookup) Pos

func (v *Lookup) Pos() Pos

func (*Lookup) Referrers

func (v *Lookup) Referrers() *[]Instruction

func (*Lookup) String

func (v *Lookup) String() string

func (*Lookup) Type

func (v *Lookup) Type() Type

type MakeChan

type MakeChan struct {
	Size Value // int; size of buffer; zero => synchronous.
	// contains filtered or unexported fields
}

The MakeChan instruction creates a new channel object and yields a value of kind chan.

Type() returns a (possibly named) *types.Chan.

Pos() returns the syntax.CallExpr.Lparen for the make(chan) that created it.

Example printed form:

t0 = make chan int 0
t0 = make IntChan 0

func (*MakeChan) Name

func (v *MakeChan) Name() string

func (*MakeChan) Operands

func (v *MakeChan) Operands(rands []*Value) []*Value

func (*MakeChan) Pos

func (v *MakeChan) Pos() Pos

func (*MakeChan) Referrers

func (v *MakeChan) Referrers() *[]Instruction

func (*MakeChan) String

func (v *MakeChan) String() string

func (*MakeChan) Type

func (v *MakeChan) Type() Type

type MakeClosure

type MakeClosure struct {
	Fn       Value   // always a *Function
	Bindings []Value // values for each free variable in Fn.FreeVars
	// contains filtered or unexported fields
}

The MakeClosure instruction yields a closure value whose code is Fn and whose free variables' values are supplied by Bindings.

Type() returns a (possibly named) *types.Signature.

Pos() returns the syntax.FuncLit.Type.Func for a function literal closure or the syntax.SelectorExpr.Sel for a bound method closure.

Example printed form:

t0 = make closure anon@1.2 [x y z]
t1 = make closure bound$(main.I).add [i]

func (*MakeClosure) Name

func (v *MakeClosure) Name() string

func (*MakeClosure) Operands

func (v *MakeClosure) Operands(rands []*Value) []*Value

func (*MakeClosure) Pos

func (v *MakeClosure) Pos() Pos

func (*MakeClosure) Referrers

func (v *MakeClosure) Referrers() *[]Instruction

func (*MakeClosure) String

func (v *MakeClosure) String() string

func (*MakeClosure) Type

func (v *MakeClosure) Type() Type

type MakeInterface

type MakeInterface struct {
	X Value
	// contains filtered or unexported fields
}

MakeInterface constructs an instance of an interface type from a value of a concrete type.

Use Program.MethodSets.MethodSet(X.Type()) to find the method-set of X, and Program.MethodValue(m) to find the implementation of a method.

To construct the zero value of an interface type T, use:

NewConst(constant.MakeNil(), T, pos)

Pos() returns the syntax.CallExpr.Lparen, if the instruction arose from an explicit conversion in the source.

Example printed form:

t1 = make interface{} <- int (42:int)
t2 = make Stringer <- t0

func (*MakeInterface) Name

func (v *MakeInterface) Name() string

func (*MakeInterface) Operands

func (v *MakeInterface) Operands(rands []*Value) []*Value

func (*MakeInterface) Pos

func (v *MakeInterface) Pos() Pos

func (*MakeInterface) Referrers

func (v *MakeInterface) Referrers() *[]Instruction

func (*MakeInterface) String

func (v *MakeInterface) String() string

func (*MakeInterface) Type

func (v *MakeInterface) Type() Type

type MakeMap

type MakeMap struct {
	Reserve Value // initial space reservation; nil => default
	// contains filtered or unexported fields
}

The MakeMap instruction creates a new hash-table-based map object and yields a value of kind map.

Type() returns a (possibly named) *types.Map.

Pos() returns the syntax.CallExpr.Lparen, if created by make(map), or the syntax.CompositeLit.Lbrack if created by a literal.

Example printed form:

t1 = make map[string]int t0
t1 = make StringIntMap t0

func (*MakeMap) Name

func (v *MakeMap) Name() string

func (*MakeMap) Operands

func (v *MakeMap) Operands(rands []*Value) []*Value

func (*MakeMap) Pos

func (v *MakeMap) Pos() Pos

func (*MakeMap) Referrers

func (v *MakeMap) Referrers() *[]Instruction

func (*MakeMap) String

func (v *MakeMap) String() string

func (*MakeMap) Type

func (v *MakeMap) Type() Type

type MakeSlice

type MakeSlice struct {
	Len Value
	Cap Value
	// contains filtered or unexported fields
}

The MakeSlice instruction yields a slice of length Len backed by a newly allocated array of length Cap.

Both Len and Cap must be non-nil Values of integer type.

(Alloc(types.Array) followed by Slice will not suffice because Alloc can only create arrays of constant length.)

Type() returns a (possibly named) *types.Slice.

Pos() returns the syntax.CallExpr.Lparen for the make([]T) that created it.

Example printed form:

t1 = make []string 1:int t0
t1 = make StringSlice 1:int t0

func (*MakeSlice) Name

func (v *MakeSlice) Name() string

func (*MakeSlice) Operands

func (v *MakeSlice) Operands(rands []*Value) []*Value

func (*MakeSlice) Pos

func (v *MakeSlice) Pos() Pos

func (*MakeSlice) Referrers

func (v *MakeSlice) Referrers() *[]Instruction

func (*MakeSlice) String

func (v *MakeSlice) String() string

func (*MakeSlice) Type

func (v *MakeSlice) Type() Type

type Map

type Map struct {
	// contains filtered or unexported fields
}

A Map represents a map type.

Example
const source = `package P

var X []string
var Y []string

const p, q = 1.0, 2.0

func f(offset int32) (value byte, ok bool)
func g(rune) (uint8, bool)
`

// Parse and type-check the package.
f, err := ParseString("P.go", source)
if err != nil {
	panic(err)
}
pkg, err := new(Config).Check("P", []*File{f}, nil)
if err != nil {
	panic(err)
}

scope := pkg.Scope()

// Group names of package-level objects by their type.
var namesByType TypeMap // value is []string
for _, name := range scope.Names() {
	T := scope.Lookup(name).Type()

	names, _ := namesByType.At(T).([]string)
	names = append(names, name)
	namesByType.Set(T, names)
}

// Format, sort, and print the map entries.
var lines []string
namesByType.Iterate(func(T Type, names interface{}) {
	lines = append(lines, fmt.Sprintf("%s   %s", names, T))
})
sort.Strings(lines)
for _, line := range lines {
	fmt.Println(line)
}
Output:

[X Y]   []string
[f g]   func(offset int32) (value byte, ok bool)
[p q]   untyped float

func NewMap

func NewMap(key, elem Type) *Map

NewMap returns a new map for the given key and element types.

func (*Map) Elem

func (m *Map) Elem() Type

Elem returns the element type of map m.

func (*Map) Key

func (m *Map) Key() Type

Key returns the key type of map m.

func (*Map) String

func (t *Map) String() string

func (*Map) Underlying

func (t *Map) Underlying() Type

type MapUpdate

type MapUpdate struct {
	Map   Value
	Key   Value
	Value Value
	// contains filtered or unexported fields
}

The MapUpdate instruction updates the association of Map[Key] to Value.

Pos() returns the syntax.KeyValueExpr.Colon or syntax.IndexExpr.Lbrack, if explicit in the source.

Example printed form:

t0[t1] = t2

func (*MapUpdate) Block

func (v *MapUpdate) Block() *BasicBlock

func (*MapUpdate) Operands

func (v *MapUpdate) Operands(rands []*Value) []*Value

func (*MapUpdate) Parent

func (v *MapUpdate) Parent() *Function

func (*MapUpdate) Pos

func (s *MapUpdate) Pos() Pos

func (*MapUpdate) Referrers

func (v *MapUpdate) Referrers() *[]Instruction

func (*MapUpdate) String

func (s *MapUpdate) String() string

type Member

type Member interface {
	Name() string              // declared name of the package member
	String() string            // package-qualified name of the package member
	RelString(*Package) string // like String, but relative refs are unqualified
	Object() Object            // typechecker's object for this member, if any
	Pos() Pos                  // position of member's declaration, if known
	Type() Type                // type of the package member
}

A Member is a member of a Go package, implemented by *NamedConst, *Global, *Function, or *Type; they are created by package-level const, var, func and type declarations respectively.

type MethodSet

type MethodSet struct {
	// contains filtered or unexported fields
}

A MethodSet is an ordered set of concrete or abstract (interface) methods; a method is a MethodVal selection, and they are ordered by ascending m.Obj().Id(). The zero value for a MethodSet is a ready-to-use empty method set.

func NewMethodSet

func NewMethodSet(T Type) *MethodSet

NewMethodSet returns the method set for the given type T. It always returns a non-nil method set, even if it is empty.

func (*MethodSet) At

func (s *MethodSet) At(i int) *Selection

At returns the i'th method in s for 0 <= i < s.Len().

func (*MethodSet) Len

func (s *MethodSet) Len() int

Len returns the number of methods in s.

func (*MethodSet) Lookup

func (s *MethodSet) Lookup(pkg *Package, name string) *Selection

Lookup returns the method with matching package and name, or nil if not found.

func (*MethodSet) String

func (s *MethodSet) String() string

type MethodSetCache

type MethodSetCache struct {
	// contains filtered or unexported fields
}

A MethodSetCache records the method set of each type T for which MethodSet(T) is called so that repeat queries are fast. The zero value is a ready-to-use cache instance.

func (*MethodSetCache) MethodSet

func (cache *MethodSetCache) MethodSet(T Type) *MethodSet

MethodSet returns the method set of type T. It is thread-safe.

If cache is nil, this function is equivalent to types.NewMethodSet(T). Utility functions can thus expose an optional *MethodSetCache parameter to clients that care about performance.

type Named

type Named struct {
	// contains filtered or unexported fields
}

A Named represents a named (defined) type.

func NewNamed

func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named

NewNamed returns a new named type for the given type name, underlying type, and associated methods. If the given type name obj doesn't have a type yet, its type is set to the returned named type. The underlying type must not be a *Named.

func (*Named) AddMethod

func (t *Named) AddMethod(m *Func)

AddMethod adds method m unless it is already in the method list. t must not have type arguments.

func (*Named) Method

func (t *Named) Method(i int) *Func

Method returns the i'th method of named type t for 0 <= i < t.NumMethods().

func (*Named) NumMethods

func (t *Named) NumMethods() int

NumMethods returns the number of explicit methods defined for t.

For an ordinary or instantiated type t, the receiver base type of these methods will be the named type t. For an uninstantiated generic type t, each method receiver will be instantiated with its receiver type parameters.

func (*Named) Obj

func (t *Named) Obj() *TypeName

Obj returns the type name for the declaration defining the named type t. For instantiated types, this is same as the type name of the origin type.

func (*Named) Origin

func (t *Named) Origin() *Named

Origin returns the generic type from which the named type t is instantiated. If t is not an instantiated type, the result is t.

func (*Named) SetTypeParams

func (t *Named) SetTypeParams(tparams []*TypeParam)

SetTypeParams sets the type parameters of the named type t. t must not have type arguments.

func (*Named) SetUnderlying

func (t *Named) SetUnderlying(underlying Type)

SetUnderlying sets the underlying type and marks t as complete. t must not have type arguments.

func (*Named) String

func (t *Named) String() string

func (*Named) TypeArgs

func (t *Named) TypeArgs() *TypeList

TypeArgs returns the type arguments used to instantiate the named type t.

func (*Named) TypeParams

func (t *Named) TypeParams() *TypeParamList

TypeParams returns the type parameters of the named type t, or nil. The result is non-nil for an (originally) generic type even if it is instantiated.

func (*Named) Underlying

func (t *Named) Underlying() Type

type Next

type Next struct {
	Iter Value
	// contains filtered or unexported fields
}

The Next instruction reads and advances the (map or string) iterator Iter and returns a 3-tuple value (ok, k, v). If the iterator is not exhausted, ok is true and k and v are the next elements of the domain and range, respectively. Otherwise ok is false and k and v are undefined.

Components of the tuple are accessed using Extract.

The IsString field distinguishes iterators over strings from those over maps, as the Type() alone is insufficient: consider map[int]rune.

Type() returns a *types.Tuple for the triple (ok, k, v). The types of k and/or v may be types.Invalid.

Example printed form:

t1 = next t0

func (*Next) Name

func (v *Next) Name() string

func (*Next) Operands

func (v *Next) Operands(rands []*Value) []*Value

func (*Next) Pos

func (v *Next) Pos() Pos

func (*Next) Referrers

func (v *Next) Referrers() *[]Instruction

func (*Next) String

func (v *Next) String() string

func (*Next) Type

func (v *Next) Type() Type

type Nil

type Nil struct {
	// contains filtered or unexported fields
}

Nil represents the predeclared value nil.

func (*Nil) Exported

func (obj *Nil) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Nil) Id

func (obj *Nil) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Nil) Name

func (obj *Nil) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Nil) Parent

func (obj *Nil) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Nil) Pkg

func (obj *Nil) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Nil) Pos

func (obj *Nil) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Nil) String

func (obj *Nil) String() string

func (*Nil) Type

func (obj *Nil) Type() Type

Type returns the object's type.

type Object

type Object interface {
	Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
	Pos() Pos       // position of object identifier in declaration
	Pkg() *Package  // package to which this object belongs; nil for labels and objects in the Universe scope
	Name() string   // package local object name
	Type() Type     // object type
	Exported() bool // reports whether the name starts with a capital letter
	Id() string     // object name if exported, qualified name if not exported (see func Id)

	// String returns a human-readable string of the object.
	String() string
	// contains filtered or unexported methods
}

An Object describes a named language entity such as a package, constant, type, variable, function (incl. methods), or label. All objects implement the Object interface.

func Callee

func Callee(info *Info, call *CallExpr) Object

Callee returns the named target of a function call, if any: a function, method, builtin, or variable.

func LookupFieldOrMethod

func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool)

LookupFieldOrMethod looks up a field or method with given package and name in T and returns the corresponding *Var or *Func, an index sequence, and a bool indicating if there were any pointer indirections on the path to the field or method. If addressable is set, T is the type of an addressable variable (only matters for method lookups). T must not be nil.

The last index entry is the field or method index in the (possibly embedded) type where the entry was found, either:

  1. the list of declared methods of a named type; or
  2. the list of all methods (method set) of an interface type; or
  3. the list of fields of a struct type.

The earlier index entries are the indices of the embedded struct fields traversed to get to the found entry, starting at depth 0.

If no entry is found, a nil object is returned. In this case, the returned index and indirect values have the following meaning:

  • If index != nil, the index sequence points to an ambiguous entry (the same name appeared more than once at the same embedding level).

  • If indirect is set, a method with a pointer receiver type was found but there was no pointer on the path from the actual receiver type to the method's formal receiver base type, nor was the receiver addressable.

type Package

type Package struct {
	// contains filtered or unexported fields
}

A Package describes a Go package.

var Unsafe *Package

The Unsafe package is the package returned by an importer for the import path "unsafe".

func Dependencies

func Dependencies(pkgs ...*Package) []*Package

Dependencies returns all dependencies of the specified packages.

Dependent packages appear in topological order: if package P imports package Q, Q appears earlier than P in the result. The algorithm follows import statements in the order they appear in the source code, so the result is a total order.

func NewPackage

func NewPackage(path, name string) *Package

NewPackage returns a new Package for the given package path and name. The package is not complete and contains no explicit imports.

func (*Package) Complete

func (pkg *Package) Complete() bool

A package is complete if its scope contains (at least) all exported objects; otherwise it is incomplete.

func (*Package) Imports

func (pkg *Package) Imports() []*Package

Imports returns the list of packages directly imported by pkg; the list is in source order.

If pkg was loaded from export data, Imports includes packages that provide package-level objects referenced by pkg. This may be more or less than the set of packages directly imported by pkg's source code.

func (*Package) MarkComplete

func (pkg *Package) MarkComplete()

MarkComplete marks a package as complete.

func (*Package) Name

func (pkg *Package) Name() string

Name returns the package name.

func (*Package) Path

func (pkg *Package) Path() string

Path returns the package path.

func (*Package) Scope

func (pkg *Package) Scope() *Scope

Scope returns the (complete or incomplete) package scope holding the objects declared at package level (TypeNames, Consts, Vars, and Funcs). For a nil pkg receiver, Scope returns the Universe scope.

func (*Package) SetImports

func (pkg *Package) SetImports(list []*Package)

SetImports sets the list of explicitly imported packages to list. It is the caller's responsibility to make sure list elements are unique.

func (*Package) SetName

func (pkg *Package) SetName(name string)

SetName sets the package name.

func (*Package) String

func (pkg *Package) String() string

type Panic

type Panic struct {
	X Value // an interface{}
	// contains filtered or unexported fields
}

The Panic instruction initiates a panic with value X.

A Panic instruction must be the last instruction of its containing BasicBlock, which must have no successors.

NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; they are treated as calls to a built-in function.

Pos() returns the syntax.CallExpr.Lparen if this panic was explicit in the source.

Example printed form:

panic t0

func (*Panic) Block

func (v *Panic) Block() *BasicBlock

func (*Panic) Operands

func (s *Panic) Operands(rands []*Value) []*Value

func (*Panic) Parent

func (v *Panic) Parent() *Function

func (*Panic) Pos

func (s *Panic) Pos() Pos

func (*Panic) Referrers

func (v *Panic) Referrers() *[]Instruction

func (*Panic) String

func (s *Panic) String() string

type Parameter

type Parameter struct {
	// contains filtered or unexported fields
}

A Parameter represents an input parameter of a function.

func (*Parameter) Name

func (v *Parameter) Name() string

func (*Parameter) Object

func (v *Parameter) Object() Object

func (*Parameter) Operands

func (v *Parameter) Operands(rands []*Value) []*Value

func (*Parameter) Parent

func (v *Parameter) Parent() *Function

func (*Parameter) Pos

func (v *Parameter) Pos() Pos

func (*Parameter) Referrers

func (v *Parameter) Referrers() *[]Instruction

func (*Parameter) String

func (v *Parameter) String() string

func (*Parameter) Type

func (v *Parameter) Type() Type

type Phi

type Phi struct {
	Comment string  // a hint as to its purpose
	Edges   []Value // Edges[i] is value for Block().Preds[i]
	// contains filtered or unexported fields
}

The Phi instruction represents an SSA φ-node, which combines values that differ across incoming control-flow edges and yields a new value. Within a block, all φ-nodes must appear before all non-φ nodes.

Pos() returns the position of the && or || for short-circuit control-flow joins, or that of the *Alloc for φ-nodes inserted during SSA renaming.

Example printed form:

t2 = phi [0: t0, 1: t1]

func (*Phi) Name

func (v *Phi) Name() string

func (*Phi) Operands

func (v *Phi) Operands(rands []*Value) []*Value

func (*Phi) Pos

func (v *Phi) Pos() Pos

func (*Phi) Referrers

func (v *Phi) Referrers() *[]Instruction

func (*Phi) String

func (v *Phi) String() string

func (*Phi) Type

func (v *Phi) Type() Type

type PkgName

type PkgName struct {
	// contains filtered or unexported fields
}

A PkgName represents an imported Go package. PkgNames don't have a type.

func NewPkgName

func NewPkgName(pos Pos, pkg *Package, name string, imported *Package) *PkgName

NewPkgName returns a new PkgName object representing an imported package. The remaining arguments set the attributes found with all Objects.

func (*PkgName) Exported

func (obj *PkgName) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*PkgName) Id

func (obj *PkgName) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*PkgName) Imported

func (obj *PkgName) Imported() *Package

Imported returns the package that was imported. It is distinct from Pkg(), which is the package containing the import statement.

func (*PkgName) Name

func (obj *PkgName) Name() string

Name returns the object's (package-local, unqualified) name.

func (*PkgName) Parent

func (obj *PkgName) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*PkgName) Pkg

func (obj *PkgName) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*PkgName) Pos

func (obj *PkgName) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*PkgName) String

func (obj *PkgName) String() string

func (*PkgName) Type

func (obj *PkgName) Type() Type

Type returns the object's type.

type Pointer

type Pointer struct {
	// contains filtered or unexported fields
}

A Pointer represents a pointer type.

func AsPointer

func AsPointer(t Type) *Pointer

If t is a pointer, AsPointer returns that type, otherwise it returns nil.

func NewPointer

func NewPointer(elem Type) *Pointer

NewPointer returns a new pointer type for the given element (base) type.

func (*Pointer) Elem

func (p *Pointer) Elem() Type

Elem returns the element type for the given pointer p.

func (*Pointer) String

func (p *Pointer) String() string

func (*Pointer) Underlying

func (p *Pointer) Underlying() Type

type Program

type Program struct {
	MethodSets MethodSetCache // cache of type-checker's method-sets
	// contains filtered or unexported fields
}

A Program is a partial or complete Go program converted to SSA form.

func NewProgram

func NewProgram(mode BuilderMode) *Program

NewProgram returns a new SSA Program.

mode controls diagnostics and checking during SSA construction.

func (*Program) AllPackages

func (prog *Program) AllPackages() []*SSAPackage

AllPackages returns a new slice containing all packages in the program prog in unspecified order.

func (*Program) Build

func (prog *Program) Build()

Build calls Package.Build for each package in prog. Building occurs in parallel unless the BuildSerially mode flag was set.

Build is intended for whole-program analysis; a typical compiler need only build a single package.

Build is idempotent and thread-safe.

func (*Program) CreatePackage

func (prog *Program) CreatePackage(pkg *Package, files []*File, info *Info, importable bool) *SSAPackage

CreatePackage constructs and returns an SSA Package from the specified type-checked, error-free file ASTs, and populates its Members mapping.

importable determines whether this package should be returned by a subsequent call to ImportedPackage(pkg.Path()).

The real work of building SSA form for each function is not done until a subsequent call to Package.Build().

func (*Program) ImportedPackage

func (prog *Program) ImportedPackage(path string) *SSAPackage

ImportedPackage returns the importable Package whose PkgPath is path, or nil if no such Package has been created.

A parameter to CreatePackage determines whether a package should be considered importable. For example, no import declaration can resolve to the ad-hoc main package created by 'go build foo.go'.

TODO(adonovan): rethink this function and the "importable" concept; most packages are importable. This function assumes that all types.Package.Path values are unique within the ssa.Program, which is false---yet this function remains very convenient. Clients should use (*Program).Package instead where possible. SSA doesn't really need a string-keyed map of packages.

func (*Program) LookupMethod

func (prog *Program) LookupMethod(T Type, pkg *Package, name string) *Function

LookupMethod returns the implementation of the method of type T identified by (pkg, name). It returns nil if the method exists but is abstract, and panics if T has no such method.

func (*Program) MethodValue

func (prog *Program) MethodValue(sel *Selection) *Function

MethodValue returns the Function implementing method sel, building wrapper methods on demand. It returns nil if sel denotes an abstract (interface) method.

Precondition: sel.Kind() == MethodVal.

Thread-safe.

EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu)

func (*Program) NewFunction

func (prog *Program) NewFunction(name string, sig *Signature, provenance string) *Function

NewFunction returns a new synthetic Function instance belonging to prog, with its name and signature fields set as specified.

The caller is responsible for initializing the remaining fields of the function object, e.g. Pkg, Params, Blocks.

It is practically impossible for clients to construct well-formed SSA functions/packages/programs directly, so we assume this is the job of the Builder alone. NewFunction exists to provide clients a little flexibility. For example, analysis tools may wish to construct fake Functions for the root of the callgraph, a fake "reflect" package, etc.

TODO(adonovan): think harder about the API here.

func (*Program) Package

func (prog *Program) Package(obj *Package) *SSAPackage

Package returns the SSA Package corresponding to the specified type-checker package object. It returns nil if no such SSA package has been created.

func (*Program) RuntimeTypes

func (prog *Program) RuntimeTypes() []Type

RuntimeTypes returns a new unordered slice containing all concrete types in the program for which a complete (non-empty) method set is required at run-time.

Thread-safe.

EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu)

type Qualifier

type Qualifier func(*Package) string

A Qualifier controls how named package-level objects are printed in calls to TypeString, ObjectString, and SelectionString.

These three formatting routines call the Qualifier for each package-level object O, and if the Qualifier returns a non-empty string p, the object is printed in the form p.O. If it returns an empty string, only the object name O is printed.

Using a nil Qualifier is equivalent to using (*Package).Path: the object is qualified by the import path, e.g., "encoding/json.Marshal".

func RelativeTo

func RelativeTo(pkg *Package) Qualifier

RelativeTo returns a Qualifier that fully qualifies members of all packages other than pkg.

type Range

type Range struct {
	X Value // string or map
	// contains filtered or unexported fields
}

The Range instruction yields an iterator over the domain and range of X, which must be a string or map.

Elements are accessed via Next.

Type() returns an opaque and degenerate "rangeIter" type.

Pos() returns the syntax.RangeStmt.For.

Example printed form:

t0 = range "hello":string

func (*Range) Name

func (v *Range) Name() string

func (*Range) Operands

func (v *Range) Operands(rands []*Value) []*Value

func (*Range) Pos

func (v *Range) Pos() Pos

func (*Range) Referrers

func (v *Range) Referrers() *[]Instruction

func (*Range) String

func (v *Range) String() string

func (*Range) Type

func (v *Range) Type() Type

type Return

type Return struct {
	Results []Value
	// contains filtered or unexported fields
}

The Return instruction returns values and control back to the calling function.

len(Results) is always equal to the number of results in the function's signature.

If len(Results) > 1, Return returns a tuple value with the specified components which the caller must access using Extract instructions.

There is no instruction to return a ready-made tuple like those returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or a tail-call to a function with multiple result parameters.

Return must be the last instruction of its containing BasicBlock. Such a block has no successors.

Pos() returns the syntax.ReturnStmt.Return, if explicit in the source.

Example printed form:

return
return nil:I, 2:int

func (*Return) Block

func (v *Return) Block() *BasicBlock

func (*Return) Operands

func (s *Return) Operands(rands []*Value) []*Value

func (*Return) Parent

func (v *Return) Parent() *Function

func (*Return) Pos

func (s *Return) Pos() Pos

func (*Return) Referrers

func (v *Return) Referrers() *[]Instruction

func (*Return) String

func (s *Return) String() string

type RunDefers

type RunDefers struct {
	// contains filtered or unexported fields
}

The RunDefers instruction pops and invokes the entire stack of procedure calls pushed by Defer instructions in this function.

It is legal to encounter multiple 'rundefers' instructions in a single control-flow path through a function; this is useful in the combined init() function, for example.

Pos() returns NoPos.

Example printed form:

rundefers

func (*RunDefers) Block

func (v *RunDefers) Block() *BasicBlock

func (*RunDefers) Operands

func (*RunDefers) Operands(rands []*Value) []*Value

func (*RunDefers) Parent

func (v *RunDefers) Parent() *Function

func (*RunDefers) Pos

func (s *RunDefers) Pos() Pos

func (*RunDefers) Referrers

func (v *RunDefers) Referrers() *[]Instruction

func (*RunDefers) String

func (*RunDefers) String() string

type SSABuiltin

type SSABuiltin struct {
	// contains filtered or unexported fields
}

A SSABuiltin represents a specific use of a built-in function, e.g. len.

Builtins are immutable values. Builtins do not have addresses. Builtins can only appear in CallCommon.Value.

Name() indicates the function: one of the built-in functions from the Go spec (excluding "make" and "new") or one of these ssa-defined intrinsics:

// wrapnilchk returns ptr if non-nil, panics otherwise.
// (For use in indirection wrappers.)
func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T

Object() returns a *types.SSABuiltin for built-ins defined by the spec, nil for others.

Type() returns a *types.Signature representing the effective signature of the built-in for this call.

func (*SSABuiltin) Name

func (v *SSABuiltin) Name() string

func (*SSABuiltin) Object

func (v *SSABuiltin) Object() Object

func (*SSABuiltin) Operands

func (v *SSABuiltin) Operands(rands []*Value) []*Value

Non-Instruction Values:

func (*SSABuiltin) Parent

func (v *SSABuiltin) Parent() *Function

func (*SSABuiltin) Pos

func (v *SSABuiltin) Pos() Pos

func (*SSABuiltin) Referrers

func (v *SSABuiltin) Referrers() *[]Instruction

func (*SSABuiltin) String

func (v *SSABuiltin) String() string

func (*SSABuiltin) Type

func (v *SSABuiltin) Type() Type

type SSAConst

type SSAConst struct {
	Value constant.Value
	// contains filtered or unexported fields
}

A SSAConst represents the value of a constant expression.

The underlying type of a constant may be any boolean, numeric, or string type. In addition, a SSAConst may represent the nil value of any reference type---interface, map, channel, pointer, slice, or function---but not "untyped nil".

All source-level constant expressions are represented by a SSAConst of the same type and value.

Value holds the value of the constant, independent of its Type(), using go/constant representation, or nil for a typed nil value.

Pos() returns syntax.NoPos.

Example printed form:

42:int
"hello":untyped string
3+4i:MyComplex

func NewSSAConst

func NewSSAConst(val constant.Value, typ Type) *SSAConst

NewSSAConst returns a new constant of the specified value and type. val must be valid according to the specification of Const.Value.

func (*SSAConst) Complex128

func (c *SSAConst) Complex128() complex128

Complex128 returns the complex value of this constant truncated to fit a complex128.

func (*SSAConst) Float64

func (c *SSAConst) Float64() float64

Float64 returns the numeric value of this constant truncated to fit a float64.

func (*SSAConst) Int64

func (c *SSAConst) Int64() int64

Int64 returns the numeric value of this constant truncated to fit a signed 64-bit integer.

func (*SSAConst) IsNil

func (c *SSAConst) IsNil() bool

IsNil returns true if this constant represents a typed or untyped nil value.

func (*SSAConst) Name

func (c *SSAConst) Name() string

func (*SSAConst) Operands

func (v *SSAConst) Operands(rands []*Value) []*Value

func (*SSAConst) Parent

func (c *SSAConst) Parent() *Function

func (*SSAConst) Pos

func (c *SSAConst) Pos() Pos

func (*SSAConst) Referrers

func (c *SSAConst) Referrers() *[]Instruction

func (*SSAConst) RelString

func (c *SSAConst) RelString(from *Package) string

func (*SSAConst) String

func (c *SSAConst) String() string

func (*SSAConst) Type

func (c *SSAConst) Type() Type

func (*SSAConst) Uint64

func (c *SSAConst) Uint64() uint64

Uint64 returns the numeric value of this constant truncated to fit an unsigned 64-bit integer.

type SSADefer

type SSADefer struct {
	Call CallCommon
	// contains filtered or unexported fields
}

The SSADefer instruction pushes the specified call onto a stack of functions to be called by a RunDefers instruction or by a panic.

See CallCommon for generic function call documentation.

Pos() returns the syntax.DeferStmt.SSADefer.

Example printed form:

defer println(t0, t1)
defer t3()
defer invoke t5.Println(...t6)

func (*SSADefer) Block

func (v *SSADefer) Block() *BasicBlock

func (*SSADefer) Common

func (s *SSADefer) Common() *CallCommon

func (*SSADefer) Operands

func (s *SSADefer) Operands(rands []*Value) []*Value

func (*SSADefer) Parent

func (v *SSADefer) Parent() *Function

func (*SSADefer) Pos

func (s *SSADefer) Pos() Pos

func (*SSADefer) Referrers

func (v *SSADefer) Referrers() *[]Instruction

func (*SSADefer) String

func (s *SSADefer) String() string

func (*SSADefer) Value

func (s *SSADefer) Value() *Call

type SSAField

type SSAField struct {
	X     Value // struct
	Field int   // index into X.Type().(*types.Struct).Fields
	// contains filtered or unexported fields
}

The SSAField instruction yields the SSAField of struct X.

The field is identified by its index within the field list of the struct type of X; by using numeric indices we avoid ambiguity of package-local identifiers and permit compact representations.

Pos() returns the position of the syntax.SelectorExpr.Sel for the field, if explicit in the source.

Example printed form:

t1 = t0.name [#1]

func (*SSAField) Name

func (v *SSAField) Name() string

func (*SSAField) Operands

func (v *SSAField) Operands(rands []*Value) []*Value

func (*SSAField) Pos

func (v *SSAField) Pos() Pos

func (*SSAField) Referrers

func (v *SSAField) Referrers() *[]Instruction

func (*SSAField) String

func (v *SSAField) String() string

func (*SSAField) Type

func (v *SSAField) Type() Type

type SSAGo

type SSAGo struct {
	Call CallCommon
	// contains filtered or unexported fields
}

The SSAGo instruction creates a new goroutine and calls the specified function within it.

See CallCommon for generic function call documentation.

Pos() returns the syntax.GoStmt.SSAGo.

Example printed form:

go println(t0, t1)
go t3()
go invoke t5.Println(...t6)

func (*SSAGo) Block

func (v *SSAGo) Block() *BasicBlock

func (*SSAGo) Common

func (s *SSAGo) Common() *CallCommon

func (*SSAGo) Operands

func (s *SSAGo) Operands(rands []*Value) []*Value

func (*SSAGo) Parent

func (v *SSAGo) Parent() *Function

func (*SSAGo) Pos

func (s *SSAGo) Pos() Pos

func (*SSAGo) Referrers

func (v *SSAGo) Referrers() *[]Instruction

func (*SSAGo) String

func (s *SSAGo) String() string

func (*SSAGo) Value

func (s *SSAGo) Value() *Call

type SSANode

type SSANode interface {
	// Common methods:
	String() string
	Pos() Pos
	Parent() *Function

	// Partial methods:
	Operands(rands []*Value) []*Value // nil for non-Instructions
	Referrers() *[]Instruction        // nil for non-Values
}

A SSANode is a node in the SSA value graph. Every concrete type that implements SSANode is also either a Value, an Instruction, or both.

SSANode contains the methods common to Value and Instruction, plus the Operands and Referrers methods generalized to return nil for non-Instructions and non-Values, respectively.

SSANode is provided to simplify SSA graph algorithms. Clients should use the more specific and informative Value or Instruction interfaces where appropriate.

type SSAPackage

type SSAPackage struct {
	Pkg *Package // the corresponding github.com/despiteallobjections/amigo/types.Package

	// all package members keyed by name (incl. init and init#%d)
	Members map[string]Member

	InitFunc  *Function // the package's synthetic init function
	InitGuard *Global   // the package's synthetic initializer guard
	// contains filtered or unexported fields
}

A SSAPackage is a single analyzed Go package containing Members for all package-level functions, variables, constants and types it declares. These may be accessed directly via Members, or via the type-specific accessor methods Func, Type, Var and Const.

Members also contains entries for "init" (the synthetic package initializer) and "init#%d", the nth declared init function, and unspecified other things too.

func (*SSAPackage) Build

func (p *SSAPackage) Build(prog *Program)

Build builds SSA code for all functions and vars in package p.

Precondition: CreatePackage must have been called for all of p's direct imports (and hence its direct imports must have been error-free).

Build is idempotent and thread-safe.

func (*SSAPackage) Func

func (p *SSAPackage) Func(name string) (f *Function)

Func returns the package-level function of the specified name, or nil if not found.

func (*SSAPackage) String

func (p *SSAPackage) String() string

func (*SSAPackage) WriteTo

func (p *SSAPackage) WriteTo(w io.Writer) (int64, error)

type SSASlice

type SSASlice struct {
	X              Value // slice, string, or *array
	Low, High, Max Value // each may be nil
	// contains filtered or unexported fields
}

The SSASlice instruction yields a slice of an existing string, slice or *array X between optional integer bounds Low and High.

Dynamically, this instruction panics if X evaluates to a nil *array pointer.

Type() returns string if the type of X was string, otherwise a *types.SSASlice with the same element type as X.

Pos() returns the syntax.SliceExpr.Lbrack if created by a x[:] slice operation, the syntax.CompositeLit.Lbrace if created by a literal, or NoPos if not explicit in the source (e.g. a variadic argument slice).

Example printed form:

t1 = slice t0[1:]

func (*SSASlice) Name

func (v *SSASlice) Name() string

func (*SSASlice) Operands

func (v *SSASlice) Operands(rands []*Value) []*Value

func (*SSASlice) Pos

func (v *SSASlice) Pos() Pos

func (*SSASlice) Referrers

func (v *SSASlice) Referrers() *[]Instruction

func (*SSASlice) String

func (v *SSASlice) String() string

func (*SSASlice) Type

func (v *SSASlice) Type() Type

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

A Scope maintains a set of objects and links to its containing (parent) and contained (children) scopes. Objects may be inserted and looked up by name. The zero value for Scope is a ready-to-use empty scope.

Example

ExampleScope prints the tree of Scopes of a package created from a set of parsed files.

// Parse the source files for a package.
var files []*File
for _, file := range []struct{ name, input string }{
	{"main.go", `
package main
import "fmt"
func main() {
	freezing := FToC(-18)
	fmt.Println(freezing, Boiling) }
`},
	{"celsius.go", `
package main
import "fmt"
type Celsius float64
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
func FToC(f float64) Celsius { return Celsius(f - 32 / 9 * 5) }
const Boiling Celsius = 100
func Unused() { {}; {{ var x int; _ = x }} } // make sure empty block scopes get printed
`},
} {
	f, err := parseSrc(file.name, file.input)
	if err != nil {
		log.Fatal(err)
	}
	files = append(files, f)
}

// Type-check a package consisting of these files.
// Type information for the imported "fmt" package
// comes from $GOROOT/pkg/$GOOS_$GOOARCH/fmt.a.
conf := types.Config{Importer: defaultImporter()}
pkg, err := conf.Check("temperature", files, nil)
if err != nil {
	log.Fatal(err)
}

// Print the tree of scopes.
// For determinism, we redact addresses.
var buf bytes.Buffer
pkg.Scope().WriteTo(&buf, 0, true)
rx := regexp.MustCompile(` 0x[a-fA-F0-9]*`)
fmt.Println(rx.ReplaceAllString(buf.String(), ""))
Output:

package "temperature" scope {
.  const temperature.Boiling temperature.Celsius
.  type temperature.Celsius float64
.  func temperature.FToC(f float64) temperature.Celsius
.  func temperature.Unused()
.  func temperature.main()
.  main.go scope {
.  .  package fmt
.  .  function scope {
.  .  .  var freezing temperature.Celsius
.  .  }
.  }
.  celsius.go scope {
.  .  package fmt
.  .  function scope {
.  .  .  var c temperature.Celsius
.  .  }
.  .  function scope {
.  .  .  var f float64
.  .  }
.  .  function scope {
.  .  .  block scope {
.  .  .  }
.  .  .  block scope {
.  .  .  .  block scope {
.  .  .  .  .  var x int
.  .  .  .  }
.  .  .  }
.  .  }
.  }
}
var Universe *Scope

The Universe scope contains all predeclared objects of Go. It is the outermost scope of any chain of nested scopes.

func NewScope

func NewScope(parent *Scope, pos, end Pos, comment string) *Scope

NewScope returns a new, empty scope contained in the given parent scope, if any. The comment is for debugging only.

func (*Scope) Child

func (s *Scope) Child(i int) *Scope

Child returns the i'th child scope for 0 <= i < NumChildren().

func (*Scope) Contains

func (s *Scope) Contains(pos Pos) bool

Contains reports whether pos is within the scope's extent. The result is guaranteed to be valid only if the type-checked AST has complete position information.

func (*Scope) End

func (s *Scope) End() Pos

func (*Scope) Innermost

func (s *Scope) Innermost(pos Pos) *Scope

Innermost returns the innermost (child) scope containing pos. If pos is not within any scope, the result is nil. The result is also nil for the Universe scope. The result is guaranteed to be valid only if the type-checked AST has complete position information.

func (*Scope) Insert

func (s *Scope) Insert(obj Object) Object

Insert attempts to insert an object obj into scope s. If s already contains an alternative object alt with the same name, Insert leaves s unchanged and returns alt. Otherwise it inserts obj, sets the object's parent scope if not already set, and returns nil.

func (*Scope) InsertLazy

func (s *Scope) InsertLazy(name string, resolve func() Object) bool

InsertLazy is like Insert, but allows deferring construction of the inserted object until it's accessed with Lookup. The Object returned by resolve must have the same name as given to InsertLazy. If s already contains an alternative object with the same name, InsertLazy leaves s unchanged and returns false. Otherwise it records the binding and returns true. The object's parent scope will be set to s after resolve is called.

func (*Scope) Len

func (s *Scope) Len() int

Len returns the number of scope elements.

func (*Scope) Lookup

func (s *Scope) Lookup(name string) Object

Lookup returns the object in scope s with the given name if such an object exists; otherwise the result is nil.

func (*Scope) LookupParent

func (s *Scope) LookupParent(name string, pos Pos) (*Scope, Object)

LookupParent follows the parent chain of scopes starting with s until it finds a scope where Lookup(name) returns a non-nil object, and then returns that scope and object. If a valid position pos is provided, only objects that were declared at or before pos are considered. If no such scope and object exists, the result is (nil, nil).

Note that obj.Parent() may be different from the returned scope if the object was inserted into the scope and already had a parent at that time (see Insert). This can only happen for dot-imported objects whose scope is the scope of the package that exported them.

func (*Scope) Names

func (s *Scope) Names() []string

Names returns the scope's element names in sorted order.

func (*Scope) NumChildren

func (s *Scope) NumChildren() int

NumChildren returns the number of scopes nested in s.

func (*Scope) Parent

func (s *Scope) Parent() *Scope

Parent returns the scope's containing (parent) scope.

func (*Scope) Pos

func (s *Scope) Pos() Pos

Pos and End describe the scope's source code extent [pos, end). The results are guaranteed to be valid only if the type-checked AST has complete position information. The extent is undefined for Universe and package scopes.

func (*Scope) Squash

func (s *Scope) Squash(err func(obj, alt Object))

Squash merges s with its parent scope p by adding all objects of s to p, adding all children of s to the children of p, and removing s from p's children. The function f is called for each object obj in s which has an object alt in p. s should be discarded after having been squashed.

func (*Scope) String

func (s *Scope) String() string

String returns a string representation of the scope, for debugging.

func (*Scope) WriteTo

func (s *Scope) WriteTo(w io.Writer, n int, recurse bool)

WriteTo writes a string representation of the scope to w, with the scope elements sorted by name. The level of indentation is controlled by n >= 0, with n == 0 for no indentation. If recurse is set, it also writes nested (children) scopes.

type Select

type Select struct {
	States   []*SelectState
	Blocking bool
	// contains filtered or unexported fields
}

The Select instruction tests whether (or blocks until) one of the specified sent or received states is entered.

Let n be the number of States for which Dir==RECV and T_i (0<=i<n) be the element type of each such state's Chan. Select returns an n+2-tuple

(index int, recvOk bool, r_0 T_0, ... r_n-1 T_n-1)

The tuple's components, described below, must be accessed via the Extract instruction.

If Blocking, select waits until exactly one state holds, i.e. a channel becomes ready for the designated operation of sending or receiving; select chooses one among the ready states pseudorandomly, performs the send or receive operation, and sets 'index' to the index of the chosen channel.

If !Blocking, select doesn't block if no states hold; instead it returns immediately with index equal to -1.

If the chosen channel was used for a receive, the r_i component is set to the received value, where i is the index of that state among all n receive states; otherwise r_i has the zero value of type T_i. Note that the receive index i is not the same as the state index index.

The second component of the triple, recvOk, is a boolean whose value is true iff the selected operation was a receive and the receive successfully yielded a value.

Pos() returns the syntax.SelectStmt.Select.

Example printed form:

t3 = select nonblocking [<-t0, t1<-t2]
t4 = select blocking []

func (*Select) Name

func (v *Select) Name() string

func (*Select) Operands

func (v *Select) Operands(rands []*Value) []*Value

func (*Select) Pos

func (v *Select) Pos() Pos

func (*Select) Referrers

func (v *Select) Referrers() *[]Instruction

func (*Select) String

func (s *Select) String() string

func (*Select) Type

func (v *Select) Type() Type

type SelectState

type SelectState struct {
	Dir       ChanDir // direction of case (SendOnly or RecvOnly)
	Chan      Value   // channel to use (for send or receive)
	Send      Value   // value to send (for send)
	Pos       Pos     // position of token.ARROW
	DebugNode Node    // syntax.SendStmt or syntax.UnaryExpr(<-) [debug mode]
}

SelectState is a helper for Select. It represents one goal state and its corresponding communication.

type Selection

type Selection struct {
	// contains filtered or unexported fields
}

A Selection describes a selector expression x.f. For the declarations:

type T struct{ x int; E }
type E struct{}
func (e E) m() {}
var p *T

the following relations exist:

Selector    Kind          Recv    Obj    Type       Index     Indirect

p.x         FieldVal      T       x      int        {0}       true
p.m         MethodVal     *T      m      func()     {1, 0}    true
T.m         MethodExpr    T       m      func(T)    {1, 0}    false

func IntuitiveMethodSet

func IntuitiveMethodSet(T Type, msets *MethodSetCache) []*Selection

IntuitiveMethodSet returns the intuitive method set of a type T, which is the set of methods you can call on an addressable value of that type.

The result always contains MethodSet(T), and is exactly MethodSet(T) for interface types and for pointer-to-concrete types. For all other concrete types T, the result additionally contains each method belonging to *T if there is no identically named method on T itself.

This corresponds to user intuition about method sets; this function is intended only for user interfaces.

The order of the result is as for types.MethodSet(T).

func (*Selection) Explicit

func (s *Selection) Explicit() int

func (*Selection) Implicits

func (s *Selection) Implicits() []int

func (*Selection) Index

func (s *Selection) Index() []int

Index describes the path from x to f in x.f. The last index entry is the field or method index of the type declaring f; either:

  1. the list of declared methods of a named type; or
  2. the list of methods of an interface type; or
  3. the list of fields of a struct type.

The earlier index entries are the indices of the embedded fields implicitly traversed to get from (the type of) x to f, starting at embedding depth 0.

func (*Selection) Indirect

func (s *Selection) Indirect() bool

Indirect reports whether any pointer indirection was required to get from x to f in x.f.

func (*Selection) Kind

func (s *Selection) Kind() SelectionKind

Kind returns the selection kind.

func (*Selection) Obj

func (s *Selection) Obj() Object

Obj returns the object denoted by x.f; a *Var for a field selection, and a *Func in all other cases.

func (*Selection) Recv

func (s *Selection) Recv() Type

Recv returns the type of x in x.f.

func (*Selection) String

func (s *Selection) String() string

func (*Selection) Type

func (s *Selection) Type() Type

Type returns the type of x.f, which may be different from the type of f. See Selection for more information.

type SelectionKind

type SelectionKind int

SelectionKind describes the kind of a selector expression x.f (excluding qualified identifiers).

const (
	FieldVal   SelectionKind = iota // x.f is a struct field selector
	MethodVal                       // x.f is a method selector
	MethodExpr                      // x.f is a method expression
)

type Send

type Send struct {
	Chan, X Value
	// contains filtered or unexported fields
}

The Send instruction sends X on channel Chan.

Pos() returns the syntax.SendStmt.Arrow, if explicit in the source.

Example printed form:

send t0 <- t1

func (*Send) Block

func (v *Send) Block() *BasicBlock

func (*Send) Operands

func (s *Send) Operands(rands []*Value) []*Value

func (*Send) Parent

func (v *Send) Parent() *Function

func (*Send) Pos

func (s *Send) Pos() Pos

func (*Send) Referrers

func (v *Send) Referrers() *[]Instruction

func (*Send) String

func (s *Send) String() string

type Signature

type Signature struct {
	// contains filtered or unexported fields
}

A Signature represents a (non-builtin) function or method type. The receiver is ignored when comparing signatures for identity.

func AsSignature

func AsSignature(t Type) *Signature

If t is a signature, AsSignature returns that type, otherwise it returns nil.

func NewSignatureType

func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params, results *Tuple, variadic bool) *Signature

NewSignatureType creates a new function type for the given receiver, receiver type parameters, type parameters, parameters, and results. If variadic is set, params must hold at least one parameter and the last parameter must be of unnamed slice type. If recv is non-nil, typeParams must be empty. If recvTypeParams is non-empty, recv must be non-nil.

func (*Signature) Params

func (s *Signature) Params() *Tuple

Params returns the parameters of signature s, or nil.

func (*Signature) Recv

func (s *Signature) Recv() *Var

Recv returns the receiver of signature s (if a method), or nil if a function. It is ignored when comparing signatures for identity.

For an abstract method, Recv returns the enclosing interface either as a *Named or an *Interface. Due to embedding, an interface may contain methods whose receiver type is a different interface.

func (*Signature) RecvTypeParams

func (s *Signature) RecvTypeParams() *TypeParamList

RecvTypeParams returns the receiver type parameters of signature s, or nil.

func (*Signature) Results

func (s *Signature) Results() *Tuple

Results returns the results of signature s, or nil.

func (*Signature) SetTypeParams

func (s *Signature) SetTypeParams(tparams []*TypeParam)

SetTypeParams sets the type parameters of signature s.

func (*Signature) String

func (s *Signature) String() string

func (*Signature) TypeParams

func (s *Signature) TypeParams() *TypeParamList

TypeParams returns the type parameters of signature s, or nil.

func (*Signature) Underlying

func (s *Signature) Underlying() Type

func (*Signature) Variadic

func (s *Signature) Variadic() bool

Variadic reports whether the signature s is variadic.

type Sizes

type Sizes interface {
	// Alignof returns the alignment of a variable of type T.
	// Alignof must implement the alignment guarantees required by the spec.
	Alignof(T Type) int64

	// Offsetsof returns the offsets of the given struct fields, in bytes.
	// Offsetsof must implement the offset guarantees required by the spec.
	Offsetsof(fields []*Var) []int64

	// Sizeof returns the size of a variable of type T.
	// Sizeof must implement the size guarantees required by the spec.
	Sizeof(T Type) int64
}

Sizes defines the sizing functions for package unsafe.

func SizesFor

func SizesFor(compiler, arch string) Sizes

SizesFor returns the Sizes used by a compiler for an architecture. The result is nil if a compiler/architecture pair is not known.

Supported architectures for compiler "gc": "386", "arm", "arm64", "amd64", "amd64p32", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "riscv64", "s390x", "sparc64", "wasm".

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

A Slice represents a slice type.

func NewSlice

func NewSlice(elem Type) *Slice

NewSlice returns a new slice type for the given element type.

func (*Slice) Elem

func (s *Slice) Elem() Type

Elem returns the element type of slice s.

func (*Slice) String

func (s *Slice) String() string

func (*Slice) Underlying

func (s *Slice) Underlying() Type

type SliceToArrayPointer

type SliceToArrayPointer struct {
	X Value
	// contains filtered or unexported fields
}

The SliceToArrayPointer instruction yields the conversion of slice X to array pointer.

Pos() returns the syntax.CallExpr.Lparen, if the instruction arose from an explicit conversion in the source.

Example printed form:

t1 = slice to array pointer *[4]byte <- []byte (t0)

func (*SliceToArrayPointer) Name

func (v *SliceToArrayPointer) Name() string

func (*SliceToArrayPointer) Operands

func (v *SliceToArrayPointer) Operands(rands []*Value) []*Value

func (*SliceToArrayPointer) Pos

func (v *SliceToArrayPointer) Pos() Pos

func (*SliceToArrayPointer) Referrers

func (v *SliceToArrayPointer) Referrers() *[]Instruction

func (*SliceToArrayPointer) String

func (v *SliceToArrayPointer) String() string

func (*SliceToArrayPointer) Type

func (v *SliceToArrayPointer) Type() Type

type StdSizes

type StdSizes struct {
	WordSize int64 // word size in bytes - must be >= 4 (32bits)
	MaxAlign int64 // maximum alignment in bytes - must be >= 1
}

StdSizes is a convenience type for creating commonly used Sizes. It makes the following simplifying assumptions:

  • The size of explicitly sized basic types (int16, etc.) is the specified size.
  • The size of strings and interfaces is 2*WordSize.
  • The size of slices is 3*WordSize.
  • The size of an array of n elements corresponds to the size of a struct of n consecutive fields of the array's element type.
  • The size of a struct is the offset of the last field plus that field's size. As with all element types, if the struct is used in an array its size must first be aligned to a multiple of the struct's alignment.
  • All other types have size WordSize.
  • Arrays and structs are aligned per spec definition; all other types are naturally aligned with a maximum alignment MaxAlign.

*StdSizes implements Sizes.

func (*StdSizes) Alignof

func (s *StdSizes) Alignof(T Type) int64

func (*StdSizes) Offsetsof

func (s *StdSizes) Offsetsof(fields []*Var) []int64

func (*StdSizes) Sizeof

func (s *StdSizes) Sizeof(T Type) int64

type Store

type Store struct {
	Addr Value
	Val  Value
	// contains filtered or unexported fields
}

The Store instruction stores Val at address Addr. Stores can be of arbitrary types.

Pos() returns the position of the source-level construct most closely associated with the memory store operation. Since implicit memory stores are numerous and varied and depend upon implementation choices, the details are not specified.

Example printed form:

*x = y

func (*Store) Block

func (v *Store) Block() *BasicBlock

func (*Store) Operands

func (s *Store) Operands(rands []*Value) []*Value

func (*Store) Parent

func (v *Store) Parent() *Function

func (*Store) Pos

func (s *Store) Pos() Pos

func (*Store) Referrers

func (v *Store) Referrers() *[]Instruction

func (*Store) String

func (s *Store) String() string

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

A Struct represents a struct type.

func NewStruct

func NewStruct(fields []*Var, tags []string) *Struct

NewStruct returns a new struct with the given fields and corresponding field tags. If a field with index i has a tag, tags[i] must be that tag, but len(tags) may be only as long as required to hold the tag with the largest index i. Consequently, if no field has a tag, tags may be nil.

func (*Struct) Field

func (s *Struct) Field(i int) *Var

Field returns the i'th field for 0 <= i < NumFields().

func (*Struct) NumFields

func (s *Struct) NumFields() int

NumFields returns the number of fields in the struct (including blank and embedded fields).

func (*Struct) String

func (s *Struct) String() string

func (*Struct) Tag

func (s *Struct) Tag(i int) string

Tag returns the i'th field tag for 0 <= i < NumFields().

func (*Struct) Underlying

func (s *Struct) Underlying() Type

type Term

type Term term

A Term represents a term in a Union.

func NewTerm

func NewTerm(tilde bool, typ Type) *Term

NewTerm returns a new union term.

func (*Term) String

func (t *Term) String() string

func (*Term) Tilde

func (t *Term) Tilde() bool

func (*Term) Type

func (t *Term) Type() Type

type Tuple

type Tuple struct {
	// contains filtered or unexported fields
}

A Tuple represents an ordered list of variables; a nil *Tuple is a valid (empty) tuple. Tuples are used as components of signatures and to represent the type of multiple assignments; they are not first class types of Go.

func NewTuple

func NewTuple(x ...*Var) *Tuple

NewTuple returns a new tuple for the given variables.

func (*Tuple) At

func (t *Tuple) At(i int) *Var

At returns the i'th variable of tuple t.

func (*Tuple) Len

func (t *Tuple) Len() int

Len returns the number variables of tuple t.

func (*Tuple) String

func (t *Tuple) String() string

func (*Tuple) Underlying

func (t *Tuple) Underlying() Type

type Type

type Type interface {
	// Underlying returns the underlying type of a type.
	Underlying() Type

	// String returns a string representation of a type.
	String() string
}

A Type represents a type of Go. All types implement the Type interface.

func CoreType

func CoreType(t Type) Type

If typ is a type parameter, CoreType returns the single underlying type of all types in the corresponding type constraint if it exists, or nil otherwise. If the type set contains only unrestricted and restricted channel types (with identical element types), the single underlying type is the restricted channel type if the restrictions are always the same. If typ is not a type parameter, CoreType returns the underlying type.

func Default

func Default(t Type) Type

Default returns the default "typed" type for an "untyped" type; it returns the incoming type for all other types. The default type for untyped nil is untyped nil.

func Instantiate

func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, error)

Instantiate instantiates the type orig with the given type arguments targs. orig must be a *Named or a *Signature type. If there is no error, the resulting Type is an instantiated type of the same kind (either a *Named or a *Signature). Methods attached to a *Named type are also instantiated, and associated with a new *Func that has the same position as the original method, but nil function scope.

If ctxt is non-nil, it may be used to de-duplicate the instance against previous instances with the same identity. As a special case, generic *Signature origin types are only considered identical if they are pointer equivalent, so that instantiating distinct (but possibly identical) signatures will yield different instances.

If validate is set, Instantiate verifies that the number of type arguments and parameters match, and that the type arguments satisfy their corresponding type constraints. If verification fails, the resulting error may wrap an *ArgumentError indicating which type argument did not satisfy its corresponding type parameter constraint, and why.

If validate is not set, Instantiate does not verify the type argument count or whether the type arguments satisfy their constraints. Instantiate is guaranteed to not return an error, but may panic. Specifically, for *Signature types, Instantiate will panic immediately if the type argument count is incorrect; for *Named types, a panic may occur later inside the *Named API.

type TypeAndValue

type TypeAndValue struct {
	Type  Type
	Value constant.Value
	// contains filtered or unexported fields
}

TypeAndValue reports the type and value (for constants) of the corresponding expression.

func Eval

func Eval(pkg *Package, pos Pos, expr string) (_ TypeAndValue, err error)

Eval returns the type and, if constant, the value for the expression expr, evaluated at position pos of package pkg, which must have been derived from type-checking an AST with complete position information relative to the provided file set.

The meaning of the parameters fset, pkg, and pos is the same as in CheckExpr. An error is returned if expr cannot be parsed successfully, or the resulting expr AST cannot be type-checked.

func (TypeAndValue) Addressable

func (tv TypeAndValue) Addressable() bool

Addressable reports whether the corresponding expression is addressable (https://golang.org/ref/spec#Address_operators).

func (TypeAndValue) Assignable

func (tv TypeAndValue) Assignable() bool

Assignable reports whether the corresponding expression is assignable to (provided a value of the right type).

func (TypeAndValue) HasOk

func (tv TypeAndValue) HasOk() bool

HasOk reports whether the corresponding expression may be used on the rhs of a comma-ok assignment.

func (TypeAndValue) IsBuiltin

func (tv TypeAndValue) IsBuiltin() bool

IsBuiltin reports whether the corresponding expression denotes a (possibly parenthesized) built-in function.

func (TypeAndValue) IsNil

func (tv TypeAndValue) IsNil() bool

IsNil reports whether the corresponding expression denotes the predeclared value nil. Depending on context, it may have been given a type different from UntypedNil.

func (TypeAndValue) IsType

func (tv TypeAndValue) IsType() bool

IsType reports whether the corresponding expression specifies a type.

func (TypeAndValue) IsValue

func (tv TypeAndValue) IsValue() bool

IsValue reports whether the corresponding expression is a value. Builtins are not considered values. Constant values have a non- nil Value.

func (TypeAndValue) IsVoid

func (tv TypeAndValue) IsVoid() bool

IsVoid reports whether the corresponding expression is a function call without results.

type TypeAssert

type TypeAssert struct {
	X            Value
	AssertedType Type
	CommaOk      bool
	// contains filtered or unexported fields
}

The TypeAssert instruction tests whether interface value X has type AssertedType.

If !CommaOk, on success it returns v, the result of the conversion (defined below); on failure it panics.

If CommaOk: on success it returns a pair (v, true) where v is the result of the conversion; on failure it returns (z, false) where z is AssertedType's zero value. The components of the pair must be accessed using the Extract instruction.

If AssertedType is a concrete type, TypeAssert checks whether the dynamic type in interface X is equal to it, and if so, the result of the conversion is a copy of the value in the interface.

If AssertedType is an interface, TypeAssert checks whether the dynamic type of the interface is assignable to it, and if so, the result of the conversion is a copy of the interface value X. If AssertedType is a superinterface of X.Type(), the operation will fail iff the operand is nil. (Contrast with ChangeInterface, which performs no nil-check.)

Type() reflects the actual type of the result, possibly a 2-types.Tuple; AssertedType is the asserted type.

Pos() returns the syntax.CallExpr.Lparen if the instruction arose from an explicit T(e) conversion; the syntax.AssertExpr.Lparen if the instruction arose from an explicit e.(T) operation; or the syntax.CaseClause.Case if the instruction arose from a case of a type-switch statement.

Example printed form:

t1 = typeassert t0.(int)
t3 = typeassert,ok t2.(T)

func (*TypeAssert) Name

func (v *TypeAssert) Name() string

func (*TypeAssert) Operands

func (v *TypeAssert) Operands(rands []*Value) []*Value

func (*TypeAssert) Pos

func (v *TypeAssert) Pos() Pos

func (*TypeAssert) Referrers

func (v *TypeAssert) Referrers() *[]Instruction

func (*TypeAssert) String

func (v *TypeAssert) String() string

func (*TypeAssert) Type

func (v *TypeAssert) Type() Type

type TypeError

type TypeError struct {
	Pos  Pos    // error position
	Msg  string // default error message, user-friendly
	Full string // full error message, for debugging (may contain internal details)
	Soft bool   // if set, error is "soft"
}

An TypeError describes a type-checking error; it implements the error interface. A "soft" error is an error that still permits a valid interpretation of a package (such as "unused variable"); "hard" errors may lead to unpredictable behavior if ignored.

func (TypeError) Error

func (err TypeError) Error() string

Error returns an error string formatted as follows: filename:line:column: message

func (TypeError) FullError

func (err TypeError) FullError() string

FullError returns an error string like Error, buy it may contain type-checker internal details such as subscript indices for type parameters and more. Useful for debugging.

type TypeList

type TypeList struct {
	// contains filtered or unexported fields
}

TypeList holds a list of types.

func (*TypeList) At

func (l *TypeList) At(i int) Type

At returns the i'th type in the list.

func (*TypeList) Len

func (l *TypeList) Len() int

Len returns the number of types in the list. It is safe to call on a nil receiver.

type TypeMap

type TypeMap struct {
	// contains filtered or unexported fields
}

TypeMap is a hash-table-based mapping from types (types.Type) to arbitrary interface{} values. The concrete types that implement the Type interface are pointers. Since they are not canonicalized, == cannot be used to check for equivalence, and thus we cannot simply use a Go map.

Just as with map[K]V, a nil *TypeMap is a valid empty map.

Not thread-safe.

func (*TypeMap) At

func (m *TypeMap) At(key Type) interface{}

At returns the map entry for the given key. The result is nil if the entry is not present.

func (*TypeMap) Delete

func (m *TypeMap) Delete(key Type) bool

Delete removes the entry with the given key, if any. It returns true if the entry was found.

func (*TypeMap) Iterate

func (m *TypeMap) Iterate(f func(key Type, value interface{}))

Iterate calls function f on each entry in the map in unspecified order.

If f should mutate the map, Iterate provides the same guarantees as Go maps: if f deletes a map entry that Iterate has not yet reached, f will not be invoked for it, but if f inserts a map entry that Iterate has not yet reached, whether or not f will be invoked for it is unspecified.

func (*TypeMap) Keys

func (m *TypeMap) Keys() []Type

Keys returns a new slice containing the set of map keys. The order is unspecified.

func (*TypeMap) KeysString

func (m *TypeMap) KeysString() string

KeysString returns a string representation of the map's key set. Order is unspecified.

func (*TypeMap) Len

func (m *TypeMap) Len() int

Len returns the number of map entries.

func (*TypeMap) Set

func (m *TypeMap) Set(key Type, value interface{}) (prev interface{})

Set sets the map entry for key to val, and returns the previous entry, if any.

func (*TypeMap) SetHasher

func (m *TypeMap) SetHasher(hasher Hasher)

SetHasher sets the hasher used by Map.

All Hashers are functionally equivalent but contain internal state used to cache the results of hashing previously seen types.

A single Hasher created by MakeHasher() may be shared among many Maps. This is recommended if the instances have many keys in common, as it will amortize the cost of hash computation.

A Hasher may grow without bound as new types are seen. Even when a type is deleted from the map, the Hasher never shrinks, since other types in the map may reference the deleted type indirectly.

Hashers are not thread-safe, and read-only operations such as Map.Lookup require updates to the hasher, so a full Mutex lock (not a read-lock) is require around all Map operations if a shared hasher is accessed from multiple threads.

If SetHasher is not called, the Map will create a private hasher at the first call to Insert.

func (*TypeMap) String

func (m *TypeMap) String() string

String returns a string representation of the map's entries. Values are printed using fmt.Sprintf("%v", v). Order is unspecified.

type TypeName

type TypeName struct {
	// contains filtered or unexported fields
}

A TypeName represents a name for a (defined or alias) type.

func NewTypeName

func NewTypeName(pos Pos, pkg *Package, name string, typ Type) *TypeName

NewTypeName returns a new type name denoting the given typ. The remaining arguments set the attributes found with all Objects.

The typ argument may be a defined (Named) type or an alias type. It may also be nil such that the returned TypeName can be used as argument for NewNamed, which will set the TypeName's type as a side- effect.

func NewTypeNameLazy

func NewTypeNameLazy(pos Pos, pkg *Package, name string, load func(named *Named) (tparams []*TypeParam, underlying Type, methods []*Func)) *TypeName

NewTypeNameLazy returns a new defined type like NewTypeName, but it lazily calls resolve to finish constructing the Named object.

func (*TypeName) Exported

func (obj *TypeName) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*TypeName) Id

func (obj *TypeName) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*TypeName) IsAlias

func (obj *TypeName) IsAlias() bool

IsAlias reports whether obj is an alias name for a type.

func (*TypeName) Name

func (obj *TypeName) Name() string

Name returns the object's (package-local, unqualified) name.

func (*TypeName) Parent

func (obj *TypeName) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*TypeName) Pkg

func (obj *TypeName) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*TypeName) Pos

func (obj *TypeName) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*TypeName) String

func (obj *TypeName) String() string

func (*TypeName) Type

func (obj *TypeName) Type() Type

Type returns the object's type.

type TypeParam

type TypeParam struct {
	// contains filtered or unexported fields
}

A TypeParam represents a type parameter type.

func NewTypeParam

func NewTypeParam(obj *TypeName, constraint Type) *TypeParam

NewTypeParam returns a new TypeParam. Type parameters may be set on a Named or Signature type by calling SetTypeParams. Setting a type parameter on more than one type will result in a panic.

The constraint argument can be nil, and set later via SetConstraint. If the constraint is non-nil, it must be fully defined.

func (*TypeParam) Constraint

func (t *TypeParam) Constraint() Type

Constraint returns the type constraint specified for t.

func (*TypeParam) Index

func (t *TypeParam) Index() int

Index returns the index of the type param within its param list, or -1 if the type parameter has not yet been bound to a type.

func (*TypeParam) Obj

func (t *TypeParam) Obj() *TypeName

Obj returns the type name for the type parameter t.

func (*TypeParam) SetConstraint

func (t *TypeParam) SetConstraint(bound Type)

SetConstraint sets the type constraint for t.

It must be called by users of NewTypeParam after the bound's underlying is fully defined, and before using the type parameter in any way other than to form other types. Once SetConstraint returns the receiver, t is safe for concurrent use.

func (*TypeParam) String

func (t *TypeParam) String() string

func (*TypeParam) Underlying

func (t *TypeParam) Underlying() Type

type TypeParamList

type TypeParamList struct {
	// contains filtered or unexported fields
}

TypeParamList holds a list of type parameters.

func (*TypeParamList) At

func (l *TypeParamList) At(i int) *TypeParam

At returns the i'th type parameter in the list.

func (*TypeParamList) Len

func (l *TypeParamList) Len() int

Len returns the number of type parameters in the list. It is safe to call on a nil receiver.

type UnOp

type UnOp struct {
	Op      Operator // One of: NOT SUB ARROW MUL XOR ! - <- * ^
	X       Value
	CommaOk bool
	// contains filtered or unexported fields
}

The UnOp instruction yields the result of Op X. ARROW is channel receive. MUL is pointer indirection (load). XOR is bitwise complement. SUB is negation. NOT is logical negation.

If CommaOk and Op=ARROW, the result is a 2-tuple of the value above and a boolean indicating the success of the receive. The components of the tuple are accessed using Extract.

Pos() returns the syntax.UnaryExpr.OpPos, if explicit in the source. For receive operations (ARROW) implicit in ranging over a channel, Pos() returns the syntax.RangeStmt.For. For implicit memory loads (STAR), Pos() returns the position of the most closely associated source-level construct; the details are not specified.

Example printed form:

t0 = *x
t2 = <-t1,ok

func (*UnOp) Name

func (v *UnOp) Name() string

func (*UnOp) Operands

func (v *UnOp) Operands(rands []*Value) []*Value

func (*UnOp) Pos

func (v *UnOp) Pos() Pos

func (*UnOp) Referrers

func (v *UnOp) Referrers() *[]Instruction

func (*UnOp) String

func (v *UnOp) String() string

func (*UnOp) Type

func (v *UnOp) Type() Type

type Union

type Union struct {
	// contains filtered or unexported fields
}

A Union represents a union of terms embedded in an interface.

func NewUnion

func NewUnion(terms []*Term) *Union

NewUnion returns a new Union type with the given terms. It is an error to create an empty union; they are syntactically not possible.

func (*Union) Len

func (u *Union) Len() int

func (*Union) String

func (u *Union) String() string

func (*Union) Term

func (u *Union) Term(i int) *Term

func (*Union) Underlying

func (u *Union) Underlying() Type

type Value

type Value interface {
	// Name returns the name of this value, and determines how
	// this Value appears when used as an operand of an
	// Instruction.
	//
	// This is the same as the source name for Parameters,
	// Builtins, Functions, FreeVars, Globals.
	// For constants, it is a representation of the constant's value
	// and type.  For all other Values this is the name of the
	// virtual register defined by the instruction.
	//
	// The name of an SSA Value is not semantically significant,
	// and may not even be unique within a function.
	Name() string

	// If this value is an Instruction, String returns its
	// disassembled form; otherwise it returns unspecified
	// human-readable information about the Value, such as its
	// kind, name and type.
	String() string

	// Type returns the type of this value.  Many instructions
	// (e.g. IndexAddr) change their behaviour depending on the
	// types of their operands.
	Type() Type

	// Parent returns the function to which this Value belongs.
	// It returns nil for named Functions, Builtin, Const and Global.
	Parent() *Function

	// Referrers returns the list of instructions that have this
	// value as one of their operands; it may contain duplicates
	// if an instruction has a repeated operand.
	//
	// Referrers actually returns a pointer through which the
	// caller may perform mutations to the object's state.
	//
	// Referrers is currently only defined if Parent()!=nil,
	// i.e. for the function-local values FreeVar, Parameter,
	// Functions (iff anonymous) and all value-defining instructions.
	// It returns nil for named Functions, Builtin, Const and Global.
	//
	// Instruction.Operands contains the inverse of this relation.
	Referrers() *[]Instruction

	// Pos returns the location of the AST token most closely
	// associated with the operation that gave rise to this value,
	// or syntax.NoPos if it was not explicit in the source.
	//
	// For each syntax.Node type, a particular token is designated as
	// the closest location for the expression, e.g. the Lparen
	// for an *syntax.CallExpr.  This permits a compact but
	// approximate mapping from Values to source positions for use
	// in diagnostic messages, for example.
	//
	// (Do not use this position to determine which Value
	// corresponds to an syntax.Expr; use Function.ValueForExpr
	// instead.  NB: it requires that the function was built with
	// debug information.)
	Pos() Pos
}

A Value is an SSA value that can be referenced by an instruction.

type Var

type Var struct {
	// contains filtered or unexported fields
}

A Variable represents a declared variable (including function parameters and results, and struct fields).

func NewField

func NewField(pos Pos, pkg *Package, name string, typ Type, embedded bool) *Var

NewField returns a new variable representing a struct field. For embedded fields, the name is the unqualified type name / under which the field is accessible.

func NewParam

func NewParam(pos Pos, pkg *Package, name string, typ Type) *Var

NewParam returns a new variable representing a function parameter.

func NewVar

func NewVar(pos Pos, pkg *Package, name string, typ Type) *Var

NewVar returns a new variable. The arguments set the attributes found with all Objects.

func (*Var) Anonymous

func (obj *Var) Anonymous() bool

Anonymous reports whether the variable is an embedded field. Same as Embedded; only present for backward-compatibility.

func (*Var) Embedded

func (obj *Var) Embedded() bool

Embedded reports whether the variable is an embedded field.

func (*Var) Exported

func (obj *Var) Exported() bool

Exported reports whether the object is exported (starts with a capital letter). It doesn't take into account whether the object is in a local (function) scope or not.

func (*Var) Id

func (obj *Var) Id() string

Id is a wrapper for Id(obj.Pkg(), obj.Name()).

func (*Var) IsField

func (obj *Var) IsField() bool

IsField reports whether the variable is a struct field.

func (*Var) Name

func (obj *Var) Name() string

Name returns the object's (package-local, unqualified) name.

func (*Var) Parent

func (obj *Var) Parent() *Scope

Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.

func (*Var) Pkg

func (obj *Var) Pkg() *Package

Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.

func (*Var) Pos

func (obj *Var) Pos() Pos

Pos returns the declaration position of the object's identifier.

func (*Var) String

func (obj *Var) String() string

func (*Var) Type

func (obj *Var) Type() Type

Type returns the object's type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL