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.
4
5package ssautil
6
7// This file defines utility functions for constructing programs in SSA form.
8
9import (
10	"go/ast"
11	"go/token"
12	"go/types"
13
14	"golang.org/x/tools/go/loader"
15	"golang.org/x/tools/go/packages"
16	"golang.org/x/tools/go/ssa"
17)
18
19// Packages creates an SSA program for a set of packages.
20//
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.
24//
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.
30//
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.
34//
35// The mode parameter controls diagnostics and checking during SSA construction.
36//
37func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) {
38	return doPackages(initial, mode, false)
39}
40
41// AllPackages creates an SSA program for a set of packages plus all
42// their dependencies.
43//
44// The packages must have been loaded from source syntax using the
45// golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.
46//
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.
52//
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.
56//
57// The mode parameter controls diagnostics and checking during SSA construction.
58//
59func AllPackages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) {
60	return doPackages(initial, mode, true)
61}
62
63func doPackages(initial []*packages.Package, mode ssa.BuilderMode, deps bool) (*ssa.Program, []*ssa.Package) {
64
65	var fset *token.FileSet
66	if len(initial) > 0 {
67		fset = initial[0].Fset
68	}
69
70	prog := ssa.NewProgram(fset, mode)
71
72	isInitial := make(map[*packages.Package]bool, len(initial))
73	for _, p := range initial {
74		isInitial[p] = true
75	}
76
77	ssamap := make(map[*packages.Package]*ssa.Package)
78	packages.Visit(initial, nil, func(p *packages.Package) {
79		if p.Types != nil && !p.IllTyped {
80			var files []*ast.File
81			if deps || isInitial[p] {
82				files = p.Syntax
83			}
84			ssamap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true)
85		}
86	})
87
88	var ssapkgs []*ssa.Package
89	for _, p := range initial {
90		ssapkgs = append(ssapkgs, ssamap[p]) // may be nil
91	}
92	return prog, ssapkgs
93}
94
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.
98//
99// Code for bodies of functions is not built until Build is called
100// on the result.
101//
102// The mode parameter controls diagnostics and checking during SSA construction.
103//
104// Deprecated: Use golang.org/x/tools/go/packages and the Packages
105// function instead; see ssa.ExampleLoadPackages.
106//
107func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program {
108	prog := ssa.NewProgram(lprog.Fset, mode)
109
110	for _, info := range lprog.AllPackages {
111		if info.TransitivelyErrorFree {
112			prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
113		}
114	}
115
116	return prog
117}
118
119// BuildPackage builds an SSA program with IR for a single package.
120//
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.
127//
128// The caller must have set pkg.Path() to the import path.
129//
130// The operation fails if there were any type-checking or import errors.
131//
132// See ../example_test.go for an example.
133//
134func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) {
135	if fset == nil {
136		panic("no token.FileSet")
137	}
138	if pkg.Path() == "" {
139		panic("package has no import path")
140	}
141
142	info := &types.Info{
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),
149	}
150	if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
151		return nil, nil, err
152	}
153
154	prog := ssa.NewProgram(fset, mode)
155
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 {
162			if !created[p] {
163				created[p] = true
164				prog.CreatePackage(p, nil, nil, true)
165				createAll(p.Imports())
166			}
167		}
168	}
169	createAll(pkg.Imports())
170
171	// Create and build the primary package.
172	ssapkg := prog.CreatePackage(pkg, files, info, false)
173	ssapkg.Build()
174	return ssapkg, info, nil
175}
176