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.
17 // userTypeInfo stores the information associated with a type the user has handed
18 // to the package. It's computed once and stored in a map keyed by reflection
20 type userTypeInfo struct {
21 user reflect.Type // the type the user handed us
22 base reflect.Type // the base type after all indirections
23 indir int // number of indirections to reach the base type
24 isGobEncoder bool // does the type implement GobEncoder?
25 isGobDecoder bool // does the type implement GobDecoder?
26 encIndir int8 // number of indirections to reach the receiver type; may be negative
27 decIndir int8 // number of indirections to reach the receiver type; may be negative
31 // Protected by an RWMutex because we read it a lot and write
32 // it only when we see a new type, typically when compiling.
33 userTypeLock sync.RWMutex
34 userTypeCache = make(map[reflect.Type]*userTypeInfo)
37 // validType returns, and saves, the information associated with user-provided type rt.
38 // If the user type is not valid, err will be non-nil. To be used when the error handler
40 func validUserType(rt reflect.Type) (ut *userTypeInfo, err error) {
42 ut = userTypeCache[rt]
43 userTypeLock.RUnlock()
47 // Now set the value under the write lock.
49 defer userTypeLock.Unlock()
50 if ut = userTypeCache[rt]; ut != nil {
51 // Lost the race; not a problem.
54 ut = new(userTypeInfo)
57 // A type that is just a cycle of pointers (such as type T *T) cannot
58 // be represented in gobs, which need some concrete data. We use a
59 // cycle detection algorithm from Knuth, Vol 2, Section 3.1, Ex 6,
60 // pp 539-540. As we step through indirections, run another type at
61 // half speed. If they meet up, there's a cycle.
62 slowpoke := ut.base // walks half as fast as ut.base
65 if pt.Kind() != reflect.Ptr {
69 if ut.base == slowpoke { // ut.base lapped slowpoke
70 // recursive pointer type.
71 return nil, errors.New("can't represent recursive pointer type " + ut.base.String())
74 slowpoke = slowpoke.Elem()
78 ut.isGobEncoder, ut.encIndir = implementsInterface(ut.user, gobEncoderInterfaceType)
79 ut.isGobDecoder, ut.decIndir = implementsInterface(ut.user, gobDecoderInterfaceType)
80 userTypeCache[rt] = ut
85 gobEncoderInterfaceType = reflect.TypeOf((*GobEncoder)(nil)).Elem()
86 gobDecoderInterfaceType = reflect.TypeOf((*GobDecoder)(nil)).Elem()
89 // implementsInterface reports whether the type implements the
90 // gobEncoder/gobDecoder interface.
91 // It also returns the number of indirections required to get to the
93 func implementsInterface(typ, gobEncDecType reflect.Type) (success bool, indir int8) {
98 // The type might be a pointer and we need to keep
99 // dereferencing to the base type until we find an implementation.
101 if rt.Implements(gobEncDecType) {
104 if p := rt; p.Kind() == reflect.Ptr {
106 if indir > 100 { // insane number of indirections
114 // No luck yet, but if this is a base type (non-pointer), the pointer might satisfy.
115 if typ.Kind() != reflect.Ptr {
116 // Not a pointer, but does the pointer work?
117 if reflect.PtrTo(typ).Implements(gobEncDecType) {
124 // userType returns, and saves, the information associated with user-provided type rt.
125 // If the user type is not valid, it calls error.
126 func userType(rt reflect.Type) *userTypeInfo {
127 ut, err := validUserType(rt)
133 // A typeId represents a gob Type as an integer that can be passed on the wire.
134 // Internally, typeIds are used as keys to a map to recover the underlying type info.
137 var nextId typeId // incremented for each new type we build
138 var typeLock sync.Mutex // set while building a type
139 const firstUserId = 64 // lowest id number granted to user
141 type gobType interface {
145 string() string // not public; only for debugging
146 safeString(seen map[typeId]bool) string
149 var types = make(map[reflect.Type]gobType)
150 var idToType = make(map[typeId]gobType)
151 var builtinIdToType map[typeId]gobType // set in init() after builtins are established
153 func setTypeId(typ gobType) {
156 idToType[nextId] = typ
159 func (t typeId) gobType() gobType {
166 // string returns the string representation of the type associated with the typeId.
167 func (t typeId) string() string {
168 if t.gobType() == nil {
171 return t.gobType().string()
174 // Name returns the name of the type associated with the typeId.
175 func (t typeId) name() string {
176 if t.gobType() == nil {
179 return t.gobType().name()
182 // Common elements of all types.
183 type CommonType struct {
188 func (t *CommonType) id() typeId { return t.Id }
190 func (t *CommonType) setId(id typeId) { t.Id = id }
192 func (t *CommonType) string() string { return t.Name }
194 func (t *CommonType) safeString(seen map[typeId]bool) string {
198 func (t *CommonType) name() string { return t.Name }
200 // Create and check predefined types
201 // The string for tBytes is "bytes" not "[]byte" to signify its specialness.
204 // Primordial types, needed during initialization.
205 // Always passed as pointers so the interface{} type
206 // goes through without losing its interfaceness.
207 tBool = bootstrapType("bool", (*bool)(nil), 1)
208 tInt = bootstrapType("int", (*int)(nil), 2)
209 tUint = bootstrapType("uint", (*uint)(nil), 3)
210 tFloat = bootstrapType("float", (*float64)(nil), 4)
211 tBytes = bootstrapType("bytes", (*[]byte)(nil), 5)
212 tString = bootstrapType("string", (*string)(nil), 6)
213 tComplex = bootstrapType("complex", (*complex128)(nil), 7)
214 tInterface = bootstrapType("interface", (*interface{})(nil), 8)
215 // Reserve some Ids for compatible expansion
216 tReserved7 = bootstrapType("_reserved1", (*struct{ r7 int })(nil), 9)
217 tReserved6 = bootstrapType("_reserved1", (*struct{ r6 int })(nil), 10)
218 tReserved5 = bootstrapType("_reserved1", (*struct{ r5 int })(nil), 11)
219 tReserved4 = bootstrapType("_reserved1", (*struct{ r4 int })(nil), 12)
220 tReserved3 = bootstrapType("_reserved1", (*struct{ r3 int })(nil), 13)
221 tReserved2 = bootstrapType("_reserved1", (*struct{ r2 int })(nil), 14)
222 tReserved1 = bootstrapType("_reserved1", (*struct{ r1 int })(nil), 15)
225 // Predefined because it's needed by the Decoder
226 var tWireType = mustGetTypeInfo(reflect.TypeOf(wireType{})).id
227 var wireTypeUserInfo *userTypeInfo // userTypeInfo of (*wireType)
230 // Some magic numbers to make sure there are no surprises.
231 checkId(16, tWireType)
232 checkId(17, mustGetTypeInfo(reflect.TypeOf(arrayType{})).id)
233 checkId(18, mustGetTypeInfo(reflect.TypeOf(CommonType{})).id)
234 checkId(19, mustGetTypeInfo(reflect.TypeOf(sliceType{})).id)
235 checkId(20, mustGetTypeInfo(reflect.TypeOf(structType{})).id)
236 checkId(21, mustGetTypeInfo(reflect.TypeOf(fieldType{})).id)
237 checkId(23, mustGetTypeInfo(reflect.TypeOf(mapType{})).id)
239 builtinIdToType = make(map[typeId]gobType)
240 for k, v := range idToType {
241 builtinIdToType[k] = v
244 // Move the id space upwards to allow for growth in the predefined world
245 // without breaking existing files.
246 if nextId > firstUserId {
247 panic(fmt.Sprintln("nextId too large:", nextId))
251 wireTypeUserInfo = userType(reflect.TypeOf((*wireType)(nil)))
255 type arrayType struct {
261 func newArrayType(name string) *arrayType {
262 a := &arrayType{CommonType{Name: name}, 0, 0}
266 func (a *arrayType) init(elem gobType, len int) {
267 // Set our type id before evaluating the element's, in case it's our own.
273 func (a *arrayType) safeString(seen map[typeId]bool) string {
278 return fmt.Sprintf("[%d]%s", a.Len, a.Elem.gobType().safeString(seen))
281 func (a *arrayType) string() string { return a.safeString(make(map[typeId]bool)) }
283 // GobEncoder type (something that implements the GobEncoder interface)
284 type gobEncoderType struct {
288 func newGobEncoderType(name string) *gobEncoderType {
289 g := &gobEncoderType{CommonType{Name: name}}
294 func (g *gobEncoderType) safeString(seen map[typeId]bool) string {
298 func (g *gobEncoderType) string() string { return g.Name }
301 type mapType struct {
307 func newMapType(name string) *mapType {
308 m := &mapType{CommonType{Name: name}, 0, 0}
312 func (m *mapType) init(key, elem gobType) {
313 // Set our type id before evaluating the element's, in case it's our own.
319 func (m *mapType) safeString(seen map[typeId]bool) string {
324 key := m.Key.gobType().safeString(seen)
325 elem := m.Elem.gobType().safeString(seen)
326 return fmt.Sprintf("map[%s]%s", key, elem)
329 func (m *mapType) string() string { return m.safeString(make(map[typeId]bool)) }
332 type sliceType struct {
337 func newSliceType(name string) *sliceType {
338 s := &sliceType{CommonType{Name: name}, 0}
342 func (s *sliceType) init(elem gobType) {
343 // Set our type id before evaluating the element's, in case it's our own.
348 func (s *sliceType) safeString(seen map[typeId]bool) string {
353 return fmt.Sprintf("[]%s", s.Elem.gobType().safeString(seen))
356 func (s *sliceType) string() string { return s.safeString(make(map[typeId]bool)) }
359 type fieldType struct {
364 type structType struct {
369 func (s *structType) safeString(seen map[typeId]bool) string {
373 if _, ok := seen[s.Id]; ok {
377 str := s.Name + " = struct { "
378 for _, f := range s.Field {
379 str += fmt.Sprintf("%s %s; ", f.Name, f.Id.gobType().safeString(seen))
385 func (s *structType) string() string { return s.safeString(make(map[typeId]bool)) }
387 func newStructType(name string) *structType {
388 s := &structType{CommonType{Name: name}, nil}
389 // For historical reasons we set the id here rather than init.
390 // See the comment in newTypeObject for details.
395 // newTypeObject allocates a gobType for the reflection type rt.
396 // Unless ut represents a GobEncoder, rt should be the base type
398 // This is only called from the encoding side. The decoding side
399 // works through typeIds and userTypeInfos alone.
400 func newTypeObject(name string, ut *userTypeInfo, rt reflect.Type) (gobType, error) {
401 // Does this type implement GobEncoder?
403 return newGobEncoderType(name), nil
406 var type0, type1 gobType
412 // Install the top-level type before the subtypes (e.g. struct before
413 // fields) so recursive types can be constructed safely.
414 switch t := rt; t.Kind() {
415 // All basic types are easy: they are predefined.
417 return tBool.gobType(), nil
419 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
420 return tInt.gobType(), nil
422 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
423 return tUint.gobType(), nil
425 case reflect.Float32, reflect.Float64:
426 return tFloat.gobType(), nil
428 case reflect.Complex64, reflect.Complex128:
429 return tComplex.gobType(), nil
432 return tString.gobType(), nil
434 case reflect.Interface:
435 return tInterface.gobType(), nil
438 at := newArrayType(name)
440 type0, err = getBaseType("", t.Elem())
445 // For arrays, maps, and slices, we set the type id after the elements
446 // are constructed. This is to retain the order of type id allocation after
447 // a fix made to handle recursive types, which changed the order in
448 // which types are built. Delaying the setting in this way preserves
449 // type ids while allowing recursive types to be described. Structs,
450 // done below, were already handling recursion correctly so they
451 // assign the top-level id before those of the field.
452 at.init(type0, t.Len())
456 mt := newMapType(name)
458 type0, err = getBaseType("", t.Key())
462 type1, err = getBaseType("", t.Elem())
466 mt.init(type0, type1)
470 // []byte == []uint8 is a special case
471 if t.Elem().Kind() == reflect.Uint8 {
472 return tBytes.gobType(), nil
474 st := newSliceType(name)
476 type0, err = getBaseType(t.Elem().Name(), t.Elem())
484 st := newStructType(name)
486 idToType[st.id()] = st
487 for i := 0; i < t.NumField(); i++ {
489 if !isExported(f.Name) {
492 typ := userType(f.Type).base
495 t := userType(f.Type).base
498 gt, err := getBaseType(tname, f.Type)
502 st.Field = append(st.Field, &fieldType{f.Name, gt.id()})
507 return nil, errors.New("gob NewTypeObject can't handle type: " + rt.String())
512 // isExported reports whether this is an exported - upper case - name.
513 func isExported(name string) bool {
514 rune, _ := utf8.DecodeRuneInString(name)
515 return unicode.IsUpper(rune)
518 // getBaseType returns the Gob type describing the given reflect.Type's base type.
519 // typeLock must be held.
520 func getBaseType(name string, rt reflect.Type) (gobType, error) {
522 return getType(name, ut, ut.base)
525 // getType returns the Gob type describing the given reflect.Type.
526 // Should be called only when handling GobEncoders/Decoders,
527 // which may be pointers. All other types are handled through the
528 // base type, never a pointer.
529 // typeLock must be held.
530 func getType(name string, ut *userTypeInfo, rt reflect.Type) (gobType, error) {
531 typ, present := types[rt]
535 typ, err := newTypeObject(name, ut, rt)
542 func checkId(want, got typeId) {
544 fmt.Fprintf(os.Stderr, "checkId: %d should be %d\n", int(got), int(want))
545 panic("bootstrap type wrong id: " + got.name() + " " + got.string() + " not " + want.string())
549 // used for building the basic types; called only from init(). the incoming
550 // interface always refers to a pointer.
551 func bootstrapType(name string, e interface{}, expect typeId) typeId {
552 rt := reflect.TypeOf(e).Elem()
553 _, present := types[rt]
555 panic("bootstrap type already present: " + name + ", " + rt.String())
557 typ := &CommonType{Name: name}
560 checkId(expect, nextId)
561 userType(rt) // might as well cache it now
565 // Representation of the information we send and receive about this type.
566 // Each value we send is preceded by its type definition: an encoded int.
567 // However, the very first time we send the value, we first send the pair
569 // For bootstrapping purposes, we assume that the recipient knows how
570 // to decode a wireType; it is exactly the wireType struct here, interpreted
571 // using the gob rules for sending a structure, except that we assume the
572 // ids for wireType and structType etc. are known. The relevant pieces
573 // are built in encode.go's init() function.
574 // To maintain binary compatibility, if you extend this type, always put
575 // the new fields last.
576 type wireType struct {
581 GobEncoderT *gobEncoderType
584 func (w *wireType) string() string {
585 const unknown = "unknown type"
590 case w.ArrayT != nil:
592 case w.SliceT != nil:
594 case w.StructT != nil:
595 return w.StructT.Name
598 case w.GobEncoderT != nil:
599 return w.GobEncoderT.Name
604 type typeInfo struct {
610 var typeInfoMap = make(map[reflect.Type]*typeInfo) // protected by typeLock
612 // typeLock must be held.
613 func getTypeInfo(ut *userTypeInfo) (*typeInfo, error) {
616 // We want the user type, not the base type.
619 info, ok := typeInfoMap[rt]
624 gt, err := getBaseType(rt.Name(), rt)
631 userType, err := getType(rt.Name(), ut, rt)
635 info.wire = &wireType{GobEncoderT: userType.id().gobType().(*gobEncoderType)}
636 typeInfoMap[ut.user] = info
640 t := info.id.gobType()
641 switch typ := rt; typ.Kind() {
643 info.wire = &wireType{ArrayT: t.(*arrayType)}
645 info.wire = &wireType{MapT: t.(*mapType)}
647 // []byte == []uint8 is a special case handled separately
648 if typ.Elem().Kind() != reflect.Uint8 {
649 info.wire = &wireType{SliceT: t.(*sliceType)}
652 info.wire = &wireType{StructT: t.(*structType)}
654 typeInfoMap[rt] = info
658 // Called only when a panic is acceptable and unexpected.
659 func mustGetTypeInfo(rt reflect.Type) *typeInfo {
660 t, err := getTypeInfo(userType(rt))
662 panic("getTypeInfo: " + err.Error())
667 // GobEncoder is the interface describing data that provides its own
668 // representation for encoding values for transmission to a GobDecoder.
669 // A type that implements GobEncoder and GobDecoder has complete
670 // control over the representation of its data and may therefore
671 // contain things such as private fields, channels, and functions,
672 // which are not usually transmissible in gob streams.
674 // Note: Since gobs can be stored permanently, It is good design
675 // to guarantee the encoding used by a GobEncoder is stable as the
676 // software evolves. For instance, it might make sense for GobEncode
677 // to include a version number in the encoding.
678 type GobEncoder interface {
679 // GobEncode returns a byte slice representing the encoding of the
680 // receiver for transmission to a GobDecoder, usually of the same
682 GobEncode() ([]byte, error)
685 // GobDecoder is the interface describing data that provides its own
686 // routine for decoding transmitted values sent by a GobEncoder.
687 type GobDecoder interface {
688 // GobDecode overwrites the receiver, which must be a pointer,
689 // with the value represented by the byte slice, which was written
690 // by GobEncode, usually for the same concrete type.
691 GobDecode([]byte) error
695 nameToConcreteType = make(map[string]reflect.Type)
696 concreteTypeToName = make(map[reflect.Type]string)
699 // RegisterName is like Register but uses the provided name rather than the
701 func RegisterName(name string, value interface{}) {
704 panic("attempt to register empty name")
706 ut := userType(reflect.TypeOf(value))
707 // Check for incompatible duplicates. The name must refer to the
708 // same user type, and vice versa.
709 if t, ok := nameToConcreteType[name]; ok && t != ut.user {
710 panic(fmt.Sprintf("gob: registering duplicate types for %q: %s != %s", name, t, ut.user))
712 if n, ok := concreteTypeToName[ut.base]; ok && n != name {
713 panic(fmt.Sprintf("gob: registering duplicate names for %s: %q != %q", ut.user, n, name))
715 // Store the name and type provided by the user....
716 nameToConcreteType[name] = reflect.TypeOf(value)
717 // but the flattened type in the type table, since that's what decode needs.
718 concreteTypeToName[ut.base] = name
721 // Register records a type, identified by a value for that type, under its
722 // internal type name. That name will identify the concrete type of a value
723 // sent or received as an interface variable. Only types that will be
724 // transferred as implementations of interface values need to be registered.
725 // Expecting to be used only during initialization, it panics if the mapping
726 // between types and names is not a bijection.
727 func Register(value interface{}) {
728 // Default to printed representation for unnamed types
729 rt := reflect.TypeOf(value)
732 // But for named types (or pointers to them), qualify with import path.
733 // Dereference one pointer looking for a named type.
736 if pt := rt; pt.Kind() == reflect.Ptr {
742 if rt.PkgPath() == "" {
743 name = star + rt.Name()
745 name = star + rt.PkgPath() + "." + rt.Name()
749 RegisterName(name, value)
752 func registerBasics() {
765 Register(complex64(0i))
766 Register(complex128(0i))
770 Register([]byte(nil))
772 Register([]int8(nil))
773 Register([]int16(nil))
774 Register([]int32(nil))
775 Register([]int64(nil))
776 Register([]uint(nil))
777 Register([]uint8(nil))
778 Register([]uint16(nil))
779 Register([]uint32(nil))
780 Register([]uint64(nil))
781 Register([]float32(nil))
782 Register([]float64(nil))
783 Register([]complex64(nil))
784 Register([]complex128(nil))
785 Register([]uintptr(nil))
786 Register([]bool(nil))
787 Register([]string(nil))