-
Notifications
You must be signed in to change notification settings - Fork 316
/
usage.go
353 lines (302 loc) · 9.89 KB
/
usage.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package usage
import (
"fmt"
"go/ast"
"go/token"
"golang.org/x/exp/slices"
"encr.dev/pkg/fns"
"encr.dev/pkg/option"
"encr.dev/pkg/paths"
"encr.dev/v2/internals/perr"
"encr.dev/v2/internals/pkginfo"
"encr.dev/v2/internals/schema"
"encr.dev/v2/parser/resource"
)
// ResolveData is the data being passed to usage resolver functions.
type ResolveData struct {
Errs *perr.List
Expr Expr
}
type Expr interface {
// Node allows use to use the expression in error messages
// where the Pos/End is used to point directly at the resource
// bind being used, rather than the overall expression.
ast.Node
ResourceBind() resource.Bind
ASTExpr() ast.Expr
DeclaredIn() *pkginfo.File
// DescriptionForTest describes the expression for testing purposes.
DescriptionForTest() string
}
// FuncCall describes a resource being called as a function.
type FuncCall struct {
File *pkginfo.File
Bind resource.Bind
Call *ast.CallExpr
Args []ast.Expr
}
func (f *FuncCall) DeclaredIn() *pkginfo.File { return f.File }
func (f *FuncCall) ASTExpr() ast.Expr { return f.Call }
func (f *FuncCall) ResourceBind() resource.Bind { return f.Bind }
func (f *FuncCall) DescriptionForTest() string { return "called" }
func (f *FuncCall) Pos() token.Pos { return f.Call.Fun.Pos() }
func (f *FuncCall) End() token.Pos { return f.Call.Fun.End() }
// MethodCall describes a resource usage via a method call.
type MethodCall struct {
File *pkginfo.File
Bind resource.Bind
Call *ast.CallExpr
Method string
Args []ast.Expr
}
func (m *MethodCall) DeclaredIn() *pkginfo.File { return m.File }
func (m *MethodCall) ASTExpr() ast.Expr { return m.Call }
func (m *MethodCall) ResourceBind() resource.Bind { return m.Bind }
func (m *MethodCall) DescriptionForTest() string { return fmt.Sprintf("call %s", m.Method) }
func (m *MethodCall) Pos() token.Pos { return m.Call.Fun.Pos() }
func (m *MethodCall) End() token.Pos { return m.Call.Fun.End() }
// FieldAccess describes a resource usage via a field access.
type FieldAccess struct {
File *pkginfo.File
Bind resource.Bind
Expr *ast.SelectorExpr
Field string
}
func (f *FieldAccess) DeclaredIn() *pkginfo.File { return f.File }
func (f *FieldAccess) ASTExpr() ast.Expr { return f.Expr }
func (f *FieldAccess) ResourceBind() resource.Bind { return f.Bind }
func (f *FieldAccess) DescriptionForTest() string { return fmt.Sprintf("field %s", f.Field) }
func (f *FieldAccess) Pos() token.Pos { return f.Expr.Pos() }
func (f *FieldAccess) End() token.Pos { return f.Expr.End() }
// FuncArg describes a resource being used as a function argument.
type FuncArg struct {
File *pkginfo.File
Bind resource.Bind
Call *ast.CallExpr
// TypeArgs are the type arguments to the function being called, if any.
TypeArgs []schema.Type
// ArgIdx is the function argument index that represents
// the resource bind, starting at 0.
ArgIdx int
// PkgFunc is the package-level function that's being called.
// It's None if the function is not a package-level function.
PkgFunc option.Option[pkginfo.QualifiedName]
}
func (f *FuncArg) DeclaredIn() *pkginfo.File { return f.File }
func (f *FuncArg) ASTExpr() ast.Expr { return f.Call }
func (f *FuncArg) ResourceBind() resource.Bind { return f.Bind }
func (f *FuncArg) DescriptionForTest() string {
if fn, ok := f.PkgFunc.Get(); ok {
return fmt.Sprintf("fn %s arg %d", fn.NaiveDisplayName(), f.ArgIdx)
}
return fmt.Sprintf("arg %d", f.ArgIdx)
}
func (f *FuncArg) Pos() token.Pos { return f.Call.Args[f.ArgIdx].Pos() }
func (f *FuncArg) End() token.Pos { return f.Call.Args[f.ArgIdx].End() }
// Other describes any other resource usage.
type Other struct {
File *pkginfo.File
Bind resource.Bind
Expr ast.Expr
BindRef ast.Expr
}
func (o *Other) DeclaredIn() *pkginfo.File { return o.File }
func (o *Other) ASTExpr() ast.Expr { return o.Expr }
func (o *Other) ResourceBind() resource.Bind { return o.Bind }
func (o *Other) DescriptionForTest() string { return "other" }
func (o *Other) Pos() token.Pos { return o.BindRef.Pos() }
func (o *Other) End() token.Pos { return o.BindRef.End() }
func ParseExprs(schema *schema.Parser, pkgs []*pkginfo.Package, binds []resource.Bind) []Expr {
p := &usageParser{
schema: schema,
bindsPerPkg: make(map[paths.Pkg][]resource.Bind, len(binds)),
bindNames: make(map[pkginfo.QualifiedName]resource.Bind, len(binds)),
}
for _, b := range binds {
pkg := b.Package()
p.bindsPerPkg[pkg.ImportPath] = append(p.bindsPerPkg[pkg.ImportPath], b)
if pkgDecl, ok := b.(*resource.PkgDeclBind); ok {
p.bindNames[pkgDecl.QualifiedName()] = b
}
}
var usages []Expr
for _, pkg := range pkgs {
usages = append(usages, p.scanUsage(pkg)...)
}
return usages
}
type usageParser struct {
schema *schema.Parser
bindsPerPkg map[paths.Pkg][]resource.Bind
bindNames map[pkginfo.QualifiedName]resource.Bind
}
func (p *usageParser) scanUsage(pkg *pkginfo.Package) (usages []Expr) {
external, internal, files := p.bindsToScanFor(pkg)
haveExternal := len(external) > 0
haveInternal := len(internal) > 0
// Compute types to scan for.
var types []ast.Node
if haveExternal {
types = append(types, (*ast.SelectorExpr)(nil))
}
if haveInternal {
types = append(types, (*ast.Ident)(nil))
}
for _, f := range files {
inspector := f.ASTInspector()
names := f.Names()
inspector.WithStack(types, func(n ast.Node, push bool, stack []ast.Node) bool {
if !push {
return true
}
// If we're scanning for both *ast.SelectorExpr and *ast.Ident,
// we will first scan the *ast.SelectorExpr and then recurse and then scan the *ast.Ident.
// To avoid having to deal with this case, detect this case and ignore the *ast.Ident
// the second time around.
if haveExternal && haveInternal {
if id, ok := n.(*ast.Ident); ok {
if sel, ok := stack[len(stack)-2].(*ast.SelectorExpr); ok && sel.Sel == id {
return true
}
}
}
expr := n.(ast.Expr) // guaranteed since the types we scan for are all expressions
if qn, ok := names.ResolvePkgLevelRef(expr); ok {
if bind, ok := p.bindNames[qn]; ok {
// Make sure this is not the actual bind definition, to avoid reporting spurious usages.
if !p.isBind(pkg, expr, bind) {
if u := p.classifyExpr(f, bind, stack); u != nil {
usages = append(usages, u)
}
}
}
}
return true
})
}
return usages
}
// bindsToScanFor returns the binds to scan for in a given package,
// and which files to scan.
// The 'external' binds are those that are imported from other packages,
// and 'internal' binds are those that are defined in the same package.
func (p *usageParser) bindsToScanFor(pkg *pkginfo.Package) (external, internal []resource.Bind, files []*pkginfo.File) {
internal = p.bindsPerPkg[pkg.ImportPath]
if len(internal) > 0 {
// If we have any internal binds we need to scan all files,
// since we can't rely on the file-level imports to tell us
// which files to scan.
files = pkg.Files
}
for imp := range pkg.Imports {
external = append(external, p.bindsPerPkg[imp]...)
}
if len(external) > 0 && len(internal) == 0 {
// If we have external binds but no internal binds,
// figure out which files to parse precisely.
FileLoop:
for _, f := range pkg.Files {
for imp := range f.Imports {
if len(p.bindsPerPkg[imp]) > 0 {
files = append(files, f)
continue FileLoop
}
}
}
}
return
}
func (p *usageParser) isBind(pkg *pkginfo.Package, expr ast.Expr, bind resource.Bind) bool {
if pkg.ImportPath != bind.Package().ImportPath {
return false
}
// If the bind isn't a package decl it can't be this bind.
pkgDecl, ok := bind.(*resource.PkgDeclBind)
if !ok {
return false
}
switch x := expr.(type) {
case *ast.SelectorExpr:
return x.Sel == pkgDecl.BoundName
case *ast.Ident:
return x == pkgDecl.BoundName
}
return false
}
func (p *usageParser) classifyExpr(file *pkginfo.File, bind resource.Bind, stack []ast.Node) Expr {
idx := len(stack) - 1
if idx >= 1 {
if sel, ok := stack[idx-1].(*ast.SelectorExpr); ok {
// bind.SomeField or bind.SomeMethod()
// Check if this is a method call
if idx >= 2 {
if call, ok := stack[idx-2].(*ast.CallExpr); ok {
return &MethodCall{
File: file,
Bind: bind,
Call: call,
Method: sel.Sel.Name,
Args: call.Args,
}
}
}
// Otherwise it's a field access
return &FieldAccess{
File: file,
Bind: bind,
Expr: sel,
Field: sel.Sel.Name,
}
}
// Is this bind being referenced in a function call argument?
if call, ok := stack[idx-1].(*ast.CallExpr); ok {
if call.Fun == stack[idx] {
return &FuncCall{
File: file,
Bind: bind,
Call: call,
Args: call.Args,
}
}
// Find which argument this is.
if argIdx := slices.Index(call.Args, stack[idx].(ast.Expr)); argIdx >= 0 {
pkgFunc := option.CommaOk(file.Names().ResolvePkgLevelRef(call.Fun))
return &FuncArg{
File: file,
Bind: bind,
Call: call,
TypeArgs: p.parseTypeArgs(file, call),
PkgFunc: pkgFunc,
ArgIdx: argIdx,
}
}
}
}
// It's some other kind of usage. Find the largest enclosing expression.
enclosing := stack[idx].(ast.Expr) // guaranteed to be an expr by the caller.
for i := idx; i >= 0; i-- {
if expr, ok := stack[i].(ast.Expr); ok {
enclosing = expr
}
}
return &Other{
File: file,
Bind: bind,
Expr: enclosing,
BindRef: stack[idx].(ast.Expr), // guaranteed to be an expr by the caller.
}
}
func (p *usageParser) parseTypeArgs(file *pkginfo.File, call *ast.CallExpr) []schema.Type {
var args []ast.Expr
switch fun := call.Fun.(type) {
case *ast.IndexExpr:
args = []ast.Expr{fun.Index}
case *ast.IndexListExpr:
args = fun.Indices
default:
return nil
}
return fns.Map(args, func(arg ast.Expr) schema.Type {
return p.schema.ParseType(file, arg)
})
}