-
Notifications
You must be signed in to change notification settings - Fork 2
/
reach.go
178 lines (147 loc) · 3.54 KB
/
reach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package compat
import (
"fmt"
"go/ast"
"go/types"
"reflect"
"golang.org/x/tools/go/packages"
)
const goLoadMode = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes
// ReachableFromPackages gets an API given list of package paths.
func ReachableFromPackages(pkgs ...string) (*API, error) {
return reachableFromPackages(false, pkgs...)
}
func reachableFromPackages(tests bool, pkgs ...string) (*API, error) {
r := newReachability()
if err := r.FromPackages(tests, pkgs...); err != nil {
return nil, err
}
return r.API, nil
}
type reachability struct {
API *API
reachable map[types.Object]bool
seen map[types.Type]bool
}
func newReachability() *reachability {
return &reachability{
API: NewAPI(),
reachable: make(map[types.Object]bool),
seen: make(map[types.Type]bool),
}
}
func (r *reachability) FromPackages(tests bool, pkgs ...string) error {
conf := &packages.Config{
Mode: goLoadMode,
Tests: tests,
}
loadedPackages, err := packages.Load(conf, pkgs...)
if err != nil {
return err
}
for _, pkg := range loadedPackages {
pkg, err := r.fromPackage(pkg)
if err != nil {
return err
}
r.API.Packages = append(r.API.Packages, pkg)
}
return nil
}
func (r *reachability) fromPackage(pkg *packages.Package) (*Package, error) {
mypkg := NewPackage(pkg.Types.Path())
if len(pkg.Errors) > 0 {
return nil, fmt.Errorf("has errors: %s", pkg.Errors[0])
}
scope := pkg.Types.Scope()
for _, name := range scope.Names() {
if !ast.IsExported(name) {
continue
}
obj := scope.Lookup(name)
mypkg.Objects[name] = ConvertObject(obj)
if err := r.reachFromObject(obj); err != nil {
return mypkg, err
}
}
return mypkg, nil
}
func (r *reachability) reachFromObject(obj types.Object) error {
if obj.Pkg() == nil {
return nil
}
if obj.Parent() != nil {
if r.reachable[obj] {
return nil
}
r.reachable[obj] = true
cobj := ConvertObject(obj)
r.API.Reachable = append(r.API.Reachable, cobj)
}
return r.reachFromType(obj.Type())
}
func (r *reachability) reachFromType(typ types.Type) error {
if r.seen[typ] {
return nil
}
r.seen[typ] = true
switch typ := typ.(type) {
case *types.Named:
if err := r.reachFromObject(typ.Obj()); err != nil {
return err
}
for i := 0; i < typ.NumMethods(); i++ {
m := typ.Method(i)
if err := r.reachFromObject(m); err != nil {
return err
}
}
return nil
case *types.Pointer:
return r.reachFromType(typ.Elem())
case *types.Struct:
for i := 0; i < typ.NumFields(); i++ {
f := typ.Field(i)
if err := r.reachFromObject(f); err != nil {
return err
}
}
return nil
case *types.Signature:
if err := r.reachFromType(typ.Params()); err != nil {
return err
}
return r.reachFromType(typ.Results())
case *types.Tuple:
for i := 0; i < typ.Len(); i++ {
f := typ.At(i)
if err := r.reachFromObject(f); err != nil {
return err
}
}
return nil
case *types.Slice:
return r.reachFromType(typ.Elem())
case *types.Map:
if err := r.reachFromType(typ.Key()); err != nil {
return err
}
return r.reachFromType(typ.Elem())
case *types.Basic:
return nil
case *types.Array:
return r.reachFromType(typ.Elem())
case *types.Chan:
return r.reachFromType(typ.Elem())
case *types.Interface:
for i := 0; i < typ.NumMethods(); i++ {
f := typ.Method(i)
if err := r.reachFromObject(f); err != nil {
return err
}
}
return nil
default:
return fmt.Errorf("unhandled type: %s", reflect.TypeOf(typ))
}
}