1
2
3
4
5
6
7 package types2
8
9 import (
10 "bytes"
11 "fmt"
12 "slices"
13 "strconv"
14 "strings"
15 "unicode/utf8"
16 )
17
18
19
20
21
22
23
24
25
26
27
28 type Qualifier func(*Package) string
29
30
31
32 func RelativeTo(pkg *Package) Qualifier {
33 if pkg == nil {
34 return nil
35 }
36 return func(other *Package) string {
37 if pkg == other {
38 return ""
39 }
40 return other.Path()
41 }
42 }
43
44
45
46
47 func TypeString(typ Type, qf Qualifier) string {
48 var buf bytes.Buffer
49 WriteType(&buf, typ, qf)
50 return buf.String()
51 }
52
53
54
55
56 func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier) {
57 newTypeWriter(buf, qf).typ(typ)
58 }
59
60
61
62
63 func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier) {
64 newTypeWriter(buf, qf).signature(sig)
65 }
66
67 type typeWriter struct {
68 buf *bytes.Buffer
69 seen map[Type]bool
70 qf Qualifier
71 ctxt *Context
72 tparams *TypeParamList
73 paramNames bool
74 tpSubscripts bool
75 pkgInfo bool
76 }
77
78 func newTypeWriter(buf *bytes.Buffer, qf Qualifier) *typeWriter {
79 return &typeWriter{buf, make(map[Type]bool), qf, nil, nil, true, false, false}
80 }
81
82 func newTypeHasher(buf *bytes.Buffer, ctxt *Context) *typeWriter {
83 assert(ctxt != nil)
84 return &typeWriter{buf, make(map[Type]bool), nil, ctxt, nil, false, false, false}
85 }
86
87 func (w *typeWriter) byte(b byte) {
88 if w.ctxt != nil {
89 if b == ' ' {
90 b = '#'
91 }
92 w.buf.WriteByte(b)
93 return
94 }
95 w.buf.WriteByte(b)
96 if b == ',' || b == ';' {
97 w.buf.WriteByte(' ')
98 }
99 }
100
101 func (w *typeWriter) string(s string) {
102 w.buf.WriteString(s)
103 }
104
105 func (w *typeWriter) error(msg string) {
106 if w.ctxt != nil {
107 panic(msg)
108 }
109 w.buf.WriteString("<" + msg + ">")
110 }
111
112 func (w *typeWriter) typ(typ Type) {
113 if w.seen[typ] {
114 w.error("cycle to " + goTypeName(typ))
115 return
116 }
117 w.seen[typ] = true
118 defer delete(w.seen, typ)
119
120 switch t := typ.(type) {
121 case nil:
122 w.error("nil")
123
124 case *Basic:
125
126
127 if isExported(t.name) {
128 if obj, _ := Unsafe.scope.Lookup(t.name).(*TypeName); obj != nil {
129 w.typeName(obj)
130 break
131 }
132 }
133 w.string(t.name)
134
135 case *Array:
136 w.byte('[')
137 w.string(strconv.FormatInt(t.len, 10))
138 w.byte(']')
139 w.typ(t.elem)
140
141 case *Slice:
142 w.string("[]")
143 w.typ(t.elem)
144
145 case *Struct:
146 w.string("struct{")
147 for i, f := range t.fields {
148 if i > 0 {
149 w.byte(';')
150 }
151
152
153
154 pkgAnnotate := false
155 if w.qf == nil && w.pkgInfo && !isExported(f.name) {
156
157 pkgAnnotate = true
158 w.pkgInfo = false
159 }
160
161
162
163
164 if !f.embedded {
165 w.string(f.name)
166 w.byte(' ')
167 }
168 w.typ(f.typ)
169 if pkgAnnotate {
170 w.string(" /* package ")
171 w.string(f.pkg.Path())
172 w.string(" */ ")
173 }
174 if tag := t.Tag(i); tag != "" {
175 w.byte(' ')
176
177
178
179 w.string(strconv.Quote(tag))
180 }
181 }
182 w.byte('}')
183
184 case *Pointer:
185 w.byte('*')
186 w.typ(t.base)
187
188 case *Tuple:
189 w.tuple(t, false)
190
191 case *Signature:
192 w.string("func")
193 w.signature(t)
194
195 case *Union:
196
197
198 if t.Len() == 0 {
199 w.error("empty union")
200 break
201 }
202 for i, t := range t.terms {
203 if i > 0 {
204 w.string(termSep)
205 }
206 if t.tilde {
207 w.byte('~')
208 }
209 w.typ(t.typ)
210 }
211
212 case *Interface:
213 if w.ctxt == nil {
214 if t == universeAnyAlias.Type().Underlying() {
215
216
217
218
219 w.string("any")
220 break
221 }
222 if t == asNamed(universeComparable.Type()).underlying {
223 w.string("interface{comparable}")
224 break
225 }
226 }
227 if t.implicit {
228 if len(t.methods) == 0 && len(t.embeddeds) == 1 {
229 w.typ(t.embeddeds[0])
230 break
231 }
232
233
234 w.string("/* implicit */ ")
235 }
236 w.string("interface{")
237 first := true
238 if w.ctxt != nil {
239 w.typeSet(t.typeSet())
240 } else {
241 for _, m := range t.methods {
242 if !first {
243 w.byte(';')
244 }
245 first = false
246 w.string(m.name)
247 w.signature(m.typ.(*Signature))
248 }
249 for _, typ := range t.embeddeds {
250 if !first {
251 w.byte(';')
252 }
253 first = false
254 w.typ(typ)
255 }
256 }
257 w.byte('}')
258
259 case *Map:
260 w.string("map[")
261 w.typ(t.key)
262 w.byte(']')
263 w.typ(t.elem)
264
265 case *Chan:
266 var s string
267 var parens bool
268 switch t.dir {
269 case SendRecv:
270 s = "chan "
271
272 if c, _ := t.elem.(*Chan); c != nil && c.dir == RecvOnly {
273 parens = true
274 }
275 case SendOnly:
276 s = "chan<- "
277 case RecvOnly:
278 s = "<-chan "
279 default:
280 w.error("unknown channel direction")
281 }
282 w.string(s)
283 if parens {
284 w.byte('(')
285 }
286 w.typ(t.elem)
287 if parens {
288 w.byte(')')
289 }
290
291 case *Named:
292
293
294 if w.ctxt != nil {
295 w.string(strconv.Itoa(w.ctxt.getID(t)))
296 }
297 w.typeName(t.obj)
298 if t.inst != nil {
299
300 w.typeList(t.inst.targs.list())
301 } else if w.ctxt == nil && t.TypeParams().Len() != 0 {
302
303 w.tParamList(t.TypeParams().list())
304 }
305
306 case *TypeParam:
307 if t.obj == nil {
308 w.error("unnamed type parameter")
309 break
310 }
311 if i := slices.Index(w.tparams.list(), t); i >= 0 {
312
313
314
315 w.string(fmt.Sprintf("$%d", i))
316 } else {
317 w.string(t.obj.name)
318 if w.tpSubscripts || w.ctxt != nil {
319 w.string(subscript(t.id))
320 }
321
322
323
324
325 if w.ctxt == nil && Universe.Lookup(t.obj.name) != nil {
326 if isTypes2 {
327 w.string(fmt.Sprintf(" /* with %s declared at %v */", t.obj.name, t.obj.Pos()))
328 } else {
329
330
331 w.string("/* type parameter */")
332 }
333 }
334 }
335
336 case *Alias:
337 w.typeName(t.obj)
338 if list := t.targs.list(); len(list) != 0 {
339
340 w.typeList(list)
341 } else if w.ctxt == nil && t.TypeParams().Len() != 0 {
342
343 w.tParamList(t.TypeParams().list())
344 }
345 if w.ctxt != nil {
346
347 w.typ(Unalias(t.obj.typ))
348 }
349
350 default:
351
352
353 w.string(t.String())
354 }
355 }
356
357
358 func (w *typeWriter) typeSet(s *_TypeSet) {
359 assert(w.ctxt != nil)
360 first := true
361 for _, m := range s.methods {
362 if !first {
363 w.byte(';')
364 }
365 first = false
366 w.string(m.name)
367 w.signature(m.typ.(*Signature))
368 }
369 switch {
370 case s.terms.isAll():
371
372 case s.terms.isEmpty():
373 w.string(s.terms.String())
374 default:
375 var termHashes []string
376 for _, term := range s.terms {
377
378 var buf bytes.Buffer
379 if term.tilde {
380 buf.WriteByte('~')
381 }
382 newTypeHasher(&buf, w.ctxt).typ(term.typ)
383 termHashes = append(termHashes, buf.String())
384 }
385 slices.Sort(termHashes)
386 if !first {
387 w.byte(';')
388 }
389 w.string(strings.Join(termHashes, "|"))
390 }
391 }
392
393 func (w *typeWriter) typeList(list []Type) {
394 w.byte('[')
395 for i, typ := range list {
396 if i > 0 {
397 w.byte(',')
398 }
399 w.typ(typ)
400 }
401 w.byte(']')
402 }
403
404 func (w *typeWriter) tParamList(list []*TypeParam) {
405 w.byte('[')
406 var prev Type
407 for i, tpar := range list {
408
409
410
411 if tpar == nil {
412 w.error("nil type parameter")
413 continue
414 }
415 if i > 0 {
416 if tpar.bound != prev {
417
418 w.byte(' ')
419 w.typ(prev)
420 }
421 w.byte(',')
422 }
423 prev = tpar.bound
424 w.typ(tpar)
425 }
426 if prev != nil {
427 w.byte(' ')
428 w.typ(prev)
429 }
430 w.byte(']')
431 }
432
433 func (w *typeWriter) typeName(obj *TypeName) {
434 w.string(packagePrefix(obj.pkg, w.qf))
435 w.string(obj.name)
436 }
437
438 func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
439 w.byte('(')
440 if tup != nil {
441 for i, v := range tup.vars {
442 if i > 0 {
443 w.byte(',')
444 }
445
446 if w.ctxt == nil && v.name != "" && w.paramNames {
447 w.string(v.name)
448 w.byte(' ')
449 }
450 typ := v.typ
451 if variadic && i == len(tup.vars)-1 {
452 if s, ok := typ.(*Slice); ok {
453 w.string("...")
454 typ = s.elem
455 } else {
456
457
458 if t, _ := under(typ).(*Basic); t == nil || t.kind != String {
459 w.error("expected string type")
460 continue
461 }
462 w.typ(typ)
463 w.string("...")
464 continue
465 }
466 }
467 w.typ(typ)
468 }
469 }
470 w.byte(')')
471 }
472
473 func (w *typeWriter) signature(sig *Signature) {
474 if sig.TypeParams().Len() != 0 {
475 if w.ctxt != nil {
476 assert(w.tparams == nil)
477 w.tparams = sig.TypeParams()
478 defer func() {
479 w.tparams = nil
480 }()
481 }
482 w.tParamList(sig.TypeParams().list())
483 }
484
485 w.tuple(sig.params, sig.variadic)
486
487 n := sig.results.Len()
488 if n == 0 {
489
490 return
491 }
492
493 w.byte(' ')
494 if n == 1 && (w.ctxt != nil || sig.results.vars[0].name == "") {
495
496 w.typ(sig.results.vars[0].typ)
497 return
498 }
499
500
501 w.tuple(sig.results, false)
502 }
503
504
505 func subscript(x uint64) string {
506 const w = len("₀")
507 var buf [32 * w]byte
508 i := len(buf)
509 for {
510 i -= w
511 utf8.EncodeRune(buf[i:], '₀'+rune(x%10))
512 x /= 10
513 if x == 0 {
514 break
515 }
516 }
517 return string(buf[i:])
518 }
519
View as plain text