1 // Copyright 2018 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.
9 // importMap computes the import map for a package by traversing the
10 // entire exported API each of its imports.
12 // This is a workaround for the fact that we cannot access the map used
13 // internally by the types.Importer returned by go/importer. The entries
14 // in this map are the packages and objects that may be relevant to the
15 // current analysis unit.
17 // Packages in the map that are only indirectly imported may be
18 // incomplete (!pkg.Complete()).
20 func importMap(imports []*types.Package) map[string]*types.Package {
21 objects := make(map[types.Object]bool)
22 packages := make(map[string]*types.Package)
24 var addObj func(obj types.Object) bool
25 var addType func(T types.Type)
27 addObj = func(obj types.Object) bool {
31 if pkg := obj.Pkg(); pkg != nil {
32 packages[pkg.Path()] = pkg
39 addType = func(T types.Type) {
40 switch T := T.(type) {
45 for i := 0; i < T.NumMethods(); i++ {
60 case *types.Signature:
64 for i := 0; i < T.NumFields(); i++ {
68 for i := 0; i < T.Len(); i++ {
71 case *types.Interface:
72 for i := 0; i < T.NumMethods(); i++ {
78 for _, imp := range imports {
79 packages[imp.Path()] = imp
82 for _, name := range scope.Names() {
83 addObj(scope.Lookup(name))