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 IR form.
14 "golang.org/x/tools/go/loader"
15 "golang.org/x/tools/go/packages"
16 "honnef.co/go/tools/ir"
20 // Which function, if any, to print in HTML form
24 // Packages creates an IR program for a set of packages.
26 // The packages must have been loaded from source syntax using the
27 // golang.org/x/tools/go/packages.Load function in LoadSyntax or
28 // LoadAllSyntax mode.
30 // Packages creates an IR package for each well-typed package in the
31 // initial list, plus all their dependencies. The resulting list of
32 // packages corresponds to the list of initial packages, and may contain
33 // a nil if IR code could not be constructed for the corresponding initial
34 // package due to type errors.
36 // Code for bodies of functions is not built until Build is called on
37 // the resulting Program. IR code is constructed only for the initial
38 // packages with well-typed syntax trees.
40 // The mode parameter controls diagnostics and checking during IR construction.
42 func Packages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package) {
43 return doPackages(initial, mode, false, opts)
46 // AllPackages creates an IR program for a set of packages plus all
47 // their dependencies.
49 // The packages must have been loaded from source syntax using the
50 // golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.
52 // AllPackages creates an IR package for each well-typed package in the
53 // initial list, plus all their dependencies. The resulting list of
54 // packages corresponds to the list of initial packages, and may contain
55 // a nil if IR code could not be constructed for the corresponding
56 // initial package due to type errors.
58 // Code for bodies of functions is not built until Build is called on
59 // the resulting Program. IR code is constructed for all packages with
60 // well-typed syntax trees.
62 // The mode parameter controls diagnostics and checking during IR construction.
64 func AllPackages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package) {
65 return doPackages(initial, mode, true, opts)
68 func doPackages(initial []*packages.Package, mode ir.BuilderMode, deps bool, opts *Options) (*ir.Program, []*ir.Package) {
70 var fset *token.FileSet
72 fset = initial[0].Fset
75 prog := ir.NewProgram(fset, mode)
77 prog.PrintFunc = opts.PrintFunc
80 isInitial := make(map[*packages.Package]bool, len(initial))
81 for _, p := range initial {
85 irmap := make(map[*packages.Package]*ir.Package)
86 packages.Visit(initial, nil, func(p *packages.Package) {
87 if p.Types != nil && !p.IllTyped {
89 if deps || isInitial[p] {
92 irmap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true)
96 var irpkgs []*ir.Package
97 for _, p := range initial {
98 irpkgs = append(irpkgs, irmap[p]) // may be nil
103 // CreateProgram returns a new program in IR form, given a program
104 // loaded from source. An IR package is created for each transitively
105 // error-free package of lprog.
107 // Code for bodies of functions is not built until Build is called
110 // The mode parameter controls diagnostics and checking during IR construction.
112 // Deprecated: use golang.org/x/tools/go/packages and the Packages
113 // function instead; see ir.ExampleLoadPackages.
115 func CreateProgram(lprog *loader.Program, mode ir.BuilderMode) *ir.Program {
116 prog := ir.NewProgram(lprog.Fset, mode)
118 for _, info := range lprog.AllPackages {
119 if info.TransitivelyErrorFree {
120 prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
127 // BuildPackage builds an IR program with IR for a single package.
129 // It populates pkg by type-checking the specified file ASTs. All
130 // dependencies are loaded using the importer specified by tc, which
131 // typically loads compiler export data; IR code cannot be built for
132 // those packages. BuildPackage then constructs an ir.Program with all
133 // dependency packages created, and builds and returns the IR package
134 // corresponding to pkg.
136 // The caller must have set pkg.Path() to the import path.
138 // The operation fails if there were any type-checking or import errors.
140 // See ../ir/example_test.go for an example.
142 func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ir.BuilderMode) (*ir.Package, *types.Info, error) {
144 panic("no token.FileSet")
146 if pkg.Path() == "" {
147 panic("package has no import path")
151 Types: make(map[ast.Expr]types.TypeAndValue),
152 Defs: make(map[*ast.Ident]types.Object),
153 Uses: make(map[*ast.Ident]types.Object),
154 Implicits: make(map[ast.Node]types.Object),
155 Scopes: make(map[ast.Node]*types.Scope),
156 Selections: make(map[*ast.SelectorExpr]*types.Selection),
158 if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
162 prog := ir.NewProgram(fset, mode)
164 // Create IR packages for all imports.
165 // Order is not significant.
166 created := make(map[*types.Package]bool)
167 var createAll func(pkgs []*types.Package)
168 createAll = func(pkgs []*types.Package) {
169 for _, p := range pkgs {
172 prog.CreatePackage(p, nil, nil, true)
173 createAll(p.Imports())
177 createAll(pkg.Imports())
179 // Create and build the primary package.
180 irpkg := prog.CreatePackage(pkg, files, info, false)
182 return irpkg, info, nil