Documentation ¶
Index ¶
- func AllFunctions(prog *ssa.Program) map[*ssa.Function]bool
- func AllPackages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package)
- func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, ...) (*ssa.Package, *types.Info, error)
- func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Programdeprecated
- func MainPackages(pkgs []*ssa.Package) []*ssa.Package
- func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package)
- type ConstCase
- type Switch
- type TypeCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllFunctions ¶
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.
TODO(adonovan): this function is underspecified. It doesn't actually work like a linker, which computes reachability from main using something like go/callgraph/cha (without materializing the call graph). In fact, it treats all public functions and all methods of public non-parameterized types as roots, even though they may be unreachable--but only in packages created from syntax.
I think we should deprecate AllFunctions function in favor of two clearly defined ones:
The first would efficiently compute CHA reachability from a set of main packages, making it suitable for a whole-program analysis context with InstantiateGenerics, in conjunction with Program.Build.
The second would return only the set of functions corresponding to source Func{Decl,Lit} syntax, like SrcFunctions in go/analysis/passes/buildssa; this is suitable for package-at-a-time (or handful of packages) context. ssa.Package could easily expose it as a field.
We could add them unexported for now and use them via the linkname hack.
func AllPackages ¶
AllPackages creates an SSA program for a set of packages plus all their dependencies.
The packages must have been loaded from source syntax using the packages.Load function in packages.LoadAllSyntax mode.
AllPackages creates an SSA 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 SSA 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. SSA code is constructed for all packages with well-typed syntax trees.
The mode parameter controls diagnostics and checking during SSA construction.
func BuildPackage ¶
func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error)
BuildPackage builds an SSA program with SSA intermediate representation (IR) for all functions of a single package.
It populates pkg by type-checking the specified file syntax trees. All dependencies are loaded using the importer specified by tc, which typically loads compiler export data; SSA code cannot be built for those packages. BuildPackage then constructs an ssa.Program with all dependency packages created, and builds and returns the SSA 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 ../example_test.go for an example.
func CreateProgram
deprecated
CreateProgram returns a new program in SSA form, given a program loaded from source. An SSA 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 SSA construction.
Deprecated: Use golang.org/x/tools/go/packages and the Packages function instead; see ssa.Example_loadPackages.
func MainPackages ¶
MainPackages returns the subset of the specified packages named "main" that define a main function. The result may include synthetic "testmain" packages.
func Packages ¶
Packages creates an SSA program for a set of packages.
The packages must have been loaded from source syntax using the packages.Load function in packages.LoadSyntax or packages.LoadAllSyntax mode.
Packages creates an SSA 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 SSA code could not be constructed for the corresponding initial package due to type errors.
Code for bodies of functions is not built until [Program.Build] is called on the resulting Program. SSA code is constructed only for the initial packages with well-typed syntax trees.
The mode parameter controls diagnostics and checking during SSA construction.
Types ¶
type ConstCase ¶
type ConstCase struct { Block *ssa.BasicBlock // block performing the comparison Body *ssa.BasicBlock // body of the case Value *ssa.Const // case comparand }
A ConstCase represents a single constant comparison. It is part of a Switch.
type Switch ¶
type Switch struct { Start *ssa.BasicBlock // block containing start of if/else chain X ssa.Value // the switch operand ConstCases []ConstCase // ordered list of constant comparisons TypeCases []TypeCase // ordered list of type assertions Default *ssa.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 ssa.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 ¶
Switches examines the control-flow graph of fn and returns the set of inferred value and type switches. A value switch tests an ssa.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 ssa.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 SSA representation.)
type TypeCase ¶
type TypeCase struct { Block *ssa.BasicBlock // block performing the type assert Body *ssa.BasicBlock // body of the case Type types.Type // case type Binding ssa.Value // value bound by this case }
A TypeCase represents a single type assertion. It is part of a Switch.