github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/encoding/gob/encode.go (about)

     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  import (
     8  	"bytes"
     9  	"math"
    10  	"reflect"
    11  	"unsafe"
    12  )
    13  
    14  const uint64Size = int(unsafe.Sizeof(uint64(0)))
    15  
    16  // encoderState is the global execution state of an instance of the encoder.
    17  // Field numbers are delta encoded and always increase. The field
    18  // number is initialized to -1 so 0 comes out as delta(1). A delta of
    19  // 0 terminates the structure.
    20  type encoderState struct {
    21  	enc      *Encoder
    22  	b        *bytes.Buffer
    23  	sendZero bool                 // encoding an array element or map key/value pair; send zero values
    24  	fieldnum int                  // the last field number written.
    25  	buf      [1 + uint64Size]byte // buffer used by the encoder; here to avoid allocation.
    26  	next     *encoderState        // for free list
    27  }
    28  
    29  func (enc *Encoder) newEncoderState(b *bytes.Buffer) *encoderState {
    30  	e := enc.freeList
    31  	if e == nil {
    32  		e = new(encoderState)
    33  		e.enc = enc
    34  	} else {
    35  		enc.freeList = e.next
    36  	}
    37  	e.sendZero = false
    38  	e.fieldnum = 0
    39  	e.b = b
    40  	return e
    41  }
    42  
    43  func (enc *Encoder) freeEncoderState(e *encoderState) {
    44  	e.next = enc.freeList
    45  	enc.freeList = e
    46  }
    47  
    48  // Unsigned integers have a two-state encoding.  If the number is less
    49  // than 128 (0 through 0x7F), its value is written directly.
    50  // Otherwise the value is written in big-endian byte order preceded
    51  // by the byte length, negated.
    52  
    53  // encodeUint writes an encoded unsigned integer to state.b.
    54  func (state *encoderState) encodeUint(x uint64) {
    55  	if x <= 0x7F {
    56  		err := state.b.WriteByte(uint8(x))
    57  		if err != nil {
    58  			error_(err)
    59  		}
    60  		return
    61  	}
    62  	i := uint64Size
    63  	for x > 0 {
    64  		state.buf[i] = uint8(x)
    65  		x >>= 8
    66  		i--
    67  	}
    68  	state.buf[i] = uint8(i - uint64Size) // = loop count, negated
    69  	_, err := state.b.Write(state.buf[i : uint64Size+1])
    70  	if err != nil {
    71  		error_(err)
    72  	}
    73  }
    74  
    75  // encodeInt writes an encoded signed integer to state.w.
    76  // The low bit of the encoding says whether to bit complement the (other bits of the)
    77  // uint to recover the int.
    78  func (state *encoderState) encodeInt(i int64) {
    79  	var x uint64
    80  	if i < 0 {
    81  		x = uint64(^i<<1) | 1
    82  	} else {
    83  		x = uint64(i << 1)
    84  	}
    85  	state.encodeUint(uint64(x))
    86  }
    87  
    88  // encOp is the signature of an encoding operator for a given type.
    89  type encOp func(i *encInstr, state *encoderState, p unsafe.Pointer)
    90  
    91  // The 'instructions' of the encoding machine
    92  type encInstr struct {
    93  	op     encOp
    94  	field  int     // field number
    95  	indir  int     // how many pointer indirections to reach the value in the struct
    96  	offset uintptr // offset in the structure of the field to encode
    97  }
    98  
    99  // update emits a field number and updates the state to record its value for delta encoding.
   100  // If the instruction pointer is nil, it does nothing
   101  func (state *encoderState) update(instr *encInstr) {
   102  	if instr != nil {
   103  		state.encodeUint(uint64(instr.field - state.fieldnum))
   104  		state.fieldnum = instr.field
   105  	}
   106  }
   107  
   108  // Each encoder for a composite is responsible for handling any
   109  // indirections associated with the elements of the data structure.
   110  // If any pointer so reached is nil, no bytes are written.  If the
   111  // data item is zero, no bytes are written.  Single values - ints,
   112  // strings etc. - are indirected before calling their encoders.
   113  // Otherwise, the output (for a scalar) is the field number, as an
   114  // encoded integer, followed by the field data in its appropriate
   115  // format.
   116  
   117  // encIndirect dereferences p indir times and returns the result.
   118  func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
   119  	for ; indir > 0; indir-- {
   120  		p = *(*unsafe.Pointer)(p)
   121  		if p == nil {
   122  			return unsafe.Pointer(nil)
   123  		}
   124  	}
   125  	return p
   126  }
   127  
   128  // encBool encodes the bool with address p as an unsigned 0 or 1.
   129  func encBool(i *encInstr, state *encoderState, p unsafe.Pointer) {
   130  	b := *(*bool)(p)
   131  	if b || state.sendZero {
   132  		state.update(i)
   133  		if b {
   134  			state.encodeUint(1)
   135  		} else {
   136  			state.encodeUint(0)
   137  		}
   138  	}
   139  }
   140  
   141  // encInt encodes the int with address p.
   142  func encInt(i *encInstr, state *encoderState, p unsafe.Pointer) {
   143  	v := int64(*(*int)(p))
   144  	if v != 0 || state.sendZero {
   145  		state.update(i)
   146  		state.encodeInt(v)
   147  	}
   148  }
   149  
   150  // encUint encodes the uint with address p.
   151  func encUint(i *encInstr, state *encoderState, p unsafe.Pointer) {
   152  	v := uint64(*(*uint)(p))
   153  	if v != 0 || state.sendZero {
   154  		state.update(i)
   155  		state.encodeUint(v)
   156  	}
   157  }
   158  
   159  // encInt8 encodes the int8 with address p.
   160  func encInt8(i *encInstr, state *encoderState, p unsafe.Pointer) {
   161  	v := int64(*(*int8)(p))
   162  	if v != 0 || state.sendZero {
   163  		state.update(i)
   164  		state.encodeInt(v)
   165  	}
   166  }
   167  
   168  // encUint8 encodes the uint8 with address p.
   169  func encUint8(i *encInstr, state *encoderState, p unsafe.Pointer) {
   170  	v := uint64(*(*uint8)(p))
   171  	if v != 0 || state.sendZero {
   172  		state.update(i)
   173  		state.encodeUint(v)
   174  	}
   175  }
   176  
   177  // encInt16 encodes the int16 with address p.
   178  func encInt16(i *encInstr, state *encoderState, p unsafe.Pointer) {
   179  	v := int64(*(*int16)(p))
   180  	if v != 0 || state.sendZero {
   181  		state.update(i)
   182  		state.encodeInt(v)
   183  	}
   184  }
   185  
   186  // encUint16 encodes the uint16 with address p.
   187  func encUint16(i *encInstr, state *encoderState, p unsafe.Pointer) {
   188  	v := uint64(*(*uint16)(p))
   189  	if v != 0 || state.sendZero {
   190  		state.update(i)
   191  		state.encodeUint(v)
   192  	}
   193  }
   194  
   195  // encInt32 encodes the int32 with address p.
   196  func encInt32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   197  	v := int64(*(*int32)(p))
   198  	if v != 0 || state.sendZero {
   199  		state.update(i)
   200  		state.encodeInt(v)
   201  	}
   202  }
   203  
   204  // encUint encodes the uint32 with address p.
   205  func encUint32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   206  	v := uint64(*(*uint32)(p))
   207  	if v != 0 || state.sendZero {
   208  		state.update(i)
   209  		state.encodeUint(v)
   210  	}
   211  }
   212  
   213  // encInt64 encodes the int64 with address p.
   214  func encInt64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   215  	v := *(*int64)(p)
   216  	if v != 0 || state.sendZero {
   217  		state.update(i)
   218  		state.encodeInt(v)
   219  	}
   220  }
   221  
   222  // encInt64 encodes the uint64 with address p.
   223  func encUint64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   224  	v := *(*uint64)(p)
   225  	if v != 0 || state.sendZero {
   226  		state.update(i)
   227  		state.encodeUint(v)
   228  	}
   229  }
   230  
   231  // encUintptr encodes the uintptr with address p.
   232  func encUintptr(i *encInstr, state *encoderState, p unsafe.Pointer) {
   233  	v := uint64(*(*uintptr)(p))
   234  	if v != 0 || state.sendZero {
   235  		state.update(i)
   236  		state.encodeUint(v)
   237  	}
   238  }
   239  
   240  // floatBits returns a uint64 holding the bits of a floating-point number.
   241  // Floating-point numbers are transmitted as uint64s holding the bits
   242  // of the underlying representation.  They are sent byte-reversed, with
   243  // the exponent end coming out first, so integer floating point numbers
   244  // (for example) transmit more compactly.  This routine does the
   245  // swizzling.
   246  func floatBits(f float64) uint64 {
   247  	u := math.Float64bits(f)
   248  	var v uint64
   249  	for i := 0; i < 8; i++ {
   250  		v <<= 8
   251  		v |= u & 0xFF
   252  		u >>= 8
   253  	}
   254  	return v
   255  }
   256  
   257  // encFloat32 encodes the float32 with address p.
   258  func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
   259  	f := *(*float32)(p)
   260  	if f != 0 || state.sendZero {
   261  		v := floatBits(float64(f))
   262  		state.update(i)
   263  		state.encodeUint(v)
   264  	}
   265  }
   266  
   267  // encFloat64 encodes the float64 with address p.
   268  func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   269  	f := *(*float64)(p)
   270  	if f != 0 || state.sendZero {
   271  		state.update(i)
   272  		v := floatBits(f)
   273  		state.encodeUint(v)
   274  	}
   275  }
   276  
   277  // encComplex64 encodes the complex64 with address p.
   278  // Complex numbers are just a pair of floating-point numbers, real part first.
   279  func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
   280  	c := *(*complex64)(p)
   281  	if c != 0+0i || state.sendZero {
   282  		rpart := floatBits(float64(real(c)))
   283  		ipart := floatBits(float64(imag(c)))
   284  		state.update(i)
   285  		state.encodeUint(rpart)
   286  		state.encodeUint(ipart)
   287  	}
   288  }
   289  
   290  // encComplex128 encodes the complex128 with address p.
   291  func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
   292  	c := *(*complex128)(p)
   293  	if c != 0+0i || state.sendZero {
   294  		rpart := floatBits(real(c))
   295  		ipart := floatBits(imag(c))
   296  		state.update(i)
   297  		state.encodeUint(rpart)
   298  		state.encodeUint(ipart)
   299  	}
   300  }
   301  
   302  // encUint8Array encodes the byte slice whose header has address p.
   303  // Byte arrays are encoded as an unsigned count followed by the raw bytes.
   304  func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
   305  	b := *(*[]byte)(p)
   306  	if len(b) > 0 || state.sendZero {
   307  		state.update(i)
   308  		state.encodeUint(uint64(len(b)))
   309  		state.b.Write(b)
   310  	}
   311  }
   312  
   313  // encString encodes the string whose header has address p.
   314  // Strings are encoded as an unsigned count followed by the raw bytes.
   315  func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
   316  	s := *(*string)(p)
   317  	if len(s) > 0 || state.sendZero {
   318  		state.update(i)
   319  		state.encodeUint(uint64(len(s)))
   320  		state.b.WriteString(s)
   321  	}
   322  }
   323  
   324  // encStructTerminator encodes the end of an encoded struct
   325  // as delta field number of 0.
   326  func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
   327  	state.encodeUint(0)
   328  }
   329  
   330  // Execution engine
   331  
   332  // encEngine an array of instructions indexed by field number of the encoding
   333  // data, typically a struct.  It is executed top to bottom, walking the struct.
   334  type encEngine struct {
   335  	instr []encInstr
   336  }
   337  
   338  const singletonField = 0
   339  
   340  // encodeSingle encodes a single top-level non-struct value.
   341  func (enc *Encoder) encodeSingle(b *bytes.Buffer, engine *encEngine, basep uintptr) {
   342  	state := enc.newEncoderState(b)
   343  	state.fieldnum = singletonField
   344  	// There is no surrounding struct to frame the transmission, so we must
   345  	// generate data even if the item is zero.  To do this, set sendZero.
   346  	state.sendZero = true
   347  	instr := &engine.instr[singletonField]
   348  	p := unsafe.Pointer(basep) // offset will be zero
   349  	if instr.indir > 0 {
   350  		if p = encIndirect(p, instr.indir); p == nil {
   351  			return
   352  		}
   353  	}
   354  	instr.op(instr, state, p)
   355  	enc.freeEncoderState(state)
   356  }
   357  
   358  // encodeStruct encodes a single struct value.
   359  func (enc *Encoder) encodeStruct(b *bytes.Buffer, engine *encEngine, basep uintptr) {
   360  	state := enc.newEncoderState(b)
   361  	state.fieldnum = -1
   362  	for i := 0; i < len(engine.instr); i++ {
   363  		instr := &engine.instr[i]
   364  		p := unsafe.Pointer(basep + instr.offset)
   365  		if instr.indir > 0 {
   366  			if p = encIndirect(p, instr.indir); p == nil {
   367  				continue
   368  			}
   369  		}
   370  		instr.op(instr, state, p)
   371  	}
   372  	enc.freeEncoderState(state)
   373  }
   374  
   375  // encodeArray encodes the array whose 0th element is at p.
   376  func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid uintptr, elemIndir int, length int) {
   377  	state := enc.newEncoderState(b)
   378  	state.fieldnum = -1
   379  	state.sendZero = true
   380  	state.encodeUint(uint64(length))
   381  	for i := 0; i < length; i++ {
   382  		elemp := p
   383  		up := unsafe.Pointer(elemp)
   384  		if elemIndir > 0 {
   385  			if up = encIndirect(up, elemIndir); up == nil {
   386  				errorf("encodeArray: nil element")
   387  			}
   388  			elemp = uintptr(up)
   389  		}
   390  		op(nil, state, unsafe.Pointer(elemp))
   391  		p += uintptr(elemWid)
   392  	}
   393  	enc.freeEncoderState(state)
   394  }
   395  
   396  // encodeReflectValue is a helper for maps. It encodes the value v.
   397  func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
   398  	for i := 0; i < indir && v.IsValid(); i++ {
   399  		v = reflect.Indirect(v)
   400  	}
   401  	if !v.IsValid() {
   402  		errorf("encodeReflectValue: nil element")
   403  	}
   404  	op(nil, state, unsafe.Pointer(unsafeAddr(v)))
   405  }
   406  
   407  // encodeMap encodes a map as unsigned count followed by key:value pairs.
   408  // Because map internals are not exposed, we must use reflection rather than
   409  // addresses.
   410  func (enc *Encoder) encodeMap(b *bytes.Buffer, mv reflect.Value, keyOp, elemOp encOp, keyIndir, elemIndir int) {
   411  	state := enc.newEncoderState(b)
   412  	state.fieldnum = -1
   413  	state.sendZero = true
   414  	keys := mv.MapKeys()
   415  	state.encodeUint(uint64(len(keys)))
   416  	for _, key := range keys {
   417  		encodeReflectValue(state, key, keyOp, keyIndir)
   418  		encodeReflectValue(state, mv.MapIndex(key), elemOp, elemIndir)
   419  	}
   420  	enc.freeEncoderState(state)
   421  }
   422  
   423  // encodeInterface encodes the interface value iv.
   424  // To send an interface, we send a string identifying the concrete type, followed
   425  // by the type identifier (which might require defining that type right now), followed
   426  // by the concrete value.  A nil value gets sent as the empty string for the name,
   427  // followed by no value.
   428  func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv reflect.Value) {
   429  	// Gobs can encode nil interface values but not typed interface
   430  	// values holding nil pointers, since nil pointers point to no value.
   431  	elem := iv.Elem()
   432  	if elem.Kind() == reflect.Ptr && elem.IsNil() {
   433  		errorf("gob: cannot encode nil pointer of type %s inside interface", iv.Elem().Type())
   434  	}
   435  	state := enc.newEncoderState(b)
   436  	state.fieldnum = -1
   437  	state.sendZero = true
   438  	if iv.IsNil() {
   439  		state.encodeUint(0)
   440  		return
   441  	}
   442  
   443  	ut := userType(iv.Elem().Type())
   444  	registerLock.RLock()
   445  	name, ok := concreteTypeToName[ut.base]
   446  	registerLock.RUnlock()
   447  	if !ok {
   448  		errorf("type not registered for interface: %s", ut.base)
   449  	}
   450  	// Send the name.
   451  	state.encodeUint(uint64(len(name)))
   452  	_, err := state.b.WriteString(name)
   453  	if err != nil {
   454  		error_(err)
   455  	}
   456  	// Define the type id if necessary.
   457  	enc.sendTypeDescriptor(enc.writer(), state, ut)
   458  	// Send the type id.
   459  	enc.sendTypeId(state, ut)
   460  	// Encode the value into a new buffer.  Any nested type definitions
   461  	// should be written to b, before the encoded value.
   462  	enc.pushWriter(b)
   463  	data := new(bytes.Buffer)
   464  	data.Write(spaceForLength)
   465  	enc.encode(data, elem, ut)
   466  	if enc.err != nil {
   467  		error_(enc.err)
   468  	}
   469  	enc.popWriter()
   470  	enc.writeMessage(b, data)
   471  	if enc.err != nil {
   472  		error_(err)
   473  	}
   474  	enc.freeEncoderState(state)
   475  }
   476  
   477  // isZero returns whether the value is the zero of its type.
   478  func isZero(val reflect.Value) bool {
   479  	switch val.Kind() {
   480  	case reflect.Array:
   481  		for i := 0; i < val.Len(); i++ {
   482  			if !isZero(val.Index(i)) {
   483  				return false
   484  			}
   485  		}
   486  		return true
   487  	case reflect.Map, reflect.Slice, reflect.String:
   488  		return val.Len() == 0
   489  	case reflect.Bool:
   490  		return !val.Bool()
   491  	case reflect.Complex64, reflect.Complex128:
   492  		return val.Complex() == 0
   493  	case reflect.Chan, reflect.Func, reflect.Ptr:
   494  		return val.IsNil()
   495  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   496  		return val.Int() == 0
   497  	case reflect.Float32, reflect.Float64:
   498  		return val.Float() == 0
   499  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   500  		return val.Uint() == 0
   501  	case reflect.Struct:
   502  		for i := 0; i < val.NumField(); i++ {
   503  			if !isZero(val.Field(i)) {
   504  				return false
   505  			}
   506  		}
   507  		return true
   508  	}
   509  	panic("unknown type in isZero " + val.Type().String())
   510  }
   511  
   512  // encGobEncoder encodes a value that implements the GobEncoder interface.
   513  // The data is sent as a byte array.
   514  func (enc *Encoder) encodeGobEncoder(b *bytes.Buffer, v reflect.Value) {
   515  	// TODO: should we catch panics from the called method?
   516  	// We know it's a GobEncoder, so just call the method directly.
   517  	data, err := v.Interface().(GobEncoder).GobEncode()
   518  	if err != nil {
   519  		error_(err)
   520  	}
   521  	state := enc.newEncoderState(b)
   522  	state.fieldnum = -1
   523  	state.encodeUint(uint64(len(data)))
   524  	state.b.Write(data)
   525  	enc.freeEncoderState(state)
   526  }
   527  
   528  var encOpTable = [...]encOp{
   529  	reflect.Bool:       encBool,
   530  	reflect.Int:        encInt,
   531  	reflect.Int8:       encInt8,
   532  	reflect.Int16:      encInt16,
   533  	reflect.Int32:      encInt32,
   534  	reflect.Int64:      encInt64,
   535  	reflect.Uint:       encUint,
   536  	reflect.Uint8:      encUint8,
   537  	reflect.Uint16:     encUint16,
   538  	reflect.Uint32:     encUint32,
   539  	reflect.Uint64:     encUint64,
   540  	reflect.Uintptr:    encUintptr,
   541  	reflect.Float32:    encFloat32,
   542  	reflect.Float64:    encFloat64,
   543  	reflect.Complex64:  encComplex64,
   544  	reflect.Complex128: encComplex128,
   545  	reflect.String:     encString,
   546  }
   547  
   548  // encOpFor returns (a pointer to) the encoding op for the base type under rt and
   549  // the indirection count to reach it.
   550  func (enc *Encoder) encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp) (*encOp, int) {
   551  	ut := userType(rt)
   552  	// If the type implements GobEncoder, we handle it without further processing.
   553  	if ut.isGobEncoder {
   554  		return enc.gobEncodeOpFor(ut)
   555  	}
   556  	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
   557  	// Return the pointer to the op we're already building.
   558  	if opPtr := inProgress[rt]; opPtr != nil {
   559  		return opPtr, ut.indir
   560  	}
   561  	typ := ut.base
   562  	indir := ut.indir
   563  	k := typ.Kind()
   564  	var op encOp
   565  	if int(k) < len(encOpTable) {
   566  		op = encOpTable[k]
   567  	}
   568  	if op == nil {
   569  		inProgress[rt] = &op
   570  		// Special cases
   571  		switch t := typ; t.Kind() {
   572  		case reflect.Slice:
   573  			if t.Elem().Kind() == reflect.Uint8 {
   574  				op = encUint8Array
   575  				break
   576  			}
   577  			// Slices have a header; we decode it to find the underlying array.
   578  			elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
   579  			op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   580  				slice := (*reflect.SliceHeader)(p)
   581  				if !state.sendZero && slice.Len == 0 {
   582  					return
   583  				}
   584  				state.update(i)
   585  				state.enc.encodeArray(state.b, slice.Data, *elemOp, t.Elem().Size(), indir, int(slice.Len))
   586  			}
   587  		case reflect.Array:
   588  			// True arrays have size in the type.
   589  			elemOp, indir := enc.encOpFor(t.Elem(), inProgress)
   590  			op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   591  				state.update(i)
   592  				state.enc.encodeArray(state.b, uintptr(p), *elemOp, t.Elem().Size(), indir, t.Len())
   593  			}
   594  		case reflect.Map:
   595  			keyOp, keyIndir := enc.encOpFor(t.Key(), inProgress)
   596  			elemOp, elemIndir := enc.encOpFor(t.Elem(), inProgress)
   597  			op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   598  				// Maps cannot be accessed by moving addresses around the way
   599  				// that slices etc. can.  We must recover a full reflection value for
   600  				// the iteration.
   601  				v := reflect.NewAt(t, unsafe.Pointer(p)).Elem()
   602  				mv := reflect.Indirect(v)
   603  				// We send zero-length (but non-nil) maps because the
   604  				// receiver might want to use the map.  (Maps don't use append.)
   605  				if !state.sendZero && mv.IsNil() {
   606  					return
   607  				}
   608  				state.update(i)
   609  				state.enc.encodeMap(state.b, mv, *keyOp, *elemOp, keyIndir, elemIndir)
   610  			}
   611  		case reflect.Struct:
   612  			// Generate a closure that calls out to the engine for the nested type.
   613  			enc.getEncEngine(userType(typ))
   614  			info := mustGetTypeInfo(typ)
   615  			op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   616  				state.update(i)
   617  				// indirect through info to delay evaluation for recursive structs
   618  				state.enc.encodeStruct(state.b, info.encoder, uintptr(p))
   619  			}
   620  		case reflect.Interface:
   621  			op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   622  				// Interfaces transmit the name and contents of the concrete
   623  				// value they contain.
   624  				v := reflect.NewAt(t, unsafe.Pointer(p)).Elem()
   625  				iv := reflect.Indirect(v)
   626  				if !state.sendZero && (!iv.IsValid() || iv.IsNil()) {
   627  					return
   628  				}
   629  				state.update(i)
   630  				state.enc.encodeInterface(state.b, iv)
   631  			}
   632  		}
   633  	}
   634  	if op == nil {
   635  		errorf("can't happen: encode type %s", rt)
   636  	}
   637  	return &op, indir
   638  }
   639  
   640  // gobEncodeOpFor returns the op for a type that is known to implement
   641  // GobEncoder.
   642  func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
   643  	rt := ut.user
   644  	if ut.encIndir == -1 {
   645  		rt = reflect.PtrTo(rt)
   646  	} else if ut.encIndir > 0 {
   647  		for i := int8(0); i < ut.encIndir; i++ {
   648  			rt = rt.Elem()
   649  		}
   650  	}
   651  	var op encOp
   652  	op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
   653  		var v reflect.Value
   654  		if ut.encIndir == -1 {
   655  			// Need to climb up one level to turn value into pointer.
   656  			v = reflect.NewAt(rt, unsafe.Pointer(&p)).Elem()
   657  		} else {
   658  			v = reflect.NewAt(rt, p).Elem()
   659  		}
   660  		if !state.sendZero && isZero(v) {
   661  			return
   662  		}
   663  		state.update(i)
   664  		state.enc.encodeGobEncoder(state.b, v)
   665  	}
   666  	return &op, int(ut.encIndir) // encIndir: op will get called with p == address of receiver.
   667  }
   668  
   669  // compileEnc returns the engine to compile the type.
   670  func (enc *Encoder) compileEnc(ut *userTypeInfo) *encEngine {
   671  	srt := ut.base
   672  	engine := new(encEngine)
   673  	seen := make(map[reflect.Type]*encOp)
   674  	rt := ut.base
   675  	if ut.isGobEncoder {
   676  		rt = ut.user
   677  	}
   678  	if !ut.isGobEncoder &&
   679  		srt.Kind() == reflect.Struct {
   680  		for fieldNum, wireFieldNum := 0, 0; fieldNum < srt.NumField(); fieldNum++ {
   681  			f := srt.Field(fieldNum)
   682  			if !isExported(f.Name) {
   683  				continue
   684  			}
   685  			op, indir := enc.encOpFor(f.Type, seen)
   686  			engine.instr = append(engine.instr, encInstr{*op, wireFieldNum, indir, uintptr(f.Offset)})
   687  			wireFieldNum++
   688  		}
   689  		if srt.NumField() > 0 && len(engine.instr) == 0 {
   690  			errorf("type %s has no exported fields", rt)
   691  		}
   692  		engine.instr = append(engine.instr, encInstr{encStructTerminator, 0, 0, 0})
   693  	} else {
   694  		engine.instr = make([]encInstr, 1)
   695  		op, indir := enc.encOpFor(rt, seen)
   696  		engine.instr[0] = encInstr{*op, singletonField, indir, 0} // offset is zero
   697  	}
   698  	return engine
   699  }
   700  
   701  // getEncEngine returns the engine to compile the type.
   702  // typeLock must be held (or we're in initialization and guaranteed single-threaded).
   703  func (enc *Encoder) getEncEngine(ut *userTypeInfo) *encEngine {
   704  	info, err1 := getTypeInfo(ut)
   705  	if err1 != nil {
   706  		error_(err1)
   707  	}
   708  	if info.encoder == nil {
   709  		// Assign the encEngine now, so recursive types work correctly. But...
   710  		info.encoder = new(encEngine)
   711  		// ... if we fail to complete building the engine, don't cache the half-built machine.
   712  		// Doing this here means we won't cache a type that is itself OK but
   713  		// that contains a nested type that won't compile. The result is consistent
   714  		// error behavior when Encode is called multiple times on the top-level type.
   715  		ok := false
   716  		defer func() {
   717  			if !ok {
   718  				info.encoder = nil
   719  			}
   720  		}()
   721  		info.encoder = enc.compileEnc(ut)
   722  		ok = true
   723  	}
   724  	return info.encoder
   725  }
   726  
   727  // lockAndGetEncEngine is a function that locks and compiles.
   728  // This lets us hold the lock only while compiling, not when encoding.
   729  func (enc *Encoder) lockAndGetEncEngine(ut *userTypeInfo) *encEngine {
   730  	typeLock.Lock()
   731  	defer typeLock.Unlock()
   732  	return enc.getEncEngine(ut)
   733  }
   734  
   735  func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value, ut *userTypeInfo) {
   736  	defer catchError(&enc.err)
   737  	engine := enc.lockAndGetEncEngine(ut)
   738  	indir := ut.indir
   739  	if ut.isGobEncoder {
   740  		indir = int(ut.encIndir)
   741  	}
   742  	for i := 0; i < indir; i++ {
   743  		value = reflect.Indirect(value)
   744  	}
   745  	if !ut.isGobEncoder && value.Type().Kind() == reflect.Struct {
   746  		enc.encodeStruct(b, engine, unsafeAddr(value))
   747  	} else {
   748  		enc.encodeSingle(b, engine, unsafeAddr(value))
   749  	}
   750  }