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 func NewArray(elem Type, len int64) *Array { return &Array{len, elem} }
101
102
103 func (a *Array) Len() int64 { return a.len }
104
105
106 func (a *Array) Elem() Type { return a.elem }
107
108
109 type Slice struct {
110 elem Type
111 }
112
113
114 func NewSlice(elem Type) *Slice { return &Slice{elem} }
115
116
117 func (s *Slice) Elem() Type { return s.elem }
118
119
120 type Struct struct {
121 fields []*Var
122 tags []string
123 }
124
125
126
127
128
129 func NewStruct(fields []*Var, tags []string) *Struct {
130 var fset objset
131 for _, f := range fields {
132 if f.name != "_" && fset.insert(f) != nil {
133 panic("multiple fields with the same name")
134 }
135 }
136 if len(tags) > len(fields) {
137 panic("more tags than fields")
138 }
139 return &Struct{fields: fields, tags: tags}
140 }
141
142
143 func (s *Struct) NumFields() int { return len(s.fields) }
144
145
146 func (s *Struct) Field(i int) *Var { return s.fields[i] }
147
148
149 func (s *Struct) Tag(i int) string {
150 if i < len(s.tags) {
151 return s.tags[i]
152 }
153 return ""
154 }
155
156
157 type Pointer struct {
158 base Type
159 }
160
161
162 func NewPointer(elem Type) *Pointer { return &Pointer{base: elem} }
163
164
165 func (p *Pointer) Elem() Type { return p.base }
166
167
168
169
170 type Tuple struct {
171 vars []*Var
172 }
173
174
175 func NewTuple(x ...*Var) *Tuple {
176 if len(x) > 0 {
177 return &Tuple{x}
178 }
179 return nil
180 }
181
182
183 func (t *Tuple) Len() int {
184 if t != nil {
185 return len(t.vars)
186 }
187 return 0
188 }
189
190
191 func (t *Tuple) At(i int) *Var { return t.vars[i] }
192
193
194
195 type Signature struct {
196
197
198
199
200 scope *Scope
201 recv *Var
202 params *Tuple
203 results *Tuple
204 variadic bool
205 }
206
207
208
209
210
211 func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature {
212 if variadic {
213 n := params.Len()
214 if n == 0 {
215 panic("types.NewSignature: variadic function must have at least one parameter")
216 }
217 if _, ok := params.At(n - 1).typ.(*Slice); !ok {
218 panic("types.NewSignature: variadic parameter must be of unnamed slice type")
219 }
220 }
221 return &Signature{nil, recv, params, results, variadic}
222 }
223
224
225
226
227
228
229
230 func (s *Signature) Recv() *Var { return s.recv }
231
232
233 func (s *Signature) Params() *Tuple { return s.params }
234
235
236 func (s *Signature) Results() *Tuple { return s.results }
237
238
239 func (s *Signature) Variadic() bool { return s.variadic }
240
241
242 type Interface struct {
243 methods []*Func
244 embeddeds []*Named
245
246 allMethods []*Func
247 }
248
249
250 var emptyInterface = Interface{allMethods: markComplete}
251
252
253
254 var markComplete = make([]*Func, 0)
255
256
257
258 func NewInterface(methods []*Func, embeddeds []*Named) *Interface {
259 typ := new(Interface)
260
261 if len(methods) == 0 && len(embeddeds) == 0 {
262 return typ
263 }
264
265 var mset objset
266 for _, m := range methods {
267 if mset.insert(m) != nil {
268 panic("multiple methods with the same name")
269 }
270
271
272
273 m.typ.(*Signature).recv = NewVar(m.pos, m.pkg, "", typ)
274 }
275 sort.Sort(byUniqueMethodName(methods))
276
277 if embeddeds != nil {
278 sort.Sort(byUniqueTypeName(embeddeds))
279 }
280
281 typ.methods = methods
282 typ.embeddeds = embeddeds
283 return typ
284 }
285
286
287 func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
288
289
290
291 func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
292
293
294 func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
295
296
297
298 func (t *Interface) Embedded(i int) *Named { return t.embeddeds[i] }
299
300
301 func (t *Interface) NumMethods() int { return len(t.allMethods) }
302
303
304
305 func (t *Interface) Method(i int) *Func { return t.allMethods[i] }
306
307
308 func (t *Interface) Empty() bool { return len(t.allMethods) == 0 }
309
310
311
312
313
314 func (t *Interface) Complete() *Interface {
315 if t.allMethods != nil {
316 return t
317 }
318
319 var allMethods []*Func
320 if t.embeddeds == nil {
321 if t.methods == nil {
322 allMethods = make([]*Func, 0, 1)
323 } else {
324 allMethods = t.methods
325 }
326 } else {
327 allMethods = append(allMethods, t.methods...)
328 for _, et := range t.embeddeds {
329 it := et.Underlying().(*Interface)
330 it.Complete()
331 for _, tm := range it.allMethods {
332
333 newm := *tm
334 newmtyp := *tm.typ.(*Signature)
335 newm.typ = &newmtyp
336 newmtyp.recv = NewVar(newm.pos, newm.pkg, "", t)
337 allMethods = append(allMethods, &newm)
338 }
339 }
340 sort.Sort(byUniqueMethodName(allMethods))
341 }
342 t.allMethods = allMethods
343
344 return t
345 }
346
347
348 type Map struct {
349 key, elem Type
350 }
351
352
353 func NewMap(key, elem Type) *Map {
354 return &Map{key, elem}
355 }
356
357
358 func (m *Map) Key() Type { return m.key }
359
360
361 func (m *Map) Elem() Type { return m.elem }
362
363
364 type Chan struct {
365 dir ChanDir
366 elem Type
367 }
368
369
370 type ChanDir int
371
372
373 const (
374 SendRecv ChanDir = iota
375 SendOnly
376 RecvOnly
377 )
378
379
380 func NewChan(dir ChanDir, elem Type) *Chan {
381 return &Chan{dir, elem}
382 }
383
384
385 func (c *Chan) Dir() ChanDir { return c.dir }
386
387
388 func (c *Chan) Elem() Type { return c.elem }
389
390
391 type Named struct {
392 obj *TypeName
393 underlying Type
394 methods []*Func
395 }
396
397
398
399
400 func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named {
401 if _, ok := underlying.(*Named); ok {
402 panic("types.NewNamed: underlying type must not be *Named")
403 }
404 typ := &Named{obj: obj, underlying: underlying, methods: methods}
405 if obj.typ == nil {
406 obj.typ = typ
407 }
408 return typ
409 }
410
411
412 func (t *Named) Obj() *TypeName { return t.obj }
413
414
415 func (t *Named) NumMethods() int { return len(t.methods) }
416
417
418 func (t *Named) Method(i int) *Func { return t.methods[i] }
419
420
421 func (t *Named) SetUnderlying(underlying Type) {
422 if underlying == nil {
423 panic("types.Named.SetUnderlying: underlying type must not be nil")
424 }
425 if _, ok := underlying.(*Named); ok {
426 panic("types.Named.SetUnderlying: underlying type must not be *Named")
427 }
428 t.underlying = underlying
429 }
430
431
432 func (t *Named) AddMethod(m *Func) {
433 if i, _ := lookupMethod(t.methods, m.pkg, m.name); i < 0 {
434 t.methods = append(t.methods, m)
435 }
436 }
437
438
439
440 func (t *Basic) Underlying() Type { return t }
441 func (t *Array) Underlying() Type { return t }
442 func (t *Slice) Underlying() Type { return t }
443 func (t *Struct) Underlying() Type { return t }
444 func (t *Pointer) Underlying() Type { return t }
445 func (t *Tuple) Underlying() Type { return t }
446 func (t *Signature) Underlying() Type { return t }
447 func (t *Interface) Underlying() Type { return t }
448 func (t *Map) Underlying() Type { return t }
449 func (t *Chan) Underlying() Type { return t }
450 func (t *Named) Underlying() Type { return t.underlying }
451
452 func (t *Basic) String() string { return TypeString(t, nil) }
453 func (t *Array) String() string { return TypeString(t, nil) }
454 func (t *Slice) String() string { return TypeString(t, nil) }
455 func (t *Struct) String() string { return TypeString(t, nil) }
456 func (t *Pointer) String() string { return TypeString(t, nil) }
457 func (t *Tuple) String() string { return TypeString(t, nil) }
458 func (t *Signature) String() string { return TypeString(t, nil) }
459 func (t *Interface) String() string { return TypeString(t, nil) }
460 func (t *Map) String() string { return TypeString(t, nil) }
461 func (t *Chan) String() string { return TypeString(t, nil) }
462 func (t *Named) String() string { return TypeString(t, nil) }
463
View as plain text