github.com/eh-steve/goloader@v0.0.0-20240111193454-90ff3cfdae39/reflectlite/reflectlite1.19/value.go (about)

     1  package reflectlite
     2  
     3  import (
     4  	"github.com/eh-steve/goloader/reflectlite/internal/goarch"
     5  	"github.com/eh-steve/goloader/reflectlite/internal/itoa"
     6  	"github.com/eh-steve/goloader/reflectlite/internal/unsafeheader"
     7  	"runtime"
     8  	"unsafe"
     9  )
    10  
    11  // Value is the reflection interface to a Go value.
    12  //
    13  // Not all methods apply to all kinds of values. Restrictions,
    14  // if any, are noted in the documentation for each method.
    15  // Use the Kind method to find out the kind of value before
    16  // calling kind-specific methods. Calling a method
    17  // inappropriate to the kind of type causes a run time panic.
    18  //
    19  // The zero Value represents no value.
    20  // Its IsValid method returns false, its Kind method returns Invalid,
    21  // its String method returns "<invalid Value>", and all other methods panic.
    22  // Most functions and methods never return an invalid value.
    23  // If one does, its documentation states the conditions explicitly.
    24  //
    25  // A Value can be used concurrently by multiple goroutines provided that
    26  // the underlying Go value can be used concurrently for the equivalent
    27  // direct operations.
    28  //
    29  // To compare two Values, compare the results of the Interface method.
    30  // Using == on two Values does not compare the underlying values
    31  // they represent.
    32  type Value struct {
    33  	// typ holds the type of the value represented by a Value.
    34  	typ *rtype
    35  
    36  	// Pointer-valued data or, if flagIndir is set, pointer to data.
    37  	// Valid when either flagIndir is set or typ.pointers() is true.
    38  	ptr unsafe.Pointer
    39  
    40  	// flag holds metadata about the value.
    41  	// The lowest bits are flag bits:
    42  	//	- flagStickyRO: obtained via unexported not embedded field, so read-only
    43  	//	- flagEmbedRO: obtained via unexported embedded field, so read-only
    44  	//	- flagIndir: val holds a pointer to the data
    45  	//	- flagAddr: v.CanAddr is true (implies flagIndir)
    46  	//	- flagMethod: v is a method value.
    47  	// The next five bits give the Kind of the value.
    48  	// This repeats typ.Kind() except for method values.
    49  	// The remaining 23+ bits give a method number for method values.
    50  	// If flag.kind() != Func, code can assume that flagMethod is unset.
    51  	// If ifaceIndir(typ), code can assume that flagIndir is set.
    52  	flag
    53  
    54  	// A method value represents a curried method invocation
    55  	// like r.Read for some receiver r. The typ+val+flag bits describe
    56  	// the receiver r, but the flag's Kind bits say Func (methods are
    57  	// functions), and the top bits of the flag give the method number
    58  	// in r's type's method table.
    59  }
    60  
    61  type flag uintptr
    62  
    63  const (
    64  	flagKindWidth        = 5 // there are 27 kinds
    65  	flagKindMask    flag = 1<<flagKindWidth - 1
    66  	flagStickyRO    flag = 1 << 5
    67  	flagEmbedRO     flag = 1 << 6
    68  	flagIndir       flag = 1 << 7
    69  	flagAddr        flag = 1 << 8
    70  	flagMethod      flag = 1 << 9
    71  	flagMethodShift      = 10
    72  	flagRO          flag = flagStickyRO | flagEmbedRO
    73  )
    74  
    75  func (f flag) kind() Kind {
    76  	return Kind(f & flagKindMask)
    77  }
    78  
    79  func (f flag) ro() flag {
    80  	if f&flagRO != 0 {
    81  		return flagStickyRO
    82  	}
    83  	return 0
    84  }
    85  
    86  // emptyInterface is the header for an interface{} value.
    87  type emptyInterface struct {
    88  	typ  *rtype
    89  	word unsafe.Pointer
    90  }
    91  
    92  // pointer returns the underlying pointer represented by v.
    93  // v.Kind() must be Pointer, Map, Chan, Func, or UnsafePointer
    94  // if v.Kind() == Pointer, the base type must not be go:notinheap.
    95  func (v Value) pointer() unsafe.Pointer {
    96  	if v.typ.size != goarch.PtrSize || !v.typ.pointers() {
    97  		panic("can't call pointer on a non-pointer Value")
    98  	}
    99  	if v.flag&flagIndir != 0 {
   100  		return *(*unsafe.Pointer)(v.ptr)
   101  	}
   102  	return v.ptr
   103  }
   104  
   105  // packEface converts v to the empty interface.
   106  func packEface(v Value) interface{} {
   107  	t := v.typ
   108  	var i interface{}
   109  	e := (*emptyInterface)(unsafe.Pointer(&i))
   110  	// First, fill in the data portion of the interface.
   111  	switch {
   112  	case ifaceIndir(t):
   113  		if v.flag&flagIndir == 0 {
   114  			panic("bad indir")
   115  		}
   116  		// Value is indirect, and so is the interface we're making.
   117  		ptr := v.ptr
   118  		if v.flag&flagAddr != 0 {
   119  			// TODO: pass safe boolean from valueInterface so
   120  			// we don't need to copy if safe==true?
   121  			c := unsafe_New(t)
   122  			typedmemmove(t, c, ptr)
   123  			ptr = c
   124  		}
   125  		e.word = ptr
   126  	case v.flag&flagIndir != 0:
   127  		// Value is indirect, but interface is direct. We need
   128  		// to load the data at v.ptr into the interface data word.
   129  		e.word = *(*unsafe.Pointer)(v.ptr)
   130  	default:
   131  		// Value is direct, and so is the interface.
   132  		e.word = v.ptr
   133  	}
   134  	// Now, fill in the type portion. We're very careful here not
   135  	// to have any operation between the e.word and e.typ assignments
   136  	// that would let the garbage collector observe the partially-built
   137  	// interface value.
   138  	e.typ = t
   139  	return i
   140  }
   141  
   142  // A ValueError occurs when a Value method is invoked on
   143  // a Value that does not support it. Such cases are documented
   144  // in the description of each method.
   145  type ValueError struct {
   146  	Method string
   147  	Kind   Kind
   148  }
   149  
   150  func (e *ValueError) Error() string {
   151  	if e.Kind == 0 {
   152  		return "reflect: call of " + e.Method + " on zero Value"
   153  	}
   154  	return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
   155  }
   156  
   157  // unpackEface converts the empty interface i to a Value.
   158  func unpackEface(i interface{}) Value {
   159  	e := (*emptyInterface)(unsafe.Pointer(&i))
   160  	// NOTE: don't read e.word until we know whether it is really a pointer or not.
   161  	t := e.typ
   162  	if t == nil {
   163  		return Value{}
   164  	}
   165  	f := flag(t.Kind())
   166  	if ifaceIndir(t) {
   167  		f |= flagIndir
   168  	}
   169  	return Value{t, e.word, f}
   170  }
   171  
   172  // Kind returns v's Kind.
   173  // If v is the zero Value (IsValid returns false), Kind returns Invalid.
   174  func (v Value) Kind() Kind {
   175  	return v.kind()
   176  }
   177  
   178  // Elem returns the value that the interface v contains
   179  // or that the pointer v points to.
   180  // It panics if v's Kind is not Interface or Pointer.
   181  // It returns the zero Value if v is nil.
   182  func (v Value) Elem() Value {
   183  	k := v.kind()
   184  	switch k {
   185  	case Interface:
   186  		var eface interface{}
   187  		if v.typ.NumMethod() == 0 {
   188  			eface = *(*interface{})(v.ptr)
   189  		} else {
   190  			eface = (interface{})(*(*interface {
   191  				M()
   192  			})(v.ptr))
   193  		}
   194  		x := unpackEface(eface)
   195  		if x.flag != 0 {
   196  			x.flag |= v.flag.ro()
   197  		}
   198  		return x
   199  	case Pointer:
   200  		ptr := v.ptr
   201  		if v.flag&flagIndir != 0 {
   202  			if ifaceIndir(v.typ) {
   203  				// This is a pointer to a not-in-heap object. ptr points to a uintptr
   204  				// in the heap. That uintptr is the address of a not-in-heap object.
   205  				// In general, pointers to not-in-heap objects can be total junk.
   206  				// But Elem() is asking to dereference it, so the user has asserted
   207  				// that at least it is a valid pointer (not just an integer stored in
   208  				// a pointer slot). So let's check, to make sure that it isn't a pointer
   209  				// that the runtime will crash on if it sees it during GC or write barriers.
   210  				// Since it is a not-in-heap pointer, all pointers to the heap are
   211  				// forbidden! That makes the test pretty easy.
   212  				// See issue 48399.
   213  				if !verifyNotInHeapPtr(*(*uintptr)(ptr)) {
   214  					panic("reflect: reflect.Value.Elem on an invalid notinheap pointer")
   215  				}
   216  			}
   217  			ptr = *(*unsafe.Pointer)(ptr)
   218  		}
   219  		// The returned value's address is v's value.
   220  		if ptr == nil {
   221  			return Value{}
   222  		}
   223  		tt := (*ptrType)(unsafe.Pointer(v.typ))
   224  		typ := tt.elem
   225  		fl := v.flag&flagRO | flagIndir | flagAddr
   226  		fl |= flag(typ.Kind())
   227  		return Value{typ, ptr, fl}
   228  	}
   229  	panic(&ValueError{"reflect.Value.Elem", v.kind()})
   230  }
   231  
   232  // Field returns the i'th field of the struct v.
   233  // It panics if v's Kind is not Struct or i is out of range.
   234  func (v Value) Field(i int) Value {
   235  	if v.kind() != Struct {
   236  		panic(&ValueError{"reflect.Value.Field", v.kind()})
   237  	}
   238  	tt := (*structType)(unsafe.Pointer(v.typ))
   239  	if uint(i) >= uint(len(tt.fields)) {
   240  		panic("reflect: Field index out of range")
   241  	}
   242  	field := &tt.fields[i]
   243  	typ := field.typ
   244  
   245  	// Inherit permission bits from v, but clear flagEmbedRO.
   246  	fl := v.flag&(flagStickyRO|flagIndir|flagAddr) | flag(typ.Kind())
   247  	// Using an unexported field forces flagRO.
   248  	if !field.name.isExported() {
   249  		if field.embedded() {
   250  			fl |= flagEmbedRO
   251  		} else {
   252  			fl |= flagStickyRO
   253  		}
   254  	}
   255  	// Either flagIndir is set and v.ptr points at struct,
   256  	// or flagIndir is not set and v.ptr is the actual struct data.
   257  	// In the former case, we want v.ptr + offset.
   258  	// In the latter case, we must have field.offset = 0,
   259  	// so v.ptr + field.offset is still the correct address.
   260  	ptr := add(v.ptr, field.offset, "same as non-reflect &v.field")
   261  	return Value{typ, ptr, fl}
   262  }
   263  
   264  // arrayAt returns the i-th element of p,
   265  // an array whose elements are eltSize bytes wide.
   266  // The array pointed at by p must have at least i+1 elements:
   267  // it is invalid (but impossible to check here) to pass i >= len,
   268  // because then the result will point outside the array.
   269  // whySafe must explain why i < len. (Passing "i < len" is fine;
   270  // the benefit is to surface this assumption at the call site.)
   271  func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Pointer {
   272  	return add(p, uintptr(i)*eltSize, "i < len")
   273  }
   274  
   275  var uint8Type = TypeOf(uint8(0)).(*rtype)
   276  
   277  // Index returns v's i'th element.
   278  // It panics if v's Kind is not Array, Slice, or String or i is out of range.
   279  func (v Value) Index(i int) Value {
   280  	switch v.kind() {
   281  	case Array:
   282  		tt := (*arrayType)(unsafe.Pointer(v.typ))
   283  		if uint(i) >= uint(tt.len) {
   284  			panic("reflect: array index out of range")
   285  		}
   286  		typ := tt.elem
   287  		offset := uintptr(i) * typ.size
   288  
   289  		// Either flagIndir is set and v.ptr points at array,
   290  		// or flagIndir is not set and v.ptr is the actual array data.
   291  		// In the former case, we want v.ptr + offset.
   292  		// In the latter case, we must be doing Index(0), so offset = 0,
   293  		// so v.ptr + offset is still the correct address.
   294  		val := add(v.ptr, offset, "same as &v[i], i < tt.len")
   295  		fl := (flagIndir | flagAddr) | v.flag.ro() | flag(typ.Kind()) // bits same as overall array
   296  		return Value{typ, val, fl}
   297  
   298  	case Slice:
   299  		// Element flag same as Elem of Pointer.
   300  		// Addressable, indirect, possibly read-only.
   301  		s := (*unsafeheader.Slice)(v.ptr)
   302  		if uint(i) >= uint(s.Len) {
   303  			panic("reflect: slice index out of range")
   304  		}
   305  		tt := (*sliceType)(unsafe.Pointer(v.typ))
   306  		typ := tt.elem
   307  		val := arrayAt(s.Data, i, typ.size, "i < s.Len")
   308  		fl := flagAddr | flagIndir | v.flag.ro() | flag(typ.Kind())
   309  		return Value{typ, val, fl}
   310  
   311  	case String:
   312  		s := (*unsafeheader.String)(v.ptr)
   313  		if uint(i) >= uint(s.Len) {
   314  			panic("reflect: string index out of range")
   315  		}
   316  		p := arrayAt(s.Data, i, 1, "i < s.Len")
   317  		fl := v.flag.ro() | flag(Uint8) | flagIndir
   318  		return Value{uint8Type, p, fl}
   319  	}
   320  	panic(&ValueError{"reflect.Value.Index", v.kind()})
   321  }
   322  func (v Value) Type() Type {
   323  	if v.flag != 0 && v.flag&flagMethod == 0 {
   324  		return v.typ
   325  	}
   326  	return v.typeSlow()
   327  }
   328  
   329  func (v Value) typeSlow() Type {
   330  	if v.flag == 0 {
   331  		panic(&ValueError{"reflect.Value.Type", Invalid})
   332  	}
   333  	if v.flag&flagMethod == 0 {
   334  		return v.typ
   335  	}
   336  
   337  	// Method value.
   338  	// v.typ describes the receiver, not the method type.
   339  	i := int(v.flag) >> flagMethodShift
   340  	if v.typ.Kind() == Interface {
   341  		// Method on interface.
   342  		tt := (*interfaceType)(unsafe.Pointer(v.typ))
   343  		if uint(i) >= uint(len(tt.methods)) {
   344  			panic("reflect: internal error: invalid method index")
   345  		}
   346  		m := &tt.methods[i]
   347  		return v.typ.typeOff(m.typ)
   348  	}
   349  	// Method on concrete type.
   350  	ms := v.typ.exportedMethods()
   351  	if uint(i) >= uint(len(ms)) {
   352  		panic("reflect: internal error: invalid method index")
   353  	}
   354  	m := ms[i]
   355  	return v.typ.typeOff(m.mtyp)
   356  }
   357  
   358  // NumField returns the number of fields in the struct v.
   359  // It panics if v's Kind is not Struct.
   360  func (v Value) NumField() int {
   361  	v.mustBe(Struct)
   362  	tt := (*structType)(unsafe.Pointer(v.typ))
   363  	return len(tt.fields)
   364  }
   365  
   366  // Indirect returns the value that v points to.
   367  // If v is a nil pointer, Indirect returns a zero Value.
   368  // If v is not a pointer, Indirect returns v.
   369  func Indirect(v Value) Value {
   370  	if v.Kind() != Pointer {
   371  		return v
   372  	}
   373  	return v.Elem()
   374  }
   375  
   376  // ValueOf returns a new Value initialized to the concrete value
   377  // stored in the interface i. ValueOf(nil) returns the zero Value.
   378  func ValueOf(i interface{}) Value {
   379  	if i == nil {
   380  		return Value{}
   381  	}
   382  
   383  	// TODO: Maybe allow contents of a Value to live on the stack.
   384  	// For now we make the contents always escape to the heap. It
   385  	// makes life easier in a few places (see chanrecv/mapassign
   386  	// comment below).
   387  	escapes(i)
   388  
   389  	return unpackEface(i)
   390  }
   391  
   392  // must match declarations in runtime/map.go.
   393  const maxZero = 1024
   394  
   395  //go:linkname zeroVal runtime.zeroVal
   396  var zeroVal [maxZero]byte
   397  
   398  // New returns a Value representing a pointer to a new zero value
   399  // for the specified type. That is, the returned Value's Type is PointerTo(typ).
   400  func New(typ Type) Value {
   401  	if typ == nil {
   402  		panic("reflect: New(nil)")
   403  	}
   404  	t := typ.(*rtype)
   405  	pt := t.ptrTo()
   406  	if ifaceIndir(pt) {
   407  		// This is a pointer to a go:notinheap type.
   408  		panic("reflect: New of type that may not be allocated in heap (possibly undefined cgo C type)")
   409  	}
   410  	ptr := unsafe_New(t)
   411  	fl := flag(Pointer)
   412  	return Value{pt, ptr, fl}
   413  }
   414  
   415  // NewAt returns a Value representing a pointer to a value of the
   416  // specified type, using p as that pointer.
   417  func NewAt(typ Type, p unsafe.Pointer) Value {
   418  	fl := flag(Pointer)
   419  	t := typ.(*rtype)
   420  	return Value{t.ptrTo(), p, fl}
   421  }
   422  
   423  // Zero returns a Value representing the zero value for the specified type.
   424  // The result is different from the zero value of the Value struct,
   425  // which represents no value at all.
   426  // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
   427  // The returned value is neither addressable nor settable.
   428  func Zero(typ Type) Value {
   429  	if typ == nil {
   430  		panic("reflect: Zero(nil)")
   431  	}
   432  	t := typ.(*rtype)
   433  	fl := flag(t.Kind())
   434  	if ifaceIndir(t) {
   435  		var p unsafe.Pointer
   436  		if t.size <= maxZero {
   437  			p = unsafe.Pointer(&zeroVal[0])
   438  		} else {
   439  			p = unsafe_New(t)
   440  		}
   441  		return Value{t, p, fl | flagIndir}
   442  	}
   443  	return Value{t, nil, fl}
   444  }
   445  
   446  // SliceHeader is the runtime representation of a slice.
   447  // It cannot be used safely or portably and its representation may
   448  // change in a later release.
   449  // Moreover, the Data field is not sufficient to guarantee the data
   450  // it references will not be garbage collected, so programs must keep
   451  // a separate, correctly typed pointer to the underlying data.
   452  type SliceHeader struct {
   453  	Data uintptr
   454  	Len  int
   455  	Cap  int
   456  }
   457  
   458  //go:nocheckptr
   459  // This prevents inlining Value.Pointer when -d=checkptr is enabled,
   460  // which ensures cmd/compile can recognize unsafe.Pointer(v.Pointer())
   461  // and make an exception.
   462  
   463  // Pointer returns v's value as a uintptr.
   464  // It returns uintptr instead of unsafe.Pointer so that
   465  // code using reflect cannot obtain unsafe.Pointers
   466  // without importing the unsafe package explicitly.
   467  // It panics if v's Kind is not Chan, Func, Map, Pointer, Slice, or UnsafePointer.
   468  //
   469  // If v's Kind is Func, the returned pointer is an underlying
   470  // code pointer, but not necessarily enough to identify a
   471  // single function uniquely. The only guarantee is that the
   472  // result is zero if and only if v is a nil func Value.
   473  //
   474  // If v's Kind is Slice, the returned pointer is to the first
   475  // element of the slice. If the slice is nil the returned value
   476  // is 0.  If the slice is empty but non-nil the return value is non-zero.
   477  //
   478  // It's preferred to use uintptr(Value.UnsafePointer()) to get the equivalent result.
   479  func (v Value) Pointer() uintptr {
   480  	k := v.kind()
   481  	switch k {
   482  	case Pointer:
   483  		if v.typ.ptrdata == 0 {
   484  			val := *(*uintptr)(v.ptr)
   485  			// Since it is a not-in-heap pointer, all pointers to the heap are
   486  			// forbidden! See comment in Value.Elem and issue #48399.
   487  			if !verifyNotInHeapPtr(val) {
   488  				panic("reflect: reflect.Value.Pointer on an invalid notinheap pointer")
   489  			}
   490  			return val
   491  		}
   492  		fallthrough
   493  	case Chan, Map, UnsafePointer:
   494  		return uintptr(v.pointer())
   495  	case Func:
   496  		if v.flag&flagMethod != 0 {
   497  			panic("method values not supported in reflectlite")
   498  		}
   499  		p := v.pointer()
   500  		// Non-nil func value points at data block.
   501  		// First word of data block is actual code.
   502  		if p != nil {
   503  			p = *(*unsafe.Pointer)(p)
   504  		}
   505  		return uintptr(p)
   506  
   507  	case Slice:
   508  		return (*SliceHeader)(v.ptr).Data
   509  	}
   510  	panic(&ValueError{"reflect.Value.Pointer", v.kind()})
   511  }
   512  
   513  //go:nocheckptr
   514  // This prevents inlining Value.UnsafeAddr when -d=checkptr is enabled,
   515  // which ensures cmd/compile can recognize unsafe.Pointer(v.UnsafeAddr())
   516  // and make an exception.
   517  
   518  // UnsafeAddr returns a pointer to v's data, as a uintptr.
   519  // It is for advanced clients that also import the "unsafe" package.
   520  // It panics if v is not addressable.
   521  //
   522  // It's preferred to use uintptr(Value.Addr().UnsafePointer()) to get the equivalent result.
   523  func (v Value) UnsafeAddr() uintptr {
   524  	if v.typ == nil {
   525  		panic(&ValueError{"reflect.Value.UnsafeAddr", Invalid})
   526  	}
   527  	if v.flag&flagAddr == 0 {
   528  		panic("reflect.Value.UnsafeAddr of unaddressable value")
   529  	}
   530  	return uintptr(v.ptr)
   531  }
   532  
   533  // valueMethodName returns the name of the exported calling method on Value.
   534  func valueMethodName() string {
   535  	var pc [5]uintptr
   536  	n := runtime.Callers(1, pc[:])
   537  	frames := runtime.CallersFrames(pc[:n])
   538  	var frame runtime.Frame
   539  	for more := true; more; {
   540  		const prefix = "reflect.Value."
   541  		frame, more = frames.Next()
   542  		name := frame.Function
   543  		if len(name) > len(prefix) && name[:len(prefix)] == prefix {
   544  			methodName := name[len(prefix):]
   545  			if len(methodName) > 0 && 'A' <= methodName[0] && methodName[0] <= 'Z' {
   546  				return name
   547  			}
   548  		}
   549  	}
   550  	return "unknown method"
   551  }
   552  
   553  // mustBe panics if f's kind is not expected.
   554  // Making this a method on flag instead of on Value
   555  // (and embedding flag in Value) means that we can write
   556  // the very clear v.mustBe(Bool) and have it compile into
   557  // v.flag.mustBe(Bool), which will only bother to copy the
   558  // single important word for the receiver.
   559  func (f flag) mustBe(expected Kind) {
   560  	// TODO(mvdan): use f.kind() again once mid-stack inlining gets better
   561  	if Kind(f&flagKindMask) != expected {
   562  		panic(&ValueError{valueMethodName(), f.kind()})
   563  	}
   564  }
   565  
   566  // mustBeAssignable panics if f records that the value is not assignable,
   567  // which is to say that either it was obtained using an unexported field
   568  // or it is not addressable.
   569  func (f flag) mustBeAssignable() {
   570  	if f&flagRO != 0 || f&flagAddr == 0 {
   571  		f.mustBeAssignableSlow()
   572  	}
   573  }
   574  
   575  func (f flag) mustBeAssignableSlow() {
   576  	if f == 0 {
   577  		panic(&ValueError{valueMethodName(), Invalid})
   578  	}
   579  	// Assignable if addressable.
   580  	if f&flagAddr == 0 {
   581  		panic("reflect: " + valueMethodName() + " using unaddressable value")
   582  	}
   583  }
   584  
   585  // directlyAssignable reports whether a value x of type V can be directly
   586  // assigned (using memmove) to a value of type T.
   587  // https://golang.org/doc/go_spec.html#Assignability
   588  // Ignoring the interface rules (implemented elsewhere)
   589  // and the ideal constant rules (no ideal constants at run time).
   590  func directlyAssignable(T, V *rtype, seen map[_typePair]struct{}) bool {
   591  	// x's type V is identical to T?
   592  	if T == V {
   593  		return true
   594  	}
   595  
   596  	// Otherwise at least one of T and V must not be defined
   597  	// and they must have the same kind.
   598  	if T.Kind() != V.Kind() {
   599  		return false
   600  	}
   601  
   602  	if T.Kind() == Chan && specialChannelAssignability(T, V, seen) {
   603  		return true
   604  	}
   605  
   606  	// x's type T and V must have identical underlying types.
   607  	return haveIdenticalUnderlyingType(T, V, false, false, seen)
   608  }
   609  
   610  // assignTo returns a value v that can be assigned directly to dst.
   611  // It panics if v is not assignable to dst.
   612  // For a conversion to an interface type, target, if not nil,
   613  // is a suggested scratch space to use.
   614  // target must be initialized memory (or nil).
   615  func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value {
   616  	if v.flag&flagMethod != 0 {
   617  		panic("method values not supported in reflectlite")
   618  	}
   619  
   620  	seen := map[_typePair]struct{}{}
   621  	switch {
   622  	case directlyAssignable(dst, v.typ, seen):
   623  		// Overwrite type so that they match.
   624  		// Same memory layout, so no harm done.
   625  		fl := v.flag&(flagAddr|flagIndir) | v.flag.ro()
   626  		fl |= flag(dst.Kind())
   627  		return Value{dst, v.ptr, fl}
   628  
   629  	case implements(dst, v.typ, seen):
   630  		if v.Kind() == Interface && v.IsNil() {
   631  			// A nil ReadWriter passed to nil Reader is OK,
   632  			// but using ifaceE2I below will panic.
   633  			// Avoid the panic by returning a nil dst (e.g., Reader) explicitly.
   634  			return Value{dst, nil, flag(Interface)}
   635  		}
   636  		x := valueInterface(v, false)
   637  		if target == nil {
   638  			target = unsafe_New(dst)
   639  		}
   640  		if dst.NumMethod() == 0 {
   641  			*(*interface{})(target) = x
   642  		} else {
   643  			ifaceE2I(dst, x, target)
   644  		}
   645  		return Value{dst, target, flagIndir | flag(Interface)}
   646  	}
   647  
   648  	// Failed.
   649  	panic(context + ": value of type " + v.typ.String() + " is not assignable to type " + dst.String())
   650  }
   651  
   652  // Set assigns x to the value v.
   653  // It panics if CanSet returns false.
   654  // As in Go, x's value must be assignable to v's type.
   655  func (v Value) Set(x Value) {
   656  	v.mustBeAssignable()
   657  	// x.mustBeExported() // do not let unexported x leak
   658  	var target unsafe.Pointer
   659  	if v.kind() == Interface {
   660  		target = v.ptr
   661  	}
   662  	x = x.assignTo("reflect.Set", v.typ, target)
   663  	if x.flag&flagIndir != 0 {
   664  		if x.ptr == unsafe.Pointer(&zeroVal[0]) {
   665  			typedmemclr(v.typ, v.ptr)
   666  		} else {
   667  			typedmemmove(v.typ, v.ptr, x.ptr)
   668  		}
   669  	} else {
   670  		*(*unsafe.Pointer)(v.ptr) = x.ptr
   671  	}
   672  }
   673  
   674  // SetPointer sets the unsafe.Pointer value v to x.
   675  // It panics if v's Kind is not UnsafePointer.
   676  func (v Value) SetPointer(x unsafe.Pointer) {
   677  	v.mustBeAssignable()
   678  	v.mustBe(UnsafePointer)
   679  	*(*unsafe.Pointer)(v.ptr) = x
   680  }
   681  
   682  // SetString sets v's underlying value to x.
   683  // It panics if v's Kind is not String or if CanSet() is false.
   684  func (v Value) SetString(x string) {
   685  	v.mustBeAssignable()
   686  	v.mustBe(String)
   687  	*(*string)(v.ptr) = x
   688  }
   689  
   690  // SetBytes sets v's underlying value.
   691  // It panics if v's underlying value is not a slice of bytes.
   692  func (v Value) SetBytes(x []byte) {
   693  	v.mustBeAssignable()
   694  	v.mustBe(Slice)
   695  	if v.typ.Elem().Kind() != Uint8 {
   696  		panic("reflect.Value.SetBytes of non-byte slice")
   697  	}
   698  	*(*[]byte)(v.ptr) = x
   699  }
   700  
   701  // setRunes sets v's underlying value.
   702  // It panics if v's underlying value is not a slice of runes (int32s).
   703  func (v Value) setRunes(x []rune) {
   704  	v.mustBeAssignable()
   705  	v.mustBe(Slice)
   706  	if v.typ.Elem().Kind() != Int32 {
   707  		panic("reflect.Value.setRunes of non-rune slice")
   708  	}
   709  	*(*[]rune)(v.ptr) = x
   710  }
   711  
   712  var stringType = TypeOf("").(*rtype)
   713  
   714  // Make sure these routines stay in sync with ../runtime/map.go!
   715  // These types exist only for GC, so we only fill out GC relevant info.
   716  // Currently, that's just size and the GC program. We also fill in string
   717  // for possible debugging use.
   718  const (
   719  	maxValSize uintptr = 128
   720  )
   721  
   722  // SetMapIndex sets the element associated with key in the map v to elem.
   723  // It panics if v's Kind is not Map.
   724  // If elem is the zero Value, SetMapIndex deletes the key from the map.
   725  // Otherwise if v holds a nil map, SetMapIndex will panic.
   726  // As in Go, key's elem must be assignable to the map's key type,
   727  // and elem's value must be assignable to the map's elem type.
   728  func (v Value) SetMapIndex(key, elem Value) {
   729  	v.mustBe(Map)
   730  	// v.mustBeExported()
   731  	// key.mustBeExported()
   732  	tt := (*mapType)(unsafe.Pointer(v.typ))
   733  
   734  	if (tt.key == stringType || key.kind() == String) && tt.key == key.typ && tt.elem.size <= maxValSize {
   735  		k := *(*string)(key.ptr)
   736  		if elem.typ == nil {
   737  			mapdelete_faststr(v.typ, v.pointer(), k)
   738  			return
   739  		}
   740  		// elem.mustBeExported()
   741  		elem = elem.assignTo("reflect.Value.SetMapIndex", tt.elem, nil)
   742  		var e unsafe.Pointer
   743  		if elem.flag&flagIndir != 0 {
   744  			e = elem.ptr
   745  		} else {
   746  			e = unsafe.Pointer(&elem.ptr)
   747  		}
   748  		mapassign_faststr(v.typ, v.pointer(), k, e)
   749  		return
   750  	}
   751  
   752  	key = key.assignTo("reflect.Value.SetMapIndex", tt.key, nil)
   753  	var k unsafe.Pointer
   754  	if key.flag&flagIndir != 0 {
   755  		k = key.ptr
   756  	} else {
   757  		k = unsafe.Pointer(&key.ptr)
   758  	}
   759  	if elem.typ == nil {
   760  		mapdelete(v.typ, v.pointer(), k)
   761  		return
   762  	}
   763  	// elem.mustBeExported()
   764  	elem = elem.assignTo("reflect.Value.SetMapIndex", tt.elem, nil)
   765  	var e unsafe.Pointer
   766  	if elem.flag&flagIndir != 0 {
   767  		e = elem.ptr
   768  	} else {
   769  		e = unsafe.Pointer(&elem.ptr)
   770  	}
   771  	mapassign(v.typ, v.pointer(), k, e)
   772  }
   773  
   774  // Int returns v's underlying value, as an int64.
   775  // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
   776  func (v Value) Int() int64 {
   777  	k := v.kind()
   778  	p := v.ptr
   779  	switch k {
   780  	case Int:
   781  		return int64(*(*int)(p))
   782  	case Int8:
   783  		return int64(*(*int8)(p))
   784  	case Int16:
   785  		return int64(*(*int16)(p))
   786  	case Int32:
   787  		return int64(*(*int32)(p))
   788  	case Int64:
   789  		return *(*int64)(p)
   790  	}
   791  	panic(&ValueError{"reflect.Value.Int", v.kind()})
   792  }
   793  
   794  // Uint returns v's underlying value, as a uint64.
   795  // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
   796  func (v Value) Uint() uint64 {
   797  	k := v.kind()
   798  	p := v.ptr
   799  	switch k {
   800  	case Uint:
   801  		return uint64(*(*uint)(p))
   802  	case Uint8:
   803  		return uint64(*(*uint8)(p))
   804  	case Uint16:
   805  		return uint64(*(*uint16)(p))
   806  	case Uint32:
   807  		return uint64(*(*uint32)(p))
   808  	case Uint64:
   809  		return *(*uint64)(p)
   810  	case Uintptr:
   811  		return uint64(*(*uintptr)(p))
   812  	}
   813  	panic(&ValueError{"reflect.Value.Uint", v.kind()})
   814  }
   815  
   816  // Float returns v's underlying value, as a float64.
   817  // It panics if v's Kind is not Float32 or Float64
   818  func (v Value) Float() float64 {
   819  	k := v.kind()
   820  	switch k {
   821  	case Float32:
   822  		return float64(*(*float32)(v.ptr))
   823  	case Float64:
   824  		return *(*float64)(v.ptr)
   825  	}
   826  	panic(&ValueError{"reflect.Value.Float", v.kind()})
   827  }
   828  
   829  // Complex returns v's underlying value, as a complex128.
   830  // It panics if v's Kind is not Complex64 or Complex128
   831  func (v Value) Complex() complex128 {
   832  	k := v.kind()
   833  	switch k {
   834  	case Complex64:
   835  		return complex128(*(*complex64)(v.ptr))
   836  	case Complex128:
   837  		return *(*complex128)(v.ptr)
   838  	}
   839  	panic(&ValueError{"reflect.Value.Complex", v.kind()})
   840  }
   841  
   842  var bytesType = TypeOf(([]byte)(nil)).(*rtype)
   843  
   844  // Bytes returns v's underlying value.
   845  // It panics if v's underlying value is not a slice of bytes or
   846  // an addressable array of bytes.
   847  func (v Value) Bytes() []byte {
   848  	// bytesSlow is split out to keep Bytes inlineable for unnamed []byte.
   849  	if v.typ == bytesType {
   850  		return *(*[]byte)(v.ptr)
   851  	}
   852  	return v.bytesSlow()
   853  }
   854  
   855  func (v Value) bytesSlow() []byte {
   856  	switch v.kind() {
   857  	case Slice:
   858  		if v.typ.Elem().Kind() != Uint8 {
   859  			panic("reflect.Value.Bytes of non-byte slice")
   860  		}
   861  		// Slice is always bigger than a word; assume flagIndir.
   862  		return *(*[]byte)(v.ptr)
   863  		// Unsafe.Slice() not available until 1.17
   864  		// case Array:
   865  		//	if v.typ.Elem().Kind() != Uint8 {
   866  		//		panic("reflect.Value.Bytes of non-byte array")
   867  		//	}
   868  		//	if !v.CanAddr() {
   869  		//		panic("reflect.Value.Bytes of unaddressable byte array")
   870  		//	}
   871  		//	p := (*byte)(v.ptr)
   872  		//	n := int((*arrayType)(unsafe.Pointer(v.typ)).len)
   873  		//	return unsafe.Slice(p, n)
   874  	}
   875  	panic(&ValueError{"reflect.Value.Bytes", v.kind()})
   876  }
   877  
   878  // runes returns v's underlying value.
   879  // It panics if v's underlying value is not a slice of runes (int32s).
   880  func (v Value) runes() []rune {
   881  	v.mustBe(Slice)
   882  	if v.typ.Elem().Kind() != Int32 {
   883  		panic("reflect.Value.Bytes of non-rune slice")
   884  	}
   885  	// Slice is always bigger than a word; assume flagIndir.
   886  	return *(*[]rune)(v.ptr)
   887  }
   888  
   889  // String returns the string v's underlying value, as a string.
   890  // String is a special case because of Go's String method convention.
   891  // Unlike the other getters, it does not panic if v's Kind is not String.
   892  // Instead, it returns a string of the form "<T value>" where T is v's type.
   893  // The fmt package treats Values specially. It does not call their String
   894  // method implicitly but instead prints the concrete values they hold.
   895  func (v Value) String() string {
   896  	// stringNonString is split out to keep String inlineable for string kinds.
   897  	if v.kind() == String {
   898  		return *(*string)(v.ptr)
   899  	}
   900  	return v.stringNonString()
   901  }
   902  
   903  func (v Value) stringNonString() string {
   904  	if v.kind() == Invalid {
   905  		return "<invalid Value>"
   906  	}
   907  	// If you call String on a reflect.Value of other type, it's better to
   908  	// print something than to panic. Useful in debugging.
   909  	return "<" + v.Type().String() + " Value>"
   910  }
   911  
   912  // Len returns v's length.
   913  // It panics if v's Kind is not Array, Chan, Map, Slice, String, or pointer to Array.
   914  func (v Value) Len() int {
   915  	// lenNonSlice is split out to keep Len inlineable for slice kinds.
   916  	if v.kind() == Slice {
   917  		return (*unsafeheader.Slice)(v.ptr).Len
   918  	}
   919  	return v.lenNonSlice()
   920  }
   921  
   922  func (v Value) lenNonSlice() int {
   923  	switch k := v.kind(); k {
   924  	case Array:
   925  		tt := (*arrayType)(unsafe.Pointer(v.typ))
   926  		return int(tt.len)
   927  	case Chan:
   928  		return chanlen(v.pointer())
   929  	case Map:
   930  		return maplen(v.pointer())
   931  	case String:
   932  		// String is bigger than a word; assume flagIndir.
   933  		return (*unsafeheader.String)(v.ptr).Len
   934  	case Ptr:
   935  		if v.typ.Elem().Kind() == Array {
   936  			return v.typ.Elem().Len()
   937  		}
   938  		panic("reflect: call of reflect.Value.Len on ptr to non-array Value")
   939  	}
   940  	panic(&ValueError{"reflect.Value.Len", v.kind()})
   941  }
   942  
   943  // Interface returns v's current value as an interface{}.
   944  // It is equivalent to:
   945  //
   946  //	var i interface{} = (v's underlying value)
   947  //
   948  // It panics if the Value was obtained by accessing
   949  // unexported struct fields.
   950  func (v Value) Interface() (i interface{}) {
   951  	return valueInterface(v, false)
   952  }
   953  
   954  func valueInterface(v Value, safe bool) interface{} {
   955  	if v.flag == 0 {
   956  		panic(&ValueError{"reflect.Value.Interface", Invalid})
   957  	}
   958  	if safe && v.flag&flagRO != 0 {
   959  		// Do not allow access to unexported values via Interface,
   960  		// because they might be pointers that should not be
   961  		// writable or methods or function that should not be callable.
   962  		panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
   963  	}
   964  	if v.flag&flagMethod != 0 {
   965  		panic("can't access methods in reflectlite")
   966  	}
   967  
   968  	if v.kind() == Interface {
   969  		// Special case: return the element inside the interface.
   970  		// Empty interface has one layout, all interfaces with
   971  		// methods have a second layout.
   972  		if v.NumMethod() == 0 {
   973  			return *(*interface{})(v.ptr)
   974  		}
   975  		return *(*interface {
   976  			M()
   977  		})(v.ptr)
   978  	}
   979  
   980  	// TODO: pass safe to packEface so we don't need to copy if safe==true?
   981  	return packEface(v)
   982  }
   983  
   984  // NumMethod returns the number of methods in the value's method set.
   985  //
   986  // For a non-interface type, it returns the number of exported methods.
   987  //
   988  // For an interface type, it returns the number of exported and unexported methods.
   989  func (v Value) NumMethod() int {
   990  	if v.typ == nil {
   991  		panic(&ValueError{"reflect.Value.NumMethod", Invalid})
   992  	}
   993  	if v.flag&flagMethod != 0 {
   994  		return 0
   995  	}
   996  	return v.typ.NumMethod()
   997  }
   998  
   999  // IsNil reports whether its argument v is nil. The argument must be
  1000  // a chan, func, interface, map, pointer, or slice value; if it is
  1001  // not, IsNil panics. Note that IsNil is not always equivalent to a
  1002  // regular comparison with nil in Go. For example, if v was created
  1003  // by calling ValueOf with an uninitialized interface variable i,
  1004  // i==nil will be true but v.IsNil will panic as v will be the zero
  1005  // Value.
  1006  func (v Value) IsNil() bool {
  1007  	k := v.kind()
  1008  	switch k {
  1009  	case Chan, Func, Map, Pointer, UnsafePointer:
  1010  		if v.flag&flagMethod != 0 {
  1011  			return false
  1012  		}
  1013  		ptr := v.ptr
  1014  		if v.flag&flagIndir != 0 {
  1015  			ptr = *(*unsafe.Pointer)(ptr)
  1016  		}
  1017  		return ptr == nil
  1018  	case Interface, Slice:
  1019  		// Both interface and slice are nil if first word is 0.
  1020  		// Both are always bigger than a word; assume flagIndir.
  1021  		return *(*unsafe.Pointer)(v.ptr) == nil
  1022  	}
  1023  	panic(&ValueError{"reflect.Value.IsNil", v.kind()})
  1024  }
  1025  
  1026  // IsValid reports whether v represents a value.
  1027  // It returns false if v is the zero Value.
  1028  // If IsValid returns false, all other methods except String panic.
  1029  // Most functions and methods never return an invalid Value.
  1030  // If one does, its documentation states the conditions explicitly.
  1031  func (v Value) IsValid() bool {
  1032  	return v.flag != 0
  1033  }
  1034  
  1035  // CanAddr reports whether the value's address can be obtained with Addr.
  1036  // Such values are called addressable. A value is addressable if it is
  1037  // an element of a slice, an element of an addressable array,
  1038  // a field of an addressable struct, or the result of dereferencing a pointer.
  1039  // If CanAddr returns false, calling Addr will panic.
  1040  func (v Value) CanAddr() bool {
  1041  	return v.flag&flagAddr != 0
  1042  }
  1043  
  1044  // CanSet reports whether the value of v can be changed.
  1045  // A Value can be changed only if it is addressable and was not
  1046  // obtained by the use of unexported struct fields.
  1047  // If CanSet returns false, calling Set or any type-specific
  1048  // setter (e.g., SetBool, SetInt) will panic.
  1049  func (v Value) CanSet() bool {
  1050  	return v.flag&(flagAddr|flagRO) == flagAddr
  1051  }
  1052  
  1053  // CanConvert reports whether the value v can be converted to type t.
  1054  // If v.CanConvert(t) returns true then v.Convert(t) will not panic.
  1055  func (v Value) CanConvert(t Type) bool {
  1056  	vt := v.Type()
  1057  	if !vt.ConvertibleTo(t) {
  1058  		return false
  1059  	}
  1060  	// Currently the only conversion that is OK in terms of type
  1061  	// but that can panic depending on the value is converting
  1062  	// from slice to pointer-to-array.
  1063  	if vt.Kind() == Slice && t.Kind() == Pointer && t.Elem().Kind() == Array {
  1064  		n := t.Elem().Len()
  1065  		if n > v.Len() {
  1066  			return false
  1067  		}
  1068  	}
  1069  	return true
  1070  }
  1071  
  1072  func (v Value) CanConvertWithInterface(t Type) bool {
  1073  	vt := v.Type()
  1074  	if !vt.ConvertibleToWithInterface(t) {
  1075  		return false
  1076  	}
  1077  	// Currently the only conversion that is OK in terms of type
  1078  	// but that can panic depending on the value is converting
  1079  	// from slice to pointer-to-array.
  1080  	if vt.Kind() == Slice && t.Kind() == Pointer && t.Elem().Kind() == Array {
  1081  		n := t.Elem().Len()
  1082  		if n > v.Len() {
  1083  			return false
  1084  		}
  1085  	}
  1086  	return true
  1087  }
  1088  
  1089  // Convert returns the value v converted to type t.
  1090  // If the usual Go conversion rules do not allow conversion
  1091  // of the value v to type t, or if converting v to type t panics, Convert panics.
  1092  func (v Value) Convert(t Type) Value {
  1093  	if v.flag&flagMethod != 0 {
  1094  		panic("method values not supported in reflectlite")
  1095  	}
  1096  	seen := map[_typePair]struct{}{}
  1097  	op := convertOp(t.common(), v.typ, false, seen)
  1098  	if op == nil {
  1099  		panic("reflect.Value.Convert: value of type " + v.typ.String() + " cannot be converted to type " + t.String())
  1100  	}
  1101  	return op(v, t)
  1102  }
  1103  
  1104  func (v Value) ConvertWithInterface(t Type) Value {
  1105  	if v.flag&flagMethod != 0 {
  1106  		panic("method values not supported in reflectlite")
  1107  	}
  1108  	seen := map[_typePair]struct{}{}
  1109  	op := convertOp(t.common(), v.typ, true, seen)
  1110  	if op == nil {
  1111  		panic("reflect.Value.Convert: value of type " + v.typ.String() + " cannot be converted to type " + t.String())
  1112  	}
  1113  	return op(v, t)
  1114  }
  1115  
  1116  // convertOp returns the function to convert a value of type src
  1117  // to a value of type dst. If the conversion is illegal, convertOp returns nil.
  1118  func convertOp(dst, src *rtype, allowInterface bool, seen map[_typePair]struct{}) func(Value, Type) Value {
  1119  	switch src.Kind() {
  1120  	case Int, Int8, Int16, Int32, Int64:
  1121  		switch dst.Kind() {
  1122  		case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
  1123  			return cvtInt
  1124  		case Float32, Float64:
  1125  			return cvtIntFloat
  1126  		case String:
  1127  			return cvtIntString
  1128  		}
  1129  
  1130  	case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
  1131  		switch dst.Kind() {
  1132  		case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
  1133  			return cvtUint
  1134  		case Float32, Float64:
  1135  			return cvtUintFloat
  1136  		case String:
  1137  			return cvtUintString
  1138  		}
  1139  
  1140  	case Float32, Float64:
  1141  		switch dst.Kind() {
  1142  		case Int, Int8, Int16, Int32, Int64:
  1143  			return cvtFloatInt
  1144  		case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
  1145  			return cvtFloatUint
  1146  		case Float32, Float64:
  1147  			return cvtFloat
  1148  		}
  1149  
  1150  	case Complex64, Complex128:
  1151  		switch dst.Kind() {
  1152  		case Complex64, Complex128:
  1153  			return cvtComplex
  1154  		}
  1155  
  1156  	case String:
  1157  		if dst.Kind() == Slice && dst.Elem().PkgPath() == "" {
  1158  			switch dst.Elem().Kind() {
  1159  			case Uint8:
  1160  				return cvtStringBytes
  1161  			case Int32:
  1162  				return cvtStringRunes
  1163  			}
  1164  		}
  1165  
  1166  	case Slice:
  1167  		if dst.Kind() == String && src.Elem().PkgPath() == "" {
  1168  			switch src.Elem().Kind() {
  1169  			case Uint8:
  1170  				return cvtBytesString
  1171  			case Int32:
  1172  				return cvtRunesString
  1173  			}
  1174  		}
  1175  		// "x is a slice, T is a pointer-to-array type,
  1176  		// and the slice and array types have identical element types."
  1177  		if dst.Kind() == Pointer && dst.Elem().Kind() == Array && src.Elem() == dst.Elem().Elem() {
  1178  			return cvtSliceArrayPtr
  1179  		}
  1180  
  1181  	case Chan:
  1182  		if dst.Kind() == Chan && specialChannelAssignability(dst, src, seen) {
  1183  			return cvtDirect
  1184  		}
  1185  	}
  1186  
  1187  	// dst and src have same underlying type.
  1188  	if haveIdenticalUnderlyingType(dst, src, false, allowInterface, seen) {
  1189  		return cvtDirect
  1190  	}
  1191  
  1192  	// dst and src are non-defined pointer types with same underlying base type.
  1193  	if dst.Kind() == Pointer && dst.Name() == "" &&
  1194  		src.Kind() == Pointer && src.Name() == "" &&
  1195  		haveIdenticalUnderlyingType(dst.Elem().common(), src.Elem().common(), false, allowInterface, seen) {
  1196  		return cvtDirect
  1197  	}
  1198  
  1199  	if implements(dst, src, seen) {
  1200  		if src.Kind() == Interface {
  1201  			return cvtI2I
  1202  		}
  1203  		return cvtT2I
  1204  	}
  1205  
  1206  	return nil
  1207  }
  1208  
  1209  func haveIdenticalType(T, V Type, cmpTags, allowInterface bool, seen map[_typePair]struct{}) bool {
  1210  	if cmpTags {
  1211  		return T == V
  1212  	}
  1213  
  1214  	if T.Name() != V.Name() || T.Kind() != V.Kind() || T.PkgPath() != V.PkgPath() {
  1215  		return false
  1216  	}
  1217  
  1218  	return haveIdenticalUnderlyingType(T.common(), V.common(), false, allowInterface, seen)
  1219  }
  1220  
  1221  func haveIdenticalUnderlyingType(T, V *rtype, cmpTags, allowInterface bool, seen map[_typePair]struct{}) bool {
  1222  	if T == V {
  1223  		return true
  1224  	}
  1225  	tp := _typePair{T, V}
  1226  	if _, ok := seen[tp]; ok {
  1227  		return true
  1228  	}
  1229  	seen[tp] = struct{}{}
  1230  
  1231  	kind := T.Kind()
  1232  	if kind != V.Kind() {
  1233  		return false
  1234  	}
  1235  
  1236  	// Non-composite types of equal kind have same underlying type
  1237  	// (the predefined instance of the type).
  1238  	if Bool <= kind && kind <= Complex128 || kind == String || kind == UnsafePointer {
  1239  		return true
  1240  	}
  1241  
  1242  	// Composite types.
  1243  	switch kind {
  1244  	case Array:
  1245  		return T.Len() == V.Len() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags, allowInterface, seen)
  1246  
  1247  	case Chan:
  1248  		return V.ChanDir() == T.ChanDir() && haveIdenticalType(T.Elem(), V.Elem(), cmpTags, allowInterface, seen)
  1249  
  1250  	case Func:
  1251  		t := (*funcType)(unsafe.Pointer(T))
  1252  		v := (*funcType)(unsafe.Pointer(V))
  1253  		if t.outCount != v.outCount || t.inCount != v.inCount {
  1254  			return false
  1255  		}
  1256  		for i := 0; i < t.NumIn(); i++ {
  1257  			if !haveIdenticalType(t.In(i), v.In(i), cmpTags, allowInterface, seen) {
  1258  				return false
  1259  			}
  1260  		}
  1261  		for i := 0; i < t.NumOut(); i++ {
  1262  			if !haveIdenticalType(t.Out(i), v.Out(i), cmpTags, allowInterface, seen) {
  1263  				return false
  1264  			}
  1265  		}
  1266  		return true
  1267  
  1268  	case Interface:
  1269  		t := (*interfaceType)(unsafe.Pointer(T))
  1270  		v := (*interfaceType)(unsafe.Pointer(V))
  1271  		if len(t.methods) == 0 && len(v.methods) == 0 {
  1272  			return true
  1273  		}
  1274  		if allowInterface && implements(V, T, seen) {
  1275  			return true
  1276  		}
  1277  		return false
  1278  	case Map:
  1279  		return haveIdenticalType(T.Key(), V.Key(), cmpTags, allowInterface, seen) && haveIdenticalType(T.Elem(), V.Elem(), cmpTags, allowInterface, seen)
  1280  
  1281  	case Pointer, Slice:
  1282  		return haveIdenticalType(T.Elem(), V.Elem(), cmpTags, allowInterface, seen)
  1283  
  1284  	case Struct:
  1285  		t := (*structType)(unsafe.Pointer(T))
  1286  		v := (*structType)(unsafe.Pointer(V))
  1287  		if len(t.fields) != len(v.fields) {
  1288  			return false
  1289  		}
  1290  		if t.pkgPath.name() != v.pkgPath.name() {
  1291  			return false
  1292  		}
  1293  		for i := range t.fields {
  1294  			tf := &t.fields[i]
  1295  			vf := &v.fields[i]
  1296  			if tf.name.name() != vf.name.name() {
  1297  				return false
  1298  			}
  1299  			if !haveIdenticalType(tf.typ, vf.typ, cmpTags, allowInterface, seen) {
  1300  				return false
  1301  			}
  1302  			if cmpTags && tf.name.tag() != vf.name.tag() {
  1303  				return false
  1304  			}
  1305  			if tf.offset != vf.offset {
  1306  				return false
  1307  			}
  1308  			if tf.embedded() != vf.embedded() {
  1309  				return false
  1310  			}
  1311  		}
  1312  		return true
  1313  	}
  1314  
  1315  	return false
  1316  }
  1317  
  1318  // MakeMapWithSize creates a new map with the specified type
  1319  // and initial space for approximately n elements.
  1320  func MakeMapWithSize(typ Type, n int) Value {
  1321  	if typ.Kind() != Map {
  1322  		panic("reflect.MakeMapWithSize of non-map type")
  1323  	}
  1324  	t := typ.(*rtype)
  1325  	m := makemap(t, n)
  1326  	return Value{t, m, flag(Map)}
  1327  }
  1328  
  1329  // copyVal returns a Value containing the map key or value at ptr,
  1330  // allocating a new variable as needed.
  1331  func copyVal(typ *rtype, fl flag, ptr unsafe.Pointer) Value {
  1332  	if ifaceIndir(typ) {
  1333  		// Copy result so future changes to the map
  1334  		// won't change the underlying value.
  1335  		c := unsafe_New(typ)
  1336  		typedmemmove(typ, c, ptr)
  1337  		return Value{typ, c, fl | flagIndir}
  1338  	}
  1339  	return Value{typ, *(*unsafe.Pointer)(ptr), fl}
  1340  }
  1341  
  1342  // MapIndex returns the value associated with key in the map v.
  1343  // It panics if v's Kind is not Map.
  1344  // It returns the zero Value if key is not found in the map or if v represents a nil map.
  1345  // As in Go, the key's value must be assignable to the map's key type.
  1346  func (v Value) MapIndex(key Value) Value {
  1347  	v.mustBe(Map)
  1348  	tt := (*mapType)(unsafe.Pointer(v.typ))
  1349  
  1350  	// Do not require key to be exported, so that DeepEqual
  1351  	// and other programs can use all the keys returned by
  1352  	// MapKeys as arguments to MapIndex. If either the map
  1353  	// or the key is unexported, though, the result will be
  1354  	// considered unexported. This is consistent with the
  1355  	// behavior for structs, which allow read but not write
  1356  	// of unexported fields.
  1357  
  1358  	var e unsafe.Pointer
  1359  	if (tt.key == stringType || key.kind() == String) && tt.key == key.typ && tt.elem.size <= maxValSize {
  1360  		k := *(*string)(key.ptr)
  1361  		e = mapaccess_faststr(v.typ, v.pointer(), k)
  1362  	} else {
  1363  		key = key.assignTo("reflect.Value.MapIndex", tt.key, nil)
  1364  		var k unsafe.Pointer
  1365  		if key.flag&flagIndir != 0 {
  1366  			k = key.ptr
  1367  		} else {
  1368  			k = unsafe.Pointer(&key.ptr)
  1369  		}
  1370  		e = mapaccess(v.typ, v.pointer(), k)
  1371  	}
  1372  	if e == nil {
  1373  		return Value{}
  1374  	}
  1375  	typ := tt.elem
  1376  	fl := (v.flag | key.flag).ro()
  1377  	fl |= flag(typ.Kind())
  1378  	return copyVal(typ, fl, e)
  1379  }
  1380  
  1381  // MapKeys returns a slice containing all the keys present in the map,
  1382  // in unspecified order.
  1383  // It panics if v's Kind is not Map.
  1384  // It returns an empty slice if v represents a nil map.
  1385  func (v Value) MapKeys() []Value {
  1386  	v.mustBe(Map)
  1387  	tt := (*mapType)(unsafe.Pointer(v.typ))
  1388  	keyType := tt.key
  1389  
  1390  	fl := v.flag.ro() | flag(keyType.Kind())
  1391  
  1392  	m := v.pointer()
  1393  	mlen := int(0)
  1394  	if m != nil {
  1395  		mlen = maplen(m)
  1396  	}
  1397  	var it hiter
  1398  	mapiterinit(v.typ, m, &it)
  1399  	a := make([]Value, mlen)
  1400  	var i int
  1401  	for i = 0; i < len(a); i++ {
  1402  		key := mapiterkey(&it)
  1403  		if key == nil {
  1404  			// Someone deleted an entry from the map since we
  1405  			// called maplen above. It's a data race, but nothing
  1406  			// we can do about it.
  1407  			break
  1408  		}
  1409  		a[i] = copyVal(keyType, fl, key)
  1410  		mapiternext(&it)
  1411  	}
  1412  	return a[:i]
  1413  }
  1414  
  1415  // makeInt returns a Value of type t equal to bits (possibly truncated),
  1416  // where t is a signed or unsigned int type.
  1417  func makeInt(f flag, bits uint64, t Type) Value {
  1418  	typ := t.common()
  1419  	ptr := unsafe_New(typ)
  1420  	switch typ.size {
  1421  	case 1:
  1422  		*(*uint8)(ptr) = uint8(bits)
  1423  	case 2:
  1424  		*(*uint16)(ptr) = uint16(bits)
  1425  	case 4:
  1426  		*(*uint32)(ptr) = uint32(bits)
  1427  	case 8:
  1428  		*(*uint64)(ptr) = bits
  1429  	}
  1430  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
  1431  }
  1432  
  1433  // makeFloat returns a Value of type t equal to v (possibly truncated to float32),
  1434  // where t is a float32 or float64 type.
  1435  func makeFloat(f flag, v float64, t Type) Value {
  1436  	typ := t.common()
  1437  	ptr := unsafe_New(typ)
  1438  	switch typ.size {
  1439  	case 4:
  1440  		*(*float32)(ptr) = float32(v)
  1441  	case 8:
  1442  		*(*float64)(ptr) = v
  1443  	}
  1444  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
  1445  }
  1446  
  1447  // makeFloat returns a Value of type t equal to v, where t is a float32 type.
  1448  func makeFloat32(f flag, v float32, t Type) Value {
  1449  	typ := t.common()
  1450  	ptr := unsafe_New(typ)
  1451  	*(*float32)(ptr) = v
  1452  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
  1453  }
  1454  
  1455  // makeComplex returns a Value of type t equal to v (possibly truncated to complex64),
  1456  // where t is a complex64 or complex128 type.
  1457  func makeComplex(f flag, v complex128, t Type) Value {
  1458  	typ := t.common()
  1459  	ptr := unsafe_New(typ)
  1460  	switch typ.size {
  1461  	case 8:
  1462  		*(*complex64)(ptr) = complex64(v)
  1463  	case 16:
  1464  		*(*complex128)(ptr) = v
  1465  	}
  1466  	return Value{typ, ptr, f | flagIndir | flag(typ.Kind())}
  1467  }
  1468  
  1469  func makeString(f flag, v string, t Type) Value {
  1470  	ret := New(t).Elem()
  1471  	ret.SetString(v)
  1472  	ret.flag = ret.flag&^flagAddr | f
  1473  	return ret
  1474  }
  1475  
  1476  func makeBytes(f flag, v []byte, t Type) Value {
  1477  	ret := New(t).Elem()
  1478  	ret.SetBytes(v)
  1479  	ret.flag = ret.flag&^flagAddr | f
  1480  	return ret
  1481  }
  1482  
  1483  func makeRunes(f flag, v []rune, t Type) Value {
  1484  	ret := New(t).Elem()
  1485  	ret.setRunes(v)
  1486  	ret.flag = ret.flag&^flagAddr | f
  1487  	return ret
  1488  }
  1489  
  1490  // These conversion functions are returned by convertOp
  1491  // for classes of conversions. For example, the first function, cvtInt,
  1492  // takes any value v of signed int type and returns the value converted
  1493  // to type t, where t is any signed or unsigned int type.
  1494  
  1495  // convertOp: intXX -> [u]intXX
  1496  func cvtInt(v Value, t Type) Value {
  1497  	return makeInt(v.flag.ro(), uint64(v.Int()), t)
  1498  }
  1499  
  1500  // convertOp: uintXX -> [u]intXX
  1501  func cvtUint(v Value, t Type) Value {
  1502  	return makeInt(v.flag.ro(), v.Uint(), t)
  1503  }
  1504  
  1505  // convertOp: floatXX -> intXX
  1506  func cvtFloatInt(v Value, t Type) Value {
  1507  	return makeInt(v.flag.ro(), uint64(int64(v.Float())), t)
  1508  }
  1509  
  1510  // convertOp: floatXX -> uintXX
  1511  func cvtFloatUint(v Value, t Type) Value {
  1512  	return makeInt(v.flag.ro(), uint64(v.Float()), t)
  1513  }
  1514  
  1515  // convertOp: intXX -> floatXX
  1516  func cvtIntFloat(v Value, t Type) Value {
  1517  	return makeFloat(v.flag.ro(), float64(v.Int()), t)
  1518  }
  1519  
  1520  // convertOp: uintXX -> floatXX
  1521  func cvtUintFloat(v Value, t Type) Value {
  1522  	return makeFloat(v.flag.ro(), float64(v.Uint()), t)
  1523  }
  1524  
  1525  // convertOp: floatXX -> floatXX
  1526  func cvtFloat(v Value, t Type) Value {
  1527  	if v.Type().Kind() == Float32 && t.Kind() == Float32 {
  1528  		// Don't do any conversion if both types have underlying type float32.
  1529  		// This avoids converting to float64 and back, which will
  1530  		// convert a signaling NaN to a quiet NaN. See issue 36400.
  1531  		return makeFloat32(v.flag.ro(), *(*float32)(v.ptr), t)
  1532  	}
  1533  	return makeFloat(v.flag.ro(), v.Float(), t)
  1534  }
  1535  
  1536  // convertOp: complexXX -> complexXX
  1537  func cvtComplex(v Value, t Type) Value {
  1538  	return makeComplex(v.flag.ro(), v.Complex(), t)
  1539  }
  1540  
  1541  // convertOp: intXX -> string
  1542  func cvtIntString(v Value, t Type) Value {
  1543  	s := "\uFFFD"
  1544  	if x := v.Int(); int64(rune(x)) == x {
  1545  		s = string(rune(x))
  1546  	}
  1547  	return makeString(v.flag.ro(), s, t)
  1548  }
  1549  
  1550  // convertOp: uintXX -> string
  1551  func cvtUintString(v Value, t Type) Value {
  1552  	s := "\uFFFD"
  1553  	if x := v.Uint(); uint64(rune(x)) == x {
  1554  		s = string(rune(x))
  1555  	}
  1556  	return makeString(v.flag.ro(), s, t)
  1557  }
  1558  
  1559  // convertOp: []byte -> string
  1560  func cvtBytesString(v Value, t Type) Value {
  1561  	return makeString(v.flag.ro(), string(v.Bytes()), t)
  1562  }
  1563  
  1564  // convertOp: string -> []byte
  1565  func cvtStringBytes(v Value, t Type) Value {
  1566  	return makeBytes(v.flag.ro(), []byte(v.String()), t)
  1567  }
  1568  
  1569  // convertOp: []rune -> string
  1570  func cvtRunesString(v Value, t Type) Value {
  1571  	return makeString(v.flag.ro(), string(v.runes()), t)
  1572  }
  1573  
  1574  // convertOp: string -> []rune
  1575  func cvtStringRunes(v Value, t Type) Value {
  1576  	return makeRunes(v.flag.ro(), []rune(v.String()), t)
  1577  }
  1578  
  1579  // convertOp: []T -> *[N]T
  1580  func cvtSliceArrayPtr(v Value, t Type) Value {
  1581  	n := t.Elem().Len()
  1582  	if n > v.Len() {
  1583  		panic("reflect: cannot convert slice with length " + itoa.Itoa(v.Len()) + " to pointer to array with length " + itoa.Itoa(n))
  1584  	}
  1585  	h := (*unsafeheader.Slice)(v.ptr)
  1586  	return Value{t.common(), h.Data, v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Pointer)}
  1587  }
  1588  
  1589  // convertOp: direct copy
  1590  func cvtDirect(v Value, typ Type) Value {
  1591  	f := v.flag
  1592  	t := typ.common()
  1593  	ptr := v.ptr
  1594  	if f&flagAddr != 0 {
  1595  		// indirect, mutable word - make a copy
  1596  		c := unsafe_New(t)
  1597  		typedmemmove(t, c, ptr)
  1598  		ptr = c
  1599  		f &^= flagAddr
  1600  	}
  1601  	return Value{t, ptr, v.flag.ro() | f} // v.flag.ro()|f == f?
  1602  }
  1603  
  1604  // convertOp: concrete -> interface
  1605  func cvtT2I(v Value, typ Type) Value {
  1606  	target := unsafe_New(typ.common())
  1607  	x := valueInterface(v, false)
  1608  	if typ.NumMethod() == 0 {
  1609  		*(*interface{})(target) = x
  1610  	} else {
  1611  		ifaceE2I(typ.(*rtype), x, target)
  1612  	}
  1613  	return Value{typ.common(), target, v.flag.ro() | flagIndir | flag(Interface)}
  1614  }
  1615  
  1616  // convertOp: interface -> interface
  1617  func cvtI2I(v Value, typ Type) Value {
  1618  	if v.IsNil() {
  1619  		ret := Zero(typ)
  1620  		ret.flag |= v.flag.ro()
  1621  		return ret
  1622  	}
  1623  	return cvtT2I(v.Elem(), typ)
  1624  }
  1625  
  1626  //go:linkname ifaceE2I reflect.ifaceE2I
  1627  func ifaceE2I(t *rtype, src interface{}, dst unsafe.Pointer)
  1628  
  1629  // typedmemmove copies a value of type t to dst from src.
  1630  //
  1631  //go:noescape
  1632  //go:linkname typedmemmove runtime.typedmemmove
  1633  func typedmemmove(t *rtype, dst, src unsafe.Pointer)
  1634  
  1635  // typedmemclr zeros the value at ptr of type t.
  1636  //
  1637  //go:noescape
  1638  //go:linkname typedmemclr reflect.typedmemclr
  1639  func typedmemclr(t *rtype, ptr unsafe.Pointer)
  1640  
  1641  //go:linkname unsafe_New reflect.unsafe_New
  1642  func unsafe_New(*rtype) unsafe.Pointer
  1643  
  1644  // ifaceIndir reports whether t is stored indirectly in an interface value.
  1645  func ifaceIndir(t *rtype) bool {
  1646  	return t.kind&kindDirectIface == 0
  1647  }
  1648  
  1649  //go:linkname chanlen reflect.chanlen
  1650  func chanlen(ch unsafe.Pointer) int
  1651  
  1652  // hiter's structure matches runtime.hiter's structure.
  1653  // Having a clone here allows us to embed a map iterator
  1654  // inside type MapIter so that MapIters can be re-used
  1655  // without doing any allocations.
  1656  type hiter struct {
  1657  	key         unsafe.Pointer
  1658  	elem        unsafe.Pointer
  1659  	t           unsafe.Pointer
  1660  	h           unsafe.Pointer
  1661  	buckets     unsafe.Pointer
  1662  	bptr        unsafe.Pointer
  1663  	overflow    *[]unsafe.Pointer
  1664  	oldoverflow *[]unsafe.Pointer
  1665  	startBucket uintptr
  1666  	offset      uint8
  1667  	wrapped     bool
  1668  	B           uint8
  1669  	i           uint8
  1670  	bucket      uintptr
  1671  	checkBucket uintptr
  1672  }
  1673  
  1674  func (h *hiter) initialized() bool {
  1675  	return h.t != nil
  1676  }
  1677  
  1678  //go:noescape
  1679  //go:linkname maplen reflect.maplen
  1680  func maplen(m unsafe.Pointer) int
  1681  
  1682  //go:linkname makemap reflect.makemap
  1683  func makemap(t *rtype, cap int) (m unsafe.Pointer)
  1684  
  1685  //go:noescape
  1686  //go:linkname mapaccess reflect.mapaccess
  1687  func mapaccess(t *rtype, m unsafe.Pointer, key unsafe.Pointer) (val unsafe.Pointer)
  1688  
  1689  //go:noescape
  1690  //go:linkname mapaccess_faststr reflect.mapaccess_faststr
  1691  func mapaccess_faststr(t *rtype, m unsafe.Pointer, key string) (val unsafe.Pointer)
  1692  
  1693  //go:noescape
  1694  //go:linkname mapassign reflect.mapassign
  1695  func mapassign(t *rtype, m unsafe.Pointer, key, val unsafe.Pointer)
  1696  
  1697  //go:noescape
  1698  //go:linkname mapassign_faststr reflect.mapassign_faststr
  1699  func mapassign_faststr(t *rtype, m unsafe.Pointer, key string, val unsafe.Pointer)
  1700  
  1701  //go:noescape
  1702  //go:linkname mapdelete reflect.mapdelete
  1703  func mapdelete(t *rtype, m unsafe.Pointer, key unsafe.Pointer)
  1704  
  1705  //go:noescape
  1706  //go:linkname mapdelete_faststr reflect.mapdelete_faststr
  1707  func mapdelete_faststr(t *rtype, m unsafe.Pointer, key string)
  1708  
  1709  //go:noescape
  1710  //go:linkname mapiterinit reflect.mapiterinit
  1711  func mapiterinit(t *rtype, m unsafe.Pointer, it *hiter)
  1712  
  1713  //go:noescape
  1714  //go:linkname mapiterkey reflect.mapiterkey
  1715  func mapiterkey(it *hiter) (key unsafe.Pointer)
  1716  
  1717  //go:noescape
  1718  //go:linkname mapiterelem reflect.mapiterelem
  1719  func mapiterelem(it *hiter) (elem unsafe.Pointer)
  1720  
  1721  //go:noescape
  1722  //go:linkname mapiternext reflect.mapiternext
  1723  func mapiternext(it *hiter)
  1724  
  1725  //go:linkname verifyNotInHeapPtr reflect.verifyNotInHeapPtr
  1726  func verifyNotInHeapPtr(p uintptr) bool
  1727  
  1728  // Dummy annotation marking that the value x escapes,
  1729  // for use in cases where the reflect code is so clever that
  1730  // the compiler cannot follow.
  1731  func escapes(x interface{}) {
  1732  	if dummy.b {
  1733  		dummy.x = x
  1734  	}
  1735  }
  1736  
  1737  var dummy struct {
  1738  	b bool
  1739  	x interface{}
  1740  }