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

View as plain text