OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / gob / decode.go
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.
4
5 package gob
6
7 // TODO(rsc): When garbage collector changes, revisit
8 // the allocations in this file that use unsafe.Pointer.
9
10 import (
11         "bytes"
12         "io"
13         "math"
14         "os"
15         "reflect"
16         "unsafe"
17 )
18
19 var (
20         errBadUint = os.ErrorString("gob: encoded unsigned integer out of range")
21         errBadType = os.ErrorString("gob: unknown type id or corrupted data")
22         errRange   = os.ErrorString("gob: bad data: field numbers out of bounds")
23 )
24
25 // decoderState is the execution state of an instance of the decoder. A new state
26 // is created for nested objects.
27 type decoderState struct {
28         dec *Decoder
29         // The buffer is stored with an extra indirection because it may be replaced
30         // if we load a type during decode (when reading an interface value).
31         b        *bytes.Buffer
32         fieldnum int // the last field number read.
33         buf      []byte
34         next     *decoderState // for free list
35 }
36
37 // We pass the bytes.Buffer separately for easier testing of the infrastructure
38 // without requiring a full Decoder.
39 func (dec *Decoder) newDecoderState(buf *bytes.Buffer) *decoderState {
40         d := dec.freeList
41         if d == nil {
42                 d = new(decoderState)
43                 d.dec = dec
44                 d.buf = make([]byte, uint64Size)
45         } else {
46                 dec.freeList = d.next
47         }
48         d.b = buf
49         return d
50 }
51
52 func (dec *Decoder) freeDecoderState(d *decoderState) {
53         d.next = dec.freeList
54         dec.freeList = d
55 }
56
57 func overflow(name string) os.ErrorString {
58         return os.ErrorString(`value for "` + name + `" out of range`)
59 }
60
61 // decodeUintReader reads an encoded unsigned integer from an io.Reader.
62 // Used only by the Decoder to read the message length.
63 func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Error) {
64         width = 1
65         _, err = r.Read(buf[0:width])
66         if err != nil {
67                 return
68         }
69         b := buf[0]
70         if b <= 0x7f {
71                 return uint64(b), width, nil
72         }
73         nb := -int(int8(b))
74         if nb > uint64Size {
75                 err = errBadUint
76                 return
77         }
78         var n int
79         n, err = io.ReadFull(r, buf[0:nb])
80         if err != nil {
81                 if err == os.EOF {
82                         err = io.ErrUnexpectedEOF
83                 }
84                 return
85         }
86         // Could check that the high byte is zero but it's not worth it.
87         for i := 0; i < n; i++ {
88                 x <<= 8
89                 x |= uint64(buf[i])
90                 width++
91         }
92         return
93 }
94
95 // decodeUint reads an encoded unsigned integer from state.r.
96 // Does not check for overflow.
97 func (state *decoderState) decodeUint() (x uint64) {
98         b, err := state.b.ReadByte()
99         if err != nil {
100                 error(err)
101         }
102         if b <= 0x7f {
103                 return uint64(b)
104         }
105         nb := -int(int8(b))
106         if nb > uint64Size {
107                 error(errBadUint)
108         }
109         n, err := state.b.Read(state.buf[0:nb])
110         if err != nil {
111                 error(err)
112         }
113         // Don't need to check error; it's safe to loop regardless.
114         // Could check that the high byte is zero but it's not worth it.
115         for i := 0; i < n; i++ {
116                 x <<= 8
117                 x |= uint64(state.buf[i])
118         }
119         return x
120 }
121
122 // decodeInt reads an encoded signed integer from state.r.
123 // Does not check for overflow.
124 func (state *decoderState) decodeInt() int64 {
125         x := state.decodeUint()
126         if x&1 != 0 {
127                 return ^int64(x >> 1)
128         }
129         return int64(x >> 1)
130 }
131
132 // decOp is the signature of a decoding operator for a given type.
133 type decOp func(i *decInstr, state *decoderState, p unsafe.Pointer)
134
135 // The 'instructions' of the decoding machine
136 type decInstr struct {
137         op     decOp
138         field  int            // field number of the wire type
139         indir  int            // how many pointer indirections to reach the value in the struct
140         offset uintptr        // offset in the structure of the field to encode
141         ovfl   os.ErrorString // error message for overflow/underflow (for arrays, of the elements)
142 }
143
144 // Since the encoder writes no zeros, if we arrive at a decoder we have
145 // a value to extract and store.  The field number has already been read
146 // (it's how we knew to call this decoder).
147 // Each decoder is responsible for handling any indirections associated
148 // with the data structure.  If any pointer so reached is nil, allocation must
149 // be done.
150
151 // Walk the pointer hierarchy, allocating if we find a nil.  Stop one before the end.
152 func decIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
153         for ; indir > 1; indir-- {
154                 if *(*unsafe.Pointer)(p) == nil {
155                         // Allocation required
156                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(unsafe.Pointer))
157                 }
158                 p = *(*unsafe.Pointer)(p)
159         }
160         return p
161 }
162
163 // ignoreUint discards a uint value with no destination.
164 func ignoreUint(i *decInstr, state *decoderState, p unsafe.Pointer) {
165         state.decodeUint()
166 }
167
168 // ignoreTwoUints discards a uint value with no destination. It's used to skip
169 // complex values.
170 func ignoreTwoUints(i *decInstr, state *decoderState, p unsafe.Pointer) {
171         state.decodeUint()
172         state.decodeUint()
173 }
174
175 // decBool decodes a uiint and stores it as a boolean through p.
176 func decBool(i *decInstr, state *decoderState, p unsafe.Pointer) {
177         if i.indir > 0 {
178                 if *(*unsafe.Pointer)(p) == nil {
179                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(bool))
180                 }
181                 p = *(*unsafe.Pointer)(p)
182         }
183         *(*bool)(p) = state.decodeUint() != 0
184 }
185
186 // decInt8 decodes an integer and stores it as an int8 through p.
187 func decInt8(i *decInstr, state *decoderState, p unsafe.Pointer) {
188         if i.indir > 0 {
189                 if *(*unsafe.Pointer)(p) == nil {
190                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int8))
191                 }
192                 p = *(*unsafe.Pointer)(p)
193         }
194         v := state.decodeInt()
195         if v < math.MinInt8 || math.MaxInt8 < v {
196                 error(i.ovfl)
197         } else {
198                 *(*int8)(p) = int8(v)
199         }
200 }
201
202 // decUint8 decodes an unsigned integer and stores it as a uint8 through p.
203 func decUint8(i *decInstr, state *decoderState, p unsafe.Pointer) {
204         if i.indir > 0 {
205                 if *(*unsafe.Pointer)(p) == nil {
206                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint8))
207                 }
208                 p = *(*unsafe.Pointer)(p)
209         }
210         v := state.decodeUint()
211         if math.MaxUint8 < v {
212                 error(i.ovfl)
213         } else {
214                 *(*uint8)(p) = uint8(v)
215         }
216 }
217
218 // decInt16 decodes an integer and stores it as an int16 through p.
219 func decInt16(i *decInstr, state *decoderState, p unsafe.Pointer) {
220         if i.indir > 0 {
221                 if *(*unsafe.Pointer)(p) == nil {
222                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int16))
223                 }
224                 p = *(*unsafe.Pointer)(p)
225         }
226         v := state.decodeInt()
227         if v < math.MinInt16 || math.MaxInt16 < v {
228                 error(i.ovfl)
229         } else {
230                 *(*int16)(p) = int16(v)
231         }
232 }
233
234 // decUint16 decodes an unsigned integer and stores it as a uint16 through p.
235 func decUint16(i *decInstr, state *decoderState, p unsafe.Pointer) {
236         if i.indir > 0 {
237                 if *(*unsafe.Pointer)(p) == nil {
238                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint16))
239                 }
240                 p = *(*unsafe.Pointer)(p)
241         }
242         v := state.decodeUint()
243         if math.MaxUint16 < v {
244                 error(i.ovfl)
245         } else {
246                 *(*uint16)(p) = uint16(v)
247         }
248 }
249
250 // decInt32 decodes an integer and stores it as an int32 through p.
251 func decInt32(i *decInstr, state *decoderState, p unsafe.Pointer) {
252         if i.indir > 0 {
253                 if *(*unsafe.Pointer)(p) == nil {
254                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int32))
255                 }
256                 p = *(*unsafe.Pointer)(p)
257         }
258         v := state.decodeInt()
259         if v < math.MinInt32 || math.MaxInt32 < v {
260                 error(i.ovfl)
261         } else {
262                 *(*int32)(p) = int32(v)
263         }
264 }
265
266 // decUint32 decodes an unsigned integer and stores it as a uint32 through p.
267 func decUint32(i *decInstr, state *decoderState, p unsafe.Pointer) {
268         if i.indir > 0 {
269                 if *(*unsafe.Pointer)(p) == nil {
270                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint32))
271                 }
272                 p = *(*unsafe.Pointer)(p)
273         }
274         v := state.decodeUint()
275         if math.MaxUint32 < v {
276                 error(i.ovfl)
277         } else {
278                 *(*uint32)(p) = uint32(v)
279         }
280 }
281
282 // decInt64 decodes an integer and stores it as an int64 through p.
283 func decInt64(i *decInstr, state *decoderState, p unsafe.Pointer) {
284         if i.indir > 0 {
285                 if *(*unsafe.Pointer)(p) == nil {
286                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int64))
287                 }
288                 p = *(*unsafe.Pointer)(p)
289         }
290         *(*int64)(p) = int64(state.decodeInt())
291 }
292
293 // decUint64 decodes an unsigned integer and stores it as a uint64 through p.
294 func decUint64(i *decInstr, state *decoderState, p unsafe.Pointer) {
295         if i.indir > 0 {
296                 if *(*unsafe.Pointer)(p) == nil {
297                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint64))
298                 }
299                 p = *(*unsafe.Pointer)(p)
300         }
301         *(*uint64)(p) = uint64(state.decodeUint())
302 }
303
304 // Floating-point numbers are transmitted as uint64s holding the bits
305 // of the underlying representation.  They are sent byte-reversed, with
306 // the exponent end coming out first, so integer floating point numbers
307 // (for example) transmit more compactly.  This routine does the
308 // unswizzling.
309 func floatFromBits(u uint64) float64 {
310         var v uint64
311         for i := 0; i < 8; i++ {
312                 v <<= 8
313                 v |= u & 0xFF
314                 u >>= 8
315         }
316         return math.Float64frombits(v)
317 }
318
319 // storeFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
320 // number, and stores it through p. It's a helper function for float32 and complex64.
321 func storeFloat32(i *decInstr, state *decoderState, p unsafe.Pointer) {
322         v := floatFromBits(state.decodeUint())
323         av := v
324         if av < 0 {
325                 av = -av
326         }
327         // +Inf is OK in both 32- and 64-bit floats.  Underflow is always OK.
328         if math.MaxFloat32 < av && av <= math.MaxFloat64 {
329                 error(i.ovfl)
330         } else {
331                 *(*float32)(p) = float32(v)
332         }
333 }
334
335 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
336 // number, and stores it through p.
337 func decFloat32(i *decInstr, state *decoderState, p unsafe.Pointer) {
338         if i.indir > 0 {
339                 if *(*unsafe.Pointer)(p) == nil {
340                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(float32))
341                 }
342                 p = *(*unsafe.Pointer)(p)
343         }
344         storeFloat32(i, state, p)
345 }
346
347 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
348 // number, and stores it through p.
349 func decFloat64(i *decInstr, state *decoderState, p unsafe.Pointer) {
350         if i.indir > 0 {
351                 if *(*unsafe.Pointer)(p) == nil {
352                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(float64))
353                 }
354                 p = *(*unsafe.Pointer)(p)
355         }
356         *(*float64)(p) = floatFromBits(uint64(state.decodeUint()))
357 }
358
359 // decComplex64 decodes a pair of unsigned integers, treats them as a
360 // pair of floating point numbers, and stores them as a complex64 through p.
361 // The real part comes first.
362 func decComplex64(i *decInstr, state *decoderState, p unsafe.Pointer) {
363         if i.indir > 0 {
364                 if *(*unsafe.Pointer)(p) == nil {
365                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(complex64))
366                 }
367                 p = *(*unsafe.Pointer)(p)
368         }
369         storeFloat32(i, state, p)
370         storeFloat32(i, state, unsafe.Pointer(uintptr(p)+uintptr(unsafe.Sizeof(float32(0)))))
371 }
372
373 // decComplex128 decodes a pair of unsigned integers, treats them as a
374 // pair of floating point numbers, and stores them as a complex128 through p.
375 // The real part comes first.
376 func decComplex128(i *decInstr, state *decoderState, p unsafe.Pointer) {
377         if i.indir > 0 {
378                 if *(*unsafe.Pointer)(p) == nil {
379                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new(complex128))
380                 }
381                 p = *(*unsafe.Pointer)(p)
382         }
383         real := floatFromBits(uint64(state.decodeUint()))
384         imag := floatFromBits(uint64(state.decodeUint()))
385         *(*complex128)(p) = complex(real, imag)
386 }
387
388 // decUint8Array decodes byte array and stores through p a slice header
389 // describing the data.
390 // uint8 arrays are encoded as an unsigned count followed by the raw bytes.
391 func decUint8Array(i *decInstr, state *decoderState, p unsafe.Pointer) {
392         if i.indir > 0 {
393                 if *(*unsafe.Pointer)(p) == nil {
394                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new([]uint8))
395                 }
396                 p = *(*unsafe.Pointer)(p)
397         }
398         b := make([]uint8, state.decodeUint())
399         state.b.Read(b)
400         *(*[]uint8)(p) = b
401 }
402
403 // decString decodes byte array and stores through p a string header
404 // describing the data.
405 // Strings are encoded as an unsigned count followed by the raw bytes.
406 func decString(i *decInstr, state *decoderState, p unsafe.Pointer) {
407         if i.indir > 0 {
408                 if *(*unsafe.Pointer)(p) == nil {
409                         *(*unsafe.Pointer)(p) = unsafe.Pointer(new([]byte))
410                 }
411                 p = *(*unsafe.Pointer)(p)
412         }
413         b := make([]byte, state.decodeUint())
414         state.b.Read(b)
415         // It would be a shame to do the obvious thing here,
416         //      *(*string)(p) = string(b)
417         // because we've already allocated the storage and this would
418         // allocate again and copy.  So we do this ugly hack, which is even
419         // even more unsafe than it looks as it depends the memory
420         // representation of a string matching the beginning of the memory
421         // representation of a byte slice (a byte slice is longer).
422         *(*string)(p) = *(*string)(unsafe.Pointer(&b))
423 }
424
425 // ignoreUint8Array skips over the data for a byte slice value with no destination.
426 func ignoreUint8Array(i *decInstr, state *decoderState, p unsafe.Pointer) {
427         b := make([]byte, state.decodeUint())
428         state.b.Read(b)
429 }
430
431 // Execution engine
432
433 // The encoder engine is an array of instructions indexed by field number of the incoming
434 // decoder.  It is executed with random access according to field number.
435 type decEngine struct {
436         instr    []decInstr
437         numInstr int // the number of active instructions
438 }
439
440 // allocate makes sure storage is available for an object of underlying type rtyp
441 // that is indir levels of indirection through p.
442 func allocate(rtyp reflect.Type, p uintptr, indir int) uintptr {
443         if indir == 0 {
444                 return p
445         }
446         up := unsafe.Pointer(p)
447         if indir > 1 {
448                 up = decIndirect(up, indir)
449         }
450         if *(*unsafe.Pointer)(up) == nil {
451                 // Allocate object.
452                 *(*unsafe.Pointer)(up) = unsafe.New(rtyp)
453         }
454         return *(*uintptr)(up)
455 }
456
457 // decodeSingle decodes a top-level value that is not a struct and stores it through p.
458 // Such values are preceded by a zero, making them have the memory layout of a
459 // struct field (although with an illegal field number).
460 func (dec *Decoder) decodeSingle(engine *decEngine, ut *userTypeInfo, p uintptr) (err os.Error) {
461         indir := ut.indir
462         if ut.isGobDecoder {
463                 indir = int(ut.decIndir)
464         }
465         p = allocate(ut.base, p, indir)
466         state := dec.newDecoderState(&dec.buf)
467         state.fieldnum = singletonField
468         basep := p
469         delta := int(state.decodeUint())
470         if delta != 0 {
471                 errorf("gob decode: corrupted data: non-zero delta for singleton")
472         }
473         instr := &engine.instr[singletonField]
474         ptr := unsafe.Pointer(basep) // offset will be zero
475         if instr.indir > 1 {
476                 ptr = decIndirect(ptr, instr.indir)
477         }
478         instr.op(instr, state, ptr)
479         dec.freeDecoderState(state)
480         return nil
481 }
482
483 // decodeSingle decodes a top-level struct and stores it through p.
484 // Indir is for the value, not the type.  At the time of the call it may
485 // differ from ut.indir, which was computed when the engine was built.
486 // This state cannot arise for decodeSingle, which is called directly
487 // from the user's value, not from the innards of an engine.
488 func (dec *Decoder) decodeStruct(engine *decEngine, ut *userTypeInfo, p uintptr, indir int) {
489         p = allocate(ut.base.(*reflect.StructType), p, indir)
490         state := dec.newDecoderState(&dec.buf)
491         state.fieldnum = -1
492         basep := p
493         for state.b.Len() > 0 {
494                 delta := int(state.decodeUint())
495                 if delta < 0 {
496                         errorf("gob decode: corrupted data: negative delta")
497                 }
498                 if delta == 0 { // struct terminator is zero delta fieldnum
499                         break
500                 }
501                 fieldnum := state.fieldnum + delta
502                 if fieldnum >= len(engine.instr) {
503                         error(errRange)
504                         break
505                 }
506                 instr := &engine.instr[fieldnum]
507                 p := unsafe.Pointer(basep + instr.offset)
508                 if instr.indir > 1 {
509                         p = decIndirect(p, instr.indir)
510                 }
511                 instr.op(instr, state, p)
512                 state.fieldnum = fieldnum
513         }
514         dec.freeDecoderState(state)
515 }
516
517 // ignoreStruct discards the data for a struct with no destination.
518 func (dec *Decoder) ignoreStruct(engine *decEngine) {
519         state := dec.newDecoderState(&dec.buf)
520         state.fieldnum = -1
521         for state.b.Len() > 0 {
522                 delta := int(state.decodeUint())
523                 if delta < 0 {
524                         errorf("gob ignore decode: corrupted data: negative delta")
525                 }
526                 if delta == 0 { // struct terminator is zero delta fieldnum
527                         break
528                 }
529                 fieldnum := state.fieldnum + delta
530                 if fieldnum >= len(engine.instr) {
531                         error(errRange)
532                 }
533                 instr := &engine.instr[fieldnum]
534                 instr.op(instr, state, unsafe.Pointer(nil))
535                 state.fieldnum = fieldnum
536         }
537         dec.freeDecoderState(state)
538 }
539
540 // ignoreSingle discards the data for a top-level non-struct value with no
541 // destination. It's used when calling Decode with a nil value.
542 func (dec *Decoder) ignoreSingle(engine *decEngine) {
543         state := dec.newDecoderState(&dec.buf)
544         state.fieldnum = singletonField
545         delta := int(state.decodeUint())
546         if delta != 0 {
547                 errorf("gob decode: corrupted data: non-zero delta for singleton")
548         }
549         instr := &engine.instr[singletonField]
550         instr.op(instr, state, unsafe.Pointer(nil))
551         dec.freeDecoderState(state)
552 }
553
554 // decodeArrayHelper does the work for decoding arrays and slices.
555 func (dec *Decoder) decodeArrayHelper(state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, elemIndir int, ovfl os.ErrorString) {
556         instr := &decInstr{elemOp, 0, elemIndir, 0, ovfl}
557         for i := 0; i < length; i++ {
558                 up := unsafe.Pointer(p)
559                 if elemIndir > 1 {
560                         up = decIndirect(up, elemIndir)
561                 }
562                 elemOp(instr, state, up)
563                 p += uintptr(elemWid)
564         }
565 }
566
567 // decodeArray decodes an array and stores it through p, that is, p points to the zeroth element.
568 // The length is an unsigned integer preceding the elements.  Even though the length is redundant
569 // (it's part of the type), it's a useful check and is included in the encoding.
570 func (dec *Decoder) decodeArray(atyp *reflect.ArrayType, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, indir, elemIndir int, ovfl os.ErrorString) {
571         if indir > 0 {
572                 p = allocate(atyp, p, 1) // All but the last level has been allocated by dec.Indirect
573         }
574         if n := state.decodeUint(); n != uint64(length) {
575                 errorf("gob: length mismatch in decodeArray")
576         }
577         dec.decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl)
578 }
579
580 // decodeIntoValue is a helper for map decoding.  Since maps are decoded using reflection,
581 // unlike the other items we can't use a pointer directly.
582 func decodeIntoValue(state *decoderState, op decOp, indir int, v reflect.Value, ovfl os.ErrorString) reflect.Value {
583         instr := &decInstr{op, 0, indir, 0, ovfl}
584         up := unsafe.Pointer(v.UnsafeAddr())
585         if indir > 1 {
586                 up = decIndirect(up, indir)
587         }
588         op(instr, state, up)
589         return v
590 }
591
592 // decodeMap decodes a map and stores its header through p.
593 // Maps are encoded as a length followed by key:value pairs.
594 // Because the internals of maps are not visible to us, we must
595 // use reflection rather than pointer magic.
596 func (dec *Decoder) decodeMap(mtyp *reflect.MapType, state *decoderState, p uintptr, keyOp, elemOp decOp, indir, keyIndir, elemIndir int, ovfl os.ErrorString) {
597         if indir > 0 {
598                 p = allocate(mtyp, p, 1) // All but the last level has been allocated by dec.Indirect
599         }
600         up := unsafe.Pointer(p)
601         if *(*unsafe.Pointer)(up) == nil { // maps are represented as a pointer in the runtime
602                 // Allocate map.
603                 *(*unsafe.Pointer)(up) = unsafe.Pointer(reflect.MakeMap(mtyp).Get())
604         }
605         // Maps cannot be accessed by moving addresses around the way
606         // that slices etc. can.  We must recover a full reflection value for
607         // the iteration.
608         v := reflect.NewValue(unsafe.Unreflect(mtyp, unsafe.Pointer(p))).(*reflect.MapValue)
609         n := int(state.decodeUint())
610         for i := 0; i < n; i++ {
611                 key := decodeIntoValue(state, keyOp, keyIndir, reflect.MakeZero(mtyp.Key()), ovfl)
612                 elem := decodeIntoValue(state, elemOp, elemIndir, reflect.MakeZero(mtyp.Elem()), ovfl)
613                 v.SetElem(key, elem)
614         }
615 }
616
617 // ignoreArrayHelper does the work for discarding arrays and slices.
618 func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) {
619         instr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
620         for i := 0; i < length; i++ {
621                 elemOp(instr, state, nil)
622         }
623 }
624
625 // ignoreArray discards the data for an array value with no destination.
626 func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) {
627         if n := state.decodeUint(); n != uint64(length) {
628                 errorf("gob: length mismatch in ignoreArray")
629         }
630         dec.ignoreArrayHelper(state, elemOp, length)
631 }
632
633 // ignoreMap discards the data for a map value with no destination.
634 func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) {
635         n := int(state.decodeUint())
636         keyInstr := &decInstr{keyOp, 0, 0, 0, os.ErrorString("no error")}
637         elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
638         for i := 0; i < n; i++ {
639                 keyOp(keyInstr, state, nil)
640                 elemOp(elemInstr, state, nil)
641         }
642 }
643
644 // decodeSlice decodes a slice and stores the slice header through p.
645 // Slices are encoded as an unsigned length followed by the elements.
646 func (dec *Decoder) decodeSlice(atyp *reflect.SliceType, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) {
647         n := int(uintptr(state.decodeUint()))
648         if indir > 0 {
649                 up := unsafe.Pointer(p)
650                 if *(*unsafe.Pointer)(up) == nil {
651                         // Allocate the slice header.
652                         *(*unsafe.Pointer)(up) = unsafe.Pointer(new([]unsafe.Pointer))
653                 }
654                 p = *(*uintptr)(up)
655         }
656         // Allocate storage for the slice elements, that is, the underlying array.
657         // Always write a header at p.
658         hdrp := (*reflect.SliceHeader)(unsafe.Pointer(p))
659         hdrp.Data = uintptr(unsafe.NewArray(atyp.Elem(), n))
660         hdrp.Len = n
661         hdrp.Cap = n
662         dec.decodeArrayHelper(state, hdrp.Data, elemOp, elemWid, n, elemIndir, ovfl)
663 }
664
665 // ignoreSlice skips over the data for a slice value with no destination.
666 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
667         dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
668 }
669
670 // setInterfaceValue sets an interface value to a concrete value through
671 // reflection.  If the concrete value does not implement the interface, the
672 // setting will panic.  This routine turns the panic into an error return.
673 // This dance avoids manually checking that the value satisfies the
674 // interface.
675 // TODO(rsc): avoid panic+recover after fixing issue 327.
676 func setInterfaceValue(ivalue *reflect.InterfaceValue, value reflect.Value) {
677         defer func() {
678                 if e := recover(); e != nil {
679                         error(e.(os.Error))
680                 }
681         }()
682         ivalue.Set(value)
683 }
684
685 // decodeInterface decodes an interface value and stores it through p.
686 // Interfaces are encoded as the name of a concrete type followed by a value.
687 // If the name is empty, the value is nil and no value is sent.
688 func (dec *Decoder) decodeInterface(ityp *reflect.InterfaceType, state *decoderState, p uintptr, indir int) {
689         // Create an interface reflect.Value.  We need one even for the nil case.
690         ivalue := reflect.MakeZero(ityp).(*reflect.InterfaceValue)
691         // Read the name of the concrete type.
692         b := make([]byte, state.decodeUint())
693         state.b.Read(b)
694         name := string(b)
695         if name == "" {
696                 // Copy the representation of the nil interface value to the target.
697                 // This is horribly unsafe and special.
698                 *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.Get()
699                 return
700         }
701         // The concrete type must be registered.
702         typ, ok := nameToConcreteType[name]
703         if !ok {
704                 errorf("gob: name not registered for interface: %q", name)
705         }
706         // Read the type id of the concrete value.
707         concreteId := dec.decodeTypeSequence(true)
708         if concreteId < 0 {
709                 error(dec.err)
710         }
711         // Byte count of value is next; we don't care what it is (it's there
712         // in case we want to ignore the value by skipping it completely).
713         state.decodeUint()
714         // Read the concrete value.
715         value := reflect.MakeZero(typ)
716         dec.decodeValue(concreteId, value)
717         if dec.err != nil {
718                 error(dec.err)
719         }
720         // Allocate the destination interface value.
721         if indir > 0 {
722                 p = allocate(ityp, p, 1) // All but the last level has been allocated by dec.Indirect
723         }
724         // Assign the concrete value to the interface.
725         // Tread carefully; it might not satisfy the interface.
726         setInterfaceValue(ivalue, value)
727         // Copy the representation of the interface value to the target.
728         // This is horribly unsafe and special.
729         *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.Get()
730 }
731
732 // ignoreInterface discards the data for an interface value with no destination.
733 func (dec *Decoder) ignoreInterface(state *decoderState) {
734         // Read the name of the concrete type.
735         b := make([]byte, state.decodeUint())
736         _, err := state.b.Read(b)
737         if err != nil {
738                 error(err)
739         }
740         id := dec.decodeTypeSequence(true)
741         if id < 0 {
742                 error(dec.err)
743         }
744         // At this point, the decoder buffer contains a delimited value. Just toss it.
745         state.b.Next(int(state.decodeUint()))
746 }
747
748 // decodeGobDecoder decodes something implementing the GobDecoder interface.
749 // The data is encoded as a byte slice.
750 func (dec *Decoder) decodeGobDecoder(state *decoderState, v reflect.Value, index int) {
751         // Read the bytes for the value.
752         b := make([]byte, state.decodeUint())
753         _, err := state.b.Read(b)
754         if err != nil {
755                 error(err)
756         }
757         // We know it's a GobDecoder, so just call the method directly.
758         err = v.Interface().(GobDecoder).GobDecode(b)
759         if err != nil {
760                 error(err)
761         }
762 }
763
764 // ignoreGobDecoder discards the data for a GobDecoder value with no destination.
765 func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
766         // Read the bytes for the value.
767         b := make([]byte, state.decodeUint())
768         _, err := state.b.Read(b)
769         if err != nil {
770                 error(err)
771         }
772 }
773
774 // Index by Go types.
775 var decOpTable = [...]decOp{
776         reflect.Bool:       decBool,
777         reflect.Int8:       decInt8,
778         reflect.Int16:      decInt16,
779         reflect.Int32:      decInt32,
780         reflect.Int64:      decInt64,
781         reflect.Uint8:      decUint8,
782         reflect.Uint16:     decUint16,
783         reflect.Uint32:     decUint32,
784         reflect.Uint64:     decUint64,
785         reflect.Float32:    decFloat32,
786         reflect.Float64:    decFloat64,
787         reflect.Complex64:  decComplex64,
788         reflect.Complex128: decComplex128,
789         reflect.String:     decString,
790 }
791
792 // Indexed by gob types.  tComplex will be added during type.init().
793 var decIgnoreOpMap = map[typeId]decOp{
794         tBool:    ignoreUint,
795         tInt:     ignoreUint,
796         tUint:    ignoreUint,
797         tFloat:   ignoreUint,
798         tBytes:   ignoreUint8Array,
799         tString:  ignoreUint8Array,
800         tComplex: ignoreTwoUints,
801 }
802
803 // decOpFor returns the decoding op for the base type under rt and
804 // the indirection count to reach it.
805 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) (*decOp, int) {
806         ut := userType(rt)
807         // If the type implements GobEncoder, we handle it without further processing.
808         if ut.isGobDecoder {
809                 return dec.gobDecodeOpFor(ut)
810         }
811         // If this type is already in progress, it's a recursive type (e.g. map[string]*T).
812         // Return the pointer to the op we're already building.
813         if opPtr := inProgress[rt]; opPtr != nil {
814                 return opPtr, ut.indir
815         }
816         typ := ut.base
817         indir := ut.indir
818         var op decOp
819         k := typ.Kind()
820         if int(k) < len(decOpTable) {
821                 op = decOpTable[k]
822         }
823         if op == nil {
824                 inProgress[rt] = &op
825                 // Special cases
826                 switch t := typ.(type) {
827                 case *reflect.ArrayType:
828                         name = "element of " + name
829                         elemId := dec.wireType[wireId].ArrayT.Elem
830                         elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
831                         ovfl := overflow(name)
832                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
833                                 state.dec.decodeArray(t, state, uintptr(p), *elemOp, t.Elem().Size(), t.Len(), i.indir, elemIndir, ovfl)
834                         }
835
836                 case *reflect.MapType:
837                         name = "element of " + name
838                         keyId := dec.wireType[wireId].MapT.Key
839                         elemId := dec.wireType[wireId].MapT.Elem
840                         keyOp, keyIndir := dec.decOpFor(keyId, t.Key(), name, inProgress)
841                         elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
842                         ovfl := overflow(name)
843                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
844                                 up := unsafe.Pointer(p)
845                                 state.dec.decodeMap(t, state, uintptr(up), *keyOp, *elemOp, i.indir, keyIndir, elemIndir, ovfl)
846                         }
847
848                 case *reflect.SliceType:
849                         name = "element of " + name
850                         if t.Elem().Kind() == reflect.Uint8 {
851                                 op = decUint8Array
852                                 break
853                         }
854                         var elemId typeId
855                         if tt, ok := builtinIdToType[wireId]; ok {
856                                 elemId = tt.(*sliceType).Elem
857                         } else {
858                                 elemId = dec.wireType[wireId].SliceT.Elem
859                         }
860                         elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
861                         ovfl := overflow(name)
862                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
863                                 state.dec.decodeSlice(t, state, uintptr(p), *elemOp, t.Elem().Size(), i.indir, elemIndir, ovfl)
864                         }
865
866                 case *reflect.StructType:
867                         // Generate a closure that calls out to the engine for the nested type.
868                         enginePtr, err := dec.getDecEnginePtr(wireId, userType(typ))
869                         if err != nil {
870                                 error(err)
871                         }
872                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
873                                 // indirect through enginePtr to delay evaluation for recursive structs.
874                                 dec.decodeStruct(*enginePtr, userType(typ), uintptr(p), i.indir)
875                         }
876                 case *reflect.InterfaceType:
877                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
878                                 state.dec.decodeInterface(t, state, uintptr(p), i.indir)
879                         }
880                 }
881         }
882         if op == nil {
883                 errorf("gob: decode can't handle type %s", rt.String())
884         }
885         return &op, indir
886 }
887
888 // decIgnoreOpFor returns the decoding op for a field that has no destination.
889 func (dec *Decoder) decIgnoreOpFor(wireId typeId) decOp {
890         op, ok := decIgnoreOpMap[wireId]
891         if !ok {
892                 if wireId == tInterface {
893                         // Special case because it's a method: the ignored item might
894                         // define types and we need to record their state in the decoder.
895                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
896                                 state.dec.ignoreInterface(state)
897                         }
898                         return op
899                 }
900                 // Special cases
901                 wire := dec.wireType[wireId]
902                 switch {
903                 case wire == nil:
904                         errorf("gob: bad data: undefined type %s", wireId.string())
905                 case wire.ArrayT != nil:
906                         elemId := wire.ArrayT.Elem
907                         elemOp := dec.decIgnoreOpFor(elemId)
908                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
909                                 state.dec.ignoreArray(state, elemOp, wire.ArrayT.Len)
910                         }
911
912                 case wire.MapT != nil:
913                         keyId := dec.wireType[wireId].MapT.Key
914                         elemId := dec.wireType[wireId].MapT.Elem
915                         keyOp := dec.decIgnoreOpFor(keyId)
916                         elemOp := dec.decIgnoreOpFor(elemId)
917                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
918                                 state.dec.ignoreMap(state, keyOp, elemOp)
919                         }
920
921                 case wire.SliceT != nil:
922                         elemId := wire.SliceT.Elem
923                         elemOp := dec.decIgnoreOpFor(elemId)
924                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
925                                 state.dec.ignoreSlice(state, elemOp)
926                         }
927
928                 case wire.StructT != nil:
929                         // Generate a closure that calls out to the engine for the nested type.
930                         enginePtr, err := dec.getIgnoreEnginePtr(wireId)
931                         if err != nil {
932                                 error(err)
933                         }
934                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
935                                 // indirect through enginePtr to delay evaluation for recursive structs
936                                 state.dec.ignoreStruct(*enginePtr)
937                         }
938
939                 case wire.GobEncoderT != nil:
940                         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
941                                 state.dec.ignoreGobDecoder(state)
942                         }
943                 }
944         }
945         if op == nil {
946                 errorf("gob: bad data: ignore can't handle type %s", wireId.string())
947         }
948         return op
949 }
950
951 // gobDecodeOpFor returns the op for a type that is known to implement
952 // GobDecoder.
953 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) (*decOp, int) {
954         rt := ut.user
955         if ut.decIndir == -1 {
956                 rt = reflect.PtrTo(rt)
957         } else if ut.decIndir > 0 {
958                 for i := int8(0); i < ut.decIndir; i++ {
959                         rt = rt.(*reflect.PtrType).Elem()
960                 }
961         }
962         var op decOp
963         op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
964                 // Allocate the underlying data, but hold on to the address we have,
965                 // since we need it to get to the receiver's address.
966                 allocate(ut.base, uintptr(p), ut.indir)
967                 var v reflect.Value
968                 if ut.decIndir == -1 {
969                         // Need to climb up one level to turn value into pointer.
970                         v = reflect.NewValue(unsafe.Unreflect(rt, unsafe.Pointer(&p)))
971                 } else {
972                         if ut.decIndir > 0 {
973                                 p = decIndirect(p, int(ut.decIndir))
974                         }
975                         v = reflect.NewValue(unsafe.Unreflect(rt, p))
976                 }
977                 state.dec.decodeGobDecoder(state, v, methodIndex(rt, gobDecodeMethodName))
978         }
979         return &op, int(ut.decIndir)
980
981 }
982
983 // compatibleType asks: Are these two gob Types compatible?
984 // Answers the question for basic types, arrays, maps and slices, plus
985 // GobEncoder/Decoder pairs.
986 // Structs are considered ok; fields will be checked later.
987 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
988         if rhs, ok := inProgress[fr]; ok {
989                 return rhs == fw
990         }
991         inProgress[fr] = fw
992         ut := userType(fr)
993         wire, ok := dec.wireType[fw]
994         // If fr is a GobDecoder, the wire type must be GobEncoder.
995         // And if fr is not a GobDecoder, the wire type must not be either.
996         if ut.isGobDecoder != (ok && wire.GobEncoderT != nil) { // the parentheses look odd but are correct.
997                 return false
998         }
999         if ut.isGobDecoder { // This test trumps all others.
1000                 return true
1001         }
1002         switch t := ut.base.(type) {
1003         default:
1004                 // chan, etc: cannot handle.
1005                 return false
1006         case *reflect.BoolType:
1007                 return fw == tBool
1008         case *reflect.IntType:
1009                 return fw == tInt
1010         case *reflect.UintType:
1011                 return fw == tUint
1012         case *reflect.FloatType:
1013                 return fw == tFloat
1014         case *reflect.ComplexType:
1015                 return fw == tComplex
1016         case *reflect.StringType:
1017                 return fw == tString
1018         case *reflect.InterfaceType:
1019                 return fw == tInterface
1020         case *reflect.ArrayType:
1021                 if !ok || wire.ArrayT == nil {
1022                         return false
1023                 }
1024                 array := wire.ArrayT
1025                 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
1026         case *reflect.MapType:
1027                 if !ok || wire.MapT == nil {
1028                         return false
1029                 }
1030                 MapType := wire.MapT
1031                 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
1032         case *reflect.SliceType:
1033                 // Is it an array of bytes?
1034                 if t.Elem().Kind() == reflect.Uint8 {
1035                         return fw == tBytes
1036                 }
1037                 // Extract and compare element types.
1038                 var sw *sliceType
1039                 if tt, ok := builtinIdToType[fw]; ok {
1040                         sw = tt.(*sliceType)
1041                 } else {
1042                         sw = dec.wireType[fw].SliceT
1043                 }
1044                 elem := userType(t.Elem()).base
1045                 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
1046         case *reflect.StructType:
1047                 return true
1048         }
1049         return true
1050 }
1051
1052 // typeString returns a human-readable description of the type identified by remoteId.
1053 func (dec *Decoder) typeString(remoteId typeId) string {
1054         if t := idToType[remoteId]; t != nil {
1055                 // globally known type.
1056                 return t.string()
1057         }
1058         return dec.wireType[remoteId].string()
1059 }
1060
1061 // compileSingle compiles the decoder engine for a non-struct top-level value, including
1062 // GobDecoders.
1063 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err os.Error) {
1064         rt := ut.base
1065         if ut.isGobDecoder {
1066                 rt = ut.user
1067         }
1068         engine = new(decEngine)
1069         engine.instr = make([]decInstr, 1) // one item
1070         name := rt.String()                // best we can do
1071         if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
1072                 return nil, os.ErrorString("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId))
1073         }
1074         op, indir := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
1075         ovfl := os.ErrorString(`value for "` + name + `" out of range`)
1076         engine.instr[singletonField] = decInstr{*op, singletonField, indir, 0, ovfl}
1077         engine.numInstr = 1
1078         return
1079 }
1080
1081 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
1082 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) (engine *decEngine, err os.Error) {
1083         engine = new(decEngine)
1084         engine.instr = make([]decInstr, 1) // one item
1085         op := dec.decIgnoreOpFor(remoteId)
1086         ovfl := overflow(dec.typeString(remoteId))
1087         engine.instr[0] = decInstr{op, 0, 0, 0, ovfl}
1088         engine.numInstr = 1
1089         return
1090 }
1091
1092 // compileDec compiles the decoder engine for a value.  If the value is not a struct,
1093 // it calls out to compileSingle.
1094 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err os.Error) {
1095         rt := ut.base
1096         srt, ok := rt.(*reflect.StructType)
1097         if !ok || ut.isGobDecoder {
1098                 return dec.compileSingle(remoteId, ut)
1099         }
1100         var wireStruct *structType
1101         // Builtin types can come from global pool; the rest must be defined by the decoder.
1102         // Also we know we're decoding a struct now, so the client must have sent one.
1103         if t, ok := builtinIdToType[remoteId]; ok {
1104                 wireStruct, _ = t.(*structType)
1105         } else {
1106                 wire := dec.wireType[remoteId]
1107                 if wire == nil {
1108                         error(errBadType)
1109                 }
1110                 wireStruct = wire.StructT
1111         }
1112         if wireStruct == nil {
1113                 errorf("gob: type mismatch in decoder: want struct type %s; got non-struct", rt.String())
1114         }
1115         engine = new(decEngine)
1116         engine.instr = make([]decInstr, len(wireStruct.Field))
1117         seen := make(map[reflect.Type]*decOp)
1118         // Loop over the fields of the wire type.
1119         for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
1120                 wireField := wireStruct.Field[fieldnum]
1121                 if wireField.Name == "" {
1122                         errorf("gob: empty name for remote field of type %s", wireStruct.Name)
1123                 }
1124                 ovfl := overflow(wireField.Name)
1125                 // Find the field of the local type with the same name.
1126                 localField, present := srt.FieldByName(wireField.Name)
1127                 // TODO(r): anonymous names
1128                 if !present || !isExported(wireField.Name) {
1129                         op := dec.decIgnoreOpFor(wireField.Id)
1130                         engine.instr[fieldnum] = decInstr{op, fieldnum, 0, 0, ovfl}
1131                         continue
1132                 }
1133                 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
1134                         errorf("gob: wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
1135                 }
1136                 op, indir := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
1137                 engine.instr[fieldnum] = decInstr{*op, fieldnum, indir, uintptr(localField.Offset), ovfl}
1138                 engine.numInstr++
1139         }
1140         return
1141 }
1142
1143 // getDecEnginePtr returns the engine for the specified type.
1144 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err os.Error) {
1145         rt := ut.base
1146         decoderMap, ok := dec.decoderCache[rt]
1147         if !ok {
1148                 decoderMap = make(map[typeId]**decEngine)
1149                 dec.decoderCache[rt] = decoderMap
1150         }
1151         if enginePtr, ok = decoderMap[remoteId]; !ok {
1152                 // To handle recursive types, mark this engine as underway before compiling.
1153                 enginePtr = new(*decEngine)
1154                 decoderMap[remoteId] = enginePtr
1155                 *enginePtr, err = dec.compileDec(remoteId, ut)
1156                 if err != nil {
1157                         decoderMap[remoteId] = nil, false
1158                 }
1159         }
1160         return
1161 }
1162
1163 // emptyStruct is the type we compile into when ignoring a struct value.
1164 type emptyStruct struct{}
1165
1166 var emptyStructType = reflect.Typeof(emptyStruct{})
1167
1168 // getDecEnginePtr returns the engine for the specified type when the value is to be discarded.
1169 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err os.Error) {
1170         var ok bool
1171         if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
1172                 // To handle recursive types, mark this engine as underway before compiling.
1173                 enginePtr = new(*decEngine)
1174                 dec.ignorerCache[wireId] = enginePtr
1175                 wire := dec.wireType[wireId]
1176                 if wire != nil && wire.StructT != nil {
1177                         *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
1178                 } else {
1179                         *enginePtr, err = dec.compileIgnoreSingle(wireId)
1180                 }
1181                 if err != nil {
1182                         dec.ignorerCache[wireId] = nil, false
1183                 }
1184         }
1185         return
1186 }
1187
1188 // decodeValue decodes the data stream representing a value and stores it in val.
1189 func (dec *Decoder) decodeValue(wireId typeId, val reflect.Value) {
1190         defer catchError(&dec.err)
1191         // If the value is nil, it means we should just ignore this item.
1192         if val == nil {
1193                 dec.decodeIgnoredValue(wireId)
1194                 return
1195         }
1196         // Dereference down to the underlying struct type.
1197         ut := userType(val.Type())
1198         base := ut.base
1199         indir := ut.indir
1200         if ut.isGobDecoder {
1201                 indir = int(ut.decIndir)
1202         }
1203         var enginePtr **decEngine
1204         enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
1205         if dec.err != nil {
1206                 return
1207         }
1208         engine := *enginePtr
1209         if st, ok := base.(*reflect.StructType); ok && !ut.isGobDecoder {
1210                 if engine.numInstr == 0 && st.NumField() > 0 && len(dec.wireType[wireId].StructT.Field) > 0 {
1211                         name := base.Name()
1212                         errorf("gob: type mismatch: no fields matched compiling decoder for %s", name)
1213                 }
1214                 dec.decodeStruct(engine, ut, uintptr(val.UnsafeAddr()), indir)
1215         } else {
1216                 dec.decodeSingle(engine, ut, uintptr(val.UnsafeAddr()))
1217         }
1218 }
1219
1220 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
1221 func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
1222         var enginePtr **decEngine
1223         enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
1224         if dec.err != nil {
1225                 return
1226         }
1227         wire := dec.wireType[wireId]
1228         if wire != nil && wire.StructT != nil {
1229                 dec.ignoreStruct(*enginePtr)
1230         } else {
1231                 dec.ignoreSingle(*enginePtr)
1232         }
1233 }
1234
1235 func init() {
1236         var iop, uop decOp
1237         switch reflect.Typeof(int(0)).Bits() {
1238         case 32:
1239                 iop = decInt32
1240                 uop = decUint32
1241         case 64:
1242                 iop = decInt64
1243                 uop = decUint64
1244         default:
1245                 panic("gob: unknown size of int/uint")
1246         }
1247         decOpTable[reflect.Int] = iop
1248         decOpTable[reflect.Uint] = uop
1249
1250         // Finally uintptr
1251         switch reflect.Typeof(uintptr(0)).Bits() {
1252         case 32:
1253                 uop = decUint32
1254         case 64:
1255                 uop = decUint64
1256         default:
1257                 panic("gob: unknown size of uintptr")
1258         }
1259         decOpTable[reflect.Uintptr] = uop
1260 }