re PR lto/48086 (bootstrap-lto creates c-common.s with too many sections on x86_64...
[official-gcc.git] / libgo / go / gob / decoder.go
blob664001a4b21e0bf7ba4ada7e9390a955e1d26b2d
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 gob
7 import (
8 "bytes"
9 "io"
10 "os"
11 "reflect"
12 "sync"
15 // A Decoder manages the receipt of type and data information read from the
16 // remote side of a connection.
17 type Decoder struct {
18 mutex sync.Mutex // each item must be received atomically
19 r io.Reader // source of the data
20 wireType map[typeId]*wireType // map from remote ID to local description
21 decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
22 ignorerCache map[typeId]**decEngine // ditto for ignored objects
23 state *decodeState // reads data from in-memory buffer
24 countState *decodeState // reads counts from wire
25 buf []byte
26 countBuf [9]byte // counts may be uint64s (unlikely!), require 9 bytes
27 byteBuffer *bytes.Buffer
28 err os.Error
31 // NewDecoder returns a new decoder that reads from the io.Reader.
32 func NewDecoder(r io.Reader) *Decoder {
33 dec := new(Decoder)
34 dec.r = r
35 dec.wireType = make(map[typeId]*wireType)
36 dec.state = newDecodeState(dec, &dec.byteBuffer) // buffer set in Decode()
37 dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
38 dec.ignorerCache = make(map[typeId]**decEngine)
40 return dec
43 // recvType loads the definition of a type and reloads the Decoder's buffer.
44 func (dec *Decoder) recvType(id typeId) {
45 // Have we already seen this type? That's an error
46 if dec.wireType[id] != nil {
47 dec.err = os.ErrorString("gob: duplicate type received")
48 return
51 // Type:
52 wire := new(wireType)
53 dec.err = dec.decode(tWireType, reflect.NewValue(wire))
54 if dec.err != nil {
55 return
57 // Remember we've seen this type.
58 dec.wireType[id] = wire
60 // Load the next parcel.
61 dec.recv()
64 // Decode reads the next value from the connection and stores
65 // it in the data represented by the empty interface value.
66 // The value underlying e must be the correct type for the next
67 // data item received, and must be a pointer.
68 func (dec *Decoder) Decode(e interface{}) os.Error {
69 value := reflect.NewValue(e)
70 // If e represents a value as opposed to a pointer, the answer won't
71 // get back to the caller. Make sure it's a pointer.
72 if value.Type().Kind() != reflect.Ptr {
73 dec.err = os.ErrorString("gob: attempt to decode into a non-pointer")
74 return dec.err
76 return dec.DecodeValue(value)
79 // recv reads the next count-delimited item from the input. It is the converse
80 // of Encoder.send.
81 func (dec *Decoder) recv() {
82 // Read a count.
83 var nbytes uint64
84 nbytes, dec.err = decodeUintReader(dec.r, dec.countBuf[0:])
85 if dec.err != nil {
86 return
88 // Allocate the buffer.
89 if nbytes > uint64(len(dec.buf)) {
90 dec.buf = make([]byte, nbytes+1000)
92 dec.byteBuffer = bytes.NewBuffer(dec.buf[0:nbytes])
94 // Read the data
95 _, dec.err = io.ReadFull(dec.r, dec.buf[0:nbytes])
96 if dec.err != nil {
97 if dec.err == os.EOF {
98 dec.err = io.ErrUnexpectedEOF
100 return
104 // decodeValueFromBuffer grabs the next value from the input. The Decoder's
105 // buffer already contains data. If the next item in the buffer is a type
106 // descriptor, it may be necessary to reload the buffer, but recvType does that.
107 func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) {
108 for dec.state.b.Len() > 0 {
109 // Receive a type id.
110 id := typeId(dec.state.decodeInt())
112 // Is it a new type?
113 if id < 0 { // 0 is the error state, handled above
114 // If the id is negative, we have a type.
115 dec.recvType(-id)
116 if dec.err != nil {
117 break
119 continue
122 // Make sure the type has been defined already or is a builtin type (for
123 // top-level singleton values).
124 if dec.wireType[id] == nil && builtinIdToType[id] == nil {
125 dec.err = errBadType
126 break
128 // An interface value is preceded by a byte count.
129 if countPresent {
130 count := int(dec.state.decodeUint())
131 if ignoreInterfaceValue {
132 // An interface value is preceded by a byte count. Just skip that many bytes.
133 dec.state.b.Next(int(count))
134 break
136 // Otherwise fall through and decode it.
138 dec.err = dec.decode(id, value)
139 break
143 // DecodeValue reads the next value from the connection and stores
144 // it in the data represented by the reflection value.
145 // The value must be the correct type for the next
146 // data item received.
147 func (dec *Decoder) DecodeValue(value reflect.Value) os.Error {
148 // Make sure we're single-threaded through here.
149 dec.mutex.Lock()
150 defer dec.mutex.Unlock()
152 dec.err = nil
153 dec.recv()
154 if dec.err != nil {
155 return dec.err
157 dec.decodeValueFromBuffer(value, false, false)
158 return dec.err
161 // If debug.go is compiled into the program , debugFunc prints a human-readable
162 // representation of the gob data read from r by calling that file's Debug function.
163 // Otherwise it is nil.
164 var debugFunc func(io.Reader)