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 ptrSize = unsafe.Sizeof((*byte)(nil))
15 const cannotSet = "cannot set value obtained from unexported struct field"
17 // TODO: This will have to go away when
18 // the new gc goes in.
19 func memmove(adst, asrc unsafe.Pointer, n uintptr) {
23 case src < dst && src+n > dst:
25 // careful: i is unsigned
28 *(*byte)(unsafe.Pointer(dst + i)) = *(*byte)(unsafe.Pointer(src + i))
30 case (n|src|dst)&(ptrSize-1) != 0:
32 for i := uintptr(0); i < n; i++ {
33 *(*byte)(unsafe.Pointer(dst + i)) = *(*byte)(unsafe.Pointer(src + i))
37 for i := uintptr(0); i < n; i += ptrSize {
38 *(*uintptr)(unsafe.Pointer(dst + i)) = *(*uintptr)(unsafe.Pointer(src + i))
43 // Value is the reflection interface to a Go value.
45 // Not all methods apply to all kinds of values. Restrictions,
46 // if any, are noted in the documentation for each method.
47 // Use the Kind method to find out the kind of value before
48 // calling kind-specific methods. Calling a method
49 // inappropriate to the kind of type causes a run time panic.
51 // The zero Value represents no value.
52 // Its IsValid method returns false, its Kind method returns Invalid,
53 // its String method returns "<invalid Value>", and all other methods panic.
54 // Most functions and methods never return an invalid value.
55 // If one does, its documentation states the conditions explicitly.
57 // The fields of Value are exported so that clients can copy and
58 // pass Values around, but they should not be edited or inspected
59 // directly. A future language change may make it possible not to
60 // export these fields while still keeping Values usable as values.
66 // A ValueError occurs when a Value method is invoked on
67 // a Value that does not support it. Such cases are documented
68 // in the description of each method.
69 type ValueError struct {
74 func (e *ValueError) Error() string {
76 return "reflect: call of " + e.Method + " on zero Value"
78 return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
81 // methodName returns the name of the calling method,
82 // assumed to be two stack frames above.
83 func methodName() string {
84 pc, _, _, _ := runtime.Caller(2)
85 f := runtime.FuncForPC(pc)
87 return "unknown method"
92 // An iword is the word that would be stored in an
93 // interface to represent a given value v. Specifically, if v is
94 // bigger than a pointer, its word is a pointer to v's data.
95 // Otherwise, its word is a zero uintptr with the data stored
96 // in the leading bytes.
99 func loadIword(p unsafe.Pointer, size uintptr) iword {
100 // Run the copy ourselves instead of calling memmove
101 // to avoid moving v to the heap.
105 panic("reflect: internal error: loadIword of " + strconv.Itoa(int(size)) + "-byte value")
108 *(*uint8)(unsafe.Pointer(&w)) = *(*uint8)(p)
110 *(*uint16)(unsafe.Pointer(&w)) = *(*uint16)(p)
112 *(*[3]byte)(unsafe.Pointer(&w)) = *(*[3]byte)(p)
114 *(*uint32)(unsafe.Pointer(&w)) = *(*uint32)(p)
116 *(*[5]byte)(unsafe.Pointer(&w)) = *(*[5]byte)(p)
118 *(*[6]byte)(unsafe.Pointer(&w)) = *(*[6]byte)(p)
120 *(*[7]byte)(unsafe.Pointer(&w)) = *(*[7]byte)(p)
122 *(*uint64)(unsafe.Pointer(&w)) = *(*uint64)(p)
127 func storeIword(p unsafe.Pointer, w iword, size uintptr) {
128 // Run the copy ourselves instead of calling memmove
129 // to avoid moving v to the heap.
132 panic("reflect: internal error: storeIword of " + strconv.Itoa(int(size)) + "-byte value")
135 *(*uint8)(p) = *(*uint8)(unsafe.Pointer(&w))
137 *(*uint16)(p) = *(*uint16)(unsafe.Pointer(&w))
139 *(*[3]byte)(p) = *(*[3]byte)(unsafe.Pointer(&w))
141 *(*uint32)(p) = *(*uint32)(unsafe.Pointer(&w))
143 *(*[5]byte)(p) = *(*[5]byte)(unsafe.Pointer(&w))
145 *(*[6]byte)(p) = *(*[6]byte)(unsafe.Pointer(&w))
147 *(*[7]byte)(p) = *(*[7]byte)(unsafe.Pointer(&w))
149 *(*uint64)(p) = *(*uint64)(unsafe.Pointer(&w))
153 // emptyInterface is the header for an interface{} value.
154 type emptyInterface struct {
159 // nonEmptyInterface is the header for a interface value with methods.
160 type nonEmptyInterface struct {
161 // see ../runtime/iface.c:/Itab
163 typ *runtime.Type // dynamic concrete type
164 fun [100000]unsafe.Pointer // method table
169 // Regarding the implementation of Value:
171 // The Internal interface is a true interface value in the Go sense,
172 // but it also serves as a (type, address) pair in which one cannot
173 // be changed separately from the other. That is, it serves as a way
174 // to prevent unsafe mutations of the Internal state even though
175 // we cannot (yet?) hide the field while preserving the ability for
176 // clients to make copies of Values.
178 // The internal method converts a Value into the expanded internalValue struct.
179 // If we could avoid exporting fields we'd probably make internalValue the
180 // definition of Value.
182 // If a Value is addressable (CanAddr returns true), then the Internal
183 // interface value holds a pointer to the actual field data, and Set stores
184 // through that pointer. If a Value is not addressable (CanAddr returns false),
185 // then the Internal interface value holds the actual value.
187 // In addition to whether a value is addressable, we track whether it was
188 // obtained by using an unexported struct field. Such values are allowed
189 // to be read, mainly to make fmt.Print more useful, but they are not
190 // allowed to be written. We call such values read-only.
192 // A Value can be set (via the Set, SetUint, etc. methods) only if it is both
193 // addressable and not read-only.
195 // The two permission bits - addressable and read-only - are stored in
196 // the bottom two bits of the type pointer in the interface value.
198 // ordinary value: Internal = value
199 // addressable value: Internal = value, Internal.typ |= flagAddr
200 // read-only value: Internal = value, Internal.typ |= flagRO
201 // addressable, read-only value: Internal = value, Internal.typ |= flagAddr | flagRO
203 // It is important that the read-only values have the extra bit set
204 // (as opposed to using the bit to mean writable), because client code
205 // can grab the interface field and try to use it. Having the extra bit
206 // set makes the type pointer compare not equal to any real type,
207 // so that a client cannot, say, write through v.Internal.(*int).
208 // The runtime routines that access interface types reject types with
211 // If a Value fv = v.Method(i), then fv = v with the InternalMethod
212 // field set to i+1. Methods are never addressable.
214 // All in all, this is a lot of effort just to avoid making this new API
215 // depend on a language change we'll probably do anyway, but
216 // it's helpful to keep the two separate, and much of the logic is
217 // necessary to implement the Interface method anyway.
220 flagAddr uint32 = 1 << iota // holds address of value
226 // An internalValue is the unpacked form of a Value.
227 // The zero Value unpacks to a zero internalValue
228 type internalValue struct {
229 typ *commonType // type of value
230 kind Kind // kind of value
239 func (v Value) internal() internalValue {
241 eface := *(*emptyInterface)(unsafe.Pointer(&v.Internal))
242 p := uintptr(unsafe.Pointer(eface.typ))
243 iv.typ = toCommonType((*runtime.Type)(unsafe.Pointer(p &^ reflectFlags)))
247 iv.flag = uint32(p & reflectFlags)
249 if iv.flag&flagAddr != 0 {
250 iv.addr = unsafe.Pointer(uintptr(iv.word))
251 iv.typ = iv.typ.Elem().common()
252 if Kind(iv.typ.kind) == Ptr || Kind(iv.typ.kind) == UnsafePointer {
253 iv.word = loadIword(iv.addr, iv.typ.size)
256 if Kind(iv.typ.kind) != Ptr && Kind(iv.typ.kind) != UnsafePointer {
257 iv.addr = unsafe.Pointer(uintptr(iv.word))
260 iv.kind = iv.typ.Kind()
262 // Is this a method? If so, iv describes the receiver.
263 // Rewrite to describe the method function.
264 if v.InternalMethod != 0 {
265 // If this Value is a method value (x.Method(i) for some Value x)
266 // then we will invoke it using the interface form of the method,
267 // which always passes the receiver as a single word.
268 // Record that information.
269 i := v.InternalMethod - 1
270 if iv.kind == Interface {
271 it := (*interfaceType)(unsafe.Pointer(iv.typ))
272 if i < 0 || i >= len(it.methods) {
273 panic("reflect: broken Value")
276 if m.pkgPath != nil {
279 iv.typ = toCommonType(m.typ)
280 iface := (*nonEmptyInterface)(iv.addr)
281 if iface.itab == nil {
285 iv.word = iword(uintptr(iface.itab.fun[i]))
289 ut := iv.typ.uncommon()
290 if ut == nil || i < 0 || i >= len(ut.methods) {
291 panic("reflect: broken Value")
294 if m.pkgPath != nil {
297 iv.typ = toCommonType(m.mtyp)
299 iv.word = iword(uintptr(m.tfn))
304 iv.word = iword(uintptr(unsafe.Pointer(p)))
309 iv.addr = unsafe.Pointer(uintptr(iv.word))
315 // packValue returns a Value with the given flag bits, type, and interface word.
316 func packValue(flag uint32, typ *runtime.Type, word iword) Value {
320 t := uintptr(unsafe.Pointer(typ))
322 eface := emptyInterface{(*runtime.Type)(unsafe.Pointer(t)), word}
323 return Value{Internal: *(*interface{})(unsafe.Pointer(&eface))}
331 // Dummy annotation marking that the value x escapes,
332 // for use in cases where the reflect code is so clever that
333 // the compiler cannot follow.
334 func escapes(x interface{}) {
340 // valueFromAddr returns a Value using the given type and address.
341 func valueFromAddr(flag uint32, typ Type, addr unsafe.Pointer) Value {
342 // TODO(rsc): Eliminate this terrible hack.
343 // The escape analysis knows that addr is a pointer
344 // but it doesn't see addr get passed to anything
345 // that keeps it. packValue keeps it, but packValue
346 // takes a uintptr (iword(addr)), and integers (non-pointers)
347 // are assumed not to matter. The escapes function works
348 // because return values always escape (for now).
351 if flag&flagAddr != 0 {
352 // Addressable, so the internal value is
353 // an interface containing a pointer to the real value.
354 return packValue(flag, PtrTo(typ).runtimeType(), iword(uintptr(addr)))
358 if k := typ.Kind(); k == Ptr || k == UnsafePointer {
359 // In line, so the interface word is the actual value.
360 w = loadIword(addr, typ.Size())
362 // Not in line: the interface word is the address.
363 w = iword(uintptr(addr))
365 return packValue(flag, typ.runtimeType(), w)
368 // valueFromIword returns a Value using the given type and interface word.
369 func valueFromIword(flag uint32, typ Type, w iword) Value {
370 if flag&flagAddr != 0 {
371 panic("reflect: internal error: valueFromIword addressable")
373 return packValue(flag, typ.runtimeType(), w)
376 func (iv internalValue) mustBe(want Kind) {
378 panic(&ValueError{methodName(), iv.kind})
382 func (iv internalValue) mustBeExported() {
384 panic(&ValueError{methodName(), iv.kind})
386 if iv.flag&flagRO != 0 {
387 panic(methodName() + " using value obtained using unexported field")
391 func (iv internalValue) mustBeAssignable() {
393 panic(&ValueError{methodName(), iv.kind})
395 // Assignable if addressable and not read-only.
396 if iv.flag&flagRO != 0 {
397 panic(methodName() + " using value obtained using unexported field")
399 if iv.flag&flagAddr == 0 {
400 panic(methodName() + " using unaddressable value")
404 // Addr returns a pointer value representing the address of v.
405 // It panics if CanAddr() returns false.
406 // Addr is typically used to obtain a pointer to a struct field
407 // or slice element in order to call a method that requires a
409 func (v Value) Addr() Value {
411 if iv.flag&flagAddr == 0 {
412 panic("reflect.Value.Addr of unaddressable value")
414 return valueFromIword(iv.flag&flagRO, PtrTo(iv.typ.toType()), iword(uintptr(iv.addr)))
417 // Bool returns v's underlying value.
418 // It panics if v's kind is not Bool.
419 func (v Value) Bool() bool {
422 return *(*bool)(unsafe.Pointer(iv.addr))
425 // Bytes returns v's underlying value.
426 // It panics if v's underlying value is not a slice of bytes.
427 func (v Value) Bytes() []byte {
430 typ := iv.typ.toType()
431 if typ.Elem().Kind() != Uint8 {
432 panic("reflect.Value.Bytes of non-byte slice")
434 return *(*[]byte)(iv.addr)
437 // CanAddr returns true if the value's address can be obtained with Addr.
438 // Such values are called addressable. A value is addressable if it is
439 // an element of a slice, an element of an addressable array,
440 // a field of an addressable struct, or the result of dereferencing a pointer.
441 // If CanAddr returns false, calling Addr will panic.
442 func (v Value) CanAddr() bool {
444 return iv.flag&flagAddr != 0
447 // CanSet returns true if the value of v can be changed.
448 // A Value can be changed only if it is addressable and was not
449 // obtained by the use of unexported struct fields.
450 // If CanSet returns false, calling Set or any type-specific
451 // setter (e.g., SetBool, SetInt64) will panic.
452 func (v Value) CanSet() bool {
454 return iv.flag&(flagAddr|flagRO) == flagAddr
457 // Call calls the function v with the input arguments in.
458 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]).
459 // Call panics if v's Kind is not Func.
460 // It returns the output results as Values.
461 // As in Go, each input argument must be assignable to the
462 // type of the function's corresponding input parameter.
463 // If v is a variadic function, Call creates the variadic slice parameter
464 // itself, copying in the corresponding values.
465 func (v Value) Call(in []Value) []Value {
469 return iv.call("Call", in)
472 // CallSlice calls the variadic function v with the input arguments in,
473 // assigning the slice in[len(in)-1] to v's final variadic argument.
474 // For example, if len(in) == 3, v.Call(in) represents the Go call v(in[0], in[1], in[2]...).
475 // Call panics if v's Kind is not Func or if v is not variadic.
476 // It returns the output results as Values.
477 // As in Go, each input argument must be assignable to the
478 // type of the function's corresponding input parameter.
479 func (v Value) CallSlice(in []Value) []Value {
483 return iv.call("CallSlice", in)
486 func (iv internalValue) call(method string, in []Value) []Value {
489 panic("reflect.Value.Call: call of method on nil interface value")
491 panic("reflect.Value.Call: call of nil function")
494 isSlice := method == "CallSlice"
499 panic("reflect: CallSlice of non-variadic function")
502 panic("reflect: CallSlice with too few input arguments")
505 panic("reflect: CallSlice with too many input arguments")
512 panic("reflect: Call with too few input arguments")
514 if !t.IsVariadic() && len(in) > n {
515 panic("reflect: Call with too many input arguments")
518 for _, x := range in {
519 if x.Kind() == Invalid {
520 panic("reflect: " + method + " using zero Value argument")
523 for i := 0; i < n; i++ {
524 if xt, targ := in[i].Type(), t.In(i); !xt.AssignableTo(targ) {
525 panic("reflect: " + method + " using " + xt.String() + " as type " + targ.String())
528 if !isSlice && t.IsVariadic() {
529 // prepare slice for remaining values
531 slice := MakeSlice(t.In(n), m, m)
532 elem := t.In(n).Elem()
533 for i := 0; i < m; i++ {
535 if xt := x.Type(); !xt.AssignableTo(elem) {
536 panic("reflect: cannot use " + xt.String() + " as type " + elem.String() + " in " + method)
538 slice.Index(i).Set(x)
541 in = make([]Value, n+1)
547 if nin != t.NumIn() {
548 panic("reflect.Value.Call: wrong argument count")
555 params := make([]unsafe.Pointer, nin)
559 // Hard-wired first argument.
562 params[0] = unsafe.Pointer(p)
566 first_pointer := false
567 for i, v := range in {
570 targ := t.In(i).(*commonType)
571 siv = convertForAssignment("reflect.Value.Call", nil, targ, siv)
573 p := new(unsafe.Pointer)
574 *p = unsafe.Pointer(uintptr(siv.word))
575 params[off] = unsafe.Pointer(p)
577 params[off] = siv.addr
579 if i == 0 && Kind(targ.kind) != Ptr && !iv.method && isMethod(iv.typ) {
580 p := new(unsafe.Pointer)
582 params[off] = unsafe.Pointer(p)
588 ret := make([]Value, nout)
589 results := make([]unsafe.Pointer, nout)
590 for i := 0; i < nout; i++ {
592 results[i] = unsafe.Pointer(v.Pointer())
596 var pp *unsafe.Pointer
600 var pr *unsafe.Pointer
601 if len(results) > 0 {
605 call(t, *(*unsafe.Pointer)(iv.addr), iv.method, first_pointer, pp, pr)
610 // gccgo specific test to see if typ is a method. We can tell by
611 // looking at the string to see if there is a receiver. We need this
612 // because for gccgo all methods take pointer receivers.
613 func isMethod(t *commonType) bool {
614 if Kind(t.kind) != Func {
621 for i, c := range s {
627 } else if parens == 0 && c == ' ' && s[i + 1] != '(' && !sawRet {
635 // Cap returns v's capacity.
636 // It panics if v's Kind is not Array, Chan, or Slice.
637 func (v Value) Cap() int {
643 return int(chancap(*(*iword)(iv.addr)))
645 return (*SliceHeader)(iv.addr).Cap
647 panic(&ValueError{"reflect.Value.Cap", iv.kind})
650 // Close closes the channel v.
651 // It panics if v's Kind is not Chan.
652 func (v Value) Close() {
656 ch := *(*iword)(iv.addr)
660 // Complex returns v's underlying value, as a complex128.
661 // It panics if v's Kind is not Complex64 or Complex128
662 func (v Value) Complex() complex128 {
666 return complex128(*(*complex64)(iv.addr))
668 return *(*complex128)(iv.addr)
670 panic(&ValueError{"reflect.Value.Complex", iv.kind})
673 // Elem returns the value that the interface v contains
674 // or that the pointer v points to.
675 // It panics if v's Kind is not Interface or Ptr.
676 // It returns the zero Value if v is nil.
677 func (v Value) Elem() Value {
682 func (iv internalValue) Elem() Value {
685 // Empty interface and non-empty interface have different layouts.
686 // Convert to empty interface.
687 var eface emptyInterface
688 if iv.typ.NumMethod() == 0 {
689 eface = *(*emptyInterface)(iv.addr)
691 iface := (*nonEmptyInterface)(iv.addr)
692 if iface.itab != nil {
693 eface.typ = iface.itab.typ
695 eface.word = iface.word
697 if eface.typ == nil {
700 return valueFromIword(iv.flag&flagRO, toType(eface.typ), eface.word)
703 // The returned value's address is v's value.
707 return valueFromAddr(iv.flag&flagRO|flagAddr, iv.typ.Elem(), unsafe.Pointer(uintptr(iv.word)))
709 panic(&ValueError{"reflect.Value.Elem", iv.kind})
712 // Field returns the i'th field of the struct v.
713 // It panics if v's Kind is not Struct or i is out of range.
714 func (v Value) Field(i int) Value {
718 if i < 0 || i >= t.NumField() {
719 panic("reflect: Field index out of range")
723 // Inherit permission bits from v.
725 // Using an unexported field forces flagRO.
729 return valueFromValueOffset(flag, f.Type, iv, f.Offset)
732 // valueFromValueOffset returns a sub-value of outer
733 // (outer is an array or a struct) with the given flag and type
734 // starting at the given byte offset into outer.
735 func valueFromValueOffset(flag uint32, typ Type, outer internalValue, offset uintptr) Value {
736 if outer.addr != nil {
737 return valueFromAddr(flag, typ, unsafe.Pointer(uintptr(outer.addr)+offset))
740 // outer is so tiny it is in line.
741 // We have to use outer.word and derive
742 // the new word (it cannot possibly be bigger).
743 // In line, so not addressable.
744 if flag&flagAddr != 0 {
745 panic("reflect: internal error: misuse of valueFromValueOffset")
747 b := *(*[ptrSize]byte)(unsafe.Pointer(&outer.word))
748 for i := uintptr(0); i < typ.Size(); i++ {
751 for i := typ.Size(); i < ptrSize; i++ {
754 w := *(*iword)(unsafe.Pointer(&b))
755 return valueFromIword(flag, typ, w)
758 // FieldByIndex returns the nested field corresponding to index.
759 // It panics if v's Kind is not struct.
760 func (v Value) FieldByIndex(index []int) Value {
761 v.internal().mustBe(Struct)
762 for i, x := range index {
764 if v.Kind() == Ptr && v.Elem().Kind() == Struct {
773 // FieldByName returns the struct field with the given name.
774 // It returns the zero Value if no field was found.
775 // It panics if v's Kind is not struct.
776 func (v Value) FieldByName(name string) Value {
779 if f, ok := iv.typ.FieldByName(name); ok {
780 return v.FieldByIndex(f.Index)
785 // FieldByNameFunc returns the struct field with a name
786 // that satisfies the match function.
787 // It panics if v's Kind is not struct.
788 // It returns the zero Value if no field was found.
789 func (v Value) FieldByNameFunc(match func(string) bool) Value {
790 v.internal().mustBe(Struct)
791 if f, ok := v.Type().FieldByNameFunc(match); ok {
792 return v.FieldByIndex(f.Index)
797 // Float returns v's underlying value, as an float64.
798 // It panics if v's Kind is not Float32 or Float64
799 func (v Value) Float() float64 {
803 return float64(*(*float32)(iv.addr))
805 return *(*float64)(iv.addr)
807 panic(&ValueError{"reflect.Value.Float", iv.kind})
810 // Index returns v's i'th element.
811 // It panics if v's Kind is not Array or Slice or i is out of range.
812 func (v Value) Index(i int) Value {
816 panic(&ValueError{"reflect.Value.Index", iv.kind})
818 flag := iv.flag // element flag same as overall array
820 if i < 0 || i > t.Len() {
821 panic("reflect: array index out of range")
824 return valueFromValueOffset(flag, typ, iv, uintptr(i)*typ.Size())
827 // Element flag same as Elem of Ptr.
828 // Addressable, possibly read-only.
829 flag := iv.flag&flagRO | flagAddr
830 s := (*SliceHeader)(iv.addr)
831 if i < 0 || i >= s.Len {
832 panic("reflect: slice index out of range")
835 addr := unsafe.Pointer(s.Data + uintptr(i)*typ.Size())
836 return valueFromAddr(flag, typ, addr)
842 // Int returns v's underlying value, as an int64.
843 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.
844 func (v Value) Int() int64 {
848 return int64(*(*int)(iv.addr))
850 return int64(*(*int8)(iv.addr))
852 return int64(*(*int16)(iv.addr))
854 return int64(*(*int32)(iv.addr))
856 return *(*int64)(iv.addr)
858 panic(&ValueError{"reflect.Value.Int", iv.kind})
861 // CanInterface returns true if Interface can be used without panicking.
862 func (v Value) CanInterface() bool {
864 if iv.kind == Invalid {
865 panic(&ValueError{"reflect.Value.CanInterface", iv.kind})
867 return v.InternalMethod == 0 && iv.flag&flagRO == 0
870 // Interface returns v's value as an interface{}.
871 // If v is a method obtained by invoking Value.Method
872 // (as opposed to Type.Method), Interface cannot return an
873 // interface value, so it panics.
874 func (v Value) Interface() interface{} {
875 return valueInterface(v, true)
878 func valueInterface(v Value, safe bool) interface{} {
880 return iv.valueInterface(safe)
883 func (iv internalValue) valueInterface(safe bool) interface{} {
885 panic(&ValueError{"reflect.Value.Interface", iv.kind})
888 panic("reflect.Value.Interface: cannot create interface value for method with bound receiver")
891 if safe && iv.flag&flagRO != 0 {
892 // Do not allow access to unexported values via Interface,
893 // because they might be pointers that should not be
894 // writable or methods or function that should not be callable.
895 panic("reflect.Value.Interface: cannot return value obtained from unexported field or method")
897 if iv.kind == Interface {
898 // Special case: return the element inside the interface.
899 // Won't recurse further because an interface cannot contain an interface.
903 return iv.Elem().Interface()
906 // Non-interface value.
907 var eface emptyInterface
908 eface.typ = iv.typ.runtimeType()
910 return *(*interface{})(unsafe.Pointer(&eface))
913 // InterfaceData returns the interface v's value as a uintptr pair.
914 // It panics if v's Kind is not Interface.
915 func (v Value) InterfaceData() [2]uintptr {
918 // We treat this as a read operation, so we allow
919 // it even for unexported data, because the caller
920 // has to import "unsafe" to turn it into something
921 // that can be abused.
922 return *(*[2]uintptr)(iv.addr)
925 // IsNil returns true if v is a nil value.
926 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
927 func (v Value) IsNil() bool {
928 return v.internal().IsNil()
931 func (iv internalValue) IsNil() bool {
935 panic("reflect: IsNil of method Value")
938 case Chan, Func, Map:
940 panic("reflect: IsNil of method Value")
942 return *(*uintptr)(iv.addr) == 0
943 case Interface, Slice:
944 // Both interface and slice are nil if first word is 0.
945 return *(*uintptr)(iv.addr) == 0
947 panic(&ValueError{"reflect.Value.IsNil", iv.kind})
950 // IsValid returns true if v represents a value.
951 // It returns false if v is the zero Value.
952 // If IsValid returns false, all other methods except String panic.
953 // Most functions and methods never return an invalid value.
954 // If one does, its documentation states the conditions explicitly.
955 func (v Value) IsValid() bool {
956 return v.Internal != nil
959 // Kind returns v's Kind.
960 // If v is the zero Value (IsValid returns false), Kind returns Invalid.
961 func (v Value) Kind() Kind {
962 return v.internal().kind
965 // Len returns v's length.
966 // It panics if v's Kind is not Array, Chan, Map, Slice, or String.
967 func (v Value) Len() int {
973 return int(chanlen(*(*iword)(iv.addr)))
975 return int(maplen(*(*iword)(iv.addr)))
977 return (*SliceHeader)(iv.addr).Len
979 return (*StringHeader)(iv.addr).Len
981 panic(&ValueError{"reflect.Value.Len", iv.kind})
984 // MapIndex returns the value associated with key in the map v.
985 // It panics if v's Kind is not Map.
986 // It returns the zero Value if key is not found in the map or if v represents a nil map.
987 // As in Go, the key's value must be assignable to the map's key type.
988 func (v Value) MapIndex(key Value) Value {
991 typ := iv.typ.toType()
993 // Do not require ikey to be exported, so that DeepEqual
994 // and other programs can use all the keys returned by
995 // MapKeys as arguments to MapIndex. If either the map
996 // or the key is unexported, though, the result will be
997 // considered unexported.
999 ikey := key.internal()
1000 ikey = convertForAssignment("reflect.Value.MapIndex", nil, typ.Key(), ikey)
1005 flag := (iv.flag | ikey.flag) & flagRO
1006 elemType := typ.Elem()
1007 elemWord, ok := mapaccess(typ.runtimeType(), *(*iword)(iv.addr), ikey.word)
1011 return valueFromIword(flag, elemType, elemWord)
1014 // MapKeys returns a slice containing all the keys present in the map,
1015 // in unspecified order.
1016 // It panics if v's Kind is not Map.
1017 // It returns an empty slice if v represents a nil map.
1018 func (v Value) MapKeys() []Value {
1021 keyType := iv.typ.Key()
1023 flag := iv.flag & flagRO
1024 m := *(*iword)(iv.addr)
1029 it := mapiterinit(iv.typ.runtimeType(), m)
1030 a := make([]Value, mlen)
1032 for i = 0; i < len(a); i++ {
1033 keyWord, ok := mapiterkey(it)
1037 a[i] = valueFromIword(flag, keyType, keyWord)
1043 // Method returns a function value corresponding to v's i'th method.
1044 // The arguments to a Call on the returned function should not include
1045 // a receiver; the returned function will always use v as the receiver.
1046 // Method panics if i is out of range.
1047 func (v Value) Method(i int) Value {
1049 if iv.kind == Invalid {
1050 panic(&ValueError{"reflect.Value.Method", Invalid})
1052 if i < 0 || i >= iv.typ.NumMethod() {
1053 panic("reflect: Method index out of range")
1055 return Value{v.Internal, i + 1}
1058 // NumMethod returns the number of methods in the value's method set.
1059 func (v Value) NumMethod() int {
1061 if iv.kind == Invalid {
1062 panic(&ValueError{"reflect.Value.NumMethod", Invalid})
1064 return iv.typ.NumMethod()
1067 // MethodByName returns a function value corresponding to the method
1068 // of v with the given name.
1069 // The arguments to a Call on the returned function should not include
1070 // a receiver; the returned function will always use v as the receiver.
1071 // It returns the zero Value if no method was found.
1072 func (v Value) MethodByName(name string) Value {
1074 if iv.kind == Invalid {
1075 panic(&ValueError{"reflect.Value.MethodByName", Invalid})
1077 m, ok := iv.typ.MethodByName(name)
1079 return Value{v.Internal, m.Index + 1}
1084 // NumField returns the number of fields in the struct v.
1085 // It panics if v's Kind is not Struct.
1086 func (v Value) NumField() int {
1089 return iv.typ.NumField()
1092 // OverflowComplex returns true if the complex128 x cannot be represented by v's type.
1093 // It panics if v's Kind is not Complex64 or Complex128.
1094 func (v Value) OverflowComplex(x complex128) bool {
1098 return overflowFloat32(real(x)) || overflowFloat32(imag(x))
1102 panic(&ValueError{"reflect.Value.OverflowComplex", iv.kind})
1105 // OverflowFloat returns true if the float64 x cannot be represented by v's type.
1106 // It panics if v's Kind is not Float32 or Float64.
1107 func (v Value) OverflowFloat(x float64) bool {
1111 return overflowFloat32(x)
1115 panic(&ValueError{"reflect.Value.OverflowFloat", iv.kind})
1118 func overflowFloat32(x float64) bool {
1122 return math.MaxFloat32 <= x && x <= math.MaxFloat64
1125 // OverflowInt returns true if the int64 x cannot be represented by v's type.
1126 // It panics if v's Kind is not Int, Int8, int16, Int32, or Int64.
1127 func (v Value) OverflowInt(x int64) bool {
1130 case Int, Int8, Int16, Int32, Int64:
1131 bitSize := iv.typ.size * 8
1132 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
1135 panic(&ValueError{"reflect.Value.OverflowInt", iv.kind})
1138 // OverflowUint returns true if the uint64 x cannot be represented by v's type.
1139 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1140 func (v Value) OverflowUint(x uint64) bool {
1143 case Uint, Uintptr, Uint8, Uint16, Uint32, Uint64:
1144 bitSize := iv.typ.size * 8
1145 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
1148 panic(&ValueError{"reflect.Value.OverflowUint", iv.kind})
1151 // Pointer returns v's value as a uintptr.
1152 // It returns uintptr instead of unsafe.Pointer so that
1153 // code using reflect cannot obtain unsafe.Pointers
1154 // without importing the unsafe package explicitly.
1155 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
1156 func (v Value) Pointer() uintptr {
1159 case Ptr, UnsafePointer:
1160 if iv.kind == Func && v.InternalMethod != 0 {
1161 panic("reflect.Value.Pointer of method Value")
1163 return uintptr(iv.word)
1164 case Chan, Func, Map:
1165 if iv.kind == Func && v.InternalMethod != 0 {
1166 panic("reflect.Value.Pointer of method Value")
1168 return *(*uintptr)(iv.addr)
1170 return (*SliceHeader)(iv.addr).Data
1172 panic(&ValueError{"reflect.Value.Pointer", iv.kind})
1175 // Recv receives and returns a value from the channel v.
1176 // It panics if v's Kind is not Chan.
1177 // The receive blocks until a value is ready.
1178 // The boolean value ok is true if the value x corresponds to a send
1179 // on the channel, false if it is a zero value received because the channel is closed.
1180 func (v Value) Recv() (x Value, ok bool) {
1184 return iv.recv(false)
1187 // internal recv, possibly non-blocking (nb)
1188 func (iv internalValue) recv(nb bool) (val Value, ok bool) {
1189 t := iv.typ.toType()
1190 if t.ChanDir()&RecvDir == 0 {
1191 panic("recv on send-only channel")
1193 ch := *(*iword)(iv.addr)
1195 panic("recv on nil channel")
1197 valWord, selected, ok := chanrecv(iv.typ.runtimeType(), ch, nb)
1199 val = valueFromIword(0, t.Elem(), valWord)
1204 // Send sends x on the channel v.
1205 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
1206 // As in Go, x's value must be assignable to the channel's element type.
1207 func (v Value) Send(x Value) {
1214 // internal send, possibly non-blocking
1215 func (iv internalValue) send(x Value, nb bool) (selected bool) {
1216 t := iv.typ.toType()
1217 if t.ChanDir()&SendDir == 0 {
1218 panic("send on recv-only channel")
1221 ix.mustBeExported() // do not let unexported x leak
1222 ix = convertForAssignment("reflect.Value.Send", nil, t.Elem(), ix)
1223 ch := *(*iword)(iv.addr)
1225 panic("send on nil channel")
1227 return chansend(iv.typ.runtimeType(), ch, ix.word, nb)
1230 // Set assigns x to the value v.
1231 // It panics if CanSet returns false.
1232 // As in Go, x's value must be assignable to v's type.
1233 func (v Value) Set(x Value) {
1237 iv.mustBeAssignable()
1238 ix.mustBeExported() // do not let unexported x leak
1240 ix = convertForAssignment("reflect.Set", iv.addr, iv.typ, ix)
1243 if Kind(ix.typ.kind) == Ptr || Kind(ix.typ.kind) == UnsafePointer {
1244 storeIword(iv.addr, ix.word, n)
1246 memmove(iv.addr, ix.addr, n)
1250 // SetBool sets v's underlying value.
1251 // It panics if v's Kind is not Bool or if CanSet() is false.
1252 func (v Value) SetBool(x bool) {
1254 iv.mustBeAssignable()
1256 *(*bool)(iv.addr) = x
1259 // SetBytes sets v's underlying value.
1260 // It panics if v's underlying value is not a slice of bytes.
1261 func (v Value) SetBytes(x []byte) {
1263 iv.mustBeAssignable()
1265 typ := iv.typ.toType()
1266 if typ.Elem().Kind() != Uint8 {
1267 panic("reflect.Value.SetBytes of non-byte slice")
1269 *(*[]byte)(iv.addr) = x
1272 // SetComplex sets v's underlying value to x.
1273 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false.
1274 func (v Value) SetComplex(x complex128) {
1276 iv.mustBeAssignable()
1279 panic(&ValueError{"reflect.Value.SetComplex", iv.kind})
1281 *(*complex64)(iv.addr) = complex64(x)
1283 *(*complex128)(iv.addr) = x
1287 // SetFloat sets v's underlying value to x.
1288 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
1289 func (v Value) SetFloat(x float64) {
1291 iv.mustBeAssignable()
1294 panic(&ValueError{"reflect.Value.SetFloat", iv.kind})
1296 *(*float32)(iv.addr) = float32(x)
1298 *(*float64)(iv.addr) = x
1302 // SetInt sets v's underlying value to x.
1303 // It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64, or if CanSet() is false.
1304 func (v Value) SetInt(x int64) {
1306 iv.mustBeAssignable()
1309 panic(&ValueError{"reflect.Value.SetInt", iv.kind})
1311 *(*int)(iv.addr) = int(x)
1313 *(*int8)(iv.addr) = int8(x)
1315 *(*int16)(iv.addr) = int16(x)
1317 *(*int32)(iv.addr) = int32(x)
1319 *(*int64)(iv.addr) = x
1323 // SetLen sets v's length to n.
1324 // It panics if v's Kind is not Slice.
1325 func (v Value) SetLen(n int) {
1327 iv.mustBeAssignable()
1329 s := (*SliceHeader)(iv.addr)
1330 if n < 0 || n > int(s.Cap) {
1331 panic("reflect: slice length out of range in SetLen")
1336 // SetMapIndex sets the value associated with key in the map v to val.
1337 // It panics if v's Kind is not Map.
1338 // If val is the zero Value, SetMapIndex deletes the key from the map.
1339 // As in Go, key's value must be assignable to the map's key type,
1340 // and val's value must be assignable to the map's value type.
1341 func (v Value) SetMapIndex(key, val Value) {
1343 ikey := key.internal()
1344 ival := val.internal()
1349 ikey.mustBeExported()
1350 ikey = convertForAssignment("reflect.Value.SetMapIndex", nil, iv.typ.Key(), ikey)
1352 if ival.kind != Invalid {
1353 ival.mustBeExported()
1354 ival = convertForAssignment("reflect.Value.SetMapIndex", nil, iv.typ.Elem(), ival)
1357 mapassign(iv.typ.runtimeType(), *(*iword)(iv.addr), ikey.word, ival.word, ival.kind != Invalid)
1360 // SetUint sets v's underlying value to x.
1361 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64, or if CanSet() is false.
1362 func (v Value) SetUint(x uint64) {
1364 iv.mustBeAssignable()
1367 panic(&ValueError{"reflect.Value.SetUint", iv.kind})
1369 *(*uint)(iv.addr) = uint(x)
1371 *(*uint8)(iv.addr) = uint8(x)
1373 *(*uint16)(iv.addr) = uint16(x)
1375 *(*uint32)(iv.addr) = uint32(x)
1377 *(*uint64)(iv.addr) = x
1379 *(*uintptr)(iv.addr) = uintptr(x)
1383 // SetPointer sets the unsafe.Pointer value v to x.
1384 // It panics if v's Kind is not UnsafePointer.
1385 func (v Value) SetPointer(x unsafe.Pointer) {
1387 iv.mustBeAssignable()
1388 iv.mustBe(UnsafePointer)
1389 *(*unsafe.Pointer)(iv.addr) = x
1392 // SetString sets v's underlying value to x.
1393 // It panics if v's Kind is not String or if CanSet() is false.
1394 func (v Value) SetString(x string) {
1396 iv.mustBeAssignable()
1398 *(*string)(iv.addr) = x
1401 // Slice returns a slice of v.
1402 // It panics if v's Kind is not Array or Slice.
1403 func (v Value) Slice(beg, end int) Value {
1405 if iv.kind != Array && iv.kind != Slice {
1406 panic(&ValueError{"reflect.Value.Slice", iv.kind})
1409 if beg < 0 || end < beg || end > cap {
1410 panic("reflect.Value.Slice: slice index out of bounds")
1416 if iv.flag&flagAddr == 0 {
1417 panic("reflect.Value.Slice: slice of unaddressable array")
1419 typ = toType((*arrayType)(unsafe.Pointer(iv.typ)).slice)
1420 base = uintptr(iv.addr)
1422 typ = iv.typ.toType()
1423 base = (*SliceHeader)(iv.addr).Data
1426 // Declare slice so that gc can see the base pointer in it.
1429 // Reinterpret as *SliceHeader to edit.
1430 s := (*SliceHeader)(unsafe.Pointer(&x))
1431 s.Data = base + uintptr(beg)*typ.Elem().Size()
1435 return valueFromAddr(iv.flag&flagRO, typ, unsafe.Pointer(&x))
1438 // String returns the string v's underlying value, as a string.
1439 // String is a special case because of Go's String method convention.
1440 // Unlike the other getters, it does not panic if v's Kind is not String.
1441 // Instead, it returns a string of the form "<T value>" where T is v's type.
1442 func (v Value) String() string {
1446 return "<invalid Value>"
1448 return *(*string)(iv.addr)
1450 // If you call String on a reflect.Value of other type, it's better to
1451 // print something than to panic. Useful in debugging.
1452 return "<" + iv.typ.String() + " Value>"
1455 // TryRecv attempts to receive a value from the channel v but will not block.
1456 // It panics if v's Kind is not Chan.
1457 // If the receive cannot finish without blocking, x is the zero Value.
1458 // The boolean ok is true if the value x corresponds to a send
1459 // on the channel, false if it is a zero value received because the channel is closed.
1460 func (v Value) TryRecv() (x Value, ok bool) {
1464 return iv.recv(true)
1467 // TrySend attempts to send x on the channel v but will not block.
1468 // It panics if v's Kind is not Chan.
1469 // It returns true if the value was sent, false otherwise.
1470 // As in Go, x's value must be assignable to the channel's element type.
1471 func (v Value) TrySend(x Value) bool {
1475 return iv.send(x, true)
1478 // Type returns v's type.
1479 func (v Value) Type() Type {
1480 t := v.internal().typ
1482 panic(&ValueError{"reflect.Value.Type", Invalid})
1487 // Uint returns v's underlying value, as a uint64.
1488 // It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.
1489 func (v Value) Uint() uint64 {
1493 return uint64(*(*uint)(iv.addr))
1495 return uint64(*(*uint8)(iv.addr))
1497 return uint64(*(*uint16)(iv.addr))
1499 return uint64(*(*uint32)(iv.addr))
1501 return uint64(*(*uintptr)(iv.addr))
1503 return *(*uint64)(iv.addr)
1505 panic(&ValueError{"reflect.Value.Uint", iv.kind})
1508 // UnsafeAddr returns a pointer to v's data.
1509 // It is for advanced clients that also import the "unsafe" package.
1510 // It panics if v is not addressable.
1511 func (v Value) UnsafeAddr() uintptr {
1513 if iv.kind == Invalid {
1514 panic(&ValueError{"reflect.Value.UnsafeAddr", iv.kind})
1516 if iv.flag&flagAddr == 0 {
1517 panic("reflect.Value.UnsafeAddr of unaddressable value")
1519 return uintptr(iv.addr)
1522 // StringHeader is the runtime representation of a string.
1523 // It cannot be used safely or portably.
1524 type StringHeader struct {
1529 // SliceHeader is the runtime representation of a slice.
1530 // It cannot be used safely or portably.
1531 type SliceHeader struct {
1537 func typesMustMatch(what string, t1, t2 Type) {
1539 panic("reflect: " + what + ": " + t1.String() + " != " + t2.String())
1543 // grow grows the slice s so that it can hold extra more values, allocating
1544 // more capacity if needed. It also returns the old and new slice lengths.
1545 func grow(s Value, extra int) (Value, int, int) {
1549 panic("reflect.Append: slice overflow")
1553 return s.Slice(0, i1), i0, i1
1566 t := MakeSlice(s.Type(), i1, m)
1571 // Append appends the values x to a slice s and returns the resulting slice.
1572 // As in Go, each x's value must be assignable to the slice's element type.
1573 func Append(s Value, x ...Value) Value {
1574 s.internal().mustBe(Slice)
1575 s, i0, i1 := grow(s, len(x))
1576 for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
1577 s.Index(i).Set(x[j])
1582 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
1583 // The slices s and t must have the same element type.
1584 func AppendSlice(s, t Value) Value {
1585 s.internal().mustBe(Slice)
1586 t.internal().mustBe(Slice)
1587 typesMustMatch("reflect.AppendSlice", s.Type().Elem(), t.Type().Elem())
1588 s, i0, i1 := grow(s, t.Len())
1589 Copy(s.Slice(i0, i1), t)
1593 // Copy copies the contents of src into dst until either
1594 // dst has been filled or src has been exhausted.
1595 // It returns the number of elements copied.
1596 // Dst and src each must have kind Slice or Array, and
1597 // dst and src must have the same element type.
1598 func Copy(dst, src Value) int {
1599 idst := dst.internal()
1600 isrc := src.internal()
1602 if idst.kind != Array && idst.kind != Slice {
1603 panic(&ValueError{"reflect.Copy", idst.kind})
1605 if idst.kind == Array {
1606 idst.mustBeAssignable()
1608 idst.mustBeExported()
1609 if isrc.kind != Array && isrc.kind != Slice {
1610 panic(&ValueError{"reflect.Copy", isrc.kind})
1612 isrc.mustBeExported()
1614 de := idst.typ.Elem()
1615 se := isrc.typ.Elem()
1616 typesMustMatch("reflect.Copy", de, se)
1619 if sn := src.Len(); n > sn {
1623 // If sk is an in-line array, cannot take its address.
1624 // Instead, copy element by element.
1625 if isrc.addr == nil {
1626 for i := 0; i < n; i++ {
1627 dst.Index(i).Set(src.Index(i))
1632 // Copy via memmove.
1633 var da, sa unsafe.Pointer
1634 if idst.kind == Array {
1637 da = unsafe.Pointer((*SliceHeader)(idst.addr).Data)
1639 if isrc.kind == Array {
1642 sa = unsafe.Pointer((*SliceHeader)(isrc.addr).Data)
1644 memmove(da, sa, uintptr(n)*de.Size())
1652 // MakeSlice creates a new zero-initialized slice value
1653 // for the specified slice type, length, and capacity.
1654 func MakeSlice(typ Type, len, cap int) Value {
1655 if typ.Kind() != Slice {
1656 panic("reflect: MakeSlice of non-slice type")
1659 // Declare slice so that gc can see the base pointer in it.
1662 // Reinterpret as *SliceHeader to edit.
1663 s := (*SliceHeader)(unsafe.Pointer(&x))
1664 s.Data = uintptr(unsafe.NewArray(typ.Elem(), cap))
1668 return valueFromAddr(0, typ, unsafe.Pointer(&x))
1671 // MakeChan creates a new channel with the specified type and buffer size.
1672 func MakeChan(typ Type, buffer int) Value {
1673 if typ.Kind() != Chan {
1674 panic("reflect: MakeChan of non-chan type")
1677 panic("MakeChan: negative buffer size")
1679 if typ.ChanDir() != BothDir {
1680 panic("MakeChan: unidirectional channel type")
1682 ch := makechan(typ.runtimeType(), uint32(buffer))
1683 return valueFromIword(0, typ, ch)
1686 // MakeMap creates a new map of the specified type.
1687 func MakeMap(typ Type) Value {
1688 if typ.Kind() != Map {
1689 panic("reflect: MakeMap of non-map type")
1691 m := makemap(typ.runtimeType())
1692 return valueFromIword(0, typ, m)
1695 // Indirect returns the value that v points to.
1696 // If v is a nil pointer, Indirect returns a nil Value.
1697 // If v is not a pointer, Indirect returns v.
1698 func Indirect(v Value) Value {
1699 if v.Kind() != Ptr {
1705 // ValueOf returns a new Value initialized to the concrete value
1706 // stored in the interface i. ValueOf(nil) returns the zero Value.
1707 func ValueOf(i interface{}) Value {
1712 // TODO(rsc): Eliminate this terrible hack.
1713 // In the call to packValue, eface.typ doesn't escape,
1714 // and eface.word is an integer. So it looks like
1715 // i (= eface) doesn't escape. But really it does,
1716 // because eface.word is actually a pointer.
1719 // For an interface value with the noAddr bit set,
1720 // the representation is identical to an empty interface.
1721 eface := *(*emptyInterface)(unsafe.Pointer(&i))
1722 return packValue(0, eface.typ, eface.word)
1725 // Zero returns a Value representing a zero value for the specified type.
1726 // The result is different from the zero value of the Value struct,
1727 // which represents no value at all.
1728 // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
1729 func Zero(typ Type) Value {
1731 panic("reflect: Zero(nil)")
1733 if typ.Kind() == Ptr || typ.Kind() == UnsafePointer {
1734 return valueFromIword(0, typ, 0)
1736 return valueFromAddr(0, typ, unsafe.New(typ))
1739 // New returns a Value representing a pointer to a new zero value
1740 // for the specified type. That is, the returned Value's Type is PtrTo(t).
1741 func New(typ Type) Value {
1743 panic("reflect: New(nil)")
1745 ptr := unsafe.New(typ)
1746 return valueFromIword(0, PtrTo(typ), iword(uintptr(ptr)))
1749 // convertForAssignment
1750 func convertForAssignment(what string, addr unsafe.Pointer, dst Type, iv internalValue) internalValue {
1752 panic(what + ": cannot assign method value to type " + dst.String())
1755 dst1 := dst.(*commonType)
1756 if directlyAssignable(dst1, iv.typ) {
1757 // Overwrite type so that they match.
1758 // Same memory layout, so no harm done.
1762 if implements(dst1, iv.typ) {
1764 addr = unsafe.Pointer(new(interface{}))
1766 x := iv.valueInterface(false)
1767 if dst.NumMethod() == 0 {
1768 *(*interface{})(addr) = x
1770 ifaceE2I(dst1.runtimeType(), x, addr)
1773 iv.word = iword(uintptr(addr))
1779 panic(what + ": value of type " + iv.typ.String() + " is not assignable to type " + dst.String())
1782 // implemented in ../pkg/runtime
1783 func chancap(ch iword) int32
1784 func chanclose(ch iword)
1785 func chanlen(ch iword) int32
1786 func chanrecv(t *runtime.Type, ch iword, nb bool) (val iword, selected, received bool)
1787 func chansend(t *runtime.Type, ch iword, val iword, nb bool) bool
1789 func makechan(typ *runtime.Type, size uint32) (ch iword)
1790 func makemap(t *runtime.Type) iword
1791 func mapaccess(t *runtime.Type, m iword, key iword) (val iword, ok bool)
1792 func mapassign(t *runtime.Type, m iword, key, val iword, ok bool)
1793 func mapiterinit(t *runtime.Type, m iword) *byte
1794 func mapiterkey(it *byte) (key iword, ok bool)
1795 func mapiternext(it *byte)
1796 func maplen(m iword) int32
1798 func call(typ *commonType, fnaddr unsafe.Pointer, isInterface bool, isMethod bool, params *unsafe.Pointer, results *unsafe.Pointer)
1799 func ifaceE2I(t *runtime.Type, src interface{}, dst unsafe.Pointer)