Debian Codegraph @

src/cmd/compile/internal/types/type.go

raw
1 // Copyright 2017 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 package types
6
7 import (
8 "cmd/compile/internal/base"
9 "cmd/internal/objabi"
10 "cmd/internal/src"
11 "fmt"
12 "go/constant"
13 "internal/types/errors"
14 "sync"
15 )
16
17 // Object represents an ir.Node, but without needing to import cmd/compile/internal/ir,
18 // which would cause an import cycle. The uses in other packages must type assert
19 // values of type Object to ir.Node or a more specific type.
20 type Object interface {
21 Pos() src.XPos
22 Sym() *Sym
23 Type() *Type
24 }
25
26 //go:generate stringer -type Kind -trimprefix T type.go
27
28 // Kind describes a kind of type.
29 type Kind uint8
30
31 const (
32 Txxx Kind = iota
33
34 TINT8
35 TUINT8
36 TINT16
37 TUINT16
38 TINT32
39 TUINT32
40 TINT64
41 TUINT64
42 TINT
43 TUINT
44 TUINTPTR
45
46 TCOMPLEX64
47 TCOMPLEX128
48
49 TFLOAT32
50 TFLOAT64
51
52 TBOOL
53
54 TPTR
55 TFUNC
56 TSLICE
57 TARRAY
58 TSTRUCT
59 TCHAN
60 TMAP
61 TINTER
62 TFORW
63 TANY
64 TSTRING
65 TUNSAFEPTR
66
67 // pseudo-types for literals
68 TIDEAL // untyped numeric constants
69 TNIL
70 TBLANK
71
72 // pseudo-types used temporarily only during frame layout (CalcSize())
73 TFUNCARGS
74 TCHANARGS
75
76 // SSA backend types
77 TSSA // internal types used by SSA backend (flags, memory, etc.)
78 TTUPLE // a pair of types, used by SSA backend
79 TRESULTS // multiple types; the result of calling a function or method, with a memory at the end.
80
81 NTYPE
82 )
83
84 // ChanDir is whether a channel can send, receive, or both.
85 type ChanDir uint8
86
87 func (c ChanDir) CanRecv() bool { return c&Crecv != 0 }
88 func (c ChanDir) CanSend() bool { return c&Csend != 0 }
89
90 const (
91 // types of channel
92 // must match ../../../../reflect/type.go:/ChanDir
93 Crecv ChanDir = 1 << 0
94 Csend ChanDir = 1 << 1
95 Cboth ChanDir = Crecv | Csend
96 )
97
98 // Types stores pointers to predeclared named types.
99 //
100 // It also stores pointers to several special types:
101 // - Types[TANY] is the placeholder "any" type recognized by SubstArgTypes.
102 // - Types[TBLANK] represents the blank variable's type.
103 // - Types[TINTER] is the canonical "interface{}" type.
104 // - Types[TNIL] represents the predeclared "nil" value's type.
105 // - Types[TUNSAFEPTR] is package unsafe's Pointer type.
106 var Types [NTYPE]*Type
107
108 var (
109 // Predeclared alias types. These are actually created as distinct
110 // defined types for better error messages, but are then specially
111 // treated as identical to their respective underlying types.
112 AnyType *Type
113 ByteType *Type
114 RuneType *Type
115
116 // Predeclared error interface type.
117 ErrorType *Type
118 // Predeclared comparable interface type.
119 ComparableType *Type
120
121 // Types to represent untyped string and boolean constants.
122 UntypedString = newType(TSTRING)
123 UntypedBool = newType(TBOOL)
124
125 // Types to represent untyped numeric constants.
126 UntypedInt = newType(TIDEAL)
127 UntypedRune = newType(TIDEAL)
128 UntypedFloat = newType(TIDEAL)
129 UntypedComplex = newType(TIDEAL)
130 )
131
132 // UntypedTypes maps from a constant.Kind to its untyped Type
133 // representation.
134 var UntypedTypes = [...]*Type{
135 constant.Bool: UntypedBool,
136 constant.String: UntypedString,
137 constant.Int: UntypedInt,
138 constant.Float: UntypedFloat,
139 constant.Complex: UntypedComplex,
140 }
141
142 // DefaultKinds maps from a constant.Kind to its default Kind.
143 var DefaultKinds = [...]Kind{
144 constant.Bool: TBOOL,
145 constant.String: TSTRING,
146 constant.Int: TINT,
147 constant.Float: TFLOAT64,
148 constant.Complex: TCOMPLEX128,
149 }
150
151 // A Type represents a Go type.
152 //
153 // There may be multiple unnamed types with identical structure. However, there must
154 // be a unique Type object for each unique named (defined) type. After noding, a
155 // package-level type can be looked up by building its unique symbol sym (sym =
156 // package.Lookup(name)) and checking sym.Def. If sym.Def is non-nil, the type
157 // already exists at package scope and is available at sym.Def.(*ir.Name).Type().
158 // Local types (which may have the same name as a package-level type) are
159 // distinguished by their vargen, which is embedded in their symbol name.
160 type Type struct {
161 // extra contains extra etype-specific fields.
162 // As an optimization, those etype-specific structs which contain exactly
163 // one pointer-shaped field are stored as values rather than pointers when possible.
164 //
165 // TMAP: *Map
166 // TFORW: *Forward
167 // TFUNC: *Func
168 // TSTRUCT: *Struct
169 // TINTER: *Interface
170 // TFUNCARGS: FuncArgs
171 // TCHANARGS: ChanArgs
172 // TCHAN: *Chan
173 // TPTR: Ptr
174 // TARRAY: *Array
175 // TSLICE: Slice
176 // TSSA: string
177 extra any
178
179 // width is the width of this Type in bytes.
180 width int64 // valid if Align > 0
181
182 // list of base methods (excluding embedding)
183 methods fields
184 // list of all methods (including embedding)
185 allMethods fields
186
187 // canonical OTYPE node for a named type (should be an ir.Name node with same sym)
188 obj Object
189 // the underlying type (type literal or predeclared type) for a defined type
190 underlying *Type
191
192 // Cache of composite types, with this type being the element type.
193 cache struct {
194 ptr *Type // *T, or nil
195 slice *Type // []T, or nil
196 }
197
198 kind Kind // kind of type
199 align uint8 // the required alignment of this type, in bytes (0 means Width and Align have not yet been computed)
200
201 intRegs, floatRegs uint8 // registers needed for ABIInternal
202
203 flags bitset8
204 alg AlgKind // valid if Align > 0
205 isSIMDTag, isSIMD bool // tag is the marker type, isSIMD means has marker type
206
207 // size of prefix of object that contains all pointers. valid if Align > 0.
208 // Note that for pointers, this is always PtrSize even if the element type
209 // is NotInHeap. See size.go:PtrDataSize for details.
210 ptrBytes int64
211 }
212
213 // Registers returns the number of integer and floating-point
214 // registers required to represent a parameter of this type under the
215 // ABIInternal calling conventions.
216 //
217 // If t must be passed by memory, Registers returns (math.MaxUint8,
218 // math.MaxUint8).
219 func (t *Type) Registers() (uint8, uint8) {
220 CalcSize(t)
221 return t.intRegs, t.floatRegs
222 }
223
224 func (*Type) CanBeAnSSAAux() {}
225
226 const (
227 typeNotInHeap = 1 << iota // type cannot be heap allocated
228 typeNoalg // suppress hash and eq algorithm generation
229 typeDeferwidth // width computation has been deferred and type is on deferredTypeStack
230 typeRecur
231 typeIsShape // represents a set of closely related types, for generics
232 typeHasShape // there is a shape somewhere in the type
233 // typeIsFullyInstantiated reports whether a type is fully instantiated generic type; i.e.
234 // an instantiated generic type where all type arguments are non-generic or fully instantiated generic types.
235 typeIsFullyInstantiated
236 )
237
238 func (t *Type) NotInHeap() bool { return t.flags&typeNotInHeap != 0 }
239 func (t *Type) Noalg() bool { return t.flags&typeNoalg != 0 }
240 func (t *Type) Deferwidth() bool { return t.flags&typeDeferwidth != 0 }
241 func (t *Type) Recur() bool { return t.flags&typeRecur != 0 }
242 func (t *Type) IsShape() bool { return t.flags&typeIsShape != 0 }
243 func (t *Type) HasShape() bool { return t.flags&typeHasShape != 0 }
244 func (t *Type) IsFullyInstantiated() bool { return t.flags&typeIsFullyInstantiated != 0 }
245
246 func (t *Type) SetNotInHeap(b bool) { t.flags.set(typeNotInHeap, b) }
247 func (t *Type) SetNoalg(b bool) { t.flags.set(typeNoalg, b) }
248 func (t *Type) SetDeferwidth(b bool) { t.flags.set(typeDeferwidth, b) }
249 func (t *Type) SetRecur(b bool) { t.flags.set(typeRecur, b) }
250 func (t *Type) SetIsFullyInstantiated(b bool) { t.flags.set(typeIsFullyInstantiated, b) }
251
252 // Should always do SetHasShape(true) when doing SetIsShape(true).
253 func (t *Type) SetIsShape(b bool) { t.flags.set(typeIsShape, b) }
254 func (t *Type) SetHasShape(b bool) { t.flags.set(typeHasShape, b) }
255
256 // Kind returns the kind of type t.
257 func (t *Type) Kind() Kind { return t.kind }
258
259 // Sym returns the name of type t.
260 func (t *Type) Sym() *Sym {
261 if t.obj != nil {
262 return t.obj.Sym()
263 }
264 return nil
265 }
266
267 // Underlying returns the underlying type of type t.
268 func (t *Type) Underlying() *Type { return t.underlying }
269
270 // Pos returns a position associated with t, if any.
271 // This should only be used for diagnostics.
272 func (t *Type) Pos() src.XPos {
273 if t.obj != nil {
274 return t.obj.Pos()
275 }
276 return src.NoXPos
277 }
278
279 // Map contains Type fields specific to maps.
280 type Map struct {
281 Key *Type // Key type
282 Elem *Type // Val (elem) type
283
284 Group *Type // internal struct type representing a slot group
285 }
286
287 // MapType returns t's extra map-specific fields.
288 func (t *Type) MapType() *Map {
289 t.wantEtype(TMAP)
290 return t.extra.(*Map)
291 }
292
293 // Forward contains Type fields specific to forward types.
294 type Forward struct {
295 Copyto []*Type // where to copy the eventual value to
296 Embedlineno src.XPos // first use of this type as an embedded type
297 }
298
299 // forwardType returns t's extra forward-type-specific fields.
300 func (t *Type) forwardType() *Forward {
301 t.wantEtype(TFORW)
302 return t.extra.(*Forward)
303 }
304
305 // Func contains Type fields specific to func types.
306 type Func struct {
307 allParams []*Field // slice of all parameters, in receiver/params/results order
308
309 startParams int // index of the start of the (regular) parameters section
310 startResults int // index of the start of the results section
311
312 resultsTuple *Type // struct-like type representing multi-value results
313
314 // Argwid is the total width of the function receiver, params, and results.
315 // It gets calculated via a temporary TFUNCARGS type.
316 // Note that TFUNC's Width is Widthptr.
317 Argwid int64
318 }
319
320 func (ft *Func) recvs() []*Field { return ft.allParams[:ft.startParams] }
321 func (ft *Func) params() []*Field { return ft.allParams[ft.startParams:ft.startResults] }
322 func (ft *Func) results() []*Field { return ft.allParams[ft.startResults:] }
323 func (ft *Func) recvParams() []*Field { return ft.allParams[:ft.startResults] }
324 func (ft *Func) paramsResults() []*Field { return ft.allParams[ft.startParams:] }
325
326 // funcType returns t's extra func-specific fields.
327 func (t *Type) funcType() *Func {
328 t.wantEtype(TFUNC)
329 return t.extra.(*Func)
330 }
331
332 // Struct contains Type fields specific to struct types.
333 type Struct struct {
334 fields fields
335
336 // Maps have three associated internal structs (see struct MapType).
337 // Map links such structs back to their map type.
338 Map *Type
339
340 ParamTuple bool // whether this struct is actually a tuple of signature parameters
341 }
342
343 // StructType returns t's extra struct-specific fields.
344 func (t *Type) StructType() *Struct {
345 t.wantEtype(TSTRUCT)
346 return t.extra.(*Struct)
347 }
348
349 // Interface contains Type fields specific to interface types.
350 type Interface struct {
351 }
352
353 // Ptr contains Type fields specific to pointer types.
354 type Ptr struct {
355 Elem *Type // element type
356 }
357
358 // ChanArgs contains Type fields specific to TCHANARGS types.
359 type ChanArgs struct {
360 T *Type // reference to a chan type whose elements need a width check
361 }
362
363 // FuncArgs contains Type fields specific to TFUNCARGS types.
364 type FuncArgs struct {
365 T *Type // reference to a func type whose elements need a width check
366 }
367
368 // Chan contains Type fields specific to channel types.
369 type Chan struct {
370 Elem *Type // element type
371 Dir ChanDir // channel direction
372 }
373
374 // chanType returns t's extra channel-specific fields.
375 func (t *Type) chanType() *Chan {
376 t.wantEtype(TCHAN)
377 return t.extra.(*Chan)
378 }
379
380 type Tuple struct {
381 first *Type
382 second *Type
383 // Any tuple with a memory type must put that memory type second.
384 }
385
386 // Results are the output from calls that will be late-expanded.
387 type Results struct {
388 Types []*Type // Last element is memory output from call.
389 }
390
391 // Array contains Type fields specific to array types.
392 type Array struct {
393 Elem *Type // element type
394 Bound int64 // number of elements; <0 if unknown yet
395 }
396
397 // Slice contains Type fields specific to slice types.
398 type Slice struct {
399 Elem *Type // element type
400 }
401
402 // A Field is a (Sym, Type) pairing along with some other information, and,
403 // depending on the context, is used to represent:
404 // - a field in a struct
405 // - a method in an interface or associated with a named type
406 // - a function parameter
407 type Field struct {
408 flags bitset8
409
410 Embedded uint8 // embedded field
411
412 Pos src.XPos
413
414 // Name of field/method/parameter. Can be nil for interface fields embedded
415 // in interfaces and unnamed parameters.
416 Sym *Sym
417 Type *Type // field type
418 Note string // literal string annotation
419
420 // For fields that represent function parameters, Nname points to the
421 // associated ONAME Node. For fields that represent methods, Nname points to
422 // the function name node.
423 Nname Object
424
425 // Offset in bytes of this field or method within its enclosing struct
426 // or interface Type. For parameters, this is BADWIDTH.
427 Offset int64
428 }
429
430 const (
431 fieldIsDDD = 1 << iota // field is ... argument
432 fieldNointerface
433 )
434
435 func (f *Field) IsDDD() bool { return f.flags&fieldIsDDD != 0 }
436 func (f *Field) Nointerface() bool { return f.flags&fieldNointerface != 0 }
437
438 func (f *Field) SetIsDDD(b bool) { f.flags.set(fieldIsDDD, b) }
439 func (f *Field) SetNointerface(b bool) { f.flags.set(fieldNointerface, b) }
440
441 // End returns the offset of the first byte immediately after this field.
442 func (f *Field) End() int64 {
443 return f.Offset + f.Type.width
444 }
445
446 // IsMethod reports whether f represents a method rather than a struct field.
447 func (f *Field) IsMethod() bool {
448 return f.Type.kind == TFUNC && f.Type.Recv() != nil
449 }
450
451 // CompareFields compares two Field values by name.
452 func CompareFields(a, b *Field) int {
453 return CompareSyms(a.Sym, b.Sym)
454 }
455
456 // fields is a pointer to a slice of *Field.
457 // This saves space in Types that do not have fields or methods
458 // compared to a simple slice of *Field.
459 type fields struct {
460 s *[]*Field
461 }
462
463 // Slice returns the entries in f as a slice.
464 // Changes to the slice entries will be reflected in f.
465 func (f *fields) Slice() []*Field {
466 if f.s == nil {
467 return nil
468 }
469 return *f.s
470 }
471
472 // Set sets f to a slice.
473 // This takes ownership of the slice.
474 func (f *fields) Set(s []*Field) {
475 if len(s) == 0 {
476 f.s = nil
477 } else {
478 // Copy s and take address of t rather than s to avoid
479 // allocation in the case where len(s) == 0.
480 t := s
481 f.s = &t
482 }
483 }
484
485 // newType returns a new Type of the specified kind.
486 func newType(et Kind) *Type {
487 t := &Type{
488 kind: et,
489 width: BADWIDTH,
490 }
491 t.underlying = t
492 // TODO(josharian): lazily initialize some of these?
493 switch t.kind {
494 case TMAP:
495 t.extra = new(Map)
496 case TFORW:
497 t.extra = new(Forward)
498 case TFUNC:
499 t.extra = new(Func)
500 case TSTRUCT:
501 t.extra = new(Struct)
502 case TINTER:
503 t.extra = new(Interface)
504 case TPTR:
505 t.extra = Ptr{}
506 case TCHANARGS:
507 t.extra = ChanArgs{}
508 case TFUNCARGS:
509 t.extra = FuncArgs{}
510 case TCHAN:
511 t.extra = new(Chan)
512 case TTUPLE:
513 t.extra = new(Tuple)
514 case TRESULTS:
515 t.extra = new(Results)
516 }
517 return t
518 }
519
520 // NewArray returns a new fixed-length array Type.
521 func NewArray(elem *Type, bound int64) *Type {
522 if bound < 0 {
523 base.Fatalf("NewArray: invalid bound %v", bound)
524 }
525 t := newType(TARRAY)
526 t.extra = &Array{Elem: elem, Bound: bound}
527 if elem.HasShape() {
528 t.SetHasShape(true)
529 }
530 if elem.NotInHeap() {
531 t.SetNotInHeap(true)
532 }
533 return t
534 }
535
536 // NewSlice returns the slice Type with element type elem.
537 func NewSlice(elem *Type) *Type {
538 if t := elem.cache.slice; t != nil {
539 if t.Elem() != elem {
540 base.Fatalf("elem mismatch")
541 }
542 if elem.HasShape() != t.HasShape() {
543 base.Fatalf("Incorrect HasShape flag for cached slice type")
544 }
545 return t
546 }
547
548 t := newType(TSLICE)
549 t.extra = Slice{Elem: elem}
550 elem.cache.slice = t
551 if elem.HasShape() {
552 t.SetHasShape(true)
553 }
554 return t
555 }
556
557 // NewChan returns a new chan Type with direction dir.
558 func NewChan(elem *Type, dir ChanDir) *Type {
559 t := newType(TCHAN)
560 ct := t.chanType()
561 ct.Elem = elem
562 ct.Dir = dir
563 if elem.HasShape() {
564 t.SetHasShape(true)
565 }
566 return t
567 }
568
569 func NewTuple(t1, t2 *Type) *Type {
570 t := newType(TTUPLE)
571 t.extra.(*Tuple).first = t1
572 t.extra.(*Tuple).second = t2
573 if t1.HasShape() || t2.HasShape() {
574 t.SetHasShape(true)
575 }
576 return t
577 }
578
579 func newResults(types []*Type) *Type {
580 t := newType(TRESULTS)
581 t.extra.(*Results).Types = types
582 return t
583 }
584
585 func NewResults(types []*Type) *Type {
586 if len(types) == 1 && types[0] == TypeMem {
587 return TypeResultMem
588 }
589 return newResults(types)
590 }
591
592 func newSSA(name string) *Type {
593 t := newType(TSSA)
594 t.extra = name
595 return t
596 }
597
598 func newSIMD(name string) *Type {
599 t := newSSA(name)
600 t.isSIMD = true
601 return t
602 }
603
604 // NewMap returns a new map Type with key type k and element (aka value) type v.
605 func NewMap(k, v *Type) *Type {
606 t := newType(TMAP)
607 mt := t.MapType()
608 mt.Key = k
609 mt.Elem = v
610 if k.HasShape() || v.HasShape() {
611 t.SetHasShape(true)
612 }
613 return t
614 }
615
616 // NewPtrCacheEnabled controls whether *T Types are cached in T.
617 // Caching is disabled just before starting the backend.
618 // This allows the backend to run concurrently.
619 var NewPtrCacheEnabled = true
620
621 // NewPtr returns the pointer type pointing to t.
622 func NewPtr(elem *Type) *Type {
623 if elem == nil {
624 base.Fatalf("NewPtr: pointer to elem Type is nil")
625 }
626
627 if t := elem.cache.ptr; t != nil {
628 if t.Elem() != elem {
629 base.Fatalf("NewPtr: elem mismatch")
630 }
631 if elem.HasShape() != t.HasShape() {
632 base.Fatalf("Incorrect HasShape flag for cached pointer type")
633 }
634 return t
635 }
636
637 t := newType(TPTR)
638 t.extra = Ptr{Elem: elem}
639 t.width = int64(PtrSize)
640 t.align = uint8(PtrSize)
641 t.intRegs = 1
642 if NewPtrCacheEnabled {
643 elem.cache.ptr = t
644 }
645 if elem.HasShape() {
646 t.SetHasShape(true)
647 }
648 t.alg = AMEM
649 if elem.Noalg() {
650 t.SetNoalg(true)
651 t.alg = ANOALG
652 }
653 // Note: we can't check elem.NotInHeap here because it might
654 // not be set yet. See size.go:PtrDataSize.
655 t.ptrBytes = int64(PtrSize)
656 return t
657 }
658
659 // NewChanArgs returns a new TCHANARGS type for channel type c.
660 func NewChanArgs(c *Type) *Type {
661 t := newType(TCHANARGS)
662 t.extra = ChanArgs{T: c}
663 return t
664 }
665
666 // NewFuncArgs returns a new TFUNCARGS type for func type f.
667 func NewFuncArgs(f *Type) *Type {
668 t := newType(TFUNCARGS)
669 t.extra = FuncArgs{T: f}
670 return t
671 }
672
673 func NewField(pos src.XPos, sym *Sym, typ *Type) *Field {
674 f := &Field{
675 Pos: pos,
676 Sym: sym,
677 Type: typ,
678 Offset: BADWIDTH,
679 }
680 if typ == nil {
681 base.Fatalf("typ is nil")
682 }
683 return f
684 }
685
686 // SubstAny walks t, replacing instances of "any" with successive
687 // elements removed from types. It returns the substituted type.
688 func SubstAny(t *Type, types *[]*Type) *Type {
689 if t == nil {
690 return nil
691 }
692
693 switch t.kind {
694 default:
695 // Leave the type unchanged.
696
697 case TANY:
698 if len(*types) == 0 {
699 base.Fatalf("SubstArgTypes: not enough argument types")
700 }
701 t = (*types)[0]
702 *types = (*types)[1:]
703
704 case TPTR:
705 elem := SubstAny(t.Elem(), types)
706 if elem != t.Elem() {
707 t = t.copy()
708 t.extra = Ptr{Elem: elem}
709 }
710
711 case TARRAY:
712 elem := SubstAny(t.Elem(), types)
713 if elem != t.Elem() {
714 t = t.copy()
715 t.extra.(*Array).Elem = elem
716 }
717
718 case TSLICE:
719 elem := SubstAny(t.Elem(), types)
720 if elem != t.Elem() {
721 t = t.copy()
722 t.extra = Slice{Elem: elem}
723 }
724
725 case TCHAN:
726 elem := SubstAny(t.Elem(), types)
727 if elem != t.Elem() {
728 t = t.copy()
729 t.extra.(*Chan).Elem = elem
730 }
731
732 case TMAP:
733 key := SubstAny(t.Key(), types)
734 elem := SubstAny(t.Elem(), types)
735 if key != t.Key() || elem != t.Elem() {
736 t = t.copy()
737 t.extra.(*Map).Key = key
738 t.extra.(*Map).Elem = elem
739 }
740
741 case TFUNC:
742 ft := t.funcType()
743 allParams := substFields(ft.allParams, types)
744
745 t = t.copy()
746 ft = t.funcType()
747 ft.allParams = allParams
748
749 rt := ft.resultsTuple
750 rt = rt.copy()
751 ft.resultsTuple = rt
752 rt.setFields(t.Results())
753
754 case TSTRUCT:
755 // Make a copy of all fields, including ones whose type does not change.
756 // This prevents aliasing across functions, which can lead to later
757 // fields getting their Offset incorrectly overwritten.
758 nfs := substFields(t.Fields(), types)
759 t = t.copy()
760 t.setFields(nfs)
761 }
762
763 return t
764 }
765
766 func substFields(fields []*Field, types *[]*Type) []*Field {
767 nfs := make([]*Field, len(fields))
768 for i, f := range fields {
769 nft := SubstAny(f.Type, types)
770 nfs[i] = f.Copy()
771 nfs[i].Type = nft
772 }
773 return nfs
774 }
775
776 // copy returns a shallow copy of the Type.
777 func (t *Type) copy() *Type {
778 if t == nil {
779 return nil
780 }
781 nt := *t
782 // copy any *T Extra fields, to avoid aliasing
783 switch t.kind {
784 case TMAP:
785 x := *t.extra.(*Map)
786 nt.extra = &x
787 case TFORW:
788 x := *t.extra.(*Forward)
789 nt.extra = &x
790 case TFUNC:
791 x := *t.extra.(*Func)
792 nt.extra = &x
793 case TSTRUCT:
794 x := *t.extra.(*Struct)
795 nt.extra = &x
796 case TINTER:
797 x := *t.extra.(*Interface)
798 nt.extra = &x
799 case TCHAN:
800 x := *t.extra.(*Chan)
801 nt.extra = &x
802 case TARRAY:
803 x := *t.extra.(*Array)
804 nt.extra = &x
805 case TTUPLE, TSSA, TRESULTS:
806 base.Fatalf("ssa types cannot be copied")
807 }
808 // TODO(mdempsky): Find out why this is necessary and explain.
809 if t.underlying == t {
810 nt.underlying = &nt
811 }
812 return &nt
813 }
814
815 func (f *Field) Copy() *Field {
816 nf := *f
817 return &nf
818 }
819
820 func (t *Type) wantEtype(et Kind) {
821 if t.kind != et {
822 base.Fatalf("want %v, but have %v", et, t)
823 }
824 }
825
826 // ResultsTuple returns the result type of signature type t as a tuple.
827 // This can be used as the type of multi-valued call expressions.
828 func (t *Type) ResultsTuple() *Type { return t.funcType().resultsTuple }
829
830 // Recvs returns a slice of receiver parameters of signature type t.
831 // The returned slice always has length 0 or 1.
832 func (t *Type) Recvs() []*Field { return t.funcType().recvs() }
833
834 // Params returns a slice of regular parameters of signature type t.
835 func (t *Type) Params() []*Field { return t.funcType().params() }
836
837 // Results returns a slice of result parameters of signature type t.
838 func (t *Type) Results() []*Field { return t.funcType().results() }
839
840 // RecvParamsResults returns a slice containing all of the
841 // signature's parameters in receiver (if any), (normal) parameters,
842 // and then results.
843 func (t *Type) RecvParamsResults() []*Field { return t.funcType().allParams }
844
845 // RecvParams returns a slice containing the signature's receiver (if
846 // any) followed by its (normal) parameters.
847 func (t *Type) RecvParams() []*Field { return t.funcType().recvParams() }
848
849 // ParamsResults returns a slice containing the signature's (normal)
850 // parameters followed by its results.
851 func (t *Type) ParamsResults() []*Field { return t.funcType().paramsResults() }
852
853 func (t *Type) NumRecvs() int { return len(t.Recvs()) }
854 func (t *Type) NumParams() int { return len(t.Params()) }
855 func (t *Type) NumResults() int { return len(t.Results()) }
856
857 // IsVariadic reports whether function type t is variadic.
858 func (t *Type) IsVariadic() bool {
859 n := t.NumParams()
860 return n > 0 && t.Param(n-1).IsDDD()
861 }
862
863 // Recv returns the receiver of function type t, if any.
864 func (t *Type) Recv() *Field {
865 if s := t.Recvs(); len(s) == 1 {
866 return s[0]
867 }
868 return nil
869 }
870
871 // Param returns the i'th parameter of signature type t.
872 func (t *Type) Param(i int) *Field { return t.Params()[i] }
873
874 // Result returns the i'th result of signature type t.
875 func (t *Type) Result(i int) *Field { return t.Results()[i] }
876
877 // Key returns the key type of map type t.
878 func (t *Type) Key() *Type {
879 t.wantEtype(TMAP)
880 return t.extra.(*Map).Key
881 }
882
883 // Elem returns the type of elements of t.
884 // Usable with pointers, channels, arrays, slices, and maps.
885 func (t *Type) Elem() *Type {
886 switch t.kind {
887 case TPTR:
888 return t.extra.(Ptr).Elem
889 case TARRAY:
890 return t.extra.(*Array).Elem
891 case TSLICE:
892 return t.extra.(Slice).Elem
893 case TCHAN:
894 return t.extra.(*Chan).Elem
895 case TMAP:
896 return t.extra.(*Map).Elem
897 }
898 base.Fatalf("Type.Elem %s", t.kind)
899 return nil
900 }
901
902 // ChanArgs returns the channel type for TCHANARGS type t.
903 func (t *Type) ChanArgs() *Type {
904 t.wantEtype(TCHANARGS)
905 return t.extra.(ChanArgs).T
906 }
907
908 // FuncArgs returns the func type for TFUNCARGS type t.
909 func (t *Type) FuncArgs() *Type {
910 t.wantEtype(TFUNCARGS)
911 return t.extra.(FuncArgs).T
912 }
913
914 // IsFuncArgStruct reports whether t is a struct representing function parameters or results.
915 func (t *Type) IsFuncArgStruct() bool {
916 return t.kind == TSTRUCT && t.extra.(*Struct).ParamTuple
917 }
918
919 // Methods returns a pointer to the base methods (excluding embedding) for type t.
920 // These can either be concrete methods (for non-interface types) or interface
921 // methods (for interface types).
922 func (t *Type) Methods() []*Field {
923 return t.methods.Slice()
924 }
925
926 // AllMethods returns a pointer to all the methods (including embedding) for type t.
927 // For an interface type, this is the set of methods that are typically iterated
928 // over. For non-interface types, AllMethods() only returns a valid result after
929 // CalcMethods() has been called at least once.
930 func (t *Type) AllMethods() []*Field {
931 if t.kind == TINTER {
932 // Calculate the full method set of an interface type on the fly
933 // now, if not done yet.
934 CalcSize(t)
935 }
936 return t.allMethods.Slice()
937 }
938
939 // SetMethods sets the direct method set for type t (i.e., *not*
940 // including promoted methods from embedded types).
941 func (t *Type) SetMethods(fs []*Field) {
942 t.methods.Set(fs)
943 }
944
945 // SetAllMethods sets the set of all methods for type t (i.e.,
946 // including promoted methods from embedded types).
947 func (t *Type) SetAllMethods(fs []*Field) {
948 t.allMethods.Set(fs)
949 }
950
951 // fields returns the fields of struct type t.
952 func (t *Type) fields() *fields {
953 t.wantEtype(TSTRUCT)
954 return &t.extra.(*Struct).fields
955 }
956
957 // Field returns the i'th field of struct type t.
958 func (t *Type) Field(i int) *Field { return t.Fields()[i] }
959
960 // Fields returns a slice of containing all fields of
961 // a struct type t.
962 func (t *Type) Fields() []*Field { return t.fields().Slice() }
963
964 // setFields sets struct type t's fields to fields.
965 func (t *Type) setFields(fields []*Field) {
966 // If we've calculated the width of t before,
967 // then some other type such as a function signature
968 // might now have the wrong type.
969 // Rather than try to track and invalidate those,
970 // enforce that SetFields cannot be called once
971 // t's width has been calculated.
972 if t.widthCalculated() {
973 base.Fatalf("SetFields of %v: width previously calculated", t)
974 }
975 t.wantEtype(TSTRUCT)
976 t.fields().Set(fields)
977 }
978
979 // SetInterface sets the base methods of an interface type t.
980 func (t *Type) SetInterface(methods []*Field) {
981 t.wantEtype(TINTER)
982 t.methods.Set(methods)
983 }
984
985 // ArgWidth returns the total aligned argument size for a function.
986 // It includes the receiver, parameters, and results.
987 func (t *Type) ArgWidth() int64 {
988 t.wantEtype(TFUNC)
989 return t.extra.(*Func).Argwid
990 }
991
992 // Size returns the width of t in bytes.
993 func (t *Type) Size() int64 {
994 if t.kind == TSSA {
995 return t.width
996 }
997 CalcSize(t)
998 return t.width
999 }
1000
1001 // Alignment returns the alignment of t in bytes.
1002 func (t *Type) Alignment() int64 {
1003 CalcSize(t)
1004 return int64(t.align)
1005 }
1006
1007 func (t *Type) SimpleString() string {
1008 return t.kind.String()
1009 }
1010
1011 // Cmp is a comparison between values a and b.
1012 //
1013 // -1 if a < b
1014 // 0 if a == b
1015 // 1 if a > b
1016 type Cmp int8
1017
1018 const (
1019 CMPlt = Cmp(-1)
1020 CMPeq = Cmp(0)
1021 CMPgt = Cmp(1)
1022 )
1023
1024 // Compare compares types for purposes of the SSA back
1025 // end, returning a Cmp (one of CMPlt, CMPeq, CMPgt).
1026 // The answers are correct for an optimizer
1027 // or code generator, but not necessarily typechecking.
1028 // The order chosen is arbitrary, only consistency and division
1029 // into equivalence classes (Types that compare CMPeq) matters.
1030 func (t *Type) Compare(x *Type) Cmp {
1031 if x == t {
1032 return CMPeq
1033 }
1034 return t.cmp(x)
1035 }
1036
1037 func cmpForNe(x bool) Cmp {
1038 if x {
1039 return CMPlt
1040 }
1041 return CMPgt
1042 }
1043
1044 func (r *Sym) cmpsym(s *Sym) Cmp {
1045 if r == s {
1046 return CMPeq
1047 }
1048 if r == nil {
1049 return CMPlt
1050 }
1051 if s == nil {
1052 return CMPgt
1053 }
1054 // Fast sort, not pretty sort
1055 if len(r.Name) != len(s.Name) {
1056 return cmpForNe(len(r.Name) < len(s.Name))
1057 }
1058 if r.Pkg != s.Pkg {
1059 if len(r.Pkg.Prefix) != len(s.Pkg.Prefix) {
1060 return cmpForNe(len(r.Pkg.Prefix) < len(s.Pkg.Prefix))
1061 }
1062 if r.Pkg.Prefix != s.Pkg.Prefix {
1063 return cmpForNe(r.Pkg.Prefix < s.Pkg.Prefix)
1064 }
1065 }
1066 if r.Name != s.Name {
1067 return cmpForNe(r.Name < s.Name)
1068 }
1069 return CMPeq
1070 }
1071
1072 // cmp compares two *Types t and x, returning CMPlt,
1073 // CMPeq, CMPgt as t<x, t==x, t>x, for an arbitrary
1074 // and optimizer-centric notion of comparison.
1075 // TODO(josharian): make this safe for recursive interface types
1076 // and use in signatlist sorting. See issue 19869.
1077 func (t *Type) cmp(x *Type) Cmp {
1078 // This follows the structure of function identical in identity.go
1079 // with two exceptions.
1080 // 1. Symbols are compared more carefully because a <,=,> result is desired.
1081 // 2. Maps are treated specially to avoid endless recursion -- maps
1082 // contain an internal data type not expressible in Go source code.
1083 if t == x {
1084 return CMPeq
1085 }
1086 if t == nil {
1087 return CMPlt
1088 }
1089 if x == nil {
1090 return CMPgt
1091 }
1092
1093 if t.kind != x.kind {
1094 return cmpForNe(t.kind < x.kind)
1095 }
1096
1097 if t.obj != nil || x.obj != nil {
1098 // Special case: we keep byte and uint8 separate
1099 // for error messages. Treat them as equal.
1100 switch t.kind {
1101 case TUINT8:
1102 if (t == Types[TUINT8] || t == ByteType) && (x == Types[TUINT8] || x == ByteType) {
1103 return CMPeq
1104 }
1105
1106 case TINT32:
1107 if (t == Types[RuneType.kind] || t == RuneType) && (x == Types[RuneType.kind] || x == RuneType) {
1108 return CMPeq
1109 }
1110
1111 case TINTER:
1112 // Make sure named any type matches any empty interface.
1113 if t == AnyType && x.IsEmptyInterface() || x == AnyType && t.IsEmptyInterface() {
1114 return CMPeq
1115 }
1116 }
1117 }
1118
1119 if c := t.Sym().cmpsym(x.Sym()); c != CMPeq {
1120 return c
1121 }
1122
1123 if x.obj != nil {
1124 return CMPeq
1125 }
1126 // both syms nil, look at structure below.
1127
1128 switch t.kind {
1129 case TBOOL, TFLOAT32, TFLOAT64, TCOMPLEX64, TCOMPLEX128, TUNSAFEPTR, TUINTPTR,
1130 TINT8, TINT16, TINT32, TINT64, TINT, TUINT8, TUINT16, TUINT32, TUINT64, TUINT:
1131 return CMPeq
1132
1133 case TSSA:
1134 tname := t.extra.(string)
1135 xname := x.extra.(string)
1136 // desire fast sorting, not pretty sorting.
1137 if len(tname) == len(xname) {
1138 if tname == xname {
1139 return CMPeq
1140 }
1141 if tname < xname {
1142 return CMPlt
1143 }
1144 return CMPgt
1145 }
1146 if len(tname) > len(xname) {
1147 return CMPgt
1148 }
1149 return CMPlt
1150
1151 case TTUPLE:
1152 xtup := x.extra.(*Tuple)
1153 ttup := t.extra.(*Tuple)
1154 if c := ttup.first.Compare(xtup.first); c != CMPeq {
1155 return c
1156 }
1157 return ttup.second.Compare(xtup.second)
1158
1159 case TRESULTS:
1160 xResults := x.extra.(*Results)
1161 tResults := t.extra.(*Results)
1162 xl, tl := len(xResults.Types), len(tResults.Types)
1163 if tl != xl {
1164 if tl < xl {
1165 return CMPlt
1166 }
1167 return CMPgt
1168 }
1169 for i := 0; i < tl; i++ {
1170 if c := tResults.Types[i].Compare(xResults.Types[i]); c != CMPeq {
1171 return c
1172 }
1173 }
1174 return CMPeq
1175
1176 case TMAP:
1177 if c := t.Key().cmp(x.Key()); c != CMPeq {
1178 return c
1179 }
1180 return t.Elem().cmp(x.Elem())
1181
1182 case TPTR, TSLICE:
1183 // No special cases for these, they are handled
1184 // by the general code after the switch.
1185
1186 case TSTRUCT:
1187 // Is this a map group type?
1188 if t.StructType().Map == nil {
1189 if x.StructType().Map != nil {
1190 return CMPlt // nil < non-nil
1191 }
1192 // to the general case
1193 } else if x.StructType().Map == nil {
1194 return CMPgt // nil > non-nil
1195 }
1196 // Both have non-nil Map, fallthrough to the general
1197 // case. Note that the map type does not directly refer
1198 // to the group type (it uses unsafe.Pointer). If it
1199 // did, this would need special handling to avoid
1200 // infinite recursion.
1201
1202 tfs := t.Fields()
1203 xfs := x.Fields()
1204 for i := 0; i < len(tfs) && i < len(xfs); i++ {
1205 t1, x1 := tfs[i], xfs[i]
1206 if t1.Embedded != x1.Embedded {
1207 return cmpForNe(t1.Embedded < x1.Embedded)
1208 }
1209 if t1.Note != x1.Note {
1210 return cmpForNe(t1.Note < x1.Note)
1211 }
1212 if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq {
1213 return c
1214 }
1215 if c := t1.Type.cmp(x1.Type); c != CMPeq {
1216 return c
1217 }
1218 }
1219 if len(tfs) != len(xfs) {
1220 return cmpForNe(len(tfs) < len(xfs))
1221 }
1222 return CMPeq
1223
1224 case TINTER:
1225 tfs := t.AllMethods()
1226 xfs := x.AllMethods()
1227 for i := 0; i < len(tfs) && i < len(xfs); i++ {
1228 t1, x1 := tfs[i], xfs[i]
1229 if c := t1.Sym.cmpsym(x1.Sym); c != CMPeq {
1230 return c
1231 }
1232 if c := t1.Type.cmp(x1.Type); c != CMPeq {
1233 return c
1234 }
1235 }
1236 if len(tfs) != len(xfs) {
1237 return cmpForNe(len(tfs) < len(xfs))
1238 }
1239 return CMPeq
1240
1241 case TFUNC:
1242 if tn, xn := t.NumRecvs(), x.NumRecvs(); tn != xn {
1243 return cmpForNe(tn < xn)
1244 }
1245 if tn, xn := t.NumParams(), x.NumParams(); tn != xn {
1246 return cmpForNe(tn < xn)
1247 }
1248 if tn, xn := t.NumResults(), x.NumResults(); tn != xn {
1249 return cmpForNe(tn < xn)
1250 }
1251 if tv, xv := t.IsVariadic(), x.IsVariadic(); tv != xv {
1252 return cmpForNe(!tv)
1253 }
1254
1255 tfs := t.RecvParamsResults()
1256 xfs := x.RecvParamsResults()
1257 for i, tf := range tfs {
1258 if c := tf.Type.cmp(xfs[i].Type); c != CMPeq {
1259 return c
1260 }
1261 }
1262 return CMPeq
1263
1264 case TARRAY:
1265 if t.NumElem() != x.NumElem() {
1266 return cmpForNe(t.NumElem() < x.NumElem())
1267 }
1268
1269 case TCHAN:
1270 if t.ChanDir() != x.ChanDir() {
1271 return cmpForNe(t.ChanDir() < x.ChanDir())
1272 }
1273
1274 default:
1275 e := fmt.Sprintf("Do not know how to compare %v with %v", t, x)
1276 panic(e)
1277 }
1278
1279 // Common element type comparison for TARRAY, TCHAN, TPTR, and TSLICE.
1280 return t.Elem().cmp(x.Elem())
1281 }
1282
1283 // IsKind reports whether t is a Type of the specified kind.
1284 func (t *Type) IsKind(et Kind) bool {
1285 return t != nil && t.kind == et
1286 }
1287
1288 func (t *Type) IsBoolean() bool {
1289 return t.kind == TBOOL
1290 }
1291
1292 var unsignedEType = [...]Kind{
1293 TINT8: TUINT8,
1294 TUINT8: TUINT8,
1295 TINT16: TUINT16,
1296 TUINT16: TUINT16,
1297 TINT32: TUINT32,
1298 TUINT32: TUINT32,
1299 TINT64: TUINT64,
1300 TUINT64: TUINT64,
1301 TINT: TUINT,
1302 TUINT: TUINT,
1303 TUINTPTR: TUINTPTR,
1304 }
1305
1306 // ToUnsigned returns the unsigned equivalent of integer type t.
1307 func (t *Type) ToUnsigned() *Type {
1308 if !t.IsInteger() {
1309 base.Fatalf("unsignedType(%v)", t)
1310 }
1311 return Types[unsignedEType[t.kind]]
1312 }
1313
1314 func (t *Type) IsInteger() bool {
1315 switch t.kind {
1316 case TINT8, TUINT8, TINT16, TUINT16, TINT32, TUINT32, TINT64, TUINT64, TINT, TUINT, TUINTPTR:
1317 return true
1318 }
1319 return t == UntypedInt || t == UntypedRune
1320 }
1321
1322 func (t *Type) IsSigned() bool {
1323 switch t.kind {
1324 case TINT8, TINT16, TINT32, TINT64, TINT:
1325 return true
1326 }
1327 return false
1328 }
1329
1330 func (t *Type) IsUnsigned() bool {
1331 switch t.kind {
1332 case TUINT8, TUINT16, TUINT32, TUINT64, TUINT, TUINTPTR:
1333 return true
1334 }
1335 return false
1336 }
1337
1338 func (t *Type) IsFloat() bool {
1339 return t.kind == TFLOAT32 || t.kind == TFLOAT64 || t == UntypedFloat
1340 }
1341
1342 func (t *Type) IsComplex() bool {
1343 return t.kind == TCOMPLEX64 || t.kind == TCOMPLEX128 || t == UntypedComplex
1344 }
1345
1346 // IsPtr reports whether t is a regular Go pointer type.
1347 // This does not include unsafe.Pointer.
1348 func (t *Type) IsPtr() bool {
1349 return t.kind == TPTR
1350 }
1351
1352 // IsPtrElem reports whether t is the element of a pointer (to t).
1353 func (t *Type) IsPtrElem() bool {
1354 return t.cache.ptr != nil
1355 }
1356
1357 // IsUnsafePtr reports whether t is an unsafe pointer.
1358 func (t *Type) IsUnsafePtr() bool {
1359 return t.kind == TUNSAFEPTR
1360 }
1361
1362 // IsUintptr reports whether t is a uintptr.
1363 func (t *Type) IsUintptr() bool {
1364 return t.kind == TUINTPTR
1365 }
1366
1367 // IsPtrShaped reports whether t is represented by a single machine pointer.
1368 // In addition to regular Go pointer types, this includes map, channel, and
1369 // function types and unsafe.Pointer. It does not include array or struct types
1370 // that consist of a single pointer shaped type.
1371 // TODO(mdempsky): Should it? See golang.org/issue/15028.
1372 func (t *Type) IsPtrShaped() bool {
1373 return t.kind == TPTR || t.kind == TUNSAFEPTR ||
1374 t.kind == TMAP || t.kind == TCHAN || t.kind == TFUNC
1375 }
1376
1377 // HasNil reports whether the set of values determined by t includes nil.
1378 func (t *Type) HasNil() bool {
1379 switch t.kind {
1380 case TCHAN, TFUNC, TINTER, TMAP, TNIL, TPTR, TSLICE, TUNSAFEPTR:
1381 return true
1382 }
1383 return false
1384 }
1385
1386 func (t *Type) IsString() bool {
1387 return t.kind == TSTRING
1388 }
1389
1390 func (t *Type) IsMap() bool {
1391 return t.kind == TMAP
1392 }
1393
1394 func (t *Type) IsChan() bool {
1395 return t.kind == TCHAN
1396 }
1397
1398 func (t *Type) IsSlice() bool {
1399 return t.kind == TSLICE
1400 }
1401
1402 func (t *Type) IsArray() bool {
1403 return t.kind == TARRAY
1404 }
1405
1406 func (t *Type) IsStruct() bool {
1407 return t.kind == TSTRUCT
1408 }
1409
1410 func (t *Type) IsInterface() bool {
1411 return t.kind == TINTER
1412 }
1413
1414 // IsEmptyInterface reports whether t is an empty interface type.
1415 func (t *Type) IsEmptyInterface() bool {
1416 return t.IsInterface() && len(t.AllMethods()) == 0
1417 }
1418
1419 // IsScalar reports whether 't' is a scalar Go type, e.g.
1420 // bool/int/float/complex. Note that struct and array types consisting
1421 // of a single scalar element are not considered scalar, likewise
1422 // pointer types are also not considered scalar.
1423 func (t *Type) IsScalar() bool {
1424 switch t.kind {
1425 case TBOOL, TINT8, TUINT8, TINT16, TUINT16, TINT32,
1426 TUINT32, TINT64, TUINT64, TINT, TUINT,
1427 TUINTPTR, TCOMPLEX64, TCOMPLEX128, TFLOAT32, TFLOAT64:
1428 return true
1429 }
1430 return false
1431 }
1432
1433 func (t *Type) PtrTo() *Type {
1434 return NewPtr(t)
1435 }
1436
1437 func (t *Type) NumFields() int {
1438 if t.kind == TRESULTS {
1439 return len(t.extra.(*Results).Types)
1440 }
1441 return len(t.Fields())
1442 }
1443 func (t *Type) FieldType(i int) *Type {
1444 if t.kind == TTUPLE {
1445 switch i {
1446 case 0:
1447 return t.extra.(*Tuple).first
1448 case 1:
1449 return t.extra.(*Tuple).second
1450 default:
1451 panic("bad tuple index")
1452 }
1453 }
1454 if t.kind == TRESULTS {
1455 return t.extra.(*Results).Types[i]
1456 }
1457 return t.Field(i).Type
1458 }
1459 func (t *Type) FieldOff(i int) int64 {
1460 return t.Field(i).Offset
1461 }
1462 func (t *Type) FieldName(i int) string {
1463 return t.Field(i).Sym.Name
1464 }
1465
1466 // OffsetOf reports the offset of the field of a struct.
1467 // The field is looked up by name.
1468 func (t *Type) OffsetOf(name string) int64 {
1469 if t.kind != TSTRUCT {
1470 base.Fatalf("can't call OffsetOf on non-struct %v", t)
1471 }
1472 for _, f := range t.Fields() {
1473 if f.Sym.Name == name {
1474 return f.Offset
1475 }
1476 }
1477 base.Fatalf("couldn't find field %s in %v", name, t)
1478 return -1
1479 }
1480
1481 func (t *Type) NumElem() int64 {
1482 t.wantEtype(TARRAY)
1483 return t.extra.(*Array).Bound
1484 }
1485
1486 type componentsIncludeBlankFields bool
1487
1488 const (
1489 IgnoreBlankFields componentsIncludeBlankFields = false
1490 CountBlankFields componentsIncludeBlankFields = true
1491 )
1492
1493 // NumComponents returns the number of primitive elements that compose t.
1494 // Struct and array types are flattened for the purpose of counting.
1495 // All other types (including string, slice, and interface types) count as one element.
1496 // If countBlank is IgnoreBlankFields, then blank struct fields
1497 // (and their comprised elements) are excluded from the count.
1498 // struct { x, y [3]int } has six components; [10]struct{ x, y string } has twenty.
1499 func (t *Type) NumComponents(countBlank componentsIncludeBlankFields) int64 {
1500 switch t.kind {
1501 case TSTRUCT:
1502 if t.IsFuncArgStruct() {
1503 base.Fatalf("NumComponents func arg struct")
1504 }
1505 var n int64
1506 for _, f := range t.Fields() {
1507 if countBlank == IgnoreBlankFields && f.Sym.IsBlank() {
1508 continue
1509 }
1510 n += f.Type.NumComponents(countBlank)
1511 }
1512 return n
1513 case TARRAY:
1514 return t.NumElem() * t.Elem().NumComponents(countBlank)
1515 }
1516 return 1
1517 }
1518
1519 // SoleComponent returns the only primitive component in t,
1520 // if there is exactly one. Otherwise, it returns nil.
1521 // Components are counted as in NumComponents, including blank fields.
1522 // Keep in sync with cmd/compile/internal/walk/convert.go:soleComponent.
1523 func (t *Type) SoleComponent() *Type {
1524 switch t.kind {
1525 case TSTRUCT:
1526 if t.IsFuncArgStruct() {
1527 base.Fatalf("SoleComponent func arg struct")
1528 }
1529 if t.NumFields() != 1 {
1530 return nil
1531 }
1532 return t.Field(0).Type.SoleComponent()
1533 case TARRAY:
1534 if t.NumElem() != 1 {
1535 return nil
1536 }
1537 return t.Elem().SoleComponent()
1538 }
1539 return t
1540 }
1541
1542 // ChanDir returns the direction of a channel type t.
1543 // The direction will be one of Crecv, Csend, or Cboth.
1544 func (t *Type) ChanDir() ChanDir {
1545 t.wantEtype(TCHAN)
1546 return t.extra.(*Chan).Dir
1547 }
1548
1549 func (t *Type) IsMemory() bool {
1550 if t == TypeMem || t.kind == TTUPLE && t.extra.(*Tuple).second == TypeMem {
1551 return true
1552 }
1553 if t.kind == TRESULTS {
1554 if types := t.extra.(*Results).Types; len(types) > 0 && types[len(types)-1] == TypeMem {
1555 return true
1556 }
1557 }
1558 return false
1559 }
1560 func (t *Type) IsFlags() bool { return t == TypeFlags }
1561 func (t *Type) IsVoid() bool { return t == TypeVoid }
1562 func (t *Type) IsTuple() bool { return t.kind == TTUPLE }
1563 func (t *Type) IsResults() bool { return t.kind == TRESULTS }
1564
1565 // IsUntyped reports whether t is an untyped type.
1566 func (t *Type) IsUntyped() bool {
1567 if t == nil {
1568 return false
1569 }
1570 if t == UntypedString || t == UntypedBool {
1571 return true
1572 }
1573 switch t.kind {
1574 case TNIL, TIDEAL:
1575 return true
1576 }
1577 return false
1578 }
1579
1580 // HasPointers reports whether t contains a heap pointer.
1581 // Note that this function ignores pointers to not-in-heap types.
1582 func (t *Type) HasPointers() bool {
1583 return PtrDataSize(t) > 0
1584 }
1585
1586 var recvType *Type
1587
1588 // FakeRecvType returns the singleton type used for interface method receivers.
1589 func FakeRecvType() *Type {
1590 if recvType == nil {
1591 recvType = NewPtr(newType(TSTRUCT))
1592 }
1593 return recvType
1594 }
1595
1596 func FakeRecv() *Field {
1597 return NewField(base.AutogeneratedPos, nil, FakeRecvType())
1598 }
1599
1600 var (
1601 // TSSA types. HasPointers assumes these are pointer-free.
1602 TypeInvalid = newSSA("invalid")
1603 TypeMem = newSSA("mem")
1604 TypeFlags = newSSA("flags")
1605 TypeVoid = newSSA("void")
1606 TypeInt128 = newSSA("int128")
1607 TypeVec128 = newSIMD("vec128")
1608 TypeVec256 = newSIMD("vec256")
1609 TypeVec512 = newSIMD("vec512")
1610 TypeMask = newSIMD("mask") // not a vector, not 100% sure what this should be.
1611 TypeResultMem = newResults([]*Type{TypeMem})
1612 )
1613
1614 func init() {
1615 TypeInt128.width = 16
1616 TypeInt128.align = 8
1617
1618 TypeVec128.width = 16
1619 TypeVec128.align = 8
1620 TypeVec256.width = 32
1621 TypeVec256.align = 8
1622 TypeVec512.width = 64
1623 TypeVec512.align = 8
1624
1625 TypeMask.width = 8 // This will depend on the architecture; spilling will be "interesting".
1626 TypeMask.align = 8
1627 }
1628
1629 // NewNamed returns a new named type for the given type name. obj should be an
1630 // ir.Name. The new type is incomplete (marked as TFORW kind), and the underlying
1631 // type should be set later via SetUnderlying(). References to the type are
1632 // maintained until the type is filled in, so those references can be updated when
1633 // the type is complete.
1634 func NewNamed(obj Object) *Type {
1635 t := newType(TFORW)
1636 t.obj = obj
1637 sym := obj.Sym()
1638 if sym.Pkg == ShapePkg {
1639 t.SetIsShape(true)
1640 t.SetHasShape(true)
1641 }
1642 if sym.Pkg.Path == "internal/runtime/sys" && sym.Name == "nih" {
1643 // Recognize the special not-in-heap type. Any type including
1644 // this type will also be not-in-heap.
1645 // This logic is duplicated in go/types and
1646 // cmd/compile/internal/types2.
1647 t.SetNotInHeap(true)
1648 }
1649 return t
1650 }
1651
1652 // Obj returns the canonical type name node for a named type t, nil for an unnamed type.
1653 func (t *Type) Obj() Object {
1654 return t.obj
1655 }
1656
1657 // SetUnderlying sets the underlying type of an incomplete type (i.e. type whose kind
1658 // is currently TFORW). SetUnderlying automatically updates any types that were waiting
1659 // for this type to be completed.
1660 func (t *Type) SetUnderlying(underlying *Type) {
1661 if underlying.kind == TFORW {
1662 // This type isn't computed yet; when it is, update n.
1663 underlying.forwardType().Copyto = append(underlying.forwardType().Copyto, t)
1664 return
1665 }
1666
1667 ft := t.forwardType()
1668
1669 // TODO(mdempsky): Fix Type rekinding.
1670 t.kind = underlying.kind
1671 t.extra = underlying.extra
1672 t.width = underlying.width
1673 t.align = underlying.align
1674 t.alg = underlying.alg
1675 t.ptrBytes = underlying.ptrBytes
1676 t.intRegs = underlying.intRegs
1677 t.floatRegs = underlying.floatRegs
1678 t.underlying = underlying.underlying
1679
1680 if underlying.NotInHeap() {
1681 t.SetNotInHeap(true)
1682 }
1683 if underlying.HasShape() {
1684 t.SetHasShape(true)
1685 }
1686
1687 // spec: "The declared type does not inherit any methods bound
1688 // to the existing type, but the method set of an interface
1689 // type [...] remains unchanged."
1690 if t.IsInterface() {
1691 t.methods = underlying.methods
1692 t.allMethods = underlying.allMethods
1693 }
1694
1695 // Update types waiting on this type.
1696 for _, w := range ft.Copyto {
1697 w.SetUnderlying(t)
1698 }
1699
1700 // Double-check use of type as embedded type.
1701 if ft.Embedlineno.IsKnown() {
1702 if t.IsPtr() || t.IsUnsafePtr() {
1703 base.ErrorfAt(ft.Embedlineno, errors.InvalidPtrEmbed, "embedded type cannot be a pointer")
1704 }
1705 }
1706 }
1707
1708 func fieldsHasShape(fields []*Field) bool {
1709 for _, f := range fields {
1710 if f.Type != nil && f.Type.HasShape() {
1711 return true
1712 }
1713 }
1714 return false
1715 }
1716
1717 // NewInterface returns a new interface for the given methods and
1718 // embedded types. Embedded types are specified as fields with no Sym.
1719 func NewInterface(methods []*Field) *Type {
1720 t := newType(TINTER)
1721 t.SetInterface(methods)
1722 for _, f := range methods {
1723 // f.Type could be nil for a broken interface declaration
1724 if f.Type != nil && f.Type.HasShape() {
1725 t.SetHasShape(true)
1726 break
1727 }
1728 }
1729 return t
1730 }
1731
1732 // NewSignature returns a new function type for the given receiver,
1733 // parameters, and results, any of which may be nil.
1734 func NewSignature(recv *Field, params, results []*Field) *Type {
1735 startParams := 0
1736 if recv != nil {
1737 startParams = 1
1738 }
1739 startResults := startParams + len(params)
1740
1741 allParams := make([]*Field, startResults+len(results))
1742 if recv != nil {
1743 allParams[0] = recv
1744 }
1745 copy(allParams[startParams:], params)
1746 copy(allParams[startResults:], results)
1747
1748 t := newType(TFUNC)
1749 ft := t.funcType()
1750
1751 funargs := func(fields []*Field) *Type {
1752 s := NewStruct(fields)
1753 s.StructType().ParamTuple = true
1754 return s
1755 }
1756
1757 ft.allParams = allParams
1758 ft.startParams = startParams
1759 ft.startResults = startResults
1760
1761 ft.resultsTuple = funargs(allParams[startResults:])
1762
1763 if fieldsHasShape(allParams) {
1764 t.SetHasShape(true)
1765 }
1766
1767 return t
1768 }
1769
1770 // NewStruct returns a new struct with the given fields.
1771 func NewStruct(fields []*Field) *Type {
1772 t := newType(TSTRUCT)
1773 t.setFields(fields)
1774 if fieldsHasShape(fields) {
1775 t.SetHasShape(true)
1776 }
1777 for _, f := range fields {
1778 if f.Type.NotInHeap() {
1779 t.SetNotInHeap(true)
1780 break
1781 }
1782 }
1783
1784 return t
1785 }
1786
1787 var (
1788 IsInt [NTYPE]bool
1789 IsFloat [NTYPE]bool
1790 IsComplex [NTYPE]bool
1791 IsSimple [NTYPE]bool
1792 )
1793
1794 var IsOrdered [NTYPE]bool
1795
1796 // IsReflexive reports whether t has a reflexive equality operator.
1797 // That is, if x==x for all x of type t.
1798 func IsReflexive(t *Type) bool {
1799 switch t.Kind() {
1800 case TBOOL,
1801 TINT,
1802 TUINT,
1803 TINT8,
1804 TUINT8,
1805 TINT16,
1806 TUINT16,
1807 TINT32,
1808 TUINT32,
1809 TINT64,
1810 TUINT64,
1811 TUINTPTR,
1812 TPTR,
1813 TUNSAFEPTR,
1814 TSTRING,
1815 TCHAN:
1816 return true
1817
1818 case TFLOAT32,
1819 TFLOAT64,
1820 TCOMPLEX64,
1821 TCOMPLEX128,
1822 TINTER:
1823 return false
1824
1825 case TARRAY:
1826 return IsReflexive(t.Elem())
1827
1828 case TSTRUCT:
1829 for _, t1 := range t.Fields() {
1830 if !IsReflexive(t1.Type) {
1831 return false
1832 }
1833 }
1834 return true
1835
1836 default:
1837 base.Fatalf("bad type for map key: %v", t)
1838 return false
1839 }
1840 }
1841
1842 // Can this type be stored directly in an interface word?
1843 // Yes, if the representation is a single pointer.
1844 func IsDirectIface(t *Type) bool {
1845 return t.Size() == int64(PtrSize) && PtrDataSize(t) == int64(PtrSize)
1846 }
1847
1848 // IsInterfaceMethod reports whether (field) m is
1849 // an interface method. Such methods have the
1850 // special receiver type types.FakeRecvType().
1851 func IsInterfaceMethod(f *Type) bool {
1852 return f.Recv().Type == FakeRecvType()
1853 }
1854
1855 // IsMethodApplicable reports whether method m can be called on a
1856 // value of type t. This is necessary because we compute a single
1857 // method set for both T and *T, but some *T methods are not
1858 // applicable to T receivers.
1859 func IsMethodApplicable(t *Type, m *Field) bool {
1860 return t.IsPtr() || !m.Type.Recv().Type.IsPtr() || IsInterfaceMethod(m.Type) || m.Embedded == 2
1861 }
1862
1863 // RuntimeSymName returns the name of s if it's in package "runtime"; otherwise
1864 // it returns "".
1865 func RuntimeSymName(s *Sym) string {
1866 if s.Pkg.Path == "runtime" {
1867 return s.Name
1868 }
1869 return ""
1870 }
1871
1872 // ReflectSymName returns the name of s if it's in package "reflect"; otherwise
1873 // it returns "".
1874 func ReflectSymName(s *Sym) string {
1875 if s.Pkg.Path == "reflect" {
1876 return s.Name
1877 }
1878 return ""
1879 }
1880
1881 // IsNoInstrumentPkg reports whether p is a package that
1882 // should not be instrumented.
1883 func IsNoInstrumentPkg(p *Pkg) bool {
1884 return objabi.LookupPkgSpecial(p.Path).NoInstrument
1885 }
1886
1887 // IsNoRacePkg reports whether p is a package that
1888 // should not be race instrumented.
1889 func IsNoRacePkg(p *Pkg) bool {
1890 return objabi.LookupPkgSpecial(p.Path).NoRaceFunc
1891 }
1892
1893 // IsRuntimePkg reports whether p is a runtime package.
1894 func IsRuntimePkg(p *Pkg) bool {
1895 return objabi.LookupPkgSpecial(p.Path).Runtime
1896 }
1897
1898 // ReceiverBaseType returns the underlying type, if any,
1899 // that owns methods with receiver parameter t.
1900 // The result is either a named type or an anonymous struct.
1901 func ReceiverBaseType(t *Type) *Type {
1902 if t == nil {
1903 return nil
1904 }
1905
1906 // Strip away pointer if it's there.
1907 if t.IsPtr() {
1908 if t.Sym() != nil {
1909 return nil
1910 }
1911 t = t.Elem()
1912 if t == nil {
1913 return nil
1914 }
1915 }
1916
1917 // Must be a named type or anonymous struct.
1918 if t.Sym() == nil && !t.IsStruct() {
1919 return nil
1920 }
1921
1922 // Check types.
1923 if IsSimple[t.Kind()] {
1924 return t
1925 }
1926 switch t.Kind() {
1927 case TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRING, TSTRUCT:
1928 return t
1929 }
1930 return nil
1931 }
1932
1933 func FloatForComplex(t *Type) *Type {
1934 switch t.Kind() {
1935 case TCOMPLEX64:
1936 return Types[TFLOAT32]
1937 case TCOMPLEX128:
1938 return Types[TFLOAT64]
1939 }
1940 base.Fatalf("unexpected type: %v", t)
1941 return nil
1942 }
1943
1944 func ComplexForFloat(t *Type) *Type {
1945 switch t.Kind() {
1946 case TFLOAT32:
1947 return Types[TCOMPLEX64]
1948 case TFLOAT64:
1949 return Types[TCOMPLEX128]
1950 }
1951 base.Fatalf("unexpected type: %v", t)
1952 return nil
1953 }
1954
1955 func TypeSym(t *Type) *Sym {
1956 return TypeSymLookup(TypeSymName(t))
1957 }
1958
1959 func TypeSymLookup(name string) *Sym {
1960 typepkgmu.Lock()
1961 s := typepkg.Lookup(name)
1962 typepkgmu.Unlock()
1963 return s
1964 }
1965
1966 func TypeSymName(t *Type) string {
1967 name := t.LinkString()
1968 // Use a separate symbol name for Noalg types for #17752.
1969 if TypeHasNoAlg(t) {
1970 name = "noalg." + name
1971 }
1972 return name
1973 }
1974
1975 // Fake package for runtime type info (headers)
1976 // Don't access directly, use typeLookup below.
1977 var (
1978 typepkgmu sync.Mutex // protects typepkg lookups
1979 typepkg = NewPkg("type", "type")
1980 )
1981
1982 var SimType [NTYPE]Kind
1983
1984 // Fake package for shape types (see typecheck.Shapify()).
1985 var ShapePkg = NewPkg("go.shape", "go.shape")
1986
1987 func (t *Type) IsSIMD() bool {
1988 return t.isSIMD
1989 }