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 "honnef.co/go/tools/go/ir"
16 "golang.org/x/tools/go/loader"
17 "golang.org/x/tools/go/packages"
21 // Which function, if any, to print in HTML form
25 // Packages creates an IR program for a set of packages.
27 // The packages must have been loaded from source syntax using the
28 // golang.org/x/tools/go/packages.Load function in LoadSyntax or
29 // LoadAllSyntax mode.
31 // Packages creates an IR package for each well-typed package in the
32 // initial list, plus all their dependencies. The resulting list of
33 // packages corresponds to the list of initial packages, and may contain
34 // a nil if IR code could not be constructed for the corresponding initial
35 // package due to type errors.
37 // Code for bodies of functions is not built until Build is called on
38 // the resulting Program. IR code is constructed only for the initial
39 // packages with well-typed syntax trees.
41 // The mode parameter controls diagnostics and checking during IR construction.
43 func Packages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package) {
44 return doPackages(initial, mode, false, opts)
47 // AllPackages creates an IR program for a set of packages plus all
48 // their dependencies.
50 // The packages must have been loaded from source syntax using the
51 // golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.
53 // AllPackages creates an IR package for each well-typed package in the
54 // initial list, plus all their dependencies. The resulting list of
55 // packages corresponds to the list of initial packages, and may contain
56 // a nil if IR code could not be constructed for the corresponding
57 // initial package due to type errors.
59 // Code for bodies of functions is not built until Build is called on
60 // the resulting Program. IR code is constructed for all packages with
61 // well-typed syntax trees.
63 // The mode parameter controls diagnostics and checking during IR construction.
65 func AllPackages(initial []*packages.Package, mode ir.BuilderMode, opts *Options) (*ir.Program, []*ir.Package) {
66 return doPackages(initial, mode, true, opts)
69 func doPackages(initial []*packages.Package, mode ir.BuilderMode, deps bool, opts *Options) (*ir.Program, []*ir.Package) {
71 var fset *token.FileSet
73 fset = initial[0].Fset
76 prog := ir.NewProgram(fset, mode)
78 prog.PrintFunc = opts.PrintFunc
81 isInitial := make(map[*packages.Package]bool, len(initial))
82 for _, p := range initial {
86 irmap := make(map[*packages.Package]*ir.Package)
87 packages.Visit(initial, nil, func(p *packages.Package) {
88 if p.Types != nil && !p.IllTyped {
90 if deps || isInitial[p] {
93 irmap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true)
97 var irpkgs []*ir.Package
98 for _, p := range initial {
99 irpkgs = append(irpkgs, irmap[p]) // may be nil
104 // CreateProgram returns a new program in IR form, given a program
105 // loaded from source. An IR package is created for each transitively
106 // error-free package of lprog.
108 // Code for bodies of functions is not built until Build is called
111 // The mode parameter controls diagnostics and checking during IR construction.
113 // Deprecated: use golang.org/x/tools/go/packages and the Packages
114 // function instead; see ir.ExampleLoadPackages.
116 func CreateProgram(lprog *loader.Program, mode ir.BuilderMode) *ir.Program {
117 prog := ir.NewProgram(lprog.Fset, mode)
119 for _, info := range lprog.AllPackages {
120 if info.TransitivelyErrorFree {
121 prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
128 // BuildPackage builds an IR program with IR for a single package.
130 // It populates pkg by type-checking the specified file ASTs. All
131 // dependencies are loaded using the importer specified by tc, which
132 // typically loads compiler export data; IR code cannot be built for
133 // those packages. BuildPackage then constructs an ir.Program with all
134 // dependency packages created, and builds and returns the IR package
135 // corresponding to pkg.
137 // The caller must have set pkg.Path() to the import path.
139 // The operation fails if there were any type-checking or import errors.
141 // See ../ir/example_test.go for an example.
143 func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ir.BuilderMode) (*ir.Package, *types.Info, error) {
145 panic("no token.FileSet")
147 if pkg.Path() == "" {
148 panic("package has no import path")
152 Types: make(map[ast.Expr]types.TypeAndValue),
153 Defs: make(map[*ast.Ident]types.Object),
154 Uses: make(map[*ast.Ident]types.Object),
155 Implicits: make(map[ast.Node]types.Object),
156 Scopes: make(map[ast.Node]*types.Scope),
157 Selections: make(map[*ast.SelectorExpr]*types.Selection),
159 if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
163 prog := ir.NewProgram(fset, mode)
165 // Create IR packages for all imports.
166 // Order is not significant.
167 created := make(map[*types.Package]bool)
168 var createAll func(pkgs []*types.Package)
169 createAll = func(pkgs []*types.Package) {
170 for _, p := range pkgs {
173 prog.CreatePackage(p, nil, nil, true)
174 createAll(p.Imports())
178 createAll(pkg.Imports())
180 // Create and build the primary package.
181 irpkg := prog.CreatePackage(pkg, files, info, false)
183 return irpkg, info, nil