forked from alecthomas/gometalinter
/
cache.go
127 lines (112 loc) · 2.44 KB
/
cache.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
// Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package interfacer
import (
"go/types"
"golang.org/x/tools/go/loader"
"github.com/mvdan/interfacer/internal/util"
)
//go:generate sh -c "go list std | go run generate/std/main.go -o std.go"
//go:generate gofmt -w -s std.go
type cache struct {
loader.Config
cur pkgCache
grabbed map[string]pkgCache
}
type pkgCache struct {
exp, unexp typeSet
}
type typeSet struct {
ifaces map[string]string
funcs map[string]string
}
func newCache() *cache {
c := &cache{
grabbed: make(map[string]pkgCache),
}
c.AllowErrors = true
c.TypeChecker.Error = func(e error) {}
c.TypeChecker.DisableUnusedImportCheck = true
c.TypeCheckFuncBodies = func(path string) bool {
_, e := stdPkgs[path]
return !e
}
return c
}
func (c *cache) funcOf(t string) string {
if s := stdFuncs[t]; s != "" {
return s
}
if s := c.cur.exp.funcs[t]; s != "" {
return s
}
return c.cur.unexp.funcs[t]
}
func (c *cache) ifaceOf(t string) string {
if s := stdIfaces[t]; s != "" {
return s
}
if s := c.cur.exp.ifaces[t]; s != "" {
return s
}
return c.cur.unexp.ifaces[t]
}
func (c *cache) grabNames(pkg *types.Package) {
c.fillCache(pkg)
c.cur = c.grabbed[pkg.Path()]
}
func (c *cache) fillCache(pkg *types.Package) {
path := pkg.Path()
if _, e := stdPkgs[path]; e {
return
}
if _, e := c.grabbed[path]; e {
return
}
for _, imp := range pkg.Imports() {
c.fillCache(imp)
}
cur := pkgCache{
exp: typeSet{
ifaces: make(map[string]string),
funcs: make(map[string]string),
},
unexp: typeSet{
ifaces: make(map[string]string),
funcs: make(map[string]string),
},
}
addTypes := func(impPath string, ifs, funs map[string]string, top bool) {
fullName := func(name string) string {
if !top {
return impPath + "." + name
}
return name
}
for iftype, name := range ifs {
if _, e := stdIfaces[iftype]; e {
continue
}
if util.Exported(name) {
cur.exp.ifaces[iftype] = fullName(name)
}
}
for ftype, name := range funs {
if _, e := stdFuncs[ftype]; e {
continue
}
if !util.Exported(name) {
cur.unexp.funcs[ftype] = fullName(name)
} else {
cur.exp.funcs[ftype] = fullName(name)
}
}
}
for _, imp := range pkg.Imports() {
pc := c.grabbed[imp.Path()]
addTypes(imp.Path(), pc.exp.ifaces, pc.exp.funcs, false)
}
ifs, funs := FromScope(pkg.Scope())
addTypes(path, ifs, funs, true)
c.grabbed[path] = cur
}