reflect: add Kind, remove Int8Type, Int8Value, etc.
update other code to match.
R=r
CC=golang-dev
https://golang.org/cl/1680044
diff --git a/src/pkg/reflect/value.go b/src/pkg/reflect/value.go
index 7730fef..5505c46 100644
--- a/src/pkg/reflect/value.go
+++ b/src/pkg/reflect/value.go
@@ -5,6 +5,7 @@
package reflect
import (
+ "math"
"runtime"
"unsafe"
)
@@ -134,72 +135,83 @@
value
}
-// Get returns the underlying float value.
-func (v *FloatValue) Get() float { return *(*float)(v.addr) }
+// Get returns the underlying int value.
+func (v *FloatValue) Get() float64 {
+ switch v.typ.(*FloatType).Kind() {
+ case Float:
+ return float64(*(*float)(v.addr))
+ case Float32:
+ return float64(*(*float32)(v.addr))
+ case Float64:
+ return *(*float64)(v.addr)
+ }
+ panic("reflect: invalid float kind")
+}
// Set sets v to the value x.
-func (v *FloatValue) Set(x float) {
+func (v *FloatValue) Set(x float64) {
if !v.canSet {
panic(cannotSet)
}
- *(*float)(v.addr) = x
+ switch v.typ.(*FloatType).Kind() {
+ default:
+ panic("reflect: invalid float kind")
+ case Float:
+ *(*float)(v.addr) = float(x)
+ case Float32:
+ *(*float32)(v.addr) = float32(x)
+ case Float64:
+ *(*float64)(v.addr) = x
+ }
+}
+
+// Overflow returns true if x cannot be represented by the type of v.
+func (v *FloatValue) Overflow(x float64) bool {
+ if v.typ.Size() == 8 {
+ return false
+ }
+ if x < 0 {
+ x = -x
+ }
+ return math.MaxFloat32 < x && x <= math.MaxFloat64
}
// Set sets v to the value x.
func (v *FloatValue) SetValue(x Value) { v.Set(x.(*FloatValue).Get()) }
-// Float32Value represents a float32 value.
-type Float32Value struct {
- value
-}
-
-// Get returns the underlying float32 value.
-func (v *Float32Value) Get() float32 { return *(*float32)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Float32Value) Set(x float32) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*float32)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Float32Value) SetValue(x Value) { v.Set(x.(*Float32Value).Get()) }
-
-// Float64Value represents a float64 value.
-type Float64Value struct {
- value
-}
-
-// Get returns the underlying float64 value.
-func (v *Float64Value) Get() float64 { return *(*float64)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Float64Value) Set(x float64) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*float64)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Float64Value) SetValue(x Value) { v.Set(x.(*Float64Value).Get()) }
-
// ComplexValue represents a complex value.
type ComplexValue struct {
value
}
// Get returns the underlying complex value.
-func (v *ComplexValue) Get() complex { return *(*complex)(v.addr) }
+func (v *ComplexValue) Get() complex128 {
+ switch v.typ.(*ComplexType).Kind() {
+ case Complex:
+ return complex128(*(*complex)(v.addr))
+ case Complex64:
+ return complex128(*(*complex64)(v.addr))
+ case Complex128:
+ return *(*complex128)(v.addr)
+ }
+ panic("reflect: invalid complex kind")
+}
// Set sets v to the value x.
-func (v *ComplexValue) Set(x complex) {
+func (v *ComplexValue) Set(x complex128) {
if !v.canSet {
panic(cannotSet)
}
- *(*complex)(v.addr) = x
+ switch v.typ.(*ComplexType).Kind() {
+ default:
+ panic("reflect: invalid complex kind")
+ case Complex:
+ *(*complex)(v.addr) = complex(x)
+ case Complex64:
+ *(*complex64)(v.addr) = complex64(x)
+ case Complex128:
+ *(*complex128)(v.addr) = x
+ }
}
// Set sets v to the value x.
@@ -249,95 +261,53 @@
}
// Get returns the underlying int value.
-func (v *IntValue) Get() int { return *(*int)(v.addr) }
+func (v *IntValue) Get() int64 {
+ switch v.typ.(*IntType).Kind() {
+ case Int:
+ return int64(*(*int)(v.addr))
+ case Int8:
+ return int64(*(*int8)(v.addr))
+ case Int16:
+ return int64(*(*int16)(v.addr))
+ case Int32:
+ return int64(*(*int32)(v.addr))
+ case Int64:
+ return *(*int64)(v.addr)
+ }
+ panic("reflect: invalid int kind")
+}
// Set sets v to the value x.
-func (v *IntValue) Set(x int) {
+func (v *IntValue) Set(x int64) {
if !v.canSet {
panic(cannotSet)
}
- *(*int)(v.addr) = x
+ switch v.typ.(*IntType).Kind() {
+ default:
+ panic("reflect: invalid int kind")
+ case Int:
+ *(*int)(v.addr) = int(x)
+ case Int8:
+ *(*int8)(v.addr) = int8(x)
+ case Int16:
+ *(*int16)(v.addr) = int16(x)
+ case Int32:
+ *(*int32)(v.addr) = int32(x)
+ case Int64:
+ *(*int64)(v.addr) = x
+ }
}
// Set sets v to the value x.
func (v *IntValue) SetValue(x Value) { v.Set(x.(*IntValue).Get()) }
-// Int8Value represents an int8 value.
-type Int8Value struct {
- value
+// Overflow returns true if x cannot be represented by the type of v.
+func (v *IntValue) Overflow(x int64) bool {
+ bitSize := v.typ.Size() * 8
+ trunc := (x << (64 - bitSize)) >> (64 - bitSize)
+ return x != trunc
}
-// Get returns the underlying int8 value.
-func (v *Int8Value) Get() int8 { return *(*int8)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Int8Value) Set(x int8) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*int8)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Int8Value) SetValue(x Value) { v.Set(x.(*Int8Value).Get()) }
-
-// Int16Value represents an int16 value.
-type Int16Value struct {
- value
-}
-
-// Get returns the underlying int16 value.
-func (v *Int16Value) Get() int16 { return *(*int16)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Int16Value) Set(x int16) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*int16)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Int16Value) SetValue(x Value) { v.Set(x.(*Int16Value).Get()) }
-
-// Int32Value represents an int32 value.
-type Int32Value struct {
- value
-}
-
-// Get returns the underlying int32 value.
-func (v *Int32Value) Get() int32 { return *(*int32)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Int32Value) Set(x int32) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*int32)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Int32Value) SetValue(x Value) { v.Set(x.(*Int32Value).Get()) }
-
-// Int64Value represents an int64 value.
-type Int64Value struct {
- value
-}
-
-// Get returns the underlying int64 value.
-func (v *Int64Value) Get() int64 { return *(*int64)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Int64Value) Set(x int64) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*int64)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Int64Value) SetValue(x Value) { v.Set(x.(*Int64Value).Get()) }
-
// StringHeader is the runtime representation of a string.
type StringHeader struct {
Data uintptr
@@ -368,115 +338,58 @@
value
}
-// Get returns the underlying uint value.
-func (v *UintValue) Get() uint { return *(*uint)(v.addr) }
+// Get returns the underlying uuint value.
+func (v *UintValue) Get() uint64 {
+ switch v.typ.(*UintType).Kind() {
+ case Uint:
+ return uint64(*(*uint)(v.addr))
+ case Uint8:
+ return uint64(*(*uint8)(v.addr))
+ case Uint16:
+ return uint64(*(*uint16)(v.addr))
+ case Uint32:
+ return uint64(*(*uint32)(v.addr))
+ case Uint64:
+ return *(*uint64)(v.addr)
+ case Uintptr:
+ return uint64(*(*uintptr)(v.addr))
+ }
+ panic("reflect: invalid uint kind")
+}
// Set sets v to the value x.
-func (v *UintValue) Set(x uint) {
+func (v *UintValue) Set(x uint64) {
if !v.canSet {
panic(cannotSet)
}
- *(*uint)(v.addr) = x
+ switch v.typ.(*UintType).Kind() {
+ default:
+ panic("reflect: invalid uint kind")
+ case Uint:
+ *(*uint)(v.addr) = uint(x)
+ case Uint8:
+ *(*uint8)(v.addr) = uint8(x)
+ case Uint16:
+ *(*uint16)(v.addr) = uint16(x)
+ case Uint32:
+ *(*uint32)(v.addr) = uint32(x)
+ case Uint64:
+ *(*uint64)(v.addr) = x
+ case Uintptr:
+ *(*uintptr)(v.addr) = uintptr(x)
+ }
+}
+
+// Overflow returns true if x cannot be represented by the type of v.
+func (v *UintValue) Overflow(x uint64) bool {
+ bitSize := v.typ.Size() * 8
+ trunc := (x << (64 - bitSize)) >> (64 - bitSize)
+ return x != trunc
}
// Set sets v to the value x.
func (v *UintValue) SetValue(x Value) { v.Set(x.(*UintValue).Get()) }
-// Uint8Value represents a uint8 value.
-type Uint8Value struct {
- value
-}
-
-// Get returns the underlying uint8 value.
-func (v *Uint8Value) Get() uint8 { return *(*uint8)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Uint8Value) Set(x uint8) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*uint8)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Uint8Value) SetValue(x Value) { v.Set(x.(*Uint8Value).Get()) }
-
-// Uint16Value represents a uint16 value.
-type Uint16Value struct {
- value
-}
-
-// Get returns the underlying uint16 value.
-func (v *Uint16Value) Get() uint16 { return *(*uint16)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Uint16Value) Set(x uint16) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*uint16)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Uint16Value) SetValue(x Value) { v.Set(x.(*Uint16Value).Get()) }
-
-// Uint32Value represents a uint32 value.
-type Uint32Value struct {
- value
-}
-
-// Get returns the underlying uint32 value.
-func (v *Uint32Value) Get() uint32 { return *(*uint32)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Uint32Value) Set(x uint32) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*uint32)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Uint32Value) SetValue(x Value) { v.Set(x.(*Uint32Value).Get()) }
-
-// Uint64Value represents a uint64 value.
-type Uint64Value struct {
- value
-}
-
-// Get returns the underlying uint64 value.
-func (v *Uint64Value) Get() uint64 { return *(*uint64)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Uint64Value) Set(x uint64) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*uint64)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Uint64Value) SetValue(x Value) { v.Set(x.(*Uint64Value).Get()) }
-
-// UintptrValue represents a uintptr value.
-type UintptrValue struct {
- value
-}
-
-// Get returns the underlying uintptr value.
-func (v *UintptrValue) Get() uintptr { return *(*uintptr)(v.addr) }
-
-// Set sets v to the value x.
-func (v *UintptrValue) Set(x uintptr) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*uintptr)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *UintptrValue) SetValue(x Value) { v.Set(x.(*UintptrValue).Get()) }
-
// UnsafePointerValue represents an unsafe.Pointer value.
type UnsafePointerValue struct {
value
@@ -1329,26 +1242,10 @@
return (*ChanValue)(v)
case *FloatType:
return (*FloatValue)(v)
- case *Float32Type:
- return (*Float32Value)(v)
- case *Float64Type:
- return (*Float64Value)(v)
case *ComplexType:
return (*ComplexValue)(v)
- case *Complex64Type:
- return (*Complex64Value)(v)
- case *Complex128Type:
- return (*Complex128Value)(v)
case *IntType:
return (*IntValue)(v)
- case *Int8Type:
- return (*Int8Value)(v)
- case *Int16Type:
- return (*Int16Value)(v)
- case *Int32Type:
- return (*Int32Value)(v)
- case *Int64Type:
- return (*Int64Value)(v)
case *InterfaceType:
return (*InterfaceValue)(v)
case *MapType:
@@ -1363,16 +1260,6 @@
return (*StructValue)(v)
case *UintType:
return (*UintValue)(v)
- case *Uint8Type:
- return (*Uint8Value)(v)
- case *Uint16Type:
- return (*Uint16Value)(v)
- case *Uint32Type:
- return (*Uint32Value)(v)
- case *Uint64Type:
- return (*Uint64Value)(v)
- case *UintptrType:
- return (*UintptrValue)(v)
case *UnsafePointerType:
return (*UnsafePointerValue)(v)
}