/
package.go
301 lines (251 loc) · 6.39 KB
/
package.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package gompatible
import (
"bytes"
"fmt"
"go/ast"
"go/build"
"go/doc"
"go/parser"
"go/printer"
"go/token"
"go/types"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/go/loader"
)
// Package represents a parsed, type-checked and documented package.
type Package struct {
// The types aspect of the package
TypesPkg *types.Package
// The docs aspect of the package
DocPkg *doc.Package
Funcs map[string]*Func
Types map[string]*Type
Values map[string]*Value
Fset *token.FileSet
}
// Func is a syntactically parsed, type-checked and (maybe) documented function.
type Func struct {
Package *Package
Types *types.Func
Doc *doc.Func
}
// Type is a syntactically parsed, type-checked and (maybe) documented type declaration.
type Type struct {
Package *Package
Types *types.TypeName
Doc *doc.Type
Funcs map[string]*Func
Methods map[string]*Func
}
// Value is a syntactically parsed, type-checked and (maybe) documented toplevel value (var or const).
type Value struct {
Name string
Package *Package
Doc *doc.Value
Types types.Object // *types.Var (IsConst == false) or *types.Const (IsConst == true)
IsConst bool
}
// XXX should the return value be a map from dir to files? (currently assumed importPath to files)
func listDirFiles(dir *DirSpec, recurse bool) (map[string][]string, error) {
ctx, err := dir.buildContext()
if err != nil {
return nil, err
}
packages := map[string][]string{}
var mode build.ImportMode
p, err := ctx.ImportDir(dir.Path, mode)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
// nop
} else {
return nil, fmt.Errorf("while loading %s: %s", dir, err)
}
} else {
importPath := p.ImportPath
if importPath == "." {
importPath = p.Dir
}
if dir.pkgOverride != "" {
importPath = dir.pkgOverride
}
// XXX something's wrong if packages[importPath] exists already
packages[importPath] = make([]string, len(p.GoFiles))
for i, file := range p.GoFiles {
packages[importPath][i] = buildutil.JoinPath(ctx, dir.Path, file)
}
}
if recurse == false {
return packages, nil
}
entries, err := dir.ReadDir()
if err != nil {
return nil, err
}
for _, e := range entries {
if e.IsDir() == false {
continue
}
if name := e.Name(); name[0] == '.' || name[0] == '_' {
continue
}
// copy
subdir := *dir
subdir.Path = buildutil.JoinPath(ctx, dir.Path, e.Name())
pkgs, err := listDirFiles(&subdir, recurse)
if err != nil {
return nil, err
}
for path, files := range pkgs {
packages[path] = files
}
}
return packages, nil
}
func LoadDir(dir *DirSpec, recurse bool) (map[string]*Package, error) {
ctx, err := dir.buildContext()
if err != nil {
return nil, err
}
files, err := listDirFiles(dir, recurse)
if err != nil {
return nil, err
}
return LoadPackages(ctx, files)
}
func LoadPackages(ctx *build.Context, filepaths map[string][]string) (map[string]*Package, error) {
conf := &loader.Config{
Build: ctx,
ParserMode: parser.ParseComments,
TypeCheckFuncBodies: func(_ string) bool { return false },
}
for path, files := range filepaths {
Debugf("CreateFromFilenames %s %v", path, files)
conf.CreateFromFilenames(path, files...)
}
prog, err := conf.Load()
if err != nil {
return nil, err
}
packages := map[string]*Package{}
for _, pkg := range prog.Created {
packages[pkg.String()] = packageFromInfo(prog, pkg)
}
return packages, nil
}
func packageFromInfo(prog *loader.Program, pkgInfo *loader.PackageInfo) *Package {
files := map[string]*ast.File{}
for _, f := range pkgInfo.Files {
files[prog.Fset.File(f.Pos()).Name()] = f
}
// Ignore (perhaps) "unresolved identifier" errors
astPkg, _ := ast.NewPackage(prog.Fset, files, nil, nil)
var mode doc.Mode
docPkg := doc.New(astPkg, pkgInfo.String(), mode)
return NewPackage(prog.Fset, docPkg, pkgInfo.Pkg)
}
// NewPackage builds a Package from one from doc and types package.
func NewPackage(fset *token.FileSet, doc *doc.Package, types *types.Package) *Package {
pkg := &Package{
Fset: fset,
DocPkg: doc,
TypesPkg: types,
}
pkg.buildFuncs()
pkg.buildTypes()
pkg.buildValues()
return pkg
}
func (p *Package) buildFuncs() map[string]*Func {
if p.Funcs != nil {
return p.Funcs
}
p.Funcs = map[string]*Func{}
for _, docF := range p.DocPkg.Funcs {
name := docF.Name
if typesF, ok := p.TypesPkg.Scope().Lookup(name).(*types.Func); ok {
p.Funcs[name] = &Func{
Package: p,
Doc: docF,
Types: typesF,
}
}
}
return p.Funcs
}
func (p *Package) buildTypes() map[string]*Type {
if p.Types != nil {
return p.Types
}
p.Types = map[string]*Type{}
for _, docT := range p.DocPkg.Types {
name := docT.Name
if typesT, ok := p.TypesPkg.Scope().Lookup(name).(*types.TypeName); ok {
funcs := make(map[string]*Func, len(docT.Funcs))
for _, f := range docT.Funcs {
funcs[f.Name] = &Func{
Package: p,
Doc: f,
Types: p.TypesPkg.Scope().Lookup(f.Name).(*types.Func),
}
}
methods := make(map[string]*Func, len(docT.Methods))
for _, m := range docT.Methods {
// Must be found and be *types.Func
obj, _, _ := types.LookupFieldOrMethod(typesT.Type(), true, p.TypesPkg, m.Name)
methods[m.Name] = &Func{
Package: p,
Doc: m,
Types: obj.(*types.Func),
}
}
p.Types[name] = &Type{
Package: p,
Doc: docT,
Types: typesT,
Funcs: funcs,
Methods: methods,
}
}
}
return p.Types
}
func (p *Package) buildValues() map[string]*Value {
if p.Values != nil {
return p.Values
}
p.Values = map[string]*Value{}
docValues := append(p.DocPkg.Vars, p.DocPkg.Consts...)
for _, docT := range p.DocPkg.Types {
docValues = append(docValues, docT.Vars...)
docValues = append(docValues, docT.Consts...)
}
for _, docV := range docValues {
for _, name := range docV.Names {
switch typesV := p.TypesPkg.Scope().Lookup(name).(type) {
case *types.Var:
p.Values[name] = &Value{
Name: name,
Package: p,
Doc: docV,
Types: typesV,
IsConst: false,
}
case *types.Const:
p.Values[name] = &Value{
Name: name,
Package: p,
Doc: docV,
Types: typesV,
IsConst: true,
}
}
}
}
return p.Values
}
// showASTNode takes an AST node to return its string presentation.
func (p Package) showASTNode(node interface{}) string {
var buf bytes.Buffer
printer.Fprint(&buf, p.Fset, node)
return buf.String()
}