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

Side by Side Diff: libgo/go/reflect/value.go

Issue 4035044: code review 4035044: Update to current version of Go library. (Closed)
Patch Set: Created 14 years, 2 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
« no previous file with comments | « libgo/go/reflect/type.go ('k') | libgo/go/regexp/all_test.go » ('j') | 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 // Set sets v to the value x. 134 // Set sets v to the value x.
135 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) } 135 func (v *BoolValue) SetValue(x Value) { v.Set(x.(*BoolValue).Get()) }
136 136
137 // FloatValue represents a float value. 137 // FloatValue represents a float value.
138 type FloatValue struct { 138 type FloatValue struct {
139 value "float" 139 value "float"
140 } 140 }
141 141
142 // Get returns the underlying int value. 142 // Get returns the underlying int value.
143 func (v *FloatValue) Get() float64 { 143 func (v *FloatValue) Get() float64 {
144 » switch v.typ.(*FloatType).Kind() { 144 » switch v.typ.Kind() {
145 » case Float:
146 » » return float64(*(*float)(v.addr))
147 case Float32: 145 case Float32:
148 return float64(*(*float32)(v.addr)) 146 return float64(*(*float32)(v.addr))
149 case Float64: 147 case Float64:
150 return *(*float64)(v.addr) 148 return *(*float64)(v.addr)
151 } 149 }
152 panic("reflect: invalid float kind") 150 panic("reflect: invalid float kind")
153 } 151 }
154 152
155 // Set sets v to the value x. 153 // Set sets v to the value x.
156 func (v *FloatValue) Set(x float64) { 154 func (v *FloatValue) Set(x float64) {
157 if !v.canSet { 155 if !v.canSet {
158 panic(cannotSet) 156 panic(cannotSet)
159 } 157 }
160 » switch v.typ.(*FloatType).Kind() { 158 » switch v.typ.Kind() {
161 default: 159 default:
162 panic("reflect: invalid float kind") 160 panic("reflect: invalid float kind")
163 case Float:
164 *(*float)(v.addr) = float(x)
165 case Float32: 161 case Float32:
166 *(*float32)(v.addr) = float32(x) 162 *(*float32)(v.addr) = float32(x)
167 case Float64: 163 case Float64:
168 *(*float64)(v.addr) = x 164 *(*float64)(v.addr) = x
169 } 165 }
170 } 166 }
171 167
172 // Overflow returns true if x cannot be represented by the type of v. 168 // Overflow returns true if x cannot be represented by the type of v.
173 func (v *FloatValue) Overflow(x float64) bool { 169 func (v *FloatValue) Overflow(x float64) bool {
174 if v.typ.Size() == 8 { 170 if v.typ.Size() == 8 {
175 return false 171 return false
176 } 172 }
177 if x < 0 { 173 if x < 0 {
178 x = -x 174 x = -x
179 } 175 }
180 return math.MaxFloat32 < x && x <= math.MaxFloat64 176 return math.MaxFloat32 < x && x <= math.MaxFloat64
181 } 177 }
182 178
183 // Set sets v to the value x. 179 // Set sets v to the value x.
184 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) } 180 func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
185 181
186 // ComplexValue represents a complex value. 182 // ComplexValue represents a complex value.
187 type ComplexValue struct { 183 type ComplexValue struct {
188 value "complex" 184 value "complex"
189 } 185 }
190 186
191 // Get returns the underlying complex value. 187 // Get returns the underlying complex value.
192 func (v *ComplexValue) Get() complex128 { 188 func (v *ComplexValue) Get() complex128 {
193 » switch v.typ.(*ComplexType).Kind() { 189 » switch v.typ.Kind() {
194 » case Complex:
195 » » return complex128(*(*complex)(v.addr))
196 case Complex64: 190 case Complex64:
197 return complex128(*(*complex64)(v.addr)) 191 return complex128(*(*complex64)(v.addr))
198 case Complex128: 192 case Complex128:
199 return *(*complex128)(v.addr) 193 return *(*complex128)(v.addr)
200 } 194 }
201 panic("reflect: invalid complex kind") 195 panic("reflect: invalid complex kind")
202 } 196 }
203 197
204 // Set sets v to the value x. 198 // Set sets v to the value x.
205 func (v *ComplexValue) Set(x complex128) { 199 func (v *ComplexValue) Set(x complex128) {
206 if !v.canSet { 200 if !v.canSet {
207 panic(cannotSet) 201 panic(cannotSet)
208 } 202 }
209 » switch v.typ.(*ComplexType).Kind() { 203 » switch v.typ.Kind() {
210 default: 204 default:
211 panic("reflect: invalid complex kind") 205 panic("reflect: invalid complex kind")
212 case Complex:
213 *(*complex)(v.addr) = complex(x)
214 case Complex64: 206 case Complex64:
215 *(*complex64)(v.addr) = complex64(x) 207 *(*complex64)(v.addr) = complex64(x)
216 case Complex128: 208 case Complex128:
217 *(*complex128)(v.addr) = x 209 *(*complex128)(v.addr) = x
218 } 210 }
219 } 211 }
220 212
221 // Set sets v to the value x. 213 // Set sets v to the value x.
222 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) } 214 func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) }
223 215
224 // IntValue represents an int value. 216 // IntValue represents an int value.
225 type IntValue struct { 217 type IntValue struct {
226 value "int" 218 value "int"
227 } 219 }
228 220
229 // Get returns the underlying int value. 221 // Get returns the underlying int value.
230 func (v *IntValue) Get() int64 { 222 func (v *IntValue) Get() int64 {
231 » switch v.typ.(*IntType).Kind() { 223 » switch v.typ.Kind() {
232 case Int: 224 case Int:
233 return int64(*(*int)(v.addr)) 225 return int64(*(*int)(v.addr))
234 case Int8: 226 case Int8:
235 return int64(*(*int8)(v.addr)) 227 return int64(*(*int8)(v.addr))
236 case Int16: 228 case Int16:
237 return int64(*(*int16)(v.addr)) 229 return int64(*(*int16)(v.addr))
238 case Int32: 230 case Int32:
239 return int64(*(*int32)(v.addr)) 231 return int64(*(*int32)(v.addr))
240 case Int64: 232 case Int64:
241 return *(*int64)(v.addr) 233 return *(*int64)(v.addr)
242 } 234 }
243 panic("reflect: invalid int kind") 235 panic("reflect: invalid int kind")
244 } 236 }
245 237
246 // Set sets v to the value x. 238 // Set sets v to the value x.
247 func (v *IntValue) Set(x int64) { 239 func (v *IntValue) Set(x int64) {
248 if !v.canSet { 240 if !v.canSet {
249 panic(cannotSet) 241 panic(cannotSet)
250 } 242 }
251 » switch v.typ.(*IntType).Kind() { 243 » switch v.typ.Kind() {
252 default: 244 default:
253 panic("reflect: invalid int kind") 245 panic("reflect: invalid int kind")
254 case Int: 246 case Int:
255 *(*int)(v.addr) = int(x) 247 *(*int)(v.addr) = int(x)
256 case Int8: 248 case Int8:
257 *(*int8)(v.addr) = int8(x) 249 *(*int8)(v.addr) = int8(x)
258 case Int16: 250 case Int16:
259 *(*int16)(v.addr) = int16(x) 251 *(*int16)(v.addr) = int16(x)
260 case Int32: 252 case Int32:
261 *(*int32)(v.addr) = int32(x) 253 *(*int32)(v.addr) = int32(x)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // Set sets v to the value x. 291 // Set sets v to the value x.
300 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) } 292 func (v *StringValue) SetValue(x Value) { v.Set(x.(*StringValue).Get()) }
301 293
302 // UintValue represents a uint value. 294 // UintValue represents a uint value.
303 type UintValue struct { 295 type UintValue struct {
304 value "uint" 296 value "uint"
305 } 297 }
306 298
307 // Get returns the underlying uuint value. 299 // Get returns the underlying uuint value.
308 func (v *UintValue) Get() uint64 { 300 func (v *UintValue) Get() uint64 {
309 » switch v.typ.(*UintType).Kind() { 301 » switch v.typ.Kind() {
310 case Uint: 302 case Uint:
311 return uint64(*(*uint)(v.addr)) 303 return uint64(*(*uint)(v.addr))
312 case Uint8: 304 case Uint8:
313 return uint64(*(*uint8)(v.addr)) 305 return uint64(*(*uint8)(v.addr))
314 case Uint16: 306 case Uint16:
315 return uint64(*(*uint16)(v.addr)) 307 return uint64(*(*uint16)(v.addr))
316 case Uint32: 308 case Uint32:
317 return uint64(*(*uint32)(v.addr)) 309 return uint64(*(*uint32)(v.addr))
318 case Uint64: 310 case Uint64:
319 return *(*uint64)(v.addr) 311 return *(*uint64)(v.addr)
320 case Uintptr: 312 case Uintptr:
321 return uint64(*(*uintptr)(v.addr)) 313 return uint64(*(*uintptr)(v.addr))
322 } 314 }
323 panic("reflect: invalid uint kind") 315 panic("reflect: invalid uint kind")
324 } 316 }
325 317
326 // Set sets v to the value x. 318 // Set sets v to the value x.
327 func (v *UintValue) Set(x uint64) { 319 func (v *UintValue) Set(x uint64) {
328 if !v.canSet { 320 if !v.canSet {
329 panic(cannotSet) 321 panic(cannotSet)
330 } 322 }
331 » switch v.typ.(*UintType).Kind() { 323 » switch v.typ.Kind() {
332 default: 324 default:
333 panic("reflect: invalid uint kind") 325 panic("reflect: invalid uint kind")
334 case Uint: 326 case Uint:
335 *(*uint)(v.addr) = uint(x) 327 *(*uint)(v.addr) = uint(x)
336 case Uint8: 328 case Uint8:
337 *(*uint8)(v.addr) = uint8(x) 329 *(*uint8)(v.addr) = uint8(x)
338 case Uint16: 330 case Uint16:
339 *(*uint16)(v.addr) = uint16(x) 331 *(*uint16)(v.addr) = uint16(x)
340 case Uint32: 332 case Uint32:
341 *(*uint32)(v.addr) = uint32(x) 333 *(*uint32)(v.addr) = uint32(x)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // ArrayOrSliceValue is the common interface 385 // ArrayOrSliceValue is the common interface
394 // implemented by both ArrayValue and SliceValue. 386 // implemented by both ArrayValue and SliceValue.
395 type ArrayOrSliceValue interface { 387 type ArrayOrSliceValue interface {
396 Value 388 Value
397 Len() int 389 Len() int
398 Cap() int 390 Cap() int
399 Elem(i int) Value 391 Elem(i int) Value
400 addr() addr 392 addr() addr
401 } 393 }
402 394
403 // ArrayCopy copies the contents of src into dst until either 395 // grow grows the slice s so that it can hold extra more values, allocating
396 // more capacity if needed. It also returns the old and new slice lengths.
397 func grow(s *SliceValue, extra int) (*SliceValue, int, int) {
398 » i0 := s.Len()
399 » i1 := i0 + extra
400 » if i1 < i0 {
401 » » panic("append: slice overflow")
402 » }
403 » m := s.Cap()
404 » if i1 <= m {
405 » » return s.Slice(0, i1), i0, i1
406 » }
407 » if m == 0 {
408 » » m = extra
409 » } else {
410 » » for m < i1 {
411 » » » if i0 < 1024 {
412 » » » » m += m
413 » » » } else {
414 » » » » m += m / 4
415 » » » }
416 » » }
417 » }
418 » t := MakeSlice(s.Type().(*SliceType), i1, m)
419 » Copy(t, s)
420 » return t, i0, i1
421 }
422
423 // Append appends the values x to a slice s and returns the resulting slice.
424 // Each x must have the same type as s' element type.
425 func Append(s *SliceValue, x ...Value) *SliceValue {
426 » s, i0, i1 := grow(s, len(x))
427 » for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
428 » » s.Elem(i).SetValue(x[j])
429 » }
430 » return s
431 }
432
433 // AppendSlice appends a slice t to a slice s and returns the resulting slice.
434 // The slices s and t must have the same element type.
435 func AppendSlice(s, t *SliceValue) *SliceValue {
436 » s, i0, i1 := grow(s, t.Len())
437 » Copy(s.Slice(i0, i1), t)
438 » return s
439 }
440
441 // Copy copies the contents of src into dst until either
404 // dst has been filled or src has been exhausted. 442 // dst has been filled or src has been exhausted.
405 // It returns the number of elements copied. 443 // It returns the number of elements copied.
406 // The arrays dst and src must have the same element type. 444 // The arrays dst and src must have the same element type.
407 func ArrayCopy(dst, src ArrayOrSliceValue) int { 445 func Copy(dst, src ArrayOrSliceValue) int {
408 // TODO: This will have to move into the runtime 446 // TODO: This will have to move into the runtime
409 // once the real gc goes in. 447 // once the real gc goes in.
410 de := dst.Type().(ArrayOrSliceType).Elem() 448 de := dst.Type().(ArrayOrSliceType).Elem()
411 se := src.Type().(ArrayOrSliceType).Elem() 449 se := src.Type().(ArrayOrSliceType).Elem()
412 typesMustMatch(de, se) 450 typesMustMatch(de, se)
413 n := dst.Len() 451 n := dst.Len()
414 if xn := src.Len(); n > xn { 452 if xn := src.Len(); n > xn {
415 n = xn 453 n = xn
416 } 454 }
417 memmove(dst.addr(), src.addr(), uintptr(n)*de.Size()) 455 memmove(dst.addr(), src.addr(), uintptr(n)*de.Size())
(...skipping 14 matching lines...) Expand all
432 // addr returns the base address of the data in the array. 470 // addr returns the base address of the data in the array.
433 func (v *ArrayValue) addr() addr { return v.value.addr } 471 func (v *ArrayValue) addr() addr { return v.value.addr }
434 472
435 // Set assigns x to v. 473 // Set assigns x to v.
436 // The new value x must have the same type as v. 474 // The new value x must have the same type as v.
437 func (v *ArrayValue) Set(x *ArrayValue) { 475 func (v *ArrayValue) Set(x *ArrayValue) {
438 if !v.canSet { 476 if !v.canSet {
439 panic(cannotSet) 477 panic(cannotSet)
440 } 478 }
441 typesMustMatch(v.typ, x.typ) 479 typesMustMatch(v.typ, x.typ)
442 » ArrayCopy(v, x) 480 » Copy(v, x)
443 } 481 }
444 482
445 // Set sets v to the value x. 483 // Set sets v to the value x.
446 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) } 484 func (v *ArrayValue) SetValue(x Value) { v.Set(x.(*ArrayValue)) }
447 485
448 // Elem returns the i'th element of v. 486 // Elem returns the i'th element of v.
449 func (v *ArrayValue) Elem(i int) Value { 487 func (v *ArrayValue) Elem(i int) Value {
450 typ := v.typ.(*ArrayType).Elem() 488 typ := v.typ.(*ArrayType).Elem()
451 n := v.Len() 489 n := v.Len()
452 if i < 0 || i >= n { 490 if i < 0 || i >= n {
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
1195 panic("newValue" + typ.String()) 1233 panic("newValue" + typ.String())
1196 } 1234 }
1197 1235
1198 // MakeZero returns a zero Value for the specified Type. 1236 // MakeZero returns a zero Value for the specified Type.
1199 func MakeZero(typ Type) Value { 1237 func MakeZero(typ Type) Value {
1200 if typ == nil { 1238 if typ == nil {
1201 return nil 1239 return nil
1202 } 1240 }
1203 return newValue(typ, addr(unsafe.New(typ)), true) 1241 return newValue(typ, addr(unsafe.New(typ)), true)
1204 } 1242 }
OLDNEW
« no previous file with comments | « libgo/go/reflect/type.go ('k') | libgo/go/regexp/all_test.go » ('j') | no next file with comments »

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