Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1981)

Side by Side Diff: src/pkg/reflect/value.go

Issue 4281055: code review 4281055: reflect: new Type and Value definitions (Closed)
Patch Set: diff -r 82050f8e7881 https://go.googlecode.com/hg/ Created 12 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« src/pkg/reflect/type.go ('K') | « src/pkg/reflect/type.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package reflect 5 package reflect
6 6
7 import ( 7 import (
8 "math" 8 "math"
9 "runtime" 9 "runtime"
10 "unsafe" 10 "unsafe"
(...skipping 23 matching lines...) Expand all
34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i)) 34 *(*byte)(addr(dst + i)) = *(*byte)(addr(src + i))
35 } 35 }
36 default: 36 default:
37 // word copy forward 37 // word copy forward
38 for i := uintptr(0); i < n; i += ptrSize { 38 for i := uintptr(0); i < n; i += ptrSize {
39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i)) 39 *(*uintptr)(addr(dst + i)) = *(*uintptr)(addr(src + i))
40 } 40 }
41 } 41 }
42 } 42 }
43 43
44 // Value is the common interface to reflection values. 44 // Value is the reflection interface to a Go value.
45 // The implementations of Value (e.g., ArrayValue, StructValue) 45 //
46 // Not all methods apply to all kinds of values. Restrictions,
47 // if any, are noted in the documentation for each method.
48 // Use the Kind method to find out the kind of value before
49 // calling kind-specific methods. Calling a method
50 // inappropriate to the kind of type causes a run time panic.
51 //
52 // The zero Value represents no value.
53 // Its IsValid method returns false, its Kind method returns Invalid,
54 // its String method returns "<invalid Value>", and all other methods panic.
niemeyer 2011/04/04 13:52:35 The usage of the zero value Value{} as meaning som
rsc 2011/04/05 16:29:45 Suggest a better name?
55 // Most functions and methods never return an invalid value.
56 // If one does, its documentation states the conditions explicitly.
57 type Value struct {
58 Internal valueInterface
r 2011/04/02 18:45:33 why is Internal exported?
niemeyer 2011/04/04 13:52:35 Likely to allow the use of Value{}, but indeed it
rog 2011/04/04 17:06:50 we don't allow passing external structs with unexp
59 }
60
61 // TODO(rsc): This implementation of Value is a just a façade
62 // in front of the old implementation, now called valueInterface.
63 // A future CL will change it to a real implementation.
64 // Changing the API is already a big enough step for one CL.
65
66 // A ValueError occurs when a Value method is invoked on
67 // a Value that does not support it. Such cases are documented
68 // in the description of each method.
69 type ValueError struct {
70 Method string
71 Kind Kind
72 }
73
74 func (e *ValueError) String() string {
75 if e.Kind == 0 {
76 return "reflect: call of " + e.Method + " on zero Value"
77 }
78 return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Val ue"
79 }
80
81 // methodName returns the name of the calling method,
82 // assumed to be two stack frames above.
83 func methodName() string {
84 pc, _, _, _ := runtime.Caller(2)
85 f := runtime.FuncForPC(pc)
86 if f == nil {
87 return "unknown method"
88 }
89 return f.Name()
90 }
91
92 func (v Value) internal() valueInterface {
93 vi := v.Internal
94 if vi == nil {
95 panic(&ValueError{methodName(), 0})
96 }
97 return vi
98 }
99
100 func (v Value) kindPanic(want Kind) valueInterface {
r 2011/04/02 18:45:33 this name reads poorly when used. i'd called it pa
101 vi := v.Internal
102 if vi == nil {
103 panic(&ValueError{methodName(), 0})
104 }
105 if k := vi.Kind(); k != want {
106 panic(&ValueError{methodName(), k})
107 }
108 return vi
109 }
110
111 func (v Value) kindsPanic(wants []Kind) valueInterface {
r 2011/04/02 18:45:33 ditto
112 vi := v.Internal
113 if vi == nil {
114 panic(&ValueError{methodName(), 0})
115 }
116 k := vi.Kind()
117 for _, want := range wants {
118 if k == want {
119 return vi
120 }
121 }
122 panic(&ValueError{methodName(), k})
123 }
124
125 // Addr returns a pointer value representing the address of v.
126 // It panics if CanAddr() returns false.
127 // Addr is typically used to obtain a pointer to a struct field
128 // or slice element in order to call a method that requires a
129 // pointer receiver.
130 func (v Value) Addr() Value {
131 return v.internal().Addr()
132 }
133
134 // Bool returns v's underlying value.
135 // It panics if v's kind is not Bool.
136 func (v Value) Bool() bool {
137 u := v.kindPanic(Bool).(*boolValue)
138 return u.Get()
139 }
140
141 // CanAddr returns true if the value's address can be obtained with Addr.
142 // Such values are called addressable. A value is addressable if it is
143 // an element of a slice, an element of an addressable array,
144 // a field of an addressable struct, the result of dereferencing a pointer,
145 // or the result of a call to NewValue, MakeChan, MakeMap, or MakeZero.
146 // If CanAddr returns false, calling Addr will panic.
147 func (v Value) CanAddr() bool {
148 return v.internal().CanAddr()
149 }
150
151 // CanSet returns true if the value of v can be changed.
152 // Values obtained by the use of unexported struct fields
153 // can be read but not set.
154 // If CanSet returns false, calling Set or any type-specific
155 // setter (e.g., SetBool, SetInt64) will panic.
156 func (v Value) CanSet() bool {
niemeyer 2011/04/04 13:52:35 Can we have a CanBeNil() or similar in the same sp
rog 2011/04/04 17:06:50 an alternative might be to have IsNil return false
rsc 2011/04/05 16:29:45 I think by "usual trick" you mean testing for the
157 return v.internal().CanSet()
158 }
159
160 // Call calls the function v with the input parameters in.
161 // It panics if v's Kind is not Func.
162 // It returns the output parameters as Values.
163 func (v Value) Call(in []Value) []Value {
164 return v.kindPanic(Func).(*funcValue).Call(in)
165 }
166
167 var capKinds = []Kind{Array, Chan, Slice}
168
169 type capper interface {
170 Cap() int
171 }
172
173 // Cap returns v's capacity.
174 // It panics if v's Kind is not Array, Chan, or Slice.
175 func (v Value) Cap() int {
176 return v.kindsPanic(capKinds).(capper).Cap()
177 }
178
179 // Close closes the channel v.
180 // It panics if v's Kind is not Chan.
181 func (v Value) Close() {
182 v.kindPanic(Chan).(*chanValue).Close()
183 }
184
185 var complexKinds = []Kind{Complex64, Complex128}
186
187 // Complex returns v's underlying value, as a complex128.
188 // It panics if v's Kind is not Complex64 or Complex128
189 func (v Value) Complex() complex128 {
190 return v.kindsPanic(complexKinds).(*complexValue).Get()
191 }
192
193 var interfaceOrPtr = []Kind{Interface, Ptr}
194
195 type elemer interface {
196 Elem() Value
197 }
198
199 // Elem returns the value that the interface v contains
200 // or that the pointer v points to.
201 // It panics if v's Kind is not Interface or Ptr.
202 // It returns the zero Value if v is nil.
203 func (v Value) Elem() Value {
204 return v.kindsPanic(interfaceOrPtr).(elemer).Elem()
205 }
206
207 // Field returns the i'th field of the struct v.
208 // It panics if v's Kind is not Struct.
209 func (v Value) Field(i int) Value {
210 return v.kindPanic(Struct).(*structValue).Field(i)
211 }
212
213 // FieldByIndex returns the nested field corresponding to index.
214 // It panics if v's Kind is not struct.
215 func (v Value) FieldByIndex(index []int) Value {
216 return v.kindPanic(Struct).(*structValue).FieldByIndex(index)
217 }
218
219 // FieldByName returns the struct field with the given name.
220 // It returns the zero Value if no field was found.
221 // It panics if v's Kind is not struct.
222 func (v Value) FieldByName(name string) Value {
223 return v.kindPanic(Struct).(*structValue).FieldByName(name)
224 }
225
226 // FieldByNameFunc returns the struct field with a name
227 // that satisfies the match function.
228 // It panics if v's Kind is not struct.
229 // It returns the zero Value if no field was found.
230 func (v Value) FieldByNameFunc(match func(string) bool) Value {
231 return v.kindPanic(Struct).(*structValue).FieldByNameFunc(match)
232 }
233
234 var floatKinds = []Kind{Float32, Float64}
235
236 // Float returns v's underlying value, as an float64.
237 // It panics if v's Kind is not Float32 or Float64
238 func (v Value) Float() float64 {
239 return v.kindsPanic(floatKinds).(*floatValue).Get()
240 }
241
242 var arrayOrSlice = []Kind{Array, Slice}
243
244 // Index returns v's i'th element.
245 // It panics if v's Kind is not Array or Slice.
246 func (v Value) Index(i int) Value {
247 return v.kindsPanic(arrayOrSlice).(arrayOrSliceValue).Elem(i)
248 }
249
250 var intKinds = []Kind{Int, Int8, Int16, Int32, Int64}
251
252 // Int returns v's underlying value, as an int64.
253 // It panics if v's Kind is not a sized or unsized Int kind.
254 func (v Value) Int() int64 {
255 return v.kindsPanic(intKinds).(*intValue).Get()
256 }
257
258 // Interface returns v's value as an interface{}.
259 // If v is a method obtained by invoking Value.Method
260 // (as opposed to Type.Method), Interface cannot return an
261 // interface value, so it panics.
262 func (v Value) Interface() interface{} {
263 return v.internal().Interface()
264 }
265
266 // InterfaceData returns the interface v's value as a uintptr pair.
267 // It panics if v's Kind is not Interface.
268 func (v Value) InterfaceData() [2]uintptr {
269 return v.kindPanic(Interface).(*InterfaceValue).Get()
270 }
271
272 var nilKinds = []Kind{Chan, Func, Interface, Map, Ptr, Slice}
273
274 type isNiller interface {
275 IsNil() bool
276 }
277
278 // IsNil returns true if v is a nil value.
279 // It panics if v's Kind is not Chan, Func, Interface, Map, Ptr, or Slice.
niemeyer 2011/04/04 13:52:35 If CanBeNil is introduced, this may be documented
280 func (v Value) IsNil() bool {
281 return v.kindsPanic(nilKinds).(isNiller).IsNil()
282 }
283
284 // IsValid returns true if v represents a value.
285 // It returns false if v is the zero Value.
286 // If IsValid returns false, all other methods except String panic.
287 // Most functions and methods never return an invalid value.
288 // If one does, its documentation states the conditions explicitly.
289 func (v Value) IsValid() bool {
290 return v.Internal != nil
291 }
292
293 // Kind returns v's Kind.
294 // If v is the zero Value (IsValid returns false), Kind returns Invalid.
295 func (v Value) Kind() Kind {
296 if v.Internal == nil {
297 return Invalid
298 }
299 return v.internal().Kind()
300 }
301
302 var lenKinds = []Kind{Array, Chan, Map, Slice}
303
304 type lenner interface {
305 Len() int
306 }
307
308 // Len returns v's length.
309 // It panics if v's Kind is not Array, Chan, Map, or Slice.
310 func (v Value) Len() int {
311 return v.kindsPanic(lenKinds).(lenner).Len()
312 }
313
314 // MapIndex returns the value associated with key in the map v.
315 // It panics if v's Kind is not Map.
316 // It returns the zero Value if key is not found in the map.
317 func (v Value) MapIndex(key Value) Value {
318 return v.kindPanic(Map).(*mapValue).Elem(key)
319 }
320
321 // MapKeys returns a slice containing all the keys present in the map,
322 // in unspecified order.
323 // It panics if v's Kind is not Map.
324 func (v Value) MapKeys() []Value {
325 return v.kindPanic(Map).(*mapValue).Keys()
326 }
327
328 // Method returns a function value corresponding to v's i'th method.
329 // The arguments to a Call on the returned function should not include
330 // a receiver; the returned function will always use v as the receiver.
331 func (v Value) Method(i int) Value {
332 return v.internal().Method(i)
333 }
334
335 // NumField returns the number of fields in the struct v.
336 // It panics if v's Kind is not Struct.
337 func (v Value) NumField() int {
338 return v.kindPanic(Struct).(*structValue).NumField()
339 }
340
341 // OverflowComplex returns true if the complex128 x cannot be represented by v's type.
r 2011/04/02 18:45:33 not clear what this means, exactly. i mean, we hav
niemeyer 2011/04/04 13:52:35 s/type/Kind/ (v's type is Value). Same for other
rsc 2011/04/05 16:29:45 You're being pedantic. v refers to the underlying
342 // It panics if v's Kind is not Complex64 or Complex128.
343 func (v Value) OverflowComplex(x complex128) bool {
344 return v.kindsPanic(complexKinds).(*complexValue).Overflow(x)
345 }
346
347 // OverflowFloat returns true if the float64 x cannot be represented by v's type .
348 // It panics if v's Kind is not Float32 or Float64.
349 func (v Value) OverflowFloat(x float64) bool {
350 return v.kindsPanic(floatKinds).(*floatValue).Overflow(x)
351 }
352
353 // OverflowInt returns true if the int64 x cannot be represented by v's type.
354 // It panics if v's Kind is not a sized or unsized Int kind.
355 func (v Value) OverflowInt(x int64) bool {
356 return v.kindsPanic(intKinds).(*intValue).Overflow(x)
357 }
358
359 // OverflowUint returns true if the uint64 x cannot be represented by v's type.
360 // It panics if v's Kind is not a sized or unsized Uint kind.
361 func (v Value) OverflowUint(x uint64) bool {
362 return v.kindsPanic(uintKinds).(*uintValue).Overflow(x)
363 }
364
365 var pointerKinds = []Kind{Chan, Func, Map, Ptr, Slice, UnsafePointer}
366
367 type uintptrGetter interface {
368 Get() uintptr
369 }
370
371 // UnsafePointer returns v's value as a uintptr.
r 2011/04/02 18:45:33 it's called Pointer
372 // It returns uintptr instead of unsafe.Pointer so that
373 // code using reflect cannot obtain unsafe.Pointers
374 // without importing the unsafe package explicitly.
375 // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer.
376 func (v Value) Pointer() uintptr {
377 return v.kindsPanic(pointerKinds).(uintptrGetter).Get()
378 }
379
380
381 // Recv receives and returns a value from the channel v.
382 // It panics if v's Kind is not Chan.
383 // The receive blocks until a value is ready.
384 // The boolean value ok is true if the value x corresponds to a send
385 // on the channel, false if it is a zero value received because the channel is c losed.
386 func (v Value) Recv() (x Value, ok bool) {
387 return v.kindPanic(Chan).(*chanValue).Recv()
388 }
389
390 // Send sends x on the channel v.
391 // It panics if v's kind is not Chan or if x's type is not the same type as v's element type.
392 func (v Value) Send(x Value) {
393 v.kindPanic(Chan).(*chanValue).Send(x)
394 }
395
396 // Set assigns x to the value v; x must have the same type as v.
niemeyer 2011/04/04 13:52:35 s/same type/same Kind/
rsc 2011/04/05 16:29:45 No. It must have the same type. You can't assign
397 // It panics if CanSet() returns false.
398 func (v Value) Set(x Value) {
399 v.internal().SetValue(x)
400 }
401
402 // SetBool sets v's underlying value.
403 // It panics if v's Kind is not Bool or if CanSet() is false.
404 func (v Value) SetBool(x bool) {
405 v.kindPanic(Bool).(*boolValue).Set(x)
406 }
407
408 // SetComplex sets v's underlying value to x.
409 // It panics if v's Kind is not Complex64 or Complex128, or if CanSet() is false .
410 func (v Value) SetComplex(x complex128) {
411 v.kindsPanic(complexKinds).(*complexValue).Set(x)
412 }
413
414 // SetFloat sets v's underlying value to x.
415 // It panics if v's Kind is not Float32 or Float64, or if CanSet() is false.
416 func (v Value) SetFloat(x float64) {
417 v.kindsPanic(floatKinds).(*floatValue).Set(x)
418 }
419
420 // SetInt sets v's underlying value to x.
421 // It panics if v's Kind is not a sized or unsized Int kind, or if CanSet() is f alse.
422 func (v Value) SetInt(x int64) {
423 v.kindsPanic(intKinds).(*intValue).Set(x)
424 }
425
426 // SetLen sets v's length to n.
427 // It panics if v's Kind is not Slice.
428 func (v Value) SetLen(n int) {
429 v.kindPanic(Slice).(*sliceValue).SetLen(n)
430 }
431
432 // SetMapElem sets the value associated with key in the map v to val.
r 2011/04/02 18:45:33 it's called SetMapIndex
433 // It panics if v's Kind is not Map.
434 // If val is the zero Value, SetMapElem deletes the key from the map.
niemeyer 2011/04/04 13:52:35 Some of the semantic confusion described can be se
rsc 2011/04/05 16:29:45 I really don't think this is confusing. The text
435 func (v Value) SetMapIndex(key, val Value) {
436 v.kindPanic(Map).(*mapValue).SetElem(key, val)
437 }
438
439 // SetUint sets v's underlying value to x.
440 // It panics if v's Kind is not a sized or unsized Uint kind, or if CanSet() is false.
441 func (v Value) SetUint(x uint64) {
442 v.kindsPanic(uintKinds).(*uintValue).Set(x)
443 }
444
445 // SetPointer sets the unsafe.Pointer value v to x.
446 // It panics if v's Kind is not UnsafePointer.
447 func (v Value) SetPointer(x unsafe.Pointer) {
448 v.kindPanic(UnsafePointer).(*unsafePointerValue).Set(x)
449 }
450
451 // SetString sets v's underlying value to x.
452 // It panics if v's Kind is not String or if CanSet() is false.
453 func (v Value) SetString(x string) {
454 v.kindPanic(String).(*stringValue).Set(x)
455 }
456
457 // BUG(rsc): Value.Slice should allow slicing arrays.
r 2011/04/02 18:45:33 can you just fix it?
458
459 // Slice returns a slice of v.
460 // It panics if v's Kind is not Slice.
461 func (v Value) Slice(beg, end int) Value {
462 return v.kindPanic(Slice).(*sliceValue).Slice(beg, end)
463 }
464
465 // String returns the string v's underlying value, as a string.
466 // String is a special case because of Go's String method convention.
467 // Unlike the other getters, it does not panic if v's Kind is not String.
468 // Instead, it returns a string of the form "<T value>" where T is v's type.
469 func (v Value) String() string {
470 vi := v.Internal
471 if vi == nil {
472 return "<invalid Value>"
473 }
474 if vi.Kind() == String {
475 return vi.(*stringValue).Get()
476 }
477 return "<" + vi.Type().String() + " Value>"
478 }
479
480 // TryRecv attempts to receive a value from the channel v but will not block.
481 // It panics if v's Kind is not Chan.
482 // If the receive cannot finish without blocking, x is the zero Value.
483 // The boolean ok is true if the value x corresponds to a send
484 // on the channel, false if it is a zero value received because the channel is c losed.
485 func (v Value) TryRecv() (x Value, ok bool) {
486 return v.kindPanic(Chan).(*chanValue).TryRecv()
487 }
488
489 // TrySend attempts to send x on the channel v but will not block.
490 // It panics if v's Kind is not Chan.
491 // It returns true if the value was sent, false otherwise.
492 func (v Value) TrySend(x Value) bool {
493 return v.kindPanic(Chan).(*chanValue).TrySend(x)
494 }
495
496 // Type returns v's type.
niemeyer 2011/04/04 13:52:35 s/v's type/v's Type value/ ?
rsc 2011/04/05 16:29:45 No. Same as above.
497 func (v Value) Type() Type {
498 return v.internal().Type()
499 }
500
501 var uintKinds = []Kind{Uint, Uint8, Uint16, Uint32, Uint64, Uintptr}
502
503 // Uint returns v's underlying value, as a uint64.
504 // It panics if v's Kind is not a sized or unsized Uint kind.
505 func (v Value) Uint() uint64 {
506 return v.kindsPanic(uintKinds).(*uintValue).Get()
507 }
508
509 // UnsafeAddr returns a pointer to v's data.
510 // It is for advanced clients that also import the "unsafe" package.
511 func (v Value) UnsafeAddr() uintptr {
512 return v.internal().UnsafeAddr()
513 }
514
515 // valueInterface is the common interface to reflection values.
516 // The implementations of Value (e.g., arrayValue, structValue)
46 // have additional type-specific methods. 517 // have additional type-specific methods.
47 type Value interface { 518 type valueInterface interface {
48 // Type returns the value's type. 519 // Type returns the value's type.
49 Type() Type 520 Type() Type
50 521
51 // Interface returns the value as an interface{}. 522 // Interface returns the value as an interface{}.
52 Interface() interface{} 523 Interface() interface{}
53 524
54 // CanSet returns true if the value can be changed. 525 // CanSet returns true if the value can be changed.
55 // Values obtained by the use of non-exported struct fields 526 // Values obtained by the use of non-exported struct fields
56 // can be used in Get but not Set. 527 // can be used in Get but not Set.
57 // If CanSet returns false, calling the type-specific Set will panic. 528 // If CanSet returns false, calling the type-specific Set will panic.
58 CanSet() bool 529 CanSet() bool
59 530
60 // SetValue assigns v to the value; v must have the same type as the val ue. 531 // SetValue assigns v to the value; v must have the same type as the val ue.
61 SetValue(v Value) 532 SetValue(v Value)
62 533
63 // CanAddr returns true if the value's address can be obtained with Addr . 534 // CanAddr returns true if the value's address can be obtained with Addr .
64 // Such values are called addressable. A value is addressable if it is 535 // Such values are called addressable. A value is addressable if it is
65 // an element of a slice, an element of an addressable array, 536 // an element of a slice, an element of an addressable array,
66 // a field of an addressable struct, the result of dereferencing a point er, 537 // a field of an addressable struct, the result of dereferencing a point er,
67 // or the result of a call to NewValue, MakeChan, MakeMap, or MakeZero. 538 // or the result of a call to NewValue, MakeChan, MakeMap, or MakeZero.
68 // If CanAddr returns false, calling Addr will panic. 539 // If CanAddr returns false, calling Addr will panic.
69 CanAddr() bool 540 CanAddr() bool
70 541
71 // Addr returns the address of the value. 542 // Addr returns the address of the value.
72 // If the value is not addressable, Addr panics. 543 // If the value is not addressable, Addr panics.
73 // Addr is typically used to obtain a pointer to a struct field or slice element 544 // Addr is typically used to obtain a pointer to a struct field or slice element
74 // in order to call a method that requires a pointer receiver. 545 // in order to call a method that requires a pointer receiver.
75 » Addr() *PtrValue 546 » Addr() Value
76 547
77 // UnsafeAddr returns a pointer to the underlying data. 548 // UnsafeAddr returns a pointer to the underlying data.
78 // It is for advanced clients that also import the "unsafe" package. 549 // It is for advanced clients that also import the "unsafe" package.
79 UnsafeAddr() uintptr 550 UnsafeAddr() uintptr
80 551
81 » // Method returns a FuncValue corresponding to the value's i'th method. 552 » // Method returns a funcValue corresponding to the value's i'th method.
82 » // The arguments to a Call on the returned FuncValue 553 » // The arguments to a Call on the returned funcValue
83 » // should not include a receiver; the FuncValue will use 554 » // should not include a receiver; the funcValue will use
84 // the value as the receiver. 555 // the value as the receiver.
85 » Method(i int) *FuncValue 556 » Method(i int) Value
557
558 » Kind() Kind
86 559
87 getAddr() addr 560 getAddr() addr
88 } 561 }
89 562
90 // flags for value 563 // flags for value
91 const ( 564 const (
92 canSet uint32 = 1 << iota // can set value (write to *v.addr) 565 canSet uint32 = 1 << iota // can set value (write to *v.addr)
93 canAddr // can take address of value 566 canAddr // can take address of value
94 canStore // can store through value (write to **v.add r) 567 canStore // can store through value (write to **v.add r)
95 ) 568 )
96 569
97 // value is the common implementation of most values. 570 // value is the common implementation of most values.
98 // It is embedded in other, public struct types, but always 571 // It is embedded in other, public struct types, but always
99 // with a unique tag like "uint" or "float" so that the client cannot 572 // with a unique tag like "uint" or "float" so that the client cannot
100 // convert from, say, *UintValue to *FloatValue. 573 // convert from, say, *uintValue to *floatValue.
101 type value struct { 574 type value struct {
102 typ Type 575 typ Type
103 addr addr 576 addr addr
104 flag uint32 577 flag uint32
105 } 578 }
106 579
107 func (v *value) Type() Type { return v.typ } 580 func (v *value) Type() Type { return v.typ }
108 581
109 func (v *value) Addr() *PtrValue { 582 func (v *value) Kind() Kind { return v.typ.Kind() }
583
584 func (v *value) Addr() Value {
110 if !v.CanAddr() { 585 if !v.CanAddr() {
111 panic("reflect: cannot take address of value") 586 panic("reflect: cannot take address of value")
112 } 587 }
113 a := v.addr 588 a := v.addr
114 flag := canSet 589 flag := canSet
115 if v.CanSet() { 590 if v.CanSet() {
116 flag |= canStore 591 flag |= canStore
117 } 592 }
118 // We could safely set canAddr here too - 593 // We could safely set canAddr here too -
119 // the caller would get the address of a - 594 // the caller would get the address of a -
120 // but it doesn't match the Go model. 595 // but it doesn't match the Go model.
121 // The language doesn't let you say &&v. 596 // The language doesn't let you say &&v.
122 » return newValue(PtrTo(v.typ), addr(&a), flag).(*PtrValue) 597 » return newValue(PtrTo(v.typ), addr(&a), flag)
123 } 598 }
124 599
125 func (v *value) UnsafeAddr() uintptr { return uintptr(v.addr) } 600 func (v *value) UnsafeAddr() uintptr { return uintptr(v.addr) }
126 601
127 func (v *value) getAddr() addr { return v.addr } 602 func (v *value) getAddr() addr { return v.addr }
128 603
129 func (v *value) Interface() interface{} { 604 func (v *value) Interface() interface{} {
130 » if typ, ok := v.typ.(*InterfaceType); ok { 605 » typ := v.typ
606 » if typ.Kind() == Interface {
131 // There are two different representations of interface values, 607 // There are two different representations of interface values,
132 // one if the interface type has methods and one if it doesn't. 608 // one if the interface type has methods and one if it doesn't.
133 // These two representations require different expressions 609 // These two representations require different expressions
134 // to extract correctly. 610 // to extract correctly.
135 if typ.NumMethod() == 0 { 611 if typ.NumMethod() == 0 {
136 // Extract as interface value without methods. 612 // Extract as interface value without methods.
137 return *(*interface{})(v.addr) 613 return *(*interface{})(v.addr)
138 } 614 }
139 // Extract from v.addr as interface value with methods. 615 // Extract from v.addr as interface value with methods.
140 return *(*interface { 616 return *(*interface {
141 m() 617 m()
142 })(v.addr) 618 })(v.addr)
143 } 619 }
144 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr)) 620 return unsafe.Unreflect(v.typ, unsafe.Pointer(v.addr))
145 } 621 }
146 622
147 func (v *value) CanSet() bool { return v.flag&canSet != 0 } 623 func (v *value) CanSet() bool { return v.flag&canSet != 0 }
148 624
149 func (v *value) CanAddr() bool { return v.flag&canAddr != 0 } 625 func (v *value) CanAddr() bool { return v.flag&canAddr != 0 }
150 626
151 627
152 /* 628 /*
153 * basic types 629 * basic types
154 */ 630 */
155 631
156 // BoolValue represents a bool value. 632 // boolValue represents a bool value.
157 type BoolValue struct { 633 type boolValue struct {
158 value "bool" 634 value "bool"
159 } 635 }
160 636
161 // Get returns the underlying bool value. 637 // Get returns the underlying bool value.
162 func (v *BoolValue) Get() bool { return *(*bool)(v.addr) } 638 func (v *boolValue) Get() bool { return *(*bool)(v.addr) }
163 639
164 // Set sets v to the value x. 640 // Set sets v to the value x.
165 func (v *BoolValue) Set(x bool) { 641 func (v *boolValue) Set(x bool) {
166 if !v.CanSet() { 642 if !v.CanSet() {
167 panic(cannotSet) 643 panic(cannotSet)
168 } 644 }
169 *(*bool)(v.addr) = x 645 *(*bool)(v.addr) = x
170 } 646 }
171 647
172 // Set sets v to the value x. 648 // Set sets v to the value x.
173 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) } 649 func (v *boolValue) SetValue(x Value) { v.Set(x.Bool()) }
174 650
175 // FloatValue represents a float value. 651 // floatValue represents a float value.
176 type FloatValue struct { 652 type floatValue struct {
177 value "float" 653 value "float"
178 } 654 }
179 655
180 // Get returns the underlying int value. 656 // Get returns the underlying int value.
181 func (v *FloatValue) Get() float64 { 657 func (v *floatValue) Get() float64 {
182 switch v.typ.Kind() { 658 switch v.typ.Kind() {
183 case Float32: 659 case Float32:
184 return float64(*(*float32)(v.addr)) 660 return float64(*(*float32)(v.addr))
185 case Float64: 661 case Float64:
186 return *(*float64)(v.addr) 662 return *(*float64)(v.addr)
187 } 663 }
188 panic("reflect: invalid float kind") 664 panic("reflect: invalid float kind")
189 } 665 }
190 666
191 // Set sets v to the value x. 667 // Set sets v to the value x.
192 func (v *FloatValue) Set(x float64) { 668 func (v *floatValue) Set(x float64) {
193 if !v.CanSet() { 669 if !v.CanSet() {
194 panic(cannotSet) 670 panic(cannotSet)
195 } 671 }
196 switch v.typ.Kind() { 672 switch v.typ.Kind() {
197 default: 673 default:
198 panic("reflect: invalid float kind") 674 panic("reflect: invalid float kind")
199 case Float32: 675 case Float32:
200 *(*float32)(v.addr) = float32(x) 676 *(*float32)(v.addr) = float32(x)
201 case Float64: 677 case Float64:
202 *(*float64)(v.addr) = x 678 *(*float64)(v.addr) = x
203 } 679 }
204 } 680 }
205 681
206 // Overflow returns true if x cannot be represented by the type of v. 682 // Overflow returns true if x cannot be represented by the type of v.
207 func (v *FloatValue) Overflow(x float64) bool { 683 func (v *floatValue) Overflow(x float64) bool {
208 if v.typ.Size() == 8 { 684 if v.typ.Size() == 8 {
209 return false 685 return false
210 } 686 }
211 if x < 0 { 687 if x < 0 {
212 x = -x 688 x = -x
213 } 689 }
214 return math.MaxFloat32 < x && x <= math.MaxFloat64 690 return math.MaxFloat32 < x && x <= math.MaxFloat64
215 } 691 }
216 692
217 // Set sets v to the value x. 693 // Set sets v to the value x.
218 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) } 694 func (v *floatValue) SetValue(x Value) { v.Set(x.Float()) }
219 695
220 // ComplexValue represents a complex value. 696 // complexValue represents a complex value.
221 type ComplexValue struct { 697 type complexValue struct {
222 value "complex" 698 value "complex"
223 } 699 }
224 700
225 // Get returns the underlying complex value. 701 // Get returns the underlying complex value.
226 func (v *ComplexValue) Get() complex128 { 702 func (v *complexValue) Get() complex128 {
227 switch v.typ.Kind() { 703 switch v.typ.Kind() {
228 case Complex64: 704 case Complex64:
229 return complex128(*(*complex64)(v.addr)) 705 return complex128(*(*complex64)(v.addr))
230 case Complex128: 706 case Complex128:
231 return *(*complex128)(v.addr) 707 return *(*complex128)(v.addr)
232 } 708 }
233 panic("reflect: invalid complex kind") 709 panic("reflect: invalid complex kind")
234 } 710 }
235 711
236 // Set sets v to the value x. 712 // Set sets v to the value x.
237 func (v *ComplexValue) Set(x complex128) { 713 func (v *complexValue) Set(x complex128) {
238 if !v.CanSet() { 714 if !v.CanSet() {
239 panic(cannotSet) 715 panic(cannotSet)
240 } 716 }
241 switch v.typ.Kind() { 717 switch v.typ.Kind() {
242 default: 718 default:
243 panic("reflect: invalid complex kind") 719 panic("reflect: invalid complex kind")
244 case Complex64: 720 case Complex64:
245 *(*complex64)(v.addr) = complex64(x) 721 *(*complex64)(v.addr) = complex64(x)
246 case Complex128: 722 case Complex128:
247 *(*complex128)(v.addr) = x 723 *(*complex128)(v.addr) = x
248 } 724 }
249 } 725 }
250 726
727 // How did we forget this one?
728 func (v *complexValue) Overflow(x complex128) bool {
729 if v.typ.Size() == 16 {
730 return false
731 }
732 r := real(x)
733 i := imag(x)
734 if r < 0 {
735 r = -r
736 }
737 if i < 0 {
738 i = -i
739 }
740 return math.MaxFloat32 <= r && r <= math.MaxFloat64 ||
741 math.MaxFloat32 <= i && i <= math.MaxFloat64
742 }
743
251 // Set sets v to the value x. 744 // Set sets v to the value x.
252 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) } 745 func (v *complexValue) SetValue(x Value) { v.Set(x.Complex()) }
253 746
254 // IntValue represents an int value. 747 // intValue represents an int value.
255 type IntValue struct { 748 type intValue struct {
256 value "int" 749 value "int"
257 } 750 }
258 751
259 // Get returns the underlying int value. 752 // Get returns the underlying int value.
260 func (v *IntValue) Get() int64 { 753 func (v *intValue) Get() int64 {
261 switch v.typ.Kind() { 754 switch v.typ.Kind() {
262 case Int: 755 case Int:
263 return int64(*(*int)(v.addr)) 756 return int64(*(*int)(v.addr))
264 case Int8: 757 case Int8:
265 return int64(*(*int8)(v.addr)) 758 return int64(*(*int8)(v.addr))
266 case Int16: 759 case Int16:
267 return int64(*(*int16)(v.addr)) 760 return int64(*(*int16)(v.addr))
268 case Int32: 761 case Int32:
269 return int64(*(*int32)(v.addr)) 762 return int64(*(*int32)(v.addr))
270 case Int64: 763 case Int64:
271 return *(*int64)(v.addr) 764 return *(*int64)(v.addr)
272 } 765 }
273 panic("reflect: invalid int kind") 766 panic("reflect: invalid int kind")
274 } 767 }
275 768
276 // Set sets v to the value x. 769 // Set sets v to the value x.
277 func (v *IntValue) Set(x int64) { 770 func (v *intValue) Set(x int64) {
278 if !v.CanSet() { 771 if !v.CanSet() {
279 panic(cannotSet) 772 panic(cannotSet)
280 } 773 }
281 switch v.typ.Kind() { 774 switch v.typ.Kind() {
282 default: 775 default:
283 panic("reflect: invalid int kind") 776 panic("reflect: invalid int kind")
284 case Int: 777 case Int:
285 *(*int)(v.addr) = int(x) 778 *(*int)(v.addr) = int(x)
286 case Int8: 779 case Int8:
287 *(*int8)(v.addr) = int8(x) 780 *(*int8)(v.addr) = int8(x)
288 case Int16: 781 case Int16:
289 *(*int16)(v.addr) = int16(x) 782 *(*int16)(v.addr) = int16(x)
290 case Int32: 783 case Int32:
291 *(*int32)(v.addr) = int32(x) 784 *(*int32)(v.addr) = int32(x)
292 case Int64: 785 case Int64:
293 *(*int64)(v.addr) = x 786 *(*int64)(v.addr) = x
294 } 787 }
295 } 788 }
296 789
297 // Set sets v to the value x. 790 // Set sets v to the value x.
298 func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) } 791 func (v *intValue) SetValue(x Value) { v.Set(x.Int()) }
299 792
300 // Overflow returns true if x cannot be represented by the type of v. 793 // Overflow returns true if x cannot be represented by the type of v.
301 func (v *IntValue) Overflow(x int64) bool { 794 func (v *intValue) Overflow(x int64) bool {
302 bitSize := uint(v.typ.Bits()) 795 bitSize := uint(v.typ.Bits())
303 trunc := (x << (64 - bitSize)) >> (64 - bitSize) 796 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
304 return x != trunc 797 return x != trunc
305 } 798 }
306 799
307 // StringHeader is the runtime representation of a string. 800 // StringHeader is the runtime representation of a string.
308 type StringHeader struct { 801 type StringHeader struct {
309 Data uintptr 802 Data uintptr
310 Len int 803 Len int
311 } 804 }
312 805
313 // StringValue represents a string value. 806 // stringValue represents a string value.
314 type StringValue struct { 807 type stringValue struct {
315 value "string" 808 value "string"
316 } 809 }
317 810
318 // Get returns the underlying string value. 811 // Get returns the underlying string value.
319 func (v *StringValue) Get() string { return *(*string)(v.addr) } 812 func (v *stringValue) Get() string { return *(*string)(v.addr) }
320 813
321 // Set sets v to the value x. 814 // Set sets v to the value x.
322 func (v *StringValue) Set(x string) { 815 func (v *stringValue) Set(x string) {
323 if !v.CanSet() { 816 if !v.CanSet() {
324 panic(cannotSet) 817 panic(cannotSet)
325 } 818 }
326 *(*string)(v.addr) = x 819 *(*string)(v.addr) = x
327 } 820 }
328 821
329 // Set sets v to the value x. 822 // Set sets v to the value x.
330 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) } 823 func (v *stringValue) SetValue(x Value) {
824 » // Do the kind check explicitly, because x.String() does not.
825 » v.Set(x.kindPanic(String).(*stringValue).Get())
826 }
331 827
332 // UintValue represents a uint value. 828 // uintValue represents a uint value.
333 type UintValue struct { 829 type uintValue struct {
334 value "uint" 830 value "uint"
335 } 831 }
336 832
337 // Get returns the underlying uuint value. 833 // Get returns the underlying uuint value.
338 func (v *UintValue) Get() uint64 { 834 func (v *uintValue) Get() uint64 {
339 switch v.typ.Kind() { 835 switch v.typ.Kind() {
340 case Uint: 836 case Uint:
341 return uint64(*(*uint)(v.addr)) 837 return uint64(*(*uint)(v.addr))
342 case Uint8: 838 case Uint8:
343 return uint64(*(*uint8)(v.addr)) 839 return uint64(*(*uint8)(v.addr))
344 case Uint16: 840 case Uint16:
345 return uint64(*(*uint16)(v.addr)) 841 return uint64(*(*uint16)(v.addr))
346 case Uint32: 842 case Uint32:
347 return uint64(*(*uint32)(v.addr)) 843 return uint64(*(*uint32)(v.addr))
348 case Uint64: 844 case Uint64:
349 return *(*uint64)(v.addr) 845 return *(*uint64)(v.addr)
350 case Uintptr: 846 case Uintptr:
351 return uint64(*(*uintptr)(v.addr)) 847 return uint64(*(*uintptr)(v.addr))
352 } 848 }
353 panic("reflect: invalid uint kind") 849 panic("reflect: invalid uint kind")
354 } 850 }
355 851
356 // Set sets v to the value x. 852 // Set sets v to the value x.
357 func (v *UintValue) Set(x uint64) { 853 func (v *uintValue) Set(x uint64) {
358 if !v.CanSet() { 854 if !v.CanSet() {
359 panic(cannotSet) 855 panic(cannotSet)
360 } 856 }
361 switch v.typ.Kind() { 857 switch v.typ.Kind() {
362 default: 858 default:
363 panic("reflect: invalid uint kind") 859 panic("reflect: invalid uint kind")
364 case Uint: 860 case Uint:
365 *(*uint)(v.addr) = uint(x) 861 *(*uint)(v.addr) = uint(x)
366 case Uint8: 862 case Uint8:
367 *(*uint8)(v.addr) = uint8(x) 863 *(*uint8)(v.addr) = uint8(x)
368 case Uint16: 864 case Uint16:
369 *(*uint16)(v.addr) = uint16(x) 865 *(*uint16)(v.addr) = uint16(x)
370 case Uint32: 866 case Uint32:
371 *(*uint32)(v.addr) = uint32(x) 867 *(*uint32)(v.addr) = uint32(x)
372 case Uint64: 868 case Uint64:
373 *(*uint64)(v.addr) = x 869 *(*uint64)(v.addr) = x
374 case Uintptr: 870 case Uintptr:
375 *(*uintptr)(v.addr) = uintptr(x) 871 *(*uintptr)(v.addr) = uintptr(x)
376 } 872 }
377 } 873 }
378 874
379 // Overflow returns true if x cannot be represented by the type of v. 875 // Overflow returns true if x cannot be represented by the type of v.
380 func (v *UintValue) Overflow(x uint64) bool { 876 func (v *uintValue) Overflow(x uint64) bool {
381 bitSize := uint(v.typ.Bits()) 877 bitSize := uint(v.typ.Bits())
382 trunc := (x << (64 - bitSize)) >> (64 - bitSize) 878 trunc := (x << (64 - bitSize)) >> (64 - bitSize)
383 return x != trunc 879 return x != trunc
384 } 880 }
385 881
386 // Set sets v to the value x. 882 // Set sets v to the value x.
387 func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) } 883 func (v *uintValue) SetValue(x Value) { v.Set(x.Uint()) }
388 884
389 // UnsafePointerValue represents an unsafe.Pointer value. 885 // unsafePointerValue represents an unsafe.Pointer value.
390 type UnsafePointerValue struct { 886 type unsafePointerValue struct {
391 value "unsafe.Pointer" 887 value "unsafe.Pointer"
392 } 888 }
393 889
394 // Get returns the underlying uintptr value. 890 // Get returns the underlying uintptr value.
395 // Get returns uintptr, not unsafe.Pointer, so that 891 // Get returns uintptr, not unsafe.Pointer, so that
396 // programs that do not import "unsafe" cannot 892 // programs that do not import "unsafe" cannot
397 // obtain a value of unsafe.Pointer type from "reflect". 893 // obtain a value of unsafe.Pointer type from "reflect".
398 func (v *UnsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v .addr)) } 894 func (v *unsafePointerValue) Get() uintptr { return uintptr(*(*unsafe.Pointer)(v .addr)) }
399 895
400 // Set sets v to the value x. 896 // Set sets v to the value x.
401 func (v *UnsafePointerValue) Set(x unsafe.Pointer) { 897 func (v *unsafePointerValue) Set(x unsafe.Pointer) {
402 if !v.CanSet() { 898 if !v.CanSet() {
403 panic(cannotSet) 899 panic(cannotSet)
404 } 900 }
405 *(*unsafe.Pointer)(v.addr) = x 901 *(*unsafe.Pointer)(v.addr) = x
406 } 902 }
407 903
408 // Set sets v to the value x. 904 // Set sets v to the value x.
409 func (v *UnsafePointerValue) SetValue(x Value) { 905 func (v *unsafePointerValue) SetValue(x Value) {
410 » v.Set(unsafe.Pointer(x.(*UnsafePointerValue).Get())) 906 » // Do the kind check explicitly, because x.UnsafePointer
907 » // applies to more than just the UnsafePointer Kind.
908 » v.Set(unsafe.Pointer(x.kindPanic(UnsafePointer).(*unsafePointerValue).Ge t()))
411 } 909 }
412 910
413 func typesMustMatch(t1, t2 Type) { 911 func typesMustMatch(t1, t2 Type) {
414 if t1 != t2 { 912 if t1 != t2 {
415 panic("type mismatch: " + t1.String() + " != " + t2.String()) 913 panic("type mismatch: " + t1.String() + " != " + t2.String())
416 } 914 }
417 } 915 }
418 916
419 /* 917 /*
420 * array 918 * array
421 */ 919 */
422 920
423 // ArrayOrSliceValue is the common interface 921 // ArrayOrSliceValue is the common interface
424 // implemented by both ArrayValue and SliceValue. 922 // implemented by both arrayValue and sliceValue.
425 type ArrayOrSliceValue interface { 923 type arrayOrSliceValue interface {
426 » Value 924 » valueInterface
427 Len() int 925 Len() int
428 Cap() int 926 Cap() int
429 Elem(i int) Value 927 Elem(i int) Value
430 addr() addr 928 addr() addr
431 } 929 }
432 930
433 // grow grows the slice s so that it can hold extra more values, allocating 931 // grow grows the slice s so that it can hold extra more values, allocating
434 // more capacity if needed. It also returns the old and new slice lengths. 932 // more capacity if needed. It also returns the old and new slice lengths.
435 func grow(s *SliceValue, extra int) (*SliceValue, int, int) { 933 func grow(s Value, extra int) (Value, int, int) {
436 i0 := s.Len() 934 i0 := s.Len()
437 i1 := i0 + extra 935 i1 := i0 + extra
438 if i1 < i0 { 936 if i1 < i0 {
439 panic("append: slice overflow") 937 panic("append: slice overflow")
440 } 938 }
441 m := s.Cap() 939 m := s.Cap()
442 if i1 <= m { 940 if i1 <= m {
443 return s.Slice(0, i1), i0, i1 941 return s.Slice(0, i1), i0, i1
444 } 942 }
445 if m == 0 { 943 if m == 0 {
446 m = extra 944 m = extra
447 } else { 945 } else {
448 for m < i1 { 946 for m < i1 {
449 if i0 < 1024 { 947 if i0 < 1024 {
450 m += m 948 m += m
451 } else { 949 } else {
452 m += m / 4 950 m += m / 4
453 } 951 }
454 } 952 }
455 } 953 }
456 » t := MakeSlice(s.Type().(*SliceType), i1, m) 954 » t := MakeSlice(s.Type(), i1, m)
457 Copy(t, s) 955 Copy(t, s)
458 return t, i0, i1 956 return t, i0, i1
459 } 957 }
460 958
461 // Append appends the values x to a slice s and returns the resulting slice. 959 // Append appends the values x to a slice s and returns the resulting slice.
462 // Each x must have the same type as s' element type. 960 // Each x must have the same type as s' element type.
463 func Append(s *SliceValue, x ...Value) *SliceValue { 961 func Append(s Value, x ...Value) Value {
464 s, i0, i1 := grow(s, len(x)) 962 s, i0, i1 := grow(s, len(x))
963 sa := s.kindPanic(Slice).(*sliceValue)
465 for i, j := i0, 0; i < i1; i, j = i+1, j+1 { 964 for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
466 » » s.Elem(i).SetValue(x[j]) 965 » » sa.Elem(i).Set(x[j])
467 } 966 }
468 return s 967 return s
469 } 968 }
470 969
471 // AppendSlice appends a slice t to a slice s and returns the resulting slice. 970 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
472 // The slices s and t must have the same element type. 971 // The slices s and t must have the same element type.
473 func AppendSlice(s, t *SliceValue) *SliceValue { 972 func AppendSlice(s, t Value) Value {
474 s, i0, i1 := grow(s, t.Len()) 973 s, i0, i1 := grow(s, t.Len())
475 Copy(s.Slice(i0, i1), t) 974 Copy(s.Slice(i0, i1), t)
476 return s 975 return s
477 } 976 }
478 977
479 // Copy copies the contents of src into dst until either 978 // Copy copies the contents of src into dst until either
480 // dst has been filled or src has been exhausted. 979 // dst has been filled or src has been exhausted.
481 // It returns the number of elements copied. 980 // It returns the number of elements copied.
482 // The arrays dst and src must have the same element type. 981 // Dst and src each must be a slice or array, and they
483 func Copy(dst, src ArrayOrSliceValue) int { 982 // must have the same element type.
983 func Copy(dst, src Value) int {
484 // TODO: This will have to move into the runtime 984 // TODO: This will have to move into the runtime
485 // once the real gc goes in. 985 // once the real gc goes in.
486 » de := dst.Type().(ArrayOrSliceType).Elem() 986 » de := dst.Type().Elem()
487 » se := src.Type().(ArrayOrSliceType).Elem() 987 » se := src.Type().Elem()
488 typesMustMatch(de, se) 988 typesMustMatch(de, se)
489 n := dst.Len() 989 n := dst.Len()
490 if xn := src.Len(); n > xn { 990 if xn := src.Len(); n > xn {
491 n = xn 991 n = xn
492 } 992 }
493 » memmove(dst.addr(), src.addr(), uintptr(n)*de.Size()) 993 » memmove(dst.kindsPanic(arrayOrSlice).(arrayOrSliceValue).addr(),
994 » » src.kindsPanic(arrayOrSlice).(arrayOrSliceValue).addr(),
995 » » uintptr(n)*de.Size())
494 return n 996 return n
495 } 997 }
496 998
497 // An ArrayValue represents an array. 999 // An arrayValue represents an array.
498 type ArrayValue struct { 1000 type arrayValue struct {
499 value "array" 1001 value "array"
500 } 1002 }
501 1003
502 // Len returns the length of the array. 1004 // Len returns the length of the array.
503 func (v *ArrayValue) Len() int { return v.typ.(*ArrayType).Len() } 1005 func (v *arrayValue) Len() int { return v.typ.Len() }
504 1006
505 // Cap returns the capacity of the array (equal to Len()). 1007 // Cap returns the capacity of the array (equal to Len()).
506 func (v *ArrayValue) Cap() int { return v.typ.(*ArrayType).Len() } 1008 func (v *arrayValue) Cap() int { return v.typ.Len() }
507 1009
508 // addr returns the base address of the data in the array. 1010 // addr returns the base address of the data in the array.
509 func (v *ArrayValue) addr() addr { return v.value.addr } 1011 func (v *arrayValue) addr() addr { return v.value.addr }
510 1012
511 // Set assigns x to v. 1013 // Set assigns x to v.
512 // The new value x must have the same type as v. 1014 // The new value x must have the same type as v.
513 func (v *ArrayValue) Set(x *ArrayValue) { 1015 func (v *arrayValue) Set(x *arrayValue) {
514 if !v.CanSet() { 1016 if !v.CanSet() {
515 panic(cannotSet) 1017 panic(cannotSet)
516 } 1018 }
517 typesMustMatch(v.typ, x.typ) 1019 typesMustMatch(v.typ, x.typ)
518 » Copy(v, x) 1020 » Copy(Value{v}, Value{x})
519 } 1021 }
520 1022
521 // Set sets v to the value x. 1023 // Set sets v to the value x.
522 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) } 1024 func (v *arrayValue) SetValue(x Value) {
1025 » v.Set(x.kindPanic(Array).(*arrayValue))
1026 }
523 1027
524 // Elem returns the i'th element of v. 1028 // Elem returns the i'th element of v.
525 func (v *ArrayValue) Elem(i int) Value { 1029 func (v *arrayValue) Elem(i int) Value {
526 » typ := v.typ.(*ArrayType).Elem() 1030 » typ := v.typ.Elem()
527 n := v.Len() 1031 n := v.Len()
528 if i < 0 || i >= n { 1032 if i < 0 || i >= n {
529 panic("array index out of bounds") 1033 panic("array index out of bounds")
530 } 1034 }
531 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) 1035 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
532 return newValue(typ, p, v.flag) 1036 return newValue(typ, p, v.flag)
533 } 1037 }
534 1038
535 /* 1039 /*
536 * slice 1040 * slice
537 */ 1041 */
538 1042
539 // runtime representation of slice 1043 // runtime representation of slice
540 type SliceHeader struct { 1044 type SliceHeader struct {
541 Data uintptr 1045 Data uintptr
542 Len int 1046 Len int
543 Cap int 1047 Cap int
544 } 1048 }
545 1049
546 // A SliceValue represents a slice. 1050 // A sliceValue represents a slice.
547 type SliceValue struct { 1051 type sliceValue struct {
548 value "slice" 1052 value "slice"
549 } 1053 }
550 1054
551 func (v *SliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr) } 1055 func (v *sliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr) }
552 1056
553 // IsNil returns whether v is a nil slice. 1057 // IsNil returns whether v is a nil slice.
554 func (v *SliceValue) IsNil() bool { return v.slice().Data == 0 } 1058 func (v *sliceValue) IsNil() bool { return v.slice().Data == 0 }
555 1059
556 // Len returns the length of the slice. 1060 // Len returns the length of the slice.
557 func (v *SliceValue) Len() int { return int(v.slice().Len) } 1061 func (v *sliceValue) Len() int { return int(v.slice().Len) }
558 1062
559 // Cap returns the capacity of the slice. 1063 // Cap returns the capacity of the slice.
560 func (v *SliceValue) Cap() int { return int(v.slice().Cap) } 1064 func (v *sliceValue) Cap() int { return int(v.slice().Cap) }
561 1065
562 // addr returns the base address of the data in the slice. 1066 // addr returns the base address of the data in the slice.
563 func (v *SliceValue) addr() addr { return addr(v.slice().Data) } 1067 func (v *sliceValue) addr() addr { return addr(v.slice().Data) }
564 1068
565 // SetLen changes the length of v. 1069 // SetLen changes the length of v.
566 // The new length n must be between 0 and the capacity, inclusive. 1070 // The new length n must be between 0 and the capacity, inclusive.
567 func (v *SliceValue) SetLen(n int) { 1071 func (v *sliceValue) SetLen(n int) {
568 s := v.slice() 1072 s := v.slice()
569 if n < 0 || n > int(s.Cap) { 1073 if n < 0 || n > int(s.Cap) {
570 panic("reflect: slice length out of range in SetLen") 1074 panic("reflect: slice length out of range in SetLen")
571 } 1075 }
572 s.Len = n 1076 s.Len = n
573 } 1077 }
574 1078
575 // Set assigns x to v. 1079 // Set assigns x to v.
576 // The new value x must have the same type as v. 1080 // The new value x must have the same type as v.
577 func (v *SliceValue) Set(x *SliceValue) { 1081 func (v *sliceValue) Set(x *sliceValue) {
578 if !v.CanSet() { 1082 if !v.CanSet() {
579 panic(cannotSet) 1083 panic(cannotSet)
580 } 1084 }
581 typesMustMatch(v.typ, x.typ) 1085 typesMustMatch(v.typ, x.typ)
582 *v.slice() = *x.slice() 1086 *v.slice() = *x.slice()
583 } 1087 }
584 1088
585 // Set sets v to the value x. 1089 // Set sets v to the value x.
586 func (v *SliceValue) SetValue(x Value) { v.Set(x.(*SliceValue)) } 1090 func (v *sliceValue) SetValue(x Value) {
1091 » v.Set(x.kindPanic(Slice).(*sliceValue))
1092 }
587 1093
588 // Get returns the uintptr address of the v.Cap()'th element. This gives 1094 // Get returns the uintptr address of the v.Cap()'th element. This gives
589 // the same result for all slices of the same array. 1095 // the same result for all slices of the same array.
590 // It is mainly useful for printing. 1096 // It is mainly useful for printing.
591 func (v *SliceValue) Get() uintptr { 1097 func (v *sliceValue) Get() uintptr {
592 » typ := v.typ.(*SliceType) 1098 » typ := v.typ
593 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size() 1099 return uintptr(v.addr()) + uintptr(v.Cap())*typ.Elem().Size()
594 } 1100 }
595 1101
596 // Slice returns a sub-slice of the slice v. 1102 // Slice returns a sub-slice of the slice v.
597 func (v *SliceValue) Slice(beg, end int) *SliceValue { 1103 func (v *sliceValue) Slice(beg, end int) Value {
598 cap := v.Cap() 1104 cap := v.Cap()
599 if beg < 0 || end < beg || end > cap { 1105 if beg < 0 || end < beg || end > cap {
600 panic("slice index out of bounds") 1106 panic("slice index out of bounds")
601 } 1107 }
602 » typ := v.typ.(*SliceType) 1108 » typ := v.typ
603 s := new(SliceHeader) 1109 s := new(SliceHeader)
604 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size() 1110 s.Data = uintptr(v.addr()) + uintptr(beg)*typ.Elem().Size()
605 s.Len = end - beg 1111 s.Len = end - beg
606 s.Cap = cap - beg 1112 s.Cap = cap - beg
607 1113
608 // Like the result of Addr, we treat Slice as an 1114 // Like the result of Addr, we treat Slice as an
609 // unaddressable temporary, so don't set canAddr. 1115 // unaddressable temporary, so don't set canAddr.
610 flag := canSet 1116 flag := canSet
611 if v.flag&canStore != 0 { 1117 if v.flag&canStore != 0 {
612 flag |= canStore 1118 flag |= canStore
613 } 1119 }
614 » return newValue(typ, addr(s), flag).(*SliceValue) 1120 » return newValue(typ, addr(s), flag)
615 } 1121 }
616 1122
617 // Elem returns the i'th element of v. 1123 // Elem returns the i'th element of v.
618 func (v *SliceValue) Elem(i int) Value { 1124 func (v *sliceValue) Elem(i int) Value {
619 » typ := v.typ.(*SliceType).Elem() 1125 » typ := v.typ.Elem()
620 n := v.Len() 1126 n := v.Len()
621 if i < 0 || i >= n { 1127 if i < 0 || i >= n {
622 panic("reflect: slice index out of range") 1128 panic("reflect: slice index out of range")
623 } 1129 }
624 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size()) 1130 p := addr(uintptr(v.addr()) + uintptr(i)*typ.Size())
625 flag := canAddr 1131 flag := canAddr
626 if v.flag&canStore != 0 { 1132 if v.flag&canStore != 0 {
627 flag |= canSet | canStore 1133 flag |= canSet | canStore
628 } 1134 }
629 return newValue(typ, p, flag) 1135 return newValue(typ, p, flag)
630 } 1136 }
631 1137
632 // MakeSlice creates a new zero-initialized slice value 1138 // MakeSlice creates a new zero-initialized slice value
633 // for the specified slice type, length, and capacity. 1139 // for the specified slice type, length, and capacity.
634 func MakeSlice(typ *SliceType, len, cap int) *SliceValue { 1140 func MakeSlice(typ Type, len, cap int) Value {
1141 » if typ.Kind() != Slice {
1142 » » panic("reflect: MakeSlice of non-slice type")
1143 » }
635 s := &SliceHeader{ 1144 s := &SliceHeader{
636 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)), 1145 Data: uintptr(unsafe.NewArray(typ.Elem(), cap)),
637 Len: len, 1146 Len: len,
638 Cap: cap, 1147 Cap: cap,
639 } 1148 }
640 » return newValue(typ, addr(s), canAddr|canSet|canStore).(*SliceValue) 1149 » return newValue(typ, addr(s), canAddr|canSet|canStore)
641 } 1150 }
642 1151
643 /* 1152 /*
644 * chan 1153 * chan
645 */ 1154 */
646 1155
647 // A ChanValue represents a chan. 1156 // A chanValue represents a chan.
648 type ChanValue struct { 1157 type chanValue struct {
649 value "chan" 1158 value "chan"
650 } 1159 }
651 1160
652 // IsNil returns whether v is a nil channel. 1161 // IsNil returns whether v is a nil channel.
653 func (v *ChanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } 1162 func (v *chanValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
654 1163
655 // Set assigns x to v. 1164 // Set assigns x to v.
656 // The new value x must have the same type as v. 1165 // The new value x must have the same type as v.
657 func (v *ChanValue) Set(x *ChanValue) { 1166 func (v *chanValue) Set(x *chanValue) {
658 if !v.CanSet() { 1167 if !v.CanSet() {
659 panic(cannotSet) 1168 panic(cannotSet)
660 } 1169 }
661 typesMustMatch(v.typ, x.typ) 1170 typesMustMatch(v.typ, x.typ)
662 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) 1171 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
663 } 1172 }
664 1173
665 // Set sets v to the value x. 1174 // Set sets v to the value x.
666 func (v *ChanValue) SetValue(x Value) { v.Set(x.(*ChanValue)) } 1175 func (v *chanValue) SetValue(x Value) {
1176 » v.Set(x.kindPanic(Chan).(*chanValue))
1177 }
667 1178
668 // Get returns the uintptr value of v. 1179 // Get returns the uintptr value of v.
669 // It is mainly useful for printing. 1180 // It is mainly useful for printing.
670 func (v *ChanValue) Get() uintptr { return *(*uintptr)(v.addr) } 1181 func (v *chanValue) Get() uintptr { return *(*uintptr)(v.addr) }
671 1182
672 // implemented in ../pkg/runtime/reflect.cgo 1183 // implemented in ../pkg/runtime/reflect.cgo
673 func makechan(typ *runtime.ChanType, size uint32) (ch *byte) 1184 func makechan(typ *runtime.ChanType, size uint32) (ch *byte)
674 func chansend(ch, val *byte, selected *bool) 1185 func chansend(ch, val *byte, selected *bool)
675 func chanrecv(ch, val *byte, selected *bool, ok *bool) 1186 func chanrecv(ch, val *byte, selected *bool, ok *bool)
676 func chanclose(ch *byte) 1187 func chanclose(ch *byte)
677 func chanlen(ch *byte) int32 1188 func chanlen(ch *byte) int32
678 func chancap(ch *byte) int32 1189 func chancap(ch *byte) int32
679 1190
680 // Close closes the channel. 1191 // Close closes the channel.
681 func (v *ChanValue) Close() { 1192 func (v *chanValue) Close() {
682 ch := *(**byte)(v.addr) 1193 ch := *(**byte)(v.addr)
683 chanclose(ch) 1194 chanclose(ch)
684 } 1195 }
685 1196
686 func (v *ChanValue) Len() int { 1197 func (v *chanValue) Len() int {
687 ch := *(**byte)(v.addr) 1198 ch := *(**byte)(v.addr)
688 return int(chanlen(ch)) 1199 return int(chanlen(ch))
689 } 1200 }
690 1201
691 func (v *ChanValue) Cap() int { 1202 func (v *chanValue) Cap() int {
692 ch := *(**byte)(v.addr) 1203 ch := *(**byte)(v.addr)
693 return int(chancap(ch)) 1204 return int(chancap(ch))
694 } 1205 }
695 1206
696 // internal send; non-blocking if selected != nil 1207 // internal send; non-blocking if selected != nil
697 func (v *ChanValue) send(x Value, selected *bool) { 1208 func (v *chanValue) send(x Value, selected *bool) {
698 » t := v.Type().(*ChanType) 1209 » t := v.Type()
699 » if t.Dir()&SendDir == 0 { 1210 » if t.ChanDir()&SendDir == 0 {
700 panic("send on recv-only channel") 1211 panic("send on recv-only channel")
701 } 1212 }
702 typesMustMatch(t.Elem(), x.Type()) 1213 typesMustMatch(t.Elem(), x.Type())
703 ch := *(**byte)(v.addr) 1214 ch := *(**byte)(v.addr)
704 » chansend(ch, (*byte)(x.getAddr()), selected) 1215 » chansend(ch, (*byte)(x.internal().getAddr()), selected)
705 } 1216 }
706 1217
707 // internal recv; non-blocking if selected != nil 1218 // internal recv; non-blocking if selected != nil
708 func (v *ChanValue) recv(selected *bool) (Value, bool) { 1219 func (v *chanValue) recv(selected *bool) (Value, bool) {
709 » t := v.Type().(*ChanType) 1220 » t := v.Type()
710 » if t.Dir()&RecvDir == 0 { 1221 » if t.ChanDir()&RecvDir == 0 {
711 panic("recv on send-only channel") 1222 panic("recv on send-only channel")
712 } 1223 }
713 ch := *(**byte)(v.addr) 1224 ch := *(**byte)(v.addr)
714 x := MakeZero(t.Elem()) 1225 x := MakeZero(t.Elem())
715 var ok bool 1226 var ok bool
716 » chanrecv(ch, (*byte)(x.getAddr()), selected, &ok) 1227 » chanrecv(ch, (*byte)(x.internal().getAddr()), selected, &ok)
717 return x, ok 1228 return x, ok
718 } 1229 }
719 1230
720 // Send sends x on the channel v. 1231 // Send sends x on the channel v.
721 func (v *ChanValue) Send(x Value) { v.send(x, nil) } 1232 func (v *chanValue) Send(x Value) { v.send(x, nil) }
722 1233
723 // Recv receives and returns a value from the channel v. 1234 // Recv receives and returns a value from the channel v.
724 // The receive blocks until a value is ready. 1235 // The receive blocks until a value is ready.
725 // The boolean value ok is true if the value x corresponds to a send 1236 // The boolean value ok is true if the value x corresponds to a send
726 // on the channel, false if it is a zero value received because the channel is c losed. 1237 // on the channel, false if it is a zero value received because the channel is c losed.
727 func (v *ChanValue) Recv() (x Value, ok bool) { 1238 func (v *chanValue) Recv() (x Value, ok bool) {
728 return v.recv(nil) 1239 return v.recv(nil)
729 } 1240 }
730 1241
731 // TrySend attempts to sends x on the channel v but will not block. 1242 // TrySend attempts to sends x on the channel v but will not block.
732 // It returns true if the value was sent, false otherwise. 1243 // It returns true if the value was sent, false otherwise.
733 func (v *ChanValue) TrySend(x Value) bool { 1244 func (v *chanValue) TrySend(x Value) bool {
734 var selected bool 1245 var selected bool
735 v.send(x, &selected) 1246 v.send(x, &selected)
736 return selected 1247 return selected
737 } 1248 }
738 1249
739 // TryRecv attempts to receive a value from the channel v but will not block. 1250 // TryRecv attempts to receive a value from the channel v but will not block.
740 // If the receive cannot finish without blocking, TryRecv instead returns x == n il. 1251 // If the receive cannot finish without blocking, TryRecv instead returns x == n il.
741 // If the receive can finish without blocking, TryRecv returns x != nil. 1252 // If the receive can finish without blocking, TryRecv returns x != nil.
742 // The boolean value ok is true if the value x corresponds to a send 1253 // The boolean value ok is true if the value x corresponds to a send
743 // on the channel, false if it is a zero value received because the channel is c losed. 1254 // on the channel, false if it is a zero value received because the channel is c losed.
744 func (v *ChanValue) TryRecv() (x Value, ok bool) { 1255 func (v *chanValue) TryRecv() (x Value, ok bool) {
745 var selected bool 1256 var selected bool
746 x, ok = v.recv(&selected) 1257 x, ok = v.recv(&selected)
747 if !selected { 1258 if !selected {
748 » » return nil, false 1259 » » return Value{}, false
749 } 1260 }
750 return x, ok 1261 return x, ok
751 } 1262 }
752 1263
753 // MakeChan creates a new channel with the specified type and buffer size. 1264 // MakeChan creates a new channel with the specified type and buffer size.
754 func MakeChan(typ *ChanType, buffer int) *ChanValue { 1265 func MakeChan(typ Type, buffer int) Value {
1266 » if typ.Kind() != Chan {
1267 » » panic("reflect: MakeChan of non-chan type")
1268 » }
755 if buffer < 0 { 1269 if buffer < 0 {
756 panic("MakeChan: negative buffer size") 1270 panic("MakeChan: negative buffer size")
757 } 1271 }
758 » if typ.Dir() != BothDir { 1272 » if typ.ChanDir() != BothDir {
759 panic("MakeChan: unidirectional channel type") 1273 panic("MakeChan: unidirectional channel type")
760 } 1274 }
761 » v := MakeZero(typ).(*ChanValue) 1275 » v := MakeZero(typ)
762 » *(**byte)(v.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ)), u int32(buffer)) 1276 » ch := v.kindPanic(Chan).(*chanValue)
1277 » *(**byte)(ch.addr) = makechan((*runtime.ChanType)(unsafe.Pointer(typ.(*c ommonType))), uint32(buffer))
763 return v 1278 return v
764 } 1279 }
765 1280
766 /* 1281 /*
767 * func 1282 * func
768 */ 1283 */
769 1284
770 // A FuncValue represents a function value. 1285 // A funcValue represents a function value.
771 type FuncValue struct { 1286 type funcValue struct {
772 value "func" 1287 value "func"
773 first *value 1288 first *value
774 isInterface bool 1289 isInterface bool
775 } 1290 }
776 1291
777 // IsNil returns whether v is a nil function. 1292 // IsNil returns whether v is a nil function.
778 func (v *FuncValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } 1293 func (v *funcValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
779 1294
780 // Get returns the uintptr value of v. 1295 // Get returns the uintptr value of v.
781 // It is mainly useful for printing. 1296 // It is mainly useful for printing.
782 func (v *FuncValue) Get() uintptr { return *(*uintptr)(v.addr) } 1297 func (v *funcValue) Get() uintptr { return *(*uintptr)(v.addr) }
783 1298
784 // Set assigns x to v. 1299 // Set assigns x to v.
785 // The new value x must have the same type as v. 1300 // The new value x must have the same type as v.
786 func (v *FuncValue) Set(x *FuncValue) { 1301 func (v *funcValue) Set(x *funcValue) {
787 if !v.CanSet() { 1302 if !v.CanSet() {
788 panic(cannotSet) 1303 panic(cannotSet)
789 } 1304 }
790 typesMustMatch(v.typ, x.typ) 1305 typesMustMatch(v.typ, x.typ)
791 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) 1306 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
792 } 1307 }
793 1308
794 // Set sets v to the value x. 1309 // Set sets v to the value x.
795 func (v *FuncValue) SetValue(x Value) { v.Set(x.(*FuncValue)) } 1310 func (v *funcValue) SetValue(x Value) {
1311 » v.Set(x.kindPanic(Func).(*funcValue))
1312 }
796 1313
797 // Method returns a FuncValue corresponding to v's i'th method. 1314 // Method returns a funcValue corresponding to v's i'th method.
798 // The arguments to a Call on the returned FuncValue 1315 // The arguments to a Call on the returned funcValue
799 // should not include a receiver; the FuncValue will use v 1316 // should not include a receiver; the funcValue will use v
800 // as the receiver. 1317 // as the receiver.
801 func (v *value) Method(i int) *FuncValue { 1318 func (v *value) Method(i int) Value {
802 t := v.Type().uncommon() 1319 t := v.Type().uncommon()
803 if t == nil || i < 0 || i >= len(t.methods) { 1320 if t == nil || i < 0 || i >= len(t.methods) {
804 » » return nil 1321 » » panic("reflect: Method index out of range")
805 } 1322 }
806 p := &t.methods[i] 1323 p := &t.methods[i]
807 fn := p.tfn 1324 fn := p.tfn
808 » fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: v, i sInterface: false} 1325 » fv := &funcValue{value: value{toType(p.typ), addr(&fn), 0}, first: v, is Interface: false}
809 » return fv 1326 » return Value{fv}
810 } 1327 }
811 1328
812 // implemented in ../pkg/runtime/*/asm.s 1329 // implemented in ../pkg/runtime/*/asm.s
813 func call(fn, arg *byte, n uint32) 1330 func call(fn, arg *byte, n uint32)
814 1331
815 type tiny struct { 1332 type tiny struct {
816 b byte 1333 b byte
817 } 1334 }
818 1335
819 // Interface returns the fv as an interface value. 1336 // Interface returns the fv as an interface value.
820 // If fv is a method obtained by invoking Value.Method 1337 // If fv is a method obtained by invoking Value.Method
821 // (as opposed to Type.Method), Interface cannot return an 1338 // (as opposed to Type.Method), Interface cannot return an
822 // interface value, so it panics. 1339 // interface value, so it panics.
823 func (fv *FuncValue) Interface() interface{} { 1340 func (fv *funcValue) Interface() interface{} {
824 if fv.first != nil { 1341 if fv.first != nil {
825 » » panic("FuncValue: cannot create interface value for method with bound receiver") 1342 » » panic("funcValue: cannot create interface value for method with bound receiver")
826 } 1343 }
827 return fv.value.Interface() 1344 return fv.value.Interface()
828 } 1345 }
829 1346
830 // Call calls the function fv with input parameters in. 1347 // Call calls the function fv with input parameters in.
831 // It returns the function's output parameters as Values. 1348 // It returns the function's output parameters as Values.
832 func (fv *FuncValue) Call(in []Value) []Value { 1349 func (fv *funcValue) Call(in []Value) []Value {
833 » t := fv.Type().(*FuncType) 1350 » t := fv.Type()
834 nin := len(in) 1351 nin := len(in)
835 if fv.first != nil && !fv.isInterface { 1352 if fv.first != nil && !fv.isInterface {
836 nin++ 1353 nin++
837 } 1354 }
838 if nin != t.NumIn() { 1355 if nin != t.NumIn() {
839 » » panic("FuncValue: wrong argument count") 1356 » » panic("funcValue: wrong argument count")
840 } 1357 }
841 nout := t.NumOut() 1358 nout := t.NumOut()
842 1359
843 // Compute arg size & allocate. 1360 // Compute arg size & allocate.
844 // This computation is 6g/8g-dependent 1361 // This computation is 6g/8g-dependent
845 // and probably wrong for gccgo, but so 1362 // and probably wrong for gccgo, but so
846 // is most of this function. 1363 // is most of this function.
847 size := uintptr(0) 1364 size := uintptr(0)
848 if fv.isInterface { 1365 if fv.isInterface {
849 // extra word for interface value 1366 // extra word for interface value
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
899 off = n 1416 off = n
900 delta = 1 1417 delta = 1
901 } 1418 }
902 } 1419 }
903 for i, v := range in { 1420 for i, v := range in {
904 tv := v.Type() 1421 tv := v.Type()
905 typesMustMatch(t.In(i+delta), tv) 1422 typesMustMatch(t.In(i+delta), tv)
906 a := uintptr(tv.Align()) 1423 a := uintptr(tv.Align())
907 off = (off + a - 1) &^ (a - 1) 1424 off = (off + a - 1) &^ (a - 1)
908 n := tv.Size() 1425 n := tv.Size()
909 » » memmove(addr(ptr+off), v.getAddr(), n) 1426 » » memmove(addr(ptr+off), v.internal().getAddr(), n)
910 off += n 1427 off += n
911 } 1428 }
912 off = (off + ptrSize - 1) &^ (ptrSize - 1) 1429 off = (off + ptrSize - 1) &^ (ptrSize - 1)
913 1430
914 // Call 1431 // Call
915 call(*(**byte)(fv.addr), (*byte)(addr(ptr)), uint32(size)) 1432 call(*(**byte)(fv.addr), (*byte)(addr(ptr)), uint32(size))
916 1433
917 // Copy return values out of args. 1434 // Copy return values out of args.
918 // 1435 //
919 // TODO(rsc): revisit like above. 1436 // TODO(rsc): revisit like above.
920 ret := make([]Value, nout) 1437 ret := make([]Value, nout)
921 for i := 0; i < nout; i++ { 1438 for i := 0; i < nout; i++ {
922 tv := t.Out(i) 1439 tv := t.Out(i)
923 a := uintptr(tv.Align()) 1440 a := uintptr(tv.Align())
924 off = (off + a - 1) &^ (a - 1) 1441 off = (off + a - 1) &^ (a - 1)
925 v := MakeZero(tv) 1442 v := MakeZero(tv)
926 n := tv.Size() 1443 n := tv.Size()
927 » » memmove(v.getAddr(), addr(ptr+off), n) 1444 » » memmove(v.internal().getAddr(), addr(ptr+off), n)
928 ret[i] = v 1445 ret[i] = v
929 off += n 1446 off += n
930 } 1447 }
931 1448
932 return ret 1449 return ret
933 } 1450 }
934 1451
935 /* 1452 /*
936 * interface 1453 * interface
937 */ 1454 */
(...skipping 11 matching lines...) Expand all
949 // Get returns the two words that represent an interface in the runtime. 1466 // Get returns the two words that represent an interface in the runtime.
950 // Those words are useful only when playing unsafe games. 1467 // Those words are useful only when playing unsafe games.
951 func (v *InterfaceValue) Get() [2]uintptr { 1468 func (v *InterfaceValue) Get() [2]uintptr {
952 return *(*[2]uintptr)(v.addr) 1469 return *(*[2]uintptr)(v.addr)
953 } 1470 }
954 1471
955 // Elem returns the concrete value stored in the interface value v. 1472 // Elem returns the concrete value stored in the interface value v.
956 func (v *InterfaceValue) Elem() Value { return NewValue(v.Interface()) } 1473 func (v *InterfaceValue) Elem() Value { return NewValue(v.Interface()) }
957 1474
958 // ../runtime/reflect.cgo 1475 // ../runtime/reflect.cgo
959 func setiface(typ *InterfaceType, x *interface{}, addr addr) 1476 func setiface(typ *interfaceType, x *interface{}, addr addr)
960 1477
961 // Set assigns x to v. 1478 // Set assigns x to v.
962 func (v *InterfaceValue) Set(x Value) { 1479 func (v *InterfaceValue) Set(x Value) {
963 var i interface{} 1480 var i interface{}
964 » if x != nil { 1481 » if x.IsValid() {
965 i = x.Interface() 1482 i = x.Interface()
966 } 1483 }
967 if !v.CanSet() { 1484 if !v.CanSet() {
968 panic(cannotSet) 1485 panic(cannotSet)
969 } 1486 }
970 // Two different representations; see comment in Get. 1487 // Two different representations; see comment in Get.
971 // Empty interface is easy. 1488 // Empty interface is easy.
972 » t := v.typ.(*InterfaceType) 1489 » t := (*interfaceType)(unsafe.Pointer(v.typ.(*commonType)))
973 if t.NumMethod() == 0 { 1490 if t.NumMethod() == 0 {
974 *(*interface{})(v.addr) = i 1491 *(*interface{})(v.addr) = i
975 return 1492 return
976 } 1493 }
977 1494
978 // Non-empty interface requires a runtime check. 1495 // Non-empty interface requires a runtime check.
979 setiface(t, &i, v.addr) 1496 setiface(t, &i, v.addr)
980 } 1497 }
981 1498
982 // Set sets v to the value x. 1499 // Set sets v to the value x.
983 func (v *InterfaceValue) SetValue(x Value) { v.Set(x) } 1500 func (v *InterfaceValue) SetValue(x Value) { v.Set(x) }
984 1501
985 // Method returns a FuncValue corresponding to v's i'th method. 1502 // Method returns a funcValue corresponding to v's i'th method.
986 // The arguments to a Call on the returned FuncValue 1503 // The arguments to a Call on the returned funcValue
987 // should not include a receiver; the FuncValue will use v 1504 // should not include a receiver; the funcValue will use v
988 // as the receiver. 1505 // as the receiver.
989 func (v *InterfaceValue) Method(i int) *FuncValue { 1506 func (v *InterfaceValue) Method(i int) Value {
990 » t := v.Type().(*InterfaceType) 1507 » t := (*interfaceType)(unsafe.Pointer(v.Type().(*commonType)))
991 if t == nil || i < 0 || i >= len(t.methods) { 1508 if t == nil || i < 0 || i >= len(t.methods) {
992 » » return nil 1509 » » panic("reflect: Method index out of range")
993 } 1510 }
994 p := &t.methods[i] 1511 p := &t.methods[i]
995 1512
996 // Interface is two words: itable, data. 1513 // Interface is two words: itable, data.
997 tab := *(**runtime.Itable)(v.addr) 1514 tab := *(**runtime.Itable)(v.addr)
998 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), 0} 1515 data := &value{Typeof((*byte)(nil)), addr(uintptr(v.addr) + ptrSize), 0}
999 1516
1000 // Function pointer is at p.perm in the table. 1517 // Function pointer is at p.perm in the table.
1001 fn := tab.Fn[i] 1518 fn := tab.Fn[i]
1002 » fv := &FuncValue{value: value{toType(*p.typ), addr(&fn), 0}, first: data , isInterface: true} 1519 » fv := &funcValue{value: value{toType(p.typ), addr(&fn), 0}, first: data, isInterface: true}
1003 » return fv 1520 » return Value{fv}
1004 } 1521 }
1005 1522
1006 /* 1523 /*
1007 * map 1524 * map
1008 */ 1525 */
1009 1526
1010 // A MapValue represents a map value. 1527 // A mapValue represents a map value.
1011 type MapValue struct { 1528 type mapValue struct {
1012 value "map" 1529 value "map"
1013 } 1530 }
1014 1531
1015 // IsNil returns whether v is a nil map value. 1532 // IsNil returns whether v is a nil map value.
1016 func (v *MapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } 1533 func (v *mapValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
1017 1534
1018 // Set assigns x to v. 1535 // Set assigns x to v.
1019 // The new value x must have the same type as v. 1536 // The new value x must have the same type as v.
1020 func (v *MapValue) Set(x *MapValue) { 1537 func (v *mapValue) Set(x *mapValue) {
1021 if !v.CanSet() { 1538 if !v.CanSet() {
1022 panic(cannotSet) 1539 panic(cannotSet)
1023 } 1540 }
1024 if x == nil { 1541 if x == nil {
1025 *(**uintptr)(v.addr) = nil 1542 *(**uintptr)(v.addr) = nil
1026 return 1543 return
1027 } 1544 }
1028 typesMustMatch(v.typ, x.typ) 1545 typesMustMatch(v.typ, x.typ)
1029 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) 1546 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
1030 } 1547 }
1031 1548
1032 // Set sets v to the value x. 1549 // Set sets v to the value x.
1033 func (v *MapValue) SetValue(x Value) { 1550 func (v *mapValue) SetValue(x Value) {
1034 » if x == nil { 1551 » if !x.IsValid() {
1035 v.Set(nil) 1552 v.Set(nil)
1036 return 1553 return
1037 } 1554 }
1038 » v.Set(x.(*MapValue)) 1555 » v.Set(x.kindPanic(Map).(*mapValue))
1039 } 1556 }
1040 1557
1041 // Get returns the uintptr value of v. 1558 // Get returns the uintptr value of v.
1042 // It is mainly useful for printing. 1559 // It is mainly useful for printing.
1043 func (v *MapValue) Get() uintptr { return *(*uintptr)(v.addr) } 1560 func (v *mapValue) Get() uintptr { return *(*uintptr)(v.addr) }
1044 1561
1045 // implemented in ../pkg/runtime/reflect.cgo 1562 // implemented in ../pkg/runtime/reflect.cgo
1046 func mapaccess(m, key, val *byte) bool 1563 func mapaccess(m, key, val *byte) bool
1047 func mapassign(m, key, val *byte) 1564 func mapassign(m, key, val *byte)
1048 func maplen(m *byte) int32 1565 func maplen(m *byte) int32
1049 func mapiterinit(m *byte) *byte 1566 func mapiterinit(m *byte) *byte
1050 func mapiternext(it *byte) 1567 func mapiternext(it *byte)
1051 func mapiterkey(it *byte, key *byte) bool 1568 func mapiterkey(it *byte, key *byte) bool
1052 func makemap(t *runtime.MapType) *byte 1569 func makemap(t *runtime.MapType) *byte
1053 1570
1054 // Elem returns the value associated with key in the map v. 1571 // Elem returns the value associated with key in the map v.
1055 // It returns nil if key is not found in the map. 1572 // It returns nil if key is not found in the map.
1056 func (v *MapValue) Elem(key Value) Value { 1573 func (v *mapValue) Elem(key Value) Value {
1057 » t := v.Type().(*MapType) 1574 » t := v.Type()
1058 typesMustMatch(t.Key(), key.Type()) 1575 typesMustMatch(t.Key(), key.Type())
1059 m := *(**byte)(v.addr) 1576 m := *(**byte)(v.addr)
1060 if m == nil { 1577 if m == nil {
1061 » » return nil 1578 » » return Value{}
1062 } 1579 }
1063 newval := MakeZero(t.Elem()) 1580 newval := MakeZero(t.Elem())
1064 » if !mapaccess(m, (*byte)(key.getAddr()), (*byte)(newval.getAddr())) { 1581 » if !mapaccess(m, (*byte)(key.internal().getAddr()), (*byte)(newval.inter nal().getAddr())) {
1065 » » return nil 1582 » » return Value{}
1066 } 1583 }
1067 return newval 1584 return newval
1068 } 1585 }
1069 1586
1070 // SetElem sets the value associated with key in the map v to val. 1587 // SetElem sets the value associated with key in the map v to val.
1071 // If val is nil, Put deletes the key from map. 1588 // If val is nil, Put deletes the key from map.
1072 func (v *MapValue) SetElem(key, val Value) { 1589 func (v *mapValue) SetElem(key, val Value) {
1073 » t := v.Type().(*MapType) 1590 » t := v.Type()
1074 typesMustMatch(t.Key(), key.Type()) 1591 typesMustMatch(t.Key(), key.Type())
1075 var vaddr *byte 1592 var vaddr *byte
1076 » if val != nil { 1593 » if val.IsValid() {
1077 typesMustMatch(t.Elem(), val.Type()) 1594 typesMustMatch(t.Elem(), val.Type())
1078 » » vaddr = (*byte)(val.getAddr()) 1595 » » vaddr = (*byte)(val.internal().getAddr())
1079 } 1596 }
1080 m := *(**byte)(v.addr) 1597 m := *(**byte)(v.addr)
1081 » mapassign(m, (*byte)(key.getAddr()), vaddr) 1598 » mapassign(m, (*byte)(key.internal().getAddr()), vaddr)
1082 } 1599 }
1083 1600
1084 // Len returns the number of keys in the map v. 1601 // Len returns the number of keys in the map v.
1085 func (v *MapValue) Len() int { 1602 func (v *mapValue) Len() int {
1086 m := *(**byte)(v.addr) 1603 m := *(**byte)(v.addr)
1087 if m == nil { 1604 if m == nil {
1088 return 0 1605 return 0
1089 } 1606 }
1090 return int(maplen(m)) 1607 return int(maplen(m))
1091 } 1608 }
1092 1609
1093 // Keys returns a slice containing all the keys present in the map, 1610 // Keys returns a slice containing all the keys present in the map,
1094 // in unspecified order. 1611 // in unspecified order.
1095 func (v *MapValue) Keys() []Value { 1612 func (v *mapValue) Keys() []Value {
1096 » tk := v.Type().(*MapType).Key() 1613 » tk := v.Type().Key()
1097 m := *(**byte)(v.addr) 1614 m := *(**byte)(v.addr)
1098 mlen := int32(0) 1615 mlen := int32(0)
1099 if m != nil { 1616 if m != nil {
1100 mlen = maplen(m) 1617 mlen = maplen(m)
1101 } 1618 }
1102 it := mapiterinit(m) 1619 it := mapiterinit(m)
1103 a := make([]Value, mlen) 1620 a := make([]Value, mlen)
1104 var i int 1621 var i int
1105 for i = 0; i < len(a); i++ { 1622 for i = 0; i < len(a); i++ {
1106 k := MakeZero(tk) 1623 k := MakeZero(tk)
1107 » » if !mapiterkey(it, (*byte)(k.getAddr())) { 1624 » » if !mapiterkey(it, (*byte)(k.internal().getAddr())) {
1108 break 1625 break
1109 } 1626 }
1110 a[i] = k 1627 a[i] = k
1111 mapiternext(it) 1628 mapiternext(it)
1112 } 1629 }
1113 return a[0:i] 1630 return a[0:i]
1114 } 1631 }
1115 1632
1116 // MakeMap creates a new map of the specified type. 1633 // MakeMap creates a new map of the specified type.
1117 func MakeMap(typ *MapType) *MapValue { 1634 func MakeMap(typ Type) Value {
1118 » v := MakeZero(typ).(*MapValue) 1635 » if typ.Kind() != Map {
1119 » *(**byte)(v.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ))) 1636 » » panic("reflect: MakeMap of non-map type")
1637 » }
1638 » v := MakeZero(typ)
1639 » m := v.kindPanic(Map).(*mapValue)
1640 » *(**byte)(m.addr) = makemap((*runtime.MapType)(unsafe.Pointer(typ.(*comm onType))))
1120 return v 1641 return v
1121 } 1642 }
1122 1643
1123 /* 1644 /*
1124 * ptr 1645 * ptr
1125 */ 1646 */
1126 1647
1127 // A PtrValue represents a pointer. 1648 // A ptrValue represents a pointer.
1128 type PtrValue struct { 1649 type ptrValue struct {
1129 value "ptr" 1650 value "ptr"
1130 } 1651 }
1131 1652
1132 // IsNil returns whether v is a nil pointer. 1653 // IsNil returns whether v is a nil pointer.
1133 func (v *PtrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 } 1654 func (v *ptrValue) IsNil() bool { return *(*uintptr)(v.addr) == 0 }
1134 1655
1135 // Get returns the uintptr value of v. 1656 // Get returns the uintptr value of v.
1136 // It is mainly useful for printing. 1657 // It is mainly useful for printing.
1137 func (v *PtrValue) Get() uintptr { return *(*uintptr)(v.addr) } 1658 func (v *ptrValue) Get() uintptr { return *(*uintptr)(v.addr) }
1138 1659
1139 // Set assigns x to v. 1660 // Set assigns x to v.
1140 // The new value x must have the same type as v, and x.Elem().CanSet() must be t rue. 1661 // The new value x must have the same type as v, and x.Elem().CanSet() must be t rue.
1141 func (v *PtrValue) Set(x *PtrValue) { 1662 func (v *ptrValue) Set(x *ptrValue) {
1142 if x == nil { 1663 if x == nil {
1143 *(**uintptr)(v.addr) = nil 1664 *(**uintptr)(v.addr) = nil
1144 return 1665 return
1145 } 1666 }
1146 if !v.CanSet() { 1667 if !v.CanSet() {
1147 panic(cannotSet) 1668 panic(cannotSet)
1148 } 1669 }
1149 if x.flag&canStore == 0 { 1670 if x.flag&canStore == 0 {
1150 panic("cannot copy pointer obtained from unexported struct field ") 1671 panic("cannot copy pointer obtained from unexported struct field ")
1151 } 1672 }
1152 typesMustMatch(v.typ, x.typ) 1673 typesMustMatch(v.typ, x.typ)
1153 // TODO: This will have to move into the runtime 1674 // TODO: This will have to move into the runtime
1154 // once the new gc goes in 1675 // once the new gc goes in
1155 *(*uintptr)(v.addr) = *(*uintptr)(x.addr) 1676 *(*uintptr)(v.addr) = *(*uintptr)(x.addr)
1156 } 1677 }
1157 1678
1158 // Set sets v to the value x. 1679 // Set sets v to the value x.
1159 func (v *PtrValue) SetValue(x Value) { 1680 func (v *ptrValue) SetValue(x Value) {
1160 » if x == nil { 1681 » if !x.IsValid() {
1161 v.Set(nil) 1682 v.Set(nil)
1162 return 1683 return
1163 } 1684 }
1164 » v.Set(x.(*PtrValue)) 1685 » v.Set(x.kindPanic(Ptr).(*ptrValue))
1165 } 1686 }
1166 1687
1167 // PointTo changes v to point to x. 1688 // PointTo changes v to point to x.
1168 // If x is a nil Value, PointTo sets v to nil. 1689 // If x is a nil Value, PointTo sets v to nil.
1169 func (v *PtrValue) PointTo(x Value) { 1690 func (v *ptrValue) PointTo(x Value) {
1170 » if x == nil { 1691 » if !x.IsValid() {
1171 *(**uintptr)(v.addr) = nil 1692 *(**uintptr)(v.addr) = nil
1172 return 1693 return
1173 } 1694 }
1174 if !x.CanSet() { 1695 if !x.CanSet() {
1175 panic("cannot set x; cannot point to x") 1696 panic("cannot set x; cannot point to x")
1176 } 1697 }
1177 » typesMustMatch(v.typ.(*PtrType).Elem(), x.Type()) 1698 » typesMustMatch(v.typ.Elem(), x.Type())
1178 // TODO: This will have to move into the runtime 1699 // TODO: This will have to move into the runtime
1179 // once the new gc goes in. 1700 // once the new gc goes in.
1180 *(*uintptr)(v.addr) = x.UnsafeAddr() 1701 *(*uintptr)(v.addr) = x.UnsafeAddr()
1181 } 1702 }
1182 1703
1183 // Elem returns the value that v points to. 1704 // Elem returns the value that v points to.
1184 // If v is a nil pointer, Elem returns a nil Value. 1705 // If v is a nil pointer, Elem returns a nil Value.
1185 func (v *PtrValue) Elem() Value { 1706 func (v *ptrValue) Elem() Value {
1186 if v.IsNil() { 1707 if v.IsNil() {
1187 » » return nil 1708 » » return Value{}
1188 } 1709 }
1189 flag := canAddr 1710 flag := canAddr
1190 if v.flag&canStore != 0 { 1711 if v.flag&canStore != 0 {
1191 flag |= canSet | canStore 1712 flag |= canSet | canStore
1192 } 1713 }
1193 » return newValue(v.typ.(*PtrType).Elem(), *(*addr)(v.addr), flag) 1714 » return newValue(v.typ.Elem(), *(*addr)(v.addr), flag)
1194 } 1715 }
1195 1716
1196 // Indirect returns the value that v points to. 1717 // Indirect returns the value that v points to.
1197 // If v is a nil pointer, Indirect returns a nil Value. 1718 // If v is a nil pointer, Indirect returns a nil Value.
1198 // If v is not a pointer, Indirect returns v. 1719 // If v is not a pointer, Indirect returns v.
1199 func Indirect(v Value) Value { 1720 func Indirect(v Value) Value {
1200 » if pv, ok := v.(*PtrValue); ok { 1721 » if v.Kind() != Ptr {
1201 » » return pv.Elem() 1722 » » return v
1202 } 1723 }
1203 » return v 1724 » return v.kindPanic(Ptr).(*ptrValue).Elem()
1204 } 1725 }
1205 1726
1206 /* 1727 /*
1207 * struct 1728 * struct
1208 */ 1729 */
1209 1730
1210 // A StructValue represents a struct value. 1731 // A structValue represents a struct value.
1211 type StructValue struct { 1732 type structValue struct {
1212 value "struct" 1733 value "struct"
1213 } 1734 }
1214 1735
1215 // Set assigns x to v. 1736 // Set assigns x to v.
1216 // The new value x must have the same type as v. 1737 // The new value x must have the same type as v.
1217 func (v *StructValue) Set(x *StructValue) { 1738 func (v *structValue) Set(x *structValue) {
1218 // TODO: This will have to move into the runtime 1739 // TODO: This will have to move into the runtime
1219 // once the gc goes in. 1740 // once the gc goes in.
1220 if !v.CanSet() { 1741 if !v.CanSet() {
1221 panic(cannotSet) 1742 panic(cannotSet)
1222 } 1743 }
1223 typesMustMatch(v.typ, x.typ) 1744 typesMustMatch(v.typ, x.typ)
1224 memmove(v.addr, x.addr, v.typ.Size()) 1745 memmove(v.addr, x.addr, v.typ.Size())
1225 } 1746 }
1226 1747
1227 // Set sets v to the value x. 1748 // Set sets v to the value x.
1228 func (v *StructValue) SetValue(x Value) { v.Set(x.(*StructValue)) } 1749 func (v *structValue) SetValue(x Value) {
1750 » v.Set(x.kindPanic(Struct).(*structValue))
1751 }
1229 1752
1230 // Field returns the i'th field of the struct. 1753 // Field returns the i'th field of the struct.
1231 func (v *StructValue) Field(i int) Value { 1754 func (v *structValue) Field(i int) Value {
1232 » t := v.typ.(*StructType) 1755 » t := v.typ
1233 if i < 0 || i >= t.NumField() { 1756 if i < 0 || i >= t.NumField() {
1234 » » return nil 1757 » » panic("reflect: Field index out of range")
1235 } 1758 }
1236 f := t.Field(i) 1759 f := t.Field(i)
1237 flag := v.flag 1760 flag := v.flag
1238 if f.PkgPath != "" { 1761 if f.PkgPath != "" {
1239 // unexported field 1762 // unexported field
1240 flag &^= canSet | canStore 1763 flag &^= canSet | canStore
1241 } 1764 }
1242 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), flag) 1765 return newValue(f.Type, addr(uintptr(v.addr)+f.Offset), flag)
1243 } 1766 }
1244 1767
1245 // FieldByIndex returns the nested field corresponding to index. 1768 // FieldByIndex returns the nested field corresponding to index.
1246 func (t *StructValue) FieldByIndex(index []int) (v Value) { 1769 func (t *structValue) FieldByIndex(index []int) (v Value) {
1247 » v = t 1770 » v = Value{t}
1248 for i, x := range index { 1771 for i, x := range index {
1249 if i > 0 { 1772 if i > 0 {
1250 » » » if p, ok := v.(*PtrValue); ok { 1773 » » » if v.Kind() == Ptr {
1251 » » » » v = p.Elem() 1774 » » » » v = v.Elem()
1252 } 1775 }
1253 » » » if s, ok := v.(*StructValue); ok { 1776 » » » if v.Kind() != Struct {
1254 » » » » t = s 1777 » » » » return Value{}
1255 » » » } else {
1256 » » » » v = nil
1257 » » » » return
1258 } 1778 }
1259 } 1779 }
1260 » » v = t.Field(x) 1780 » » v = v.Field(x)
1261 } 1781 }
1262 return 1782 return
1263 } 1783 }
1264 1784
1265 // FieldByName returns the struct field with the given name. 1785 // FieldByName returns the struct field with the given name.
1266 // The result is nil if no field was found. 1786 // The result is nil if no field was found.
1267 func (t *StructValue) FieldByName(name string) Value { 1787 func (t *structValue) FieldByName(name string) Value {
1268 » if f, ok := t.Type().(*StructType).FieldByName(name); ok { 1788 » if f, ok := t.Type().FieldByName(name); ok {
1269 return t.FieldByIndex(f.Index) 1789 return t.FieldByIndex(f.Index)
1270 } 1790 }
1271 » return nil 1791 » return Value{}
1272 } 1792 }
1273 1793
1274 // FieldByNameFunc returns the struct field with a name that satisfies the 1794 // FieldByNameFunc returns the struct field with a name that satisfies the
1275 // match function. 1795 // match function.
1276 // The result is nil if no field was found. 1796 // The result is nil if no field was found.
1277 func (t *StructValue) FieldByNameFunc(match func(string) bool) Value { 1797 func (t *structValue) FieldByNameFunc(match func(string) bool) Value {
1278 » if f, ok := t.Type().(*StructType).FieldByNameFunc(match); ok { 1798 » if f, ok := t.Type().FieldByNameFunc(match); ok {
1279 return t.FieldByIndex(f.Index) 1799 return t.FieldByIndex(f.Index)
1280 } 1800 }
1281 » return nil 1801 » return Value{}
1282 } 1802 }
1283 1803
1284 // NumField returns the number of fields in the struct. 1804 // NumField returns the number of fields in the struct.
1285 func (v *StructValue) NumField() int { return v.typ.(*StructType).NumField() } 1805 func (v *structValue) NumField() int { return v.typ.NumField() }
1286 1806
1287 /* 1807 /*
1288 * constructors 1808 * constructors
1289 */ 1809 */
1290 1810
1291 // NewValue returns a new Value initialized to the concrete value 1811 // NewValue returns a new Value initialized to the concrete value
1292 // stored in the interface i. NewValue(nil) returns nil. 1812 // stored in the interface i. NewValue(nil) returns the zero Value.
1293 func NewValue(i interface{}) Value { 1813 func NewValue(i interface{}) Value {
1294 if i == nil { 1814 if i == nil {
1295 » » return nil 1815 » » return Value{}
1296 } 1816 }
1297 » t, a := unsafe.Reflect(i) 1817 » _, a := unsafe.Reflect(i)
1298 » return newValue(toType(t), addr(a), canSet|canAddr|canStore) 1818 » return newValue(Typeof(i), addr(a), canSet|canAddr|canStore)
1299 } 1819 }
1300 1820
1301 func newValue(typ Type, addr addr, flag uint32) Value { 1821 func newValue(typ Type, addr addr, flag uint32) Value {
1302 v := value{typ, addr, flag} 1822 v := value{typ, addr, flag}
1303 » switch typ.(type) { 1823 » switch typ.Kind() {
1304 » case *ArrayType: 1824 » case Array:
1305 » » return &ArrayValue{v} 1825 » » return Value{&arrayValue{v}}
1306 » case *BoolType: 1826 » case Bool:
1307 » » return &BoolValue{v} 1827 » » return Value{&boolValue{v}}
1308 » case *ChanType: 1828 » case Chan:
1309 » » return &ChanValue{v} 1829 » » return Value{&chanValue{v}}
1310 » case *FloatType: 1830 » case Float32, Float64:
1311 » » return &FloatValue{v} 1831 » » return Value{&floatValue{v}}
1312 » case *FuncType: 1832 » case Func:
1313 » » return &FuncValue{value: v} 1833 » » return Value{&funcValue{value: v}}
1314 » case *ComplexType: 1834 » case Complex64, Complex128:
1315 » » return &ComplexValue{v} 1835 » » return Value{&complexValue{v}}
1316 » case *IntType: 1836 » case Int, Int8, Int16, Int32, Int64:
1317 » » return &IntValue{v} 1837 » » return Value{&intValue{v}}
1318 » case *InterfaceType: 1838 » case Interface:
1319 » » return &InterfaceValue{v} 1839 » » return Value{&InterfaceValue{v}}
1320 » case *MapType: 1840 » case Map:
1321 » » return &MapValue{v} 1841 » » return Value{&mapValue{v}}
1322 » case *PtrType: 1842 » case Ptr:
1323 » » return &PtrValue{v} 1843 » » return Value{&ptrValue{v}}
1324 » case *SliceType: 1844 » case Slice:
1325 » » return &SliceValue{v} 1845 » » return Value{&sliceValue{v}}
1326 » case *StringType: 1846 » case String:
1327 » » return &StringValue{v} 1847 » » return Value{&stringValue{v}}
1328 » case *StructType: 1848 » case Struct:
1329 » » return &StructValue{v} 1849 » » return Value{&structValue{v}}
1330 » case *UintType: 1850 » case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
1331 » » return &UintValue{v} 1851 » » return Value{&uintValue{v}}
1332 » case *UnsafePointerType: 1852 » case UnsafePointer:
1333 » » return &UnsafePointerValue{v} 1853 » » return Value{&unsafePointerValue{v}}
1334 } 1854 }
1335 panic("newValue" + typ.String()) 1855 panic("newValue" + typ.String())
1336 } 1856 }
1337 1857
1338 // MakeZero returns a zero Value for the specified Type. 1858 // MakeZero returns a zero Value for the specified Type.
niemeyer 2011/04/04 13:52:35 More confusion. Now the zero Value is actually the
rsc 2011/04/05 16:29:45 Thanks, this one is wrong. Changed. // MakeZero
1339 func MakeZero(typ Type) Value { 1859 func MakeZero(typ Type) Value {
1340 if typ == nil { 1860 if typ == nil {
1341 » » return nil 1861 » » panic("reflect: MakeZero(nil)")
1342 } 1862 }
1343 return newValue(typ, addr(unsafe.New(typ)), canSet|canAddr|canStore) 1863 return newValue(typ, addr(unsafe.New(typ)), canSet|canAddr|canStore)
1344 } 1864 }
OLDNEW
« src/pkg/reflect/type.go ('K') | « src/pkg/reflect/type.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b