Source file src/go/types/type.go
1
2
3
4
5 package types
6
7 import "sort"
8
9
10
11 type Type interface {
12
13 Underlying() Type
14
15
16 String() string
17 }
18
19
20 type BasicKind int
21
22 const (
23 Invalid BasicKind = iota
24
25
26 Bool
27 Int
28 Int8
29 Int16
30 Int32
31 Int64
32 Uint
33 Uint8
34 Uint16
35 Uint32
36 Uint64
37 Uintptr
38 Float32
39 Float64
40 Complex64
41 Complex128
42 String
43 UnsafePointer
44
45
46 UntypedBool
47 UntypedInt
48 UntypedRune
49 UntypedFloat
50 UntypedComplex
51 UntypedString
52 UntypedNil
53
54
55 Byte = Uint8
56 Rune = Int32
57 )
58
59
60 type BasicInfo int
61
62
63 const (
64 IsBoolean BasicInfo = 1 << iota
65 IsInteger
66 IsUnsigned
67 IsFloat
68 IsComplex
69 IsString
70 IsUntyped
71
72 IsOrdered = IsInteger | IsFloat | IsString
73 IsNumeric = IsInteger | IsFloat | IsComplex
74 IsConstType = IsBoolean | IsNumeric | IsString
75 )
76
77
78 type Basic struct {
79 kind BasicKind
80 info BasicInfo
81 name string
82 }
83
84
85 func (b *Basic) Kind() BasicKind { return b.kind }
86
87
88 func (b *Basic) Info() BasicInfo { return b.info }
89
90
91 func (b *Basic) Name() string { return b.name }
92
93
94 type Array struct {
95 len int64
96 elem Type
97 }
98
99
100
101 func NewArray(elem Type, len int64) *Array { return &Array{len, elem} }
102
103
104
105 func (a *Array) Len() int64 { return a.len }
106
107
108 func (a *Array) Elem() Type { return a.elem }
109
110
111 type Slice struct {
112 elem Type
113 }
114
115
116 func NewSlice(elem Type) *Slice { return &Slice{elem} }
117
118
119 func (s *Slice) Elem() Type { return s.elem }
120
121
122 type Struct struct {
123 fields []*Var
124 tags []string
125 }
126
127
128
129
130
131 func NewStruct(fields []*Var, tags []string) *Struct {
132 var fset objset
133 for _, f := range fields {
134 if f.name != "_" && fset.insert(f) != nil {
135 panic("multiple fields with the same name")
136 }
137 }
138 if len(tags) > len(fields) {
139 panic("more tags than fields")
140 }
141 return &Struct{fields: fields, tags: tags}
142 }
143
144
145 func (s *Struct) NumFields() int { return len(s.fields) }
146
147
148 func (s *Struct) Field(i int) *Var { return s.fields[i] }
149
150
151 func (s *Struct) Tag(i int) string {
152 if i < len(s.tags) {
153 return s.tags[i]
154 }
155 return ""
156 }
157
158
159 type Pointer struct {
160 base Type
161 }
162
163
164 func NewPointer(elem Type) *Pointer { return &Pointer{base: elem} }
165
166
167 func (p *Pointer) Elem() Type { return p.base }
168
169
170
171
172 type Tuple struct {
173 vars []*Var
174 }
175
176
177 func NewTuple(x ...*Var) *Tuple {
178 if len(x) > 0 {
179 return &Tuple{x}
180 }
181 return nil
182 }
183
184
185 func (t *Tuple) Len() int {
186 if t != nil {
187 return len(t.vars)
188 }
189 return 0
190 }
191
192
193 func (t *Tuple) At(i int) *Var { return t.vars[i] }
194
195
196
197 type Signature struct {
198
199
200
201
202 scope *Scope
203 recv *Var
204 params *Tuple
205 results *Tuple
206 variadic bool
207 }
208
209
210
211
212
213 func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
214 if variadic {
215 n := params.Len()
216 if n == 0 {
217 panic("types.NewSignature: variadic function must have at least one parameter")
218 }
219 if _, ok := params.At(n - 1).typ.(*Slice); !ok {
220 panic("types.NewSignature: variadic parameter must be of unnamed slice type")
221 }
222 }
223 return &Signature{nil, recv, params, results, variadic}
224 }
225
226
227
228
229
230
231
232 func (s *Signature) Recv() *Var { return s.recv }
233
234
235 func (s *Signature) Params() *Tuple { return s.params }
236
237
238 func (s *Signature) Results() *Tuple { return s.results }
239
240
241 func (s *Signature) Variadic() bool { return s.variadic }
242
243
244 type Interface struct {
245 methods []*Func
246 embeddeds []Type
247
248 allMethods []*Func
249 }
250
251
252 var emptyInterface = Interface{allMethods: markComplete}
253
254
255
256 var markComplete = make([]*Func, 0)
257
258
259
260
261
262
263
264
265
266 func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
267 tnames := make([]Type, len(embeddeds))
268 for i, t := range embeddeds {
269 tnames[i] = t
270 }
271 return NewInterfaceType(methods, tnames)
272 }
273
274
275
276
277
278
279
280 func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
281 typ := new(Interface)
282
283 if len(methods) == 0 && len(embeddeds) == 0 {
284 return typ
285 }
286
287 var mset objset
288 for _, m := range methods {
289 if mset.insert(m) != nil {
290 panic("multiple methods with the same name")
291 }
292
293 if sig := m.typ.(*Signature); sig.recv == nil {
294 sig.recv = NewVar(m.pos, m.pkg, "", typ)
295 }
296 }
297 sort.Sort(byUniqueMethodName(methods))
298
299 if len(embeddeds) > 0 {
300
301
302
303
304 for _, t := range embeddeds {
305 if _, ok := t.(*Named); !ok && !IsInterface(t) {
306 panic("embedded type is not an interface")
307 }
308 }
309 sort.Stable(byUniqueTypeName(embeddeds))
310 }
311
312 typ.methods = methods
313 typ.embeddeds = embeddeds
314 return typ
315 }
316
317
318 func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
319
320
321
322 func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
323
324
325 func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
326
327
328
329
330
331 func (t *Interface) Embedded(i int) *Named { tname, _ := t.embeddeds[i].(*Named); return tname }
332
333
334 func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
335
336
337 func (t *Interface) NumMethods() int { return len(t.allMethods) }
338
339
340
341 func (t *Interface) Method(i int) *Func { return t.allMethods[i] }
342
343
344 func (t *Interface) Empty() bool { return len(t.allMethods) == 0 }
345
346
347
348
349
350 func (t *Interface) Complete() *Interface {
351 if t.allMethods != nil {
352 return t
353 }
354
355
356 var allMethods []*Func
357 allMethods = append(allMethods, t.methods...)
358 for _, et := range t.embeddeds {
359 it := et.Underlying().(*Interface)
360 it.Complete()
361
362 allMethods = append(allMethods, it.allMethods...)
363 }
364 sort.Sort(byUniqueMethodName(allMethods))
365
366
367 if allMethods == nil {
368 allMethods = markComplete
369 }
370 t.allMethods = allMethods
371
372 return t
373 }
374
375
376 type Map struct {
377 key, elem Type
378 }
379
380
381 func NewMap(key, elem Type) *Map {
382 return &Map{key, elem}
383 }
384
385
386 func (m *Map) Key() Type { return m.key }
387
388
389 func (m *Map) Elem() Type { return m.elem }
390
391
392 type Chan struct {
393 dir ChanDir
394 elem Type
395 }
396
397
398 type ChanDir int
399
400
401 const (
402 SendRecv ChanDir = iota
403 SendOnly
404 RecvOnly
405 )
406
407
408 func NewChan(dir ChanDir, elem Type) *Chan {
409 return &Chan{dir, elem}
410 }
411
412
413 func (c *Chan) Dir() ChanDir { return c.dir }
414
415
416 func (c *Chan) Elem() Type { return c.elem }
417
418
419 type Named struct {
420 obj *TypeName
421 underlying Type
422 methods []*Func
423 }
424
425
426
427
428 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
429 if _, ok := underlying.(*Named); ok {
430 panic("types.NewNamed: underlying type must not be *Named")
431 }
432 typ := &Named{obj: obj, underlying: underlying, methods: methods}
433 if obj.typ == nil {
434 obj.typ = typ
435 }
436 return typ
437 }
438
439
440 func (t *Named) Obj() *TypeName { return t.obj }
441
442
443 func (t *Named) NumMethods() int { return len(t.methods) }
444
445
446 func (t *Named) Method(i int) *Func { return t.methods[i] }
447
448
449 func (t *Named) SetUnderlying(underlying Type) {
450 if underlying == nil {
451 panic("types.Named.SetUnderlying: underlying type must not be nil")
452 }
453 if _, ok := underlying.(*Named); ok {
454 panic("types.Named.SetUnderlying: underlying type must not be *Named")
455 }
456 t.underlying = underlying
457 }
458
459
460 func (t *Named) AddMethod(m *Func) {
461 if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
462 t.methods = append(t.methods, m)
463 }
464 }
465
466
467
468 func (b *Basic) Underlying() Type { return b }
469 func (a *Array) Underlying() Type { return a }
470 func (s *Slice) Underlying() Type { return s }
471 func (s *Struct) Underlying() Type { return s }
472 func (p *Pointer) Underlying() Type { return p }
473 func (t *Tuple) Underlying() Type { return t }
474 func (s *Signature) Underlying() Type { return s }
475 func (t *Interface) Underlying() Type { return t }
476 func (m *Map) Underlying() Type { return m }
477 func (c *Chan) Underlying() Type { return c }
478 func (t *Named) Underlying() Type { return t.underlying }
479
480 func (b *Basic) String() string { return TypeString(b, nil) }
481 func (a *Array) String() string { return TypeString(a, nil) }
482 func (s *Slice) String() string { return TypeString(s, nil) }
483 func (s *Struct) String() string { return TypeString(s, nil) }
484 func (p *Pointer) String() string { return TypeString(p, nil) }
485 func (t *Tuple) String() string { return TypeString(t, nil) }
486 func (s *Signature) String() string { return TypeString(s, nil) }
487 func (t *Interface) String() string { return TypeString(t, nil) }
488 func (m *Map) String() string { return TypeString(m, nil) }
489 func (c *Chan) String() string { return TypeString(c, nil) }
490 func (t *Named) String() string { return TypeString(t, nil) }
491
View as plain text