-
Notifications
You must be signed in to change notification settings - Fork 27
/
types_util.go
304 lines (276 loc) · 7.87 KB
/
types_util.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
// Copyright 2017 Felix Lange <fjl@twurst.com>.
// Use of this source code is governed by the MIT license,
// which can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"go/types"
"io"
"sort"
"strconv"
)
// walkNamedTypes runs the callback for all named types contained in the given type.
func walkNamedTypes(typ types.Type, callback func(*types.Named)) {
switch typ := typ.(type) {
case *types.Basic:
case *types.Chan:
walkNamedTypes(typ.Elem(), callback)
case *types.Map:
walkNamedTypes(typ.Key(), callback)
walkNamedTypes(typ.Elem(), callback)
case *types.Named:
callback(typ)
case *types.Pointer:
walkNamedTypes(typ.Elem(), callback)
case *types.Slice:
walkNamedTypes(typ.Elem(), callback)
case *types.Array:
walkNamedTypes(typ.Elem(), callback)
case *types.Struct:
for i := 0; i < typ.NumFields(); i++ {
walkNamedTypes(typ.Field(i).Type(), callback)
}
case *types.Interface:
if typ.NumMethods() > 0 {
panic("BUG: can't walk non-empty interface")
}
default:
panic(fmt.Errorf("BUG: can't walk %T", typ))
}
}
func lookupStructType(scope *types.Scope, name string) (*types.Named, error) {
typ, err := lookupType(scope, name)
if err != nil {
return nil, err
}
_, ok := typ.Underlying().(*types.Struct)
if !ok {
return nil, errors.New("not a struct type")
}
return typ, nil
}
func lookupType(scope *types.Scope, name string) (*types.Named, error) {
obj := scope.Lookup(name)
if obj == nil {
return nil, errors.New("no such identifier")
}
typ, ok := obj.(*types.TypeName)
if !ok {
return nil, errors.New("not a type")
}
return typ.Type().(*types.Named), nil
}
func isPointer(typ types.Type) bool {
_, ok := typ.(*types.Pointer)
return ok
}
func underlyingArray(typ types.Type) *types.Array {
return underlying[*types.Array](typ)
}
func underlyingSlice(typ types.Type) *types.Slice {
return underlying[*types.Slice](typ)
}
func underlyingMap(typ types.Type) *types.Map {
return underlying[*types.Map](typ)
}
func underlying[T types.Type](typ types.Type) T {
for {
switch typ.(type) {
case *types.Named:
typ = typ.Underlying()
case T:
return typ.(T)
default:
var zero T
return zero
}
}
}
func ensureNilCheckable(typ types.Type) types.Type {
orig := typ
named := false
for {
switch typ.(type) {
case *types.Named:
typ = typ.Underlying()
named = true
case *types.Slice, *types.Map:
if named {
// Named slices, maps, etc. are special because they can have a custom
// decoder function that prevents the JSON null value. Wrap them with a
// pointer to allow null always so required/optional works as expected.
return types.NewPointer(orig)
}
return orig
case *types.Pointer, *types.Interface:
return orig
default:
return types.NewPointer(orig)
}
}
}
// checkConvertible determines whether values of type from can be converted to type to. It
// returns nil if convertible and a descriptive error otherwise.
// See package documentation for this definition of 'convertible'.
func checkConvertible(from, to types.Type) error {
if types.ConvertibleTo(from, to) {
return nil
}
// Slices.
sfrom := underlyingSlice(from)
sto := underlyingSlice(to)
if sfrom != nil && sto != nil {
if !types.ConvertibleTo(sfrom.Elem(), sto.Elem()) {
return fmt.Errorf("slice element type %s is not convertible to %s", sfrom.Elem(), sto.Elem())
}
return nil
}
// Arrays.
afrom := underlyingArray(from)
ato := underlyingArray(to)
if afrom != nil && sto != nil {
// Array -> slice
if !types.ConvertibleTo(afrom.Elem(), sto.Elem()) {
return fmt.Errorf("array element type %s is not convertible to %s", afrom.Elem(), sto.Elem())
}
return nil
}
if sfrom != nil && ato != nil {
// Slice -> array
if !types.ConvertibleTo(sfrom.Elem(), ato.Elem()) {
return fmt.Errorf("slice element type %s is not convertible to array element %s", sfrom.Elem(), ato.Elem())
}
return nil
}
// Maps.
mfrom := underlyingMap(from)
mto := underlyingMap(to)
if mfrom != nil && mto != nil {
if !types.ConvertibleTo(mfrom.Key(), mto.Key()) {
return fmt.Errorf("map key type %s is not convertible to %s", mfrom.Key(), mto.Key())
}
if !types.ConvertibleTo(mfrom.Elem(), mto.Elem()) {
return fmt.Errorf("map element type %s is not convertible to %s", mfrom.Elem(), mto.Elem())
}
return nil
}
return fmt.Errorf("type %s is not convertible to %s", from, to)
}
// fileScope tracks imports and other names at file scope.
type fileScope struct {
imports []*types.Package
importsByName map[string]*types.Package
importNames map[string]string
otherNames map[string]bool // non-package identifiers
pkg *types.Package
imp types.Importer
}
func newFileScope(imp types.Importer, pkg *types.Package) *fileScope {
return &fileScope{otherNames: make(map[string]bool), pkg: pkg, imp: imp}
}
func (s *fileScope) writeImportDecl(w io.Writer) {
fmt.Fprintln(w, "import (")
for _, pkg := range s.imports {
if s.importNames[pkg.Path()] != pkg.Name() {
fmt.Fprintf(w, "\t%s %q\n", s.importNames[pkg.Path()], pkg.Path())
} else {
fmt.Fprintf(w, "\t%q\n", pkg.Path())
}
}
fmt.Fprintln(w, ")")
}
// addImport loads a package and adds it to the import set.
func (s *fileScope) addImport(path string) {
pkg, err := s.imp.Import(path)
if err != nil {
panic(fmt.Errorf("can't import %q: %v", path, err))
}
s.insertImport(pkg)
s.rebuildImports()
}
// addReferences marks all names referenced by typ as used.
func (s *fileScope) addReferences(typ types.Type) {
walkNamedTypes(typ, func(nt *types.Named) {
pkg := nt.Obj().Pkg()
if pkg == s.pkg {
s.otherNames[nt.Obj().Name()] = true
} else if pkg != nil {
s.insertImport(nt.Obj().Pkg())
}
})
s.rebuildImports()
}
// insertImport adds pkg to the list of known imports.
// This method should not be used directly because it doesn't
// rebuild the import name cache.
func (s *fileScope) insertImport(pkg *types.Package) {
i := sort.Search(len(s.imports), func(i int) bool {
return s.imports[i].Path() >= pkg.Path()
})
if i < len(s.imports) && s.imports[i] == pkg {
return
}
s.imports = append(s.imports[:i], append([]*types.Package{pkg}, s.imports[i:]...)...)
}
// rebuildImports caches the names of imported packages.
func (s *fileScope) rebuildImports() {
s.importNames = make(map[string]string)
s.importsByName = make(map[string]*types.Package)
for _, pkg := range s.imports {
s.maybeRenameImport(pkg)
}
}
func (s *fileScope) maybeRenameImport(pkg *types.Package) {
name := pkg.Name()
for i := 0; s.isNameTaken(name); i++ {
name = pkg.Name()
if i > 0 {
name += strconv.Itoa(i - 1)
}
}
s.importNames[pkg.Path()] = name
s.importsByName[name] = pkg
}
// isNameTaken reports whether the given name is used by an import or other identifier.
func (s *fileScope) isNameTaken(name string) bool {
return s.importsByName[name] != nil || s.otherNames[name] || types.Universe.Lookup(name) != nil
}
// qualify is a types.Qualifier that prepends the (possibly renamed) package name of
// imported types to a type name.
func (s *fileScope) qualify(pkg *types.Package) string {
if pkg == s.pkg {
return ""
}
return s.packageName(pkg.Path())
}
func (s *fileScope) packageName(path string) string {
name, ok := s.importNames[path]
if !ok {
panic("BUG: missing package " + path)
}
return name
}
// funcScope tracks used identifiers in a function. It can create new identifiers that do
// not clash with the parent scope.
type funcScope struct {
used map[string]bool
parent *fileScope
}
func newFuncScope(parent *fileScope) *funcScope {
return &funcScope{make(map[string]bool), parent}
}
// newIdent creates a new identifier that doesn't clash with any name
// in the scope or its parent file scope.
func (s *funcScope) newIdent(base string) string {
for i := 0; ; i++ {
name := base
if i > 0 {
name += strconv.Itoa(i - 1)
}
if !s.parent.isNameTaken(name) && !s.used[name] {
s.used[name] = true
return name
}
}
}