Debian Codegraph @

cmd/godex/writetype.go

raw symbols SCIP
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // This file implements writing of types. The functionality is lifted
6 // directly from go/types, but now contains various modifications for
7 // nicer output.
8 //
9 // TODO(gri) back-port once we have a fixed interface and once the
10 // go/types API is not frozen anymore for the 1.3 release; and remove
11 // this implementation if possible.
12
13 package main
14
15 import (
16 "go/types"
17 "slices"
18 )
19
20 func (p *printer) writeType(this *types.Package, typ types.Type) {
21 p.writeTypeInternal(this, typ, make([]types.Type, 8))
22 }
23
24 // From go/types - leave for now to ease back-porting this code.
25 const GcCompatibilityMode = false
26
27 func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited []types.Type) {
28 // Theoretically, this is a quadratic lookup algorithm, but in
29 // practice deeply nested composite types with unnamed component
30 // types are uncommon. This code is likely more efficient than
31 // using a map.
32 if slices.Contains(visited, typ) {
33 p.printf("○%T", typ) // cycle to typ
34 return
35 }
36 visited = append(visited, typ)
37
38 switch t := typ.(type) {
39 case nil:
40 p.print("<nil>")
41
42 case *types.Basic:
43 if t.Kind() == types.UnsafePointer {
44 p.print("unsafe.")
45 }
46 if GcCompatibilityMode {
47 // forget the alias names
48 switch t.Kind() {
49 case types.Byte:
50 t = types.Typ[types.Uint8]
51 case types.Rune:
52 t = types.Typ[types.Int32]
53 }
54 }
55 p.print(t.Name())
56
57 case *types.Array:
58 p.printf("[%d]", t.Len())
59 p.writeTypeInternal(this, t.Elem(), visited)
60
61 case *types.Slice:
62 p.print("[]")
63 p.writeTypeInternal(this, t.Elem(), visited)
64
65 case *types.Struct:
66 n := t.NumFields()
67 if n == 0 {
68 p.print("struct{}")
69 return
70 }
71
72 p.print("struct {\n")
73 p.indent++
74 for i := range n {
75 f := t.Field(i)
76 if !f.Anonymous() {
77 p.printf("%s ", f.Name())
78 }
79 p.writeTypeInternal(this, f.Type(), visited)
80 if tag := t.Tag(i); tag != "" {
81 p.printf(" %q", tag)
82 }
83 p.print("\n")
84 }
85 p.indent--
86 p.print("}")
87
88 case *types.Pointer:
89 p.print("*")
90 p.writeTypeInternal(this, t.Elem(), visited)
91
92 case *types.Tuple:
93 p.writeTuple(this, t, false, visited)
94
95 case *types.Signature:
96 p.print("func")
97 p.writeSignatureInternal(this, t, visited)
98
99 case *types.Interface:
100 // We write the source-level methods and embedded types rather
101 // than the actual method set since resolved method signatures
102 // may have non-printable cycles if parameters have anonymous
103 // interface types that (directly or indirectly) embed the
104 // current interface. For instance, consider the result type
105 // of m:
106 //
107 // type T interface{
108 // m() interface{ T }
109 // }
110 //
111 n := t.NumMethods()
112 if n == 0 {
113 p.print("any")
114 return
115 }
116
117 p.print("interface {\n")
118 p.indent++
119 if GcCompatibilityMode {
120 // print flattened interface
121 // (useful to compare against gc-generated interfaces)
122 for i := range n {
123 m := t.Method(i)
124 p.print(m.Name())
125 p.writeSignatureInternal(this, m.Type().(*types.Signature), visited)
126 p.print("\n")
127 }
128 } else {
129 // print explicit interface methods and embedded types
130 for i, n := 0, t.NumExplicitMethods(); i < n; i++ {
131 m := t.ExplicitMethod(i)
132 p.print(m.Name())
133 p.writeSignatureInternal(this, m.Type().(*types.Signature), visited)
134 p.print("\n")
135 }
136 for i, n := 0, t.NumEmbeddeds(); i < n; i++ {
137 typ := t.EmbeddedType(i)
138 p.writeTypeInternal(this, typ, visited)
139 p.print("\n")
140 }
141 }
142 p.indent--
143 p.print("}")
144
145 case *types.Map:
146 p.print("map[")
147 p.writeTypeInternal(this, t.Key(), visited)
148 p.print("]")
149 p.writeTypeInternal(this, t.Elem(), visited)
150
151 case *types.Chan:
152 var s string
153 var parens bool
154 switch t.Dir() {
155 case types.SendRecv:
156 s = "chan "
157 // chan (<-chan T) requires parentheses
158 if c, _ := t.Elem().(*types.Chan); c != nil && c.Dir() == types.RecvOnly {
159 parens = true
160 }
161 case types.SendOnly:
162 s = "chan<- "
163 case types.RecvOnly:
164 s = "<-chan "
165 default:
166 panic("unreachable")
167 }
168 p.print(s)
169 if parens {
170 p.print("(")
171 }
172 p.writeTypeInternal(this, t.Elem(), visited)
173 if parens {
174 p.print(")")
175 }
176
177 case *types.Alias:
178 // TODO(adonovan): display something aliasy.
179 p.writeTypeInternal(this, types.Unalias(t), visited)
180
181 case *types.Named:
182 s := "<Named w/o object>"
183 if obj := t.Obj(); obj != nil {
184 if pkg := obj.Pkg(); pkg != nil {
185 if pkg != this {
186 p.print(pkg.Path())
187 p.print(".")
188 }
189 // TODO(gri): function-local named types should be displayed
190 // differently from named types at package level to avoid
191 // ambiguity.
192 }
193 s = obj.Name()
194 }
195 p.print(s)
196
197 default:
198 // For externally defined implementations of Type.
199 p.print(t.String())
200 }
201 }
202
203 func (p *printer) writeTuple(this *types.Package, tup *types.Tuple, variadic bool, visited []types.Type) {
204 p.print("(")
205 for i, n := 0, tup.Len(); i < n; i++ {
206 if i > 0 {
207 p.print(", ")
208 }
209 v := tup.At(i)
210 if name := v.Name(); name != "" {
211 p.print(name)
212 p.print(" ")
213 }
214 typ := v.Type()
215 if variadic && i == n-1 {
216 p.print("...")
217 typ = typ.(*types.Slice).Elem()
218 }
219 p.writeTypeInternal(this, typ, visited)
220 }
221 p.print(")")
222 }
223
224 func (p *printer) writeSignature(this *types.Package, sig *types.Signature) {
225 p.writeSignatureInternal(this, sig, make([]types.Type, 8))
226 }
227
228 func (p *printer) writeSignatureInternal(this *types.Package, sig *types.Signature, visited []types.Type) {
229 p.writeTuple(this, sig.Params(), sig.Variadic(), visited)
230
231 res := sig.Results()
232 n := res.Len()
233 if n == 0 {
234 // no result
235 return
236 }
237
238 p.print(" ")
239 if n == 1 && res.At(0).Name() == "" {
240 // single unnamed result
241 p.writeTypeInternal(this, res.At(0).Type(), visited)
242 return
243 }
244
245 // multiple or named result(s)
246 p.writeTuple(this, res, false, visited)
247 }