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 *runtimeType // 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 // CanAddr returns true if the value's address can be obtained with Addr.
302 // Such values are called addressable. A value is addressable if it is
303 // an element of a slice, an element of an addressable array,
304 // a field of an addressable struct, or the result of dereferencing a pointer.
305 // If CanAddr returns false, calling Addr will panic.
306 func (v Value) CanAddr() bool {
307 return v.flag&flagAddr != 0
310 // CanSet returns true if the value of v can be changed.
311 // A Value can be changed only if it is addressable and was not
312 // obtained by the use of unexported struct fields.
313 // If CanSet returns false, calling Set or any type-specific
314 // setter (e.g., SetBool, SetInt64) will panic.
315 func (v Value) CanSet() bool {
316 return v.flag&(flagAddr|flagRO) == flagAddr
319 // Call calls the function v with the input arguments in.
320 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).
321 // Call panics if v's Kind is not Func.
322 // It returns the output results as Values.
323 // As in Go, each input argument must be assignable to the
324 // type of the function's corresponding input parameter.
325 // If v is a variadic function, Call creates the variadic slice parameter
326 // itself, copying in the corresponding values.
327 func (v Value) Call(in []Value) []Value {
330 return v.call("Call", in)
333 // CallSlice calls the variadic function v with the input arguments in,
334 // assigning the slice in[len(in)-1] to v's final variadic argument.
335 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]...).
336 // Call panics if v's Kind is not Func or if v is not variadic.
337 // It returns the output results as Values.
338 // As in Go, each input argument must be assignable to the
339 // type of the function's corresponding input parameter.
340 func (v Value) CallSlice(in []Value) []Value {
343 return v.call("CallSlice", in)
346 func (v Value) call(method string, in []Value) []Value {
347 // Get function pointer, type.
353 if v.flag&flagMethod != 0 {
354 i := int(v.flag) >> flagMethodShift
355 if v.typ.Kind() == Interface {
356 tt := (*interfaceType)(unsafe.Pointer(v.typ))
357 if i < 0 || i >= len(tt.methods) {
358 panic("reflect: broken Value")
361 if m.pkgPath != nil {
362 panic(method + " of unexported method")
364 t = toCommonType(m.typ)
365 iface := (*nonEmptyInterface)(v.val)
366 if iface.itab == nil {
367 panic(method + " of method on nil interface value")
369 fn = iface.itab.fun[i]
372 ut := v.typ.uncommon()
373 if ut == nil || i < 0 || i >= len(ut.methods) {
374 panic("reflect: broken Value")
377 if m.pkgPath != nil {
378 panic(method + " of unexported method")
381 t = toCommonType(m.mtyp)
384 } else if v.flag&flagIndir != 0 {
385 fn = *(*unsafe.Pointer)(v.val)
391 panic("reflect.Value.Call: call of nil function")
394 isSlice := method == "CallSlice"
398 panic("reflect: CallSlice of non-variadic function")
401 panic("reflect: CallSlice with too few input arguments")
404 panic("reflect: CallSlice with too many input arguments")
411 panic("reflect: Call with too few input arguments")
413 if !t.IsVariadic() && len(in) > n {
414 panic("reflect: Call with too many input arguments")
417 for _, x := range in {
418 if x.Kind() == Invalid {
419 panic("reflect: " + method + " using zero Value argument")
422 for i := 0; i < n; i++ {
423 if xt, targ := in[i].Type(), t.In(i); !xt.AssignableTo(targ) {
424 panic("reflect: " + method + " using " + xt.String() + " as type " + targ.String())
427 if !isSlice && t.IsVariadic() {
428 // prepare slice for remaining values
430 slice := MakeSlice(t.In(n), m, m)
431 elem := t.In(n).Elem()
432 for i := 0; i < m; i++ {
434 if xt := x.Type(); !xt.AssignableTo(elem) {
435 panic("reflect: cannot use " + xt.String() + " as type " + elem.String() + " in " + method)
437 slice.Index(i).Set(x)
440 in = make([]Value, n+1)
446 if nin != t.NumIn() {
447 panic("reflect.Value.Call: wrong argument count")
451 if v.flag&flagMethod != 0 {
454 params := make([]unsafe.Pointer, nin)
456 if v.flag&flagMethod != 0 {
457 // Hard-wired first argument.
460 params[0] = unsafe.Pointer(p)
463 first_pointer := false
464 for i, pv := range in {
466 targ := t.In(i).(*commonType)
467 pv = pv.assignTo("reflect.Value.Call", targ, nil)
468 if pv.flag&flagIndir == 0 {
469 p := new(unsafe.Pointer)
471 params[off] = unsafe.Pointer(p)
475 if i == 0 && Kind(targ.kind) != Ptr && v.flag&flagMethod == 0 && isMethod(v.typ) {
476 p := new(unsafe.Pointer)
478 params[off] = unsafe.Pointer(p)
484 ret := make([]Value, nout)
485 results := make([]unsafe.Pointer, nout)
486 for i := 0; i < nout; i++ {
488 results[i] = unsafe.Pointer(v.Pointer())
492 var pp *unsafe.Pointer
496 var pr *unsafe.Pointer
497 if len(results) > 0 {
501 call(t, fn, v.flag&flagMethod != 0, first_pointer, pp, pr)
506 // gccgo specific test to see if typ is a method. We can tell by
507 // looking at the string to see if there is a receiver. We need this
508 // because for gccgo all methods take pointer receivers.
509 func isMethod(t *commonType) bool {
510 if Kind(t.kind) != Func {
517 for i, c := range s {
523 } else if parens == 0 && c == ' ' && s[i+1] != '(' && !sawRet {
531 // Cap returns v's capacity.
532 // It panics if v's Kind is not Array, Chan, or Slice.
533 func (v Value) Cap() int {
539 return int(chancap(*(*iword)(v.iword())))
541 // Slice is always bigger than a word; assume flagIndir.
542 return (*SliceHeader)(v.val).Cap
544 panic(&ValueError{"reflect.Value.Cap", k})
547 // Close closes the channel v.
548 // It panics if v's Kind is not Chan.
549 func (v Value) Close() {
552 chanclose(*(*iword)(v.iword()))
555 // Complex returns v's underlying value, as a complex128.
556 // It panics if v's Kind is not Complex64 or Complex128
557 func (v Value) Complex() complex128 {
561 if v.flag&flagIndir != 0 {
562 return complex128(*(*complex64)(v.val))
564 return complex128(*(*complex64)(unsafe.Pointer(&v.val)))
566 // complex128 is always bigger than a word; assume flagIndir.
567 return *(*complex128)(v.val)
569 panic(&ValueError{"reflect.Value.Complex", k})
572 // Elem returns the value that the interface v contains
573 // or that the pointer v points to.
574 // It panics if v's Kind is not Interface or Ptr.
575 // It returns the zero Value if v is nil.
576 func (v Value) Elem() Value {
584 if v.typ.NumMethod() == 0 {
585 eface := (*emptyInterface)(v.val)
586 if eface.typ == nil {
587 // nil interface value
590 typ = toCommonType(eface.typ)
591 val = unsafe.Pointer(eface.word)
593 iface := (*nonEmptyInterface)(v.val)
594 if iface.itab == nil {
595 // nil interface value
598 typ = toCommonType(iface.itab.typ)
599 val = unsafe.Pointer(iface.word)
601 fl := v.flag & flagRO
602 fl |= flag(typ.Kind()) << flagKindShift
603 if typ.Kind() != Ptr && typ.Kind() != UnsafePointer {
606 return Value{typ, val, fl}
610 if v.flag&flagIndir != 0 {
611 val = *(*unsafe.Pointer)(val)
613 // The returned value's address is v's value.
617 tt := (*ptrType)(unsafe.Pointer(v.typ))
618 typ := toCommonType(tt.elem)
619 fl := v.flag&flagRO | flagIndir | flagAddr
620 fl |= flag(typ.Kind() << flagKindShift)
621 return Value{typ, val, fl}
623 panic(&ValueError{"reflect.Value.Elem", k})
626 // Field returns the i'th field of the struct v.
627 // It panics if v's Kind is not Struct or i is out of range.
628 func (v Value) Field(i int) Value {
630 tt := (*structType)(unsafe.Pointer(v.typ))
631 if i < 0 || i >= len(tt.fields) {
632 panic("reflect: Field index out of range")
634 field := &tt.fields[i]
635 typ := toCommonType(field.typ)
637 // Inherit permission bits from v.
638 fl := v.flag & (flagRO | flagIndir | flagAddr)
639 // Using an unexported field forces flagRO.
640 if field.pkgPath != nil {
643 fl |= flag(typ.Kind()) << flagKindShift
645 var val unsafe.Pointer
647 case fl&flagIndir != 0:
648 // Indirect. Just bump pointer.
649 val = unsafe.Pointer(uintptr(v.val) + field.offset)
651 // Direct. Discard leading bytes.
652 val = unsafe.Pointer(uintptr(v.val) << (field.offset * 8))
654 // Direct. Discard leading bytes.
655 val = unsafe.Pointer(uintptr(v.val) >> (field.offset * 8))
658 return Value{typ, val, fl}
661 // FieldByIndex returns the nested field corresponding to index.
662 // It panics if v's Kind is not struct.
663 func (v Value) FieldByIndex(index []int) Value {
665 for i, x := range index {
667 if v.Kind() == Ptr && v.Elem().Kind() == Struct {
676 // FieldByName returns the struct field with the given name.
677 // It returns the zero Value if no field was found.
678 // It panics if v's Kind is not struct.
679 func (v Value) FieldByName(name string) Value {
681 if f, ok := v.typ.FieldByName(name); ok {
682 return v.FieldByIndex(f.Index)
687 // FieldByNameFunc returns the struct field with a name
688 // that satisfies the match function.
689 // It panics if v's Kind is not struct.
690 // It returns the zero Value if no field was found.
691 func (v Value) FieldByNameFunc(match func(string) bool) Value {
693 if f, ok := v.typ.FieldByNameFunc(match); ok {
694 return v.FieldByIndex(f.Index)
699 // Float returns v's underlying value, as a float64.
700 // It panics if v's Kind is not Float32 or Float64
701 func (v Value) Float() float64 {
705 if v.flag&flagIndir != 0 {
706 return float64(*(*float32)(v.val))
708 return float64(*(*float32)(unsafe.Pointer(&v.val)))
710 if v.flag&flagIndir != 0 {
711 return *(*float64)(v.val)
713 return *(*float64)(unsafe.Pointer(&v.val))
715 panic(&ValueError{"reflect.Value.Float", k})
718 // Index returns v's i'th element.
719 // It panics if v's Kind is not Array or Slice or i is out of range.
720 func (v Value) Index(i int) Value {
724 tt := (*arrayType)(unsafe.Pointer(v.typ))
725 if i < 0 || i > int(tt.len) {
726 panic("reflect: array index out of range")
728 typ := toCommonType(tt.elem)
729 fl := v.flag & (flagRO | flagIndir | flagAddr) // bits same as overall array
730 fl |= flag(typ.Kind()) << flagKindShift
731 offset := uintptr(i) * typ.size
733 var val unsafe.Pointer
735 case fl&flagIndir != 0:
736 // Indirect. Just bump pointer.
737 val = unsafe.Pointer(uintptr(v.val) + offset)
739 // Direct. Discard leading bytes.
740 val = unsafe.Pointer(uintptr(v.val) << (offset * 8))
742 // Direct. Discard leading bytes.
743 val = unsafe.Pointer(uintptr(v.val) >> (offset * 8))
745 return Value{typ, val, fl}
748 // Element flag same as Elem of Ptr.
749 // Addressable, indirect, possibly read-only.
750 fl := flagAddr | flagIndir | v.flag&flagRO
751 s := (*SliceHeader)(v.val)
752 if i < 0 || i >= s.Len {
753 panic("reflect: slice index out of range")
755 tt := (*sliceType)(unsafe.Pointer(v.typ))
756 typ := toCommonType(tt.elem)
757 fl |= flag(typ.Kind()) << flagKindShift
758 val := unsafe.Pointer(s.Data + uintptr(i)*typ.size)
759 return Value{typ, val, fl}
761 panic(&ValueError{"reflect.Value.Index", k})
764 // Int returns v's underlying value, as an int64.
765 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
766 func (v Value) Int() int64 {
769 if v.flag&flagIndir != 0 {
772 // The escape analysis is good enough that &v.val
773 // does not trigger a heap allocation.
774 p = unsafe.Pointer(&v.val)
778 return int64(*(*int)(p))
780 return int64(*(*int8)(p))
782 return int64(*(*int16)(p))
784 return int64(*(*int32)(p))
786 return int64(*(*int64)(p))
788 panic(&ValueError{"reflect.Value.Int", k})
791 // CanInterface returns true if Interface can be used without panicking.
792 func (v Value) CanInterface() bool {
794 panic(&ValueError{"reflect.Value.CanInterface", Invalid})
796 return v.flag&(flagMethod|flagRO) == 0
799 // Interface returns v's current value as an interface{}.
800 // It is equivalent to:
801 // var i interface{} = (v's underlying value)
802 // If v is a method obtained by invoking Value.Method
803 // (as opposed to Type.Method), Interface cannot return an
804 // interface value, so it panics.
805 // It also panics if the Value was obtained by accessing
806 // unexported struct fields.
807 func (v Value) Interface() (i interface{}) {
808 return valueInterface(v, true)
811 func valueInterface(v Value, safe bool) interface{} {
813 panic(&ValueError{"reflect.Value.Interface", 0})
815 if v.flag&flagMethod != 0 {
816 panic("reflect.Value.Interface: cannot create interface value for method with bound receiver")
819 if safe && v.flag&flagRO != 0 {
820 // Do not allow access to unexported values via Interface,
821 // because they might be pointers that should not be
822 // writable or methods or function that should not be callable.
823 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
828 // Special case: return the element inside the interface.
829 // Empty interface has one layout, all interfaces with
830 // methods have a second layout.
831 if v.NumMethod() == 0 {
832 return *(*interface{})(v.val)
834 return *(*interface {
839 // Non-interface value.
840 var eface emptyInterface
841 eface.typ = v.typ.runtimeType()
842 eface.word = v.iword()
844 if v.flag&flagIndir != 0 && v.typ.size > ptrSize {
845 // eface.word is a pointer to the actual data,
846 // which might be changed. We need to return
847 // a pointer to unchanging data, so make a copy.
848 ptr := unsafe_New(v.typ)
849 memmove(ptr, unsafe.Pointer(eface.word), v.typ.size)
850 eface.word = iword(ptr)
853 return *(*interface{})(unsafe.Pointer(&eface))
856 // InterfaceData returns the interface v's value as a uintptr pair.
857 // It panics if v's Kind is not Interface.
858 func (v Value) InterfaceData() [2]uintptr {
860 // We treat this as a read operation, so we allow
861 // it even for unexported data, because the caller
862 // has to import "unsafe" to turn it into something
863 // that can be abused.
864 // Interface value is always bigger than a word; assume flagIndir.
865 return *(*[2]uintptr)(v.val)
868 // IsNil returns true if v is a nil value.
869 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
870 func (v Value) IsNil() bool {
873 case Chan, Func, Map, Ptr:
874 if v.flag&flagMethod != 0 {
875 panic("reflect: IsNil of method Value")
878 if v.flag&flagIndir != 0 {
879 ptr = *(*unsafe.Pointer)(ptr)
882 case Interface, Slice:
883 // Both interface and slice are nil if first word is 0.
884 // Both are always bigger than a word; assume flagIndir.
885 return *(*unsafe.Pointer)(v.val) == nil
887 panic(&ValueError{"reflect.Value.IsNil", k})
890 // IsValid returns true if v represents a value.
891 // It returns false if v is the zero Value.
892 // If IsValid returns false, all other methods except String panic.
893 // Most functions and methods never return an invalid value.
894 // If one does, its documentation states the conditions explicitly.
895 func (v Value) IsValid() bool {
899 // Kind returns v's Kind.
900 // If v is the zero Value (IsValid returns false), Kind returns Invalid.
901 func (v Value) Kind() Kind {
905 // Len returns v's length.
906 // It panics if v's Kind is not Array, Chan, Map, Slice, or String.
907 func (v Value) Len() int {
911 tt := (*arrayType)(unsafe.Pointer(v.typ))
914 return int(chanlen(*(*iword)(v.iword())))
916 return int(maplen(*(*iword)(v.iword())))
918 // Slice is bigger than a word; assume flagIndir.
919 return (*SliceHeader)(v.val).Len
921 // String is bigger than a word; assume flagIndir.
922 return (*StringHeader)(v.val).Len
924 panic(&ValueError{"reflect.Value.Len", k})
927 // MapIndex returns the value associated with key in the map v.
928 // It panics if v's Kind is not Map.
929 // It returns the zero Value if key is not found in the map or if v represents a nil map.
930 // As in Go, the key's value must be assignable to the map's key type.
931 func (v Value) MapIndex(key Value) Value {
933 tt := (*mapType)(unsafe.Pointer(v.typ))
935 // Do not require key to be exported, so that DeepEqual
936 // and other programs can use all the keys returned by
937 // MapKeys as arguments to MapIndex. If either the map
938 // or the key is unexported, though, the result will be
939 // considered unexported. This is consistent with the
940 // behavior for structs, which allow read but not write
941 // of unexported fields.
942 key = key.assignTo("reflect.Value.MapIndex", toCommonType(tt.key), nil)
944 word, ok := mapaccess(v.typ.runtimeType(), *(*iword)(v.iword()), key.iword())
948 typ := toCommonType(tt.elem)
949 fl := (v.flag | key.flag) & flagRO
950 if typ.Kind() != Ptr && typ.Kind() != UnsafePointer {
953 fl |= flag(typ.Kind()) << flagKindShift
954 return Value{typ, unsafe.Pointer(word), fl}
957 // MapKeys returns a slice containing all the keys present in the map,
958 // in unspecified order.
959 // It panics if v's Kind is not Map.
960 // It returns an empty slice if v represents a nil map.
961 func (v Value) MapKeys() []Value {
963 tt := (*mapType)(unsafe.Pointer(v.typ))
964 keyType := toCommonType(tt.key)
966 fl := v.flag & flagRO
967 fl |= flag(keyType.Kind()) << flagKindShift
968 if keyType.Kind() != Ptr && keyType.Kind() != UnsafePointer {
972 m := *(*iword)(v.iword())
977 it := mapiterinit(v.typ.runtimeType(), m)
978 a := make([]Value, mlen)
980 for i = 0; i < len(a); i++ {
981 keyWord, ok := mapiterkey(it)
985 a[i] = Value{keyType, unsafe.Pointer(keyWord), fl}
991 // Method returns a function value corresponding to v's i'th method.
992 // The arguments to a Call on the returned function should not include
993 // a receiver; the returned function will always use v as the receiver.
994 // Method panics if i is out of range.
995 func (v Value) Method(i int) Value {
997 panic(&ValueError{"reflect.Value.Method", Invalid})
999 if v.flag&flagMethod != 0 || i < 0 || i >= v.typ.NumMethod() {
1000 panic("reflect: Method index out of range")
1002 fl := v.flag & (flagRO | flagAddr | flagIndir)
1003 fl |= flag(Func) << flagKindShift
1004 fl |= flag(i)<<flagMethodShift | flagMethod
1005 return Value{v.typ, v.val, fl}
1008 // NumMethod returns the number of methods in the value's method set.
1009 func (v Value) NumMethod() int {
1011 panic(&ValueError{"reflect.Value.NumMethod", Invalid})
1013 if v.flag&flagMethod != 0 {
1016 return v.typ.NumMethod()
1019 // MethodByName returns a function value corresponding to the method
1020 // of v with the given name.
1021 // The arguments to a Call on the returned function should not include
1022 // a receiver; the returned function will always use v as the receiver.
1023 // It returns the zero Value if no method was found.
1024 func (v Value) MethodByName(name string) Value {
1026 panic(&ValueError{"reflect.Value.MethodByName", Invalid})
1028 if v.flag&flagMethod != 0 {
1031 m, ok := v.typ.MethodByName(name)
1035 return v.Method(m.Index)
1038 // NumField returns the number of fields in the struct v.
1039 // It panics if v's Kind is not Struct.
1040 func (v Value) NumField() int {
1042 tt := (*structType)(unsafe.Pointer(v.typ))
1043 return len(tt.fields)
1046 // OverflowComplex returns true if the complex128 x cannot be represented by v's type.
1047 // It panics if v's Kind is not Complex64 or Complex128.
1048 func (v Value) OverflowComplex(x complex128) bool {
1052 return overflowFloat32(real(x)) || overflowFloat32(imag(x))
1056 panic(&ValueError{"reflect.Value.OverflowComplex", k})
1059 // OverflowFloat returns true if the float64 x cannot be represented by v's type.
1060 // It panics if v's Kind is not Float32 or Float64.
1061 func (v Value) OverflowFloat(x float64) bool {
1065 return overflowFloat32(x)
1069 panic(&ValueError{"reflect.Value.OverflowFloat", k})
1072 func overflowFloat32(x float64) bool {
1076 return math.MaxFloat32 <= x && x <= math.MaxFloat64
1079 // OverflowInt returns true if the int64 x cannot be represented by v's type.
1080 // It panics if v's Kind is not Int, Int8, int16, Int32, or Int64.
1081 func (v Value) OverflowInt(x int64) bool {
1084 case Int, Int8, Int16, Int32, Int64:
1085 bitSize := v.typ.size * 8
1086 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
1089 panic(&ValueError{"reflect.Value.OverflowInt", k})
1092 // OverflowUint returns true if the uint64 x cannot be represented by v's type.
1093 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1094 func (v Value) OverflowUint(x uint64) bool {
1097 case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
1098 bitSize := v.typ.size * 8
1099 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
1102 panic(&ValueError{"reflect.Value.OverflowUint", k})
1105 // Pointer returns v's value as a uintptr.
1106 // It returns uintptr instead of unsafe.Pointer so that
1107 // code using reflect cannot obtain unsafe.Pointers
1108 // without importing the unsafe package explicitly.
1109 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
1110 func (v Value) Pointer() uintptr {
1113 case Chan, Func, Map, Ptr, UnsafePointer:
1114 if k == Func && v.flag&flagMethod != 0 {
1115 panic("reflect.Value.Pointer of method Value")
1118 if v.flag&flagIndir != 0 {
1119 p = *(*unsafe.Pointer)(p)
1123 return (*SliceHeader)(v.val).Data
1125 panic(&ValueError{"reflect.Value.Pointer", k})
1128 // Recv receives and returns a value from the channel v.
1129 // It panics if v's Kind is not Chan.
1130 // The receive blocks until a value is ready.
1131 // The boolean value ok is true if the value x corresponds to a send
1132 // on the channel, false if it is a zero value received because the channel is closed.
1133 func (v Value) Recv() (x Value, ok bool) {
1136 return v.recv(false)
1139 // internal recv, possibly non-blocking (nb).
1140 // v is known to be a channel.
1141 func (v Value) recv(nb bool) (val Value, ok bool) {
1142 tt := (*chanType)(unsafe.Pointer(v.typ))
1143 if ChanDir(tt.dir)&RecvDir == 0 {
1144 panic("recv on send-only channel")
1146 word, selected, ok := chanrecv(v.typ.runtimeType(), *(*iword)(v.iword()), nb)
1148 typ := toCommonType(tt.elem)
1149 fl := flag(typ.Kind()) << flagKindShift
1150 if typ.Kind() != Ptr && typ.Kind() != UnsafePointer {
1153 val = Value{typ, unsafe.Pointer(word), fl}
1158 // Send sends x on the channel v.
1159 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
1160 // As in Go, x's value must be assignable to the channel's element type.
1161 func (v Value) Send(x Value) {
1167 // internal send, possibly non-blocking.
1168 // v is known to be a channel.
1169 func (v Value) send(x Value, nb bool) (selected bool) {
1170 tt := (*chanType)(unsafe.Pointer(v.typ))
1171 if ChanDir(tt.dir)&SendDir == 0 {
1172 panic("send on recv-only channel")
1175 x = x.assignTo("reflect.Value.Send", toCommonType(tt.elem), nil)
1176 return chansend(v.typ.runtimeType(), *(*iword)(v.iword()), x.iword(), nb)
1179 // Set assigns x to the value v.
1180 // It panics if CanSet returns false.
1181 // As in Go, x's value must be assignable to v's type.
1182 func (v Value) Set(x Value) {
1183 v.mustBeAssignable()
1184 x.mustBeExported() // do not let unexported x leak
1185 var target *interface{}
1186 if v.kind() == Interface {
1187 target = (*interface{})(v.val)
1189 x = x.assignTo("reflect.Set", v.typ, target)
1190 if x.flag&flagIndir != 0 {
1191 memmove(v.val, x.val, v.typ.size)
1193 storeIword(v.val, iword(x.val), v.typ.size)
1197 // SetBool sets v's underlying value.
1198 // It panics if v's Kind is not Bool or if CanSet() is false.
1199 func (v Value) SetBool(x bool) {
1200 v.mustBeAssignable()
1205 // SetBytes sets v's underlying value.
1206 // It panics if v's underlying value is not a slice of bytes.
1207 func (v Value) SetBytes(x []byte) {
1208 v.mustBeAssignable()
1210 if v.typ.Elem().Kind() != Uint8 {
1211 panic("reflect.Value.SetBytes of non-byte slice")
1213 *(*[]byte)(v.val) = x
1216 // SetComplex sets v's underlying value to x.
1217 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
1218 func (v Value) SetComplex(x complex128) {
1219 v.mustBeAssignable()
1220 switch k := v.kind(); k {
1222 panic(&ValueError{"reflect.Value.SetComplex", k})
1224 *(*complex64)(v.val) = complex64(x)
1226 *(*complex128)(v.val) = x
1230 // SetFloat sets v's underlying value to x.
1231 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
1232 func (v Value) SetFloat(x float64) {
1233 v.mustBeAssignable()
1234 switch k := v.kind(); k {
1236 panic(&ValueError{"reflect.Value.SetFloat", k})
1238 *(*float32)(v.val) = float32(x)
1240 *(*float64)(v.val) = x
1244 // SetInt sets v's underlying value to x.
1245 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
1246 func (v Value) SetInt(x int64) {
1247 v.mustBeAssignable()
1248 switch k := v.kind(); k {
1250 panic(&ValueError{"reflect.Value.SetInt", k})
1252 *(*int)(v.val) = int(x)
1254 *(*int8)(v.val) = int8(x)
1256 *(*int16)(v.val) = int16(x)
1258 *(*int32)(v.val) = int32(x)
1260 *(*int64)(v.val) = x
1264 // SetLen sets v's length to n.
1265 // It panics if v's Kind is not Slice or if n is negative or
1266 // greater than the capacity of the slice.
1267 func (v Value) SetLen(n int) {
1268 v.mustBeAssignable()
1270 s := (*SliceHeader)(v.val)
1271 if n < 0 || n > int(s.Cap) {
1272 panic("reflect: slice length out of range in SetLen")
1277 // SetMapIndex sets the value associated with key in the map v to val.
1278 // It panics if v's Kind is not Map.
1279 // If val is the zero Value, SetMapIndex deletes the key from the map.
1280 // As in Go, key's value must be assignable to the map's key type,
1281 // and val's value must be assignable to the map's value type.
1282 func (v Value) SetMapIndex(key, val Value) {
1285 key.mustBeExported()
1286 tt := (*mapType)(unsafe.Pointer(v.typ))
1287 key = key.assignTo("reflect.Value.SetMapIndex", toCommonType(tt.key), nil)
1289 val.mustBeExported()
1290 val = val.assignTo("reflect.Value.SetMapIndex", toCommonType(tt.elem), nil)
1292 mapassign(v.typ.runtimeType(), *(*iword)(v.iword()), key.iword(), val.iword(), val.typ != nil)
1295 // SetUint sets v's underlying value to x.
1296 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
1297 func (v Value) SetUint(x uint64) {
1298 v.mustBeAssignable()
1299 switch k := v.kind(); k {
1301 panic(&ValueError{"reflect.Value.SetUint", k})
1303 *(*uint)(v.val) = uint(x)
1305 *(*uint8)(v.val) = uint8(x)
1307 *(*uint16)(v.val) = uint16(x)
1309 *(*uint32)(v.val) = uint32(x)
1311 *(*uint64)(v.val) = x
1313 *(*uintptr)(v.val) = uintptr(x)
1317 // SetPointer sets the unsafe.Pointer value v to x.
1318 // It panics if v's Kind is not UnsafePointer.
1319 func (v Value) SetPointer(x unsafe.Pointer) {
1320 v.mustBeAssignable()
1321 v.mustBe(UnsafePointer)
1322 *(*unsafe.Pointer)(v.val) = x
1325 // SetString sets v's underlying value to x.
1326 // It panics if v's Kind is not String or if CanSet() is false.
1327 func (v Value) SetString(x string) {
1328 v.mustBeAssignable()
1330 *(*string)(v.val) = x
1333 // Slice returns a slice of v.
1334 // It panics if v's Kind is not Array or Slice.
1335 func (v Value) Slice(beg, end int) Value {
1341 switch k := v.kind(); k {
1343 panic(&ValueError{"reflect.Value.Slice", k})
1345 if v.flag&flagAddr == 0 {
1346 panic("reflect.Value.Slice: slice of unaddressable array")
1348 tt := (*arrayType)(unsafe.Pointer(v.typ))
1350 typ = (*sliceType)(unsafe.Pointer(toCommonType(tt.slice)))
1353 typ = (*sliceType)(unsafe.Pointer(v.typ))
1354 s := (*SliceHeader)(v.val)
1355 base = unsafe.Pointer(s.Data)
1359 if beg < 0 || end < beg || end > cap {
1360 panic("reflect.Value.Slice: slice index out of bounds")
1363 // Declare slice so that gc can see the base pointer in it.
1366 // Reinterpret as *SliceHeader to edit.
1367 s := (*SliceHeader)(unsafe.Pointer(&x))
1368 s.Data = uintptr(base) + uintptr(beg)*toCommonType(typ.elem).Size()
1372 fl := v.flag&flagRO | flagIndir | flag(Slice)<<flagKindShift
1373 return Value{typ.common(), unsafe.Pointer(&x), fl}
1376 // String returns the string v's underlying value, as a string.
1377 // String is a special case because of Go's String method convention.
1378 // Unlike the other getters, it does not panic if v's Kind is not String.
1379 // Instead, it returns a string of the form "<T value>" where T is v's type.
1380 func (v Value) String() string {
1381 switch k := v.kind(); k {
1383 return "<invalid Value>"
1385 return *(*string)(v.val)
1387 // If you call String on a reflect.Value of other type, it's better to
1388 // print something than to panic. Useful in debugging.
1389 return "<" + v.typ.String() + " Value>"
1392 // TryRecv attempts to receive a value from the channel v but will not block.
1393 // It panics if v's Kind is not Chan.
1394 // If the receive cannot finish without blocking, x is the zero Value.
1395 // The boolean ok is true if the value x corresponds to a send
1396 // on the channel, false if it is a zero value received because the channel is closed.
1397 func (v Value) TryRecv() (x Value, ok bool) {
1403 // TrySend attempts to send x on the channel v but will not block.
1404 // It panics if v's Kind is not Chan.
1405 // It returns true if the value was sent, false otherwise.
1406 // As in Go, x's value must be assignable to the channel's element type.
1407 func (v Value) TrySend(x Value) bool {
1410 return v.send(x, true)
1413 // Type returns v's type.
1414 func (v Value) Type() Type {
1417 panic(&ValueError{"reflect.Value.Type", Invalid})
1419 if f&flagMethod == 0 {
1421 return v.typ.toType()
1425 // v.typ describes the receiver, not the method type.
1426 i := int(v.flag) >> flagMethodShift
1427 if v.typ.Kind() == Interface {
1428 // Method on interface.
1429 tt := (*interfaceType)(unsafe.Pointer(v.typ))
1430 if i < 0 || i >= len(tt.methods) {
1431 panic("reflect: broken Value")
1434 return toCommonType(m.typ).toType()
1436 // Method on concrete type.
1437 ut := v.typ.uncommon()
1438 if ut == nil || i < 0 || i >= len(ut.methods) {
1439 panic("reflect: broken Value")
1442 return toCommonType(m.mtyp).toType()
1445 // Uint returns v's underlying value, as a uint64.
1446 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1447 func (v Value) Uint() uint64 {
1449 var p unsafe.Pointer
1450 if v.flag&flagIndir != 0 {
1453 // The escape analysis is good enough that &v.val
1454 // does not trigger a heap allocation.
1455 p = unsafe.Pointer(&v.val)
1459 return uint64(*(*uint)(p))
1461 return uint64(*(*uint8)(p))
1463 return uint64(*(*uint16)(p))
1465 return uint64(*(*uint32)(p))
1467 return uint64(*(*uint64)(p))
1469 return uint64(*(*uintptr)(p))
1471 panic(&ValueError{"reflect.Value.Uint", k})
1474 // UnsafeAddr returns a pointer to v's data.
1475 // It is for advanced clients that also import the "unsafe" package.
1476 // It panics if v is not addressable.
1477 func (v Value) UnsafeAddr() uintptr {
1479 panic(&ValueError{"reflect.Value.UnsafeAddr", Invalid})
1481 if v.flag&flagAddr == 0 {
1482 panic("reflect.Value.UnsafeAddr of unaddressable value")
1484 return uintptr(v.val)
1487 // StringHeader is the runtime representation of a string.
1488 // It cannot be used safely or portably.
1489 type StringHeader struct {
1494 // SliceHeader is the runtime representation of a slice.
1495 // It cannot be used safely or portably.
1496 type SliceHeader struct {
1502 func typesMustMatch(what string, t1, t2 Type) {
1504 panic(what + ": " + t1.String() + " != " + t2.String())
1508 // grow grows the slice s so that it can hold extra more values, allocating
1509 // more capacity if needed. It also returns the old and new slice lengths.
1510 func grow(s Value, extra int) (Value, int, int) {
1514 panic("reflect.Append: slice overflow")
1518 return s.Slice(0, i1), i0, i1
1531 t := MakeSlice(s.Type(), i1, m)
1536 // Append appends the values x to a slice s and returns the resulting slice.
1537 // As in Go, each x's value must be assignable to the slice's element type.
1538 func Append(s Value, x ...Value) Value {
1540 s, i0, i1 := grow(s, len(x))
1541 for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
1542 s.Index(i).Set(x[j])
1547 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
1548 // The slices s and t must have the same element type.
1549 func AppendSlice(s, t Value) Value {
1552 typesMustMatch("reflect.AppendSlice", s.Type().Elem(), t.Type().Elem())
1553 s, i0, i1 := grow(s, t.Len())
1554 Copy(s.Slice(i0, i1), t)
1558 // Copy copies the contents of src into dst until either
1559 // dst has been filled or src has been exhausted.
1560 // It returns the number of elements copied.
1561 // Dst and src each must have kind Slice or Array, and
1562 // dst and src must have the same element type.
1563 func Copy(dst, src Value) int {
1565 if dk != Array && dk != Slice {
1566 panic(&ValueError{"reflect.Copy", dk})
1569 dst.mustBeAssignable()
1571 dst.mustBeExported()
1574 if sk != Array && sk != Slice {
1575 panic(&ValueError{"reflect.Copy", sk})
1577 src.mustBeExported()
1579 de := dst.typ.Elem()
1580 se := src.typ.Elem()
1581 typesMustMatch("reflect.Copy", de, se)
1584 if sn := src.Len(); n > sn {
1588 // If sk is an in-line array, cannot take its address.
1589 // Instead, copy element by element.
1590 if src.flag&flagIndir == 0 {
1591 for i := 0; i < n; i++ {
1592 dst.Index(i).Set(src.Index(i))
1597 // Copy via memmove.
1598 var da, sa unsafe.Pointer
1602 da = unsafe.Pointer((*SliceHeader)(dst.val).Data)
1607 sa = unsafe.Pointer((*SliceHeader)(src.val).Data)
1609 memmove(da, sa, uintptr(n)*de.Size())
1617 // implemented in package runtime
1618 func unsafe_New(Type) unsafe.Pointer
1619 func unsafe_NewArray(Type, int) unsafe.Pointer
1621 // MakeSlice creates a new zero-initialized slice value
1622 // for the specified slice type, length, and capacity.
1623 func MakeSlice(typ Type, len, cap int) Value {
1624 if typ.Kind() != Slice {
1625 panic("reflect.MakeSlice of non-slice type")
1628 panic("reflect.MakeSlice: negative len")
1631 panic("reflect.MakeSlice: negative cap")
1634 panic("reflect.MakeSlice: len > cap")
1637 // Declare slice so that gc can see the base pointer in it.
1640 // Reinterpret as *SliceHeader to edit.
1641 s := (*SliceHeader)(unsafe.Pointer(&x))
1642 s.Data = uintptr(unsafe_NewArray(typ.Elem(), cap))
1646 return Value{typ.common(), unsafe.Pointer(&x), flagIndir | flag(Slice)<<flagKindShift}
1649 // MakeChan creates a new channel with the specified type and buffer size.
1650 func MakeChan(typ Type, buffer int) Value {
1651 if typ.Kind() != Chan {
1652 panic("reflect.MakeChan of non-chan type")
1655 panic("reflect.MakeChan: negative buffer size")
1657 if typ.ChanDir() != BothDir {
1658 panic("reflect.MakeChan: unidirectional channel type")
1660 ch := makechan(typ.runtimeType(), uint32(buffer))
1661 return Value{typ.common(), unsafe.Pointer(ch), flagIndir | (flag(Chan) << flagKindShift)}
1664 // MakeMap creates a new map of the specified type.
1665 func MakeMap(typ Type) Value {
1666 if typ.Kind() != Map {
1667 panic("reflect.MakeMap of non-map type")
1669 m := makemap(typ.runtimeType())
1670 return Value{typ.common(), unsafe.Pointer(m), flagIndir | (flag(Map) << flagKindShift)}
1673 // Indirect returns the value that v points to.
1674 // If v is a nil pointer, Indirect returns a zero Value.
1675 // If v is not a pointer, Indirect returns v.
1676 func Indirect(v Value) Value {
1677 if v.Kind() != Ptr {
1683 // ValueOf returns a new Value initialized to the concrete value
1684 // stored in the interface i. ValueOf(nil) returns the zero Value.
1685 func ValueOf(i interface{}) Value {
1690 // TODO(rsc): Eliminate this terrible hack.
1691 // In the call to packValue, eface.typ doesn't escape,
1692 // and eface.word is an integer. So it looks like
1693 // i (= eface) doesn't escape. But really it does,
1694 // because eface.word is actually a pointer.
1697 // For an interface value with the noAddr bit set,
1698 // the representation is identical to an empty interface.
1699 eface := *(*emptyInterface)(unsafe.Pointer(&i))
1700 typ := toCommonType(eface.typ)
1701 fl := flag(typ.Kind()) << flagKindShift
1702 if typ.Kind() != Ptr && typ.Kind() != UnsafePointer {
1705 return Value{typ, unsafe.Pointer(eface.word), fl}
1708 // Zero returns a Value representing a zero value for the specified type.
1709 // The result is different from the zero value of the Value struct,
1710 // which represents no value at all.
1711 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
1712 func Zero(typ Type) Value {
1714 panic("reflect: Zero(nil)")
1717 fl := flag(t.Kind()) << flagKindShift
1718 if t.Kind() == Ptr || t.Kind() == UnsafePointer {
1719 return Value{t, nil, fl}
1721 return Value{t, unsafe_New(typ), fl | flagIndir}
1724 // New returns a Value representing a pointer to a new zero value
1725 // for the specified type. That is, the returned Value's Type is PtrTo(t).
1726 func New(typ Type) Value {
1728 panic("reflect: New(nil)")
1730 ptr := unsafe_New(typ)
1731 fl := flag(Ptr) << flagKindShift
1732 return Value{typ.common().ptrTo(), ptr, fl}
1735 // NewAt returns a Value representing a pointer to a value of the
1736 // specified type, using p as that pointer.
1737 func NewAt(typ Type, p unsafe.Pointer) Value {
1738 fl := flag(Ptr) << flagKindShift
1739 return Value{typ.common().ptrTo(), p, fl}
1742 // assignTo returns a value v that can be assigned directly to typ.
1743 // It panics if v is not assignable to typ.
1744 // For a conversion to an interface type, target is a suggested scratch space to use.
1745 func (v Value) assignTo(context string, dst *commonType, target *interface{}) Value {
1746 if v.flag&flagMethod != 0 {
1747 panic(context + ": cannot assign method value to type " + dst.String())
1751 case directlyAssignable(dst, v.typ):
1752 // Overwrite type so that they match.
1753 // Same memory layout, so no harm done.
1755 fl := v.flag & (flagRO | flagAddr | flagIndir)
1756 fl |= flag(dst.Kind()) << flagKindShift
1757 return Value{dst, v.val, fl}
1759 case implements(dst, v.typ):
1761 target = new(interface{})
1763 x := valueInterface(v, false)
1764 if dst.NumMethod() == 0 {
1767 ifaceE2I(dst.runtimeType(), x, unsafe.Pointer(target))
1769 return Value{dst, unsafe.Pointer(target), flagIndir | flag(Interface)<<flagKindShift}
1773 panic(context + ": value of type " + v.typ.String() + " is not assignable to type " + dst.String())
1776 // implemented in ../pkg/runtime
1777 func chancap(ch iword) int32
1778 func chanclose(ch iword)
1779 func chanlen(ch iword) int32
1780 func chanrecv(t *runtimeType, ch iword, nb bool) (val iword, selected, received bool)
1781 func chansend(t *runtimeType, ch iword, val iword, nb bool) bool
1783 func makechan(typ *runtimeType, size uint32) (ch iword)
1784 func makemap(t *runtimeType) (m iword)
1785 func mapaccess(t *runtimeType, m iword, key iword) (val iword, ok bool)
1786 func mapassign(t *runtimeType, m iword, key, val iword, ok bool)
1787 func mapiterinit(t *runtimeType, m iword) *byte
1788 func mapiterkey(it *byte) (key iword, ok bool)
1789 func mapiternext(it *byte)
1790 func maplen(m iword) int32
1792 func call(typ *commonType, fnaddr unsafe.Pointer, isInterface bool, isMethod bool, params *unsafe.Pointer, results *unsafe.Pointer)
1793 func ifaceE2I(t *runtimeType, src interface{}, dst unsafe.Pointer)
1795 // Dummy annotation marking that the value x escapes,
1796 // for use in cases where the reflect code is so clever that
1797 // the compiler cannot follow.
1798 func escapes(x interface{}) {