...

Source file src/reflect/value.go

Documentation: reflect

		 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 reflect
		 6  
		 7  import (
		 8  	"internal/abi"
		 9  	"internal/itoa"
		10  	"internal/unsafeheader"
		11  	"math"
		12  	"runtime"
		13  	"unsafe"
		14  )
		15  
		16  const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
		17  
		18  // Value is the reflection interface to a Go value.
		19  //
		20  // Not all methods apply to all kinds of values. Restrictions,
		21  // if any, are noted in the documentation for each method.
		22  // Use the Kind method to find out the kind of value before
		23  // calling kind-specific methods. Calling a method
		24  // inappropriate to the kind of type causes a run time panic.
		25  //
		26  // The zero Value represents no value.
		27  // Its IsValid method returns false, its Kind method returns Invalid,
		28  // its String method returns "<invalid Value>", and all other methods panic.
		29  // Most functions and methods never return an invalid value.
		30  // If one does, its documentation states the conditions explicitly.
		31  //
		32  // A Value can be used concurrently by multiple goroutines provided that
		33  // the underlying Go value can be used concurrently for the equivalent
		34  // direct operations.
		35  //
		36  // To compare two Values, compare the results of the Interface method.
		37  // Using == on two Values does not compare the underlying values
		38  // they represent.
		39  type Value struct {
		40  	// typ holds the type of the value represented by a Value.
		41  	typ *rtype
		42  
		43  	// Pointer-valued data or, if flagIndir is set, pointer to data.
		44  	// Valid when either flagIndir is set or typ.pointers() is true.
		45  	ptr unsafe.Pointer
		46  
		47  	// flag holds metadata about the value.
		48  	// The lowest bits are flag bits:
		49  	//	- flagStickyRO: obtained via unexported not embedded field, so read-only
		50  	//	- flagEmbedRO: obtained via unexported embedded field, so read-only
		51  	//	- flagIndir: val holds a pointer to the data
		52  	//	- flagAddr: v.CanAddr is true (implies flagIndir)
		53  	//	- flagMethod: v is a method value.
		54  	// The next five bits give the Kind of the value.
		55  	// This repeats typ.Kind() except for method values.
		56  	// The remaining 23+ bits give a method number for method values.
		57  	// If flag.kind() != Func, code can assume that flagMethod is unset.
		58  	// If ifaceIndir(typ), code can assume that flagIndir is set.
		59  	flag
		60  
		61  	// A method value represents a curried method invocation
		62  	// like r.Read for some receiver r. The typ+val+flag bits describe
		63  	// the receiver r, but the flag's Kind bits say Func (methods are
		64  	// functions), and the top bits of the flag give the method number
		65  	// in r's type's method table.
		66  }
		67  
		68  type flag uintptr
		69  
		70  const (
		71  	flagKindWidth				= 5 // there are 27 kinds
		72  	flagKindMask		flag = 1<<flagKindWidth - 1
		73  	flagStickyRO		flag = 1 << 5
		74  	flagEmbedRO		 flag = 1 << 6
		75  	flagIndir			 flag = 1 << 7
		76  	flagAddr				flag = 1 << 8
		77  	flagMethod			flag = 1 << 9
		78  	flagMethodShift			= 10
		79  	flagRO					flag = flagStickyRO | flagEmbedRO
		80  )
		81  
		82  func (f flag) kind() Kind {
		83  	return Kind(f & flagKindMask)
		84  }
		85  
		86  func (f flag) ro() flag {
		87  	if f&flagRO != 0 {
		88  		return flagStickyRO
		89  	}
		90  	return 0
		91  }
		92  
		93  // pointer returns the underlying pointer represented by v.
		94  // v.Kind() must be Ptr, Map, Chan, Func, or UnsafePointer
		95  // if v.Kind() == Ptr, the base type must not be go:notinheap.
		96  func (v Value) pointer() unsafe.Pointer {
		97  	if v.typ.size != ptrSize || !v.typ.pointers() {
		98  		panic("can't call pointer on a non-pointer Value")
		99  	}
	 100  	if v.flag&flagIndir != 0 {
	 101  		return *(*unsafe.Pointer)(v.ptr)
	 102  	}
	 103  	return v.ptr
	 104  }
	 105  
	 106  // packEface converts v to the empty interface.
	 107  func packEface(v Value) interface{} {
	 108  	t := v.typ
	 109  	var i interface{}
	 110  	e := (*emptyInterface)(unsafe.Pointer(&i))
	 111  	// First, fill in the data portion of the interface.
	 112  	switch {
	 113  	case ifaceIndir(t):
	 114  		if v.flag&flagIndir == 0 {
	 115  			panic("bad indir")
	 116  		}
	 117  		// Value is indirect, and so is the interface we're making.
	 118  		ptr := v.ptr
	 119  		if v.flag&flagAddr != 0 {
	 120  			// TODO: pass safe boolean from valueInterface so
	 121  			// we don't need to copy if safe==true?
	 122  			c := unsafe_New(t)
	 123  			typedmemmove(t, c, ptr)
	 124  			ptr = c
	 125  		}
	 126  		e.word = ptr
	 127  	case v.flag&flagIndir != 0:
	 128  		// Value is indirect, but interface is direct. We need
	 129  		// to load the data at v.ptr into the interface data word.
	 130  		e.word = *(*unsafe.Pointer)(v.ptr)
	 131  	default:
	 132  		// Value is direct, and so is the interface.
	 133  		e.word = v.ptr
	 134  	}
	 135  	// Now, fill in the type portion. We're very careful here not
	 136  	// to have any operation between the e.word and e.typ assignments
	 137  	// that would let the garbage collector observe the partially-built
	 138  	// interface value.
	 139  	e.typ = t
	 140  	return i
	 141  }
	 142  
	 143  // unpackEface converts the empty interface i to a Value.
	 144  func unpackEface(i interface{}) Value {
	 145  	e := (*emptyInterface)(unsafe.Pointer(&i))
	 146  	// NOTE: don't read e.word until we know whether it is really a pointer or not.
	 147  	t := e.typ
	 148  	if t == nil {
	 149  		return Value{}
	 150  	}
	 151  	f := flag(t.Kind())
	 152  	if ifaceIndir(t) {
	 153  		f |= flagIndir
	 154  	}
	 155  	return Value{t, e.word, f}
	 156  }
	 157  
	 158  // A ValueError occurs when a Value method is invoked on
	 159  // a Value that does not support it. Such cases are documented
	 160  // in the description of each method.
	 161  type ValueError struct {
	 162  	Method string
	 163  	Kind	 Kind
	 164  }
	 165  
	 166  func (e *ValueError) Error() string {
	 167  	if e.Kind == 0 {
	 168  		return "reflect: call of " + e.Method + " on zero Value"
	 169  	}
	 170  	return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
	 171  }
	 172  
	 173  // methodName returns the name of the calling method,
	 174  // assumed to be two stack frames above.
	 175  func methodName() string {
	 176  	pc, _, _, _ := runtime.Caller(2)
	 177  	f := runtime.FuncForPC(pc)
	 178  	if f == nil {
	 179  		return "unknown method"
	 180  	}
	 181  	return f.Name()
	 182  }
	 183  
	 184  // methodNameSkip is like methodName, but skips another stack frame.
	 185  // This is a separate function so that reflect.flag.mustBe will be inlined.
	 186  func methodNameSkip() string {
	 187  	pc, _, _, _ := runtime.Caller(3)
	 188  	f := runtime.FuncForPC(pc)
	 189  	if f == nil {
	 190  		return "unknown method"
	 191  	}
	 192  	return f.Name()
	 193  }
	 194  
	 195  // emptyInterface is the header for an interface{} value.
	 196  type emptyInterface struct {
	 197  	typ	*rtype
	 198  	word unsafe.Pointer
	 199  }
	 200  
	 201  // nonEmptyInterface is the header for an interface value with methods.
	 202  type nonEmptyInterface struct {
	 203  	// see ../runtime/iface.go:/Itab
	 204  	itab *struct {
	 205  		ityp *rtype // static interface type
	 206  		typ	*rtype // dynamic concrete type
	 207  		hash uint32 // copy of typ.hash
	 208  		_		[4]byte
	 209  		fun	[100000]unsafe.Pointer // method table
	 210  	}
	 211  	word unsafe.Pointer
	 212  }
	 213  
	 214  // mustBe panics if f's kind is not expected.
	 215  // Making this a method on flag instead of on Value
	 216  // (and embedding flag in Value) means that we can write
	 217  // the very clear v.mustBe(Bool) and have it compile into
	 218  // v.flag.mustBe(Bool), which will only bother to copy the
	 219  // single important word for the receiver.
	 220  func (f flag) mustBe(expected Kind) {
	 221  	// TODO(mvdan): use f.kind() again once mid-stack inlining gets better
	 222  	if Kind(f&flagKindMask) != expected {
	 223  		panic(&ValueError{methodName(), f.kind()})
	 224  	}
	 225  }
	 226  
	 227  // mustBeExported panics if f records that the value was obtained using
	 228  // an unexported field.
	 229  func (f flag) mustBeExported() {
	 230  	if f == 0 || f&flagRO != 0 {
	 231  		f.mustBeExportedSlow()
	 232  	}
	 233  }
	 234  
	 235  func (f flag) mustBeExportedSlow() {
	 236  	if f == 0 {
	 237  		panic(&ValueError{methodNameSkip(), Invalid})
	 238  	}
	 239  	if f&flagRO != 0 {
	 240  		panic("reflect: " + methodNameSkip() + " using value obtained using unexported field")
	 241  	}
	 242  }
	 243  
	 244  // mustBeAssignable panics if f records that the value is not assignable,
	 245  // which is to say that either it was obtained using an unexported field
	 246  // or it is not addressable.
	 247  func (f flag) mustBeAssignable() {
	 248  	if f&flagRO != 0 || f&flagAddr == 0 {
	 249  		f.mustBeAssignableSlow()
	 250  	}
	 251  }
	 252  
	 253  func (f flag) mustBeAssignableSlow() {
	 254  	if f == 0 {
	 255  		panic(&ValueError{methodNameSkip(), Invalid})
	 256  	}
	 257  	// Assignable if addressable and not read-only.
	 258  	if f&flagRO != 0 {
	 259  		panic("reflect: " + methodNameSkip() + " using value obtained using unexported field")
	 260  	}
	 261  	if f&flagAddr == 0 {
	 262  		panic("reflect: " + methodNameSkip() + " using unaddressable value")
	 263  	}
	 264  }
	 265  
	 266  // Addr returns a pointer value representing the address of v.
	 267  // It panics if CanAddr() returns false.
	 268  // Addr is typically used to obtain a pointer to a struct field
	 269  // or slice element in order to call a method that requires a
	 270  // pointer receiver.
	 271  func (v Value) Addr() Value {
	 272  	if v.flag&flagAddr == 0 {
	 273  		panic("reflect.Value.Addr of unaddressable value")
	 274  	}
	 275  	// Preserve flagRO instead of using v.flag.ro() so that
	 276  	// v.Addr().Elem() is equivalent to v (#32772)
	 277  	fl := v.flag & flagRO
	 278  	return Value{v.typ.ptrTo(), v.ptr, fl | flag(Ptr)}
	 279  }
	 280  
	 281  // Bool returns v's underlying value.
	 282  // It panics if v's kind is not Bool.
	 283  func (v Value) Bool() bool {
	 284  	v.mustBe(Bool)
	 285  	return *(*bool)(v.ptr)
	 286  }
	 287  
	 288  // Bytes returns v's underlying value.
	 289  // It panics if v's underlying value is not a slice of bytes.
	 290  func (v Value) Bytes() []byte {
	 291  	v.mustBe(Slice)
	 292  	if v.typ.Elem().Kind() != Uint8 {
	 293  		panic("reflect.Value.Bytes of non-byte slice")
	 294  	}
	 295  	// Slice is always bigger than a word; assume flagIndir.
	 296  	return *(*[]byte)(v.ptr)
	 297  }
	 298  
	 299  // runes returns v's underlying value.
	 300  // It panics if v's underlying value is not a slice of runes (int32s).
	 301  func (v Value) runes() []rune {
	 302  	v.mustBe(Slice)
	 303  	if v.typ.Elem().Kind() != Int32 {
	 304  		panic("reflect.Value.Bytes of non-rune slice")
	 305  	}
	 306  	// Slice is always bigger than a word; assume flagIndir.
	 307  	return *(*[]rune)(v.ptr)
	 308  }
	 309  
	 310  // CanAddr reports whether the value's address can be obtained with Addr.
	 311  // Such values are called addressable. A value is addressable if it is
	 312  // an element of a slice, an element of an addressable array,
	 313  // a field of an addressable struct, or the result of dereferencing a pointer.
	 314  // If CanAddr returns false, calling Addr will panic.
	 315  func (v Value) CanAddr() bool {
	 316  	return v.flag&flagAddr != 0
	 317  }
	 318  
	 319  // CanSet reports whether the value of v can be changed.
	 320  // A Value can be changed only if it is addressable and was not
	 321  // obtained by the use of unexported struct fields.
	 322  // If CanSet returns false, calling Set or any type-specific
	 323  // setter (e.g., SetBool, SetInt) will panic.
	 324  func (v Value) CanSet() bool {
	 325  	return v.flag&(flagAddr|flagRO) == flagAddr
	 326  }
	 327  
	 328  // Call calls the function v with the input arguments in.
	 329  // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).
	 330  // Call panics if v's Kind is not Func.
	 331  // It returns the output results as Values.
	 332  // As in Go, each input argument must be assignable to the
	 333  // type of the function's corresponding input parameter.
	 334  // If v is a variadic function, Call creates the variadic slice parameter
	 335  // itself, copying in the corresponding values.
	 336  func (v Value) Call(in []Value) []Value {
	 337  	v.mustBe(Func)
	 338  	v.mustBeExported()
	 339  	return v.call("Call", in)
	 340  }
	 341  
	 342  // CallSlice calls the variadic function v with the input arguments in,
	 343  // assigning the slice in[len(in)-1] to v's final variadic argument.
	 344  // For example, if len(in) == 3, v.CallSlice(in) represents the Go call v(in[0], in[1], in[2]...).
	 345  // CallSlice panics if v's Kind is not Func or if v is not variadic.
	 346  // It returns the output results as Values.
	 347  // As in Go, each input argument must be assignable to the
	 348  // type of the function's corresponding input parameter.
	 349  func (v Value) CallSlice(in []Value) []Value {
	 350  	v.mustBe(Func)
	 351  	v.mustBeExported()
	 352  	return v.call("CallSlice", in)
	 353  }
	 354  
	 355  var callGC bool // for testing; see TestCallMethodJump and TestCallArgLive
	 356  
	 357  const debugReflectCall = false
	 358  
	 359  func (v Value) call(op string, in []Value) []Value {
	 360  	// Get function pointer, type.
	 361  	t := (*funcType)(unsafe.Pointer(v.typ))
	 362  	var (
	 363  		fn			 unsafe.Pointer
	 364  		rcvr		 Value
	 365  		rcvrtype *rtype
	 366  	)
	 367  	if v.flag&flagMethod != 0 {
	 368  		rcvr = v
	 369  		rcvrtype, t, fn = methodReceiver(op, v, int(v.flag)>>flagMethodShift)
	 370  	} else if v.flag&flagIndir != 0 {
	 371  		fn = *(*unsafe.Pointer)(v.ptr)
	 372  	} else {
	 373  		fn = v.ptr
	 374  	}
	 375  
	 376  	if fn == nil {
	 377  		panic("reflect.Value.Call: call of nil function")
	 378  	}
	 379  
	 380  	isSlice := op == "CallSlice"
	 381  	n := t.NumIn()
	 382  	isVariadic := t.IsVariadic()
	 383  	if isSlice {
	 384  		if !isVariadic {
	 385  			panic("reflect: CallSlice of non-variadic function")
	 386  		}
	 387  		if len(in) < n {
	 388  			panic("reflect: CallSlice with too few input arguments")
	 389  		}
	 390  		if len(in) > n {
	 391  			panic("reflect: CallSlice with too many input arguments")
	 392  		}
	 393  	} else {
	 394  		if isVariadic {
	 395  			n--
	 396  		}
	 397  		if len(in) < n {
	 398  			panic("reflect: Call with too few input arguments")
	 399  		}
	 400  		if !isVariadic && len(in) > n {
	 401  			panic("reflect: Call with too many input arguments")
	 402  		}
	 403  	}
	 404  	for _, x := range in {
	 405  		if x.Kind() == Invalid {
	 406  			panic("reflect: " + op + " using zero Value argument")
	 407  		}
	 408  	}
	 409  	for i := 0; i < n; i++ {
	 410  		if xt, targ := in[i].Type(), t.In(i); !xt.AssignableTo(targ) {
	 411  			panic("reflect: " + op + " using " + xt.String() + " as type " + targ.String())
	 412  		}
	 413  	}
	 414  	if !isSlice && isVariadic {
	 415  		// prepare slice for remaining values
	 416  		m := len(in) - n
	 417  		slice := MakeSlice(t.In(n), m, m)
	 418  		elem := t.In(n).Elem()
	 419  		for i := 0; i < m; i++ {
	 420  			x := in[n+i]
	 421  			if xt := x.Type(); !xt.AssignableTo(elem) {
	 422  				panic("reflect: cannot use " + xt.String() + " as type " + elem.String() + " in " + op)
	 423  			}
	 424  			slice.Index(i).Set(x)
	 425  		}
	 426  		origIn := in
	 427  		in = make([]Value, n+1)
	 428  		copy(in[:n], origIn)
	 429  		in[n] = slice
	 430  	}
	 431  
	 432  	nin := len(in)
	 433  	if nin != t.NumIn() {
	 434  		panic("reflect.Value.Call: wrong argument count")
	 435  	}
	 436  	nout := t.NumOut()
	 437  
	 438  	// Register argument space.
	 439  	var regArgs abi.RegArgs
	 440  
	 441  	// Compute frame type.
	 442  	frametype, framePool, abi := funcLayout(t, rcvrtype)
	 443  
	 444  	// Allocate a chunk of memory for frame if needed.
	 445  	var stackArgs unsafe.Pointer
	 446  	if frametype.size != 0 {
	 447  		if nout == 0 {
	 448  			stackArgs = framePool.Get().(unsafe.Pointer)
	 449  		} else {
	 450  			// Can't use pool if the function has return values.
	 451  			// We will leak pointer to args in ret, so its lifetime is not scoped.
	 452  			stackArgs = unsafe_New(frametype)
	 453  		}
	 454  	}
	 455  	frameSize := frametype.size
	 456  
	 457  	if debugReflectCall {
	 458  		println("reflect.call", t.String())
	 459  		abi.dump()
	 460  	}
	 461  
	 462  	// Copy inputs into args.
	 463  
	 464  	// Handle receiver.
	 465  	inStart := 0
	 466  	if rcvrtype != nil {
	 467  		// Guaranteed to only be one word in size,
	 468  		// so it will only take up exactly 1 abiStep (either
	 469  		// in a register or on the stack).
	 470  		switch st := abi.call.steps[0]; st.kind {
	 471  		case abiStepStack:
	 472  			storeRcvr(rcvr, stackArgs)
	 473  		case abiStepIntReg, abiStepPointer:
	 474  			// Even pointers can go into the uintptr slot because
	 475  			// they'll be kept alive by the Values referenced by
	 476  			// this frame. Reflection forces these to be heap-allocated,
	 477  			// so we don't need to worry about stack copying.
	 478  			storeRcvr(rcvr, unsafe.Pointer(&regArgs.Ints[st.ireg]))
	 479  		case abiStepFloatReg:
	 480  			storeRcvr(rcvr, unsafe.Pointer(&regArgs.Floats[st.freg]))
	 481  		default:
	 482  			panic("unknown ABI parameter kind")
	 483  		}
	 484  		inStart = 1
	 485  	}
	 486  
	 487  	// Handle arguments.
	 488  	for i, v := range in {
	 489  		v.mustBeExported()
	 490  		targ := t.In(i).(*rtype)
	 491  		// TODO(mknyszek): Figure out if it's possible to get some
	 492  		// scratch space for this assignment check. Previously, it
	 493  		// was possible to use space in the argument frame.
	 494  		v = v.assignTo("reflect.Value.Call", targ, nil)
	 495  	stepsLoop:
	 496  		for _, st := range abi.call.stepsForValue(i + inStart) {
	 497  			switch st.kind {
	 498  			case abiStepStack:
	 499  				// Copy values to the "stack."
	 500  				addr := add(stackArgs, st.stkOff, "precomputed stack arg offset")
	 501  				if v.flag&flagIndir != 0 {
	 502  					typedmemmove(targ, addr, v.ptr)
	 503  				} else {
	 504  					*(*unsafe.Pointer)(addr) = v.ptr
	 505  				}
	 506  				// There's only one step for a stack-allocated value.
	 507  				break stepsLoop
	 508  			case abiStepIntReg, abiStepPointer:
	 509  				// Copy values to "integer registers."
	 510  				if v.flag&flagIndir != 0 {
	 511  					offset := add(v.ptr, st.offset, "precomputed value offset")
	 512  					if st.kind == abiStepPointer {
	 513  						// Duplicate this pointer in the pointer area of the
	 514  						// register space. Otherwise, there's the potential for
	 515  						// this to be the last reference to v.ptr.
	 516  						regArgs.Ptrs[st.ireg] = *(*unsafe.Pointer)(offset)
	 517  					}
	 518  					memmove(unsafe.Pointer(&regArgs.Ints[st.ireg]), offset, st.size)
	 519  				} else {
	 520  					if st.kind == abiStepPointer {
	 521  						// See the comment in abiStepPointer case above.
	 522  						regArgs.Ptrs[st.ireg] = v.ptr
	 523  					}
	 524  					regArgs.Ints[st.ireg] = uintptr(v.ptr)
	 525  				}
	 526  			case abiStepFloatReg:
	 527  				// Copy values to "float registers."
	 528  				if v.flag&flagIndir == 0 {
	 529  					panic("attempted to copy pointer to FP register")
	 530  				}
	 531  				offset := add(v.ptr, st.offset, "precomputed value offset")
	 532  				memmove(unsafe.Pointer(&regArgs.Floats[st.freg]), offset, st.size)
	 533  			default:
	 534  				panic("unknown ABI part kind")
	 535  			}
	 536  		}
	 537  	}
	 538  	// TODO(mknyszek): Remove this when we no longer have
	 539  	// caller reserved spill space.
	 540  	frameSize = align(frameSize, ptrSize)
	 541  	frameSize += abi.spill
	 542  
	 543  	// Mark pointers in registers for the return path.
	 544  	regArgs.ReturnIsPtr = abi.outRegPtrs
	 545  
	 546  	if debugReflectCall {
	 547  		regArgs.Dump()
	 548  	}
	 549  
	 550  	// For testing; see TestCallArgLive.
	 551  	if callGC {
	 552  		runtime.GC()
	 553  	}
	 554  
	 555  	// Call.
	 556  	call(frametype, fn, stackArgs, uint32(frametype.size), uint32(abi.retOffset), uint32(frameSize), &regArgs)
	 557  
	 558  	// For testing; see TestCallMethodJump.
	 559  	if callGC {
	 560  		runtime.GC()
	 561  	}
	 562  
	 563  	var ret []Value
	 564  	if nout == 0 {
	 565  		if stackArgs != nil {
	 566  			typedmemclr(frametype, stackArgs)
	 567  			framePool.Put(stackArgs)
	 568  		}
	 569  	} else {
	 570  		if stackArgs != nil {
	 571  			// Zero the now unused input area of args,
	 572  			// because the Values returned by this function contain pointers to the args object,
	 573  			// and will thus keep the args object alive indefinitely.
	 574  			typedmemclrpartial(frametype, stackArgs, 0, abi.retOffset)
	 575  		}
	 576  
	 577  		// Wrap Values around return values in args.
	 578  		ret = make([]Value, nout)
	 579  		for i := 0; i < nout; i++ {
	 580  			tv := t.Out(i)
	 581  			if tv.Size() == 0 {
	 582  				// For zero-sized return value, args+off may point to the next object.
	 583  				// In this case, return the zero value instead.
	 584  				ret[i] = Zero(tv)
	 585  				continue
	 586  			}
	 587  			steps := abi.ret.stepsForValue(i)
	 588  			if st := steps[0]; st.kind == abiStepStack {
	 589  				// This value is on the stack. If part of a value is stack
	 590  				// allocated, the entire value is according to the ABI. So
	 591  				// just make an indirection into the allocated frame.
	 592  				fl := flagIndir | flag(tv.Kind())
	 593  				ret[i] = Value{tv.common(), add(stackArgs, st.stkOff, "tv.Size() != 0"), fl}
	 594  				// Note: this does introduce false sharing between results -
	 595  				// if any result is live, they are all live.
	 596  				// (And the space for the args is live as well, but as we've
	 597  				// cleared that space it isn't as big a deal.)
	 598  				continue
	 599  			}
	 600  
	 601  			// Handle pointers passed in registers.
	 602  			if !ifaceIndir(tv.common()) {
	 603  				// Pointer-valued data gets put directly
	 604  				// into v.ptr.
	 605  				if steps[0].kind != abiStepPointer {
	 606  					print("kind=", steps[0].kind, ", type=", tv.String(), "\n")
	 607  					panic("mismatch between ABI description and types")
	 608  				}
	 609  				ret[i] = Value{tv.common(), regArgs.Ptrs[steps[0].ireg], flag(tv.Kind())}
	 610  				continue
	 611  			}
	 612  
	 613  			// All that's left is values passed in registers that we need to
	 614  			// create space for and copy values back into.
	 615  			//
	 616  			// TODO(mknyszek): We make a new allocation for each register-allocated
	 617  			// value, but previously we could always point into the heap-allocated
	 618  			// stack frame. This is a regression that could be fixed by adding
	 619  			// additional space to the allocated stack frame and storing the
	 620  			// register-allocated return values into the allocated stack frame and
	 621  			// referring there in the resulting Value.
	 622  			s := unsafe_New(tv.common())
	 623  			for _, st := range steps {
	 624  				switch st.kind {
	 625  				case abiStepIntReg:
	 626  					offset := add(s, st.offset, "precomputed value offset")
	 627  					memmove(offset, unsafe.Pointer(&regArgs.Ints[st.ireg]), st.size)
	 628  				case abiStepPointer:
	 629  					s := add(s, st.offset, "precomputed value offset")
	 630  					*((*unsafe.Pointer)(s)) = regArgs.Ptrs[st.ireg]
	 631  				case abiStepFloatReg:
	 632  					offset := add(s, st.offset, "precomputed value offset")
	 633  					memmove(offset, unsafe.Pointer(&regArgs.Floats[st.freg]), st.size)
	 634  				case abiStepStack:
	 635  					panic("register-based return value has stack component")
	 636  				default:
	 637  					panic("unknown ABI part kind")
	 638  				}
	 639  			}
	 640  			ret[i] = Value{tv.common(), s, flagIndir | flag(tv.Kind())}
	 641  		}
	 642  	}
	 643  
	 644  	return ret
	 645  }
	 646  
	 647  // callReflect is the call implementation used by a function
	 648  // returned by MakeFunc. In many ways it is the opposite of the
	 649  // method Value.call above. The method above converts a call using Values
	 650  // into a call of a function with a concrete argument frame, while
	 651  // callReflect converts a call of a function with a concrete argument
	 652  // frame into a call using Values.
	 653  // It is in this file so that it can be next to the call method above.
	 654  // The remainder of the MakeFunc implementation is in makefunc.go.
	 655  //
	 656  // NOTE: This function must be marked as a "wrapper" in the generated code,
	 657  // so that the linker can make it work correctly for panic and recover.
	 658  // The gc compilers know to do that for the name "reflect.callReflect".
	 659  //
	 660  // ctxt is the "closure" generated by MakeFunc.
	 661  // frame is a pointer to the arguments to that closure on the stack.
	 662  // retValid points to a boolean which should be set when the results
	 663  // section of frame is set.
	 664  //
	 665  // regs contains the argument values passed in registers and will contain
	 666  // the values returned from ctxt.fn in registers.
	 667  func callReflect(ctxt *makeFuncImpl, frame unsafe.Pointer, retValid *bool, regs *abi.RegArgs) {
	 668  	if callGC {
	 669  		// Call GC upon entry during testing.
	 670  		// Getting our stack scanned here is the biggest hazard, because
	 671  		// our caller (makeFuncStub) could have failed to place the last
	 672  		// pointer to a value in regs' pointer space, in which case it
	 673  		// won't be visible to the GC.
	 674  		runtime.GC()
	 675  	}
	 676  	ftyp := ctxt.ftyp
	 677  	f := ctxt.fn
	 678  
	 679  	_, _, abi := funcLayout(ftyp, nil)
	 680  
	 681  	// Copy arguments into Values.
	 682  	ptr := frame
	 683  	in := make([]Value, 0, int(ftyp.inCount))
	 684  	for i, typ := range ftyp.in() {
	 685  		if typ.Size() == 0 {
	 686  			in = append(in, Zero(typ))
	 687  			continue
	 688  		}
	 689  		v := Value{typ, nil, flag(typ.Kind())}
	 690  		steps := abi.call.stepsForValue(i)
	 691  		if st := steps[0]; st.kind == abiStepStack {
	 692  			if ifaceIndir(typ) {
	 693  				// value cannot be inlined in interface data.
	 694  				// Must make a copy, because f might keep a reference to it,
	 695  				// and we cannot let f keep a reference to the stack frame
	 696  				// after this function returns, not even a read-only reference.
	 697  				v.ptr = unsafe_New(typ)
	 698  				if typ.size > 0 {
	 699  					typedmemmove(typ, v.ptr, add(ptr, st.stkOff, "typ.size > 0"))
	 700  				}
	 701  				v.flag |= flagIndir
	 702  			} else {
	 703  				v.ptr = *(*unsafe.Pointer)(add(ptr, st.stkOff, "1-ptr"))
	 704  			}
	 705  		} else {
	 706  			if ifaceIndir(typ) {
	 707  				// All that's left is values passed in registers that we need to
	 708  				// create space for the values.
	 709  				v.flag |= flagIndir
	 710  				v.ptr = unsafe_New(typ)
	 711  				for _, st := range steps {
	 712  					switch st.kind {
	 713  					case abiStepIntReg:
	 714  						offset := add(v.ptr, st.offset, "precomputed value offset")
	 715  						memmove(offset, unsafe.Pointer(&regs.Ints[st.ireg]), st.size)
	 716  					case abiStepPointer:
	 717  						s := add(v.ptr, st.offset, "precomputed value offset")
	 718  						*((*unsafe.Pointer)(s)) = regs.Ptrs[st.ireg]
	 719  					case abiStepFloatReg:
	 720  						offset := add(v.ptr, st.offset, "precomputed value offset")
	 721  						memmove(offset, unsafe.Pointer(&regs.Floats[st.freg]), st.size)
	 722  					case abiStepStack:
	 723  						panic("register-based return value has stack component")
	 724  					default:
	 725  						panic("unknown ABI part kind")
	 726  					}
	 727  				}
	 728  			} else {
	 729  				// Pointer-valued data gets put directly
	 730  				// into v.ptr.
	 731  				if steps[0].kind != abiStepPointer {
	 732  					print("kind=", steps[0].kind, ", type=", typ.String(), "\n")
	 733  					panic("mismatch between ABI description and types")
	 734  				}
	 735  				v.ptr = regs.Ptrs[steps[0].ireg]
	 736  			}
	 737  		}
	 738  		in = append(in, v)
	 739  	}
	 740  
	 741  	// Call underlying function.
	 742  	out := f(in)
	 743  	numOut := ftyp.NumOut()
	 744  	if len(out) != numOut {
	 745  		panic("reflect: wrong return count from function created by MakeFunc")
	 746  	}
	 747  
	 748  	// Copy results back into argument frame and register space.
	 749  	if numOut > 0 {
	 750  		for i, typ := range ftyp.out() {
	 751  			v := out[i]
	 752  			if v.typ == nil {
	 753  				panic("reflect: function created by MakeFunc using " + funcName(f) +
	 754  					" returned zero Value")
	 755  			}
	 756  			if v.flag&flagRO != 0 {
	 757  				panic("reflect: function created by MakeFunc using " + funcName(f) +
	 758  					" returned value obtained from unexported field")
	 759  			}
	 760  			if typ.size == 0 {
	 761  				continue
	 762  			}
	 763  
	 764  			// Convert v to type typ if v is assignable to a variable
	 765  			// of type t in the language spec.
	 766  			// See issue 28761.
	 767  			//
	 768  			//
	 769  			// TODO(mknyszek): In the switch to the register ABI we lost
	 770  			// the scratch space here for the register cases (and
	 771  			// temporarily for all the cases).
	 772  			//
	 773  			// If/when this happens, take note of the following:
	 774  			//
	 775  			// We must clear the destination before calling assignTo,
	 776  			// in case assignTo writes (with memory barriers) to the
	 777  			// target location used as scratch space. See issue 39541.
	 778  			v = v.assignTo("reflect.MakeFunc", typ, nil)
	 779  		stepsLoop:
	 780  			for _, st := range abi.ret.stepsForValue(i) {
	 781  				switch st.kind {
	 782  				case abiStepStack:
	 783  					// Copy values to the "stack."
	 784  					addr := add(ptr, st.stkOff, "precomputed stack arg offset")
	 785  					// Do not use write barriers. The stack space used
	 786  					// for this call is not adequately zeroed, and we
	 787  					// are careful to keep the arguments alive until we
	 788  					// return to makeFuncStub's caller.
	 789  					if v.flag&flagIndir != 0 {
	 790  						memmove(addr, v.ptr, st.size)
	 791  					} else {
	 792  						// This case must be a pointer type.
	 793  						*(*uintptr)(addr) = uintptr(v.ptr)
	 794  					}
	 795  					// There's only one step for a stack-allocated value.
	 796  					break stepsLoop
	 797  				case abiStepIntReg, abiStepPointer:
	 798  					// Copy values to "integer registers."
	 799  					if v.flag&flagIndir != 0 {
	 800  						offset := add(v.ptr, st.offset, "precomputed value offset")
	 801  						memmove(unsafe.Pointer(&regs.Ints[st.ireg]), offset, st.size)
	 802  					} else {
	 803  						// Only populate the Ints space on the return path.
	 804  						// This is safe because out is kept alive until the
	 805  						// end of this function, and the return path through
	 806  						// makeFuncStub has no preemption, so these pointers
	 807  						// are always visible to the GC.
	 808  						regs.Ints[st.ireg] = uintptr(v.ptr)
	 809  					}
	 810  				case abiStepFloatReg:
	 811  					// Copy values to "float registers."
	 812  					if v.flag&flagIndir == 0 {
	 813  						panic("attempted to copy pointer to FP register")
	 814  					}
	 815  					offset := add(v.ptr, st.offset, "precomputed value offset")
	 816  					memmove(unsafe.Pointer(&regs.Floats[st.freg]), offset, st.size)
	 817  				default:
	 818  					panic("unknown ABI part kind")
	 819  				}
	 820  			}
	 821  		}
	 822  	}
	 823  
	 824  	// Announce that the return values are valid.
	 825  	// After this point the runtime can depend on the return values being valid.
	 826  	*retValid = true
	 827  
	 828  	// We have to make sure that the out slice lives at least until
	 829  	// the runtime knows the return values are valid. Otherwise, the
	 830  	// return values might not be scanned by anyone during a GC.
	 831  	// (out would be dead, and the return slots not yet alive.)
	 832  	runtime.KeepAlive(out)
	 833  
	 834  	// runtime.getArgInfo expects to be able to find ctxt on the
	 835  	// stack when it finds our caller, makeFuncStub. Make sure it
	 836  	// doesn't get garbage collected.
	 837  	runtime.KeepAlive(ctxt)
	 838  }
	 839  
	 840  // methodReceiver returns information about the receiver
	 841  // described by v. The Value v may or may not have the
	 842  // flagMethod bit set, so the kind cached in v.flag should
	 843  // not be used.
	 844  // The return value rcvrtype gives the method's actual receiver type.
	 845  // The return value t gives the method type signature (without the receiver).
	 846  // The return value fn is a pointer to the method code.
	 847  func methodReceiver(op string, v Value, methodIndex int) (rcvrtype *rtype, t *funcType, fn unsafe.Pointer) {
	 848  	i := methodIndex
	 849  	if v.typ.Kind() == Interface {
	 850  		tt := (*interfaceType)(unsafe.Pointer(v.typ))
	 851  		if uint(i) >= uint(len(tt.methods)) {
	 852  			panic("reflect: internal error: invalid method index")
	 853  		}
	 854  		m := &tt.methods[i]
	 855  		if !tt.nameOff(m.name).isExported() {
	 856  			panic("reflect: " + op + " of unexported method")
	 857  		}
	 858  		iface := (*nonEmptyInterface)(v.ptr)
	 859  		if iface.itab == nil {
	 860  			panic("reflect: " + op + " of method on nil interface value")
	 861  		}
	 862  		rcvrtype = iface.itab.typ
	 863  		fn = unsafe.Pointer(&iface.itab.fun[i])
	 864  		t = (*funcType)(unsafe.Pointer(tt.typeOff(m.typ)))
	 865  	} else {
	 866  		rcvrtype = v.typ
	 867  		ms := v.typ.exportedMethods()
	 868  		if uint(i) >= uint(len(ms)) {
	 869  			panic("reflect: internal error: invalid method index")
	 870  		}
	 871  		m := ms[i]
	 872  		if !v.typ.nameOff(m.name).isExported() {
	 873  			panic("reflect: " + op + " of unexported method")
	 874  		}
	 875  		ifn := v.typ.textOff(m.ifn)
	 876  		fn = unsafe.Pointer(&ifn)
	 877  		t = (*funcType)(unsafe.Pointer(v.typ.typeOff(m.mtyp)))
	 878  	}
	 879  	return
	 880  }
	 881  
	 882  // v is a method receiver. Store at p the word which is used to
	 883  // encode that receiver at the start of the argument list.
	 884  // Reflect uses the "interface" calling convention for
	 885  // methods, which always uses one word to record the receiver.
	 886  func storeRcvr(v Value, p unsafe.Pointer) {
	 887  	t := v.typ
	 888  	if t.Kind() == Interface {
	 889  		// the interface data word becomes the receiver word
	 890  		iface := (*nonEmptyInterface)(v.ptr)
	 891  		*(*unsafe.Pointer)(p) = iface.word
	 892  	} else if v.flag&flagIndir != 0 && !ifaceIndir(t) {
	 893  		*(*unsafe.Pointer)(p) = *(*unsafe.Pointer)(v.ptr)
	 894  	} else {
	 895  		*(*unsafe.Pointer)(p) = v.ptr
	 896  	}
	 897  }
	 898  
	 899  // align returns the result of rounding x up to a multiple of n.
	 900  // n must be a power of two.
	 901  func align(x, n uintptr) uintptr {
	 902  	return (x + n - 1) &^ (n - 1)
	 903  }
	 904  
	 905  // callMethod is the call implementation used by a function returned
	 906  // by makeMethodValue (used by v.Method(i).Interface()).
	 907  // It is a streamlined version of the usual reflect call: the caller has
	 908  // already laid out the argument frame for us, so we don't have
	 909  // to deal with individual Values for each argument.
	 910  // It is in this file so that it can be next to the two similar functions above.
	 911  // The remainder of the makeMethodValue implementation is in makefunc.go.
	 912  //
	 913  // NOTE: This function must be marked as a "wrapper" in the generated code,
	 914  // so that the linker can make it work correctly for panic and recover.
	 915  // The gc compilers know to do that for the name "reflect.callMethod".
	 916  //
	 917  // ctxt is the "closure" generated by makeVethodValue.
	 918  // frame is a pointer to the arguments to that closure on the stack.
	 919  // retValid points to a boolean which should be set when the results
	 920  // section of frame is set.
	 921  //
	 922  // regs contains the argument values passed in registers and will contain
	 923  // the values returned from ctxt.fn in registers.
	 924  func callMethod(ctxt *methodValue, frame unsafe.Pointer, retValid *bool, regs *abi.RegArgs) {
	 925  	rcvr := ctxt.rcvr
	 926  	rcvrType, valueFuncType, methodFn := methodReceiver("call", rcvr, ctxt.method)
	 927  
	 928  	// There are two ABIs at play here.
	 929  	//
	 930  	// methodValueCall was invoked with the ABI assuming there was no
	 931  	// receiver ("value ABI") and that's what frame and regs are holding.
	 932  	//
	 933  	// Meanwhile, we need to actually call the method with a receiver, which
	 934  	// has its own ABI ("method ABI"). Everything that follows is a translation
	 935  	// between the two.
	 936  	_, _, valueABI := funcLayout(valueFuncType, nil)
	 937  	valueFrame, valueRegs := frame, regs
	 938  	methodFrameType, methodFramePool, methodABI := funcLayout(valueFuncType, rcvrType)
	 939  
	 940  	// Make a new frame that is one word bigger so we can store the receiver.
	 941  	// This space is used for both arguments and return values.
	 942  	methodFrame := methodFramePool.Get().(unsafe.Pointer)
	 943  	var methodRegs abi.RegArgs
	 944  
	 945  	// Deal with the receiver. It's guaranteed to only be one word in size.
	 946  	if st := methodABI.call.steps[0]; st.kind == abiStepStack {
	 947  		// Only copy the reciever to the stack if the ABI says so.
	 948  		// Otherwise, it'll be in a register already.
	 949  		storeRcvr(rcvr, methodFrame)
	 950  	} else {
	 951  		// Put the receiver in a register.
	 952  		storeRcvr(rcvr, unsafe.Pointer(&methodRegs.Ints))
	 953  	}
	 954  
	 955  	// Translate the rest of the arguments.
	 956  	for i, t := range valueFuncType.in() {
	 957  		valueSteps := valueABI.call.stepsForValue(i)
	 958  		methodSteps := methodABI.call.stepsForValue(i + 1)
	 959  
	 960  		// Zero-sized types are trivial: nothing to do.
	 961  		if len(valueSteps) == 0 {
	 962  			if len(methodSteps) != 0 {
	 963  				panic("method ABI and value ABI do not align")
	 964  			}
	 965  			continue
	 966  		}
	 967  
	 968  		// There are four cases to handle in translating each
	 969  		// argument:
	 970  		// 1. Stack -> stack translation.
	 971  		// 2. Stack -> registers translation.
	 972  		// 3. Registers -> stack translation.
	 973  		// 4. Registers -> registers translation.
	 974  		// TODO(mknyszek): Cases 2 and 3 below only work on little endian
	 975  		// architectures. This is OK for now, but this needs to be fixed
	 976  		// before supporting the register ABI on big endian architectures.
	 977  
	 978  		// If the value ABI passes the value on the stack,
	 979  		// then the method ABI does too, because it has strictly
	 980  		// fewer arguments. Simply copy between the two.
	 981  		if vStep := valueSteps[0]; vStep.kind == abiStepStack {
	 982  			mStep := methodSteps[0]
	 983  			// Handle stack -> stack translation.
	 984  			if mStep.kind == abiStepStack {
	 985  				if vStep.size != mStep.size {
	 986  					panic("method ABI and value ABI do not align")
	 987  				}
	 988  				typedmemmove(t,
	 989  					add(methodFrame, mStep.stkOff, "precomputed stack offset"),
	 990  					add(valueFrame, vStep.stkOff, "precomputed stack offset"))
	 991  				continue
	 992  			}
	 993  			// Handle stack -> register translation.
	 994  			for _, mStep := range methodSteps {
	 995  				from := add(valueFrame, vStep.stkOff+mStep.offset, "precomputed stack offset")
	 996  				switch mStep.kind {
	 997  				case abiStepPointer:
	 998  					// Do the pointer copy directly so we get a write barrier.
	 999  					methodRegs.Ptrs[mStep.ireg] = *(*unsafe.Pointer)(from)
	1000  					fallthrough // We need to make sure this ends up in Ints, too.
	1001  				case abiStepIntReg:
	1002  					memmove(unsafe.Pointer(&methodRegs.Ints[mStep.ireg]), from, mStep.size)
	1003  				case abiStepFloatReg:
	1004  					memmove(unsafe.Pointer(&methodRegs.Floats[mStep.freg]), from, mStep.size)
	1005  				default:
	1006  					panic("unexpected method step")
	1007  				}
	1008  			}
	1009  			continue
	1010  		}
	1011  		// Handle register -> stack translation.
	1012  		if mStep := methodSteps[0]; mStep.kind == abiStepStack {
	1013  			for _, vStep := range valueSteps {
	1014  				to := add(methodFrame, mStep.stkOff+vStep.offset, "precomputed stack offset")
	1015  				switch vStep.kind {
	1016  				case abiStepPointer:
	1017  					// Do the pointer copy directly so we get a write barrier.
	1018  					*(*unsafe.Pointer)(to) = valueRegs.Ptrs[vStep.ireg]
	1019  				case abiStepIntReg:
	1020  					memmove(to, unsafe.Pointer(&valueRegs.Ints[vStep.ireg]), vStep.size)
	1021  				case abiStepFloatReg:
	1022  					memmove(to, unsafe.Pointer(&valueRegs.Floats[vStep.freg]), vStep.size)
	1023  				default:
	1024  					panic("unexpected value step")
	1025  				}
	1026  			}
	1027  			continue
	1028  		}
	1029  		// Handle register -> register translation.
	1030  		if len(valueSteps) != len(methodSteps) {
	1031  			// Because it's the same type for the value, and it's assigned
	1032  			// to registers both times, it should always take up the same
	1033  			// number of registers for each ABI.
	1034  			panic("method ABI and value ABI don't align")
	1035  		}
	1036  		for i, vStep := range valueSteps {
	1037  			mStep := methodSteps[i]
	1038  			if mStep.kind != vStep.kind {
	1039  				panic("method ABI and value ABI don't align")
	1040  			}
	1041  			switch vStep.kind {
	1042  			case abiStepPointer:
	1043  				// Copy this too, so we get a write barrier.
	1044  				methodRegs.Ptrs[mStep.ireg] = valueRegs.Ptrs[vStep.ireg]
	1045  				fallthrough
	1046  			case abiStepIntReg:
	1047  				methodRegs.Ints[mStep.ireg] = valueRegs.Ints[vStep.ireg]
	1048  			case abiStepFloatReg:
	1049  				methodRegs.Floats[mStep.freg] = valueRegs.Floats[vStep.freg]
	1050  			default:
	1051  				panic("unexpected value step")
	1052  			}
	1053  		}
	1054  	}
	1055  
	1056  	methodFrameSize := methodFrameType.size
	1057  	// TODO(mknyszek): Remove this when we no longer have
	1058  	// caller reserved spill space.
	1059  	methodFrameSize = align(methodFrameSize, ptrSize)
	1060  	methodFrameSize += methodABI.spill
	1061  
	1062  	// Mark pointers in registers for the return path.
	1063  	methodRegs.ReturnIsPtr = methodABI.outRegPtrs
	1064  
	1065  	// Call.
	1066  	// Call copies the arguments from scratch to the stack, calls fn,
	1067  	// and then copies the results back into scratch.
	1068  	call(methodFrameType, methodFn, methodFrame, uint32(methodFrameType.size), uint32(methodABI.retOffset), uint32(methodFrameSize), &methodRegs)
	1069  
	1070  	// Copy return values.
	1071  	//
	1072  	// This is somewhat simpler because both ABIs have an identical
	1073  	// return value ABI (the types are identical). As a result, register
	1074  	// results can simply be copied over. Stack-allocated values are laid
	1075  	// out the same, but are at different offsets from the start of the frame
	1076  	// Ignore any changes to args.
	1077  	// Avoid constructing out-of-bounds pointers if there are no return values.
	1078  	// because the arguments may be laid out differently.
	1079  	if valueRegs != nil {
	1080  		*valueRegs = methodRegs
	1081  	}
	1082  	if retSize := methodFrameType.size - methodABI.retOffset; retSize > 0 {
	1083  		valueRet := add(valueFrame, valueABI.retOffset, "valueFrame's size > retOffset")
	1084  		methodRet := add(methodFrame, methodABI.retOffset, "methodFrame's size > retOffset")
	1085  		// This copies to the stack. Write barriers are not needed.
	1086  		memmove(valueRet, methodRet, retSize)
	1087  	}
	1088  
	1089  	// Tell the runtime it can now depend on the return values
	1090  	// being properly initialized.
	1091  	*retValid = true
	1092  
	1093  	// Clear the scratch space and put it back in the pool.
	1094  	// This must happen after the statement above, so that the return
	1095  	// values will always be scanned by someone.
	1096  	typedmemclr(methodFrameType, methodFrame)
	1097  	methodFramePool.Put(methodFrame)
	1098  
	1099  	// See the comment in callReflect.
	1100  	runtime.KeepAlive(ctxt)
	1101  
	1102  	// Keep valueRegs alive because it may hold live pointer results.
	1103  	// The caller (methodValueCall) has it as a stack object, which is only
	1104  	// scanned when there is a reference to it.
	1105  	runtime.KeepAlive(valueRegs)
	1106  }
	1107  
	1108  // funcName returns the name of f, for use in error messages.
	1109  func funcName(f func([]Value) []Value) string {
	1110  	pc := *(*uintptr)(unsafe.Pointer(&f))
	1111  	rf := runtime.FuncForPC(pc)
	1112  	if rf != nil {
	1113  		return rf.Name()
	1114  	}
	1115  	return "closure"
	1116  }
	1117  
	1118  // Cap returns v's capacity.
	1119  // It panics if v's Kind is not Array, Chan, or Slice.
	1120  func (v Value) Cap() int {
	1121  	k := v.kind()
	1122  	switch k {
	1123  	case Array:
	1124  		return v.typ.Len()
	1125  	case Chan:
	1126  		return chancap(v.pointer())
	1127  	case Slice:
	1128  		// Slice is always bigger than a word; assume flagIndir.
	1129  		return (*unsafeheader.Slice)(v.ptr).Cap
	1130  	}
	1131  	panic(&ValueError{"reflect.Value.Cap", v.kind()})
	1132  }
	1133  
	1134  // Close closes the channel v.
	1135  // It panics if v's Kind is not Chan.
	1136  func (v Value) Close() {
	1137  	v.mustBe(Chan)
	1138  	v.mustBeExported()
	1139  	chanclose(v.pointer())
	1140  }
	1141  
	1142  // Complex returns v's underlying value, as a complex128.
	1143  // It panics if v's Kind is not Complex64 or Complex128
	1144  func (v Value) Complex() complex128 {
	1145  	k := v.kind()
	1146  	switch k {
	1147  	case Complex64:
	1148  		return complex128(*(*complex64)(v.ptr))
	1149  	case Complex128:
	1150  		return *(*complex128)(v.ptr)
	1151  	}
	1152  	panic(&ValueError{"reflect.Value.Complex", v.kind()})
	1153  }
	1154  
	1155  // Elem returns the value that the interface v contains
	1156  // or that the pointer v points to.
	1157  // It panics if v's Kind is not Interface or Ptr.
	1158  // It returns the zero Value if v is nil.
	1159  func (v Value) Elem() Value {
	1160  	k := v.kind()
	1161  	switch k {
	1162  	case Interface:
	1163  		var eface interface{}
	1164  		if v.typ.NumMethod() == 0 {
	1165  			eface = *(*interface{})(v.ptr)
	1166  		} else {
	1167  			eface = (interface{})(*(*interface {
	1168  				M()
	1169  			})(v.ptr))
	1170  		}
	1171  		x := unpackEface(eface)
	1172  		if x.flag != 0 {
	1173  			x.flag |= v.flag.ro()
	1174  		}
	1175  		return x
	1176  	case Ptr:
	1177  		ptr := v.ptr
	1178  		if v.flag&flagIndir != 0 {
	1179  			ptr = *(*unsafe.Pointer)(ptr)
	1180  		}
	1181  		// The returned value's address is v's value.
	1182  		if ptr == nil {
	1183  			return Value{}
	1184  		}
	1185  		tt := (*ptrType)(unsafe.Pointer(v.typ))
	1186  		typ := tt.elem
	1187  		fl := v.flag&flagRO | flagIndir | flagAddr
	1188  		fl |= flag(typ.Kind())
	1189  		return Value{typ, ptr, fl}
	1190  	}
	1191  	panic(&ValueError{"reflect.Value.Elem", v.kind()})
	1192  }
	1193  
	1194  // Field returns the i'th field of the struct v.
	1195  // It panics if v's Kind is not Struct or i is out of range.
	1196  func (v Value) Field(i int) Value {
	1197  	if v.kind() != Struct {
	1198  		panic(&ValueError{"reflect.Value.Field", v.kind()})
	1199  	}
	1200  	tt := (*structType)(unsafe.Pointer(v.typ))
	1201  	if uint(i) >= uint(len(tt.fields)) {
	1202  		panic("reflect: Field index out of range")
	1203  	}
	1204  	field := &tt.fields[i]
	1205  	typ := field.typ
	1206  
	1207  	// Inherit permission bits from v, but clear flagEmbedRO.
	1208  	fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
	1209  	// Using an unexported field forces flagRO.
	1210  	if !field.name.isExported() {
	1211  		if field.embedded() {
	1212  			fl |= flagEmbedRO
	1213  		} else {
	1214  			fl |= flagStickyRO
	1215  		}
	1216  	}
	1217  	// Either flagIndir is set and v.ptr points at struct,
	1218  	// or flagIndir is not set and v.ptr is the actual struct data.
	1219  	// In the former case, we want v.ptr + offset.
	1220  	// In the latter case, we must have field.offset = 0,
	1221  	// so v.ptr + field.offset is still the correct address.
	1222  	ptr := add(v.ptr, field.offset(), "same as non-reflect &v.field")
	1223  	return Value{typ, ptr, fl}
	1224  }
	1225  
	1226  // FieldByIndex returns the nested field corresponding to index.
	1227  // It panics if v's Kind is not struct.
	1228  func (v Value) FieldByIndex(index []int) Value {
	1229  	if len(index) == 1 {
	1230  		return v.Field(index[0])
	1231  	}
	1232  	v.mustBe(Struct)
	1233  	for i, x := range index {
	1234  		if i > 0 {
	1235  			if v.Kind() == Ptr && v.typ.Elem().Kind() == Struct {
	1236  				if v.IsNil() {
	1237  					panic("reflect: indirection through nil pointer to embedded struct")
	1238  				}
	1239  				v = v.Elem()
	1240  			}
	1241  		}
	1242  		v = v.Field(x)
	1243  	}
	1244  	return v
	1245  }
	1246  
	1247  // FieldByName returns the struct field with the given name.
	1248  // It returns the zero Value if no field was found.
	1249  // It panics if v's Kind is not struct.
	1250  func (v Value) FieldByName(name string) Value {
	1251  	v.mustBe(Struct)
	1252  	if f, ok := v.typ.FieldByName(name); ok {
	1253  		return v.FieldByIndex(f.Index)
	1254  	}
	1255  	return Value{}
	1256  }
	1257  
	1258  // FieldByNameFunc returns the struct field with a name
	1259  // that satisfies the match function.
	1260  // It panics if v's Kind is not struct.
	1261  // It returns the zero Value if no field was found.
	1262  func (v Value) FieldByNameFunc(match func(string) bool) Value {
	1263  	if f, ok := v.typ.FieldByNameFunc(match); ok {
	1264  		return v.FieldByIndex(f.Index)
	1265  	}
	1266  	return Value{}
	1267  }
	1268  
	1269  // Float returns v's underlying value, as a float64.
	1270  // It panics if v's Kind is not Float32 or Float64
	1271  func (v Value) Float() float64 {
	1272  	k := v.kind()
	1273  	switch k {
	1274  	case Float32:
	1275  		return float64(*(*float32)(v.ptr))
	1276  	case Float64:
	1277  		return *(*float64)(v.ptr)
	1278  	}
	1279  	panic(&ValueError{"reflect.Value.Float", v.kind()})
	1280  }
	1281  
	1282  var uint8Type = TypeOf(uint8(0)).(*rtype)
	1283  
	1284  // Index returns v's i'th element.
	1285  // It panics if v's Kind is not Array, Slice, or String or i is out of range.
	1286  func (v Value) Index(i int) Value {
	1287  	switch v.kind() {
	1288  	case Array:
	1289  		tt := (*arrayType)(unsafe.Pointer(v.typ))
	1290  		if uint(i) >= uint(tt.len) {
	1291  			panic("reflect: array index out of range")
	1292  		}
	1293  		typ := tt.elem
	1294  		offset := uintptr(i) * typ.size
	1295  
	1296  		// Either flagIndir is set and v.ptr points at array,
	1297  		// or flagIndir is not set and v.ptr is the actual array data.
	1298  		// In the former case, we want v.ptr + offset.
	1299  		// In the latter case, we must be doing Index(0), so offset = 0,
	1300  		// so v.ptr + offset is still the correct address.
	1301  		val := add(v.ptr, offset, "same as &v[i], i < tt.len")
	1302  		fl := v.flag&(flagIndir|flagAddr) | v.flag.ro() | flag(typ.Kind()) // bits same as overall array
	1303  		return Value{typ, val, fl}
	1304  
	1305  	case Slice:
	1306  		// Element flag same as Elem of Ptr.
	1307  		// Addressable, indirect, possibly read-only.
	1308  		s := (*unsafeheader.Slice)(v.ptr)
	1309  		if uint(i) >= uint(s.Len) {
	1310  			panic("reflect: slice index out of range")
	1311  		}
	1312  		tt := (*sliceType)(unsafe.Pointer(v.typ))
	1313  		typ := tt.elem
	1314  		val := arrayAt(s.Data, i, typ.size, "i < s.Len")
	1315  		fl := flagAddr | flagIndir | v.flag.ro() | flag(typ.Kind())
	1316  		return Value{typ, val, fl}
	1317  
	1318  	case String:
	1319  		s := (*unsafeheader.String)(v.ptr)
	1320  		if uint(i) >= uint(s.Len) {
	1321  			panic("reflect: string index out of range")
	1322  		}
	1323  		p := arrayAt(s.Data, i, 1, "i < s.Len")
	1324  		fl := v.flag.ro() | flag(Uint8) | flagIndir
	1325  		return Value{uint8Type, p, fl}
	1326  	}
	1327  	panic(&ValueError{"reflect.Value.Index", v.kind()})
	1328  }
	1329  
	1330  // Int returns v's underlying value, as an int64.
	1331  // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
	1332  func (v Value) Int() int64 {
	1333  	k := v.kind()
	1334  	p := v.ptr
	1335  	switch k {
	1336  	case Int:
	1337  		return int64(*(*int)(p))
	1338  	case Int8:
	1339  		return int64(*(*int8)(p))
	1340  	case Int16:
	1341  		return int64(*(*int16)(p))
	1342  	case Int32:
	1343  		return int64(*(*int32)(p))
	1344  	case Int64:
	1345  		return *(*int64)(p)
	1346  	}
	1347  	panic(&ValueError{"reflect.Value.Int", v.kind()})
	1348  }
	1349  
	1350  // CanInterface reports whether Interface can be used without panicking.
	1351  func (v Value) CanInterface() bool {
	1352  	if v.flag == 0 {
	1353  		panic(&ValueError{"reflect.Value.CanInterface", Invalid})
	1354  	}
	1355  	return v.flag&flagRO == 0
	1356  }
	1357  
	1358  // Interface returns v's current value as an interface{}.
	1359  // It is equivalent to:
	1360  //	var i interface{} = (v's underlying value)
	1361  // It panics if the Value was obtained by accessing
	1362  // unexported struct fields.
	1363  func (v Value) Interface() (i interface{}) {
	1364  	return valueInterface(v, true)
	1365  }
	1366  
	1367  func valueInterface(v Value, safe bool) interface{} {
	1368  	if v.flag == 0 {
	1369  		panic(&ValueError{"reflect.Value.Interface", Invalid})
	1370  	}
	1371  	if safe && v.flag&flagRO != 0 {
	1372  		// Do not allow access to unexported values via Interface,
	1373  		// because they might be pointers that should not be
	1374  		// writable or methods or function that should not be callable.
	1375  		panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
	1376  	}
	1377  	if v.flag&flagMethod != 0 {
	1378  		v = makeMethodValue("Interface", v)
	1379  	}
	1380  
	1381  	if v.kind() == Interface {
	1382  		// Special case: return the element inside the interface.
	1383  		// Empty interface has one layout, all interfaces with
	1384  		// methods have a second layout.
	1385  		if v.NumMethod() == 0 {
	1386  			return *(*interface{})(v.ptr)
	1387  		}
	1388  		return *(*interface {
	1389  			M()
	1390  		})(v.ptr)
	1391  	}
	1392  
	1393  	// TODO: pass safe to packEface so we don't need to copy if safe==true?
	1394  	return packEface(v)
	1395  }
	1396  
	1397  // InterfaceData returns a pair of unspecified uintptr values.
	1398  // It panics if v's Kind is not Interface.
	1399  //
	1400  // In earlier versions of Go, this function returned the interface's
	1401  // value as a uintptr pair. As of Go 1.4, the implementation of
	1402  // interface values precludes any defined use of InterfaceData.
	1403  //
	1404  // Deprecated: The memory representation of interface values is not
	1405  // compatible with InterfaceData.
	1406  func (v Value) InterfaceData() [2]uintptr {
	1407  	v.mustBe(Interface)
	1408  	// We treat this as a read operation, so we allow
	1409  	// it even for unexported data, because the caller
	1410  	// has to import "unsafe" to turn it into something
	1411  	// that can be abused.
	1412  	// Interface value is always bigger than a word; assume flagIndir.
	1413  	return *(*[2]uintptr)(v.ptr)
	1414  }
	1415  
	1416  // IsNil reports whether its argument v is nil. The argument must be
	1417  // a chan, func, interface, map, pointer, or slice value; if it is
	1418  // not, IsNil panics. Note that IsNil is not always equivalent to a
	1419  // regular comparison with nil in Go. For example, if v was created
	1420  // by calling ValueOf with an uninitialized interface variable i,
	1421  // i==nil will be true but v.IsNil will panic as v will be the zero
	1422  // Value.
	1423  func (v Value) IsNil() bool {
	1424  	k := v.kind()
	1425  	switch k {
	1426  	case Chan, Func, Map, Ptr, UnsafePointer:
	1427  		if v.flag&flagMethod != 0 {
	1428  			return false
	1429  		}
	1430  		ptr := v.ptr
	1431  		if v.flag&flagIndir != 0 {
	1432  			ptr = *(*unsafe.Pointer)(ptr)
	1433  		}
	1434  		return ptr == nil
	1435  	case Interface, Slice:
	1436  		// Both interface and slice are nil if first word is 0.
	1437  		// Both are always bigger than a word; assume flagIndir.
	1438  		return *(*unsafe.Pointer)(v.ptr) == nil
	1439  	}
	1440  	panic(&ValueError{"reflect.Value.IsNil", v.kind()})
	1441  }
	1442  
	1443  // IsValid reports whether v represents a value.
	1444  // It returns false if v is the zero Value.
	1445  // If IsValid returns false, all other methods except String panic.
	1446  // Most functions and methods never return an invalid Value.
	1447  // If one does, its documentation states the conditions explicitly.
	1448  func (v Value) IsValid() bool {
	1449  	return v.flag != 0
	1450  }
	1451  
	1452  // IsZero reports whether v is the zero value for its type.
	1453  // It panics if the argument is invalid.
	1454  func (v Value) IsZero() bool {
	1455  	switch v.kind() {
	1456  	case Bool:
	1457  		return !v.Bool()
	1458  	case Int, Int8, Int16, Int32, Int64:
	1459  		return v.Int() == 0
	1460  	case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
	1461  		return v.Uint() == 0
	1462  	case Float32, Float64:
	1463  		return math.Float64bits(v.Float()) == 0
	1464  	case Complex64, Complex128:
	1465  		c := v.Complex()
	1466  		return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
	1467  	case Array:
	1468  		for i := 0; i < v.Len(); i++ {
	1469  			if !v.Index(i).IsZero() {
	1470  				return false
	1471  			}
	1472  		}
	1473  		return true
	1474  	case Chan, Func, Interface, Map, Ptr, Slice, UnsafePointer:
	1475  		return v.IsNil()
	1476  	case String:
	1477  		return v.Len() == 0
	1478  	case Struct:
	1479  		for i := 0; i < v.NumField(); i++ {
	1480  			if !v.Field(i).IsZero() {
	1481  				return false
	1482  			}
	1483  		}
	1484  		return true
	1485  	default:
	1486  		// This should never happens, but will act as a safeguard for
	1487  		// later, as a default value doesn't makes sense here.
	1488  		panic(&ValueError{"reflect.Value.IsZero", v.Kind()})
	1489  	}
	1490  }
	1491  
	1492  // Kind returns v's Kind.
	1493  // If v is the zero Value (IsValid returns false), Kind returns Invalid.
	1494  func (v Value) Kind() Kind {
	1495  	return v.kind()
	1496  }
	1497  
	1498  // Len returns v's length.
	1499  // It panics if v's Kind is not Array, Chan, Map, Slice, or String.
	1500  func (v Value) Len() int {
	1501  	k := v.kind()
	1502  	switch k {
	1503  	case Array:
	1504  		tt := (*arrayType)(unsafe.Pointer(v.typ))
	1505  		return int(tt.len)
	1506  	case Chan:
	1507  		return chanlen(v.pointer())
	1508  	case Map:
	1509  		return maplen(v.pointer())
	1510  	case Slice:
	1511  		// Slice is bigger than a word; assume flagIndir.
	1512  		return (*unsafeheader.Slice)(v.ptr).Len
	1513  	case String:
	1514  		// String is bigger than a word; assume flagIndir.
	1515  		return (*unsafeheader.String)(v.ptr).Len
	1516  	}
	1517  	panic(&ValueError{"reflect.Value.Len", v.kind()})
	1518  }
	1519  
	1520  // MapIndex returns the value associated with key in the map v.
	1521  // It panics if v's Kind is not Map.
	1522  // It returns the zero Value if key is not found in the map or if v represents a nil map.
	1523  // As in Go, the key's value must be assignable to the map's key type.
	1524  func (v Value) MapIndex(key Value) Value {
	1525  	v.mustBe(Map)
	1526  	tt := (*mapType)(unsafe.Pointer(v.typ))
	1527  
	1528  	// Do not require key to be exported, so that DeepEqual
	1529  	// and other programs can use all the keys returned by
	1530  	// MapKeys as arguments to MapIndex. If either the map
	1531  	// or the key is unexported, though, the result will be
	1532  	// considered unexported. This is consistent with the
	1533  	// behavior for structs, which allow read but not write
	1534  	// of unexported fields.
	1535  	key = key.assignTo("reflect.Value.MapIndex", tt.key, nil)
	1536  
	1537  	var k unsafe.Pointer
	1538  	if key.flag&flagIndir != 0 {
	1539  		k = key.ptr
	1540  	} else {
	1541  		k = unsafe.Pointer(&key.ptr)
	1542  	}
	1543  	e := mapaccess(v.typ, v.pointer(), k)
	1544  	if e == nil {
	1545  		return Value{}
	1546  	}
	1547  	typ := tt.elem
	1548  	fl := (v.flag | key.flag).ro()
	1549  	fl |= flag(typ.Kind())
	1550  	return copyVal(typ, fl, e)
	1551  }
	1552  
	1553  // MapKeys returns a slice containing all the keys present in the map,
	1554  // in unspecified order.
	1555  // It panics if v's Kind is not Map.
	1556  // It returns an empty slice if v represents a nil map.
	1557  func (v Value) MapKeys() []Value {
	1558  	v.mustBe(Map)
	1559  	tt := (*mapType)(unsafe.Pointer(v.typ))
	1560  	keyType := tt.key
	1561  
	1562  	fl := v.flag.ro() | flag(keyType.Kind())
	1563  
	1564  	m := v.pointer()
	1565  	mlen := int(0)
	1566  	if m != nil {
	1567  		mlen = maplen(m)
	1568  	}
	1569  	it := mapiterinit(v.typ, m)
	1570  	a := make([]Value, mlen)
	1571  	var i int
	1572  	for i = 0; i < len(a); i++ {
	1573  		key := mapiterkey(it)
	1574  		if key == nil {
	1575  			// Someone deleted an entry from the map since we
	1576  			// called maplen above. It's a data race, but nothing
	1577  			// we can do about it.
	1578  			break
	1579  		}
	1580  		a[i] = copyVal(keyType, fl, key)
	1581  		mapiternext(it)
	1582  	}
	1583  	return a[:i]
	1584  }
	1585  
	1586  // A MapIter is an iterator for ranging over a map.
	1587  // See Value.MapRange.
	1588  type MapIter struct {
	1589  	m	Value
	1590  	it unsafe.Pointer
	1591  }
	1592  
	1593  // Key returns the key of the iterator's current map entry.
	1594  func (it *MapIter) Key() Value {
	1595  	if it.it == nil {
	1596  		panic("MapIter.Key called before Next")
	1597  	}
	1598  	if mapiterkey(it.it) == nil {
	1599  		panic("MapIter.Key called on exhausted iterator")
	1600  	}
	1601  
	1602  	t := (*mapType)(unsafe.Pointer(it.m.typ))
	1603  	ktype := t.key
	1604  	return copyVal(ktype, it.m.flag.ro()|flag(ktype.Kind()), mapiterkey(it.it))
	1605  }
	1606  
	1607  // Value returns the value of the iterator's current map entry.
	1608  func (it *MapIter) Value() Value {
	1609  	if it.it == nil {
	1610  		panic("MapIter.Value called before Next")
	1611  	}
	1612  	if mapiterkey(it.it) == nil {
	1613  		panic("MapIter.Value called on exhausted iterator")
	1614  	}
	1615  
	1616  	t := (*mapType)(unsafe.Pointer(it.m.typ))
	1617  	vtype := t.elem
	1618  	return copyVal(vtype, it.m.flag.ro()|flag(vtype.Kind()), mapiterelem(it.it))
	1619  }
	1620  
	1621  // Next advances the map iterator and reports whether there is another
	1622  // entry. It returns false when the iterator is exhausted; subsequent
	1623  // calls to Key, Value, or Next will panic.
	1624  func (it *MapIter) Next() bool {
	1625  	if it.it == nil {
	1626  		it.it = mapiterinit(it.m.typ, it.m.pointer())
	1627  	} else {
	1628  		if mapiterkey(it.it) == nil {
	1629  			panic("MapIter.Next called on exhausted iterator")
	1630  		}
	1631  		mapiternext(it.it)
	1632  	}
	1633  	return mapiterkey(it.it) != nil
	1634  }
	1635  
	1636  // MapRange returns a range iterator for a map.
	1637  // It panics if v's Kind is not Map.
	1638  //
	1639  // Call Next to advance the iterator, and Key/Value to access each entry.
	1640  // Next returns false when the iterator is exhausted.
	1641  // MapRange follows the same iteration semantics as a range statement.
	1642  //
	1643  // Example:
	1644  //
	1645  //	iter := reflect.ValueOf(m).MapRange()
	1646  // 	for iter.Next() {
	1647  //		k := iter.Key()
	1648  //		v := iter.Value()
	1649  //		...
	1650  //	}
	1651  //
	1652  func (v Value) MapRange() *MapIter {
	1653  	v.mustBe(Map)
	1654  	return &MapIter{m: v}
	1655  }
	1656  
	1657  // copyVal returns a Value containing the map key or value at ptr,
	1658  // allocating a new variable as needed.
	1659  func copyVal(typ *rtype, fl flag, ptr unsafe.Pointer) Value {
	1660  	if ifaceIndir(typ) {
	1661  		// Copy result so future changes to the map
	1662  		// won't change the underlying value.
	1663  		c := unsafe_New(typ)
	1664  		typedmemmove(typ, c, ptr)
	1665  		return Value{typ, c, fl | flagIndir}
	1666  	}
	1667  	return Value{typ, *(*unsafe.Pointer)(ptr), fl}
	1668  }
	1669  
	1670  // Method returns a function value corresponding to v's i'th method.
	1671  // The arguments to a Call on the returned function should not include
	1672  // a receiver; the returned function will always use v as the receiver.
	1673  // Method panics if i is out of range or if v is a nil interface value.
	1674  func (v Value) Method(i int) Value {
	1675  	if v.typ == nil {
	1676  		panic(&ValueError{"reflect.Value.Method", Invalid})
	1677  	}
	1678  	if v.flag&flagMethod != 0 || uint(i) >= uint(v.typ.NumMethod()) {
	1679  		panic("reflect: Method index out of range")
	1680  	}
	1681  	if v.typ.Kind() == Interface && v.IsNil() {
	1682  		panic("reflect: Method on nil interface value")
	1683  	}
	1684  	fl := v.flag.ro() | (v.flag & flagIndir)
	1685  	fl |= flag(Func)
	1686  	fl |= flag(i)<<flagMethodShift | flagMethod
	1687  	return Value{v.typ, v.ptr, fl}
	1688  }
	1689  
	1690  // NumMethod returns the number of exported methods in the value's method set.
	1691  func (v Value) NumMethod() int {
	1692  	if v.typ == nil {
	1693  		panic(&ValueError{"reflect.Value.NumMethod", Invalid})
	1694  	}
	1695  	if v.flag&flagMethod != 0 {
	1696  		return 0
	1697  	}
	1698  	return v.typ.NumMethod()
	1699  }
	1700  
	1701  // MethodByName returns a function value corresponding to the method
	1702  // of v with the given name.
	1703  // The arguments to a Call on the returned function should not include
	1704  // a receiver; the returned function will always use v as the receiver.
	1705  // It returns the zero Value if no method was found.
	1706  func (v Value) MethodByName(name string) Value {
	1707  	if v.typ == nil {
	1708  		panic(&ValueError{"reflect.Value.MethodByName", Invalid})
	1709  	}
	1710  	if v.flag&flagMethod != 0 {
	1711  		return Value{}
	1712  	}
	1713  	m, ok := v.typ.MethodByName(name)
	1714  	if !ok {
	1715  		return Value{}
	1716  	}
	1717  	return v.Method(m.Index)
	1718  }
	1719  
	1720  // NumField returns the number of fields in the struct v.
	1721  // It panics if v's Kind is not Struct.
	1722  func (v Value) NumField() int {
	1723  	v.mustBe(Struct)
	1724  	tt := (*structType)(unsafe.Pointer(v.typ))
	1725  	return len(tt.fields)
	1726  }
	1727  
	1728  // OverflowComplex reports whether the complex128 x cannot be represented by v's type.
	1729  // It panics if v's Kind is not Complex64 or Complex128.
	1730  func (v Value) OverflowComplex(x complex128) bool {
	1731  	k := v.kind()
	1732  	switch k {
	1733  	case Complex64:
	1734  		return overflowFloat32(real(x)) || overflowFloat32(imag(x))
	1735  	case Complex128:
	1736  		return false
	1737  	}
	1738  	panic(&ValueError{"reflect.Value.OverflowComplex", v.kind()})
	1739  }
	1740  
	1741  // OverflowFloat reports whether the float64 x cannot be represented by v's type.
	1742  // It panics if v's Kind is not Float32 or Float64.
	1743  func (v Value) OverflowFloat(x float64) bool {
	1744  	k := v.kind()
	1745  	switch k {
	1746  	case Float32:
	1747  		return overflowFloat32(x)
	1748  	case Float64:
	1749  		return false
	1750  	}
	1751  	panic(&ValueError{"reflect.Value.OverflowFloat", v.kind()})
	1752  }
	1753  
	1754  func overflowFloat32(x float64) bool {
	1755  	if x < 0 {
	1756  		x = -x
	1757  	}
	1758  	return math.MaxFloat32 < x && x <= math.MaxFloat64
	1759  }
	1760  
	1761  // OverflowInt reports whether the int64 x cannot be represented by v's type.
	1762  // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
	1763  func (v Value) OverflowInt(x int64) bool {
	1764  	k := v.kind()
	1765  	switch k {
	1766  	case Int, Int8, Int16, Int32, Int64:
	1767  		bitSize := v.typ.size * 8
	1768  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
	1769  		return x != trunc
	1770  	}
	1771  	panic(&ValueError{"reflect.Value.OverflowInt", v.kind()})
	1772  }
	1773  
	1774  // OverflowUint reports whether the uint64 x cannot be represented by v's type.
	1775  // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
	1776  func (v Value) OverflowUint(x uint64) bool {
	1777  	k := v.kind()
	1778  	switch k {
	1779  	case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
	1780  		bitSize := v.typ.size * 8
	1781  		trunc := (x << (64 - bitSize)) >> (64 - bitSize)
	1782  		return x != trunc
	1783  	}
	1784  	panic(&ValueError{"reflect.Value.OverflowUint", v.kind()})
	1785  }
	1786  
	1787  //go:nocheckptr
	1788  // This prevents inlining Value.Pointer when -d=checkptr is enabled,
	1789  // which ensures cmd/compile can recognize unsafe.Pointer(v.Pointer())
	1790  // and make an exception.
	1791  
	1792  // Pointer returns v's value as a uintptr.
	1793  // It returns uintptr instead of unsafe.Pointer so that
	1794  // code using reflect cannot obtain unsafe.Pointers
	1795  // without importing the unsafe package explicitly.
	1796  // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
	1797  //
	1798  // If v's Kind is Func, the returned pointer is an underlying
	1799  // code pointer, but not necessarily enough to identify a
	1800  // single function uniquely. The only guarantee is that the
	1801  // result is zero if and only if v is a nil func Value.
	1802  //
	1803  // If v's Kind is Slice, the returned pointer is to the first
	1804  // element of the slice. If the slice is nil the returned value
	1805  // is 0.	If the slice is empty but non-nil the return value is non-zero.
	1806  func (v Value) Pointer() uintptr {
	1807  	// TODO: deprecate
	1808  	k := v.kind()
	1809  	switch k {
	1810  	case Ptr:
	1811  		if v.typ.ptrdata == 0 {
	1812  			// Handle pointers to go:notinheap types directly,
	1813  			// so we never materialize such pointers as an
	1814  			// unsafe.Pointer. (Such pointers are always indirect.)
	1815  			// See issue 42076.
	1816  			return *(*uintptr)(v.ptr)
	1817  		}
	1818  		fallthrough
	1819  	case Chan, Map, UnsafePointer:
	1820  		return uintptr(v.pointer())
	1821  	case Func:
	1822  		if v.flag&flagMethod != 0 {
	1823  			// As the doc comment says, the returned pointer is an
	1824  			// underlying code pointer but not necessarily enough to
	1825  			// identify a single function uniquely. All method expressions
	1826  			// created via reflect have the same underlying code pointer,
	1827  			// so their Pointers are equal. The function used here must
	1828  			// match the one used in makeMethodValue.
	1829  			f := methodValueCall
	1830  			return **(**uintptr)(unsafe.Pointer(&f))
	1831  		}
	1832  		p := v.pointer()
	1833  		// Non-nil func value points at data block.
	1834  		// First word of data block is actual code.
	1835  		if p != nil {
	1836  			p = *(*unsafe.Pointer)(p)
	1837  		}
	1838  		return uintptr(p)
	1839  
	1840  	case Slice:
	1841  		return (*SliceHeader)(v.ptr).Data
	1842  	}
	1843  	panic(&ValueError{"reflect.Value.Pointer", v.kind()})
	1844  }
	1845  
	1846  // Recv receives and returns a value from the channel v.
	1847  // It panics if v's Kind is not Chan.
	1848  // The receive blocks until a value is ready.
	1849  // The boolean value ok is true if the value x corresponds to a send
	1850  // on the channel, false if it is a zero value received because the channel is closed.
	1851  func (v Value) Recv() (x Value, ok bool) {
	1852  	v.mustBe(Chan)
	1853  	v.mustBeExported()
	1854  	return v.recv(false)
	1855  }
	1856  
	1857  // internal recv, possibly non-blocking (nb).
	1858  // v is known to be a channel.
	1859  func (v Value) recv(nb bool) (val Value, ok bool) {
	1860  	tt := (*chanType)(unsafe.Pointer(v.typ))
	1861  	if ChanDir(tt.dir)&RecvDir == 0 {
	1862  		panic("reflect: recv on send-only channel")
	1863  	}
	1864  	t := tt.elem
	1865  	val = Value{t, nil, flag(t.Kind())}
	1866  	var p unsafe.Pointer
	1867  	if ifaceIndir(t) {
	1868  		p = unsafe_New(t)
	1869  		val.ptr = p
	1870  		val.flag |= flagIndir
	1871  	} else {
	1872  		p = unsafe.Pointer(&val.ptr)
	1873  	}
	1874  	selected, ok := chanrecv(v.pointer(), nb, p)
	1875  	if !selected {
	1876  		val = Value{}
	1877  	}
	1878  	return
	1879  }
	1880  
	1881  // Send sends x on the channel v.
	1882  // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
	1883  // As in Go, x's value must be assignable to the channel's element type.
	1884  func (v Value) Send(x Value) {
	1885  	v.mustBe(Chan)
	1886  	v.mustBeExported()
	1887  	v.send(x, false)
	1888  }
	1889  
	1890  // internal send, possibly non-blocking.
	1891  // v is known to be a channel.
	1892  func (v Value) send(x Value, nb bool) (selected bool) {
	1893  	tt := (*chanType)(unsafe.Pointer(v.typ))
	1894  	if ChanDir(tt.dir)&SendDir == 0 {
	1895  		panic("reflect: send on recv-only channel")
	1896  	}
	1897  	x.mustBeExported()
	1898  	x = x.assignTo("reflect.Value.Send", tt.elem, nil)
	1899  	var p unsafe.Pointer
	1900  	if x.flag&flagIndir != 0 {
	1901  		p = x.ptr
	1902  	} else {
	1903  		p = unsafe.Pointer(&x.ptr)
	1904  	}
	1905  	return chansend(v.pointer(), p, nb)
	1906  }
	1907  
	1908  // Set assigns x to the value v.
	1909  // It panics if CanSet returns false.
	1910  // As in Go, x's value must be assignable to v's type.
	1911  func (v Value) Set(x Value) {
	1912  	v.mustBeAssignable()
	1913  	x.mustBeExported() // do not let unexported x leak
	1914  	var target unsafe.Pointer
	1915  	if v.kind() == Interface {
	1916  		target = v.ptr
	1917  	}
	1918  	x = x.assignTo("reflect.Set", v.typ, target)
	1919  	if x.flag&flagIndir != 0 {
	1920  		if x.ptr == unsafe.Pointer(&zeroVal[0]) {
	1921  			typedmemclr(v.typ, v.ptr)
	1922  		} else {
	1923  			typedmemmove(v.typ, v.ptr, x.ptr)
	1924  		}
	1925  	} else {
	1926  		*(*unsafe.Pointer)(v.ptr) = x.ptr
	1927  	}
	1928  }
	1929  
	1930  // SetBool sets v's underlying value.
	1931  // It panics if v's Kind is not Bool or if CanSet() is false.
	1932  func (v Value) SetBool(x bool) {
	1933  	v.mustBeAssignable()
	1934  	v.mustBe(Bool)
	1935  	*(*bool)(v.ptr) = x
	1936  }
	1937  
	1938  // SetBytes sets v's underlying value.
	1939  // It panics if v's underlying value is not a slice of bytes.
	1940  func (v Value) SetBytes(x []byte) {
	1941  	v.mustBeAssignable()
	1942  	v.mustBe(Slice)
	1943  	if v.typ.Elem().Kind() != Uint8 {
	1944  		panic("reflect.Value.SetBytes of non-byte slice")
	1945  	}
	1946  	*(*[]byte)(v.ptr) = x
	1947  }
	1948  
	1949  // setRunes sets v's underlying value.
	1950  // It panics if v's underlying value is not a slice of runes (int32s).
	1951  func (v Value) setRunes(x []rune) {
	1952  	v.mustBeAssignable()
	1953  	v.mustBe(Slice)
	1954  	if v.typ.Elem().Kind() != Int32 {
	1955  		panic("reflect.Value.setRunes of non-rune slice")
	1956  	}
	1957  	*(*[]rune)(v.ptr) = x
	1958  }
	1959  
	1960  // SetComplex sets v's underlying value to x.
	1961  // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
	1962  func (v Value) SetComplex(x complex128) {
	1963  	v.mustBeAssignable()
	1964  	switch k := v.kind(); k {
	1965  	default:
	1966  		panic(&ValueError{"reflect.Value.SetComplex", v.kind()})
	1967  	case Complex64:
	1968  		*(*complex64)(v.ptr) = complex64(x)
	1969  	case Complex128:
	1970  		*(*complex128)(v.ptr) = x
	1971  	}
	1972  }
	1973  
	1974  // SetFloat sets v's underlying value to x.
	1975  // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
	1976  func (v Value) SetFloat(x float64) {
	1977  	v.mustBeAssignable()
	1978  	switch k := v.kind(); k {
	1979  	default:
	1980  		panic(&ValueError{"reflect.Value.SetFloat", v.kind()})
	1981  	case Float32:
	1982  		*(*float32)(v.ptr) = float32(x)
	1983  	case Float64:
	1984  		*(*float64)(v.ptr) = x
	1985  	}
	1986  }
	1987  
	1988  // SetInt sets v's underlying value to x.
	1989  // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
	1990  func (v Value) SetInt(x int64) {
	1991  	v.mustBeAssignable()
	1992  	switch k := v.kind(); k {
	1993  	default:
	1994  		panic(&ValueError{"reflect.Value.SetInt", v.kind()})
	1995  	case Int:
	1996  		*(*int)(v.ptr) = int(x)
	1997  	case Int8:
	1998  		*(*int8)(v.ptr) = int8(x)
	1999  	case Int16:
	2000  		*(*int16)(v.ptr) = int16(x)
	2001  	case Int32:
	2002  		*(*int32)(v.ptr) = int32(x)
	2003  	case Int64:
	2004  		*(*int64)(v.ptr) = x
	2005  	}
	2006  }
	2007  
	2008  // SetLen sets v's length to n.
	2009  // It panics if v's Kind is not Slice or if n is negative or
	2010  // greater than the capacity of the slice.
	2011  func (v Value) SetLen(n int) {
	2012  	v.mustBeAssignable()
	2013  	v.mustBe(Slice)
	2014  	s := (*unsafeheader.Slice)(v.ptr)
	2015  	if uint(n) > uint(s.Cap) {
	2016  		panic("reflect: slice length out of range in SetLen")
	2017  	}
	2018  	s.Len = n
	2019  }
	2020  
	2021  // SetCap sets v's capacity to n.
	2022  // It panics if v's Kind is not Slice or if n is smaller than the length or
	2023  // greater than the capacity of the slice.
	2024  func (v Value) SetCap(n int) {
	2025  	v.mustBeAssignable()
	2026  	v.mustBe(Slice)
	2027  	s := (*unsafeheader.Slice)(v.ptr)
	2028  	if n < s.Len || n > s.Cap {
	2029  		panic("reflect: slice capacity out of range in SetCap")
	2030  	}
	2031  	s.Cap = n
	2032  }
	2033  
	2034  // SetMapIndex sets the element associated with key in the map v to elem.
	2035  // It panics if v's Kind is not Map.
	2036  // If elem is the zero Value, SetMapIndex deletes the key from the map.
	2037  // Otherwise if v holds a nil map, SetMapIndex will panic.
	2038  // As in Go, key's elem must be assignable to the map's key type,
	2039  // and elem's value must be assignable to the map's elem type.
	2040  func (v Value) SetMapIndex(key, elem Value) {
	2041  	v.mustBe(Map)
	2042  	v.mustBeExported()
	2043  	key.mustBeExported()
	2044  	tt := (*mapType)(unsafe.Pointer(v.typ))
	2045  	key = key.assignTo("reflect.Value.SetMapIndex", tt.key, nil)
	2046  	var k unsafe.Pointer
	2047  	if key.flag&flagIndir != 0 {
	2048  		k = key.ptr
	2049  	} else {
	2050  		k = unsafe.Pointer(&key.ptr)
	2051  	}
	2052  	if elem.typ == nil {
	2053  		mapdelete(v.typ, v.pointer(), k)
	2054  		return
	2055  	}
	2056  	elem.mustBeExported()
	2057  	elem = elem.assignTo("reflect.Value.SetMapIndex", tt.elem, nil)
	2058  	var e unsafe.Pointer
	2059  	if elem.flag&flagIndir != 0 {
	2060  		e = elem.ptr
	2061  	} else {
	2062  		e = unsafe.Pointer(&elem.ptr)
	2063  	}
	2064  	mapassign(v.typ, v.pointer(), k, e)
	2065  }
	2066  
	2067  // SetUint sets v's underlying value to x.
	2068  // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
	2069  func (v Value) SetUint(x uint64) {
	2070  	v.mustBeAssignable()
	2071  	switch k := v.kind(); k {
	2072  	default:
	2073  		panic(&ValueError{"reflect.Value.SetUint", v.kind()})
	2074  	case Uint:
	2075  		*(*uint)(v.ptr) = uint(x)
	2076  	case Uint8:
	2077  		*(*uint8)(v.ptr) = uint8(x)
	2078  	case Uint16:
	2079  		*(*uint16)(v.ptr) = uint16(x)
	2080  	case Uint32:
	2081  		*(*uint32)(v.ptr) = uint32(x)
	2082  	case Uint64:
	2083  		*(*uint64)(v.ptr) = x
	2084  	case Uintptr:
	2085  		*(*uintptr)(v.ptr) = uintptr(x)
	2086  	}
	2087  }
	2088  
	2089  // SetPointer sets the unsafe.Pointer value v to x.
	2090  // It panics if v's Kind is not UnsafePointer.
	2091  func (v Value) SetPointer(x unsafe.Pointer) {
	2092  	v.mustBeAssignable()
	2093  	v.mustBe(UnsafePointer)
	2094  	*(*unsafe.Pointer)(v.ptr) = x
	2095  }
	2096  
	2097  // SetString sets v's underlying value to x.
	2098  // It panics if v's Kind is not String or if CanSet() is false.
	2099  func (v Value) SetString(x string) {
	2100  	v.mustBeAssignable()
	2101  	v.mustBe(String)
	2102  	*(*string)(v.ptr) = x
	2103  }
	2104  
	2105  // Slice returns v[i:j].
	2106  // It panics if v's Kind is not Array, Slice or String, or if v is an unaddressable array,
	2107  // or if the indexes are out of bounds.
	2108  func (v Value) Slice(i, j int) Value {
	2109  	var (
	2110  		cap	int
	2111  		typ	*sliceType
	2112  		base unsafe.Pointer
	2113  	)
	2114  	switch kind := v.kind(); kind {
	2115  	default:
	2116  		panic(&ValueError{"reflect.Value.Slice", v.kind()})
	2117  
	2118  	case Array:
	2119  		if v.flag&flagAddr == 0 {
	2120  			panic("reflect.Value.Slice: slice of unaddressable array")
	2121  		}
	2122  		tt := (*arrayType)(unsafe.Pointer(v.typ))
	2123  		cap = int(tt.len)
	2124  		typ = (*sliceType)(unsafe.Pointer(tt.slice))
	2125  		base = v.ptr
	2126  
	2127  	case Slice:
	2128  		typ = (*sliceType)(unsafe.Pointer(v.typ))
	2129  		s := (*unsafeheader.Slice)(v.ptr)
	2130  		base = s.Data
	2131  		cap = s.Cap
	2132  
	2133  	case String:
	2134  		s := (*unsafeheader.String)(v.ptr)
	2135  		if i < 0 || j < i || j > s.Len {
	2136  			panic("reflect.Value.Slice: string slice index out of bounds")
	2137  		}
	2138  		var t unsafeheader.String
	2139  		if i < s.Len {
	2140  			t = unsafeheader.String{Data: arrayAt(s.Data, i, 1, "i < s.Len"), Len: j - i}
	2141  		}
	2142  		return Value{v.typ, unsafe.Pointer(&t), v.flag}
	2143  	}
	2144  
	2145  	if i < 0 || j < i || j > cap {
	2146  		panic("reflect.Value.Slice: slice index out of bounds")
	2147  	}
	2148  
	2149  	// Declare slice so that gc can see the base pointer in it.
	2150  	var x []unsafe.Pointer
	2151  
	2152  	// Reinterpret as *unsafeheader.Slice to edit.
	2153  	s := (*unsafeheader.Slice)(unsafe.Pointer(&x))
	2154  	s.Len = j - i
	2155  	s.Cap = cap - i
	2156  	if cap-i > 0 {
	2157  		s.Data = arrayAt(base, i, typ.elem.Size(), "i < cap")
	2158  	} else {
	2159  		// do not advance pointer, to avoid pointing beyond end of slice
	2160  		s.Data = base
	2161  	}
	2162  
	2163  	fl := v.flag.ro() | flagIndir | flag(Slice)
	2164  	return Value{typ.common(), unsafe.Pointer(&x), fl}
	2165  }
	2166  
	2167  // Slice3 is the 3-index form of the slice operation: it returns v[i:j:k].
	2168  // It panics if v's Kind is not Array or Slice, or if v is an unaddressable array,
	2169  // or if the indexes are out of bounds.
	2170  func (v Value) Slice3(i, j, k int) Value {
	2171  	var (
	2172  		cap	int
	2173  		typ	*sliceType
	2174  		base unsafe.Pointer
	2175  	)
	2176  	switch kind := v.kind(); kind {
	2177  	default:
	2178  		panic(&ValueError{"reflect.Value.Slice3", v.kind()})
	2179  
	2180  	case Array:
	2181  		if v.flag&flagAddr == 0 {
	2182  			panic("reflect.Value.Slice3: slice of unaddressable array")
	2183  		}
	2184  		tt := (*arrayType)(unsafe.Pointer(v.typ))
	2185  		cap = int(tt.len)
	2186  		typ = (*sliceType)(unsafe.Pointer(tt.slice))
	2187  		base = v.ptr
	2188  
	2189  	case Slice:
	2190  		typ = (*sliceType)(unsafe.Pointer(v.typ))
	2191  		s := (*unsafeheader.Slice)(v.ptr)
	2192  		base = s.Data
	2193  		cap = s.Cap
	2194  	}
	2195  
	2196  	if i < 0 || j < i || k < j || k > cap {
	2197  		panic("reflect.Value.Slice3: slice index out of bounds")
	2198  	}
	2199  
	2200  	// Declare slice so that the garbage collector
	2201  	// can see the base pointer in it.
	2202  	var x []unsafe.Pointer
	2203  
	2204  	// Reinterpret as *unsafeheader.Slice to edit.
	2205  	s := (*unsafeheader.Slice)(unsafe.Pointer(&x))
	2206  	s.Len = j - i
	2207  	s.Cap = k - i
	2208  	if k-i > 0 {
	2209  		s.Data = arrayAt(base, i, typ.elem.Size(), "i < k <= cap")
	2210  	} else {
	2211  		// do not advance pointer, to avoid pointing beyond end of slice
	2212  		s.Data = base
	2213  	}
	2214  
	2215  	fl := v.flag.ro() | flagIndir | flag(Slice)
	2216  	return Value{typ.common(), unsafe.Pointer(&x), fl}
	2217  }
	2218  
	2219  // String returns the string v's underlying value, as a string.
	2220  // String is a special case because of Go's String method convention.
	2221  // Unlike the other getters, it does not panic if v's Kind is not String.
	2222  // Instead, it returns a string of the form "<T value>" where T is v's type.
	2223  // The fmt package treats Values specially. It does not call their String
	2224  // method implicitly but instead prints the concrete values they hold.
	2225  func (v Value) String() string {
	2226  	switch k := v.kind(); k {
	2227  	case Invalid:
	2228  		return "<invalid Value>"
	2229  	case String:
	2230  		return *(*string)(v.ptr)
	2231  	}
	2232  	// If you call String on a reflect.Value of other type, it's better to
	2233  	// print something than to panic. Useful in debugging.
	2234  	return "<" + v.Type().String() + " Value>"
	2235  }
	2236  
	2237  // TryRecv attempts to receive a value from the channel v but will not block.
	2238  // It panics if v's Kind is not Chan.
	2239  // If the receive delivers a value, x is the transferred value and ok is true.
	2240  // If the receive cannot finish without blocking, x is the zero Value and ok is false.
	2241  // If the channel is closed, x is the zero value for the channel's element type and ok is false.
	2242  func (v Value) TryRecv() (x Value, ok bool) {
	2243  	v.mustBe(Chan)
	2244  	v.mustBeExported()
	2245  	return v.recv(true)
	2246  }
	2247  
	2248  // TrySend attempts to send x on the channel v but will not block.
	2249  // It panics if v's Kind is not Chan.
	2250  // It reports whether the value was sent.
	2251  // As in Go, x's value must be assignable to the channel's element type.
	2252  func (v Value) TrySend(x Value) bool {
	2253  	v.mustBe(Chan)
	2254  	v.mustBeExported()
	2255  	return v.send(x, true)
	2256  }
	2257  
	2258  // Type returns v's type.
	2259  func (v Value) Type() Type {
	2260  	f := v.flag
	2261  	if f == 0 {
	2262  		panic(&ValueError{"reflect.Value.Type", Invalid})
	2263  	}
	2264  	if f&flagMethod == 0 {
	2265  		// Easy case
	2266  		return v.typ
	2267  	}
	2268  
	2269  	// Method value.
	2270  	// v.typ describes the receiver, not the method type.
	2271  	i := int(v.flag) >> flagMethodShift
	2272  	if v.typ.Kind() == Interface {
	2273  		// Method on interface.
	2274  		tt := (*interfaceType)(unsafe.Pointer(v.typ))
	2275  		if uint(i) >= uint(len(tt.methods)) {
	2276  			panic("reflect: internal error: invalid method index")
	2277  		}
	2278  		m := &tt.methods[i]
	2279  		return v.typ.typeOff(m.typ)
	2280  	}
	2281  	// Method on concrete type.
	2282  	ms := v.typ.exportedMethods()
	2283  	if uint(i) >= uint(len(ms)) {
	2284  		panic("reflect: internal error: invalid method index")
	2285  	}
	2286  	m := ms[i]
	2287  	return v.typ.typeOff(m.mtyp)
	2288  }
	2289  
	2290  // Uint returns v's underlying value, as a uint64.
	2291  // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
	2292  func (v Value) Uint() uint64 {
	2293  	k := v.kind()
	2294  	p := v.ptr
	2295  	switch k {
	2296  	case Uint:
	2297  		return uint64(*(*uint)(p))
	2298  	case Uint8:
	2299  		return uint64(*(*uint8)(p))
	2300  	case Uint16:
	2301  		return uint64(*(*uint16)(p))
	2302  	case Uint32:
	2303  		return uint64(*(*uint32)(p))
	2304  	case Uint64:
	2305  		return *(*uint64)(p)
	2306  	case Uintptr:
	2307  		return uint64(*(*uintptr)(p))
	2308  	}
	2309  	panic(&ValueError{"reflect.Value.Uint", v.kind()})
	2310  }
	2311  
	2312  //go:nocheckptr
	2313  // This prevents inlining Value.UnsafeAddr when -d=checkptr is enabled,
	2314  // which ensures cmd/compile can recognize unsafe.Pointer(v.UnsafeAddr())
	2315  // and make an exception.
	2316  
	2317  // UnsafeAddr returns a pointer to v's data.
	2318  // It is for advanced clients that also import the "unsafe" package.
	2319  // It panics if v is not addressable.
	2320  func (v Value) UnsafeAddr() uintptr {
	2321  	// TODO: deprecate
	2322  	if v.typ == nil {
	2323  		panic(&ValueError{"reflect.Value.UnsafeAddr", Invalid})
	2324  	}
	2325  	if v.flag&flagAddr == 0 {
	2326  		panic("reflect.Value.UnsafeAddr of unaddressable value")
	2327  	}
	2328  	return uintptr(v.ptr)
	2329  }
	2330  
	2331  // StringHeader is the runtime representation of a string.
	2332  // It cannot be used safely or portably and its representation may
	2333  // change in a later release.
	2334  // Moreover, the Data field is not sufficient to guarantee the data
	2335  // it references will not be garbage collected, so programs must keep
	2336  // a separate, correctly typed pointer to the underlying data.
	2337  type StringHeader struct {
	2338  	Data uintptr
	2339  	Len	int
	2340  }
	2341  
	2342  // SliceHeader is the runtime representation of a slice.
	2343  // It cannot be used safely or portably and its representation may
	2344  // change in a later release.
	2345  // Moreover, the Data field is not sufficient to guarantee the data
	2346  // it references will not be garbage collected, so programs must keep
	2347  // a separate, correctly typed pointer to the underlying data.
	2348  type SliceHeader struct {
	2349  	Data uintptr
	2350  	Len	int
	2351  	Cap	int
	2352  }
	2353  
	2354  func typesMustMatch(what string, t1, t2 Type) {
	2355  	if t1 != t2 {
	2356  		panic(what + ": " + t1.String() + " != " + t2.String())
	2357  	}
	2358  }
	2359  
	2360  // arrayAt returns the i-th element of p,
	2361  // an array whose elements are eltSize bytes wide.
	2362  // The array pointed at by p must have at least i+1 elements:
	2363  // it is invalid (but impossible to check here) to pass i >= len,
	2364  // because then the result will point outside the array.
	2365  // whySafe must explain why i < len. (Passing "i < len" is fine;
	2366  // the benefit is to surface this assumption at the call site.)
	2367  func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Pointer {
	2368  	return add(p, uintptr(i)*eltSize, "i < len")
	2369  }
	2370  
	2371  // grow grows the slice s so that it can hold extra more values, allocating
	2372  // more capacity if needed. It also returns the old and new slice lengths.
	2373  func grow(s Value, extra int) (Value, int, int) {
	2374  	i0 := s.Len()
	2375  	i1 := i0 + extra
	2376  	if i1 < i0 {
	2377  		panic("reflect.Append: slice overflow")
	2378  	}
	2379  	m := s.Cap()
	2380  	if i1 <= m {
	2381  		return s.Slice(0, i1), i0, i1
	2382  	}
	2383  	if m == 0 {
	2384  		m = extra
	2385  	} else {
	2386  		for m < i1 {
	2387  			if i0 < 1024 {
	2388  				m += m
	2389  			} else {
	2390  				m += m / 4
	2391  			}
	2392  		}
	2393  	}
	2394  	t := MakeSlice(s.Type(), i1, m)
	2395  	Copy(t, s)
	2396  	return t, i0, i1
	2397  }
	2398  
	2399  // Append appends the values x to a slice s and returns the resulting slice.
	2400  // As in Go, each x's value must be assignable to the slice's element type.
	2401  func Append(s Value, x ...Value) Value {
	2402  	s.mustBe(Slice)
	2403  	s, i0, i1 := grow(s, len(x))
	2404  	for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
	2405  		s.Index(i).Set(x[j])
	2406  	}
	2407  	return s
	2408  }
	2409  
	2410  // AppendSlice appends a slice t to a slice s and returns the resulting slice.
	2411  // The slices s and t must have the same element type.
	2412  func AppendSlice(s, t Value) Value {
	2413  	s.mustBe(Slice)
	2414  	t.mustBe(Slice)
	2415  	typesMustMatch("reflect.AppendSlice", s.Type().Elem(), t.Type().Elem())
	2416  	s, i0, i1 := grow(s, t.Len())
	2417  	Copy(s.Slice(i0, i1), t)
	2418  	return s
	2419  }
	2420  
	2421  // Copy copies the contents of src into dst until either
	2422  // dst has been filled or src has been exhausted.
	2423  // It returns the number of elements copied.
	2424  // Dst and src each must have kind Slice or Array, and
	2425  // dst and src must have the same element type.
	2426  //
	2427  // As a special case, src can have kind String if the element type of dst is kind Uint8.
	2428  func Copy(dst, src Value) int {
	2429  	dk := dst.kind()
	2430  	if dk != Array && dk != Slice {
	2431  		panic(&ValueError{"reflect.Copy", dk})
	2432  	}
	2433  	if dk == Array {
	2434  		dst.mustBeAssignable()
	2435  	}
	2436  	dst.mustBeExported()
	2437  
	2438  	sk := src.kind()
	2439  	var stringCopy bool
	2440  	if sk != Array && sk != Slice {
	2441  		stringCopy = sk == String && dst.typ.Elem().Kind() == Uint8
	2442  		if !stringCopy {
	2443  			panic(&ValueError{"reflect.Copy", sk})
	2444  		}
	2445  	}
	2446  	src.mustBeExported()
	2447  
	2448  	de := dst.typ.Elem()
	2449  	if !stringCopy {
	2450  		se := src.typ.Elem()
	2451  		typesMustMatch("reflect.Copy", de, se)
	2452  	}
	2453  
	2454  	var ds, ss unsafeheader.Slice
	2455  	if dk == Array {
	2456  		ds.Data = dst.ptr
	2457  		ds.Len = dst.Len()
	2458  		ds.Cap = ds.Len
	2459  	} else {
	2460  		ds = *(*unsafeheader.Slice)(dst.ptr)
	2461  	}
	2462  	if sk == Array {
	2463  		ss.Data = src.ptr
	2464  		ss.Len = src.Len()
	2465  		ss.Cap = ss.Len
	2466  	} else if sk == Slice {
	2467  		ss = *(*unsafeheader.Slice)(src.ptr)
	2468  	} else {
	2469  		sh := *(*unsafeheader.String)(src.ptr)
	2470  		ss.Data = sh.Data
	2471  		ss.Len = sh.Len
	2472  		ss.Cap = sh.Len
	2473  	}
	2474  
	2475  	return typedslicecopy(de.common(), ds, ss)
	2476  }
	2477  
	2478  // A runtimeSelect is a single case passed to rselect.
	2479  // This must match ../runtime/select.go:/runtimeSelect
	2480  type runtimeSelect struct {
	2481  	dir SelectDir			// SelectSend, SelectRecv or SelectDefault
	2482  	typ *rtype				 // channel type
	2483  	ch	unsafe.Pointer // channel
	2484  	val unsafe.Pointer // ptr to data (SendDir) or ptr to receive buffer (RecvDir)
	2485  }
	2486  
	2487  // rselect runs a select. It returns the index of the chosen case.
	2488  // If the case was a receive, val is filled in with the received value.
	2489  // The conventional OK bool indicates whether the receive corresponds
	2490  // to a sent value.
	2491  //go:noescape
	2492  func rselect([]runtimeSelect) (chosen int, recvOK bool)
	2493  
	2494  // A SelectDir describes the communication direction of a select case.
	2495  type SelectDir int
	2496  
	2497  // NOTE: These values must match ../runtime/select.go:/selectDir.
	2498  
	2499  const (
	2500  	_						 SelectDir = iota
	2501  	SelectSend							// case Chan <- Send
	2502  	SelectRecv							// case <-Chan:
	2503  	SelectDefault					 // default
	2504  )
	2505  
	2506  // A SelectCase describes a single case in a select operation.
	2507  // The kind of case depends on Dir, the communication direction.
	2508  //
	2509  // If Dir is SelectDefault, the case represents a default case.
	2510  // Chan and Send must be zero Values.
	2511  //
	2512  // If Dir is SelectSend, the case represents a send operation.
	2513  // Normally Chan's underlying value must be a channel, and Send's underlying value must be
	2514  // assignable to the channel's element type. As a special case, if Chan is a zero Value,
	2515  // then the case is ignored, and the field Send will also be ignored and may be either zero
	2516  // or non-zero.
	2517  //
	2518  // If Dir is SelectRecv, the case represents a receive operation.
	2519  // Normally Chan's underlying value must be a channel and Send must be a zero Value.
	2520  // If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value.
	2521  // When a receive operation is selected, the received Value is returned by Select.
	2522  //
	2523  type SelectCase struct {
	2524  	Dir	SelectDir // direction of case
	2525  	Chan Value		 // channel to use (for send or receive)
	2526  	Send Value		 // value to send (for send)
	2527  }
	2528  
	2529  // Select executes a select operation described by the list of cases.
	2530  // Like the Go select statement, it blocks until at least one of the cases
	2531  // can proceed, makes a uniform pseudo-random choice,
	2532  // and then executes that case. It returns the index of the chosen case
	2533  // and, if that case was a receive operation, the value received and a
	2534  // boolean indicating whether the value corresponds to a send on the channel
	2535  // (as opposed to a zero value received because the channel is closed).
	2536  // Select supports a maximum of 65536 cases.
	2537  func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool) {
	2538  	if len(cases) > 65536 {
	2539  		panic("reflect.Select: too many cases (max 65536)")
	2540  	}
	2541  	// NOTE: Do not trust that caller is not modifying cases data underfoot.
	2542  	// The range is safe because the caller cannot modify our copy of the len
	2543  	// and each iteration makes its own copy of the value c.
	2544  	var runcases []runtimeSelect
	2545  	if len(cases) > 4 {
	2546  		// Slice is heap allocated due to runtime dependent capacity.
	2547  		runcases = make([]runtimeSelect, len(cases))
	2548  	} else {
	2549  		// Slice can be stack allocated due to constant capacity.
	2550  		runcases = make([]runtimeSelect, len(cases), 4)
	2551  	}
	2552  
	2553  	haveDefault := false
	2554  	for i, c := range cases {
	2555  		rc := &runcases[i]
	2556  		rc.dir = c.Dir
	2557  		switch c.Dir {
	2558  		default:
	2559  			panic("reflect.Select: invalid Dir")
	2560  
	2561  		case SelectDefault: // default
	2562  			if haveDefault {
	2563  				panic("reflect.Select: multiple default cases")
	2564  			}
	2565  			haveDefault = true
	2566  			if c.Chan.IsValid() {
	2567  				panic("reflect.Select: default case has Chan value")
	2568  			}
	2569  			if c.Send.IsValid() {
	2570  				panic("reflect.Select: default case has Send value")
	2571  			}
	2572  
	2573  		case SelectSend:
	2574  			ch := c.Chan
	2575  			if !ch.IsValid() {
	2576  				break
	2577  			}
	2578  			ch.mustBe(Chan)
	2579  			ch.mustBeExported()
	2580  			tt := (*chanType)(unsafe.Pointer(ch.typ))
	2581  			if ChanDir(tt.dir)&SendDir == 0 {
	2582  				panic("reflect.Select: SendDir case using recv-only channel")
	2583  			}
	2584  			rc.ch = ch.pointer()
	2585  			rc.typ = &tt.rtype
	2586  			v := c.Send
	2587  			if !v.IsValid() {
	2588  				panic("reflect.Select: SendDir case missing Send value")
	2589  			}
	2590  			v.mustBeExported()
	2591  			v = v.assignTo("reflect.Select", tt.elem, nil)
	2592  			if v.flag&flagIndir != 0 {
	2593  				rc.val = v.ptr
	2594  			} else {
	2595  				rc.val = unsafe.Pointer(&v.ptr)
	2596  			}
	2597  
	2598  		case SelectRecv:
	2599  			if c.Send.IsValid() {
	2600  				panic("reflect.Select: RecvDir case has Send value")
	2601  			}
	2602  			ch := c.Chan
	2603  			if !ch.IsValid() {
	2604  				break
	2605  			}
	2606  			ch.mustBe(Chan)
	2607  			ch.mustBeExported()
	2608  			tt := (*chanType)(unsafe.Pointer(ch.typ))
	2609  			if ChanDir(tt.dir)&RecvDir == 0 {
	2610  				panic("reflect.Select: RecvDir case using send-only channel")
	2611  			}
	2612  			rc.ch = ch.pointer()
	2613  			rc.typ = &tt.rtype
	2614  			rc.val = unsafe_New(tt.elem)
	2615  		}
	2616  	}
	2617  
	2618  	chosen, recvOK = rselect(runcases)
	2619  	if runcases[chosen].dir == SelectRecv {
	2620  		tt := (*chanType)(unsafe.Pointer(runcases[chosen].typ))
	2621  		t := tt.elem
	2622  		p := runcases[chosen].val
	2623  		fl := flag(t.Kind())
	2624  		if ifaceIndir(t) {
	2625  			recv = Value{t, p, fl | flagIndir}
	2626  		} else {
	2627  			recv = Value{t, *(*unsafe.Pointer)(p), fl}
	2628  		}
	2629  	}
	2630  	return chosen, recv, recvOK
	2631  }
	2632  
	2633  /*
	2634   * constructors
	2635   */
	2636  
	2637  // implemented in package runtime
	2638  func unsafe_New(*rtype) unsafe.Pointer
	2639  func unsafe_NewArray(*rtype, int) unsafe.Pointer
	2640  
	2641  // MakeSlice creates a new zero-initialized slice value
	2642  // for the specified slice type, length, and capacity.
	2643  func MakeSlice(typ Type, len, cap int) Value {
	2644  	if typ.Kind() != Slice {
	2645  		panic("reflect.MakeSlice of non-slice type")
	2646  	}
	2647  	if len < 0 {
	2648  		panic("reflect.MakeSlice: negative len")
	2649  	}
	2650  	if cap < 0 {
	2651  		panic("reflect.MakeSlice: negative cap")
	2652  	}
	2653  	if len > cap {
	2654  		panic("reflect.MakeSlice: len > cap")
	2655  	}
	2656  
	2657  	s := unsafeheader.Slice{Data: unsafe_NewArray(typ.Elem().(*rtype), cap), Len: len, Cap: cap}
	2658  	return Value{typ.(*rtype), unsafe.Pointer(&s), flagIndir | flag(Slice)}
	2659  }
	2660  
	2661  // MakeChan creates a new channel with the specified type and buffer size.
	2662  func MakeChan(typ Type, buffer int) Value {
	2663  	if typ.Kind() != Chan {
	2664  		panic("reflect.MakeChan of non-chan type")
	2665  	}
	2666  	if buffer < 0 {
	2667  		panic("reflect.MakeChan: negative buffer size")
	2668  	}
	2669  	if typ.ChanDir() != BothDir {
	2670  		panic("reflect.MakeChan: unidirectional channel type")
	2671  	}
	2672  	t := typ.(*rtype)
	2673  	ch := makechan(t, buffer)
	2674  	return Value{t, ch, flag(Chan)}
	2675  }
	2676  
	2677  // MakeMap creates a new map with the specified type.
	2678  func MakeMap(typ Type) Value {
	2679  	return MakeMapWithSize(typ, 0)
	2680  }
	2681  
	2682  // MakeMapWithSize creates a new map with the specified type
	2683  // and initial space for approximately n elements.
	2684  func MakeMapWithSize(typ Type, n int) Value {
	2685  	if typ.Kind() != Map {
	2686  		panic("reflect.MakeMapWithSize of non-map type")
	2687  	}
	2688  	t := typ.(*rtype)
	2689  	m := makemap(t, n)
	2690  	return Value{t, m, flag(Map)}
	2691  }
	2692  
	2693  // Indirect returns the value that v points to.
	2694  // If v is a nil pointer, Indirect returns a zero Value.
	2695  // If v is not a pointer, Indirect returns v.
	2696  func Indirect(v Value) Value {
	2697  	if v.Kind() != Ptr {
	2698  		return v
	2699  	}
	2700  	return v.Elem()
	2701  }
	2702  
	2703  // ValueOf returns a new Value initialized to the concrete value
	2704  // stored in the interface i. ValueOf(nil) returns the zero Value.
	2705  func ValueOf(i interface{}) Value {
	2706  	if i == nil {
	2707  		return Value{}
	2708  	}
	2709  
	2710  	// TODO: Maybe allow contents of a Value to live on the stack.
	2711  	// For now we make the contents always escape to the heap. It
	2712  	// makes life easier in a few places (see chanrecv/mapassign
	2713  	// comment below).
	2714  	escapes(i)
	2715  
	2716  	return unpackEface(i)
	2717  }
	2718  
	2719  // Zero returns a Value representing the zero value for the specified type.
	2720  // The result is different from the zero value of the Value struct,
	2721  // which represents no value at all.
	2722  // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
	2723  // The returned value is neither addressable nor settable.
	2724  func Zero(typ Type) Value {
	2725  	if typ == nil {
	2726  		panic("reflect: Zero(nil)")
	2727  	}
	2728  	t := typ.(*rtype)
	2729  	fl := flag(t.Kind())
	2730  	if ifaceIndir(t) {
	2731  		var p unsafe.Pointer
	2732  		if t.size <= maxZero {
	2733  			p = unsafe.Pointer(&zeroVal[0])
	2734  		} else {
	2735  			p = unsafe_New(t)
	2736  		}
	2737  		return Value{t, p, fl | flagIndir}
	2738  	}
	2739  	return Value{t, nil, fl}
	2740  }
	2741  
	2742  // must match declarations in runtime/map.go.
	2743  const maxZero = 1024
	2744  
	2745  //go:linkname zeroVal runtime.zeroVal
	2746  var zeroVal [maxZero]byte
	2747  
	2748  // New returns a Value representing a pointer to a new zero value
	2749  // for the specified type. That is, the returned Value's Type is PtrTo(typ).
	2750  func New(typ Type) Value {
	2751  	if typ == nil {
	2752  		panic("reflect: New(nil)")
	2753  	}
	2754  	t := typ.(*rtype)
	2755  	pt := t.ptrTo()
	2756  	if ifaceIndir(pt) {
	2757  		// This is a pointer to a go:notinheap type.
	2758  		panic("reflect: New of type that may not be allocated in heap (possibly undefined cgo C type)")
	2759  	}
	2760  	ptr := unsafe_New(t)
	2761  	fl := flag(Ptr)
	2762  	return Value{pt, ptr, fl}
	2763  }
	2764  
	2765  // NewAt returns a Value representing a pointer to a value of the
	2766  // specified type, using p as that pointer.
	2767  func NewAt(typ Type, p unsafe.Pointer) Value {
	2768  	fl := flag(Ptr)
	2769  	t := typ.(*rtype)
	2770  	return Value{t.ptrTo(), p, fl}
	2771  }
	2772  
	2773  // assignTo returns a value v that can be assigned directly to typ.
	2774  // It panics if v is not assignable to typ.
	2775  // For a conversion to an interface type, target is a suggested scratch space to use.
	2776  // target must be initialized memory (or nil).
	2777  func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value {
	2778  	if v.flag&flagMethod != 0 {
	2779  		v = makeMethodValue(context, v)
	2780  	}
	2781  
	2782  	switch {
	2783  	case directlyAssignable(dst, v.typ):
	2784  		// Overwrite type so that they match.
	2785  		// Same memory layout, so no harm done.
	2786  		fl := v.flag&(flagAddr|flagIndir) | v.flag.ro()
	2787  		fl |= flag(dst.Kind())
	2788  		return Value{dst, v.ptr, fl}
	2789  
	2790  	case implements(dst, v.typ):
	2791  		if target == nil {
	2792  			target = unsafe_New(dst)
	2793  		}
	2794  		if v.Kind() == Interface && v.IsNil() {
	2795  			// A nil ReadWriter passed to nil Reader is OK,
	2796  			// but using ifaceE2I below will panic.
	2797  			// Avoid the panic by returning a nil dst (e.g., Reader) explicitly.
	2798  			return Value{dst, nil, flag(Interface)}
	2799  		}
	2800  		x := valueInterface(v, false)
	2801  		if dst.NumMethod() == 0 {
	2802  			*(*interface{})(target) = x
	2803  		} else {
	2804  			ifaceE2I(dst, x, target)
	2805  		}
	2806  		return Value{dst, target, flagIndir | flag(Interface)}
	2807  	}
	2808  
	2809  	// Failed.
	2810  	panic(context + ": value of type " + v.typ.String() + " is not assignable to type " + dst.String())
	2811  }
	2812  
	2813  // Convert returns the value v converted to type t.
	2814  // If the usual Go conversion rules do not allow conversion
	2815  // of the value v to type t, or if converting v to type t panics, Convert panics.
	2816  func (v Value) Convert(t Type) Value {
	2817  	if v.flag&flagMethod != 0 {
	2818  		v = makeMethodValue("Convert", v)
	2819  	}
	2820  	op := convertOp(t.common(), v.typ)
	2821  	if op == nil {
	2822  		panic("reflect.Value.Convert: value of type " + v.typ.String() + " cannot be converted to type " + t.String())
	2823  	}
	2824  	return op(v, t)
	2825  }
	2826  
	2827  // CanConvert reports whether the value v can be converted to type t.
	2828  // If v.CanConvert(t) returns true then v.Convert(t) will not panic.
	2829  func (v Value) CanConvert(t Type) bool {
	2830  	vt := v.Type()
	2831  	if !vt.ConvertibleTo(t) {
	2832  		return false
	2833  	}
	2834  	// Currently the only conversion that is OK in terms of type
	2835  	// but that can panic depending on the value is converting
	2836  	// from slice to pointer-to-array.
	2837  	if vt.Kind() == Slice && t.Kind() == Ptr && t.Elem().Kind() == Array {
	2838  		n := t.Elem().Len()
	2839  		h := (*unsafeheader.Slice)(v.ptr)
	2840  		if n > h.Len {
	2841  			return false
	2842  		}
	2843  	}
	2844  	return true
	2845  }
	2846  
	2847  // convertOp returns the function to convert a value of type src
	2848  // to a value of type dst. If the conversion is illegal, convertOp returns nil.
	2849  func convertOp(dst, src *rtype) func(Value, Type) Value {
	2850  	switch src.Kind() {
	2851  	case Int, Int8, Int16, Int32, Int64:
	2852  		switch dst.Kind() {
	2853  		case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
	2854  			return cvtInt
	2855  		case Float32, Float64:
	2856  			return cvtIntFloat
	2857  		case String:
	2858  			return cvtIntString
	2859  		}
	2860  
	2861  	case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
	2862  		switch dst.Kind() {
	2863  		case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
	2864  			return cvtUint
	2865  		case Float32, Float64:
	2866  			return cvtUintFloat
	2867  		case String:
	2868  			return cvtUintString
	2869  		}
	2870  
	2871  	case Float32, Float64:
	2872  		switch dst.Kind() {
	2873  		case Int, Int8, Int16, Int32, Int64:
	2874  			return cvtFloatInt
	2875  		case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
	2876  			return cvtFloatUint
	2877  		case Float32, Float64:
	2878  			return cvtFloat
	2879  		}
	2880  
	2881  	case Complex64, Complex128:
	2882  		switch dst.Kind() {
	2883  		case Complex64, Complex128:
	2884  			return cvtComplex
	2885  		}
	2886  
	2887  	case String:
	2888  		if dst.Kind() == Slice && dst.Elem().PkgPath() == "" {
	2889  			switch dst.Elem().Kind() {
	2890  			case Uint8:
	2891  				return cvtStringBytes
	2892  			case Int32:
	2893  				return cvtStringRunes
	2894  			}
	2895  		}
	2896  
	2897  	case Slice:
	2898  		if dst.Kind() == String && src.Elem().PkgPath() == "" {
	2899  			switch src.Elem().Kind() {
	2900  			case Uint8:
	2901  				return cvtBytesString
	2902  			case Int32:
	2903  				return cvtRunesString
	2904  			}
	2905  		}
	2906  		// "x is a slice, T is a pointer-to-array type,
	2907  		// and the slice and array types have identical element types."
	2908  		if dst.Kind() == Ptr && dst.Elem().Kind() == Array && src.Elem() == dst.Elem().Elem() {
	2909  			return cvtSliceArrayPtr
	2910  		}
	2911  
	2912  	case Chan:
	2913  		if dst.Kind() == Chan && specialChannelAssignability(dst, src) {
	2914  			return cvtDirect
	2915  		}
	2916  	}
	2917  
	2918  	// dst and src have same underlying type.
	2919  	if haveIdenticalUnderlyingType(dst, src, false) {
	2920  		return cvtDirect
	2921  	}
	2922  
	2923  	// dst and src are non-defined pointer types with same underlying base type.
	2924  	if dst.Kind() == Ptr && dst.Name() == "" &&
	2925  		src.Kind() == Ptr && src.Name() == "" &&
	2926  		haveIdenticalUnderlyingType(dst.Elem().common(), src.Elem().common(), false) {
	2927  		return cvtDirect
	2928  	}
	2929  
	2930  	if implements(dst, src) {
	2931  		if src.Kind() == Interface {
	2932  			return cvtI2I
	2933  		}
	2934  		return cvtT2I
	2935  	}
	2936  
	2937  	return nil
	2938  }
	2939  
	2940  // makeInt returns a Value of type t equal to bits (possibly truncated),
	2941  // where t is a signed or unsigned int type.
	2942  func makeInt(f flag, bits uint64, t Type) Value {
	2943  	typ := t.common()
	2944  	ptr := unsafe_New(typ)
	2945  	switch typ.size {
	2946  	case 1:
	2947  		*(*uint8)(ptr) = uint8(bits)
	2948  	case 2:
	2949  		*(*uint16)(ptr) = uint16(bits)
	2950  	case 4:
	2951  		*(*uint32)(ptr) = uint32(bits)
	2952  	case 8:
	2953  		*(*uint64)(ptr) = bits
	2954  	}
	2955  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
	2956  }
	2957  
	2958  // makeFloat returns a Value of type t equal to v (possibly truncated to float32),
	2959  // where t is a float32 or float64 type.
	2960  func makeFloat(f flag, v float64, t Type) Value {
	2961  	typ := t.common()
	2962  	ptr := unsafe_New(typ)
	2963  	switch typ.size {
	2964  	case 4:
	2965  		*(*float32)(ptr) = float32(v)
	2966  	case 8:
	2967  		*(*float64)(ptr) = v
	2968  	}
	2969  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
	2970  }
	2971  
	2972  // makeFloat returns a Value of type t equal to v, where t is a float32 type.
	2973  func makeFloat32(f flag, v float32, t Type) Value {
	2974  	typ := t.common()
	2975  	ptr := unsafe_New(typ)
	2976  	*(*float32)(ptr) = v
	2977  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
	2978  }
	2979  
	2980  // makeComplex returns a Value of type t equal to v (possibly truncated to complex64),
	2981  // where t is a complex64 or complex128 type.
	2982  func makeComplex(f flag, v complex128, t Type) Value {
	2983  	typ := t.common()
	2984  	ptr := unsafe_New(typ)
	2985  	switch typ.size {
	2986  	case 8:
	2987  		*(*complex64)(ptr) = complex64(v)
	2988  	case 16:
	2989  		*(*complex128)(ptr) = v
	2990  	}
	2991  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
	2992  }
	2993  
	2994  func makeString(f flag, v string, t Type) Value {
	2995  	ret := New(t).Elem()
	2996  	ret.SetString(v)
	2997  	ret.flag = ret.flag&^flagAddr | f
	2998  	return ret
	2999  }
	3000  
	3001  func makeBytes(f flag, v []byte, t Type) Value {
	3002  	ret := New(t).Elem()
	3003  	ret.SetBytes(v)
	3004  	ret.flag = ret.flag&^flagAddr | f
	3005  	return ret
	3006  }
	3007  
	3008  func makeRunes(f flag, v []rune, t Type) Value {
	3009  	ret := New(t).Elem()
	3010  	ret.setRunes(v)
	3011  	ret.flag = ret.flag&^flagAddr | f
	3012  	return ret
	3013  }
	3014  
	3015  // These conversion functions are returned by convertOp
	3016  // for classes of conversions. For example, the first function, cvtInt,
	3017  // takes any value v of signed int type and returns the value converted
	3018  // to type t, where t is any signed or unsigned int type.
	3019  
	3020  // convertOp: intXX -> [u]intXX
	3021  func cvtInt(v Value, t Type) Value {
	3022  	return makeInt(v.flag.ro(), uint64(v.Int()), t)
	3023  }
	3024  
	3025  // convertOp: uintXX -> [u]intXX
	3026  func cvtUint(v Value, t Type) Value {
	3027  	return makeInt(v.flag.ro(), v.Uint(), t)
	3028  }
	3029  
	3030  // convertOp: floatXX -> intXX
	3031  func cvtFloatInt(v Value, t Type) Value {
	3032  	return makeInt(v.flag.ro(), uint64(int64(v.Float())), t)
	3033  }
	3034  
	3035  // convertOp: floatXX -> uintXX
	3036  func cvtFloatUint(v Value, t Type) Value {
	3037  	return makeInt(v.flag.ro(), uint64(v.Float()), t)
	3038  }
	3039  
	3040  // convertOp: intXX -> floatXX
	3041  func cvtIntFloat(v Value, t Type) Value {
	3042  	return makeFloat(v.flag.ro(), float64(v.Int()), t)
	3043  }
	3044  
	3045  // convertOp: uintXX -> floatXX
	3046  func cvtUintFloat(v Value, t Type) Value {
	3047  	return makeFloat(v.flag.ro(), float64(v.Uint()), t)
	3048  }
	3049  
	3050  // convertOp: floatXX -> floatXX
	3051  func cvtFloat(v Value, t Type) Value {
	3052  	if v.Type().Kind() == Float32 && t.Kind() == Float32 {
	3053  		// Don't do any conversion if both types have underlying type float32.
	3054  		// This avoids converting to float64 and back, which will
	3055  		// convert a signaling NaN to a quiet NaN. See issue 36400.
	3056  		return makeFloat32(v.flag.ro(), *(*float32)(v.ptr), t)
	3057  	}
	3058  	return makeFloat(v.flag.ro(), v.Float(), t)
	3059  }
	3060  
	3061  // convertOp: complexXX -> complexXX
	3062  func cvtComplex(v Value, t Type) Value {
	3063  	return makeComplex(v.flag.ro(), v.Complex(), t)
	3064  }
	3065  
	3066  // convertOp: intXX -> string
	3067  func cvtIntString(v Value, t Type) Value {
	3068  	s := "\uFFFD"
	3069  	if x := v.Int(); int64(rune(x)) == x {
	3070  		s = string(rune(x))
	3071  	}
	3072  	return makeString(v.flag.ro(), s, t)
	3073  }
	3074  
	3075  // convertOp: uintXX -> string
	3076  func cvtUintString(v Value, t Type) Value {
	3077  	s := "\uFFFD"
	3078  	if x := v.Uint(); uint64(rune(x)) == x {
	3079  		s = string(rune(x))
	3080  	}
	3081  	return makeString(v.flag.ro(), s, t)
	3082  }
	3083  
	3084  // convertOp: []byte -> string
	3085  func cvtBytesString(v Value, t Type) Value {
	3086  	return makeString(v.flag.ro(), string(v.Bytes()), t)
	3087  }
	3088  
	3089  // convertOp: string -> []byte
	3090  func cvtStringBytes(v Value, t Type) Value {
	3091  	return makeBytes(v.flag.ro(), []byte(v.String()), t)
	3092  }
	3093  
	3094  // convertOp: []rune -> string
	3095  func cvtRunesString(v Value, t Type) Value {
	3096  	return makeString(v.flag.ro(), string(v.runes()), t)
	3097  }
	3098  
	3099  // convertOp: string -> []rune
	3100  func cvtStringRunes(v Value, t Type) Value {
	3101  	return makeRunes(v.flag.ro(), []rune(v.String()), t)
	3102  }
	3103  
	3104  // convertOp: []T -> *[N]T
	3105  func cvtSliceArrayPtr(v Value, t Type) Value {
	3106  	n := t.Elem().Len()
	3107  	h := (*unsafeheader.Slice)(v.ptr)
	3108  	if n > h.Len {
	3109  		panic("reflect: cannot convert slice with length " + itoa.Itoa(h.Len) + " to pointer to array with length " + itoa.Itoa(n))
	3110  	}
	3111  	return Value{t.common(), h.Data, v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Ptr)}
	3112  }
	3113  
	3114  // convertOp: direct copy
	3115  func cvtDirect(v Value, typ Type) Value {
	3116  	f := v.flag
	3117  	t := typ.common()
	3118  	ptr := v.ptr
	3119  	if f&flagAddr != 0 {
	3120  		// indirect, mutable word - make a copy
	3121  		c := unsafe_New(t)
	3122  		typedmemmove(t, c, ptr)
	3123  		ptr = c
	3124  		f &^= flagAddr
	3125  	}
	3126  	return Value{t, ptr, v.flag.ro() | f} // v.flag.ro()|f == f?
	3127  }
	3128  
	3129  // convertOp: concrete -> interface
	3130  func cvtT2I(v Value, typ Type) Value {
	3131  	target := unsafe_New(typ.common())
	3132  	x := valueInterface(v, false)
	3133  	if typ.NumMethod() == 0 {
	3134  		*(*interface{})(target) = x
	3135  	} else {
	3136  		ifaceE2I(typ.(*rtype), x, target)
	3137  	}
	3138  	return Value{typ.common(), target, v.flag.ro() | flagIndir | flag(Interface)}
	3139  }
	3140  
	3141  // convertOp: interface -> interface
	3142  func cvtI2I(v Value, typ Type) Value {
	3143  	if v.IsNil() {
	3144  		ret := Zero(typ)
	3145  		ret.flag |= v.flag.ro()
	3146  		return ret
	3147  	}
	3148  	return cvtT2I(v.Elem(), typ)
	3149  }
	3150  
	3151  // implemented in ../runtime
	3152  func chancap(ch unsafe.Pointer) int
	3153  func chanclose(ch unsafe.Pointer)
	3154  func chanlen(ch unsafe.Pointer) int
	3155  
	3156  // Note: some of the noescape annotations below are technically a lie,
	3157  // but safe in the context of this package. Functions like chansend
	3158  // and mapassign don't escape the referent, but may escape anything
	3159  // the referent points to (they do shallow copies of the referent).
	3160  // It is safe in this package because the referent may only point
	3161  // to something a Value may point to, and that is always in the heap
	3162  // (due to the escapes() call in ValueOf).
	3163  
	3164  //go:noescape
	3165  func chanrecv(ch unsafe.Pointer, nb bool, val unsafe.Pointer) (selected, received bool)
	3166  
	3167  //go:noescape
	3168  func chansend(ch unsafe.Pointer, val unsafe.Pointer, nb bool) bool
	3169  
	3170  func makechan(typ *rtype, size int) (ch unsafe.Pointer)
	3171  func makemap(t *rtype, cap int) (m unsafe.Pointer)
	3172  
	3173  //go:noescape
	3174  func mapaccess(t *rtype, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer)
	3175  
	3176  //go:noescape
	3177  func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
	3178  
	3179  //go:noescape
	3180  func mapdelete(t *rtype, m unsafe.Pointer, key unsafe.Pointer)
	3181  
	3182  // m escapes into the return value, but the caller of mapiterinit
	3183  // doesn't let the return value escape.
	3184  //go:noescape
	3185  func mapiterinit(t *rtype, m unsafe.Pointer) unsafe.Pointer
	3186  
	3187  //go:noescape
	3188  func mapiterkey(it unsafe.Pointer) (key unsafe.Pointer)
	3189  
	3190  //go:noescape
	3191  func mapiterelem(it unsafe.Pointer) (elem unsafe.Pointer)
	3192  
	3193  //go:noescape
	3194  func mapiternext(it unsafe.Pointer)
	3195  
	3196  //go:noescape
	3197  func maplen(m unsafe.Pointer) int
	3198  
	3199  // call calls fn with "stackArgsSize" bytes of stack arguments laid out
	3200  // at stackArgs and register arguments laid out in regArgs. frameSize is
	3201  // the total amount of stack space that will be reserved by call, so this
	3202  // should include enough space to spill register arguments to the stack in
	3203  // case of preemption.
	3204  //
	3205  // After fn returns, call copies stackArgsSize-stackRetOffset result bytes
	3206  // back into stackArgs+stackRetOffset before returning, for any return
	3207  // values passed on the stack. Register-based return values will be found
	3208  // in the same regArgs structure.
	3209  //
	3210  // regArgs must also be prepared with an appropriate ReturnIsPtr bitmap
	3211  // indicating which registers will contain pointer-valued return values. The
	3212  // purpose of this bitmap is to keep pointers visible to the GC between
	3213  // returning from reflectcall and actually using them.
	3214  //
	3215  // If copying result bytes back from the stack, the caller must pass the
	3216  // argument frame type as stackArgsType, so that call can execute appropriate
	3217  // write barriers during the copy.
	3218  //
	3219  // Arguments passed through to call do not escape. The type is used only in a
	3220  // very limited callee of call, the stackArgs are copied, and regArgs is only
	3221  // used in the call frame.
	3222  //go:noescape
	3223  //go:linkname call runtime.reflectcall
	3224  func call(stackArgsType *rtype, f, stackArgs unsafe.Pointer, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs)
	3225  
	3226  func ifaceE2I(t *rtype, src interface{}, dst unsafe.Pointer)
	3227  
	3228  // memmove copies size bytes to dst from src. No write barriers are used.
	3229  //go:noescape
	3230  func memmove(dst, src unsafe.Pointer, size uintptr)
	3231  
	3232  // typedmemmove copies a value of type t to dst from src.
	3233  //go:noescape
	3234  func typedmemmove(t *rtype, dst, src unsafe.Pointer)
	3235  
	3236  // typedmemmovepartial is like typedmemmove but assumes that
	3237  // dst and src point off bytes into the value and only copies size bytes.
	3238  //go:noescape
	3239  func typedmemmovepartial(t *rtype, dst, src unsafe.Pointer, off, size uintptr)
	3240  
	3241  // typedmemclr zeros the value at ptr of type t.
	3242  //go:noescape
	3243  func typedmemclr(t *rtype, ptr unsafe.Pointer)
	3244  
	3245  // typedmemclrpartial is like typedmemclr but assumes that
	3246  // dst points off bytes into the value and only clears size bytes.
	3247  //go:noescape
	3248  func typedmemclrpartial(t *rtype, ptr unsafe.Pointer, off, size uintptr)
	3249  
	3250  // typedslicecopy copies a slice of elemType values from src to dst,
	3251  // returning the number of elements copied.
	3252  //go:noescape
	3253  func typedslicecopy(elemType *rtype, dst, src unsafeheader.Slice) int
	3254  
	3255  //go:noescape
	3256  func typehash(t *rtype, p unsafe.Pointer, h uintptr) uintptr
	3257  
	3258  // Dummy annotation marking that the value x escapes,
	3259  // for use in cases where the reflect code is so clever that
	3260  // the compiler cannot follow.
	3261  func escapes(x interface{}) {
	3262  	if dummy.b {
	3263  		dummy.x = x
	3264  	}
	3265  }
	3266  
	3267  var dummy struct {
	3268  	b bool
	3269  	x interface{}
	3270  }
	3271  

View as plain text