1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // This file defines utility functions for constructing programs in SSA form.
14 "golang.org/x/tools/go/loader"
15 "golang.org/x/tools/go/packages"
16 "golang.org/x/tools/go/ssa"
19 // Packages creates an SSA program for a set of packages.
21 // The packages must have been loaded from source syntax using the
22 // golang.org/x/tools/go/packages.Load function in LoadSyntax or
23 // LoadAllSyntax mode.
25 // Packages creates an SSA package for each well-typed package in the
26 // initial list, plus all their dependencies. The resulting list of
27 // packages corresponds to the list of initial packages, and may contain
28 // a nil if SSA code could not be constructed for the corresponding initial
29 // package due to type errors.
31 // Code for bodies of functions is not built until Build is called on
32 // the resulting Program. SSA code is constructed only for the initial
33 // packages with well-typed syntax trees.
35 // The mode parameter controls diagnostics and checking during SSA construction.
37 func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) {
38 return doPackages(initial, mode, false)
41 // AllPackages creates an SSA program for a set of packages plus all
42 // their dependencies.
44 // The packages must have been loaded from source syntax using the
45 // golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.
47 // AllPackages creates an SSA package for each well-typed package in the
48 // initial list, plus all their dependencies. The resulting list of
49 // packages corresponds to the list of initial packages, and may contain
50 // a nil if SSA code could not be constructed for the corresponding
51 // initial package due to type errors.
53 // Code for bodies of functions is not built until Build is called on
54 // the resulting Program. SSA code is constructed for all packages with
55 // well-typed syntax trees.
57 // The mode parameter controls diagnostics and checking during SSA construction.
59 func AllPackages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) {
60 return doPackages(initial, mode, true)
63 func doPackages(initial []*packages.Package, mode ssa.BuilderMode, deps bool) (*ssa.Program, []*ssa.Package) {
65 var fset *token.FileSet
67 fset = initial[0].Fset
70 prog := ssa.NewProgram(fset, mode)
72 isInitial := make(map[*packages.Package]bool, len(initial))
73 for _, p := range initial {
77 ssamap := make(map[*packages.Package]*ssa.Package)
78 packages.Visit(initial, nil, func(p *packages.Package) {
79 if p.Types != nil && !p.IllTyped {
81 if deps || isInitial[p] {
84 ssamap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true)
88 var ssapkgs []*ssa.Package
89 for _, p := range initial {
90 ssapkgs = append(ssapkgs, ssamap[p]) // may be nil
95 // CreateProgram returns a new program in SSA form, given a program
96 // loaded from source. An SSA package is created for each transitively
97 // error-free package of lprog.
99 // Code for bodies of functions is not built until Build is called
102 // The mode parameter controls diagnostics and checking during SSA construction.
104 // Deprecated: Use golang.org/x/tools/go/packages and the Packages
105 // function instead; see ssa.ExampleLoadPackages.
107 func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program {
108 prog := ssa.NewProgram(lprog.Fset, mode)
110 for _, info := range lprog.AllPackages {
111 if info.TransitivelyErrorFree {
112 prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
119 // BuildPackage builds an SSA program with IR for a single package.
121 // It populates pkg by type-checking the specified file ASTs. All
122 // dependencies are loaded using the importer specified by tc, which
123 // typically loads compiler export data; SSA code cannot be built for
124 // those packages. BuildPackage then constructs an ssa.Program with all
125 // dependency packages created, and builds and returns the SSA package
126 // corresponding to pkg.
128 // The caller must have set pkg.Path() to the import path.
130 // The operation fails if there were any type-checking or import errors.
132 // See ../ssa/example_test.go for an example.
134 func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) {
136 panic("no token.FileSet")
138 if pkg.Path() == "" {
139 panic("package has no import path")
143 Types: make(map[ast.Expr]types.TypeAndValue),
144 Defs: make(map[*ast.Ident]types.Object),
145 Uses: make(map[*ast.Ident]types.Object),
146 Implicits: make(map[ast.Node]types.Object),
147 Scopes: make(map[ast.Node]*types.Scope),
148 Selections: make(map[*ast.SelectorExpr]*types.Selection),
150 if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
154 prog := ssa.NewProgram(fset, mode)
156 // Create SSA packages for all imports.
157 // Order is not significant.
158 created := make(map[*types.Package]bool)
159 var createAll func(pkgs []*types.Package)
160 createAll = func(pkgs []*types.Package) {
161 for _, p := range pkgs {
164 prog.CreatePackage(p, nil, nil, true)
165 createAll(p.Imports())
169 createAll(pkg.Imports())
171 // Create and build the primary package.
172 ssapkg := prog.CreatePackage(pkg, files, info, false)
174 return ssapkg, info, nil