/
decl.go
280 lines (253 loc) · 5.74 KB
/
decl.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
package turbine
import (
"go/ast"
"go/token"
"sort"
"github.com/willfaught/forklift"
"golang.org/x/tools/go/packages"
)
type Decl struct {
Name Name
Type *Type
kind declKind
}
type declKind int
const (
declKindInvalid declKind = iota
declKindConst
declKindFunc
declKindType
declKindVar
)
func (d *Decl) IsConst() bool {
return d.kind == declKindConst
}
func (d *Decl) IsFunc() bool {
return d.kind == declKindFunc
}
func (d *Decl) IsType() bool {
return d.kind == declKindType
}
func (d *Decl) IsVar() bool {
return d.kind == declKindVar
}
type DeclList []*Decl
func (dl DeclList) Sorted() DeclList {
dl2 := make(DeclList, len(dl))
copy(dl2, dl)
sort.Slice(dl2, func(i, j int) bool {
return dl2[i].Name < dl2[j].Name
})
return dl2
}
type DeclGroup struct {
Names []Name
Type *Type
kind declKind
}
func (dg *DeclGroup) IsConst() bool {
return dg.kind == declKindConst
}
func (dg *DeclGroup) IsType() bool {
return dg.kind == declKindType
}
func (dg *DeclGroup) IsVar() bool {
return dg.kind == declKindVar
}
func (dg *DeclGroup) Decls() DeclList {
dl := make(DeclList, len(dg.Names))
for i, n := range dg.Names {
dl[i] = &Decl{Name: n, Type: dg.Type, kind: dg.kind}
}
return dl
}
type DeclGroupList []*DeclGroup
func (dgl DeclGroupList) Decls() DeclList {
var dl DeclList
for _, dg := range dgl {
dl = append(dl, dg.Decls()...)
}
return dl
}
type DeclBlock struct {
Groups DeclGroupList
}
func (db *DeclBlock) Decls() DeclList {
var dl DeclList
for _, dg := range db.Groups {
dl = append(dl, dg.Decls()...)
}
return dl
}
type DeclBlockList []*DeclBlock
func (dbl DeclBlockList) Decls() DeclList {
var ds []*Decl
for _, db := range dbl {
ds = append(ds, db.Decls()...)
}
return ds
}
func (dbl DeclBlockList) DeclGroups() DeclGroupList {
var dgl DeclGroupList
for _, db := range dbl {
dgl = append(dgl, db.Groups...)
}
return dgl
}
// LoadPackageDecl returns a [*Decl] for the declaration in the package.
// It returns nil if the declaration does not exist.
func LoadPackageDecl(p *packages.Package, name string) *Decl {
o := p.Types.Scope().Lookup(name)
if o == nil {
return nil
}
d := &Decl{Name: Name(name), Type: &Type{t: o.Type()}}
for _, f := range p.Syntax {
ast.Inspect(f, func(n ast.Node) bool {
if n == nil || d.kind != declKindInvalid {
return false
}
switch n := n.(type) {
case *ast.FuncDecl:
if n.Name.Name == name {
d.kind = declKindFunc
}
case *ast.GenDecl:
for _, s := range n.Specs {
switch s := s.(type) {
case *ast.TypeSpec:
if s.Name.Name == name {
d.kind = declKindType
}
case *ast.ValueSpec:
for _, i := range s.Names {
if i.Name == name {
switch n.Tok {
case token.CONST:
d.kind = declKindConst
case token.VAR:
d.kind = declKindVar
default:
panic(n.Tok)
}
break
}
}
}
}
}
return false
})
if d.kind != declKindInvalid {
break
}
}
if d.kind == declKindInvalid {
return nil
}
return d
}
// LoadDecl returns a [*Decl] for the declaration in the package.
// It returns nil if the declaration does not exist.
// It returns an error if the package cannot be loaded.
func LoadDecl(path, name string) (*Decl, error) {
p, err := forklift.LoadPackage(path)
if err != nil {
return nil, err
}
return LoadPackageDecl(p, name), nil
}
type Package struct {
All DeclList
Consts DeclBlockList
Funcs DeclList
Lookup map[string]*Decl
Types DeclBlockList
Vars DeclBlockList
}
// LoadPackage returns a [*Package] for the package.
// It returns an error if the package cannot be loaded.
func LoadPackage(path string) (*Package, error) {
fp, err := forklift.LoadPackage(path)
if err != nil {
return nil, err
}
tp := &Package{Lookup: map[string]*Decl{}}
for _, f := range fp.Syntax {
ast.Inspect(f, func(an ast.Node) bool {
if an == nil {
return false
}
switch an := an.(type) {
case *ast.FuncDecl:
td := &Decl{
Name: Name(an.Name.Name),
Type: &Type{t: fp.Types.Scope().Lookup(an.Name.Name).Type()},
kind: declKindFunc,
}
tp.All = append(tp.All, td)
tp.Lookup[string(td.Name)] = td
tp.Funcs = append(tp.Funcs, td)
case *ast.GenDecl:
var db DeclBlock
for _, s := range an.Specs {
switch s := s.(type) {
// TODO: ImportSpec
case *ast.TypeSpec:
dn := Name(s.Name.Name)
dt := &Type{t: fp.Types.Scope().Lookup(s.Name.Name).Type()}
d := &Decl{Name: dn, Type: dt, kind: declKindType}
dg := &DeclGroup{Names: []Name{dn}, Type: dt, kind: declKindType}
tp.All = append(tp.All, d)
tp.Lookup[string(d.Name)] = d
db.Groups = append(db.Groups, dg)
case *ast.ValueSpec:
var dg DeclGroup
var dt *Type
switch an.Tok {
case token.CONST:
dg.kind = declKindConst
case token.VAR:
dg.kind = declKindVar
default:
panic(an.Tok)
}
for _, name := range s.Names {
dn := Name(name.Name)
if dt == nil {
dt = &Type{t: fp.Types.Scope().Lookup(name.Name).Type()}
}
d := &Decl{Name: dn, Type: dt}
dg.Names = append(dg.Names, dn)
switch an.Tok {
case token.CONST:
d.kind = declKindConst
case token.VAR:
d.kind = declKindVar
default:
panic(an.Tok)
}
tp.All = append(tp.All, d)
tp.Lookup[string(d.Name)] = d
}
db.Groups = append(db.Groups, &dg)
default:
panic(s)
}
}
switch an.Tok {
case token.CONST:
tp.Consts = append(tp.Consts, &db)
case token.TYPE:
tp.Types = append(tp.Types, &db)
case token.VAR:
tp.Vars = append(tp.Vars, &db)
default:
panic(an.Tok)
}
}
return false
})
}
return tp, nil
}