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 var allMethods []*Func
356 allMethods = append(allMethods, t.methods...)
357 for _, et := range t.embeddeds {
358 it := et.Underlying().(*Interface)
359 it.Complete()
360 for _, tm := range it.allMethods {
361
362 newm := *tm
363 newmtyp := *tm.typ.(*Signature)
364 newm.typ = &newmtyp
365 newmtyp.recv = NewVar(newm.pos, newm.pkg, "", t)
366 allMethods = append(allMethods, &newm)
367 }
368 }
369 sort.Sort(byUniqueMethodName(allMethods))
370
371
372 if allMethods == nil {
373 allMethods = markComplete
374 }
375 t.allMethods = allMethods
376
377 return t
378 }
379
380
381 type Map struct {
382 key, elem Type
383 }
384
385
386 func NewMap(key, elem Type) *Map {
387 return &Map{key, elem}
388 }
389
390
391 func (m *Map) Key() Type { return m.key }
392
393
394 func (m *Map) Elem() Type { return m.elem }
395
396
397 type Chan struct {
398 dir ChanDir
399 elem Type
400 }
401
402
403 type ChanDir int
404
405
406 const (
407 SendRecv ChanDir = iota
408 SendOnly
409 RecvOnly
410 )
411
412
413 func NewChan(dir ChanDir, elem Type) *Chan {
414 return &Chan{dir, elem}
415 }
416
417
418 func (c *Chan) Dir() ChanDir { return c.dir }
419
420
421 func (c *Chan) Elem() Type { return c.elem }
422
423
424 type Named struct {
425 obj *TypeName
426 underlying Type
427 methods []*Func
428 }
429
430
431
432
433 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
434 if _, ok := underlying.(*Named); ok {
435 panic("types.NewNamed: underlying type must not be *Named")
436 }
437 typ := &Named{obj: obj, underlying: underlying, methods: methods}
438 if obj.typ == nil {
439 obj.typ = typ
440 }
441 return typ
442 }
443
444
445 func (t *Named) Obj() *TypeName { return t.obj }
446
447
448 func (t *Named) NumMethods() int { return len(t.methods) }
449
450
451 func (t *Named) Method(i int) *Func { return t.methods[i] }
452
453
454 func (t *Named) SetUnderlying(underlying Type) {
455 if underlying == nil {
456 panic("types.Named.SetUnderlying: underlying type must not be nil")
457 }
458 if _, ok := underlying.(*Named); ok {
459 panic("types.Named.SetUnderlying: underlying type must not be *Named")
460 }
461 t.underlying = underlying
462 }
463
464
465 func (t *Named) AddMethod(m *Func) {
466 if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
467 t.methods = append(t.methods, m)
468 }
469 }
470
471
472
473 func (b *Basic) Underlying() Type { return b }
474 func (a *Array) Underlying() Type { return a }
475 func (s *Slice) Underlying() Type { return s }
476 func (s *Struct) Underlying() Type { return s }
477 func (p *Pointer) Underlying() Type { return p }
478 func (t *Tuple) Underlying() Type { return t }
479 func (s *Signature) Underlying() Type { return s }
480 func (t *Interface) Underlying() Type { return t }
481 func (m *Map) Underlying() Type { return m }
482 func (c *Chan) Underlying() Type { return c }
483 func (t *Named) Underlying() Type { return t.underlying }
484
485 func (b *Basic) String() string { return TypeString(b, nil) }
486 func (a *Array) String() string { return TypeString(a, nil) }
487 func (s *Slice) String() string { return TypeString(s, nil) }
488 func (s *Struct) String() string { return TypeString(s, nil) }
489 func (p *Pointer) String() string { return TypeString(p, nil) }
490 func (t *Tuple) String() string { return TypeString(t, nil) }
491 func (s *Signature) String() string { return TypeString(s, nil) }
492 func (t *Interface) String() string { return TypeString(t, nil) }
493 func (m *Map) String() string { return TypeString(m, nil) }
494 func (c *Chan) String() string { return TypeString(c, nil) }
495 func (t *Named) String() string { return TypeString(t, nil) }
496
View as plain text