-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtslist.go
320 lines (274 loc) · 7.02 KB
/
tslist.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
package tslist
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"reflect"
"strings"
"sync"
"github.com/samber/lo"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
const doc = "tslist shows the list about types satisfied by type sets"
const (
ANY = "any"
TILDA = "~"
)
type Visitor struct {
nest int
pass *analysis.Pass
interfaceName string
methodNames []string
methodResults []Method
mp map[string]types.Type
mu sync.Mutex
typeResults map[int][]string
}
type VisitorResult struct {
Pos token.Pos
Name string
TypeSets []TypeValue
Methods []Method
}
type Method struct {
Name string
Args []TypeValue
Outputs []TypeValue
}
type TypeValue struct {
Name string
Type types.Type
}
type Result struct {
Results []VisitorResult
}
// Analyzer is retruning type sets and method lists
var Analyzer = &analysis.Analyzer{
Name: "tslist",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
ResultType: reflect.TypeOf(new(Result)),
}
func run(pass *analysis.Pass) (interface{}, error) {
var result Result
for _, f := range pass.Files {
for _, dec := range f.Decls {
dec, _ := dec.(*ast.GenDecl)
if dec == nil || dec.Tok != token.TYPE {
continue
}
for _, spec := range dec.Specs {
spec, _ := spec.(*ast.TypeSpec)
if spec == nil || spec.Type == nil {
continue
}
interfaceType, _ := spec.Type.(*ast.InterfaceType)
if interfaceType == nil {
continue
}
res := InterfaceVisitor(spec.Name.Name, interfaceType, pass)
result.Results = append(result.Results, res)
}
}
}
for _, res := range result.Results {
ts := typeSetPrint(res)
methodList := methodListPrint(res)
format := fmt.Sprintf("\n%s\ntype set: %v\nmethod list:\n", res.Name, ts)
for _, method := range methodList {
format += fmt.Sprintf("%s\n", method)
}
pass.Reportf(res.Pos, format)
}
return &result, nil
}
func InterfaceVisitor(name string, interfaceType *ast.InterfaceType, pass *analysis.Pass) VisitorResult {
visit := Visitor{pass: pass, interfaceName: name, typeResults: make(map[int][]string), mp: make(map[string]types.Type)}
visit.interfaceVisitor(interfaceType)
typeSet := visit.parseTypeSet()
visit.parseMethodList()
return VisitorResult{Pos: interfaceType.Pos(), Name: name, TypeSets: typeSet, Methods: visit.methodResults}
}
func (v *Visitor) parseTypeSet() []TypeValue {
typeSet := make(map[string]int)
// union
for _, results := range v.typeResults {
if lo.Contains(results, ANY) {
typeSet[ANY]++
continue
}
results = lo.Uniq(results)
for _, result := range results {
if lo.Contains(results, fmt.Sprintf("~%s", result)) {
continue
}
typeSet[result]++
}
}
res := make([]TypeValue, 0, len(typeSet))
if len(v.typeResults) == 0 {
res = append(res, TypeValue{Name: ANY})
return res
}
// intersection
if _, ok := typeSet[ANY]; ok {
if len(typeSet) == 1 {
if typ, ok := v.mp[ANY]; ok {
res = append(res, TypeValue{ANY, typ})
return res
}
res = append(res, TypeValue{Name: ANY})
return res
}
v.nest -= typeSet[ANY]
typeSet[ANY] = -1
}
for typ := range typeSet {
if strings.HasPrefix(typ, TILDA) {
defaultType := strings.Trim(typ, TILDA)
if _, ok := typeSet[defaultType]; ok {
typeSet[defaultType]++
}
}
}
for typeName, num := range typeSet {
if num == v.nest {
if typ, ok := v.mp[typeName]; ok {
res = append(res, TypeValue{typeName, typ})
}
}
}
return res
}
func (v *Visitor) parseMethodList() {
for i, name := range v.methodNames {
v.methodResults[i].Name = name
}
}
func (v *Visitor) interfaceVisitor(expr *ast.InterfaceType) {
if expr.Methods == nil {
return
}
if expr.Methods.List == nil {
v.nest++
v.typeResults[v.nest] = append(v.typeResults[v.nest], ANY)
}
for _, field := range expr.Methods.List {
v.nest++
for _, name := range field.Names {
v.methodNames = append(v.methodNames, name.Name)
}
v.exprVisitor(field.Type)
}
}
func (v *Visitor) exprVisitor(expr ast.Expr) {
switch expr := expr.(type) {
case *ast.BinaryExpr:
v.exprVisitor(expr.X)
v.exprVisitor(expr.Y)
case *ast.Ident:
v.identVisitor(expr)
case *ast.UnaryExpr:
v.unaryVisitor(expr)
case *ast.FuncType:
v.funcTypeVisitor(expr)
case *ast.StarExpr:
v.starExprVisitor(expr)
case *ast.ArrayType:
v.arrayTypeVisitor(expr)
}
}
func (v *Visitor) identVisitor(expr *ast.Ident) {
if expr.Obj == nil || expr.Obj.Decl == nil {
typ := v.pass.TypesInfo.TypeOf(expr)
v.addType(typ.String(), typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], typ.String())
} else {
dec, _ := expr.Obj.Decl.(*ast.TypeSpec)
if dec == nil || dec.Type == nil {
return
}
switch dec := dec.Type.(type) {
case *ast.InterfaceType:
res := InterfaceVisitor(v.interfaceName, dec, v.pass)
for _, ts := range res.TypeSets {
v.typeResults[v.nest] = append(v.typeResults[v.nest], ts.Name)
v.addType(ts.Name, ts.Type)
}
case *ast.Ident:
typ := v.pass.TypesInfo.TypeOf(dec)
v.addType(expr.Name, typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], expr.Name)
case *ast.StructType:
typ := v.pass.TypesInfo.TypeOf(dec)
name := fmt.Sprintf("%s.%s", v.pass.Pkg.Name(), expr.Name)
v.addType(name, typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], name)
}
}
}
func (v *Visitor) unaryVisitor(expr *ast.UnaryExpr) {
if expr.Op != token.TILDE {
return
}
exprX, _ := expr.X.(*ast.Ident)
if exprX == nil {
return
}
typ := v.pass.TypesInfo.TypeOf(exprX)
name := fmt.Sprintf("~%s", typ.String())
v.addType(name, typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], name)
}
func (v *Visitor) funcTypeVisitor(expr *ast.FuncType) {
v.nest--
var method Method
if expr.Params != nil && expr.Params.List != nil {
values := v.params(expr.Params.List)
method.Args = values
}
if expr.Results != nil && expr.Results.List != nil {
values := v.params(expr.Results.List)
method.Outputs = values
}
v.methodResults = append(v.methodResults, method)
}
func (v *Visitor) starExprVisitor(expr *ast.StarExpr) {
typ := v.pass.TypesInfo.TypeOf(expr.X)
name := fmt.Sprintf("*%s", typ.String())
v.addType(name, typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], name)
}
func (v *Visitor) arrayTypeVisitor(expr *ast.ArrayType) {
typ := v.pass.TypesInfo.TypeOf(expr.Elt)
name := fmt.Sprintf("[]%s", typ.String())
v.addType(name, typ)
v.typeResults[v.nest] = append(v.typeResults[v.nest], name)
}
func (v *Visitor) params(fields []*ast.Field) []TypeValue {
values := make([]TypeValue, 0, len(fields))
for _, field := range fields {
if field.Names == nil {
typ := v.pass.TypesInfo.TypeOf(field.Type)
values = append(values, TypeValue{"", typ})
continue
}
for _, fieldName := range field.Names {
typ := v.pass.TypesInfo.TypeOf(fieldName)
values = append(values, TypeValue{fieldName.Name, typ})
}
}
return values
}
func (v *Visitor) addType(name string, typ types.Type) {
v.mu.Lock()
if _, ok := v.mp[name]; !ok {
v.mp[name] = typ
}
v.mu.Unlock()
}