/
analyzer.go
169 lines (131 loc) · 3.51 KB
/
analyzer.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
package analyzer
import (
"go/types"
"log"
"sort"
"github.com/jakebailey/plugingen/typesext"
"golang.org/x/tools/go/types/typeutil"
)
type Analyzer struct {
allowError bool
done map[string]*Interface
interfaces []*Interface
cache typeutil.MethodSetCache
}
func NewAnalyzer(allowError bool) *Analyzer {
return &Analyzer{
allowError: allowError,
done: map[string]*Interface{},
}
}
type Interface struct {
Typ types.Type
Methods []*Method
sortName string
}
type Method struct {
Name string
Params []*Var
Results []*Var
Variadic bool
}
type Var struct {
Name string
Typ types.Type
IFace *Interface
}
func (a *Analyzer) AnalyzeAll(ts []types.Type) []*Interface {
for _, t := range ts {
a.analyze(t)
}
sort.Slice(a.interfaces, func(i, j int) bool {
return a.interfaces[i].sortName < a.interfaces[j].sortName
})
ret := a.interfaces
a.interfaces = nil
for k := range a.done {
delete(a.done, k)
}
return ret
}
func (a *Analyzer) analyze(t types.Type) *Interface {
typeString := t.String()
if iface, ok := a.done[typeString]; ok {
return iface
}
iface := &Interface{
Typ: t,
sortName: typeString,
}
a.done[typeString] = iface
for _, sel := range typeutil.IntuitiveMethodSet(t, &a.cache) {
o := sel.Obj()
methodName := o.Name()
sig := o.Type().(*types.Signature)
params := tupleToSlice(sig.Params())
results := tupleToSlice(sig.Results())
variadic := sig.Variadic()
method := &Method{
Name: methodName,
Params: make([]*Var, 0, len(params)),
Results: make([]*Var, 0, len(results)),
Variadic: variadic,
}
if variadic {
last := params[len(params)-1].Type().Underlying().(*types.Slice).Elem()
if types.IsInterface(last) {
log.Fatalf("error: variadic interface arguments in %s.%s are unsupported", typeString, methodName)
}
}
for _, param := range params {
typ := param.Type()
v := &Var{
Name: param.Name(),
Typ: typ,
}
if typesext.IsPluggable(typ) {
v.IFace = a.analyze(typ)
} else {
if typesext.IsEmptyInterface(typ) {
log.Printf("warning: empty interface parameter in %s.%s may not be compatible", typeString, methodName)
} else if a.allowError && typesext.IsError(typ) {
log.Printf("warning: error interface parameter in %s.%s may not be compatible", typeString, methodName)
} else if typesext.IsPointerLike(typ) {
log.Printf("warning: pointer-like parameter in %s.%s, writes made in a plugin will not propogate", typeString, methodName)
}
}
method.Params = append(method.Params, v)
}
for _, result := range results {
typ := result.Type()
v := &Var{
Name: result.Name(),
Typ: typ,
}
if typesext.IsPluggable(typ) {
log.Fatalf("error: non-empty or error interface return in %s.%s is unsupported", typeString, methodName)
} else {
if typesext.IsEmptyInterface(typ) {
log.Printf("warning: empty interface result in %s.%s may not be compatible", typeString, methodName)
} else if a.allowError && typesext.IsError(typ) {
log.Printf("warning: error interface result in %s.%s may not be compatible", typeString, methodName)
}
}
method.Results = append(method.Results, v)
}
iface.Methods = append(iface.Methods, method)
}
a.interfaces = append(a.interfaces, iface)
return iface
}
func tupleToSlice(tuple *types.Tuple) []*types.Var {
listLen := tuple.Len()
if listLen == 0 {
return nil
}
list := make([]*types.Var, listLen)
for i := 0; i < listLen; i++ {
list[i] = tuple.At(i)
}
return list
}