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