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("reflect: " + 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("reflect: " + methodName() + " using value obtained using unexported field")
264 panic("reflect: " + 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(op
string, in
[]Value
) []Value
{
358 // Get function pointer, type.
364 if v
.flag
&flagMethod
!= 0 {
365 t
, fn
, rcvr
= methodReceiver(op
, v
, int(v
.flag
)>>flagMethodShift
)
366 } else if v
.flag
&flagIndir
!= 0 {
367 fn
= *(*unsafe
.Pointer
)(v
.val
)
373 panic("reflect.Value.Call: call of nil function")
376 isSlice
:= op
== "CallSlice"
380 panic("reflect: CallSlice of non-variadic function")
383 panic("reflect: CallSlice with too few input arguments")
386 panic("reflect: CallSlice with too many input arguments")
393 panic("reflect: Call with too few input arguments")
395 if !t
.IsVariadic() && len(in
) > n
{
396 panic("reflect: Call with too many input arguments")
399 for _
, x
:= range in
{
400 if x
.Kind() == Invalid
{
401 panic("reflect: " + op
+ " using zero Value argument")
404 for i
:= 0; i
< n
; i
++ {
405 if xt
, targ
:= in
[i
].Type(), t
.In(i
); !xt
.AssignableTo(targ
) {
406 panic("reflect: " + op
+ " using " + xt
.String() + " as type " + targ
.String())
409 if !isSlice
&& t
.IsVariadic() {
410 // prepare slice for remaining values
412 slice
:= MakeSlice(t
.In(n
), m
, m
)
413 elem
:= t
.In(n
).Elem()
414 for i
:= 0; i
< m
; i
++ {
416 if xt
:= x
.Type(); !xt
.AssignableTo(elem
) {
417 panic("reflect: cannot use " + xt
.String() + " as type " + elem
.String() + " in " + op
)
419 slice
.Index(i
).Set(x
)
422 in
= make([]Value
, n
+1)
428 if nin
!= t
.NumIn() {
429 panic("reflect.Value.Call: wrong argument count")
433 if v
.flag
&flagMethod
!= 0 {
436 firstPointer
:= len(in
) > 0 && t
.In(0).Kind() != Ptr
&& v
.flag
&flagMethod
== 0 && isMethod(v
.typ
)
437 params
:= make([]unsafe
.Pointer
, nin
)
439 if v
.flag
&flagMethod
!= 0 {
440 // Hard-wired first argument.
443 params
[0] = unsafe
.Pointer(p
)
446 for i
, pv
:= range in
{
448 targ
:= t
.In(i
).(*rtype
)
449 pv
= pv
.assignTo("reflect.Value.Call", targ
, nil)
450 if pv
.flag
&flagIndir
== 0 {
451 p
:= new(unsafe
.Pointer
)
453 params
[off
] = unsafe
.Pointer(p
)
457 if i
== 0 && firstPointer
{
458 p
:= new(unsafe
.Pointer
)
460 params
[off
] = unsafe
.Pointer(p
)
465 ret
:= make([]Value
, nout
)
466 results
:= make([]unsafe
.Pointer
, nout
)
467 for i
:= 0; i
< nout
; i
++ {
469 results
[i
] = unsafe
.Pointer(v
.Pointer())
473 var pp
*unsafe
.Pointer
477 var pr
*unsafe
.Pointer
478 if len(results
) > 0 {
482 call(t
, fn
, v
.flag
&flagMethod
!= 0, firstPointer
, pp
, pr
)
487 // gccgo specific test to see if typ is a method. We can tell by
488 // looking at the string to see if there is a receiver. We need this
489 // because for gccgo all methods take pointer receivers.
490 func isMethod(t
*rtype
) bool {
491 if Kind(t
.kind
) != Func
{
498 for i
, c
:= range s
{
506 } else if parens
== 0 && c
== ' ' && s
[i
+1] != '(' && !sawRet
{
514 // methodReceiver returns information about the receiver
515 // described by v. The Value v may or may not have the
516 // flagMethod bit set, so the kind cached in v.flag should
518 func methodReceiver(op
string, v Value
, methodIndex
int) (t
*rtype
, fn unsafe
.Pointer
, rcvr iword
) {
520 if v
.typ
.Kind() == Interface
{
521 tt
:= (*interfaceType
)(unsafe
.Pointer(v
.typ
))
522 if i
< 0 || i
>= len(tt
.methods
) {
523 panic("reflect: internal error: invalid method index")
526 if m
.pkgPath
!= nil {
527 panic("reflect: " + op
+ " of unexported method")
530 iface
:= (*nonEmptyInterface
)(v
.val
)
531 if iface
.itab
== nil {
532 panic("reflect: " + op
+ " of method on nil interface value")
534 fn
= unsafe
.Pointer(&iface
.itab
.fun
[i
])
537 ut
:= v
.typ
.uncommon()
538 if ut
== nil || i
< 0 || i
>= len(ut
.methods
) {
539 panic("reflect: internal error: invalid method index")
542 if m
.pkgPath
!= nil {
543 panic("reflect: " + op
+ " of unexported method")
545 fn
= unsafe
.Pointer(&m
.tfn
)
547 // Can't call iword here, because it checks v.kind,
548 // and that is always Func.
549 if v
.flag
&flagIndir
!= 0 && (v
.typ
.Kind() == Ptr || v
.typ
.Kind() == UnsafePointer
) {
550 rcvr
= loadIword(v
.val
, v
.typ
.size
)
558 // align returns the result of rounding x up to a multiple of n.
559 // n must be a power of two.
560 func align(x
, n
uintptr) uintptr {
561 return (x
+ n
- 1) &^ (n
- 1)
564 // funcName returns the name of f, for use in error messages.
565 func funcName(f
func([]Value
) []Value
) string {
566 pc
:= *(*uintptr)(unsafe
.Pointer(&f
))
567 rf
:= runtime
.FuncForPC(pc
)
574 // Cap returns v's capacity.
575 // It panics if v's Kind is not Array, Chan, or Slice.
576 func (v Value
) Cap() int {
582 return int(chancap(*(*iword
)(v
.iword())))
584 // Slice is always bigger than a word; assume flagIndir.
585 return (*SliceHeader
)(v
.val
).Cap
587 panic(&ValueError
{"reflect.Value.Cap", k
})
590 // Close closes the channel v.
591 // It panics if v's Kind is not Chan.
592 func (v Value
) Close() {
595 chanclose(*(*iword
)(v
.iword()))
598 // Complex returns v's underlying value, as a complex128.
599 // It panics if v's Kind is not Complex64 or Complex128
600 func (v Value
) Complex() complex128
{
604 if v
.flag
&flagIndir
!= 0 {
605 return complex128(*(*complex64
)(v
.val
))
607 return complex128(*(*complex64
)(unsafe
.Pointer(&v
.val
)))
609 // complex128 is always bigger than a word; assume flagIndir.
610 return *(*complex128
)(v
.val
)
612 panic(&ValueError
{"reflect.Value.Complex", k
})
615 // Elem returns the value that the interface v contains
616 // or that the pointer v points to.
617 // It panics if v's Kind is not Interface or Ptr.
618 // It returns the zero Value if v is nil.
619 func (v Value
) Elem() Value
{
627 if v
.typ
.NumMethod() == 0 {
628 eface
:= (*emptyInterface
)(v
.val
)
629 if eface
.typ
== nil {
630 // nil interface value
634 val
= unsafe
.Pointer(eface
.word
)
636 iface
:= (*nonEmptyInterface
)(v
.val
)
637 if iface
.itab
== nil {
638 // nil interface value
642 val
= unsafe
.Pointer(iface
.word
)
644 fl
:= v
.flag
& flagRO
645 fl |
= flag(typ
.Kind()) << flagKindShift
646 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
649 return Value
{typ
, val
, fl
}
653 if v
.flag
&flagIndir
!= 0 {
654 val
= *(*unsafe
.Pointer
)(val
)
656 // The returned value's address is v's value.
660 tt
:= (*ptrType
)(unsafe
.Pointer(v
.typ
))
662 fl
:= v
.flag
&flagRO | flagIndir | flagAddr
663 fl |
= flag(typ
.Kind() << flagKindShift
)
664 return Value
{typ
, val
, fl
}
666 panic(&ValueError
{"reflect.Value.Elem", k
})
669 // Field returns the i'th field of the struct v.
670 // It panics if v's Kind is not Struct or i is out of range.
671 func (v Value
) Field(i
int) Value
{
673 tt
:= (*structType
)(unsafe
.Pointer(v
.typ
))
674 if i
< 0 || i
>= len(tt
.fields
) {
675 panic("reflect: Field index out of range")
677 field
:= &tt
.fields
[i
]
680 // Inherit permission bits from v.
681 fl
:= v
.flag
& (flagRO | flagIndir | flagAddr
)
682 // Using an unexported field forces flagRO.
683 if field
.pkgPath
!= nil {
686 fl |
= flag(typ
.Kind()) << flagKindShift
688 var val unsafe
.Pointer
690 case fl
&flagIndir
!= 0:
691 // Indirect. Just bump pointer.
692 val
= unsafe
.Pointer(uintptr(v
.val
) + field
.offset
)
694 // Direct. Discard leading bytes.
695 val
= unsafe
.Pointer(uintptr(v
.val
) << (field
.offset
* 8))
697 // Direct. Discard leading bytes.
698 val
= unsafe
.Pointer(uintptr(v
.val
) >> (field
.offset
* 8))
701 return Value
{typ
, val
, fl
}
704 // FieldByIndex returns the nested field corresponding to index.
705 // It panics if v's Kind is not struct.
706 func (v Value
) FieldByIndex(index
[]int) Value
{
708 for i
, x
:= range index
{
710 if v
.Kind() == Ptr
&& v
.Elem().Kind() == Struct
{
719 // FieldByName returns the struct field with the given name.
720 // It returns the zero Value if no field was found.
721 // It panics if v's Kind is not struct.
722 func (v Value
) FieldByName(name
string) Value
{
724 if f
, ok
:= v
.typ
.FieldByName(name
); ok
{
725 return v
.FieldByIndex(f
.Index
)
730 // FieldByNameFunc returns the struct field with a name
731 // that satisfies the match function.
732 // It panics if v's Kind is not struct.
733 // It returns the zero Value if no field was found.
734 func (v Value
) FieldByNameFunc(match
func(string) bool) Value
{
736 if f
, ok
:= v
.typ
.FieldByNameFunc(match
); ok
{
737 return v
.FieldByIndex(f
.Index
)
742 // Float returns v's underlying value, as a float64.
743 // It panics if v's Kind is not Float32 or Float64
744 func (v Value
) Float() float64 {
748 if v
.flag
&flagIndir
!= 0 {
749 return float64(*(*float32)(v
.val
))
751 return float64(*(*float32)(unsafe
.Pointer(&v
.val
)))
753 if v
.flag
&flagIndir
!= 0 {
754 return *(*float64)(v
.val
)
756 return *(*float64)(unsafe
.Pointer(&v
.val
))
758 panic(&ValueError
{"reflect.Value.Float", k
})
761 var uint8Type
= TypeOf(uint8(0)).(*rtype
)
763 // Index returns v's i'th element.
764 // It panics if v's Kind is not Array, Slice, or String or i is out of range.
765 func (v Value
) Index(i
int) Value
{
769 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
770 if i
< 0 || i
> int(tt
.len) {
771 panic("reflect: array index out of range")
774 fl
:= v
.flag
& (flagRO | flagIndir | flagAddr
) // bits same as overall array
775 fl |
= flag(typ
.Kind()) << flagKindShift
776 offset
:= uintptr(i
) * typ
.size
778 var val unsafe
.Pointer
780 case fl
&flagIndir
!= 0:
781 // Indirect. Just bump pointer.
782 val
= unsafe
.Pointer(uintptr(v
.val
) + offset
)
784 // Direct. Discard leading bytes.
785 val
= unsafe
.Pointer(uintptr(v
.val
) << (offset
* 8))
787 // Direct. Discard leading bytes.
788 val
= unsafe
.Pointer(uintptr(v
.val
) >> (offset
* 8))
790 return Value
{typ
, val
, fl
}
793 // Element flag same as Elem of Ptr.
794 // Addressable, indirect, possibly read-only.
795 fl
:= flagAddr | flagIndir | v
.flag
&flagRO
796 s
:= (*SliceHeader
)(v
.val
)
797 if i
< 0 || i
>= s
.Len
{
798 panic("reflect: slice index out of range")
800 tt
:= (*sliceType
)(unsafe
.Pointer(v
.typ
))
802 fl |
= flag(typ
.Kind()) << flagKindShift
803 val
:= unsafe
.Pointer(s
.Data
+ uintptr(i
)*typ
.size
)
804 return Value
{typ
, val
, fl
}
807 fl
:= v
.flag
&flagRO |
flag(Uint8
<<flagKindShift
) | flagIndir
808 s
:= (*StringHeader
)(v
.val
)
809 if i
< 0 || i
>= s
.Len
{
810 panic("reflect: string index out of range")
812 val
:= *(*byte)(unsafe
.Pointer(s
.Data
+ uintptr(i
)))
813 return Value
{uint8Type
, unsafe
.Pointer(&val
), fl
}
815 panic(&ValueError
{"reflect.Value.Index", k
})
818 // Int returns v's underlying value, as an int64.
819 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
820 func (v Value
) Int() int64 {
823 if v
.flag
&flagIndir
!= 0 {
826 // The escape analysis is good enough that &v.val
827 // does not trigger a heap allocation.
828 p
= unsafe
.Pointer(&v
.val
)
832 return int64(*(*int)(p
))
834 return int64(*(*int8)(p
))
836 return int64(*(*int16)(p
))
838 return int64(*(*int32)(p
))
840 return int64(*(*int64)(p
))
842 panic(&ValueError
{"reflect.Value.Int", k
})
845 // CanInterface returns true if Interface can be used without panicking.
846 func (v Value
) CanInterface() bool {
848 panic(&ValueError
{"reflect.Value.CanInterface", Invalid
})
850 return v
.flag
&flagRO
== 0
853 // Interface returns v's current value as an interface{}.
854 // It is equivalent to:
855 // var i interface{} = (v's underlying value)
856 // It panics if the Value was obtained by accessing
857 // unexported struct fields.
858 func (v Value
) Interface() (i
interface{}) {
859 return valueInterface(v
, true)
862 func valueInterface(v Value
, safe
bool) interface{} {
864 panic(&ValueError
{"reflect.Value.Interface", 0})
866 if safe
&& v
.flag
&flagRO
!= 0 {
867 // Do not allow access to unexported values via Interface,
868 // because they might be pointers that should not be
869 // writable or methods or function that should not be callable.
870 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
872 if v
.flag
&flagMethod
!= 0 {
873 v
= makeMethodValue("Interface", v
)
878 // Special case: return the element inside the interface.
879 // Empty interface has one layout, all interfaces with
880 // methods have a second layout.
881 if v
.NumMethod() == 0 {
882 return *(*interface{})(v
.val
)
884 return *(*interface {
889 // Non-interface value.
890 var eface emptyInterface
891 eface
.typ
= toType(v
.typ
).common()
892 eface
.word
= v
.iword()
894 // Don't need to allocate if v is not addressable or fits in one word.
895 if v
.flag
&flagAddr
!= 0 && v
.kind() != Ptr
&& v
.kind() != UnsafePointer
{
896 // eface.word is a pointer to the actual data,
897 // which might be changed. We need to return
898 // a pointer to unchanging data, so make a copy.
899 ptr
:= unsafe_New(v
.typ
)
900 memmove(ptr
, unsafe
.Pointer(eface
.word
), v
.typ
.size
)
901 eface
.word
= iword(ptr
)
904 if v
.flag
&flagIndir
== 0 && v
.kind() != Ptr
&& v
.kind() != UnsafePointer
{
905 panic("missing flagIndir")
908 return *(*interface{})(unsafe
.Pointer(&eface
))
911 // InterfaceData returns the interface v's value as a uintptr pair.
912 // It panics if v's Kind is not Interface.
913 func (v Value
) InterfaceData() [2]uintptr {
915 // We treat this as a read operation, so we allow
916 // it even for unexported data, because the caller
917 // has to import "unsafe" to turn it into something
918 // that can be abused.
919 // Interface value is always bigger than a word; assume flagIndir.
920 return *(*[2]uintptr)(v
.val
)
923 // IsNil returns true if v is a nil value.
924 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
925 func (v Value
) IsNil() bool {
928 case Chan
, Func
, Map
, Ptr
:
929 if v
.flag
&flagMethod
!= 0 {
933 if v
.flag
&flagIndir
!= 0 {
934 ptr
= *(*unsafe
.Pointer
)(ptr
)
937 case Interface
, Slice
:
938 // Both interface and slice are nil if first word is 0.
939 // Both are always bigger than a word; assume flagIndir.
940 return *(*unsafe
.Pointer
)(v
.val
) == nil
942 panic(&ValueError
{"reflect.Value.IsNil", k
})
945 // IsValid returns true if v represents a value.
946 // It returns false if v is the zero Value.
947 // If IsValid returns false, all other methods except String panic.
948 // Most functions and methods never return an invalid value.
949 // If one does, its documentation states the conditions explicitly.
950 func (v Value
) IsValid() bool {
954 // Kind returns v's Kind.
955 // If v is the zero Value (IsValid returns false), Kind returns Invalid.
956 func (v Value
) Kind() Kind
{
960 // Len returns v's length.
961 // It panics if v's Kind is not Array, Chan, Map, Slice, or String.
962 func (v Value
) Len() int {
966 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
969 return chanlen(*(*iword
)(v
.iword()))
971 return maplen(*(*iword
)(v
.iword()))
973 // Slice is bigger than a word; assume flagIndir.
974 return (*SliceHeader
)(v
.val
).Len
976 // String is bigger than a word; assume flagIndir.
977 return (*StringHeader
)(v
.val
).Len
979 panic(&ValueError
{"reflect.Value.Len", k
})
982 // MapIndex returns the value associated with key in the map v.
983 // It panics if v's Kind is not Map.
984 // It returns the zero Value if key is not found in the map or if v represents a nil map.
985 // As in Go, the key's value must be assignable to the map's key type.
986 func (v Value
) MapIndex(key Value
) Value
{
988 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
990 // Do not require key to be exported, so that DeepEqual
991 // and other programs can use all the keys returned by
992 // MapKeys as arguments to MapIndex. If either the map
993 // or the key is unexported, though, the result will be
994 // considered unexported. This is consistent with the
995 // behavior for structs, which allow read but not write
996 // of unexported fields.
997 key
= key
.assignTo("reflect.Value.MapIndex", tt
.key
, nil)
999 word
, ok
:= mapaccess(v
.typ
, *(*iword
)(v
.iword()), key
.iword())
1004 fl
:= (v
.flag | key
.flag
) & flagRO
1005 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1008 fl |
= flag(typ
.Kind()) << flagKindShift
1009 return Value
{typ
, unsafe
.Pointer(word
), fl
}
1012 // MapKeys returns a slice containing all the keys present in the map,
1013 // in unspecified order.
1014 // It panics if v's Kind is not Map.
1015 // It returns an empty slice if v represents a nil map.
1016 func (v Value
) MapKeys() []Value
{
1018 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
1021 fl
:= v
.flag
& flagRO
1022 fl |
= flag(keyType
.Kind()) << flagKindShift
1023 if keyType
.Kind() != Ptr
&& keyType
.Kind() != UnsafePointer
{
1027 m
:= *(*iword
)(v
.iword())
1032 it
:= mapiterinit(v
.typ
, m
)
1033 a
:= make([]Value
, mlen
)
1035 for i
= 0; i
< len(a
); i
++ {
1036 keyWord
, ok
:= mapiterkey(it
)
1040 a
[i
] = Value
{keyType
, unsafe
.Pointer(keyWord
), fl
}
1046 // Method returns a function value corresponding to v's i'th method.
1047 // The arguments to a Call on the returned function should not include
1048 // a receiver; the returned function will always use v as the receiver.
1049 // Method panics if i is out of range or if v is a nil interface value.
1050 func (v Value
) Method(i
int) Value
{
1052 panic(&ValueError
{"reflect.Value.Method", Invalid
})
1054 if v
.flag
&flagMethod
!= 0 || i
< 0 || i
>= v
.typ
.NumMethod() {
1055 panic("reflect: Method index out of range")
1057 if v
.typ
.Kind() == Interface
&& v
.IsNil() {
1058 panic("reflect: Method on nil interface value")
1060 fl
:= v
.flag
& (flagRO | flagIndir
)
1061 fl |
= flag(Func
) << flagKindShift
1062 fl |
= flag(i
)<<flagMethodShift | flagMethod
1063 return Value
{v
.typ
, v
.val
, fl
}
1066 // NumMethod returns the number of methods in the value's method set.
1067 func (v Value
) NumMethod() int {
1069 panic(&ValueError
{"reflect.Value.NumMethod", Invalid
})
1071 if v
.flag
&flagMethod
!= 0 {
1074 return v
.typ
.NumMethod()
1077 // MethodByName returns a function value corresponding to the method
1078 // of v with the given name.
1079 // The arguments to a Call on the returned function should not include
1080 // a receiver; the returned function will always use v as the receiver.
1081 // It returns the zero Value if no method was found.
1082 func (v Value
) MethodByName(name
string) Value
{
1084 panic(&ValueError
{"reflect.Value.MethodByName", Invalid
})
1086 if v
.flag
&flagMethod
!= 0 {
1089 m
, ok
:= v
.typ
.MethodByName(name
)
1093 return v
.Method(m
.Index
)
1096 // NumField returns the number of fields in the struct v.
1097 // It panics if v's Kind is not Struct.
1098 func (v Value
) NumField() int {
1100 tt
:= (*structType
)(unsafe
.Pointer(v
.typ
))
1101 return len(tt
.fields
)
1104 // OverflowComplex returns true if the complex128 x cannot be represented by v's type.
1105 // It panics if v's Kind is not Complex64 or Complex128.
1106 func (v Value
) OverflowComplex(x complex128
) bool {
1110 return overflowFloat32(real(x
)) ||
overflowFloat32(imag(x
))
1114 panic(&ValueError
{"reflect.Value.OverflowComplex", k
})
1117 // OverflowFloat returns true if the float64 x cannot be represented by v's type.
1118 // It panics if v's Kind is not Float32 or Float64.
1119 func (v Value
) OverflowFloat(x
float64) bool {
1123 return overflowFloat32(x
)
1127 panic(&ValueError
{"reflect.Value.OverflowFloat", k
})
1130 func overflowFloat32(x
float64) bool {
1134 return math
.MaxFloat32
< x
&& x
<= math
.MaxFloat64
1137 // OverflowInt returns true if the int64 x cannot be represented by v's type.
1138 // It panics if v's Kind is not Int, Int8, int16, Int32, or Int64.
1139 func (v Value
) OverflowInt(x
int64) bool {
1142 case Int
, Int8
, Int16
, Int32
, Int64
:
1143 bitSize
:= v
.typ
.size
* 8
1144 trunc
:= (x
<< (64 - bitSize
)) >> (64 - bitSize
)
1147 panic(&ValueError
{"reflect.Value.OverflowInt", k
})
1150 // OverflowUint returns true if the uint64 x cannot be represented by v's type.
1151 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1152 func (v Value
) OverflowUint(x
uint64) bool {
1155 case Uint
, Uintptr
, Uint8
, Uint16
, Uint32
, Uint64
:
1156 bitSize
:= v
.typ
.size
* 8
1157 trunc
:= (x
<< (64 - bitSize
)) >> (64 - bitSize
)
1160 panic(&ValueError
{"reflect.Value.OverflowUint", k
})
1163 // Pointer returns v's value as a uintptr.
1164 // It returns uintptr instead of unsafe.Pointer so that
1165 // code using reflect cannot obtain unsafe.Pointers
1166 // without importing the unsafe package explicitly.
1167 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
1169 // If v's Kind is Func, the returned pointer is an underlying
1170 // code pointer, but not necessarily enough to identify a
1171 // single function uniquely. The only guarantee is that the
1172 // result is zero if and only if v is a nil func Value.
1173 func (v Value
) Pointer() uintptr {
1176 case Chan
, Map
, Ptr
, UnsafePointer
:
1178 if v
.flag
&flagIndir
!= 0 {
1179 p
= *(*unsafe
.Pointer
)(p
)
1183 if v
.flag
&flagMethod
!= 0 {
1184 // As the doc comment says, the returned pointer is an
1185 // underlying code pointer but not necessarily enough to
1186 // identify a single function uniquely. All method expressions
1187 // created via reflect have the same underlying code pointer,
1188 // so their Pointers are equal. The function used here must
1189 // match the one used in makeMethodValue.
1190 // This is not properly implemented for gccgo.
1192 return **(**uintptr)(unsafe
.Pointer(&f
))
1195 if v
.flag
&flagIndir
!= 0 {
1196 p
= *(*unsafe
.Pointer
)(p
)
1198 // Non-nil func value points at data block.
1199 // First word of data block is actual code.
1201 p
= *(*unsafe
.Pointer
)(p
)
1206 return (*SliceHeader
)(v
.val
).Data
1208 panic(&ValueError
{"reflect.Value.Pointer", k
})
1211 // Recv receives and returns a value from the channel v.
1212 // It panics if v's Kind is not Chan.
1213 // The receive blocks until a value is ready.
1214 // The boolean value ok is true if the value x corresponds to a send
1215 // on the channel, false if it is a zero value received because the channel is closed.
1216 func (v Value
) Recv() (x Value
, ok
bool) {
1219 return v
.recv(false)
1222 // internal recv, possibly non-blocking (nb).
1223 // v is known to be a channel.
1224 func (v Value
) recv(nb
bool) (val Value
, ok
bool) {
1225 tt
:= (*chanType
)(unsafe
.Pointer(v
.typ
))
1226 if ChanDir(tt
.dir
)&RecvDir
== 0 {
1227 panic("reflect: recv on send-only channel")
1229 word
, selected
, ok
:= chanrecv(v
.typ
, *(*iword
)(v
.iword()), nb
)
1232 fl
:= flag(typ
.Kind()) << flagKindShift
1233 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1236 val
= Value
{typ
, unsafe
.Pointer(word
), fl
}
1241 // Send sends x on the channel v.
1242 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
1243 // As in Go, x's value must be assignable to the channel's element type.
1244 func (v Value
) Send(x Value
) {
1250 // internal send, possibly non-blocking.
1251 // v is known to be a channel.
1252 func (v Value
) send(x Value
, nb
bool) (selected
bool) {
1253 tt
:= (*chanType
)(unsafe
.Pointer(v
.typ
))
1254 if ChanDir(tt
.dir
)&SendDir
== 0 {
1255 panic("reflect: send on recv-only channel")
1258 x
= x
.assignTo("reflect.Value.Send", tt
.elem
, nil)
1259 return chansend(v
.typ
, *(*iword
)(v
.iword()), x
.iword(), nb
)
1262 // Set assigns x to the value v.
1263 // It panics if CanSet returns false.
1264 // As in Go, x's value must be assignable to v's type.
1265 func (v Value
) Set(x Value
) {
1266 v
.mustBeAssignable()
1267 x
.mustBeExported() // do not let unexported x leak
1268 var target
*interface{}
1269 if v
.kind() == Interface
{
1270 target
= (*interface{})(v
.val
)
1272 x
= x
.assignTo("reflect.Set", v
.typ
, target
)
1273 if x
.flag
&flagIndir
!= 0 {
1274 memmove(v
.val
, x
.val
, v
.typ
.size
)
1276 storeIword(v
.val
, iword(x
.val
), v
.typ
.size
)
1280 // SetBool sets v's underlying value.
1281 // It panics if v's Kind is not Bool or if CanSet() is false.
1282 func (v Value
) SetBool(x
bool) {
1283 v
.mustBeAssignable()
1288 // SetBytes sets v's underlying value.
1289 // It panics if v's underlying value is not a slice of bytes.
1290 func (v Value
) SetBytes(x
[]byte) {
1291 v
.mustBeAssignable()
1293 if v
.typ
.Elem().Kind() != Uint8
{
1294 panic("reflect.Value.SetBytes of non-byte slice")
1296 *(*[]byte)(v
.val
) = x
1299 // setRunes sets v's underlying value.
1300 // It panics if v's underlying value is not a slice of runes (int32s).
1301 func (v Value
) setRunes(x
[]rune
) {
1302 v
.mustBeAssignable()
1304 if v
.typ
.Elem().Kind() != Int32
{
1305 panic("reflect.Value.setRunes of non-rune slice")
1307 *(*[]rune
)(v
.val
) = x
1310 // SetComplex sets v's underlying value to x.
1311 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
1312 func (v Value
) SetComplex(x complex128
) {
1313 v
.mustBeAssignable()
1314 switch k
:= v
.kind(); k
{
1316 panic(&ValueError
{"reflect.Value.SetComplex", k
})
1318 *(*complex64
)(v
.val
) = complex64(x
)
1320 *(*complex128
)(v
.val
) = x
1324 // SetFloat sets v's underlying value to x.
1325 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
1326 func (v Value
) SetFloat(x
float64) {
1327 v
.mustBeAssignable()
1328 switch k
:= v
.kind(); k
{
1330 panic(&ValueError
{"reflect.Value.SetFloat", k
})
1332 *(*float32)(v
.val
) = float32(x
)
1334 *(*float64)(v
.val
) = x
1338 // SetInt sets v's underlying value to x.
1339 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
1340 func (v Value
) SetInt(x
int64) {
1341 v
.mustBeAssignable()
1342 switch k
:= v
.kind(); k
{
1344 panic(&ValueError
{"reflect.Value.SetInt", k
})
1346 *(*int)(v
.val
) = int(x
)
1348 *(*int8)(v
.val
) = int8(x
)
1350 *(*int16)(v
.val
) = int16(x
)
1352 *(*int32)(v
.val
) = int32(x
)
1354 *(*int64)(v
.val
) = x
1358 // SetLen sets v's length to n.
1359 // It panics if v's Kind is not Slice or if n is negative or
1360 // greater than the capacity of the slice.
1361 func (v Value
) SetLen(n
int) {
1362 v
.mustBeAssignable()
1364 s
:= (*SliceHeader
)(v
.val
)
1365 if n
< 0 || n
> int(s
.Cap
) {
1366 panic("reflect: slice length out of range in SetLen")
1371 // SetCap sets v's capacity to n.
1372 // It panics if v's Kind is not Slice or if n is smaller than the length or
1373 // greater than the capacity of the slice.
1374 func (v Value
) SetCap(n
int) {
1375 v
.mustBeAssignable()
1377 s
:= (*SliceHeader
)(v
.val
)
1378 if n
< int(s
.Len
) || n
> int(s
.Cap
) {
1379 panic("reflect: slice capacity out of range in SetCap")
1384 // SetMapIndex sets the value associated with key in the map v to val.
1385 // It panics if v's Kind is not Map.
1386 // If val is the zero Value, SetMapIndex deletes the key from the map.
1387 // As in Go, key's value must be assignable to the map's key type,
1388 // and val's value must be assignable to the map's value type.
1389 func (v Value
) SetMapIndex(key
, val Value
) {
1392 key
.mustBeExported()
1393 tt
:= (*mapType
)(unsafe
.Pointer(v
.typ
))
1394 key
= key
.assignTo("reflect.Value.SetMapIndex", tt
.key
, nil)
1396 val
.mustBeExported()
1397 val
= val
.assignTo("reflect.Value.SetMapIndex", tt
.elem
, nil)
1399 mapassign(v
.typ
, *(*iword
)(v
.iword()), key
.iword(), val
.iword(), val
.typ
!= nil)
1402 // SetUint sets v's underlying value to x.
1403 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
1404 func (v Value
) SetUint(x
uint64) {
1405 v
.mustBeAssignable()
1406 switch k
:= v
.kind(); k
{
1408 panic(&ValueError
{"reflect.Value.SetUint", k
})
1410 *(*uint)(v
.val
) = uint(x
)
1412 *(*uint8)(v
.val
) = uint8(x
)
1414 *(*uint16)(v
.val
) = uint16(x
)
1416 *(*uint32)(v
.val
) = uint32(x
)
1418 *(*uint64)(v
.val
) = x
1420 *(*uintptr)(v
.val
) = uintptr(x
)
1424 // SetPointer sets the unsafe.Pointer value v to x.
1425 // It panics if v's Kind is not UnsafePointer.
1426 func (v Value
) SetPointer(x unsafe
.Pointer
) {
1427 v
.mustBeAssignable()
1428 v
.mustBe(UnsafePointer
)
1429 *(*unsafe
.Pointer
)(v
.val
) = x
1432 // SetString sets v's underlying value to x.
1433 // It panics if v's Kind is not String or if CanSet() is false.
1434 func (v Value
) SetString(x
string) {
1435 v
.mustBeAssignable()
1437 *(*string)(v
.val
) = x
1440 // Slice returns v[i:j].
1441 // It panics if v's Kind is not Array, Slice or String, or if v is an unaddressable array,
1442 // or if the indexes are out of bounds.
1443 func (v Value
) Slice(i
, j
int) Value
{
1449 switch kind
:= v
.kind(); kind
{
1451 panic(&ValueError
{"reflect.Value.Slice", kind
})
1454 if v
.flag
&flagAddr
== 0 {
1455 panic("reflect.Value.Slice: slice of unaddressable array")
1457 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
1459 typ
= (*sliceType
)(unsafe
.Pointer(tt
.slice
))
1463 typ
= (*sliceType
)(unsafe
.Pointer(v
.typ
))
1464 s
:= (*SliceHeader
)(v
.val
)
1465 base
= unsafe
.Pointer(s
.Data
)
1469 s
:= (*StringHeader
)(v
.val
)
1470 if i
< 0 || j
< i || j
> s
.Len
{
1471 panic("reflect.Value.Slice: string slice index out of bounds")
1474 val
:= (*StringHeader
)(unsafe
.Pointer(&x
))
1475 val
.Data
= s
.Data
+ uintptr(i
)
1477 return Value
{v
.typ
, unsafe
.Pointer(&x
), v
.flag
}
1480 if i
< 0 || j
< i || j
> cap {
1481 panic("reflect.Value.Slice: slice index out of bounds")
1484 // Declare slice so that gc can see the base pointer in it.
1485 var x
[]unsafe
.Pointer
1487 // Reinterpret as *SliceHeader to edit.
1488 s
:= (*SliceHeader
)(unsafe
.Pointer(&x
))
1489 s
.Data
= uintptr(base
) + uintptr(i
)*typ
.elem
.Size()
1493 fl
:= v
.flag
&flagRO | flagIndir |
flag(Slice
)<<flagKindShift
1494 return Value
{typ
.common(), unsafe
.Pointer(&x
), fl
}
1497 // Slice3 is the 3-index form of the slice operation: it returns v[i:j:k].
1498 // It panics if v's Kind is not Array or Slice, or if v is an unaddressable array,
1499 // or if the indexes are out of bounds.
1500 func (v Value
) Slice3(i
, j
, k
int) Value
{
1506 switch kind
:= v
.kind(); kind
{
1508 panic(&ValueError
{"reflect.Value.Slice3", kind
})
1511 if v
.flag
&flagAddr
== 0 {
1512 panic("reflect.Value.Slice: slice of unaddressable array")
1514 tt
:= (*arrayType
)(unsafe
.Pointer(v
.typ
))
1516 typ
= (*sliceType
)(unsafe
.Pointer(tt
.slice
))
1520 typ
= (*sliceType
)(unsafe
.Pointer(v
.typ
))
1521 s
:= (*SliceHeader
)(v
.val
)
1522 base
= unsafe
.Pointer(s
.Data
)
1526 if i
< 0 || j
< i || k
< j || k
> cap {
1527 panic("reflect.Value.Slice3: slice index out of bounds")
1530 // Declare slice so that the garbage collector
1531 // can see the base pointer in it.
1532 var x
[]unsafe
.Pointer
1534 // Reinterpret as *SliceHeader to edit.
1535 s
:= (*SliceHeader
)(unsafe
.Pointer(&x
))
1536 s
.Data
= uintptr(base
) + uintptr(i
)*typ
.elem
.Size()
1540 fl
:= v
.flag
&flagRO | flagIndir |
flag(Slice
)<<flagKindShift
1541 return Value
{typ
.common(), unsafe
.Pointer(&x
), fl
}
1544 // String returns the string v's underlying value, as a string.
1545 // String is a special case because of Go's String method convention.
1546 // Unlike the other getters, it does not panic if v's Kind is not String.
1547 // Instead, it returns a string of the form "<T value>" where T is v's type.
1548 func (v Value
) String() string {
1549 switch k
:= v
.kind(); k
{
1551 return "<invalid Value>"
1553 return *(*string)(v
.val
)
1555 // If you call String on a reflect.Value of other type, it's better to
1556 // print something than to panic. Useful in debugging.
1557 return "<" + v
.typ
.String() + " Value>"
1560 // TryRecv attempts to receive a value from the channel v but will not block.
1561 // It panics if v's Kind is not Chan.
1562 // If the receive cannot finish without blocking, x is the zero Value.
1563 // The boolean ok is true if the value x corresponds to a send
1564 // on the channel, false if it is a zero value received because the channel is closed.
1565 func (v Value
) TryRecv() (x Value
, ok
bool) {
1571 // TrySend attempts to send x on the channel v but will not block.
1572 // It panics if v's Kind is not Chan.
1573 // It returns true if the value was sent, false otherwise.
1574 // As in Go, x's value must be assignable to the channel's element type.
1575 func (v Value
) TrySend(x Value
) bool {
1578 return v
.send(x
, true)
1581 // Type returns v's type.
1582 func (v Value
) Type() Type
{
1585 panic(&ValueError
{"reflect.Value.Type", Invalid
})
1587 if f
&flagMethod
== 0 {
1589 return toType(v
.typ
)
1593 // v.typ describes the receiver, not the method type.
1594 i
:= int(v
.flag
) >> flagMethodShift
1595 if v
.typ
.Kind() == Interface
{
1596 // Method on interface.
1597 tt
:= (*interfaceType
)(unsafe
.Pointer(v
.typ
))
1598 if i
< 0 || i
>= len(tt
.methods
) {
1599 panic("reflect: internal error: invalid method index")
1602 return toType(m
.typ
)
1604 // Method on concrete type.
1605 ut
:= v
.typ
.uncommon()
1606 if ut
== nil || i
< 0 || i
>= len(ut
.methods
) {
1607 panic("reflect: internal error: invalid method index")
1610 return toType(m
.mtyp
)
1613 // Uint returns v's underlying value, as a uint64.
1614 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1615 func (v Value
) Uint() uint64 {
1617 var p unsafe
.Pointer
1618 if v
.flag
&flagIndir
!= 0 {
1621 // The escape analysis is good enough that &v.val
1622 // does not trigger a heap allocation.
1623 p
= unsafe
.Pointer(&v
.val
)
1627 return uint64(*(*uint)(p
))
1629 return uint64(*(*uint8)(p
))
1631 return uint64(*(*uint16)(p
))
1633 return uint64(*(*uint32)(p
))
1635 return uint64(*(*uint64)(p
))
1637 return uint64(*(*uintptr)(p
))
1639 panic(&ValueError
{"reflect.Value.Uint", k
})
1642 // UnsafeAddr returns a pointer to v's data.
1643 // It is for advanced clients that also import the "unsafe" package.
1644 // It panics if v is not addressable.
1645 func (v Value
) UnsafeAddr() uintptr {
1647 panic(&ValueError
{"reflect.Value.UnsafeAddr", Invalid
})
1649 if v
.flag
&flagAddr
== 0 {
1650 panic("reflect.Value.UnsafeAddr of unaddressable value")
1652 return uintptr(v
.val
)
1655 // StringHeader is the runtime representation of a string.
1656 // It cannot be used safely or portably and its representation may
1657 // change in a later release.
1658 // Moreover, the Data field is not sufficient to guarantee the data
1659 // it references will not be garbage collected, so programs must keep
1660 // a separate, correctly typed pointer to the underlying data.
1661 type StringHeader
struct {
1666 // SliceHeader is the runtime representation of a slice.
1667 // It cannot be used safely or portably and its representation may
1668 // change in a later release.
1669 // Moreover, the Data field is not sufficient to guarantee the data
1670 // it references will not be garbage collected, so programs must keep
1671 // a separate, correctly typed pointer to the underlying data.
1672 type SliceHeader
struct {
1678 func typesMustMatch(what
string, t1
, t2 Type
) {
1680 panic(what
+ ": " + t1
.String() + " != " + t2
.String())
1684 // grow grows the slice s so that it can hold extra more values, allocating
1685 // more capacity if needed. It also returns the old and new slice lengths.
1686 func grow(s Value
, extra
int) (Value
, int, int) {
1690 panic("reflect.Append: slice overflow")
1694 return s
.Slice(0, i1
), i0
, i1
1707 t
:= MakeSlice(s
.Type(), i1
, m
)
1712 // Append appends the values x to a slice s and returns the resulting slice.
1713 // As in Go, each x's value must be assignable to the slice's element type.
1714 func Append(s Value
, x
...Value
) Value
{
1716 s
, i0
, i1
:= grow(s
, len(x
))
1717 for i
, j
:= i0
, 0; i
< i1
; i
, j
= i
+1, j
+1 {
1718 s
.Index(i
).Set(x
[j
])
1723 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
1724 // The slices s and t must have the same element type.
1725 func AppendSlice(s
, t Value
) Value
{
1728 typesMustMatch("reflect.AppendSlice", s
.Type().Elem(), t
.Type().Elem())
1729 s
, i0
, i1
:= grow(s
, t
.Len())
1730 Copy(s
.Slice(i0
, i1
), t
)
1734 // Copy copies the contents of src into dst until either
1735 // dst has been filled or src has been exhausted.
1736 // It returns the number of elements copied.
1737 // Dst and src each must have kind Slice or Array, and
1738 // dst and src must have the same element type.
1739 func Copy(dst
, src Value
) int {
1741 if dk
!= Array
&& dk
!= Slice
{
1742 panic(&ValueError
{"reflect.Copy", dk
})
1745 dst
.mustBeAssignable()
1747 dst
.mustBeExported()
1750 if sk
!= Array
&& sk
!= Slice
{
1751 panic(&ValueError
{"reflect.Copy", sk
})
1753 src
.mustBeExported()
1755 de
:= dst
.typ
.Elem()
1756 se
:= src
.typ
.Elem()
1757 typesMustMatch("reflect.Copy", de
, se
)
1760 if sn
:= src
.Len(); n
> sn
{
1764 // If sk is an in-line array, cannot take its address.
1765 // Instead, copy element by element.
1766 if src
.flag
&flagIndir
== 0 {
1767 for i
:= 0; i
< n
; i
++ {
1768 dst
.Index(i
).Set(src
.Index(i
))
1773 // Copy via memmove.
1774 var da
, sa unsafe
.Pointer
1778 da
= unsafe
.Pointer((*SliceHeader
)(dst
.val
).Data
)
1783 sa
= unsafe
.Pointer((*SliceHeader
)(src
.val
).Data
)
1785 memmove(da
, sa
, uintptr(n
)*de
.Size())
1789 // A runtimeSelect is a single case passed to rselect.
1790 // This must match ../runtime/chan.c:/runtimeSelect
1791 type runtimeSelect
struct {
1792 dir
uintptr // 0, SendDir, or RecvDir
1793 typ
*rtype
// channel type
1794 ch iword
// interface word for channel
1795 val iword
// interface word for value (for SendDir)
1798 // rselect runs a select. It returns the index of the chosen case,
1799 // and if the case was a receive, the interface word of the received
1800 // value and the conventional OK bool to indicate whether the receive
1801 // corresponds to a sent value.
1802 func rselect([]runtimeSelect
) (chosen
int, recv iword
, recvOK
bool)
1804 // A SelectDir describes the communication direction of a select case.
1807 // NOTE: These values must match ../runtime/chan.c:/SelectDir.
1811 SelectSend
// case Chan <- Send
1812 SelectRecv
// case <-Chan:
1813 SelectDefault
// default
1816 // A SelectCase describes a single case in a select operation.
1817 // The kind of case depends on Dir, the communication direction.
1819 // If Dir is SelectDefault, the case represents a default case.
1820 // Chan and Send must be zero Values.
1822 // If Dir is SelectSend, the case represents a send operation.
1823 // Normally Chan's underlying value must be a channel, and Send's underlying value must be
1824 // assignable to the channel's element type. As a special case, if Chan is a zero Value,
1825 // then the case is ignored, and the field Send will also be ignored and may be either zero
1828 // If Dir is SelectRecv, the case represents a receive operation.
1829 // Normally Chan's underlying value must be a channel and Send must be a zero Value.
1830 // If Chan is a zero Value, then the case is ignored, but Send must still be a zero Value.
1831 // When a receive operation is selected, the received Value is returned by Select.
1833 type SelectCase
struct {
1834 Dir SelectDir
// direction of case
1835 Chan Value
// channel to use (for send or receive)
1836 Send Value
// value to send (for send)
1839 // Select executes a select operation described by the list of cases.
1840 // Like the Go select statement, it blocks until at least one of the cases
1841 // can proceed, makes a uniform pseudo-random choice,
1842 // and then executes that case. It returns the index of the chosen case
1843 // and, if that case was a receive operation, the value received and a
1844 // boolean indicating whether the value corresponds to a send on the channel
1845 // (as opposed to a zero value received because the channel is closed).
1846 func Select(cases
[]SelectCase
) (chosen
int, recv Value
, recvOK
bool) {
1847 // NOTE: Do not trust that caller is not modifying cases data underfoot.
1848 // The range is safe because the caller cannot modify our copy of the len
1849 // and each iteration makes its own copy of the value c.
1850 runcases
:= make([]runtimeSelect
, len(cases
))
1851 haveDefault
:= false
1852 for i
, c
:= range cases
{
1854 rc
.dir
= uintptr(c
.Dir
)
1857 panic("reflect.Select: invalid Dir")
1859 case SelectDefault
: // default
1861 panic("reflect.Select: multiple default cases")
1864 if c
.Chan
.IsValid() {
1865 panic("reflect.Select: default case has Chan value")
1867 if c
.Send
.IsValid() {
1868 panic("reflect.Select: default case has Send value")
1878 tt
:= (*chanType
)(unsafe
.Pointer(ch
.typ
))
1879 if ChanDir(tt
.dir
)&SendDir
== 0 {
1880 panic("reflect.Select: SendDir case using recv-only channel")
1882 rc
.ch
= *(*iword
)(ch
.iword())
1886 panic("reflect.Select: SendDir case missing Send value")
1889 v
= v
.assignTo("reflect.Select", tt
.elem
, nil)
1893 if c
.Send
.IsValid() {
1894 panic("reflect.Select: RecvDir case has Send value")
1902 tt
:= (*chanType
)(unsafe
.Pointer(ch
.typ
))
1904 if ChanDir(tt
.dir
)&RecvDir
== 0 {
1905 panic("reflect.Select: RecvDir case using send-only channel")
1907 rc
.ch
= *(*iword
)(ch
.iword())
1911 chosen
, word
, recvOK
:= rselect(runcases
)
1912 if runcases
[chosen
].dir
== uintptr(SelectRecv
) {
1913 tt
:= (*chanType
)(unsafe
.Pointer(runcases
[chosen
].typ
))
1915 fl
:= flag(typ
.Kind()) << flagKindShift
1916 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
1919 recv
= Value
{typ
, unsafe
.Pointer(word
), fl
}
1921 return chosen
, recv
, recvOK
1928 // implemented in package runtime
1929 func unsafe_New(*rtype
) unsafe
.Pointer
1930 func unsafe_NewArray(*rtype
, int) unsafe
.Pointer
1932 // MakeSlice creates a new zero-initialized slice value
1933 // for the specified slice type, length, and capacity.
1934 func MakeSlice(typ Type
, len, cap int) Value
{
1935 if typ
.Kind() != Slice
{
1936 panic("reflect.MakeSlice of non-slice type")
1939 panic("reflect.MakeSlice: negative len")
1942 panic("reflect.MakeSlice: negative cap")
1945 panic("reflect.MakeSlice: len > cap")
1948 // Declare slice so that gc can see the base pointer in it.
1949 var x
[]unsafe
.Pointer
1951 // Reinterpret as *SliceHeader to edit.
1952 s
:= (*SliceHeader
)(unsafe
.Pointer(&x
))
1953 s
.Data
= uintptr(unsafe_NewArray(typ
.Elem().(*rtype
), cap))
1957 return Value
{typ
.common(), unsafe
.Pointer(&x
), flagIndir |
flag(Slice
)<<flagKindShift
}
1960 // MakeChan creates a new channel with the specified type and buffer size.
1961 func MakeChan(typ Type
, buffer
int) Value
{
1962 if typ
.Kind() != Chan
{
1963 panic("reflect.MakeChan of non-chan type")
1966 panic("reflect.MakeChan: negative buffer size")
1968 if typ
.ChanDir() != BothDir
{
1969 panic("reflect.MakeChan: unidirectional channel type")
1971 ch
:= makechan(typ
.(*rtype
), uint64(buffer
))
1972 return Value
{typ
.common(), unsafe
.Pointer(ch
), flagIndir |
(flag(Chan
) << flagKindShift
)}
1975 // MakeMap creates a new map of the specified type.
1976 func MakeMap(typ Type
) Value
{
1977 if typ
.Kind() != Map
{
1978 panic("reflect.MakeMap of non-map type")
1980 m
:= makemap(typ
.(*rtype
))
1981 return Value
{typ
.common(), unsafe
.Pointer(m
), flagIndir |
(flag(Map
) << flagKindShift
)}
1984 // Indirect returns the value that v points to.
1985 // If v is a nil pointer, Indirect returns a zero Value.
1986 // If v is not a pointer, Indirect returns v.
1987 func Indirect(v Value
) Value
{
1988 if v
.Kind() != Ptr
{
1994 // ValueOf returns a new Value initialized to the concrete value
1995 // stored in the interface i. ValueOf(nil) returns the zero Value.
1996 func ValueOf(i
interface{}) Value
{
2001 // TODO(rsc): Eliminate this terrible hack.
2002 // In the call to packValue, eface.typ doesn't escape,
2003 // and eface.word is an integer. So it looks like
2004 // i (= eface) doesn't escape. But really it does,
2005 // because eface.word is actually a pointer.
2008 // For an interface value with the noAddr bit set,
2009 // the representation is identical to an empty interface.
2010 eface
:= *(*emptyInterface
)(unsafe
.Pointer(&i
))
2012 fl
:= flag(typ
.Kind()) << flagKindShift
2013 if typ
.Kind() != Ptr
&& typ
.Kind() != UnsafePointer
{
2016 return Value
{typ
, unsafe
.Pointer(eface
.word
), fl
}
2019 // Zero returns a Value representing the zero value for the specified type.
2020 // The result is different from the zero value of the Value struct,
2021 // which represents no value at all.
2022 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
2023 // The returned value is neither addressable nor settable.
2024 func Zero(typ Type
) Value
{
2026 panic("reflect: Zero(nil)")
2029 fl
:= flag(t
.Kind()) << flagKindShift
2030 if t
.Kind() == Ptr || t
.Kind() == UnsafePointer
{
2031 return Value
{t
, nil, fl
}
2033 return Value
{t
, unsafe_New(typ
.(*rtype
)), fl | flagIndir
}
2036 // New returns a Value representing a pointer to a new zero value
2037 // for the specified type. That is, the returned Value's Type is PtrTo(t).
2038 func New(typ Type
) Value
{
2040 panic("reflect: New(nil)")
2042 ptr
:= unsafe_New(typ
.(*rtype
))
2043 fl
:= flag(Ptr
) << flagKindShift
2044 return Value
{typ
.common().ptrTo(), ptr
, fl
}
2047 // NewAt returns a Value representing a pointer to a value of the
2048 // specified type, using p as that pointer.
2049 func NewAt(typ Type
, p unsafe
.Pointer
) Value
{
2050 fl
:= flag(Ptr
) << flagKindShift
2051 return Value
{typ
.common().ptrTo(), p
, fl
}
2054 // assignTo returns a value v that can be assigned directly to typ.
2055 // It panics if v is not assignable to typ.
2056 // For a conversion to an interface type, target is a suggested scratch space to use.
2057 func (v Value
) assignTo(context
string, dst
*rtype
, target
*interface{}) Value
{
2058 if v
.flag
&flagMethod
!= 0 {
2059 v
= makeMethodValue(context
, v
)
2063 case directlyAssignable(dst
, v
.typ
):
2064 // Overwrite type so that they match.
2065 // Same memory layout, so no harm done.
2067 fl
:= v
.flag
& (flagRO | flagAddr | flagIndir
)
2068 fl |
= flag(dst
.Kind()) << flagKindShift
2069 return Value
{dst
, v
.val
, fl
}
2071 case implements(dst
, v
.typ
):
2073 target
= new(interface{})
2075 x
:= valueInterface(v
, false)
2076 if dst
.NumMethod() == 0 {
2079 ifaceE2I(dst
, x
, unsafe
.Pointer(target
))
2081 return Value
{dst
, unsafe
.Pointer(target
), flagIndir |
flag(Interface
)<<flagKindShift
}
2085 panic(context
+ ": value of type " + v
.typ
.String() + " is not assignable to type " + dst
.String())
2088 // Convert returns the value v converted to type t.
2089 // If the usual Go conversion rules do not allow conversion
2090 // of the value v to type t, Convert panics.
2091 func (v Value
) Convert(t Type
) Value
{
2092 if v
.flag
&flagMethod
!= 0 {
2093 v
= makeMethodValue("Convert", v
)
2095 op
:= convertOp(t
.common(), v
.typ
)
2097 panic("reflect.Value.Convert: value of type " + v
.typ
.String() + " cannot be converted to type " + t
.String())
2102 // convertOp returns the function to convert a value of type src
2103 // to a value of type dst. If the conversion is illegal, convertOp returns nil.
2104 func convertOp(dst
, src
*rtype
) func(Value
, Type
) Value
{
2106 case Int
, Int8
, Int16
, Int32
, Int64
:
2108 case Int
, Int8
, Int16
, Int32
, Int64
, Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2110 case Float32
, Float64
:
2116 case Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2118 case Int
, Int8
, Int16
, Int32
, Int64
, Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2120 case Float32
, Float64
:
2123 return cvtUintString
2126 case Float32
, Float64
:
2128 case Int
, Int8
, Int16
, Int32
, Int64
:
2130 case Uint
, Uint8
, Uint16
, Uint32
, Uint64
, Uintptr
:
2132 case Float32
, Float64
:
2136 case Complex64
, Complex128
:
2138 case Complex64
, Complex128
:
2143 if dst
.Kind() == Slice
&& dst
.Elem().PkgPath() == "" {
2144 switch dst
.Elem().Kind() {
2146 return cvtStringBytes
2148 return cvtStringRunes
2153 if dst
.Kind() == String
&& src
.Elem().PkgPath() == "" {
2154 switch src
.Elem().Kind() {
2156 return cvtBytesString
2158 return cvtRunesString
2163 // dst and src have same underlying type.
2164 if haveIdenticalUnderlyingType(dst
, src
) {
2168 // dst and src are unnamed pointer types with same underlying base type.
2169 if dst
.Kind() == Ptr
&& dst
.Name() == "" &&
2170 src
.Kind() == Ptr
&& src
.Name() == "" &&
2171 haveIdenticalUnderlyingType(dst
.Elem().common(), src
.Elem().common()) {
2175 if implements(dst
, src
) {
2176 if src
.Kind() == Interface
{
2185 // makeInt returns a Value of type t equal to bits (possibly truncated),
2186 // where t is a signed or unsigned int type.
2187 func makeInt(f flag
, bits
uint64, t Type
) Value
{
2189 if typ
.size
> ptrSize
{
2190 // Assume ptrSize >= 4, so this must be uint64.
2191 ptr
:= unsafe_New(typ
)
2192 *(*uint64)(unsafe
.Pointer(ptr
)) = bits
2193 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2198 *(*uint8)(unsafe
.Pointer(&w
)) = uint8(bits
)
2200 *(*uint16)(unsafe
.Pointer(&w
)) = uint16(bits
)
2202 *(*uint32)(unsafe
.Pointer(&w
)) = uint32(bits
)
2204 *(*uint64)(unsafe
.Pointer(&w
)) = uint64(bits
)
2206 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2209 // makeFloat returns a Value of type t equal to v (possibly truncated to float32),
2210 // where t is a float32 or float64 type.
2211 func makeFloat(f flag
, v
float64, t Type
) Value
{
2213 if typ
.size
> ptrSize
{
2214 // Assume ptrSize >= 4, so this must be float64.
2215 ptr
:= unsafe_New(typ
)
2216 *(*float64)(unsafe
.Pointer(ptr
)) = v
2217 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2223 *(*float32)(unsafe
.Pointer(&w
)) = float32(v
)
2225 *(*float64)(unsafe
.Pointer(&w
)) = v
2227 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2230 // makeComplex returns a Value of type t equal to v (possibly truncated to complex64),
2231 // where t is a complex64 or complex128 type.
2232 func makeComplex(f flag
, v complex128
, t Type
) Value
{
2234 if typ
.size
> ptrSize
{
2235 ptr
:= unsafe_New(typ
)
2238 *(*complex64
)(unsafe
.Pointer(ptr
)) = complex64(v
)
2240 *(*complex128
)(unsafe
.Pointer(ptr
)) = v
2242 return Value
{typ
, ptr
, f | flagIndir |
flag(typ
.Kind())<<flagKindShift
}
2245 // Assume ptrSize <= 8 so this must be complex64.
2247 *(*complex64
)(unsafe
.Pointer(&w
)) = complex64(v
)
2248 return Value
{typ
, unsafe
.Pointer(&w
), f |
flag(typ
.Kind())<<flagKindShift | flagIndir
}
2251 func makeString(f flag
, v
string, t Type
) Value
{
2252 ret
:= New(t
).Elem()
2254 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2258 func makeBytes(f flag
, v
[]byte, t Type
) Value
{
2259 ret
:= New(t
).Elem()
2261 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2265 func makeRunes(f flag
, v
[]rune
, t Type
) Value
{
2266 ret
:= New(t
).Elem()
2268 ret
.flag
= ret
.flag
&^flagAddr | f | flagIndir
2272 // These conversion functions are returned by convertOp
2273 // for classes of conversions. For example, the first function, cvtInt,
2274 // takes any value v of signed int type and returns the value converted
2275 // to type t, where t is any signed or unsigned int type.
2277 // convertOp: intXX -> [u]intXX
2278 func cvtInt(v Value
, t Type
) Value
{
2279 return makeInt(v
.flag
&flagRO
, uint64(v
.Int()), t
)
2282 // convertOp: uintXX -> [u]intXX
2283 func cvtUint(v Value
, t Type
) Value
{
2284 return makeInt(v
.flag
&flagRO
, v
.Uint(), t
)
2287 // convertOp: floatXX -> intXX
2288 func cvtFloatInt(v Value
, t Type
) Value
{
2289 return makeInt(v
.flag
&flagRO
, uint64(int64(v
.Float())), t
)
2292 // convertOp: floatXX -> uintXX
2293 func cvtFloatUint(v Value
, t Type
) Value
{
2294 return makeInt(v
.flag
&flagRO
, uint64(v
.Float()), t
)
2297 // convertOp: intXX -> floatXX
2298 func cvtIntFloat(v Value
, t Type
) Value
{
2299 return makeFloat(v
.flag
&flagRO
, float64(v
.Int()), t
)
2302 // convertOp: uintXX -> floatXX
2303 func cvtUintFloat(v Value
, t Type
) Value
{
2304 return makeFloat(v
.flag
&flagRO
, float64(v
.Uint()), t
)
2307 // convertOp: floatXX -> floatXX
2308 func cvtFloat(v Value
, t Type
) Value
{
2309 return makeFloat(v
.flag
&flagRO
, v
.Float(), t
)
2312 // convertOp: complexXX -> complexXX
2313 func cvtComplex(v Value
, t Type
) Value
{
2314 return makeComplex(v
.flag
&flagRO
, v
.Complex(), t
)
2317 // convertOp: intXX -> string
2318 func cvtIntString(v Value
, t Type
) Value
{
2319 return makeString(v
.flag
&flagRO
, string(v
.Int()), t
)
2322 // convertOp: uintXX -> string
2323 func cvtUintString(v Value
, t Type
) Value
{
2324 return makeString(v
.flag
&flagRO
, string(v
.Uint()), t
)
2327 // convertOp: []byte -> string
2328 func cvtBytesString(v Value
, t Type
) Value
{
2329 return makeString(v
.flag
&flagRO
, string(v
.Bytes()), t
)
2332 // convertOp: string -> []byte
2333 func cvtStringBytes(v Value
, t Type
) Value
{
2334 return makeBytes(v
.flag
&flagRO
, []byte(v
.String()), t
)
2337 // convertOp: []rune -> string
2338 func cvtRunesString(v Value
, t Type
) Value
{
2339 return makeString(v
.flag
&flagRO
, string(v
.runes()), t
)
2342 // convertOp: string -> []rune
2343 func cvtStringRunes(v Value
, t Type
) Value
{
2344 return makeRunes(v
.flag
&flagRO
, []rune(v
.String()), t
)
2347 // convertOp: direct copy
2348 func cvtDirect(v Value
, typ Type
) Value
{
2352 if f
&flagAddr
!= 0 {
2353 // indirect, mutable word - make a copy
2354 ptr
:= unsafe_New(t
)
2355 memmove(ptr
, val
, t
.size
)
2359 return Value
{t
, val
, v
.flag
&flagRO | f
}
2362 // convertOp: concrete -> interface
2363 func cvtT2I(v Value
, typ Type
) Value
{
2364 target
:= new(interface{})
2365 x
:= valueInterface(v
, false)
2366 if typ
.NumMethod() == 0 {
2369 ifaceE2I(typ
.(*rtype
), x
, unsafe
.Pointer(target
))
2371 return Value
{typ
.common(), unsafe
.Pointer(target
), v
.flag
&flagRO | flagIndir |
flag(Interface
)<<flagKindShift
}
2374 // convertOp: interface -> interface
2375 func cvtI2I(v Value
, typ Type
) Value
{
2378 ret
.flag |
= v
.flag
& flagRO
2381 return cvtT2I(v
.Elem(), typ
)
2384 // implemented in ../pkg/runtime
2385 func chancap(ch iword
) int
2386 func chanclose(ch iword
)
2387 func chanlen(ch iword
) int
2388 func chanrecv(t
*rtype
, ch iword
, nb
bool) (val iword
, selected
, received
bool)
2389 func chansend(t
*rtype
, ch iword
, val iword
, nb
bool) bool
2391 func makechan(typ
*rtype
, size
uint64) (ch iword
)
2392 func makemap(t
*rtype
) (m iword
)
2393 func mapaccess(t
*rtype
, m iword
, key iword
) (val iword
, ok
bool)
2394 func mapassign(t
*rtype
, m iword
, key
, val iword
, ok
bool)
2395 func mapiterinit(t
*rtype
, m iword
) *byte
2396 func mapiterkey(it
*byte) (key iword
, ok
bool)
2397 func mapiternext(it
*byte)
2398 func maplen(m iword
) int
2400 func call(typ
*rtype
, fnaddr unsafe
.Pointer
, isInterface
bool, isMethod
bool, params
*unsafe
.Pointer
, results
*unsafe
.Pointer
)
2401 func ifaceE2I(t
*rtype
, src
interface{}, dst unsafe
.Pointer
)
2403 // Dummy annotation marking that the value x escapes,
2404 // for use in cases where the reflect code is so clever that
2405 // the compiler cannot follow.
2406 func escapes(x
interface{}) {