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

View as plain text