forked from matthewmueller/joy
/
index.go
403 lines (350 loc) · 8.99 KB
/
index.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package index
import (
"errors"
"fmt"
"go/ast"
"go/types"
"sort"
"strings"
"github.com/matthewmueller/joy/internal/compiler/def"
"github.com/matthewmueller/joy/internal/compiler/util"
"golang.org/x/tools/go/loader"
)
type vdomSettings struct {
Pragma string
File def.Definition
}
// Index type
type Index struct {
program *loader.Program
defs map[string]def.Definition
aliases map[string]def.Definition
defpaths map[string][]def.Definition
imports map[string]map[string]string
vdom vdomSettings
}
// New index
func New(program *loader.Program) *Index {
return &Index{
program: program,
defs: map[string]def.Definition{},
defpaths: map[string][]def.Definition{},
aliases: map[string]def.Definition{},
imports: map[string]map[string]string{},
}
}
// AddDefinition adds a definition to the index
func (i *Index) AddDefinition(d def.Definition) {
// add by id
i.defs[d.ID()] = d
// group by path
p := d.Path()
if i.defpaths[p] == nil {
i.defpaths[p] = []def.Definition{}
}
i.defpaths[p] = append(i.defpaths[p], d)
}
// AddImport fn
func (i *Index) AddImport(parentPath string, alias, depPath string) {
if i.imports[parentPath] == nil {
i.imports[parentPath] = map[string]string{}
}
i.imports[parentPath][alias] = depPath
}
// GetImports fn
func (i *Index) GetImports(parentPath string) map[string]string {
deps := i.imports[parentPath]
if deps == nil {
return map[string]string{}
}
return deps
}
// All gets all definitions from the index
// TODO: remove
func (i *Index) All() map[string]def.Definition {
return i.defs
}
// Paths gets directory paths
func (i *Index) Paths() (paths []string) {
for path := range i.defpaths {
paths = append(paths, path)
}
return paths
}
// Get all definitions from the index
func (i *Index) Get(id string) def.Definition {
if i.aliases[id] != nil {
return i.aliases[id]
}
return i.defs[id]
}
// Link is like a symlink to a definition
func (i *Index) Link(alias string, d def.Definition) {
i.aliases[alias] = d
}
// Inits gets all the main functions
func (i *Index) Inits() (inits []def.Definition) {
var defids []string
for _, info := range i.program.InitialPackages() {
p := info.Pkg.Path()
def := i.defs[p+" init"]
if def == nil {
continue
}
defids = append(defids, def.ID())
}
sort.Strings(defids)
for _, id := range defids {
inits = append(inits, i.defs[id])
}
return inits
}
// Mains gets all the main functions
func (i *Index) Mains() (mains []def.Definition) {
var defids []string
for _, info := range i.program.InitialPackages() {
p := info.Pkg.Path()
def := i.defs[p+" main"]
if def == nil {
continue
}
defids = append(defids, def.ID())
}
sort.Strings(defids)
for _, id := range defids {
mains = append(mains, i.defs[id])
}
return mains
}
// DefinitionsByPath returns a unique, deterministic number of definitions
// given a particular path
func (i *Index) DefinitionsByPath(path string) (defs []def.Definition) {
defs = i.defpaths[path]
return sortByID(defs)
}
func sortByID(defs []def.Definition) (sorted []def.Definition) {
defmap := map[string]def.Definition{}
order := []string{}
for _, def := range defs {
id := def.ID()
defmap[def.ID()] = def
order = append(order, id)
}
sort.Strings(order)
for _, o := range order {
sorted = append(sorted, defmap[o])
}
return sorted
}
// Runtime gets a definition from the runtime
func (i *Index) Runtime(names ...string) (runtimes []def.Definition, err error) {
var defs []string
for _, def := range i.defs {
if !def.FromRuntime() {
continue
}
for _, name := range names {
if def.Name() == name {
defs = append(defs, def.ID())
}
}
}
// sort so we have consistent builds
sort.Strings(defs)
for _, def := range defs {
runtimes = append(runtimes, i.defs[def])
}
return runtimes, nil
}
// DefinitionOf fn
// Will return nil when ident is:
// - package name
// - basic type
// - local variable that points to a basic type
// - function parameters & results
// TODO: memoize
func (i *Index) DefinitionOf(packagePath string, n ast.Node) (def.Definition, error) {
switch t := n.(type) {
// case *ast.CallExpr:
// return i.callExpr(packagePath, t)
case *ast.SelectorExpr:
return i.selectorDefinition(packagePath, t)
case *ast.Ident:
return i.identDefinition(packagePath, t)
default:
id, e := util.GetIdentifier(t)
if e != nil {
return nil, e
} else if id == nil {
return nil, nil
}
return i.identDefinition(packagePath, id)
}
}
// func (i *Index) callExpr(packagePath string, n *ast.CallExpr) (def.Definition, error) {
// var caller def.Definition
// switch t := n.Fun.(type) {
// case *ast.SelectorExpr:
// c, e := i.selectorDefinition(packagePath, t)
// if e != nil {
// return nil, e
// }
// caller = c
// case *ast.Ident:
// c, e := i.identDefinition(packagePath, t)
// if e != nil {
// return nil, e
// }
// caller = c
// default:
// return nil, fmt.Errorf("index/callExpr: unexpected func %T", n.Fun)
// }
// if caller != nil {
// d := i.Get(caller.ID() + " " + )
// if d != nil {
// log.Infof()
// }
// log.Infof("KIND=%s ID=%s", caller.Kind(), caller.ID())
// }
// // switch t := caller.(type) {
// // case defs.Structer:
// // _ = t
// // case defs.Interfacer:
// // _ = t
// // default:
// // return nil, fmt.Errorf("index/callExpr: unexpected caller %T", caller)
// // }
// return caller, nil
// }
// Finds the definition based on the selector expression
// This is a bit subtle, but we basically do the following:
// 1. Check the rightmost identifier for a definition
// if we find one and it's not a struct, then return
// 2. If the rightmost identifier is nil or a struct,
// we find the rightmost parent of the selector
// expression. If we find that definition, we return
// it immediately
// 3. If we don't, we return the original rightmost identifier
// which is either a struct or nil at this point
func (i *Index) selectorDefinition(packagePath string, n *ast.SelectorExpr) (def.Definition, error) {
sel, e := i.identDefinition(packagePath, n.Sel)
if e != nil {
return nil, e
}
// prioritize selector definitions for functions
if sel != nil && sel.Kind() != "STRUCT" {
return sel, nil
}
id, e := util.GetIdentifier(n.X)
if e != nil {
return nil, e
}
d, e := i.identDefinition(packagePath, id)
if e != nil {
return nil, e
} else if d != nil {
return d, nil
}
return sel, nil
}
func (i *Index) identDefinition(packagePath string, n *ast.Ident) (def.Definition, error) {
info := i.program.Package(packagePath)
if info == nil {
return nil, errors.New("DefinitionOf: no package found")
}
// built-in or package name
obj := info.ObjectOf(n)
if obj == nil {
return nil, nil
}
// package is nil for types (e.g int)
// & built-in functions (e.g. append)
pkg := obj.Pkg()
if pkg == nil {
return nil, nil
}
// lookup using the type. Useful for:
// - local variables
// - methods (funcs w/ receivers)
ids, err := i.typeToDef(obj.Name(), obj.Type())
if err != nil {
return nil, err
}
// first try getting the definition from
// the package and object name
//
// TODO: d.Kind() != "FUNCTION"
// is a hack to make sure that we prioritize methods over functions
// e.g. t.test() > test()
// ... but prioritize values over methods
// e.g [document.Body].InnerHTML() > document.[Body.InnerHTML()]
d := i.Get(pkg.Path() + " " + obj.Name())
if d != nil && d.Kind() == "METHOD" {
return d, nil
}
if len(ids) > 0 {
id := strings.Join(ids, " ")
if d := i.Get(id); d != nil {
return d, nil
}
}
// if we didn't find the method,
// return the function which may
// or may not be nil
return d, nil
}
// TypeOf fn
func (i *Index) TypeOf(packagePath string, n ast.Node) (types.Type, error) {
info := i.program.Package(packagePath)
id, e := util.GetIdentifier(n)
if e != nil {
return nil, e
}
return info.TypeOf(id), nil
}
func (i *Index) typeToDef(name string, kind types.Type) (arr []string, err error) {
switch t := kind.(type) {
case *types.Basic:
return arr, nil
case *types.Interface:
if t.Empty() {
return arr, nil
}
return arr, fmt.Errorf("typeToDef: unhandled non-interface %s", t)
case *types.Slice:
return i.typeToDef(name, t.Elem())
case *types.Signature:
recv := t.Recv()
// TODO: clean this up
if recv != nil {
a, e := i.typeToDef(name, recv.Type())
if e != nil {
return arr, e
} else if len(a) == 2 {
return []string{a[0], name, a[1]}, nil
}
return arr, nil
}
case *types.Named:
obj := t.Obj()
if obj == nil {
return arr, fmt.Errorf("typeToDef: unhandled unnamed types.Named: %s", t.String())
}
// pkg will be nil for Error in error.Error()
if obj.Pkg() == nil {
return arr, nil
}
return append(arr, obj.Pkg().Path(), obj.Name()), nil
case *types.Pointer:
return i.typeToDef(name, t.Elem())
case *types.Map:
// TODO: is this always the case?
return arr, nil
case *types.Chan:
return i.typeToDef(name, t.Elem())
default:
return arr, fmt.Errorf("typeToDef: unhandled type %T", kind)
}
// log.Infof("ident %+v", i/d)
return arr, nil
}