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.
13 const ptrSize = uintptr(unsafe.Sizeof((*byte)(nil)))
14 const cannotSet = "cannot set value obtained via unexported struct field"
16 type addr unsafe.Pointer
18 // TODO: This will have to go away when
19 // the new gc goes in.
20 func memmove(adst, asrc addr, n uintptr) {
24 case src < dst && src+n > dst:
26 // careful: i is unsigned
29 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i))
31 case (n|src|dst)&(ptrSize-1) != 0:
33 for i := uintptr(0); i < n; i++ {
34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i))
38 for i := uintptr(0); i < n; i += ptrSize {
39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i))
44 // Value is the common interface to reflection values.
45 // The implementations of Value (e.g., ArrayValue, StructValue)
46 // have additional type-specific methods.
47 type Value interface {
48 // Type returns the value's type.
51 // Interface returns the value as an interface{}.
52 Interface() interface{}
54 // CanSet returns whether the value can be changed.
55 // Values obtained by the use of non-exported struct fields
56 // can be used in Get but not Set.
57 // If CanSet() returns false, calling the type-specific Set
58 // will cause a crash.
61 // SetValue assigns v to the value; v must have the same type as the value.
64 // Addr returns a pointer to the underlying data.
65 // It is for advanced clients that also
66 // import the "unsafe" package.
69 // Method returns a FuncValue corresponding to the value's i'th method.
70 // The arguments to a Call on the returned FuncValue
71 // should not include a receiver; the FuncValue will use
72 // the value as the receiver.
73 Method(i int) *FuncValue
78 // value is the common implementation of most values.
79 // It is embedded in other, public struct types, but always
80 // with a unique tag like "uint" or "float" so that the client cannot
81 // convert from, say, *UintValue to *FloatValue.
88 func (v *value) Type() Type { return v.typ }
90 func (v *value) Addr() uintptr { return uintptr(v.addr) }
92 func (v *value) getAddr() addr { return v.addr }
94 func (v *value) Interface() interface{} {
95 if typ, ok := v.typ.(*InterfaceType); ok {
96 // There are two different representations of interface values,
97 // one if the interface type has methods and one if it doesn't.
98 // These two representations require different expressions
99 // to extract correctly.
100 if typ.NumMethod() == 0 {
101 // Extract as interface value without methods.
102 return *(*interface{})(v.addr)
104 // Extract from v.addr as interface value with methods.
105 return *(*interface {
109 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr))
112 func (v *value) CanSet() bool { return v.canSet }
118 // BoolValue represents a bool value.
119 type BoolValue struct {
123 // Get returns the underlying bool value.
124 func (v *BoolValue) Get() bool { return *(*bool)(v.addr) }
126 // Set sets v to the value x.
127 func (v *BoolValue) Set(x bool) {
134 // Set sets v to the value x.
135 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) }
137 // FloatValue represents a float value.
138 type FloatValue struct {
142 // Get returns the underlying int value.
143 func (v *FloatValue) Get() float64 {
144 switch v.typ.(*FloatType).Kind() {
146 return float64(*(*float)(v.addr))
148 return float64(*(*float32)(v.addr))
150 return *(*float64)(v.addr)
152 panic("reflect: invalid float kind")
155 // Set sets v to the value x.
156 func (v *FloatValue) Set(x float64) {
160 switch v.typ.(*FloatType).Kind() {
162 panic("reflect: invalid float kind")
164 *(*float)(v.addr) = float(x)
166 *(*float32)(v.addr) = float32(x)
168 *(*float64)(v.addr) = x
172 // Overflow returns true if x cannot be represented by the type of v.
173 func (v *FloatValue) Overflow(x float64) bool {
174 if v.typ.Size() == 8 {
180 return math.MaxFloat32 < x && x <= math.MaxFloat64
183 // Set sets v to the value x.
184 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
186 // ComplexValue represents a complex value.
187 type ComplexValue struct {
191 // Get returns the underlying complex value.
192 func (v *ComplexValue) Get() complex128 {
193 switch v.typ.(*ComplexType).Kind() {
195 return complex128(*(*complex)(v.addr))
197 return complex128(*(*complex64)(v.addr))
199 return *(*complex128)(v.addr)
201 panic("reflect: invalid complex kind")
204 // Set sets v to the value x.
205 func (v *ComplexValue) Set(x complex128) {
209 switch v.typ.(*ComplexType).Kind() {
211 panic("reflect: invalid complex kind")
213 *(*complex)(v.addr) = complex(x)
215 *(*complex64)(v.addr) = complex64(x)
217 *(*complex128)(v.addr) = x
221 // Set sets v to the value x.
222 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) }
224 // IntValue represents an int value.
225 type IntValue struct {
229 // Get returns the underlying int value.
230 func (v *IntValue) Get() int64 {
231 switch v.typ.(*IntType).Kind() {
233 return int64(*(*int)(v.addr))
235 return int64(*(*int8)(v.addr))
237 return int64(*(*int16)(v.addr))
239 return int64(*(*int32)(v.addr))
241 return *(*int64)(v.addr)
243 panic("reflect: invalid int kind")
246 // Set sets v to the value x.
247 func (v *IntValue) Set(x int64) {
251 switch v.typ.(*IntType).Kind() {
253 panic("reflect: invalid int kind")
255 *(*int)(v.addr) = int(x)
257 *(*int8)(v.addr) = int8(x)
259 *(*int16)(v.addr) = int16(x)
261 *(*int32)(v.addr) = int32(x)
263 *(*int64)(v.addr) = x
267 // Set sets v to the value x.
268 func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) }
270 // Overflow returns true if x cannot be represented by the type of v.
271 func (v *IntValue) Overflow(x int64) bool {
272 bitSize := uint(v.typ.Bits())
273 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
277 // StringHeader is the runtime representation of a string.
278 type StringHeader struct {
283 // StringValue represents a string value.
284 type StringValue struct {
288 // Get returns the underlying string value.
289 func (v *StringValue) Get() string { return *(*string)(v.addr) }
291 // Set sets v to the value x.
292 func (v *StringValue) Set(x string) {
296 *(*string)(v.addr) = x
299 // Set sets v to the value x.
300 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) }
302 // UintValue represents a uint value.
303 type UintValue struct {
307 // Get returns the underlying uuint value.
308 func (v *UintValue) Get() uint64 {
309 switch v.typ.(*UintType).Kind() {
311 return uint64(*(*uint)(v.addr))
313 return uint64(*(*uint8)(v.addr))
315 return uint64(*(*uint16)(v.addr))
317 return uint64(*(*uint32)(v.addr))
319 return *(*uint64)(v.addr)
321 return uint64(*(*uintptr)(v.addr))
323 panic("reflect: invalid uint kind")
326 // Set sets v to the value x.
327 func (v *UintValue) Set(x uint64) {
331 switch v.typ.(*UintType).Kind() {
333 panic("reflect: invalid uint kind")
335 *(*uint)(v.addr) = uint(x)
337 *(*uint8)(v.addr) = uint8(x)
339 *(*uint16)(v.addr) = uint16(x)
341 *(*uint32)(v.addr) = uint32(x)
343 *(*uint64)(v.addr) = x
345 *(*uintptr)(v.addr) = uintptr(x)
349 // Overflow returns true if x cannot be represented by the type of v.
350 func (v *UintValue) Overflow(x uint64) bool {
351 bitSize := uint(v.typ.Bits())
352 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
356 // Set sets v to the value x.
357 func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) }
359 // UnsafePointerValue represents an unsafe.Pointer value.
360 type UnsafePointerValue struct {
361 value "unsafe.Pointer"
364 // Get returns the underlying uintptr value.
365 // Get returns uintptr, not unsafe.Pointer, so that
366 // programs that do not import "unsafe" cannot
367 // obtain a value of unsafe.Pointer type from "reflect".
368 func (v *UnsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v.addr)) }
370 // Set sets v to the value x.
371 func (v *UnsafePointerValue) Set(x unsafe.Pointer) {
375 *(*unsafe.Pointer)(v.addr) = x
378 // Set sets v to the value x.
379 func (v *UnsafePointerValue) SetValue(x Value) {
380 v.Set(unsafe.Pointer(x.(*UnsafePointerValue).Get()))
383 func typesMustMatch(t1, t2 Type) {
385 panic("type mismatch: " + t1.String() + " != " + t2.String())
393 // ArrayOrSliceValue is the common interface
394 // implemented by both ArrayValue and SliceValue.
395 type ArrayOrSliceValue interface {
403 // ArrayCopy copies the contents of src into dst until either
404 // dst has been filled or src has been exhausted.
405 // It returns the number of elements copied.
406 // The arrays dst and src must have the same element type.
407 func ArrayCopy(dst, src ArrayOrSliceValue) int {
408 // TODO: This will have to move into the runtime
409 // once the real gc goes in.
410 de := dst.Type().(ArrayOrSliceType).Elem()
411 se := src.Type().(ArrayOrSliceType).Elem()
412 typesMustMatch(de, se)
414 if xn := src.Len(); n > xn {
417 memmove(dst.addr(), src.addr(), uintptr(n)*de.Size())
421 // An ArrayValue represents an array.
422 type ArrayValue struct {
426 // Len returns the length of the array.
427 func (v *ArrayValue) Len() int { return v.typ.(*ArrayType).Len() }
429 // Cap returns the capacity of the array (equal to Len()).
430 func (v *ArrayValue) Cap() int { return v.typ.(*ArrayType).Len() }
432 // addr returns the base address of the data in the array.
433 func (v *ArrayValue) addr() addr { return v.value.addr }
435 // Set assigns x to v.
436 // The new value x must have the same type as v.
437 func (v *ArrayValue) Set(x *ArrayValue) {
441 typesMustMatch(v.typ, x.typ)
445 // Set sets v to the value x.
446 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) }
448 // Elem returns the i'th element of v.
449 func (v *ArrayValue) Elem(i int) Value {
450 typ := v.typ.(*ArrayType).Elem()
453 panic("array index out of bounds")
455 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
456 return newValue(typ, p, v.canSet)
463 // runtime representation of slice
464 type SliceHeader struct {
470 // A SliceValue represents a slice.
471 type SliceValue struct {
475 func (v *SliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr) }
477 // IsNil returns whether v is a nil slice.
478 func (v *SliceValue) IsNil() bool { return v.slice().Data == 0 }
480 // Len returns the length of the slice.
481 func (v *SliceValue) Len() int { return int(v.slice().Len) }
483 // Cap returns the capacity of the slice.
484 func (v *SliceValue) Cap() int { return int(v.slice().Cap) }
486 // addr returns the base address of the data in the slice.
487 func (v *SliceValue) addr() addr { return addr(v.slice().Data) }
489 // SetLen changes the length of v.
490 // The new length n must be between 0 and the capacity, inclusive.
491 func (v *SliceValue) SetLen(n int) {
493 if n < 0 || n > int(s.Cap) {
494 panic("reflect: slice length out of range in SetLen")
499 // Set assigns x to v.
500 // The new value x must have the same type as v.
501 func (v *SliceValue) Set(x *SliceValue) {
505 typesMustMatch(v.typ, x.typ)
506 *v.slice() = *x.slice()
509 // Set sets v to the value x.
510 func (v *SliceValue) SetValue(x Value) { v.Set(x.(*SliceValue)) }
512 // Get returns the uintptr address of the v.Cap()'th element. This gives
513 // the same result for all slices of the same array.
514 // It is mainly useful for printing.
515 func (v *SliceValue) Get() uintptr {
516 typ := v.typ.(*SliceType)
517 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size()
520 // Slice returns a sub-slice of the slice v.
521 func (v *SliceValue) Slice(beg, end int) *SliceValue {
523 if beg < 0 || end < beg || end > cap {
524 panic("slice index out of bounds")
526 typ := v.typ.(*SliceType)
527 s := new(SliceHeader)
528 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size()
531 return newValue(typ, addr(s), v.canSet).(*SliceValue)
534 // Elem returns the i'th element of v.
535 func (v *SliceValue) Elem(i int) Value {
536 typ := v.typ.(*SliceType).Elem()
539 panic("reflect: slice index out of range")
541 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
542 return newValue(typ, p, v.canSet)
545 // MakeSlice creates a new zero-initialized slice value
546 // for the specified slice type, length, and capacity.
547 func MakeSlice(typ *SliceType, len, cap int) *SliceValue {
549 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)),
553 return newValue(typ, addr(s), true).(*SliceValue)
560 // A ChanValue represents a chan.
561 type ChanValue struct {
565 // IsNil returns whether v is a nil channel.
566 func (v *ChanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
568 // Set assigns x to v.
569 // The new value x must have the same type as v.
570 func (v *ChanValue) Set(x *ChanValue) {
574 typesMustMatch(v.typ, x.typ)
575 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
578 // Set sets v to the value x.
579 func (v *ChanValue) SetValue(x Value) { v.Set(x.(*ChanValue)) }
581 // Get returns the uintptr value of v.
582 // It is mainly useful for printing.
583 func (v *ChanValue) Get() uintptr { return *(*uintptr)(v.addr) }
585 // implemented in ../pkg/runtime/reflect.cgo
586 func makechan(typ *runtime.ChanType, size uint32) (ch *byte)
587 func chansend(ch, val *byte, pres *bool)
588 func chanrecv(ch, val *byte, pres *bool)
589 func chanclosed(ch *byte) bool
590 func chanclose(ch *byte)
591 func chanlen(ch *byte) int32
592 func chancap(ch *byte) int32
594 // Closed returns the result of closed(c) on the underlying channel.
595 func (v *ChanValue) Closed() bool {
596 ch := *(**byte)(v.addr)
597 return chanclosed(ch)
600 // Close closes the channel.
601 func (v *ChanValue) Close() {
602 ch := *(**byte)(v.addr)
606 func (v *ChanValue) Len() int {
607 ch := *(**byte)(v.addr)
608 return int(chanlen(ch))
611 func (v *ChanValue) Cap() int {
612 ch := *(**byte)(v.addr)
613 return int(chancap(ch))
616 // internal send; non-blocking if b != nil
617 func (v *ChanValue) send(x Value, b *bool) {
618 t := v.Type().(*ChanType)
619 if t.Dir()&SendDir == 0 {
620 panic("send on recv-only channel")
622 typesMustMatch(t.Elem(), x.Type())
623 ch := *(**byte)(v.addr)
624 chansend(ch, (*byte)(x.getAddr()), b)
627 // internal recv; non-blocking if b != nil
628 func (v *ChanValue) recv(b *bool) Value {
629 t := v.Type().(*ChanType)
630 if t.Dir()&RecvDir == 0 {
631 panic("recv on send-only channel")
633 ch := *(**byte)(v.addr)
634 x := MakeZero(t.Elem())
635 chanrecv(ch, (*byte)(x.getAddr()), b)
639 // Send sends x on the channel v.
640 func (v *ChanValue) Send(x Value) { v.send(x, nil) }
642 // Recv receives and returns a value from the channel v.
643 func (v *ChanValue) Recv() Value { return v.recv(nil) }
645 // TrySend attempts to sends x on the channel v but will not block.
646 // It returns true if the value was sent, false otherwise.
647 func (v *ChanValue) TrySend(x Value) bool {
653 // TryRecv attempts to receive a value from the channel v but will not block.
654 // It returns the value if one is received, nil otherwise.
655 func (v *ChanValue) TryRecv() Value {
664 // MakeChan creates a new channel with the specified type and buffer size.
665 func MakeChan(typ *ChanType, buffer int) *ChanValue {
667 panic("MakeChan: negative buffer size")
669 if typ.Dir() != BothDir {
670 panic("MakeChan: unidirectional channel type")
672 v := MakeZero(typ).(*ChanValue)
673 *(**byte)(v.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ)), uint32(buffer))
681 // A FuncValue represents a function value.
682 type FuncValue struct {
688 // IsNil returns whether v is a nil function.
689 func (v *FuncValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
691 // Get returns the uintptr value of v.
692 // It is mainly useful for printing.
693 func (v *FuncValue) Get() uintptr { return *(*uintptr)(v.addr) }
695 // Set assigns x to v.
696 // The new value x must have the same type as v.
697 func (v *FuncValue) Set(x *FuncValue) {
701 typesMustMatch(v.typ, x.typ)
702 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
705 // Set sets v to the value x.
706 func (v *FuncValue) SetValue(x Value) { v.Set(x.(*FuncValue)) }
708 // Method returns a FuncValue corresponding to v's i'th method.
709 // The arguments to a Call on the returned FuncValue
710 // should not include a receiver; the FuncValue will use v
712 func (v *value) Method(i int) *FuncValue {
713 t := v.Type().uncommon()
714 if t == nil || i < 0 || i >= len(t.methods) {
719 fv := &FuncValue{value: value{runtimeToType(p.typ), addr(&fn), true}, first: v, isInterface: false}
723 // implemented in ../pkg/runtime/*/asm.s
724 func call(typ *FuncType, fnaddr *byte, isInterface bool, params *addr, results *addr)
726 // Call calls the function fv with input parameters in.
727 // It returns the function's output parameters as Values.
728 func (fv *FuncValue) Call(in []Value) []Value {
729 t := fv.Type().(*FuncType)
731 if fv.first != nil && !fv.isInterface {
734 if nin != t.NumIn() {
735 panic("FuncValue: wrong argument count")
737 if fv.first != nil && fv.isInterface {
742 params := make([]addr, nin)
745 if v := fv.first; v != nil {
746 // Hard-wired first argument.
748 // v is a single uninterpreted word
749 params[0] = v.getAddr()
754 // This is a method, so we need to always pass
757 if ptv, ok := tv.(*PtrType); ok {
758 typesMustMatch(t.In(0), tv)
763 typesMustMatch(t.In(0).(*PtrType).Elem(), tv)
771 for i, v := range in {
773 tf := t.In(i + delta)
775 // If this is really a method, and we are explicitly
776 // passing the object, then we need to pass the address
777 // of the object instead. Unfortunately, we don't
778 // have any way to know that this is a method, so we just
779 // check the type. FIXME: This is ugly.
781 if i == 0 && tf != tv {
782 if ptf, ok := tf.(*PtrType); ok {
790 typesMustMatch(tf, tv)
791 params[i+off] = vAddr
794 ret := make([]Value, nout)
795 results := make([]addr, nout)
796 for i := 0; i < nout; i++ {
799 results[i] = v.getAddr()
803 call(t, *(**byte)(fv.addr), fv.isInterface, ¶ms[0], &results[0])
812 // An InterfaceValue represents an interface value.
813 type InterfaceValue struct {
817 // IsNil returns whether v is a nil interface value.
818 func (v *InterfaceValue) IsNil() bool { return v.Interface() == nil }
820 // No single uinptr Get because v.Interface() is available.
822 // Get returns the two words that represent an interface in the runtime.
823 // Those words are useful only when playing unsafe games.
824 func (v *InterfaceValue) Get() [2]uintptr {
825 return *(*[2]uintptr)(v.addr)
828 // Elem returns the concrete value stored in the interface value v.
829 func (v *InterfaceValue) Elem() Value { return NewValue(v.Interface()) }
831 // ../runtime/reflect.cgo
832 func setiface(typ *InterfaceType, x *interface{}, addr addr)
834 // Set assigns x to v.
835 func (v *InterfaceValue) Set(x Value) {
843 // Two different representations; see comment in Get.
844 // Empty interface is easy.
845 t := v.typ.(*InterfaceType)
846 if t.NumMethod() == 0 {
847 *(*interface{})(v.addr) = i
851 // Non-empty interface requires a runtime check.
852 setiface(t, &i, v.addr)
855 // Set sets v to the value x.
856 func (v *InterfaceValue) SetValue(x Value) { v.Set(x) }
858 // Method returns a FuncValue corresponding to v's i'th method.
859 // The arguments to a Call on the returned FuncValue
860 // should not include a receiver; the FuncValue will use v
862 func (v *InterfaceValue) Method(i int) *FuncValue {
863 t := v.Type().(*InterfaceType)
864 if t == nil || i < 0 || i >= len(t.methods) {
869 // Interface is two words: itable, data.
870 tab := *(**[10000]addr)(v.addr)
871 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), true}
874 fv := &FuncValue{value: value{runtimeToType(p.typ), addr(&fn), true}, first: data, isInterface: true}
882 // A MapValue represents a map value.
883 type MapValue struct {
887 // IsNil returns whether v is a nil map value.
888 func (v *MapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
890 // Set assigns x to v.
891 // The new value x must have the same type as v.
892 func (v *MapValue) Set(x *MapValue) {
897 *(**uintptr)(v.addr) = nil
900 typesMustMatch(v.typ, x.typ)
901 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
904 // Set sets v to the value x.
905 func (v *MapValue) SetValue(x Value) {
913 // Get returns the uintptr value of v.
914 // It is mainly useful for printing.
915 func (v *MapValue) Get() uintptr { return *(*uintptr)(v.addr) }
917 // implemented in ../pkg/runtime/reflect.cgo
918 func mapaccess(m, key, val *byte) bool
919 func mapassign(m, key, val *byte)
920 func maplen(m *byte) int32
921 func mapiterinit(m *byte) *byte
922 func mapiternext(it *byte)
923 func mapiterkey(it *byte, key *byte) bool
924 func makemap(t *runtime.MapType) *byte
926 // Elem returns the value associated with key in the map v.
927 // It returns nil if key is not found in the map.
928 func (v *MapValue) Elem(key Value) Value {
929 t := v.Type().(*MapType)
930 typesMustMatch(t.Key(), key.Type())
931 m := *(**byte)(v.addr)
935 newval := MakeZero(t.Elem())
936 if !mapaccess(m, (*byte)(key.getAddr()), (*byte)(newval.getAddr())) {
942 // SetElem sets the value associated with key in the map v to val.
943 // If val is nil, Put deletes the key from map.
944 func (v *MapValue) SetElem(key, val Value) {
945 t := v.Type().(*MapType)
946 typesMustMatch(t.Key(), key.Type())
949 typesMustMatch(t.Elem(), val.Type())
950 vaddr = (*byte)(val.getAddr())
952 m := *(**byte)(v.addr)
953 mapassign(m, (*byte)(key.getAddr()), vaddr)
956 // Len returns the number of keys in the map v.
957 func (v *MapValue) Len() int {
958 m := *(**byte)(v.addr)
962 return int(maplen(m))
965 // Keys returns a slice containing all the keys present in the map,
966 // in unspecified order.
967 func (v *MapValue) Keys() []Value {
968 tk := v.Type().(*MapType).Key()
969 m := *(**byte)(v.addr)
975 a := make([]Value, mlen)
977 for i = 0; i < len(a); i++ {
979 if !mapiterkey(it, (*byte)(k.getAddr())) {
988 // MakeMap creates a new map of the specified type.
989 func MakeMap(typ *MapType) *MapValue {
990 v := MakeZero(typ).(*MapValue)
991 *(**byte)(v.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ)))
999 // A PtrValue represents a pointer.
1000 type PtrValue struct {
1004 // IsNil returns whether v is a nil pointer.
1005 func (v *PtrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
1007 // Get returns the uintptr value of v.
1008 // It is mainly useful for printing.
1009 func (v *PtrValue) Get() uintptr { return *(*uintptr)(v.addr) }
1011 // Set assigns x to v.
1012 // The new value x must have the same type as v.
1013 func (v *PtrValue) Set(x *PtrValue) {
1015 *(**uintptr)(v.addr) = nil
1021 typesMustMatch(v.typ, x.typ)
1022 // TODO: This will have to move into the runtime
1023 // once the new gc goes in
1024 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
1027 // Set sets v to the value x.
1028 func (v *PtrValue) SetValue(x Value) {
1033 v.Set(x.(*PtrValue))
1036 // PointTo changes v to point to x.
1037 // If x is a nil Value, PointTo sets v to nil.
1038 func (v *PtrValue) PointTo(x Value) {
1040 *(**uintptr)(v.addr) = nil
1044 panic("cannot set x; cannot point to x")
1046 typesMustMatch(v.typ.(*PtrType).Elem(), x.Type())
1047 // TODO: This will have to move into the runtime
1048 // once the new gc goes in.
1049 *(*uintptr)(v.addr) = x.Addr()
1052 // Elem returns the value that v points to.
1053 // If v is a nil pointer, Elem returns a nil Value.
1054 func (v *PtrValue) Elem() Value {
1058 return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), v.canSet)
1061 // Indirect returns the value that v points to.
1062 // If v is a nil pointer, Indirect returns a nil Value.
1063 // If v is not a pointer, Indirect returns v.
1064 func Indirect(v Value) Value {
1065 if pv, ok := v.(*PtrValue); ok {
1075 // A StructValue represents a struct value.
1076 type StructValue struct {
1080 // Set assigns x to v.
1081 // The new value x must have the same type as v.
1082 func (v *StructValue) Set(x *StructValue) {
1083 // TODO: This will have to move into the runtime
1084 // once the gc goes in.
1088 typesMustMatch(v.typ, x.typ)
1089 memmove(v.addr, x.addr, v.typ.Size())
1092 // Set sets v to the value x.
1093 func (v *StructValue) SetValue(x Value) { v.Set(x.(*StructValue)) }
1095 // Field returns the i'th field of the struct.
1096 func (v *StructValue) Field(i int) Value {
1097 t := v.typ.(*StructType)
1098 if i < 0 || i >= t.NumField() {
1102 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), v.canSet && f.PkgPath == "")
1105 // FieldByIndex returns the nested field corresponding to index.
1106 func (t *StructValue) FieldByIndex(index []int) (v Value) {
1108 for i, x := range index {
1110 if p, ok := v.(*PtrValue); ok {
1113 if s, ok := v.(*StructValue); ok {
1125 // FieldByName returns the struct field with the given name.
1126 // The result is nil if no field was found.
1127 func (t *StructValue) FieldByName(name string) Value {
1128 if f, ok := t.Type().(*StructType).FieldByName(name); ok {
1129 return t.FieldByIndex(f.Index)
1134 // FieldByNameFunc returns the struct field with a name that satisfies the
1136 // The result is nil if no field was found.
1137 func (t *StructValue) FieldByNameFunc(match func(string) bool) Value {
1138 if f, ok := t.Type().(*StructType).FieldByNameFunc(match); ok {
1139 return t.FieldByIndex(f.Index)
1144 // NumField returns the number of fields in the struct.
1145 func (v *StructValue) NumField() int { return v.typ.(*StructType).NumField() }
1151 // NewValue returns a new Value initialized to the concrete value
1152 // stored in the interface i. NewValue(nil) returns nil.
1153 func NewValue(i interface{}) Value {
1157 t, a := unsafe.Reflect(i)
1158 return newValue(canonicalize(toType(t)), addr(a), true)
1161 func newValue(typ Type, addr addr, canSet bool) Value {
1162 v := value{typ, addr, canSet}
1165 return &ArrayValue{v}
1167 return &BoolValue{v}
1169 return &ChanValue{v}
1171 return &FloatValue{v}
1173 return &FuncValue{value: v}
1175 return &ComplexValue{v}
1178 case *InterfaceType:
1179 return &InterfaceValue{v}
1185 return &SliceValue{v}
1187 return &StringValue{v}
1189 return &StructValue{v}
1191 return &UintValue{v}
1192 case *UnsafePointerType:
1193 return &UnsafePointerValue{v}
1195 panic("newValue" + typ.String())
1198 // MakeZero returns a zero Value for the specified Type.
1199 func MakeZero(typ Type) Value {
1203 return newValue(typ, addr(unsafe.New(typ)), true)