irutil

package
v0.0.0-...-de5fe6c Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2020 License: MIT, BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllFunctions

func AllFunctions(prog *ir.Program) map[*ir.Function]bool

AllFunctions finds and returns the set of functions potentially needed by program prog, as determined by a simple linker-style reachability algorithm starting from the members and method-sets of each package. The result may include anonymous functions and synthetic wrappers.

Precondition: all packages are built.

func AllPackages

func AllPackages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package)

AllPackages creates an IR program for a set of packages plus all their dependencies.

The packages must have been loaded from source syntax using the golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.

AllPackages creates an IR package for each well-typed package in the initial list, plus all their dependencies. The resulting list of packages corresponds to the list of initial packages, and may contain a nil if IR code could not be constructed for the corresponding initial package due to type errors.

Code for bodies of functions is not built until Build is called on the resulting Program. IR code is constructed for all packages with well-typed syntax trees.

The mode parameter controls diagnostics and checking during IR construction.

func BuildPackage

func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ir.BuilderMode) (*ir.Package, *types.Info, error)

BuildPackage builds an IR program with IR for a single package.

It populates pkg by type-checking the specified file ASTs. All dependencies are loaded using the importer specified by tc, which typically loads compiler export data; IR code cannot be built for those packages. BuildPackage then constructs an ir.Program with all dependency packages created, and builds and returns the IR package corresponding to pkg.

The caller must have set pkg.Path() to the import path.

The operation fails if there were any type-checking or import errors.

See ../ir/example_test.go for an example.

func CallName

func CallName(call *ir.CallCommon) string

func CreateProgram deprecated

func CreateProgram(lprog *loader.Program, mode ir.BuilderMode) *ir.Program

CreateProgram returns a new program in IR form, given a program loaded from source. An IR package is created for each transitively error-free package of lprog.

Code for bodies of functions is not built until Build is called on the result.

The mode parameter controls diagnostics and checking during IR construction.

Deprecated: use golang.org/x/tools/go/packages and the Packages function instead; see ir.ExampleLoadPackages.

func FilterDebug

func FilterDebug(instr []ir.Instruction) []ir.Instruction

func IsCallTo

func IsCallTo(call *ir.CallCommon, name string) bool

func IsCallToAny

func IsCallToAny(call *ir.CallCommon, names ...string) bool

func IsExample

func IsExample(fn *ir.Function) bool

func IsStub

func IsStub(fn *ir.Function) bool

IsStub reports whether a function is a stub. A function is considered a stub if it has no instructions or if all it does is return a constant value.

func MainPackages

func MainPackages(pkgs []*ir.Package) []*ir.Package

MainPackages returns the subset of the specified packages named "main" that define a main function. The result may include synthetic "testmain" packages.

func Packages

func Packages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package)

Packages creates an IR program for a set of packages.

The packages must have been loaded from source syntax using the golang.org/x/tools/go/packages.Load function in LoadSyntax or LoadAllSyntax mode.

Packages creates an IR package for each well-typed package in the initial list, plus all their dependencies. The resulting list of packages corresponds to the list of initial packages, and may contain a nil if IR code could not be constructed for the corresponding initial package due to type errors.

Code for bodies of functions is not built until Build is called on the resulting Program. IR code is constructed only for the initial packages with well-typed syntax trees.

The mode parameter controls diagnostics and checking during IR construction.

func Reachable

func Reachable(from, to *ir.BasicBlock) bool

func Terminates

func Terminates(fn *ir.Function) bool

Terminates reports whether fn is supposed to return, that is if it has at least one theoretic path that returns from the function. Explicit panics do not count as terminating.

func Vararg

func Vararg(x *ir.Slice) ([]ir.Value, bool)

func Walk

func Walk(b *ir.BasicBlock, fn func(*ir.BasicBlock) bool)

Types

type ConstCase

type ConstCase struct {
	Block *ir.BasicBlock // block performing the comparison
	Body  *ir.BasicBlock // body of the case
	Value *ir.Const      // case comparand
}

A ConstCase represents a single constant comparison. It is part of a Switch.

type Loop

type Loop struct{ *ir.BlockSet }

func FindLoops

func FindLoops(fn *ir.Function) []Loop

type Options

type Options struct {
	// Which function, if any, to print in HTML form
	PrintFunc string
}

type Switch

type Switch struct {
	Start      *ir.BasicBlock // block containing start of if/else chain
	X          ir.Value       // the switch operand
	ConstCases []ConstCase    // ordered list of constant comparisons
	TypeCases  []TypeCase     // ordered list of type assertions
	Default    *ir.BasicBlock // successor if all comparisons fail
}

A Switch is a logical high-level control flow operation (a multiway branch) discovered by analysis of a CFG containing only if/else chains. It is not part of the ir.Instruction set.

One of ConstCases and TypeCases has length >= 2; the other is nil.

In a value switch, the list of cases may contain duplicate constants. A type switch may contain duplicate types, or types assignable to an interface type also in the list. TODO(adonovan): eliminate such duplicates.

func Switches

func Switches(fn *ir.Function) []Switch

Switches examines the control-flow graph of fn and returns the set of inferred value and type switches. A value switch tests an ir.Value for equality against two or more compile-time constant values. Switches involving link-time constants (addresses) are ignored. A type switch type-asserts an ir.Value against two or more types.

The switches are returned in dominance order.

The resulting switches do not necessarily correspond to uses of the 'switch' keyword in the source: for example, a single source-level switch statement with non-constant cases may result in zero, one or many Switches, one per plural sequence of constant cases. Switches may even be inferred from if/else- or goto-based control flow. (In general, the control flow constructs of the source program cannot be faithfully reproduced from the IR.)

func (*Switch) String

func (sw *Switch) String() string

type TypeCase

type TypeCase struct {
	Block   *ir.BasicBlock // block performing the type assert
	Body    *ir.BasicBlock // body of the case
	Type    types.Type     // case type
	Binding ir.Value       // value bound by this case
}

A TypeCase represents a single type assertion. It is part of a Switch.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL