Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | // Reflection library. |
| 6 | // Handling values. |
| 7 | |
| 8 | package reflect |
| 9 | |
| 10 | import ( |
| 11 | "reflect"; |
Rob Pike | 50d0695 | 2008-12-09 15:41:21 -0800 | [diff] [blame] | 12 | "unsafe"; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 15 | // Addr is shorthand for unsafe.Pointer and is used to represent the address of Values. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 16 | type Addr unsafe.Pointer |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 17 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 18 | func equalType(a, b Type) bool { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 19 | return a.String() == b.String() |
| 20 | } |
| 21 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 22 | // Value is the generic interface to reflection values. Once its Kind is known, |
| 23 | // such as BoolKind, the Value can be narrowed to the appropriate, more |
| 24 | // specific interface, such as BoolValue. Such narrowed values still implement |
| 25 | // the Value interface. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 26 | type Value interface { |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 27 | // The kind of thing described: ArrayKind, BoolKind, etc. |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 28 | Kind() int; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 29 | // The reflection Type of the value. |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 30 | Type() Type; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 31 | // The address of the value. |
Rob Pike | b1d37b7 | 2008-11-12 14:19:39 -0800 | [diff] [blame] | 32 | Addr() Addr; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 33 | // The value itself is the dynamic value of an empty interface. |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 34 | Interface() interface {}; |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 37 | func NewValue(e interface{}) Value; |
| 38 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 39 | // commonValue fields and functionality for all values |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 40 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 41 | type commonValue struct { |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 42 | kind int; |
| 43 | typ Type; |
| 44 | addr Addr; |
| 45 | } |
| 46 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 47 | func (c *commonValue) Kind() int { |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 48 | return c.kind |
| 49 | } |
| 50 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 51 | func (c *commonValue) Type() Type { |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 52 | return c.typ |
| 53 | } |
| 54 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 55 | func (c *commonValue) Addr() Addr { |
Rob Pike | b1d37b7 | 2008-11-12 14:19:39 -0800 | [diff] [blame] | 56 | return c.addr |
| 57 | } |
| 58 | |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 59 | func (c *commonValue) Interface() interface {} { |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 60 | var i interface {}; |
| 61 | if c.typ.Size() > 8 { // TODO(rsc): how do we know it is 8? |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 62 | i = sys.Unreflect(uint64(uintptr(c.addr)), c.typ.String(), true); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 63 | } else { |
| 64 | if uintptr(c.addr) == 0 { |
| 65 | panicln("reflect: address 0 for", c.typ.String()); |
| 66 | } |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 67 | i = sys.Unreflect(uint64(uintptr(*(*Addr)(c.addr))), c.typ.String(), false); |
Russ Cox | ba882f9 | 2008-12-19 03:06:19 -0800 | [diff] [blame] | 68 | } |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 69 | return i; |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 72 | func newValueAddr(typ Type, addr Addr) Value |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 73 | |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 74 | type creatorFn func(typ Type, addr Addr) Value |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 75 | |
| 76 | |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 77 | // -- Missing |
| 78 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 79 | // MissingValue represents a value whose type is not known. It usually |
| 80 | // indicates an error. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 81 | type MissingValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 82 | Value; |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 85 | type missingValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 86 | commonValue |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 89 | func missingCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 90 | return &missingValueStruct{ commonValue{MissingKind, typ, addr} } |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 93 | // -- Int |
| 94 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 95 | // IntValue represents an int value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 96 | type IntValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 97 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 98 | Get() int; // Get the underlying int. |
| 99 | Set(int); // Set the underlying int. |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 102 | type intValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 103 | commonValue |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 106 | func intCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 107 | return &intValueStruct{ commonValue{IntKind, typ, addr} } |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 110 | func (v *intValueStruct) Get() int { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 111 | return *(*int)(v.addr) |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 114 | func (v *intValueStruct) Set(i int) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 115 | *(*int)(v.addr) = i |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 118 | // -- Int8 |
| 119 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 120 | // Int8Value represents an int8 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 121 | type Int8Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 122 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 123 | Get() int8; // Get the underlying int8. |
| 124 | Set(int8); // Set the underlying int8. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 127 | type int8ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 128 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 131 | func int8Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 132 | return &int8ValueStruct{ commonValue{Int8Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 135 | func (v *int8ValueStruct) Get() int8 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 136 | return *(*int8)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 139 | func (v *int8ValueStruct) Set(i int8) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 140 | *(*int8)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 143 | // -- Int16 |
| 144 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 145 | // Int16Value represents an int16 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 146 | type Int16Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 147 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 148 | Get() int16; // Get the underlying int16. |
| 149 | Set(int16); // Set the underlying int16. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 152 | type int16ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 153 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 156 | func int16Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 157 | return &int16ValueStruct{ commonValue{Int16Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 160 | func (v *int16ValueStruct) Get() int16 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 161 | return *(*int16)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 164 | func (v *int16ValueStruct) Set(i int16) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 165 | *(*int16)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 168 | // -- Int32 |
| 169 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 170 | // Int32Value represents an int32 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 171 | type Int32Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 172 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 173 | Get() int32; // Get the underlying int32. |
| 174 | Set(int32); // Set the underlying int32. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 177 | type int32ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 178 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 181 | func int32Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 182 | return &int32ValueStruct{ commonValue{Int32Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 185 | func (v *int32ValueStruct) Get() int32 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 186 | return *(*int32)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 189 | func (v *int32ValueStruct) Set(i int32) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 190 | *(*int32)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 193 | // -- Int64 |
| 194 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 195 | // Int64Value represents an int64 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 196 | type Int64Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 197 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 198 | Get() int64; // Get the underlying int64. |
| 199 | Set(int64); // Set the underlying int64. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 202 | type int64ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 203 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 206 | func int64Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 207 | return &int64ValueStruct{ commonValue{Int64Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 210 | func (v *int64ValueStruct) Get() int64 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 211 | return *(*int64)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 214 | func (v *int64ValueStruct) Set(i int64) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 215 | *(*int64)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 218 | // -- Uint |
| 219 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 220 | // UintValue represents a uint value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 221 | type UintValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 222 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 223 | Get() uint; // Get the underlying uint. |
| 224 | Set(uint); // Set the underlying uint. |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 227 | type uintValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 228 | commonValue |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 231 | func uintCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 232 | return &uintValueStruct{ commonValue{UintKind, typ, addr} } |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 235 | func (v *uintValueStruct) Get() uint { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 236 | return *(*uint)(v.addr) |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 239 | func (v *uintValueStruct) Set(i uint) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 240 | *(*uint)(v.addr) = i |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 243 | // -- Uint8 |
| 244 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 245 | // Uint8Value represents a uint8 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 246 | type Uint8Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 247 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 248 | Get() uint8; // Get the underlying uint8. |
| 249 | Set(uint8); // Set the underlying uint8. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 252 | type uint8ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 253 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 256 | func uint8Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 257 | return &uint8ValueStruct{ commonValue{Uint8Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 260 | func (v *uint8ValueStruct) Get() uint8 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 261 | return *(*uint8)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 264 | func (v *uint8ValueStruct) Set(i uint8) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 265 | *(*uint8)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 268 | // -- Uint16 |
| 269 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 270 | // Uint16Value represents a uint16 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 271 | type Uint16Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 272 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 273 | Get() uint16; // Get the underlying uint16. |
| 274 | Set(uint16); // Set the underlying uint16. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 277 | type uint16ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 278 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 281 | func uint16Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 282 | return &uint16ValueStruct{ commonValue{Uint16Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 285 | func (v *uint16ValueStruct) Get() uint16 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 286 | return *(*uint16)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 289 | func (v *uint16ValueStruct) Set(i uint16) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 290 | *(*uint16)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 293 | // -- Uint32 |
| 294 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 295 | // Uint32Value represents a uint32 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 296 | type Uint32Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 297 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 298 | Get() uint32; // Get the underlying uint32. |
| 299 | Set(uint32); // Set the underlying uint32. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 302 | type uint32ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 303 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 306 | func uint32Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 307 | return &uint32ValueStruct{ commonValue{Uint32Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 310 | func (v *uint32ValueStruct) Get() uint32 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 311 | return *(*uint32)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 314 | func (v *uint32ValueStruct) Set(i uint32) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 315 | *(*uint32)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 318 | // -- Uint64 |
| 319 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 320 | // Uint64Value represents a uint64 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 321 | type Uint64Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 322 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 323 | Get() uint64; // Get the underlying uint64. |
| 324 | Set(uint64); // Set the underlying uint64. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 327 | type uint64ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 328 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 331 | func uint64Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 332 | return &uint64ValueStruct{ commonValue{Uint64Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 335 | func (v *uint64ValueStruct) Get() uint64 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 336 | return *(*uint64)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 339 | func (v *uint64ValueStruct) Set(i uint64) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 340 | *(*uint64)(v.addr) = i |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 343 | // -- Uintptr |
| 344 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 345 | // UintptrValue represents a uintptr value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 346 | type UintptrValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 347 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 348 | Get() uintptr; // Get the underlying uintptr. |
| 349 | Set(uintptr); // Set the underlying uintptr. |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 352 | type uintptrValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 353 | commonValue |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 356 | func uintptrCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 357 | return &uintptrValueStruct{ commonValue{UintptrKind, typ, addr} } |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 360 | func (v *uintptrValueStruct) Get() uintptr { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 361 | return *(*uintptr)(v.addr) |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 364 | func (v *uintptrValueStruct) Set(i uintptr) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 365 | *(*uintptr)(v.addr) = i |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 368 | // -- Float |
| 369 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 370 | // FloatValue represents a float value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 371 | type FloatValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 372 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 373 | Get() float; // Get the underlying float. |
| 374 | Set(float); // Get the underlying float. |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 377 | type floatValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 378 | commonValue |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 381 | func floatCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 382 | return &floatValueStruct{ commonValue{FloatKind, typ, addr} } |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 385 | func (v *floatValueStruct) Get() float { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 386 | return *(*float)(v.addr) |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 389 | func (v *floatValueStruct) Set(f float) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 390 | *(*float)(v.addr) = f |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 393 | // -- Float32 |
| 394 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 395 | // Float32Value represents a float32 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 396 | type Float32Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 397 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 398 | Get() float32; // Get the underlying float32. |
| 399 | Set(float32); // Get the underlying float32. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 402 | type float32ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 403 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 406 | func float32Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 407 | return &float32ValueStruct{ commonValue{Float32Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 410 | func (v *float32ValueStruct) Get() float32 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 411 | return *(*float32)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 414 | func (v *float32ValueStruct) Set(f float32) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 415 | *(*float32)(v.addr) = f |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 418 | // -- Float64 |
| 419 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 420 | // Float64Value represents a float64 value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 421 | type Float64Value interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 422 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 423 | Get() float64; // Get the underlying float64. |
| 424 | Set(float64); // Get the underlying float64. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 427 | type float64ValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 428 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 431 | func float64Creator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 432 | return &float64ValueStruct{ commonValue{Float64Kind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 435 | func (v *float64ValueStruct) Get() float64 { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 436 | return *(*float64)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 439 | func (v *float64ValueStruct) Set(f float64) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 440 | *(*float64)(v.addr) = f |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 443 | // -- String |
| 444 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 445 | // StringValue represents a string value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 446 | type StringValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 447 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 448 | Get() string; // Get the underlying string value. |
| 449 | Set(string); // Set the underlying string value. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 452 | type stringValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 453 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 456 | func stringCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 457 | return &stringValueStruct{ commonValue{StringKind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 460 | func (v *stringValueStruct) Get() string { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 461 | return *(*string)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 464 | func (v *stringValueStruct) Set(s string) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 465 | *(*string)(v.addr) = s |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 468 | // -- Bool |
| 469 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 470 | // BoolValue represents a bool value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 471 | type BoolValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 472 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 473 | Get() bool; // Get the underlying bool value. |
| 474 | Set(bool); // Set the underlying bool value. |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 477 | type boolValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 478 | commonValue |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 481 | func boolCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 482 | return &boolValueStruct{ commonValue{BoolKind, typ, addr} } |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 485 | func (v *boolValueStruct) Get() bool { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 486 | return *(*bool)(v.addr) |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 489 | func (v *boolValueStruct) Set(b bool) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 490 | *(*bool)(v.addr) = b |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 493 | // -- Pointer |
| 494 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 495 | // PtrValue represents a pointer value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 496 | type PtrValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 497 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 498 | Sub() Value; // The Value pointed to. |
| 499 | Get() Addr; // Get the address stored in the pointer. |
| 500 | SetSub(Value); // Set the the pointed-to Value. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 503 | type ptrValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 504 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 507 | func (v *ptrValueStruct) Get() Addr { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 508 | return *(*Addr)(v.addr) |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 511 | func (v *ptrValueStruct) Sub() Value { |
| 512 | return newValueAddr(v.typ.(PtrType).Sub(), v.Get()); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 515 | func (v *ptrValueStruct) SetSub(subv Value) { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 516 | a := v.typ.(PtrType).Sub(); |
| 517 | b := subv.Type(); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 518 | if !equalType(a, b) { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 519 | panicln("reflect: incompatible types in PtrValue.SetSub:", |
| 520 | a.String(), b.String()); |
Rob Pike | 419e1e0 | 2008-11-12 19:05:05 -0800 | [diff] [blame] | 521 | } |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 522 | *(*Addr)(v.addr) = subv.Addr(); |
Rob Pike | b1d37b7 | 2008-11-12 14:19:39 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 525 | func ptrCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 526 | return &ptrValueStruct{ commonValue{PtrKind, typ, addr} }; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Rob Pike | c4af3e7 | 2008-10-26 08:28:33 -0700 | [diff] [blame] | 529 | // -- Array |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 530 | // Slices and arrays are represented by the same interface. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 531 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 532 | // ArrayValue represents an array or slice value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 533 | type ArrayValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 534 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 535 | IsSlice() bool; // Is this a slice (true) or array (false)? |
| 536 | Len() int; // The length of the array/slice. |
| 537 | Cap() int; // The capacity of the array/slice (==Len() for arrays). |
| 538 | Elem(i int) Value; // The Value of the i'th element. |
| 539 | SetLen(len int); // Set the length; slice only. |
| 540 | Set(src ArrayValue); // Set the underlying Value; slice only for src and dest both. |
| 541 | CopyFrom(src ArrayValue, n int) // Copy the elements from src; lengths must match. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 542 | } |
Rob Pike | c4af3e7 | 2008-10-26 08:28:33 -0700 | [diff] [blame] | 543 | |
Rob Pike | c5f99cc | 2009-01-21 15:45:54 -0800 | [diff] [blame] | 544 | func copyArray(dst ArrayValue, src ArrayValue, n int); |
| 545 | |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 546 | /* |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 547 | Run-time representation of slices looks like this: |
| 548 | struct Slice { |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 549 | byte* array; // actual data |
| 550 | uint32 nel; // number of elements |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 551 | uint32 cap; |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 552 | }; |
| 553 | */ |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 554 | type runtimeSlice struct { |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 555 | data Addr; |
| 556 | len uint32; |
| 557 | cap uint32; |
| 558 | } |
| 559 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 560 | type sliceValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 561 | commonValue; |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 562 | elemtype Type; |
| 563 | elemsize int; |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 564 | slice *runtimeSlice; |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 565 | } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 566 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 567 | func (v *sliceValueStruct) IsSlice() bool { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 568 | return true |
| 569 | } |
| 570 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 571 | func (v *sliceValueStruct) Len() int { |
| 572 | return int(v.slice.len); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 575 | func (v *sliceValueStruct) Cap() int { |
| 576 | return int(v.slice.cap); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 579 | func (v *sliceValueStruct) SetLen(len int) { |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 580 | if len > v.Cap() { |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 581 | panicln("reflect: sliceValueStruct.SetLen", len, v.Cap()); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 582 | } |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 583 | v.slice.len = uint32(len); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 586 | func (v *sliceValueStruct) Set(src ArrayValue) { |
| 587 | if !src.IsSlice() { |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 588 | panic("can't set slice from array"); |
Rob Pike | 1b3299e | 2009-01-23 12:40:55 -0800 | [diff] [blame] | 589 | } |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 590 | s := src.(*sliceValueStruct); |
Rob Pike | 1b3299e | 2009-01-23 12:40:55 -0800 | [diff] [blame] | 591 | if !equalType(v.typ, s.typ) { |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 592 | panicln("incompatible types in ArrayValue.Set()"); |
Rob Pike | 1b3299e | 2009-01-23 12:40:55 -0800 | [diff] [blame] | 593 | } |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 594 | *v.slice = *s.slice; |
Rob Pike | 1b3299e | 2009-01-23 12:40:55 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 597 | func (v *sliceValueStruct) Elem(i int) Value { |
| 598 | data_uint := uintptr(v.slice.data) + uintptr(i * v.elemsize); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 599 | return newValueAddr(v.elemtype, Addr(data_uint)); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 602 | func (v *sliceValueStruct) CopyFrom(src ArrayValue, n int) { |
Rob Pike | c5f99cc | 2009-01-21 15:45:54 -0800 | [diff] [blame] | 603 | copyArray(v, src, n); |
| 604 | } |
| 605 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 606 | type arrayValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 607 | commonValue; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 608 | elemtype Type; |
Rob Pike | 554d0aa | 2008-11-05 10:17:38 -0800 | [diff] [blame] | 609 | elemsize int; |
| 610 | len int; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 613 | func (v *arrayValueStruct) IsSlice() bool { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 614 | return false |
| 615 | } |
| 616 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 617 | func (v *arrayValueStruct) Len() int { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 618 | return v.len |
| 619 | } |
| 620 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 621 | func (v *arrayValueStruct) Cap() int { |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 622 | return v.len |
| 623 | } |
| 624 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 625 | func (v *arrayValueStruct) SetLen(len int) { |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 626 | panicln("can't set len of array"); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 629 | func (v *arrayValueStruct) Set(src ArrayValue) { |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 630 | panicln("can't set array"); |
Rob Pike | 1b3299e | 2009-01-23 12:40:55 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 633 | func (v *arrayValueStruct) Elem(i int) Value { |
Rob Pike | 50d0695 | 2008-12-09 15:41:21 -0800 | [diff] [blame] | 634 | data_uint := uintptr(v.addr) + uintptr(i * v.elemsize); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 635 | return newValueAddr(v.elemtype, Addr(data_uint)); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 636 | return nil |
| 637 | } |
| 638 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 639 | func (v *arrayValueStruct) CopyFrom(src ArrayValue, n int) { |
Rob Pike | c5f99cc | 2009-01-21 15:45:54 -0800 | [diff] [blame] | 640 | copyArray(v, src, n); |
| 641 | } |
| 642 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 643 | func arrayCreator(typ Type, addr Addr) Value { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 644 | arraytype := typ.(ArrayType); |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 645 | if arraytype.IsSlice() { |
| 646 | v := new(sliceValueStruct); |
Rob Pike | c4af3e7 | 2008-10-26 08:28:33 -0700 | [diff] [blame] | 647 | v.kind = ArrayKind; |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 648 | v.addr = addr; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 649 | v.typ = typ; |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 650 | v.elemtype = arraytype.Elem(); |
| 651 | v.elemsize = v.elemtype.Size(); |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 652 | v.slice = (*runtimeSlice)(addr); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 653 | return v; |
| 654 | } |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 655 | v := new(arrayValueStruct); |
Rob Pike | c4af3e7 | 2008-10-26 08:28:33 -0700 | [diff] [blame] | 656 | v.kind = ArrayKind; |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 657 | v.addr = addr; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 658 | v.typ = typ; |
| 659 | v.elemtype = arraytype.Elem(); |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 660 | v.elemsize = v.elemtype.Size(); |
| 661 | v.len = arraytype.Len(); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 662 | return v; |
| 663 | } |
| 664 | |
| 665 | // -- Map TODO: finish and test |
| 666 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 667 | // MapValue represents a map value. |
| 668 | // Its implementation is incomplete. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 669 | type MapValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 670 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 671 | Len() int; // The number of elements; currently always returns 0. |
| 672 | Elem(key Value) Value; // The value indexed by key; unimplemented. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 675 | type mapValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 676 | commonValue |
Russ Cox | de13727 | 2008-10-23 12:41:06 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 679 | func mapCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 680 | return &mapValueStruct{ commonValue{MapKind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 683 | func (v *mapValueStruct) Len() int { |
Russ Cox | de13727 | 2008-10-23 12:41:06 -0700 | [diff] [blame] | 684 | return 0 // TODO: probably want this to be dynamic |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 687 | func (v *mapValueStruct) Elem(key Value) Value { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 688 | panic("map value element"); |
| 689 | return nil |
| 690 | } |
| 691 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 692 | // -- Chan |
| 693 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 694 | // ChanValue represents a chan value. |
| 695 | // Its implementation is incomplete. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 696 | type ChanValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 697 | Value; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 700 | type chanValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 701 | commonValue |
Russ Cox | de13727 | 2008-10-23 12:41:06 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 704 | func chanCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 705 | return &chanValueStruct{ commonValue{ChanKind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 708 | // -- Struct |
| 709 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 710 | // StructValue represents a struct value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 711 | type StructValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 712 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 713 | Len() int; // The number of fields. |
| 714 | Field(i int) Value; // The Value of field i. |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 717 | type structValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 718 | commonValue; |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 719 | field []Value; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 722 | func (v *structValueStruct) Len() int { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 723 | return len(v.field) |
| 724 | } |
| 725 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 726 | func (v *structValueStruct) Field(i int) Value { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 727 | return v.field[i] |
| 728 | } |
| 729 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 730 | func structCreator(typ Type, addr Addr) Value { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 731 | t := typ.(StructType); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 732 | nfield := t.Len(); |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 733 | v := &structValueStruct{ commonValue{StructKind, typ, addr}, make([]Value, nfield) }; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 734 | for i := 0; i < nfield; i++ { |
Rob Pike | 12a3435 | 2008-10-30 17:29:53 -0700 | [diff] [blame] | 735 | name, ftype, str, offset := t.Field(i); |
Rob Pike | 50d0695 | 2008-12-09 15:41:21 -0800 | [diff] [blame] | 736 | addr_uint := uintptr(addr) + uintptr(offset); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 737 | v.field[i] = newValueAddr(ftype, Addr(addr_uint)); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 738 | } |
| 739 | v.typ = typ; |
| 740 | return v; |
| 741 | } |
| 742 | |
| 743 | // -- Interface |
| 744 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 745 | // InterfaceValue represents an interface value. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 746 | type InterfaceValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 747 | Value; |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 748 | Get() interface {}; // Get the underlying interface{} value. |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 749 | Value() Value; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 752 | type interfaceValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 753 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 756 | func (v *interfaceValueStruct) Get() interface{} { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 757 | return *(*interface{})(v.addr) |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 760 | func (v *interfaceValueStruct) Value() Value { |
| 761 | i := v.Get(); |
| 762 | if i == nil { |
| 763 | return nil; |
| 764 | } |
| 765 | return NewValue(i); |
| 766 | } |
| 767 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 768 | func interfaceCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 769 | return &interfaceValueStruct{ commonValue{InterfaceKind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 772 | // -- Func |
| 773 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 774 | |
| 775 | // FuncValue represents a func value. |
| 776 | // Its implementation is incomplete. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 777 | type FuncValue interface { |
Russ Cox | dfad8ea | 2009-02-16 16:37:49 -0800 | [diff] [blame] | 778 | Value; |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 781 | type funcValueStruct struct { |
Russ Cox | b54133d | 2009-01-15 16:16:42 -0800 | [diff] [blame] | 782 | commonValue |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 785 | func funcCreator(typ Type, addr Addr) Value { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 786 | return &funcValueStruct{ commonValue{FuncKind, typ, addr} } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 789 | var creator = map[int] creatorFn { |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 790 | MissingKind : missingCreator, |
| 791 | IntKind : intCreator, |
| 792 | Int8Kind : int8Creator, |
| 793 | Int16Kind : int16Creator, |
| 794 | Int32Kind : int32Creator, |
| 795 | Int64Kind : int64Creator, |
| 796 | UintKind : uintCreator, |
| 797 | Uint8Kind : uint8Creator, |
| 798 | Uint16Kind : uint16Creator, |
| 799 | Uint32Kind : uint32Creator, |
| 800 | Uint64Kind : uint64Creator, |
| 801 | UintptrKind : uintptrCreator, |
| 802 | FloatKind : floatCreator, |
| 803 | Float32Kind : float32Creator, |
| 804 | Float64Kind : float64Creator, |
Russ Cox | 4cf7711 | 2009-01-30 14:39:31 -0800 | [diff] [blame] | 805 | StringKind : stringCreator, |
| 806 | BoolKind : boolCreator, |
| 807 | PtrKind : ptrCreator, |
| 808 | ArrayKind : arrayCreator, |
| 809 | MapKind : mapCreator, |
| 810 | ChanKind : chanCreator, |
| 811 | StructKind : structCreator, |
| 812 | InterfaceKind : interfaceCreator, |
| 813 | FuncKind : funcCreator, |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 814 | } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 815 | |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 816 | var typecache = make(map[string] Type); |
Rob Pike | 9ba97ca | 2008-12-11 14:41:12 -0800 | [diff] [blame] | 817 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 818 | func newValueAddr(typ Type, addr Addr) Value { |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 819 | c, ok := creator[typ.Kind()]; |
| 820 | if !ok { |
| 821 | panicln("no creator for type" , typ.Kind()); |
| 822 | } |
| 823 | return c(typ, addr); |
| 824 | } |
| 825 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 826 | // NewInitValue creates a new, zero-initialized Value for the specified Type. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 827 | func NewInitValue(typ Type) Value { |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 828 | // Some values cannot be made this way. |
| 829 | switch typ.Kind() { |
Russ Cox | ba882f9 | 2008-12-19 03:06:19 -0800 | [diff] [blame] | 830 | case FuncKind: // must be pointers, at least for now (TODO?) |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 831 | return nil; |
| 832 | case ArrayKind: |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 833 | if typ.(ArrayType).IsSlice() { |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 834 | return nil |
| 835 | } |
| 836 | } |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 837 | size := typ.Size(); |
| 838 | if size == 0 { |
| 839 | size = 1; |
| 840 | } |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 841 | data := make([]uint8, size); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 842 | return newValueAddr(typ, Addr(&data[0])); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 845 | // NewSliceValue creates a new, zero-initialized slice value (ArrayValue) for the specified |
| 846 | // slice type (ArrayType), length, and capacity. |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 847 | func NewSliceValue(typ ArrayType, len, cap int) ArrayValue { |
| 848 | if !typ.IsSlice() { |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 849 | return nil |
| 850 | } |
| 851 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 852 | array := new(runtimeSlice); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 853 | size := typ.Elem().Size() * cap; |
| 854 | if size == 0 { |
| 855 | size = 1; |
| 856 | } |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 857 | data := make([]uint8, size); |
Rob Pike | 50d0695 | 2008-12-09 15:41:21 -0800 | [diff] [blame] | 858 | array.data = Addr(&data[0]); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 859 | array.len = uint32(len); |
| 860 | array.cap = uint32(cap); |
| 861 | |
Russ Cox | 49e2087 | 2009-02-11 17:55:16 -0800 | [diff] [blame] | 862 | return newValueAddr(typ, Addr(array)).(ArrayValue); |
Russ Cox | e4f4ab0 | 2008-11-13 13:42:59 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Rob Pike | 9a7332f | 2009-01-23 15:56:04 -0800 | [diff] [blame] | 865 | // Works on both slices and arrays |
Rob Pike | c5f99cc | 2009-01-21 15:45:54 -0800 | [diff] [blame] | 866 | func copyArray(dst ArrayValue, src ArrayValue, n int) { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 867 | if n == 0 { |
| 868 | return |
| 869 | } |
| 870 | dt := dst.Type().(ArrayType).Elem(); |
| 871 | st := src.Type().(ArrayType).Elem(); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 872 | if !equalType(dt, st) { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 873 | panicln("reflect: incompatible types in CopyArray:", |
| 874 | dt.String(), st.String()); |
| 875 | } |
| 876 | if n < 0 || n > dst.Len() || n > src.Len() { |
| 877 | panicln("reflect: CopyArray: invalid count", n); |
| 878 | } |
| 879 | dstp := uintptr(dst.Elem(0).Addr()); |
| 880 | srcp := uintptr(src.Elem(0).Addr()); |
| 881 | end := uintptr(n)*uintptr(dt.Size()); |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 882 | if end % 8 == 0 { |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 883 | for i := uintptr(0); i < end; i += 8{ |
| 884 | di := Addr(dstp + i); |
| 885 | si := Addr(srcp + i); |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 886 | *(*uint64)(di) = *(*uint64)(si); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 887 | } |
| 888 | } else { |
| 889 | for i := uintptr(0); i < end; i++ { |
| 890 | di := Addr(dstp + i); |
| 891 | si := Addr(srcp + i); |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 892 | *(*byte)(di) = *(*byte)(si); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
Rob Pike | 3bc6fd6 | 2009-03-09 17:47:15 -0700 | [diff] [blame] | 897 | // NewValue creates a new Value from the interface{} object provided. |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 898 | func NewValue(e interface {}) Value { |
Russ Cox | 3609624 | 2009-01-16 14:58:14 -0800 | [diff] [blame] | 899 | value, typestring, indir := sys.Reflect(e); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 900 | typ, ok := typecache[typestring]; |
Rob Pike | 842e1a9 | 2008-11-10 14:53:40 -0800 | [diff] [blame] | 901 | if !ok { |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 902 | typ = ParseTypeString("", typestring); |
| 903 | typecache[typestring] = typ; |
Rob Pike | 842e1a9 | 2008-11-10 14:53:40 -0800 | [diff] [blame] | 904 | } |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 905 | |
| 906 | if indir { |
| 907 | // Content of interface is a pointer. |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 908 | return newValueAddr(typ, Addr(uintptr(value))); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | // Content of interface is a value; |
| 912 | // need a permanent copy to take its address. |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 913 | ap := new(uint64); |
Rob Pike | 34b8873 | 2008-10-22 16:48:17 -0700 | [diff] [blame] | 914 | *ap = value; |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 915 | return newValueAddr(typ, Addr(ap)); |
Rob Pike | fac3dfe | 2008-10-22 11:02:56 -0700 | [diff] [blame] | 916 | } |
Rob Pike | 3a7df4d | 2009-04-08 23:33:31 -0700 | [diff] [blame^] | 917 | |
| 918 | // Indirect indirects one level through a value, if it is a pointer. |
| 919 | // If not a pointer, the value is returned unchanged. |
| 920 | // Useful when walking arbitrary data structures. |
| 921 | func Indirect(v Value) Value { |
| 922 | if v.Kind() == PtrKind { |
| 923 | p := v.(PtrValue); |
| 924 | if p.Get() == nil { |
| 925 | return nil |
| 926 | } |
| 927 | v = p.Sub() |
| 928 | } |
| 929 | return v |
| 930 | } |