Source file src/reflect/value.go

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

View as plain text