Debian Codegraph @

src/reflect/value.go

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