Debian Codegraph @

go/ir/irutil/load.go

raw
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
5 package irutil
6
7 // This file defines utility functions for constructing programs in IR form.
8
9 import (
10 "go/ast"
11 "go/token"
12 "go/types"
13
14 "honnef.co/go/tools/go/ir"
15
16 "golang.org/x/tools/go/packages"
17 )
18
19 // Packages creates an IR 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 IR 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 IR 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. IR code is constructed only for the initial
33 // packages with well-typed syntax trees.
34 //
35 // The mode parameter controls diagnostics and checking during IR construction.
36 func Packages(initial []*packages.Package, mode ir.BuilderMode) (*ir.Program, []*ir.Package) {
37 // TODO(adonovan): opt: this calls CreatePackage far more than
38 // necessary: for all dependencies, not just the (non-initial)
39 // direct dependencies of the initial packages.
40 //
41 // But can it reasonably be changed without breaking the
42 // spirit and/or letter of the law above? Clients may notice
43 // if we call CreatePackage less, as methods like
44 // Program.FuncValue will return nil. Or must we provide a new
45 // function (and perhaps deprecate this one)? Is it worth it?
46 //
47 // Tim King makes the interesting point that it would be
48 // possible to entirely alleviate the client from the burden
49 // of calling CreatePackage for non-syntax packages, if we
50 // were to treat vars and funcs lazily in the same way we now
51 // treat methods. (In essence, try to move away from the
52 // notion of ir.Packages, and make the Program answer
53 // all reasonable questions about any types.Object.)
54
55 return doPackages(initial, mode, false)
56 }
57
58 // AllPackages creates an IR program for a set of packages plus all
59 // their dependencies.
60 //
61 // The packages must have been loaded from source syntax using the
62 // golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode.
63 //
64 // AllPackages creates an IR package for each well-typed package in the
65 // initial list, plus all their dependencies. The resulting list of
66 // packages corresponds to the list of initial packages, and may contain
67 // a nil if IR code could not be constructed for the corresponding
68 // initial package due to type errors.
69 //
70 // Code for bodies of functions is not built until Build is called on
71 // the resulting Program. IR code is constructed for all packages with
72 // well-typed syntax trees.
73 //
74 // The mode parameter controls diagnostics and checking during IR construction.
75 func AllPackages(initial []*packages.Package, mode ir.BuilderMode) (*ir.Program, []*ir.Package) {
76 return doPackages(initial, mode, true)
77 }
78
79 func doPackages(initial []*packages.Package, mode ir.BuilderMode, deps bool) (*ir.Program, []*ir.Package) {
80 var fset *token.FileSet
81 if len(initial) > 0 {
82 fset = initial[0].Fset
83 }
84
85 prog := ir.NewProgram(fset, mode)
86
87 isInitial := make(map[*packages.Package]bool, len(initial))
88 for _, p := range initial {
89 isInitial[p] = true
90 }
91
92 irmap := make(map[*packages.Package]*ir.Package)
93 packages.Visit(initial, nil, func(p *packages.Package) {
94 if p.Types != nil && !p.IllTyped {
95 var files []*ast.File
96 var info *types.Info
97 if deps || isInitial[p] {
98 files = p.Syntax
99 info = p.TypesInfo
100 }
101 irmap[p] = prog.CreatePackage(p.Types, files, info, true)
102 }
103 })
104
105 var irpkgs []*ir.Package
106 for _, p := range initial {
107 irpkgs = append(irpkgs, irmap[p]) // may be nil
108 }
109 return prog, irpkgs
110 }
111
112 // BuildPackage builds an IR program with IR intermediate
113 // representation (IR) for all functions of a single package.
114 //
115 // It populates pkg by type-checking the specified file ASTs. All
116 // dependencies are loaded using the importer specified by tc, which
117 // typically loads compiler export data; IR code cannot be built for
118 // those packages. BuildPackage then constructs an ir.Program with all
119 // dependency packages created, and builds and returns the IR package
120 // corresponding to pkg.
121 //
122 // The caller must have set pkg.Path() to the import path.
123 //
124 // The operation fails if there were any type-checking or import errors.
125 //
126 // See ../ir/example_test.go for an example.
127 func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ir.BuilderMode) (*ir.Package, *types.Info, error) {
128 if fset == nil {
129 panic("no token.FileSet")
130 }
131 if pkg.Path() == "" {
132 panic("package has no import path")
133 }
134
135 info := &types.Info{
136 Types: make(map[ast.Expr]types.TypeAndValue),
137 Defs: make(map[*ast.Ident]types.Object),
138 Uses: make(map[*ast.Ident]types.Object),
139 Implicits: make(map[ast.Node]types.Object),
140 Scopes: make(map[ast.Node]*types.Scope),
141 Selections: make(map[*ast.SelectorExpr]*types.Selection),
142 Instances: make(map[*ast.Ident]types.Instance),
143 FileVersions: make(map[*ast.File]string),
144 }
145 if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil {
146 return nil, nil, err
147 }
148
149 prog := ir.NewProgram(fset, mode)
150
151 // Create IR packages for all imports.
152 // Order is not significant.
153 created := make(map[*types.Package]bool)
154 var createAll func(pkgs []*types.Package)
155 createAll = func(pkgs []*types.Package) {
156 for _, p := range pkgs {
157 if !created[p] {
158 created[p] = true
159 prog.CreatePackage(p, nil, nil, true)
160 createAll(p.Imports())
161 }
162 }
163 }
164 createAll(pkg.Imports())
165
166 // TODO(adonovan): we could replace createAll with just:
167 //
168 // // Create IR packages for all imports.
169 // for _, p := range pkg.Imports() {
170 // prog.CreatePackage(p, nil, nil, true)
171 // }
172 //
173 // (with minor changes to changes to ../builder_test.go as
174 // shown in CL 511715 PS 10.) But this would strictly violate
175 // the letter of the doc comment above, which says "all
176 // dependencies created".
177 //
178 // Tim makes the good point with some extra work we could
179 // remove the need for any CreatePackage calls except the
180 // ones with syntax (i.e. primary packages). Of course
181 // You wouldn't have ir.Packages and Members for as
182 // many things but no-one really uses that anyway.
183 // I wish I had done this from the outset.
184
185 // Create and build the primary package.
186 irpkg := prog.CreatePackage(pkg, files, info, false)
187 irpkg.Build()
188 return irpkg, info, nil
189 }