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.
14 const bigEndian
= false // can be smarter if we find a big-endian machine
15 const ptrSize
= unsafe
.Sizeof((*byte)(nil))
16 const cannotSet
= "cannot set value obtained from unexported struct field"
18 // TODO: This will have to go away when
19 // the new gc goes in.
20 func memmove(adst
, asrc unsafe
.Pointer
, n
uintptr) {
24 case src
< dst
&& src
+n
> dst
:
26 // careful: i is unsigned
29 *(*byte)(unsafe
.Pointer(dst
+ i
)) = *(*byte)(unsafe
.Pointer(src
+ i
))
31 case (n|src|dst
)&(ptrSize
-1) != 0:
33 for i
:= uintptr(0); i
< n
; i
++ {
34 *(*byte)(unsafe
.Pointer(dst
+ i
)) = *(*byte)(unsafe
.Pointer(src
+ i
))
38 for i
:= uintptr(0); i
< n
; i
+= ptrSize
{
39 *(*uintptr)(unsafe
.Pointer(dst
+ i
)) = *(*uintptr)(unsafe
.Pointer(src
+ i
))
44 // Value is the reflection interface to a Go value.
46 // Not all methods apply to all kinds of values. Restrictions,
47 // if any, are noted in the documentation for each method.
48 // Use the Kind method to find out the kind of value before
49 // calling kind-specific methods. Calling a method
50 // inappropriate to the kind of type causes a run time panic.
52 // The zero Value represents no value.
53 // Its IsValid method returns false, its Kind method returns Invalid,
54 // its String method returns "<invalid Value>", and all other methods panic.
55 // Most functions and methods never return an invalid value.
56 // If one does, its documentation states the conditions explicitly.
58 // A Value can be used concurrently by multiple goroutines provided that
59 // the underlying Go value can be used concurrently for the equivalent
62 // typ holds the type of the value represented by a Value.
65 // val holds the 1-word representation of the value.
66 // If flag's flagIndir bit is set, then val is a pointer to the data.
67 // Otherwise val is a word holding the actual data.
68 // When the data is smaller than a word, it begins at
69 // the first byte (in the memory address sense) of val.
70 // We use unsafe.Pointer so that the garbage collector
71 // knows that val could be a pointer.
74 // flag holds metadata about the value.
75 // The lowest bits are flag bits:
76 // - flagRO: obtained via unexported field, so read-only
77 // - flagIndir: val holds a pointer to the data
78 // - flagAddr: v.CanAddr is true (implies flagIndir)
79 // - flagMethod: v is a method value.
80 // The next five bits give the Kind of the value.
81 // This repeats typ.Kind() except for method values.
82 // The remaining 23+ bits give a method number for method values.
83 // If flag.kind() != Func, code can assume that flagMethod is unset.
84 // If typ.size > ptrSize, code can assume that flagIndir is set.
87 // A method value represents a curried method invocation
88 // like r.Read for some receiver r. The typ+val+flag bits describe
89 // the receiver r, but the flag's Kind bits say Func (methods are
90 // functions), and the top bits of the flag give the method number
91 // in r's type's method table.
97 flagRO flag
= 1 << iota
102 flagKindWidth
= 5 // there are 27 kinds
103 flagKindMask flag
= 1<<flagKindWidth
- 1
104 flagMethodShift
= flagKindShift
+ flagKindWidth
107 func (f flag
) kind() Kind
{
108 return Kind((f
>> flagKindShift
) & flagKindMask
)
111 // A ValueError occurs when a Value method is invoked on
112 // a Value that does not support it. Such cases are documented
113 // in the description of each method.
114 type ValueError
struct {
119 func (e
*ValueError
) Error() string {
121 return "reflect: call of " + e
.Method
+ " on zero Value"
123 return "reflect: call of " + e
.Method
+ " on " + e
.Kind
.String() + " Value"
126 // methodName returns the name of the calling method,
127 // assumed to be two stack frames above.
128 func methodName() string {
129 pc
, _
, _
, _
:= runtime
.Caller(2)
130 f
:= runtime
.FuncForPC(pc
)
132 return "unknown method"
137 // An iword is the word that would be stored in an
138 // interface to represent a given value v. Specifically, if v is
139 // bigger than a pointer, its word is a pointer to v's data.
140 // Otherwise, its word holds the data stored
141 // in its leading bytes (so is not a pointer).
142 // Because the value sometimes holds a pointer, we use
143 // unsafe.Pointer to represent it, so that if iword appears
144 // in a struct, the garbage collector knows that might be
146 type iword unsafe
.Pointer
148 func (v Value
) iword() iword
{
149 if v
.flag
&flagIndir
!= 0 && (v
.kind() == Ptr || v
.kind() == UnsafePointer
) {
150 // Have indirect but want direct word.
151 return loadIword(v
.val
, v
.typ
.size
)
156 // loadIword loads n bytes at p from memory into an iword.
157 func loadIword(p unsafe
.Pointer
, n
uintptr) iword
{
158 // Run the copy ourselves instead of calling memmove
159 // to avoid moving w to the heap.
163 panic("reflect: internal error: loadIword of " + strconv
.Itoa(int(n
)) + "-byte value")
166 *(*uint8)(unsafe
.Pointer(&w
)) = *(*uint8)(p
)
168 *(*uint16)(unsafe
.Pointer(&w
)) = *(*uint16)(p
)
170 *(*[3]byte)(unsafe
.Pointer(&w
)) = *(*[3]byte)(p
)
172 *(*uint32)(unsafe
.Pointer(&w
)) = *(*uint32)(p
)
174 *(*[5]byte)(unsafe
.Pointer(&w
)) = *(*[5]byte)(p
)
176 *(*[6]byte)(unsafe
.Pointer(&w
)) = *(*[6]byte)(p
)
178 *(*[7]byte)(unsafe
.Pointer(&w
)) = *(*[7]byte)(p
)
180 *(*uint64)(unsafe
.Pointer(&w
)) = *(*uint64)(p
)
185 // storeIword stores n bytes from w into p.
186 func storeIword(p unsafe
.Pointer
, w iword
, n
uintptr) {
187 // Run the copy ourselves instead of calling memmove
188 // to avoid moving w to the heap.
191 panic("reflect: internal error: storeIword of " + strconv
.Itoa(int(n
)) + "-byte value")
194 *(*uint8)(p
) = *(*uint8)(unsafe
.Pointer(&w
))
196 *(*uint16)(p
) = *(*uint16)(unsafe
.Pointer(&w
))
198 *(*[3]byte)(p
) = *(*[3]byte)(unsafe
.Pointer(&w
))
200 *(*uint32)(p
) = *(*uint32)(unsafe
.Pointer(&w
))
202 *(*[5]byte)(p
) = *(*[5]byte)(unsafe
.Pointer(&w
))
204 *(*[6]byte)(p
) = *(*[6]byte)(unsafe
.Pointer(&w
))
206 *(*[7]byte)(p
) = *(*[7]byte)(unsafe
.Pointer(&w
))
208 *(*uint64)(p
) = *(*uint64)(unsafe
.Pointer(&w
))
212 // emptyInterface is the header for an interface{} value.
213 type emptyInterface
struct {
218 // nonEmptyInterface is the header for a interface value with methods.
219 type nonEmptyInterface
struct {
220 // see ../runtime/iface.c:/Itab
222 typ
*rtype
// dynamic concrete type
223 fun
[100000]unsafe
.Pointer
// method table
228 // mustBe panics if f's kind is not expected.
229 // Making this a method on flag instead of on Value
230 // (and embedding flag in Value) means that we can write
231 // the very clear v.mustBe(Bool) and have it compile into
232 // v.flag.mustBe(Bool), which will only bother to copy the
233 // single important word for the receiver.
234 func (f flag
) mustBe(expected Kind
) {
237 panic(&ValueError
{methodName(), k
})
241 // mustBeExported panics if f records that the value was obtained using
242 // an unexported field.
243 func (f flag
) mustBeExported() {
245 panic(&ValueError
{methodName(), 0})
248 panic(methodName() + " using value obtained using unexported field")
252 // mustBeAssignable panics if f records that the value is not assignable,
253 // which is to say that either it was obtained using an unexported field
254 // or it is not addressable.
255 func (f flag
) mustBeAssignable() {
257 panic(&ValueError
{methodName(), Invalid
})
259 // Assignable if addressable and not read-only.
261 panic(methodName() + " using value obtained using unexported field")
264 panic(methodName() + " using unaddressable value")
268 // Addr returns a pointer value representing the address of v.
269 // It panics if CanAddr() returns false.
270 // Addr is typically used to obtain a pointer to a struct field
271 // or slice element in order to call a method that requires a
273 func (v Value
) Addr() Value
{
274 if v
.flag
&flagAddr
== 0 {
275 panic("reflect.Value.Addr of unaddressable value")
277 return Value
{v
.typ
.ptrTo(), v
.val
, (v
.flag
& flagRO
) |
flag(Ptr
)<<flagKindShift
}
280 // Bool returns v's underlying value.
281 // It panics if v's kind is not Bool.
282 func (v Value
) Bool() bool {
284 if v
.flag
&flagIndir
!= 0 {
285 return *(*bool)(v
.val
)
287 return *(*bool)(unsafe
.Pointer(&v
.val
))
290 // Bytes returns v's underlying value.
291 // It panics if v's underlying value is not a slice of bytes.
292 func (v Value
) Bytes() []byte {
294 if v
.typ
.Elem().Kind() != Uint8
{
295 panic("reflect.Value.Bytes of non-byte slice")
297 // Slice is always bigger than a word; assume flagIndir.
298 return *(*[]byte)(v
.val
)
301 // runes returns v's underlying value.
302 // It panics if v's underlying value is not a slice of runes (int32s).
303 func (v Value
) runes() []rune
{
305 if v
.typ
.Elem().Kind() != Int32
{
306 panic("reflect.Value.Bytes of non-rune slice")
308 // Slice is always bigger than a word; assume flagIndir.
309 return *(*[]rune
)(v
.val
)
312 // CanAddr returns true if the value's address can be obtained with Addr.
313 // Such values are called addressable. A value is addressable if it is
314 // an element of a slice, an element of an addressable array,
315 // a field of an addressable struct, or the result of dereferencing a pointer.
316 // If CanAddr returns false, calling Addr will panic.
317 func (v Value
) CanAddr() bool {
318 return v
.flag
&flagAddr
!= 0
321 // CanSet returns true if the value of v can be changed.
322 // A Value can be changed only if it is addressable and was not
323 // obtained by the use of unexported struct fields.
324 // If CanSet returns false, calling Set or any type-specific
325 // setter (e.g., SetBool, SetInt64) will panic.
326 func (v Value
) CanSet() bool {
327 return v
.flag
&(flagAddr|flagRO
) == flagAddr
330 // Call calls the function v with the input arguments in.
331 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).
332 // Call panics if v's Kind is not Func.
333 // It returns the output results as Values.
334 // As in Go, each input argument must be assignable to the
335 // type of the function's corresponding input parameter.
336 // If v is a variadic function, Call creates the variadic slice parameter
337 // itself, copying in the corresponding values.
338 func (v Value
) Call(in
[]Value
) []Value
{
341 return v
.call("Call", in
)
344 // CallSlice calls the variadic function v with the input arguments in,
345 // assigning the slice in[len(in)-1] to v's final variadic argument.
346 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]...).
347 // Call panics if v's Kind is not Func or if v is not variadic.
348 // It returns the output results as Values.
349 // As in Go, each input argument must be assignable to the
350 // type of the function's corresponding input parameter.
351 func (v Value
) CallSlice(in
[]Value
) []Value
{
354 return v
.call("CallSlice", in
)
357 func (v Value
) call(method
string, in
[]Value
) []Value
{
358 // Get function pointer, type.
364 if v
.flag
&flagMethod
!= 0 {
365 i
:= int(v
.flag
) >> flagMethodShift
366 if v
.typ
.Kind() == Interface
{
367 tt
:= (*interfaceType
)(unsafe
.Pointer(v
.typ
))
368 if i
< 0 || i
>= len(tt
.methods
) {
369 panic("reflect: broken Value")
372 if m
.pkgPath
!= nil {
373 panic(method
+ " of unexported method")
376 iface
:= (*nonEmptyInterface
)(v
.val
)
377 if iface
.itab
== nil {
378 panic(method
+ " of method on nil interface value")
380 fn
= unsafe
.Pointer(&iface
.itab
.fun
[i
])
383 ut
:= v
.typ
.uncommon()
384 if ut
== nil || i
< 0 || i
>= len(ut
.methods
) {
385 panic("reflect: broken Value")
388 if m
.pkgPath
!= nil {
389 panic(method
+ " of unexported method")
391 fn
= unsafe
.Pointer(&m
.tfn
)
395 } else if v
.flag
&flagIndir
!= 0 {
396 fn
= *(*unsafe
.Pointer
)(v
.val
)
402 panic("reflect.Value.Call: call of nil function")
405 isSlice
:= method
== "CallSlice"
409 panic("reflect: CallSlice of non-variadic function")
412 panic("reflect: CallSlice with too few input arguments")
415 panic("reflect: CallSlice with too many input arguments")
422 panic("reflect: Call with too few input arguments")
424 if !t
.IsVariadic() && len(in
) > n
{
425 panic("reflect: Call with too many input arguments")
428 for _
, x
:= range in
{
429 if x
.Kind() == Invalid
{
430 panic("reflect: " + method
+ " using zero Value argument")
433 for i
:= 0; i
< n
; i
++ {
434 if xt
, targ
:= in
[i
].Type(), t
.In(i
); !xt
.AssignableTo(targ
) {
435 panic("reflect: " + method
+ " using " + xt
.String() + " as type " + targ
.String())
438 if !isSlice
&& t
.IsVariadic() {
439 // prepare slice for remaining values
441 slice
:= MakeSlice(t
.In(n
), m
, m
)
442 elem
:= t
.In(n
).Elem()
443 for i
:= 0; i
< m
; i
++ {
445 if xt
:= x
.Type(); !xt
.AssignableTo(elem
) {
446 panic("reflect: cannot use " + xt
.String() + " as type " + elem
.String() + " in " + method
)
448 slice
.Index(i
).Set(x
)
451 in
= make([]Value
, n
+1)
457 if nin
!= t
.NumIn() {
458 panic("reflect.Value.Call: wrong argument count")
462 if v
.flag
&flagMethod
!= 0 {
465 firstPointer
:= len(in
) > 0 && Kind(t
.In(0).(*rtype
).kind
) != Ptr
&& v
.flag
&flagMethod
== 0 && isMethod(v
.typ
)
466 if v
.flag
&flagMethod
== 0 && !firstPointer
{
469 params
:= make([]unsafe
.Pointer
, nin
)
471 if v
.flag
&flagMethod
!= 0 {
472 // Hard-wired first argument.
475 params
[0] = unsafe
.Pointer(p
)
478 for i
, pv
:= range in
{
480 targ
:= t
.In(i
).(*rtype
)
481 pv
= pv
.assignTo("reflect.Value.Call", targ
, nil)
482 if pv
.flag
&flagIndir
== 0 {
483 p
:= new(unsafe
.Pointer
)
485 params
[off
] = unsafe
.Pointer(p
)
489 if i
== 0 && firstPointer
{
490 p
:= new(unsafe
.Pointer
)
492 params
[off
] = unsafe
.Pointer(p
)
496 if v
.flag
&flagMethod
== 0 && !firstPointer
{
498 params
[off
] = unsafe
.Pointer(&fn
)
501 ret
:= make([]Value
, nout
)
502 results
:= make([]unsafe
.Pointer
, nout
)
503 for i
:= 0; i
< nout
; i
++ {
505 results
[i
] = unsafe
.Pointer(v
.Pointer())
509 var pp
*unsafe
.Pointer
513 var pr
*unsafe
.Pointer
514 if len(results
) > 0 {
518 call(t
, fn
, v
.flag
&flagMethod
!= 0, firstPointer
, pp
, pr
)
523 // gccgo specific test to see if typ is a method. We can tell by
524 // looking at the string to see if there is a receiver. We need this
525 // because for gccgo all methods take pointer receivers.
526 func isMethod(t
*rtype
) bool {
527 if Kind(t
.kind
) != Func
{
534 for i
, c
:= range s
{
540 } else if parens
== 0 && c
== ' ' && s
[i
+1] != '(' && !sawRet
{
548 // callReflect is the call implementation used by a function
549 // returned by MakeFunc. In many ways it is the opposite of the
550 // method Value.call above. The method above converts a call using Values
551 // into a call of a function with a concrete argument frame, while
552 // callReflect converts a call of a function with a concrete argument
553 // frame into a call using Values.
554 // It is in this file so that it can be next to the call method above.
555 // The remainder of the MakeFunc implementation is in makefunc.go.
556 func callReflect(ftyp
*funcType
, f
func([]Value
) []Value
, frame unsafe
.Pointer
) {
557 // Copy argument frame into Values.
560 in
:= make([]Value
, 0, len(ftyp
.in
))
561 for _
, arg
:= range ftyp
.in
{
563 off
+= -off
& uintptr(typ
.align
-1)
564 v
:= Value
{typ
, nil, flag(typ
.Kind()) << flagKindShift
}
565 if typ
.size
<= ptrSize
{
566 // value fits in word.
567 v
.val
= unsafe
.Pointer(loadIword(unsafe
.Pointer(uintptr(ptr
)+off
), typ
.size
))
569 // value does not fit in word.
570 // Must make a copy, because f might keep a reference to it,
571 // and we cannot let f keep a reference to the stack frame
572 // after this function returns, not even a read-only reference.
573 v
.val
= unsafe_New(typ
)
574 memmove(v
.val
, unsafe
.Pointer(uintptr(ptr
)+off
), typ
.size
)
581 // Call underlying function.
583 if len(out
) != len(ftyp
.out
) {
584 panic("reflect: wrong return count from function created by MakeFunc")
587 // Copy results back into argument frame.
588 if len(ftyp
.out
) > 0 {
589 off
+= -off
& (ptrSize
- 1)
590 for i
, arg
:= range ftyp
.out
{
594 panic("reflect: function created by MakeFunc using " + funcName(f
) +
595 " returned wrong type: have " +
596 out
[i
].typ
.String() + " for " + typ
.String())
598 if v
.flag
&flagRO
!= 0 {
599 panic("reflect: function created by MakeFunc using " + funcName(f
) +
600 " returned value obtained from unexported field")
602 off
+= -off
& uintptr(typ
.align
-1)
603 addr
:= unsafe
.Pointer(uintptr(ptr
) + off
)
604 if v
.flag
&flagIndir
== 0 {
605 storeIword(addr
, iword(v
.val
), typ
.size
)
607 memmove(addr
, v
.val
, typ
.size
)
614 // funcName returns the name of f, for use in error messages.
615 func funcName(f
func([]Value
) []Value
) string {
616 pc
:= *(*uintptr)(unsafe
.Pointer(&f
))
617 rf
:= runtime
.FuncForPC(pc
)
624 // Cap returns v's capacity.
625 // It panics if v's Kind is not Array, Chan, or Slice.
626 func (v Value
) Cap() int {
632 return int(chancap(*(*iword
)(v
.iword())))
634 // Slice is always bigger than a word; assume flagIndir.
635 return (*SliceHeader
)(v
.val
).Cap
637 panic(&ValueError
{"reflect.Value.Cap", k
})
640 // Close closes the channel v.
641 // It panics if v's Kind is not Chan.
642 func (v Value
) Close() {
645 chanclose(*(*iword
)(v
.iword()))
648 // Complex returns v's underlying value, as a complex128.
649 // It panics if v's Kind is not Complex64 or Complex128
650 func (v Value
) Complex() complex128
{
654 if v
.flag
&flagIndir
!= 0 {
655 return complex128(*(*complex64
)(v
.val
))
657 return complex128(*(*complex64
)(unsafe
.Pointer(&v
.val
)))
659 // complex128 is always bigger than a word; assume flagIndir.
660 return *(*complex128
)(v
.val
)
662 panic(&ValueError
{"reflect.Value.Complex", k
})
665 // Elem returns the value that the interface v contains
666 // or that the pointer v points to.
667 // It panics if v's Kind is not Interface or Ptr.
668 // It returns the zero Value if v is nil.
669 func (v Value
) Elem() Value
{
677 if v
.typ
.NumMethod() == 0 {
678 eface
:= (*emptyInterface
)(v
.val
)
679 if eface
.typ
== nil {
680 // nil interface value
684 val
= unsafe
.Pointer(eface
.word
)
686 iface
:= (*nonEmptyInterface
)(v
.val
)
687 if iface
.itab
== nil {
688 // nil interface value
692 val
= unsafe
.Pointer(iface
.word
)
694 fl
:= v
.flag
& flagRO
695 fl |
= flag(typ
.Kind()) << flagKindShift
696 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
699 return Value
{typ
, val
, fl
}
703 if v
.flag
&flagIndir
!= 0 {
704 val
= *(*unsafe
.Pointer
)(val
)
706 // The returned value's address is v's value.
710 tt
:= (*ptrType
)(unsafe
.Pointer(v
.typ
))
712 fl
:= v
.flag
&flagRO | flagIndir | flagAddr
713 fl |
= flag(typ
.Kind() << flagKindShift
)
714 return Value
{typ
, val
, fl
}
716 panic(&ValueError
{"reflect.Value.Elem", k
})
719 // Field returns the i'th field of the struct v.
720 // It panics if v's Kind is not Struct or i is out of range.
721 func (v Value
) Field(i
int) Value
{
723 tt
:= (*structType
)(unsafe
.Pointer(v
.typ
))
724 if i
< 0 || i
>= len(tt
.fields
) {
725 panic("reflect: Field index out of range")
727 field
:= &tt
.fields
[i
]
730 // Inherit permission bits from v.
731 fl
:= v
.flag
& (flagRO | flagIndir | flagAddr
)
732 // Using an unexported field forces flagRO.
733 if field
.pkgPath
!= nil {
736 fl |
= flag(typ
.Kind()) << flagKindShift
738 var val unsafe
.Pointer
740 case fl
&flagIndir
!= 0:
741 // Indirect. Just bump pointer.
742 val
= unsafe
.Pointer(uintptr(v
.val
) + field
.offset
)
744 // Direct. Discard leading bytes.
745 val
= unsafe
.Pointer(uintptr(v
.val
) << (field
.offset
* 8))
747 // Direct. Discard leading bytes.
748 val
= unsafe
.Pointer(uintptr(v
.val
) >> (field
.offset
* 8))
751 return Value
{typ
, val
, fl
}
754 // FieldByIndex returns the nested field corresponding to index.
755 // It panics if v's Kind is not struct.
756 func (v Value
) FieldByIndex(index
[]int) Value
{
758 for i
, x
:= range index
{
760 if v
.Kind() == Ptr
&& v
.Elem().Kind() == Struct
{
769 // FieldByName returns the struct field with the given name.
770 // It returns the zero Value if no field was found.
771 // It panics if v's Kind is not struct.
772 func (v Value
) FieldByName(name
string) Value
{
774 if f
, ok
:= v
.typ
.FieldByName(name
); ok
{
775 return v
.FieldByIndex(f
.Index
)
780 // FieldByNameFunc returns the struct field with a name
781 // that satisfies the match function.
782 // It panics if v's Kind is not struct.
783 // It returns the zero Value if no field was found.
784 func (v Value
) FieldByNameFunc(match
func(string) bool) Value
{
786 if f
, ok
:= v
.typ
.FieldByNameFunc(match
); ok
{
787 return v
.FieldByIndex(f
.Index
)
792 // Float returns v's underlying value, as a float64.
793 // It panics if v's Kind is not Float32 or Float64
794 func (v Value
) Float() float64 {
798 if v
.flag
&flagIndir
!= 0 {
799 return float64(*(*float32)(v
.val
))
801 return float64(*(*float32)(unsafe
.Pointer(&v
.val
)))
803 if v
.flag
&flagIndir
!= 0 {
804 return *(*float64)(v
.val
)
806 return *(*float64)(unsafe
.Pointer(&v
.val
))
808 panic(&ValueError
{"reflect.Value.Float", k
})
811 var uint8Type
= TypeOf(uint8(0)).(*rtype
)
813 // Index returns v's i'th element.
814 // It panics if v's Kind is not Array, Slice, or String or i is out of range.
815 func (v Value
) Index(i
int) Value
{
819 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
820 if i
< 0 || i
> int(tt
.len) {
821 panic("reflect: array index out of range")
824 fl
:= v
.flag
& (flagRO | flagIndir | flagAddr
) // bits same as overall array
825 fl |
= flag(typ
.Kind()) << flagKindShift
826 offset
:= uintptr(i
) * typ
.size
828 var val unsafe
.Pointer
830 case fl
&flagIndir
!= 0:
831 // Indirect. Just bump pointer.
832 val
= unsafe
.Pointer(uintptr(v
.val
) + offset
)
834 // Direct. Discard leading bytes.
835 val
= unsafe
.Pointer(uintptr(v
.val
) << (offset
* 8))
837 // Direct. Discard leading bytes.
838 val
= unsafe
.Pointer(uintptr(v
.val
) >> (offset
* 8))
840 return Value
{typ
, val
, fl
}
843 // Element flag same as Elem of Ptr.
844 // Addressable, indirect, possibly read-only.
845 fl
:= flagAddr | flagIndir | v
.flag
&flagRO
846 s
:= (*SliceHeader
)(v
.val
)
847 if i
< 0 || i
>= s
.Len
{
848 panic("reflect: slice index out of range")
850 tt
:= (*sliceType
)(unsafe
.Pointer(v
.typ
))
852 fl |
= flag(typ
.Kind()) << flagKindShift
853 val
:= unsafe
.Pointer(s
.Data
+ uintptr(i
)*typ
.size
)
854 return Value
{typ
, val
, fl
}
857 fl
:= v
.flag
&flagRO |
flag(Uint8
<<flagKindShift
) | flagIndir
858 s
:= (*StringHeader
)(v
.val
)
859 if i
< 0 || i
>= s
.Len
{
860 panic("reflect: string index out of range")
862 val
:= *(*byte)(unsafe
.Pointer(s
.Data
+ uintptr(i
)))
863 return Value
{uint8Type
, unsafe
.Pointer(&val
), fl
}
865 panic(&ValueError
{"reflect.Value.Index", k
})
868 // Int returns v's underlying value, as an int64.
869 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
870 func (v Value
) Int() int64 {
873 if v
.flag
&flagIndir
!= 0 {
876 // The escape analysis is good enough that &v.val
877 // does not trigger a heap allocation.
878 p
= unsafe
.Pointer(&v
.val
)
882 return int64(*(*int)(p
))
884 return int64(*(*int8)(p
))
886 return int64(*(*int16)(p
))
888 return int64(*(*int32)(p
))
890 return int64(*(*int64)(p
))
892 panic(&ValueError
{"reflect.Value.Int", k
})
895 // CanInterface returns true if Interface can be used without panicking.
896 func (v Value
) CanInterface() bool {
898 panic(&ValueError
{"reflect.Value.CanInterface", Invalid
})
900 return v
.flag
&(flagMethod|flagRO
) == 0
903 // Interface returns v's current value as an interface{}.
904 // It is equivalent to:
905 // var i interface{} = (v's underlying value)
906 // If v is a method obtained by invoking Value.Method
907 // (as opposed to Type.Method), Interface cannot return an
908 // interface value, so it panics.
909 // It also panics if the Value was obtained by accessing
910 // unexported struct fields.
911 func (v Value
) Interface() (i
interface{}) {
912 return valueInterface(v
, true)
915 func valueInterface(v Value
, safe
bool) interface{} {
917 panic(&ValueError
{"reflect.Value.Interface", 0})
919 if v
.flag
&flagMethod
!= 0 {
920 panic("reflect.Value.Interface: cannot create interface value for method with bound receiver")
923 if safe
&& v
.flag
&flagRO
!= 0 {
924 // Do not allow access to unexported values via Interface,
925 // because they might be pointers that should not be
926 // writable or methods or function that should not be callable.
927 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
932 // Special case: return the element inside the interface.
933 // Empty interface has one layout, all interfaces with
934 // methods have a second layout.
935 if v
.NumMethod() == 0 {
936 return *(*interface{})(v
.val
)
938 return *(*interface {
943 // Non-interface value.
944 var eface emptyInterface
945 eface
.typ
= toType(v
.typ
).common()
946 eface
.word
= v
.iword()
948 if v
.flag
&flagIndir
!= 0 && v
.kind() != Ptr
&& v
.kind() != UnsafePointer
{
949 // eface.word is a pointer to the actual data,
950 // which might be changed. We need to return
951 // a pointer to unchanging data, so make a copy.
952 ptr
:= unsafe_New(v
.typ
)
953 memmove(ptr
, unsafe
.Pointer(eface
.word
), v
.typ
.size
)
954 eface
.word
= iword(ptr
)
957 if v
.flag
&flagIndir
== 0 && v
.kind() != Ptr
&& v
.kind() != UnsafePointer
{
958 panic("missing flagIndir")
961 return *(*interface{})(unsafe
.Pointer(&eface
))
964 // InterfaceData returns the interface v's value as a uintptr pair.
965 // It panics if v's Kind is not Interface.
966 func (v Value
) InterfaceData() [2]uintptr {
968 // We treat this as a read operation, so we allow
969 // it even for unexported data, because the caller
970 // has to import "unsafe" to turn it into something
971 // that can be abused.
972 // Interface value is always bigger than a word; assume flagIndir.
973 return *(*[2]uintptr)(v
.val
)
976 // IsNil returns true if v is a nil value.
977 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
978 func (v Value
) IsNil() bool {
981 case Chan
, Func
, Map
, Ptr
:
982 if v
.flag
&flagMethod
!= 0 {
983 panic("reflect: IsNil of method Value")
986 if v
.flag
&flagIndir
!= 0 {
987 ptr
= *(*unsafe
.Pointer
)(ptr
)
990 case Interface
, Slice
:
991 // Both interface and slice are nil if first word is 0.
992 // Both are always bigger than a word; assume flagIndir.
993 return *(*unsafe
.Pointer
)(v
.val
) == nil
995 panic(&ValueError
{"reflect.Value.IsNil", k
})
998 // IsValid returns true if v represents a value.
999 // It returns false if v is the zero Value.
1000 // If IsValid returns false, all other methods except String panic.
1001 // Most functions and methods never return an invalid value.
1002 // If one does, its documentation states the conditions explicitly.
1003 func (v Value
) IsValid() bool {
1007 // Kind returns v's Kind.
1008 // If v is the zero Value (IsValid returns false), Kind returns Invalid.
1009 func (v Value
) Kind() Kind
{
1013 // Len returns v's length.
1014 // It panics if v's Kind is not Array, Chan, Map, Slice, or String.
1015 func (v Value
) Len() int {
1019 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
1022 return chanlen(*(*iword
)(v
.iword()))
1024 return maplen(*(*iword
)(v
.iword()))
1026 // Slice is bigger than a word; assume flagIndir.
1027 return (*SliceHeader
)(v
.val
).Len
1029 // String is bigger than a word; assume flagIndir.
1030 return (*StringHeader
)(v
.val
).Len
1032 panic(&ValueError
{"reflect.Value.Len", k
})
1035 // MapIndex returns the value associated with key in the map v.
1036 // It panics if v's Kind is not Map.
1037 // It returns the zero Value if key is not found in the map or if v represents a nil map.
1038 // As in Go, the key's value must be assignable to the map's key type.
1039 func (v Value
) MapIndex(key Value
) Value
{
1041 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
1043 // Do not require key to be exported, so that DeepEqual
1044 // and other programs can use all the keys returned by
1045 // MapKeys as arguments to MapIndex. If either the map
1046 // or the key is unexported, though, the result will be
1047 // considered unexported. This is consistent with the
1048 // behavior for structs, which allow read but not write
1049 // of unexported fields.
1050 key
= key
.assignTo("reflect.Value.MapIndex", tt
.key
, nil)
1052 word
, ok
:= mapaccess(v
.typ
, *(*iword
)(v
.iword()), key
.iword())
1057 fl
:= (v
.flag | key
.flag
) & flagRO
1058 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1061 fl |
= flag(typ
.Kind()) << flagKindShift
1062 return Value
{typ
, unsafe
.Pointer(word
), fl
}
1065 // MapKeys returns a slice containing all the keys present in the map,
1066 // in unspecified order.
1067 // It panics if v's Kind is not Map.
1068 // It returns an empty slice if v represents a nil map.
1069 func (v Value
) MapKeys() []Value
{
1071 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
1074 fl
:= v
.flag
& flagRO
1075 fl |
= flag(keyType
.Kind()) << flagKindShift
1076 if keyType
.Kind() != Ptr
&& keyType
.Kind() != UnsafePointer
{
1080 m
:= *(*iword
)(v
.iword())
1085 it
:= mapiterinit(v
.typ
, m
)
1086 a
:= make([]Value
, mlen
)
1088 for i
= 0; i
< len(a
); i
++ {
1089 keyWord
, ok
:= mapiterkey(it
)
1093 a
[i
] = Value
{keyType
, unsafe
.Pointer(keyWord
), fl
}
1099 // Method returns a function value corresponding to v's i'th method.
1100 // The arguments to a Call on the returned function should not include
1101 // a receiver; the returned function will always use v as the receiver.
1102 // Method panics if i is out of range.
1103 func (v Value
) Method(i
int) Value
{
1105 panic(&ValueError
{"reflect.Value.Method", Invalid
})
1107 if v
.flag
&flagMethod
!= 0 || i
< 0 || i
>= v
.typ
.NumMethod() {
1108 panic("reflect: Method index out of range")
1110 fl
:= v
.flag
& (flagRO | flagAddr | flagIndir
)
1111 fl |
= flag(Func
) << flagKindShift
1112 fl |
= flag(i
)<<flagMethodShift | flagMethod
1113 return Value
{v
.typ
, v
.val
, fl
}
1116 // NumMethod returns the number of methods in the value's method set.
1117 func (v Value
) NumMethod() int {
1119 panic(&ValueError
{"reflect.Value.NumMethod", Invalid
})
1121 if v
.flag
&flagMethod
!= 0 {
1124 return v
.typ
.NumMethod()
1127 // MethodByName returns a function value corresponding to the method
1128 // of v with the given name.
1129 // The arguments to a Call on the returned function should not include
1130 // a receiver; the returned function will always use v as the receiver.
1131 // It returns the zero Value if no method was found.
1132 func (v Value
) MethodByName(name
string) Value
{
1134 panic(&ValueError
{"reflect.Value.MethodByName", Invalid
})
1136 if v
.flag
&flagMethod
!= 0 {
1139 m
, ok
:= v
.typ
.MethodByName(name
)
1143 return v
.Method(m
.Index
)
1146 // NumField returns the number of fields in the struct v.
1147 // It panics if v's Kind is not Struct.
1148 func (v Value
) NumField() int {
1150 tt
:= (*structType
)(unsafe
.Pointer(v
.typ
))
1151 return len(tt
.fields
)
1154 // OverflowComplex returns true if the complex128 x cannot be represented by v's type.
1155 // It panics if v's Kind is not Complex64 or Complex128.
1156 func (v Value
) OverflowComplex(x complex128
) bool {
1160 return overflowFloat32(real(x
)) ||
overflowFloat32(imag(x
))
1164 panic(&ValueError
{"reflect.Value.OverflowComplex", k
})
1167 // OverflowFloat returns true if the float64 x cannot be represented by v's type.
1168 // It panics if v's Kind is not Float32 or Float64.
1169 func (v Value
) OverflowFloat(x
float64) bool {
1173 return overflowFloat32(x
)
1177 panic(&ValueError
{"reflect.Value.OverflowFloat", k
})
1180 func overflowFloat32(x
float64) bool {
1184 return math
.MaxFloat32
< x
&& x
<= math
.MaxFloat64
1187 // OverflowInt returns true if the int64 x cannot be represented by v's type.
1188 // It panics if v's Kind is not Int, Int8, int16, Int32, or Int64.
1189 func (v Value
) OverflowInt(x
int64) bool {
1192 case Int
, Int8
, Int16
, Int32
, Int64
:
1193 bitSize
:= v
.typ
.size
* 8
1194 trunc
:= (x
<< (64 - bitSize
)) >> (64 - bitSize
)
1197 panic(&ValueError
{"reflect.Value.OverflowInt", k
})
1200 // OverflowUint returns true if the uint64 x cannot be represented by v's type.
1201 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1202 func (v Value
) OverflowUint(x
uint64) bool {
1205 case Uint
, Uintptr
, Uint8
, Uint16
, Uint32
, Uint64
:
1206 bitSize
:= v
.typ
.size
* 8
1207 trunc
:= (x
<< (64 - bitSize
)) >> (64 - bitSize
)
1210 panic(&ValueError
{"reflect.Value.OverflowUint", k
})
1213 // Pointer returns v's value as a uintptr.
1214 // It returns uintptr instead of unsafe.Pointer so that
1215 // code using reflect cannot obtain unsafe.Pointers
1216 // without importing the unsafe package explicitly.
1217 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
1219 // If v's Kind is Func, the returned pointer is an underlying
1220 // code pointer, but not necessarily enough to identify a
1221 // single function uniquely. The only guarantee is that the
1222 // result is zero if and only if v is a nil func Value.
1223 func (v Value
) Pointer() uintptr {
1226 case Chan
, Map
, Ptr
, UnsafePointer
:
1228 if v
.flag
&flagIndir
!= 0 {
1229 p
= *(*unsafe
.Pointer
)(p
)
1233 if v
.flag
&flagMethod
!= 0 {
1234 panic("reflect.Value.Pointer of method Value")
1237 if v
.flag
&flagIndir
!= 0 {
1238 p
= *(*unsafe
.Pointer
)(p
)
1240 // Non-nil func value points at data block.
1241 // First word of data block is actual code.
1243 p
= *(*unsafe
.Pointer
)(p
)
1248 return (*SliceHeader
)(v
.val
).Data
1250 panic(&ValueError
{"reflect.Value.Pointer", k
})
1253 // Recv receives and returns a value from the channel v.
1254 // It panics if v's Kind is not Chan.
1255 // The receive blocks until a value is ready.
1256 // The boolean value ok is true if the value x corresponds to a send
1257 // on the channel, false if it is a zero value received because the channel is closed.
1258 func (v Value
) Recv() (x Value
, ok
bool) {
1261 return v
.recv(false)
1264 // internal recv, possibly non-blocking (nb).
1265 // v is known to be a channel.
1266 func (v Value
) recv(nb
bool) (val Value
, ok
bool) {
1267 tt
:= (*chanType
)(unsafe
.Pointer(v
.typ
))
1268 if ChanDir(tt
.dir
)&RecvDir
== 0 {
1269 panic("recv on send-only channel")
1271 word
, selected
, ok
:= chanrecv(v
.typ
, *(*iword
)(v
.iword()), nb
)
1274 fl
:= flag(typ
.Kind()) << flagKindShift
1275 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1278 val
= Value
{typ
, unsafe
.Pointer(word
), fl
}
1283 // Send sends x on the channel v.
1284 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
1285 // As in Go, x's value must be assignable to the channel's element type.
1286 func (v Value
) Send(x Value
) {
1292 // internal send, possibly non-blocking.
1293 // v is known to be a channel.
1294 func (v Value
) send(x Value
, nb
bool) (selected
bool) {
1295 tt
:= (*chanType
)(unsafe
.Pointer(v
.typ
))
1296 if ChanDir(tt
.dir
)&SendDir
== 0 {
1297 panic("send on recv-only channel")
1300 x
= x
.assignTo("reflect.Value.Send", tt
.elem
, nil)
1301 return chansend(v
.typ
, *(*iword
)(v
.iword()), x
.iword(), nb
)
1304 // Set assigns x to the value v.
1305 // It panics if CanSet returns false.
1306 // As in Go, x's value must be assignable to v's type.
1307 func (v Value
) Set(x Value
) {
1308 v
.mustBeAssignable()
1309 x
.mustBeExported() // do not let unexported x leak
1310 var target
*interface{}
1311 if v
.kind() == Interface
{
1312 target
= (*interface{})(v
.val
)
1314 x
= x
.assignTo("reflect.Set", v
.typ
, target
)
1315 if x
.flag
&flagIndir
!= 0 {
1316 memmove(v
.val
, x
.val
, v
.typ
.size
)
1318 storeIword(v
.val
, iword(x
.val
), v
.typ
.size
)
1322 // SetBool sets v's underlying value.
1323 // It panics if v's Kind is not Bool or if CanSet() is false.
1324 func (v Value
) SetBool(x
bool) {
1325 v
.mustBeAssignable()
1330 // SetBytes sets v's underlying value.
1331 // It panics if v's underlying value is not a slice of bytes.
1332 func (v Value
) SetBytes(x
[]byte) {
1333 v
.mustBeAssignable()
1335 if v
.typ
.Elem().Kind() != Uint8
{
1336 panic("reflect.Value.SetBytes of non-byte slice")
1338 *(*[]byte)(v
.val
) = x
1341 // setRunes sets v's underlying value.
1342 // It panics if v's underlying value is not a slice of runes (int32s).
1343 func (v Value
) setRunes(x
[]rune
) {
1344 v
.mustBeAssignable()
1346 if v
.typ
.Elem().Kind() != Int32
{
1347 panic("reflect.Value.setRunes of non-rune slice")
1349 *(*[]rune
)(v
.val
) = x
1352 // SetComplex sets v's underlying value to x.
1353 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
1354 func (v Value
) SetComplex(x complex128
) {
1355 v
.mustBeAssignable()
1356 switch k
:= v
.kind(); k
{
1358 panic(&ValueError
{"reflect.Value.SetComplex", k
})
1360 *(*complex64
)(v
.val
) = complex64(x
)
1362 *(*complex128
)(v
.val
) = x
1366 // SetFloat sets v's underlying value to x.
1367 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
1368 func (v Value
) SetFloat(x
float64) {
1369 v
.mustBeAssignable()
1370 switch k
:= v
.kind(); k
{
1372 panic(&ValueError
{"reflect.Value.SetFloat", k
})
1374 *(*float32)(v
.val
) = float32(x
)
1376 *(*float64)(v
.val
) = x
1380 // SetInt sets v's underlying value to x.
1381 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
1382 func (v Value
) SetInt(x
int64) {
1383 v
.mustBeAssignable()
1384 switch k
:= v
.kind(); k
{
1386 panic(&ValueError
{"reflect.Value.SetInt", k
})
1388 *(*int)(v
.val
) = int(x
)
1390 *(*int8)(v
.val
) = int8(x
)
1392 *(*int16)(v
.val
) = int16(x
)
1394 *(*int32)(v
.val
) = int32(x
)
1396 *(*int64)(v
.val
) = x
1400 // SetLen sets v's length to n.
1401 // It panics if v's Kind is not Slice or if n is negative or
1402 // greater than the capacity of the slice.
1403 func (v Value
) SetLen(n
int) {
1404 v
.mustBeAssignable()
1406 s
:= (*SliceHeader
)(v
.val
)
1407 if n
< 0 || n
> int(s
.Cap
) {
1408 panic("reflect: slice length out of range in SetLen")
1413 // SetMapIndex sets the value associated with key in the map v to val.
1414 // It panics if v's Kind is not Map.
1415 // If val is the zero Value, SetMapIndex deletes the key from the map.
1416 // As in Go, key's value must be assignable to the map's key type,
1417 // and val's value must be assignable to the map's value type.
1418 func (v Value
) SetMapIndex(key
, val Value
) {
1421 key
.mustBeExported()
1422 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
1423 key
= key
.assignTo("reflect.Value.SetMapIndex", tt
.key
, nil)
1425 val
.mustBeExported()
1426 val
= val
.assignTo("reflect.Value.SetMapIndex", tt
.elem
, nil)
1428 mapassign(v
.typ
, *(*iword
)(v
.iword()), key
.iword(), val
.iword(), val
.typ
!= nil)
1431 // SetUint sets v's underlying value to x.
1432 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
1433 func (v Value
) SetUint(x
uint64) {
1434 v
.mustBeAssignable()
1435 switch k
:= v
.kind(); k
{
1437 panic(&ValueError
{"reflect.Value.SetUint", k
})
1439 *(*uint)(v
.val
) = uint(x
)
1441 *(*uint8)(v
.val
) = uint8(x
)
1443 *(*uint16)(v
.val
) = uint16(x
)
1445 *(*uint32)(v
.val
) = uint32(x
)
1447 *(*uint64)(v
.val
) = x
1449 *(*uintptr)(v
.val
) = uintptr(x
)
1453 // SetPointer sets the unsafe.Pointer value v to x.
1454 // It panics if v's Kind is not UnsafePointer.
1455 func (v Value
) SetPointer(x unsafe
.Pointer
) {
1456 v
.mustBeAssignable()
1457 v
.mustBe(UnsafePointer
)
1458 *(*unsafe
.Pointer
)(v
.val
) = x
1461 // SetString sets v's underlying value to x.
1462 // It panics if v's Kind is not String or if CanSet() is false.
1463 func (v Value
) SetString(x
string) {
1464 v
.mustBeAssignable()
1466 *(*string)(v
.val
) = x
1469 // Slice returns a slice of v.
1470 // It panics if v's Kind is not Array, Slice, or String.
1471 func (v Value
) Slice(beg
, end
int) Value
{
1477 switch k
:= v
.kind(); k
{
1479 panic(&ValueError
{"reflect.Value.Slice", k
})
1482 if v
.flag
&flagAddr
== 0 {
1483 panic("reflect.Value.Slice: slice of unaddressable array")
1485 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
1487 typ
= (*sliceType
)(unsafe
.Pointer(tt
.slice
))
1491 typ
= (*sliceType
)(unsafe
.Pointer(v
.typ
))
1492 s
:= (*SliceHeader
)(v
.val
)
1493 base
= unsafe
.Pointer(s
.Data
)
1497 s
:= (*StringHeader
)(v
.val
)
1498 if beg
< 0 || end
< beg || end
> s
.Len
{
1499 panic("reflect.Value.Slice: string slice index out of bounds")
1502 val
:= (*StringHeader
)(unsafe
.Pointer(&x
))
1503 val
.Data
= s
.Data
+ uintptr(beg
)
1505 return Value
{v
.typ
, unsafe
.Pointer(&x
), v
.flag
}
1508 if beg
< 0 || end
< beg || end
> cap {
1509 panic("reflect.Value.Slice: slice index out of bounds")
1512 // Declare slice so that gc can see the base pointer in it.
1513 var x
[]unsafe
.Pointer
1515 // Reinterpret as *SliceHeader to edit.
1516 s
:= (*SliceHeader
)(unsafe
.Pointer(&x
))
1517 s
.Data
= uintptr(base
) + uintptr(beg
)*typ
.elem
.Size()
1521 fl
:= v
.flag
&flagRO | flagIndir |
flag(Slice
)<<flagKindShift
1522 return Value
{typ
.common(), unsafe
.Pointer(&x
), fl
}
1525 // String returns the string v's underlying value, as a string.
1526 // String is a special case because of Go's String method convention.
1527 // Unlike the other getters, it does not panic if v's Kind is not String.
1528 // Instead, it returns a string of the form "<T value>" where T is v's type.
1529 func (v Value
) String() string {
1530 switch k
:= v
.kind(); k
{
1532 return "<invalid Value>"
1534 return *(*string)(v
.val
)
1536 // If you call String on a reflect.Value of other type, it's better to
1537 // print something than to panic. Useful in debugging.
1538 return "<" + v
.typ
.String() + " Value>"
1541 // TryRecv attempts to receive a value from the channel v but will not block.
1542 // It panics if v's Kind is not Chan.
1543 // If the receive cannot finish without blocking, x is the zero Value.
1544 // The boolean ok is true if the value x corresponds to a send
1545 // on the channel, false if it is a zero value received because the channel is closed.
1546 func (v Value
) TryRecv() (x Value
, ok
bool) {
1552 // TrySend attempts to send x on the channel v but will not block.
1553 // It panics if v's Kind is not Chan.
1554 // It returns true if the value was sent, false otherwise.
1555 // As in Go, x's value must be assignable to the channel's element type.
1556 func (v Value
) TrySend(x Value
) bool {
1559 return v
.send(x
, true)
1562 // Type returns v's type.
1563 func (v Value
) Type() Type
{
1566 panic(&ValueError
{"reflect.Value.Type", Invalid
})
1568 if f
&flagMethod
== 0 {
1570 return toType(v
.typ
)
1574 // v.typ describes the receiver, not the method type.
1575 i
:= int(v
.flag
) >> flagMethodShift
1576 if v
.typ
.Kind() == Interface
{
1577 // Method on interface.
1578 tt
:= (*interfaceType
)(unsafe
.Pointer(v
.typ
))
1579 if i
< 0 || i
>= len(tt
.methods
) {
1580 panic("reflect: broken Value")
1583 return toType(m
.typ
)
1585 // Method on concrete type.
1586 ut
:= v
.typ
.uncommon()
1587 if ut
== nil || i
< 0 || i
>= len(ut
.methods
) {
1588 panic("reflect: broken Value")
1591 return toType(m
.mtyp
)
1594 // Uint returns v's underlying value, as a uint64.
1595 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1596 func (v Value
) Uint() uint64 {
1598 var p unsafe
.Pointer
1599 if v
.flag
&flagIndir
!= 0 {
1602 // The escape analysis is good enough that &v.val
1603 // does not trigger a heap allocation.
1604 p
= unsafe
.Pointer(&v
.val
)
1608 return uint64(*(*uint)(p
))
1610 return uint64(*(*uint8)(p
))
1612 return uint64(*(*uint16)(p
))
1614 return uint64(*(*uint32)(p
))
1616 return uint64(*(*uint64)(p
))
1618 return uint64(*(*uintptr)(p
))
1620 panic(&ValueError
{"reflect.Value.Uint", k
})
1623 // UnsafeAddr returns a pointer to v's data.
1624 // It is for advanced clients that also import the "unsafe" package.
1625 // It panics if v is not addressable.
1626 func (v Value
) UnsafeAddr() uintptr {
1628 panic(&ValueError
{"reflect.Value.UnsafeAddr", Invalid
})
1630 if v
.flag
&flagAddr
== 0 {
1631 panic("reflect.Value.UnsafeAddr of unaddressable value")
1633 return uintptr(v
.val
)
1636 // StringHeader is the runtime representation of a string.
1637 // It cannot be used safely or portably.
1638 type StringHeader
struct {
1643 // SliceHeader is the runtime representation of a slice.
1644 // It cannot be used safely or portably.
1645 type SliceHeader
struct {
1651 func typesMustMatch(what
string, t1
, t2 Type
) {
1653 panic(what
+ ": " + t1
.String() + " != " + t2
.String())
1657 // grow grows the slice s so that it can hold extra more values, allocating
1658 // more capacity if needed. It also returns the old and new slice lengths.
1659 func grow(s Value
, extra
int) (Value
, int, int) {
1663 panic("reflect.Append: slice overflow")
1667 return s
.Slice(0, i1
), i0
, i1
1680 t
:= MakeSlice(s
.Type(), i1
, m
)
1685 // Append appends the values x to a slice s and returns the resulting slice.
1686 // As in Go, each x's value must be assignable to the slice's element type.
1687 func Append(s Value
, x
...Value
) Value
{
1689 s
, i0
, i1
:= grow(s
, len(x
))
1690 for i
, j
:= i0
, 0; i
< i1
; i
, j
= i
+1, j
+1 {
1691 s
.Index(i
).Set(x
[j
])
1696 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
1697 // The slices s and t must have the same element type.
1698 func AppendSlice(s
, t Value
) Value
{
1701 typesMustMatch("reflect.AppendSlice", s
.Type().Elem(), t
.Type().Elem())
1702 s
, i0
, i1
:= grow(s
, t
.Len())
1703 Copy(s
.Slice(i0
, i1
), t
)
1707 // Copy copies the contents of src into dst until either
1708 // dst has been filled or src has been exhausted.
1709 // It returns the number of elements copied.
1710 // Dst and src each must have kind Slice or Array, and
1711 // dst and src must have the same element type.
1712 func Copy(dst
, src Value
) int {
1714 if dk
!= Array
&& dk
!= Slice
{
1715 panic(&ValueError
{"reflect.Copy", dk
})
1718 dst
.mustBeAssignable()
1720 dst
.mustBeExported()
1723 if sk
!= Array
&& sk
!= Slice
{
1724 panic(&ValueError
{"reflect.Copy", sk
})
1726 src
.mustBeExported()
1728 de
:= dst
.typ
.Elem()
1729 se
:= src
.typ
.Elem()
1730 typesMustMatch("reflect.Copy", de
, se
)
1733 if sn
:= src
.Len(); n
> sn
{
1737 // If sk is an in-line array, cannot take its address.
1738 // Instead, copy element by element.
1739 if src
.flag
&flagIndir
== 0 {
1740 for i
:= 0; i
< n
; i
++ {
1741 dst
.Index(i
).Set(src
.Index(i
))
1746 // Copy via memmove.
1747 var da
, sa unsafe
.Pointer
1751 da
= unsafe
.Pointer((*SliceHeader
)(dst
.val
).Data
)
1756 sa
= unsafe
.Pointer((*SliceHeader
)(src
.val
).Data
)
1758 memmove(da
, sa
, uintptr(n
)*de
.Size())
1762 // A runtimeSelect is a single case passed to rselect.
1763 // This must match ../runtime/chan.c:/runtimeSelect
1764 type runtimeSelect
struct {
1765 dir
uintptr // 0, SendDir, or RecvDir
1766 typ
*rtype
// channel type
1767 ch iword
// interface word for channel
1768 val iword
// interface word for value (for SendDir)
1771 // rselect runs a select. It returns the index of the chosen case,
1772 // and if the case was a receive, the interface word of the received
1773 // value and the conventional OK bool to indicate whether the receive
1774 // corresponds to a sent value.
1775 func rselect([]runtimeSelect
) (chosen
int, recv iword
, recvOK
bool)
1777 // A SelectDir describes the communication direction of a select case.
1780 // NOTE: These values must match ../runtime/chan.c:/SelectDir.
1784 SelectSend
// case Chan <- Send
1785 SelectRecv
// case <-Chan:
1786 SelectDefault
// default
1789 // A SelectCase describes a single case in a select operation.
1790 // The kind of case depends on Dir, the communication direction.
1792 // If Dir is SelectDefault, the case represents a default case.
1793 // Chan and Send must be zero Values.
1795 // If Dir is SelectSend, the case represents a send operation.
1796 // Normally Chan's underlying value must be a channel, and Send's underlying value must be
1797 // assignable to the channel's element type. As a special case, if Chan is a zero Value,
1798 // then the case is ignored, and the field Send will also be ignored and may be either zero
1801 // If Dir is SelectRecv, the case represents a receive operation.
1802 // Normally Chan's underlying value must be a channel and Send must be a zero Value.
1803 // If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value.
1804 // When a receive operation is selected, the received Value is returned by Select.
1806 type SelectCase
struct {
1807 Dir SelectDir
// direction of case
1808 Chan Value
// channel to use (for send or receive)
1809 Send Value
// value to send (for send)
1812 // Select executes a select operation described by the list of cases.
1813 // Like the Go select statement, it blocks until one of the cases can
1814 // proceed and then executes that case. It returns the index of the chosen case
1815 // and, if that case was a receive operation, the value received and a
1816 // boolean indicating whether the value corresponds to a send on the channel
1817 // (as opposed to a zero value received because the channel is closed).
1818 func Select(cases
[]SelectCase
) (chosen
int, recv Value
, recvOK
bool) {
1819 // NOTE: Do not trust that caller is not modifying cases data underfoot.
1820 // The range is safe because the caller cannot modify our copy of the len
1821 // and each iteration makes its own copy of the value c.
1822 runcases
:= make([]runtimeSelect
, len(cases
))
1823 haveDefault
:= false
1824 for i
, c
:= range cases
{
1826 rc
.dir
= uintptr(c
.Dir
)
1829 panic("reflect.Select: invalid Dir")
1831 case SelectDefault
: // default
1833 panic("reflect.Select: multiple default cases")
1836 if c
.Chan
.IsValid() {
1837 panic("reflect.Select: default case has Chan value")
1839 if c
.Send
.IsValid() {
1840 panic("reflect.Select: default case has Send value")
1850 tt
:= (*chanType
)(unsafe
.Pointer(ch
.typ
))
1851 if ChanDir(tt
.dir
)&SendDir
== 0 {
1852 panic("reflect.Select: SendDir case using recv-only channel")
1854 rc
.ch
= *(*iword
)(ch
.iword())
1858 panic("reflect.Select: SendDir case missing Send value")
1861 v
= v
.assignTo("reflect.Select", tt
.elem
, nil)
1865 if c
.Send
.IsValid() {
1866 panic("reflect.Select: RecvDir case has Send value")
1874 tt
:= (*chanType
)(unsafe
.Pointer(ch
.typ
))
1876 if ChanDir(tt
.dir
)&RecvDir
== 0 {
1877 panic("reflect.Select: RecvDir case using send-only channel")
1879 rc
.ch
= *(*iword
)(ch
.iword())
1883 chosen
, word
, recvOK
:= rselect(runcases
)
1884 if runcases
[chosen
].dir
== uintptr(SelectRecv
) {
1885 tt
:= (*chanType
)(unsafe
.Pointer(runcases
[chosen
].typ
))
1887 fl
:= flag(typ
.Kind()) << flagKindShift
1888 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1891 recv
= Value
{typ
, unsafe
.Pointer(word
), fl
}
1893 return chosen
, recv
, recvOK
1900 // implemented in package runtime
1901 func unsafe_New(*rtype
) unsafe
.Pointer
1902 func unsafe_NewArray(*rtype
, int) unsafe
.Pointer
1904 // MakeSlice creates a new zero-initialized slice value
1905 // for the specified slice type, length, and capacity.
1906 func MakeSlice(typ Type
, len, cap int) Value
{
1907 if typ
.Kind() != Slice
{
1908 panic("reflect.MakeSlice of non-slice type")
1911 panic("reflect.MakeSlice: negative len")
1914 panic("reflect.MakeSlice: negative cap")
1917 panic("reflect.MakeSlice: len > cap")
1920 // Declare slice so that gc can see the base pointer in it.
1921 var x
[]unsafe
.Pointer
1923 // Reinterpret as *SliceHeader to edit.
1924 s
:= (*SliceHeader
)(unsafe
.Pointer(&x
))
1925 s
.Data
= uintptr(unsafe_NewArray(typ
.Elem().(*rtype
), cap))
1929 return Value
{typ
.common(), unsafe
.Pointer(&x
), flagIndir |
flag(Slice
)<<flagKindShift
}
1932 // MakeChan creates a new channel with the specified type and buffer size.
1933 func MakeChan(typ Type
, buffer
int) Value
{
1934 if typ
.Kind() != Chan
{
1935 panic("reflect.MakeChan of non-chan type")
1938 panic("reflect.MakeChan: negative buffer size")
1940 if typ
.ChanDir() != BothDir
{
1941 panic("reflect.MakeChan: unidirectional channel type")
1943 ch
:= makechan(typ
.(*rtype
), uint64(buffer
))
1944 return Value
{typ
.common(), unsafe
.Pointer(ch
), flagIndir |
(flag(Chan
) << flagKindShift
)}
1947 // MakeMap creates a new map of the specified type.
1948 func MakeMap(typ Type
) Value
{
1949 if typ
.Kind() != Map
{
1950 panic("reflect.MakeMap of non-map type")
1952 m
:= makemap(typ
.(*rtype
))
1953 return Value
{typ
.common(), unsafe
.Pointer(m
), flagIndir |
(flag(Map
) << flagKindShift
)}
1956 // Indirect returns the value that v points to.
1957 // If v is a nil pointer, Indirect returns a zero Value.
1958 // If v is not a pointer, Indirect returns v.
1959 func Indirect(v Value
) Value
{
1960 if v
.Kind() != Ptr
{
1966 // ValueOf returns a new Value initialized to the concrete value
1967 // stored in the interface i. ValueOf(nil) returns the zero Value.
1968 func ValueOf(i
interface{}) Value
{
1973 // TODO(rsc): Eliminate this terrible hack.
1974 // In the call to packValue, eface.typ doesn't escape,
1975 // and eface.word is an integer. So it looks like
1976 // i (= eface) doesn't escape. But really it does,
1977 // because eface.word is actually a pointer.
1980 // For an interface value with the noAddr bit set,
1981 // the representation is identical to an empty interface.
1982 eface
:= *(*emptyInterface
)(unsafe
.Pointer(&i
))
1984 fl
:= flag(typ
.Kind()) << flagKindShift
1985 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1988 return Value
{typ
, unsafe
.Pointer(eface
.word
), fl
}
1991 // Zero returns a Value representing the zero value for the specified type.
1992 // The result is different from the zero value of the Value struct,
1993 // which represents no value at all.
1994 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
1995 // The returned value is neither addressable nor settable.
1996 func Zero(typ Type
) Value
{
1998 panic("reflect: Zero(nil)")
2001 fl
:= flag(t
.Kind()) << flagKindShift
2002 if t
.Kind() == Ptr || t
.Kind() == UnsafePointer
{
2003 return Value
{t
, nil, fl
}
2005 return Value
{t
, unsafe_New(typ
.(*rtype
)), fl | flagIndir
}
2008 // New returns a Value representing a pointer to a new zero value
2009 // for the specified type. That is, the returned Value's Type is PtrTo(t).
2010 func New(typ Type
) Value
{
2012 panic("reflect: New(nil)")
2014 ptr
:= unsafe_New(typ
.(*rtype
))
2015 fl
:= flag(Ptr
) << flagKindShift
2016 return Value
{typ
.common().ptrTo(), ptr
, fl
}
2019 // NewAt returns a Value representing a pointer to a value of the
2020 // specified type, using p as that pointer.
2021 func NewAt(typ Type
, p unsafe
.Pointer
) Value
{
2022 fl
:= flag(Ptr
) << flagKindShift
2023 return Value
{typ
.common().ptrTo(), p
, fl
}
2026 // assignTo returns a value v that can be assigned directly to typ.
2027 // It panics if v is not assignable to typ.
2028 // For a conversion to an interface type, target is a suggested scratch space to use.
2029 func (v Value
) assignTo(context
string, dst
*rtype
, target
*interface{}) Value
{
2030 if v
.flag
&flagMethod
!= 0 {
2031 panic(context
+ ": cannot assign method value to type " + dst
.String())
2035 case directlyAssignable(dst
, v
.typ
):
2036 // Overwrite type so that they match.
2037 // Same memory layout, so no harm done.
2039 fl
:= v
.flag
& (flagRO | flagAddr | flagIndir
)
2040 fl |
= flag(dst
.Kind()) << flagKindShift
2041 return Value
{dst
, v
.val
, fl
}
2043 case implements(dst
, v
.typ
):
2045 target
= new(interface{})
2047 x
:= valueInterface(v
, false)
2048 if dst
.NumMethod() == 0 {
2051 ifaceE2I(dst
, x
, unsafe
.Pointer(target
))
2053 return Value
{dst
, unsafe
.Pointer(target
), flagIndir |
flag(Interface
)<<flagKindShift
}
2057 panic(context
+ ": value of type " + v
.typ
.String() + " is not assignable to type " + dst
.String())
2060 // Convert returns the value v converted to type t.
2061 // If the usual Go conversion rules do not allow conversion
2062 // of the value v to type t, Convert panics.
2063 func (v Value
) Convert(t Type
) Value
{
2064 if v
.flag
&flagMethod
!= 0 {
2065 panic("reflect.Value.Convert: cannot convert method values")
2067 op
:= convertOp(t
.common(), v
.typ
)
2069 panic("reflect.Value.Convert: value of type " + v
.typ
.String() + " cannot be converted to type " + t
.String())
2074 // convertOp returns the function to convert a value of type src
2075 // to a value of type dst. If the conversion is illegal, convertOp returns nil.
2076 func convertOp(dst
, src
*rtype
) func(Value
, Type
) Value
{
2078 case Int
, Int8
, Int16
, Int32
, Int64
:
2080 case Int
, Int8
, Int16
, Int32
, Int64
, Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2082 case Float32
, Float64
:
2088 case Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2090 case Int
, Int8
, Int16
, Int32
, Int64
, Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2092 case Float32
, Float64
:
2095 return cvtUintString
2098 case Float32
, Float64
:
2100 case Int
, Int8
, Int16
, Int32
, Int64
:
2102 case Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2104 case Float32
, Float64
:
2108 case Complex64
, Complex128
:
2110 case Complex64
, Complex128
:
2115 if dst
.Kind() == Slice
&& dst
.Elem().PkgPath() == "" {
2116 switch dst
.Elem().Kind() {
2118 return cvtStringBytes
2120 return cvtStringRunes
2125 if dst
.Kind() == String
&& src
.Elem().PkgPath() == "" {
2126 switch src
.Elem().Kind() {
2128 return cvtBytesString
2130 return cvtRunesString
2135 // dst and src have same underlying type.
2136 if haveIdenticalUnderlyingType(dst
, src
) {
2140 // dst and src are unnamed pointer types with same underlying base type.
2141 if dst
.Kind() == Ptr
&& dst
.Name() == "" &&
2142 src
.Kind() == Ptr
&& src
.Name() == "" &&
2143 haveIdenticalUnderlyingType(dst
.Elem().common(), src
.Elem().common()) {
2147 if implements(dst
, src
) {
2148 if src
.Kind() == Interface
{
2157 // makeInt returns a Value of type t equal to bits (possibly truncated),
2158 // where t is a signed or unsigned int type.
2159 func makeInt(f flag
, bits
uint64, t Type
) Value
{
2161 if typ
.size
> ptrSize
{
2162 // Assume ptrSize >= 4, so this must be uint64.
2163 ptr
:= unsafe_New(typ
)
2164 *(*uint64)(unsafe
.Pointer(ptr
)) = bits
2165 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2170 *(*uint8)(unsafe
.Pointer(&w
)) = uint8(bits
)
2172 *(*uint16)(unsafe
.Pointer(&w
)) = uint16(bits
)
2174 *(*uint32)(unsafe
.Pointer(&w
)) = uint32(bits
)
2176 *(*uint64)(unsafe
.Pointer(&w
)) = uint64(bits
)
2178 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2181 // makeFloat returns a Value of type t equal to v (possibly truncated to float32),
2182 // where t is a float32 or float64 type.
2183 func makeFloat(f flag
, v
float64, t Type
) Value
{
2185 if typ
.size
> ptrSize
{
2186 // Assume ptrSize >= 4, so this must be float64.
2187 ptr
:= unsafe_New(typ
)
2188 *(*float64)(unsafe
.Pointer(ptr
)) = v
2189 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2195 *(*float32)(unsafe
.Pointer(&w
)) = float32(v
)
2197 *(*float64)(unsafe
.Pointer(&w
)) = v
2199 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2202 // makeComplex returns a Value of type t equal to v (possibly truncated to complex64),
2203 // where t is a complex64 or complex128 type.
2204 func makeComplex(f flag
, v complex128
, t Type
) Value
{
2206 if typ
.size
> ptrSize
{
2207 ptr
:= unsafe_New(typ
)
2210 *(*complex64
)(unsafe
.Pointer(ptr
)) = complex64(v
)
2212 *(*complex128
)(unsafe
.Pointer(ptr
)) = v
2214 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2217 // Assume ptrSize <= 8 so this must be complex64.
2219 *(*complex64
)(unsafe
.Pointer(&w
)) = complex64(v
)
2220 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2223 func makeString(f flag
, v
string, t Type
) Value
{
2224 ret
:= New(t
).Elem()
2226 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2230 func makeBytes(f flag
, v
[]byte, t Type
) Value
{
2231 ret
:= New(t
).Elem()
2233 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2237 func makeRunes(f flag
, v
[]rune
, t Type
) Value
{
2238 ret
:= New(t
).Elem()
2240 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2244 // These conversion functions are returned by convertOp
2245 // for classes of conversions. For example, the first function, cvtInt,
2246 // takes any value v of signed int type and returns the value converted
2247 // to type t, where t is any signed or unsigned int type.
2249 // convertOp: intXX -> [u]intXX
2250 func cvtInt(v Value
, t Type
) Value
{
2251 return makeInt(v
.flag
&flagRO
, uint64(v
.Int()), t
)
2254 // convertOp: uintXX -> [u]intXX
2255 func cvtUint(v Value
, t Type
) Value
{
2256 return makeInt(v
.flag
&flagRO
, v
.Uint(), t
)
2259 // convertOp: floatXX -> intXX
2260 func cvtFloatInt(v Value
, t Type
) Value
{
2261 return makeInt(v
.flag
&flagRO
, uint64(int64(v
.Float())), t
)
2264 // convertOp: floatXX -> uintXX
2265 func cvtFloatUint(v Value
, t Type
) Value
{
2266 return makeInt(v
.flag
&flagRO
, uint64(v
.Float()), t
)
2269 // convertOp: intXX -> floatXX
2270 func cvtIntFloat(v Value
, t Type
) Value
{
2271 return makeFloat(v
.flag
&flagRO
, float64(v
.Int()), t
)
2274 // convertOp: uintXX -> floatXX
2275 func cvtUintFloat(v Value
, t Type
) Value
{
2276 return makeFloat(v
.flag
&flagRO
, float64(v
.Uint()), t
)
2279 // convertOp: floatXX -> floatXX
2280 func cvtFloat(v Value
, t Type
) Value
{
2281 return makeFloat(v
.flag
&flagRO
, v
.Float(), t
)
2284 // convertOp: complexXX -> complexXX
2285 func cvtComplex(v Value
, t Type
) Value
{
2286 return makeComplex(v
.flag
&flagRO
, v
.Complex(), t
)
2289 // convertOp: intXX -> string
2290 func cvtIntString(v Value
, t Type
) Value
{
2291 return makeString(v
.flag
&flagRO
, string(v
.Int()), t
)
2294 // convertOp: uintXX -> string
2295 func cvtUintString(v Value
, t Type
) Value
{
2296 return makeString(v
.flag
&flagRO
, string(v
.Uint()), t
)
2299 // convertOp: []byte -> string
2300 func cvtBytesString(v Value
, t Type
) Value
{
2301 return makeString(v
.flag
&flagRO
, string(v
.Bytes()), t
)
2304 // convertOp: string -> []byte
2305 func cvtStringBytes(v Value
, t Type
) Value
{
2306 return makeBytes(v
.flag
&flagRO
, []byte(v
.String()), t
)
2309 // convertOp: []rune -> string
2310 func cvtRunesString(v Value
, t Type
) Value
{
2311 return makeString(v
.flag
&flagRO
, string(v
.runes()), t
)
2314 // convertOp: string -> []rune
2315 func cvtStringRunes(v Value
, t Type
) Value
{
2316 return makeRunes(v
.flag
&flagRO
, []rune(v
.String()), t
)
2319 // convertOp: direct copy
2320 func cvtDirect(v Value
, typ Type
) Value
{
2324 if f
&flagAddr
!= 0 {
2325 // indirect, mutable word - make a copy
2326 ptr
:= unsafe_New(t
)
2327 memmove(ptr
, val
, t
.size
)
2331 return Value
{t
, val
, v
.flag
&flagRO | f
}
2334 // convertOp: concrete -> interface
2335 func cvtT2I(v Value
, typ Type
) Value
{
2336 target
:= new(interface{})
2337 x
:= valueInterface(v
, false)
2338 if typ
.NumMethod() == 0 {
2341 ifaceE2I(typ
.(*rtype
), x
, unsafe
.Pointer(target
))
2343 return Value
{typ
.common(), unsafe
.Pointer(target
), v
.flag
&flagRO | flagIndir |
flag(Interface
)<<flagKindShift
}
2346 // convertOp: interface -> interface
2347 func cvtI2I(v Value
, typ Type
) Value
{
2350 ret
.flag |
= v
.flag
& flagRO
2353 return cvtT2I(v
.Elem(), typ
)
2356 // implemented in ../pkg/runtime
2357 func chancap(ch iword
) int
2358 func chanclose(ch iword
)
2359 func chanlen(ch iword
) int
2360 func chanrecv(t
*rtype
, ch iword
, nb
bool) (val iword
, selected
, received
bool)
2361 func chansend(t
*rtype
, ch iword
, val iword
, nb
bool) bool
2363 func makechan(typ
*rtype
, size
uint64) (ch iword
)
2364 func makemap(t
*rtype
) (m iword
)
2365 func mapaccess(t
*rtype
, m iword
, key iword
) (val iword
, ok
bool)
2366 func mapassign(t
*rtype
, m iword
, key
, val iword
, ok
bool)
2367 func mapiterinit(t
*rtype
, m iword
) *byte
2368 func mapiterkey(it
*byte) (key iword
, ok
bool)
2369 func mapiternext(it
*byte)
2370 func maplen(m iword
) int
2372 func call(typ
*rtype
, fnaddr unsafe
.Pointer
, isInterface
bool, isMethod
bool, params
*unsafe
.Pointer
, results
*unsafe
.Pointer
)
2373 func ifaceE2I(t
*rtype
, src
interface{}, dst unsafe
.Pointer
)
2375 // Dummy annotation marking that the value x escapes,
2376 // for use in cases where the reflect code is so clever that
2377 // the compiler cannot follow.
2378 func escapes(x
interface{}) {