1 // Copyright 2009 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.
5 // Package reflect implements run-time reflection, allowing a program to
6 // manipulate objects with arbitrary types. The typical use is to take a value
7 // with static type interface{} and extract its dynamic type information by
8 // calling TypeOf, which returns a Type.
10 // A call to ValueOf returns a Value representing the run-time data.
11 // Zero takes a Type and returns a Value representing a zero value
14 // See "The Laws of Reflection" for an introduction to reflection in Go:
15 // http://golang.org/doc/articles/laws_of_reflection.html
24 // Type is the representation of a Go type.
26 // Not all methods apply to all kinds of types. Restrictions,
27 // if any, are noted in the documentation for each method.
28 // Use the Kind method to find out the kind of type before
29 // calling kind-specific methods. Calling a method
30 // inappropriate to the kind of type causes a run-time panic.
32 // Methods applicable to all types.
34 // Align returns the alignment in bytes of a value of
35 // this type when allocated in memory.
38 // FieldAlign returns the alignment in bytes of a value of
39 // this type when used as a field in a struct.
42 // Method returns the i'th method in the type's method set.
43 // It panics if i is not in the range [0, NumMethod()).
45 // For a non-interface type T or *T, the returned Method's Type and Func
46 // fields describe a function whose first argument is the receiver.
48 // For an interface type, the returned Method's Type field gives the
49 // method signature, without a receiver, and the Func field is nil.
52 // MethodByName returns the method with that name in the type's
53 // method set and a boolean indicating if the method was found.
55 // For a non-interface type T or *T, the returned Method's Type and Func
56 // fields describe a function whose first argument is the receiver.
58 // For an interface type, the returned Method's Type field gives the
59 // method signature, without a receiver, and the Func field is nil.
60 MethodByName(string) (Method, bool)
62 // NumMethod returns the number of methods in the type's method set.
65 // Name returns the type's name within its package.
66 // It returns an empty string for unnamed types.
69 // PkgPath returns a named type's package path, that is, the import path
70 // that uniquely identifies the package, such as "encoding/base64".
71 // If the type was predeclared (string, error) or unnamed (*T, struct{}, []int),
72 // the package path will be the empty string.
75 // Size returns the number of bytes needed to store
76 // a value of the given type; it is analogous to unsafe.Sizeof.
79 // String returns a string representation of the type.
80 // The string representation may use shortened package names
81 // (e.g., base64 instead of "encoding/base64") and is not
82 // guaranteed to be unique among types. To test for equality,
83 // compare the Types directly.
86 // Used internally by gccgo--the string retaining quoting.
89 // Kind returns the specific kind of this type.
92 // Implements returns true if the type implements the interface type u.
93 Implements(u Type) bool
95 // AssignableTo returns true if a value of the type is assignable to type u.
96 AssignableTo(u Type) bool
98 // ConvertibleTo returns true if a value of the type is convertible to type u.
99 ConvertibleTo(u Type) bool
101 // Methods applicable only to some types, depending on Kind.
102 // The methods allowed for each kind are:
104 // Int*, Uint*, Float*, Complex*: Bits
106 // Chan: ChanDir, Elem
107 // Func: In, NumIn, Out, NumOut, IsVariadic.
111 // Struct: Field, FieldByIndex, FieldByName, FieldByNameFunc, NumField
113 // Bits returns the size of the type in bits.
114 // It panics if the type's Kind is not one of the
115 // sized or unsized Int, Uint, Float, or Complex kinds.
118 // ChanDir returns a channel type's direction.
119 // It panics if the type's Kind is not Chan.
122 // IsVariadic returns true if a function type's final input parameter
123 // is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's
124 // implicit actual type []T.
126 // For concreteness, if t represents func(x int, y ... float64), then
129 // t.In(0) is the reflect.Type for "int"
130 // t.In(1) is the reflect.Type for "[]float64"
131 // t.IsVariadic() == true
133 // IsVariadic panics if the type's Kind is not Func.
136 // Elem returns a type's element type.
137 // It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
140 // Field returns a struct type's i'th field.
141 // It panics if the type's Kind is not Struct.
142 // It panics if i is not in the range [0, NumField()).
143 Field(i int) StructField
145 // FieldByIndex returns the nested field corresponding
146 // to the index sequence. It is equivalent to calling Field
147 // successively for each index i.
148 // It panics if the type's Kind is not Struct.
149 FieldByIndex(index []int) StructField
151 // FieldByName returns the struct field with the given name
152 // and a boolean indicating if the field was found.
153 FieldByName(name string) (StructField, bool)
155 // FieldByNameFunc returns the first struct field with a name
156 // that satisfies the match function and a boolean indicating if
157 // the field was found.
158 FieldByNameFunc(match func(string) bool) (StructField, bool)
160 // In returns the type of a function type's i'th input parameter.
161 // It panics if the type's Kind is not Func.
162 // It panics if i is not in the range [0, NumIn()).
165 // Key returns a map type's key type.
166 // It panics if the type's Kind is not Map.
169 // Len returns an array type's length.
170 // It panics if the type's Kind is not Array.
173 // NumField returns a struct type's field count.
174 // It panics if the type's Kind is not Struct.
177 // NumIn returns a function type's input parameter count.
178 // It panics if the type's Kind is not Func.
181 // NumOut returns a function type's output parameter count.
182 // It panics if the type's Kind is not Func.
185 // Out returns the type of a function type's i'th output parameter.
186 // It panics if the type's Kind is not Func.
187 // It panics if i is not in the range [0, NumOut()).
191 uncommon() *uncommonType
195 * These data structures are known to the compiler (../../cmd/gc/reflect.c).
196 * A few are known to ../runtime/type.go to convey to debuggers.
197 * They are also known to ../runtime/type.h.
200 // A Kind represents the specific kind of type that a Type represents.
201 // The zero Kind is not a valid kind.
234 // rtype is the common implementation of most values.
235 // It is embedded in other, public struct types, but always
236 // with a unique tag like `reflect:"array"` or `reflect:"ptr"`
237 // so that code cannot convert from, say, *arrayType to *ptrType.
239 kind uint8 // enumeration for C
240 align int8 // alignment of variable with this type
241 fieldAlign uint8 // alignment of struct field with this type
242 _ uint8 // unused/padding
243 size uintptr // size in bytes
244 hash uint32 // hash of type; avoids computation in hash tables
246 hashfn func(unsafe.Pointer, uintptr) // hash function
247 equalfn func(unsafe.Pointer, unsafe.Pointer, uintptr) // equality function
249 string *string // string form; unnecessary but undeniably useful
250 *uncommonType // (relatively) uncommon fields
251 ptrToThis *rtype // type for pointer to this type, if used in binary or has methods
254 // Method on non-interface type
256 name *string // name of method
257 pkgPath *string // nil for exported Names; otherwise import path
258 mtyp *rtype // method type (without receiver)
259 typ *rtype // .(*FuncType) underneath (with receiver)
260 tfn unsafe.Pointer // fn used for normal method call
263 // uncommonType is present only for types with names or methods
264 // (if T is a named type, the uncommonTypes for T and *T have methods).
265 // Using a pointer to this struct reduces the overall size required
266 // to describe an unnamed type with no methods.
267 type uncommonType struct {
268 name *string // name of type
269 pkgPath *string // import path; nil for built-in types like int, string
270 methods []method // methods associated with type
273 // ChanDir represents a channel type's direction.
277 RecvDir ChanDir = 1 << iota // <-chan
279 BothDir = RecvDir | SendDir // chan
282 // arrayType represents a fixed array type.
283 type arrayType struct {
284 rtype `reflect:"array"`
285 elem *rtype // array element type
286 slice *rtype // slice type
290 // chanType represents a channel type.
291 type chanType struct {
292 rtype `reflect:"chan"`
293 elem *rtype // channel element type
294 dir uintptr // channel direction (ChanDir)
297 // funcType represents a function type.
298 type funcType struct {
299 rtype `reflect:"func"`
300 dotdotdot bool // last input parameter is ...
301 in []*rtype // input parameter types
302 out []*rtype // output parameter types
305 // imethod represents a method on an interface type
306 type imethod struct {
307 name *string // name of method
308 pkgPath *string // nil for exported Names; otherwise import path
309 typ *rtype // .(*FuncType) underneath
312 // interfaceType represents an interface type.
313 type interfaceType struct {
314 rtype `reflect:"interface"`
315 methods []imethod // sorted by hash
318 // mapType represents a map type.
319 type mapType struct {
320 rtype `reflect:"map"`
321 key *rtype // map key type
322 elem *rtype // map element (value) type
325 // ptrType represents a pointer type.
326 type ptrType struct {
327 rtype `reflect:"ptr"`
328 elem *rtype // pointer element (pointed at) type
331 // sliceType represents a slice type.
332 type sliceType struct {
333 rtype `reflect:"slice"`
334 elem *rtype // slice element type
338 type structField struct {
339 name *string // nil for embedded fields
340 pkgPath *string // nil for exported Names; otherwise import path
341 typ *rtype // type of field
342 tag *string // nil if no tag
343 offset uintptr // byte offset of field within struct
346 // structType represents a struct type.
347 type structType struct {
348 rtype `reflect:"struct"`
349 fields []structField // sorted by offset
353 * The compiler knows the exact layout of all the data structures above.
354 * The compiler does not know about the data structures and methods below.
357 // Method represents a single method.
359 // Name is the method name.
360 // PkgPath is the package path that qualifies a lower case (unexported)
361 // method name. It is empty for upper case (exported) method names.
362 // The combination of PkgPath and Name uniquely identifies a method
364 // See http://golang.org/ref/spec#Uniqueness_of_identifiers
368 Type Type // method type
369 Func Value // func with receiver as first argument
370 Index int // index for Type.Method
373 // High bit says whether type has
374 // embedded pointers,to help garbage collector.
375 const kindMask = 0x7f
377 func (k Kind) String() string {
378 if int(k) < len(kindNames) {
381 return "kind" + strconv.Itoa(int(k))
384 var kindNames = []string{
400 Complex64: "complex64",
401 Complex128: "complex128",
405 Interface: "interface",
411 UnsafePointer: "unsafe.Pointer",
414 func (t *uncommonType) uncommon() *uncommonType {
418 func (t *uncommonType) PkgPath() string {
419 if t == nil || t.pkgPath == nil {
425 func (t *uncommonType) Name() string {
426 if t == nil || t.name == nil {
432 func (t *rtype) rawString() string { return *t.string }
434 func (t *rtype) String() string {
435 // For gccgo, strip out quoted strings.
438 r := make([]byte, len(s))
440 for i := 0; i < len(s); i++ {
451 func (t *rtype) Size() uintptr { return t.size }
453 func (t *rtype) Bits() int {
455 panic("reflect: Bits of nil Type")
458 if k < Int || k > Complex128 {
459 panic("reflect: Bits of non-arithmetic Type " + t.String())
461 return int(t.size) * 8
464 func (t *rtype) Align() int { return int(t.align) }
466 func (t *rtype) FieldAlign() int { return int(t.fieldAlign) }
468 func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }
470 func (t *rtype) common() *rtype { return t }
472 func (t *uncommonType) Method(i int) (m Method) {
473 if t == nil || i < 0 || i >= len(t.methods) {
474 panic("reflect: Method index out of range")
480 fl := flag(Func) << flagKindShift
481 if p.pkgPath != nil {
482 m.PkgPath = *p.pkgPath
487 x := new(unsafe.Pointer)
489 m.Func = Value{mt, unsafe.Pointer(x), fl | flagIndir}
494 func (t *uncommonType) NumMethod() int {
498 return len(t.methods)
501 func (t *uncommonType) MethodByName(name string) (m Method, ok bool) {
506 for i := range t.methods {
508 if p.name != nil && *p.name == name {
509 return t.Method(i), true
515 // TODO(rsc): 6g supplies these, but they are not
516 // as efficient as they could be: they have commonType
517 // as the receiver instead of *rtype.
518 func (t *rtype) NumMethod() int {
519 if t.Kind() == Interface {
520 tt := (*interfaceType)(unsafe.Pointer(t))
521 return tt.NumMethod()
523 return t.uncommonType.NumMethod()
526 func (t *rtype) Method(i int) (m Method) {
527 if t.Kind() == Interface {
528 tt := (*interfaceType)(unsafe.Pointer(t))
531 return t.uncommonType.Method(i)
534 func (t *rtype) MethodByName(name string) (m Method, ok bool) {
535 if t.Kind() == Interface {
536 tt := (*interfaceType)(unsafe.Pointer(t))
537 return tt.MethodByName(name)
539 return t.uncommonType.MethodByName(name)
542 func (t *rtype) PkgPath() string {
543 return t.uncommonType.PkgPath()
546 func (t *rtype) Name() string {
547 return t.uncommonType.Name()
550 func (t *rtype) ChanDir() ChanDir {
551 if t.Kind() != Chan {
552 panic("reflect: ChanDir of non-chan type")
554 tt := (*chanType)(unsafe.Pointer(t))
555 return ChanDir(tt.dir)
558 func (t *rtype) IsVariadic() bool {
559 if t.Kind() != Func {
560 panic("reflect: IsVariadic of non-func type")
562 tt := (*funcType)(unsafe.Pointer(t))
566 func (t *rtype) Elem() Type {
569 tt := (*arrayType)(unsafe.Pointer(t))
570 return toType(tt.elem)
572 tt := (*chanType)(unsafe.Pointer(t))
573 return toType(tt.elem)
575 tt := (*mapType)(unsafe.Pointer(t))
576 return toType(tt.elem)
578 tt := (*ptrType)(unsafe.Pointer(t))
579 return toType(tt.elem)
581 tt := (*sliceType)(unsafe.Pointer(t))
582 return toType(tt.elem)
584 panic("reflect: Elem of invalid type")
587 func (t *rtype) Field(i int) StructField {
588 if t.Kind() != Struct {
589 panic("reflect: Field of non-struct type")
591 tt := (*structType)(unsafe.Pointer(t))
595 func (t *rtype) FieldByIndex(index []int) StructField {
596 if t.Kind() != Struct {
597 panic("reflect: FieldByIndex of non-struct type")
599 tt := (*structType)(unsafe.Pointer(t))
600 return tt.FieldByIndex(index)
603 func (t *rtype) FieldByName(name string) (StructField, bool) {
604 if t.Kind() != Struct {
605 panic("reflect: FieldByName of non-struct type")
607 tt := (*structType)(unsafe.Pointer(t))
608 return tt.FieldByName(name)
611 func (t *rtype) FieldByNameFunc(match func(string) bool) (StructField, bool) {
612 if t.Kind() != Struct {
613 panic("reflect: FieldByNameFunc of non-struct type")
615 tt := (*structType)(unsafe.Pointer(t))
616 return tt.FieldByNameFunc(match)
619 func (t *rtype) In(i int) Type {
620 if t.Kind() != Func {
621 panic("reflect: In of non-func type")
623 tt := (*funcType)(unsafe.Pointer(t))
624 return toType(tt.in[i])
627 func (t *rtype) Key() Type {
629 panic("reflect: Key of non-map type")
631 tt := (*mapType)(unsafe.Pointer(t))
632 return toType(tt.key)
635 func (t *rtype) Len() int {
636 if t.Kind() != Array {
637 panic("reflect: Len of non-array type")
639 tt := (*arrayType)(unsafe.Pointer(t))
643 func (t *rtype) NumField() int {
644 if t.Kind() != Struct {
645 panic("reflect: NumField of non-struct type")
647 tt := (*structType)(unsafe.Pointer(t))
648 return len(tt.fields)
651 func (t *rtype) NumIn() int {
652 if t.Kind() != Func {
653 panic("reflect: NumIn of non-func type")
655 tt := (*funcType)(unsafe.Pointer(t))
659 func (t *rtype) NumOut() int {
660 if t.Kind() != Func {
661 panic("reflect: NumOut of non-func type")
663 tt := (*funcType)(unsafe.Pointer(t))
667 func (t *rtype) Out(i int) Type {
668 if t.Kind() != Func {
669 panic("reflect: Out of non-func type")
671 tt := (*funcType)(unsafe.Pointer(t))
672 return toType(tt.out[i])
675 func (d ChanDir) String() string {
684 return "ChanDir" + strconv.Itoa(int(d))
687 // Method returns the i'th method in the type's method set.
688 func (t *interfaceType) Method(i int) (m Method) {
689 if i < 0 || i >= len(t.methods) {
694 if p.pkgPath != nil {
695 m.PkgPath = *p.pkgPath
697 m.Type = toType(p.typ)
702 // NumMethod returns the number of interface methods in the type's method set.
703 func (t *interfaceType) NumMethod() int { return len(t.methods) }
705 // MethodByName method with the given name in the type's method set.
706 func (t *interfaceType) MethodByName(name string) (m Method, ok bool) {
711 for i := range t.methods {
714 return t.Method(i), true
720 // A StructField describes a single field in a struct.
721 type StructField struct {
722 // Name is the field name.
723 // PkgPath is the package path that qualifies a lower case (unexported)
724 // field name. It is empty for upper case (exported) field names.
725 // See http://golang.org/ref/spec#Uniqueness_of_identifiers
729 Type Type // field type
730 Tag StructTag // field tag string
731 Offset uintptr // offset within struct, in bytes
732 Index []int // index sequence for Type.FieldByIndex
733 Anonymous bool // is an anonymous field
736 // A StructTag is the tag string in a struct field.
738 // By convention, tag strings are a concatenation of
739 // optionally space-separated key:"value" pairs.
740 // Each key is a non-empty string consisting of non-control
741 // characters other than space (U+0020 ' '), quote (U+0022 '"'),
742 // and colon (U+003A ':'). Each value is quoted using U+0022 '"'
743 // characters and Go string literal syntax.
744 type StructTag string
746 // Get returns the value associated with key in the tag string.
747 // If there is no such key in the tag, Get returns the empty string.
748 // If the tag does not have the conventional format, the value
749 // returned by Get is unspecified.
750 func (tag StructTag) Get(key string) string {
752 // skip leading space
754 for i < len(tag) && tag[i] == ' ' {
763 // a space or a quote is a syntax error
765 for i < len(tag) && tag[i] != ' ' && tag[i] != ':' && tag[i] != '"' {
768 if i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' {
771 name := string(tag[:i])
774 // scan quoted string to find value
776 for i < len(tag) && tag[i] != '"' {
785 qvalue := string(tag[:i+1])
789 value, _ := strconv.Unquote(qvalue)
796 // Field returns the i'th struct field.
797 func (t *structType) Field(i int) (f StructField) {
798 if i < 0 || i >= len(t.fields) {
802 f.Type = toType(p.typ)
813 if p.pkgPath != nil {
814 f.PkgPath = *p.pkgPath
817 f.Tag = StructTag(*p.tag)
821 // NOTE(rsc): This is the only allocation in the interface
822 // presented by a reflect.Type. It would be nice to avoid,
823 // at least in the common cases, but we need to make sure
824 // that misbehaving clients of reflect cannot affect other
825 // uses of reflect. One possibility is CL 5371098, but we
826 // postponed that ugliness until there is a demonstrated
827 // need for the performance. This is issue 2320.
832 // TODO(gri): Should there be an error/bool indicator if the index
833 // is wrong for FieldByIndex?
835 // FieldByIndex returns the nested field corresponding to index.
836 func (t *structType) FieldByIndex(index []int) (f StructField) {
837 f.Type = toType(&t.rtype)
838 for i, x := range index {
841 if ft.Kind() == Ptr && ft.Elem().Kind() == Struct {
851 // A fieldScan represents an item on the fieldByNameFunc scan work list.
852 type fieldScan struct {
857 // FieldByNameFunc returns the struct field with a name that satisfies the
858 // match function and a boolean to indicate if the field was found.
859 func (t *structType) FieldByNameFunc(match func(string) bool) (result StructField, ok bool) {
860 // This uses the same condition that the Go language does: there must be a unique instance
861 // of the match at a given depth level. If there are multiple instances of a match at the
862 // same depth, they annihilate each other and inhibit any possible match at a lower level.
863 // The algorithm is breadth first search, one depth level at a time.
865 // The current and next slices are work queues:
866 // current lists the fields to visit on this depth level,
867 // and next lists the fields on the next lower level.
868 current := []fieldScan{}
869 next := []fieldScan{{typ: t}}
871 // nextCount records the number of times an embedded type has been
872 // encountered and considered for queueing in the 'next' slice.
873 // We only queue the first one, but we increment the count on each.
874 // If a struct type T can be reached more than once at a given depth level,
875 // then it annihilates itself and need not be considered at all when we
876 // process that next depth level.
877 var nextCount map[*structType]int
879 // visited records the structs that have been considered already.
880 // Embedded pointer fields can create cycles in the graph of
881 // reachable embedded types; visited avoids following those cycles.
882 // It also avoids duplicated effort: if we didn't find the field in an
883 // embedded type T at level 2, we won't find it in one at level 4 either.
884 visited := map[*structType]bool{}
887 current, next = next, current[:0]
891 // Process all the fields at this depth, now listed in 'current'.
892 // The loop queues embedded fields found in 'next', for processing during the next
893 // iteration. The multiplicity of the 'current' field counts is recorded
894 // in 'count'; the multiplicity of the 'next' field counts is recorded in 'nextCount'.
895 for _, scan := range current {
898 // We've looked through this type before, at a higher level.
899 // That higher level would shadow the lower level we're now at,
900 // so this one can't be useful to us. Ignore it.
904 for i := range t.fields {
906 // Find name and type for field f.
912 // Anonymous field of type T or *T.
913 // Name taken from type.
915 if ntyp.Kind() == Ptr {
916 ntyp = ntyp.Elem().common()
924 if count[t] > 1 || ok {
925 // Name appeared multiple times at this level: annihilate.
926 return StructField{}, false
930 result.Index = append(result.Index, scan.index...)
931 result.Index = append(result.Index, i)
936 // Queue embedded struct fields for processing with next level,
937 // but only if we haven't seen a match yet at this level and only
938 // if the embedded types haven't already been queued.
939 if ok || ntyp == nil || ntyp.Kind() != Struct {
942 ntyp = toType(ntyp).common()
943 styp := (*structType)(unsafe.Pointer(ntyp))
944 if nextCount[styp] > 0 {
945 nextCount[styp] = 2 // exact multiple doesn't matter
948 if nextCount == nil {
949 nextCount = map[*structType]int{}
953 nextCount[styp] = 2 // exact multiple doesn't matter
956 index = append(index, scan.index...)
957 index = append(index, i)
958 next = append(next, fieldScan{styp, index})
968 // FieldByName returns the struct field with the given name
969 // and a boolean to indicate if the field was found.
970 func (t *structType) FieldByName(name string) (f StructField, present bool) {
971 // Quick check for top-level name, or struct without anonymous fields.
974 for i := range t.fields {
980 if *tf.name == name {
981 return t.Field(i), true
988 return t.FieldByNameFunc(func(s string) bool { return s == name })
991 // TypeOf returns the reflection Type of the value in the interface{}.
992 // TypeOf(nil) returns nil.
993 func TypeOf(i interface{}) Type {
994 eface := *(*emptyInterface)(unsafe.Pointer(&i))
995 return toType(eface.typ)
998 // ptrMap is the cache for PtrTo.
1001 m map[*rtype]*ptrType
1004 // PtrTo returns the pointer type with element t.
1005 // For example, if t represents type Foo, PtrTo(t) represents *Foo.
1006 func PtrTo(t Type) Type {
1007 return t.(*rtype).ptrTo()
1010 func (t *rtype) ptrTo() *rtype {
1011 if p := t.ptrToThis; p != nil {
1015 // Otherwise, synthesize one.
1016 // This only happens for pointers with no methods.
1017 // We keep the mapping in a map on the side, because
1018 // this operation is rare and a separate map lets us keep
1019 // the type structures in read-only memory.
1021 if m := ptrMap.m; m != nil {
1022 if p := m[t]; p != nil {
1029 if ptrMap.m == nil {
1030 ptrMap.m = make(map[*rtype]*ptrType)
1034 // some other goroutine won the race and created it
1039 s := "*" + *t.string
1041 canonicalTypeLock.RLock()
1042 r, ok := canonicalType[s]
1043 canonicalTypeLock.RUnlock()
1045 ptrMap.m[t] = (*ptrType)(unsafe.Pointer(r.(*rtype)))
1050 // initialize p using *byte's ptrType as a prototype.
1052 var iptr interface{} = (*unsafe.Pointer)(nil)
1053 prototype := *(**ptrType)(unsafe.Pointer(&iptr))
1058 // For the type structures linked into the binary, the
1059 // compiler provides a good hash of the string.
1060 // Create a good hash for the new string by using
1061 // the FNV-1 hash's mixing function to combine the
1062 // old hash and the new "*".
1063 // p.hash = fnv1(t.hash, '*')
1064 // This is the gccgo version.
1065 p.hash = (t.hash << 4) + 9
1067 p.uncommonType = nil
1071 q := canonicalize(&p.rtype)
1072 p = (*ptrType)(unsafe.Pointer(q.(*rtype)))
1079 // fnv1 incorporates the list of bytes into the hash x using the FNV-1 hash function.
1080 func fnv1(x uint32, list ...byte) uint32 {
1081 for _, b := range list {
1082 x = x*16777619 ^ uint32(b)
1087 func (t *rtype) Implements(u Type) bool {
1089 panic("reflect: nil type passed to Type.Implements")
1091 if u.Kind() != Interface {
1092 panic("reflect: non-interface type passed to Type.Implements")
1094 return implements(u.(*rtype), t)
1097 func (t *rtype) AssignableTo(u Type) bool {
1099 panic("reflect: nil type passed to Type.AssignableTo")
1102 return directlyAssignable(uu, t) || implements(uu, t)
1105 func (t *rtype) ConvertibleTo(u Type) bool {
1107 panic("reflect: nil type passed to Type.ConvertibleTo")
1110 return convertOp(uu, t) != nil
1113 // implements returns true if the type V implements the interface type T.
1114 func implements(T, V *rtype) bool {
1115 if T.Kind() != Interface {
1118 t := (*interfaceType)(unsafe.Pointer(T))
1119 if len(t.methods) == 0 {
1123 // The same algorithm applies in both cases, but the
1124 // method tables for an interface type and a concrete type
1125 // are different, so the code is duplicated.
1126 // In both cases the algorithm is a linear scan over the two
1127 // lists - T's methods and V's methods - simultaneously.
1128 // Since method tables are stored in a unique sorted order
1129 // (alphabetical, with no duplicate method names), the scan
1130 // through V's methods must hit a match for each of T's
1131 // methods along the way, or else V does not implement T.
1132 // This lets us run the scan in overall linear time instead of
1133 // the quadratic time a naive search would require.
1134 // See also ../runtime/iface.c.
1135 if V.Kind() == Interface {
1136 v := (*interfaceType)(unsafe.Pointer(V))
1138 for j := 0; j < len(v.methods); j++ {
1141 if *vm.name == *tm.name && (vm.pkgPath == tm.pkgPath || (vm.pkgPath != nil && tm.pkgPath != nil && *vm.pkgPath == *tm.pkgPath)) && toType(vm.typ).common() == toType(tm.typ).common() {
1142 if i++; i >= len(t.methods) {
1155 for j := 0; j < len(v.methods); j++ {
1158 if *vm.name == *tm.name && (vm.pkgPath == tm.pkgPath || (vm.pkgPath != nil && tm.pkgPath != nil && *vm.pkgPath == *tm.pkgPath)) && toType(vm.mtyp).common() == toType(tm.typ).common() {
1159 if i++; i >= len(t.methods) {
1167 // directlyAssignable returns true if a value x of type V can be directly
1168 // assigned (using memmove) to a value of type T.
1169 // http://golang.org/doc/go_spec.html#Assignability
1170 // Ignoring the interface rules (implemented elsewhere)
1171 // and the ideal constant rules (no ideal constants at run time).
1172 func directlyAssignable(T, V *rtype) bool {
1173 // x's type V is identical to T?
1178 // Otherwise at least one of T and V must be unnamed
1179 // and they must have the same kind.
1180 if T.Name() != "" && V.Name() != "" || T.Kind() != V.Kind() {
1184 // x's type T and V must have identical underlying types.
1185 return haveIdenticalUnderlyingType(T, V)
1188 func haveIdenticalUnderlyingType(T, V *rtype) bool {
1194 if kind != V.Kind() {
1198 // Non-composite types of equal kind have same underlying type
1199 // (the predefined instance of the type).
1200 if Bool <= kind && kind <= Complex128 || kind == String || kind == UnsafePointer {
1207 return T.Elem() == V.Elem() && T.Len() == V.Len()
1211 // x is a bidirectional channel value, T is a channel type,
1212 // and x's type V and T have identical element types.
1213 if V.ChanDir() == BothDir && T.Elem() == V.Elem() {
1217 // Otherwise continue test for identical underlying type.
1218 return V.ChanDir() == T.ChanDir() && T.Elem() == V.Elem()
1221 t := (*funcType)(unsafe.Pointer(T))
1222 v := (*funcType)(unsafe.Pointer(V))
1223 if t.dotdotdot != v.dotdotdot || len(t.in) != len(v.in) || len(t.out) != len(v.out) {
1226 for i, typ := range t.in {
1231 for i, typ := range t.out {
1232 if typ != v.out[i] {
1239 t := (*interfaceType)(unsafe.Pointer(T))
1240 v := (*interfaceType)(unsafe.Pointer(V))
1241 if len(t.methods) == 0 && len(v.methods) == 0 {
1244 // Might have the same methods but still
1245 // need a run time conversion.
1249 return T.Key() == V.Key() && T.Elem() == V.Elem()
1252 return T.Elem() == V.Elem()
1255 t := (*structType)(unsafe.Pointer(T))
1256 v := (*structType)(unsafe.Pointer(V))
1257 if len(t.fields) != len(v.fields) {
1260 for i := range t.fields {
1263 if tf.name != vf.name && (tf.name == nil || vf.name == nil || *tf.name != *vf.name) {
1266 if tf.pkgPath != vf.pkgPath && (tf.pkgPath == nil || vf.pkgPath == nil || *tf.pkgPath != *vf.pkgPath) {
1269 if tf.typ != vf.typ {
1272 if tf.tag != vf.tag && (tf.tag == nil || vf.tag == nil || *tf.tag != *vf.tag) {
1275 if tf.offset != vf.offset {
1285 // The lookupCache caches ChanOf, MapOf, and SliceOf lookups.
1286 var lookupCache struct {
1288 m map[cacheKey]*rtype
1291 // A cacheKey is the key for use in the lookupCache.
1292 // Four values describe any of the types we are looking for:
1293 // type kind, one or two subtypes, and an extra integer.
1294 type cacheKey struct {
1301 // cacheGet looks for a type under the key k in the lookupCache.
1302 // If it finds one, it returns that type.
1303 // If not, it returns nil with the cache locked.
1304 // The caller is expected to use cachePut to unlock the cache.
1305 func cacheGet(k cacheKey) Type {
1307 t := lookupCache.m[k]
1308 lookupCache.RUnlock()
1314 t = lookupCache.m[k]
1316 lookupCache.Unlock()
1320 if lookupCache.m == nil {
1321 lookupCache.m = make(map[cacheKey]*rtype)
1327 // cachePut stores the given type in the cache, unlocks the cache,
1328 // and returns the type. It is expected that the cache is locked
1329 // because cacheGet returned nil.
1330 func cachePut(k cacheKey, t *rtype) Type {
1331 t = toType(t).common()
1332 lookupCache.m[k] = t
1333 lookupCache.Unlock()
1337 // ChanOf returns the channel type with the given direction and element type.
1338 // For example, if t represents int, ChanOf(RecvDir, t) represents <-chan int.
1340 // The gc runtime imposes a limit of 64 kB on channel element types.
1341 // If t's size is equal to or exceeds this limit, ChanOf panics.
1342 func ChanOf(dir ChanDir, t Type) Type {
1346 ckey := cacheKey{Chan, typ, nil, uintptr(dir)}
1347 if ch := cacheGet(ckey); ch != nil {
1351 // This restriction is imposed by the gc compiler and the runtime.
1352 if typ.size >= 1<<16 {
1353 lookupCache.Unlock()
1354 panic("reflect.ChanOf: element size too large")
1357 // Look in known types.
1358 // TODO: Precedence when constructing string.
1362 lookupCache.Unlock()
1363 panic("reflect.ChanOf: invalid dir")
1365 s = "chan<- " + *typ.string
1367 s = "<-chan " + *typ.string
1369 s = "chan " + *typ.string
1372 // Make a channel type.
1373 var ichan interface{} = (chan unsafe.Pointer)(nil)
1374 prototype := *(**chanType)(unsafe.Pointer(&ichan))
1379 // gccgo uses a different hash.
1380 // ch.hash = fnv1(typ.hash, 'c', byte(dir))
1382 if dir&SendDir != 0 {
1385 if dir&RecvDir != 0 {
1388 ch.hash += typ.hash << 2
1393 ch.uncommonType = nil
1396 return cachePut(ckey, &ch.rtype)
1399 // MapOf returns the map type with the given key and element types.
1400 // For example, if k represents int and e represents string,
1401 // MapOf(k, e) represents map[int]string.
1403 // If the key type is not a valid map key type (that is, if it does
1404 // not implement Go's == operator), MapOf panics. TODO(rsc).
1405 func MapOf(key, elem Type) Type {
1406 ktyp := key.(*rtype)
1407 etyp := elem.(*rtype)
1409 // TODO: Check for invalid key types.
1412 ckey := cacheKey{Map, ktyp, etyp, 0}
1413 if mt := cacheGet(ckey); mt != nil {
1417 // Look in known types.
1418 s := "map[" + *ktyp.string + "]" + *etyp.string
1421 var imap interface{} = (map[unsafe.Pointer]unsafe.Pointer)(nil)
1422 prototype := *(**mapType)(unsafe.Pointer(&imap))
1427 // gccgo uses a different hash
1428 // mt.hash = fnv1(etyp.hash, 'm', byte(ktyp.hash>>24), byte(ktyp.hash>>16), byte(ktyp.hash>>8), byte(ktyp.hash))
1429 mt.hash = ktyp.hash + etyp.hash + 2 + 14
1433 mt.uncommonType = nil
1436 return cachePut(ckey, &mt.rtype)
1439 // SliceOf returns the slice type with element type t.
1440 // For example, if t represents int, SliceOf(t) represents []int.
1441 func SliceOf(t Type) Type {
1445 ckey := cacheKey{Slice, typ, nil, 0}
1446 if slice := cacheGet(ckey); slice != nil {
1450 // Look in known types.
1451 s := "[]" + *typ.string
1453 // Make a slice type.
1454 var islice interface{} = ([]unsafe.Pointer)(nil)
1455 prototype := *(**sliceType)(unsafe.Pointer(&islice))
1456 slice := new(sliceType)
1460 // gccgo uses a different hash.
1461 // slice.hash = fnv1(typ.hash, '[')
1462 slice.hash = typ.hash + 1 + 13
1465 slice.uncommonType = nil
1466 slice.ptrToThis = nil
1468 return cachePut(ckey, &slice.rtype)
1471 // ArrayOf returns the array type with the given count and element type.
1472 // For example, if t represents int, ArrayOf(5, t) represents [5]int.
1474 // If the resulting type would be larger than the available address space,
1477 // TODO(rsc): Unexported for now. Export once the alg field is set correctly
1478 // for the type. This may require significant work.
1479 func arrayOf(count int, elem Type) Type {
1480 typ := elem.(*rtype)
1481 slice := SliceOf(elem)
1484 ckey := cacheKey{Array, typ, nil, uintptr(count)}
1485 if slice := cacheGet(ckey); slice != nil {
1489 // Look in known types.
1490 s := "[" + strconv.Itoa(count) + "]" + *typ.string
1492 // Make an array type.
1493 var iarray interface{} = [1]unsafe.Pointer{}
1494 prototype := *(**arrayType)(unsafe.Pointer(&iarray))
1495 array := new(arrayType)
1499 // gccgo uses a different hash.
1500 // array.hash = fnv1(typ.hash, '[')
1501 // for n := uint32(count); n > 0; n >>= 8 {
1502 // array.hash = fnv1(array.hash, byte(n))
1504 // array.hash = fnv1(array.hash, ']')
1505 array.hash = typ.hash + 1 + 13
1508 max := ^uintptr(0) / typ.size
1509 if uintptr(count) > max {
1510 panic("reflect.ArrayOf: array size would exceed virtual address space")
1512 array.size = typ.size * uintptr(count)
1513 array.align = typ.align
1514 array.fieldAlign = typ.fieldAlign
1517 array.uncommonType = nil
1518 array.ptrToThis = nil
1519 array.len = uintptr(count)
1520 array.slice = slice.(*rtype)
1522 return cachePut(ckey, &array.rtype)
1525 // toType converts from a *rtype to a Type that can be returned
1526 // to the client of package reflect. In gc, the only concern is that
1527 // a nil *rtype must be replaced by a nil Type, but in gccgo this
1528 // function takes care of ensuring that multiple *rtype for the same
1529 // type are coalesced into a single Type.
1530 var canonicalType = make(map[string]Type)
1532 var canonicalTypeLock sync.RWMutex
1534 func canonicalize(t Type) Type {
1540 if u == nil || u.PkgPath() == "" {
1543 s = u.PkgPath() + "." + u.Name()
1545 canonicalTypeLock.RLock()
1546 if r, ok := canonicalType[s]; ok {
1547 canonicalTypeLock.RUnlock()
1550 canonicalTypeLock.RUnlock()
1551 canonicalTypeLock.Lock()
1552 if r, ok := canonicalType[s]; ok {
1553 canonicalTypeLock.Unlock()
1556 canonicalType[s] = t
1557 canonicalTypeLock.Unlock()
1561 func toType(p *rtype) Type {
1565 return canonicalize(p)