}
var (
- intBits = uintptr(reflect.Typeof(int(0)).Size() * 8)
- floatBits = uintptr(reflect.Typeof(float(0)).Size() * 8)
- complexBits = uintptr(reflect.Typeof(complex(0+0i)).Size() * 8)
- uintptrBits = uintptr(reflect.Typeof(uintptr(0)).Size() * 8)
+ intBits = reflect.Typeof(0).Bits()
+ floatBits = reflect.Typeof(0.0).Bits()
+ complexBits = reflect.Typeof(1i).Bits()
+ uintptrBits = reflect.Typeof(uintptr(0)).Bits()
)
func (p *pp) printField(field interface{}, verb int, plus, sharp bool, depth int) (was_string bool) {
}
// scanRune returns the next rune value in the input.
-func (s *ss) scanRune(bitSize uintptr) int64 {
+func (s *ss) scanRune(bitSize int) int64 {
rune := int64(s.mustGetRune())
- x := (rune << (64 - bitSize)) >> (64 - bitSize)
+ n := uint(bitSize)
+ x := (rune << (64 - n)) >> (64 - n)
if x != rune {
s.errorString("overflow on character value " + string(rune))
}
// scanInt returns the value of the integer represented by the next
// token, checking for overflow. Any error is stored in s.err.
-func (s *ss) scanInt(verb int, bitSize uintptr) int64 {
+func (s *ss) scanInt(verb int, bitSize int) int64 {
if verb == 'c' {
return s.scanRune(bitSize)
}
if err != nil {
s.error(err)
}
- x := (i << (64 - bitSize)) >> (64 - bitSize)
+ n := uint(bitSize)
+ x := (i << (64 - n)) >> (64 - n)
if x != i {
s.errorString("integer overflow on token " + tok)
}
// scanUint returns the value of the unsigned integer represented
// by the next token, checking for overflow. Any error is stored in s.err.
-func (s *ss) scanUint(verb int, bitSize uintptr) uint64 {
+func (s *ss) scanUint(verb int, bitSize int) uint64 {
if verb == 'c' {
return uint64(s.scanRune(bitSize))
}
if err != nil {
s.error(err)
}
- x := (i << (64 - bitSize)) >> (64 - bitSize)
+ n := uint(bitSize)
+ x := (i << (64 - n)) >> (64 - n)
if x != i {
s.errorString("unsigned integer overflow on token " + tok)
}
case *reflect.BoolValue:
v.Set(s.scanBool(verb))
case *reflect.IntValue:
- v.Set(s.scanInt(verb, v.Type().Size()*8))
+ v.Set(s.scanInt(verb, v.Type().Bits()))
case *reflect.UintValue:
- v.Set(s.scanUint(verb, v.Type().Size()*8))
+ v.Set(s.scanUint(verb, v.Type().Bits()))
case *reflect.StringValue:
v.Set(s.convertString(verb))
case *reflect.SliceValue:
}
case *reflect.FloatValue:
s.skipSpace()
- v.Set(s.convertFloat(s.floatToken(), int(v.Type().Size()*8)))
+ v.Set(s.convertFloat(s.floatToken(), v.Type().Bits()))
case *reflect.ComplexValue:
- v.Set(s.scanComplex(verb, int(v.Type().Size()*8)))
+ v.Set(s.scanComplex(verb, v.Type().Bits()))
default:
CantHandle:
s.errorString("Scan: can't handle type: " + val.Type().String())
return ignoreArrayHelper(state, elemOp, int(decodeUint(state)))
}
-var decOpMap = map[reflect.Kind]decOp{
+var decOpMap = []decOp{
reflect.Bool: decBool,
reflect.Int8: decInt8,
reflect.Int16: decInt16,
// the indirection count to reach it.
func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string) (decOp, int, os.Error) {
typ, indir := indirect(rt)
- op, ok := decOpMap[typ.Kind()]
- if !ok {
+ var op decOp
+ k := typ.Kind()
+ if int(k) < len(decOpMap) {
+ op = decOpMap[k]
+ }
+ if op == nil {
// Special cases
switch t := typ.(type) {
case *reflect.ArrayType:
return state.err
}
-var encOpMap = map[reflect.Kind]encOp{
+var encOpMap = []encOp{
reflect.Bool: encBool,
reflect.Int: encInt,
reflect.Int8: encInt8,
// the indirection count to reach it.
func encOpFor(rt reflect.Type) (encOp, int, os.Error) {
typ, indir := indirect(rt)
- op, ok := encOpMap[typ.Kind()]
- if !ok {
+ var op encOp
+ k := typ.Kind()
+ if int(k) < len(encOpMap) {
+ op = encOpMap[k]
+ }
+ if op == nil {
// Special cases
switch t := typ.(type) {
case *reflect.SliceType:
v.Set(n)
case *reflect.FloatValue:
- n, err := strconv.AtofN(s, int(v.Type().Size()*8))
+ n, err := strconv.AtofN(s, v.Type().Bits())
if err != nil || v.Overflow(n) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break
e.WriteString(strconv.Uitoa64(v.Get()))
case *reflect.FloatValue:
- e.WriteString(strconv.FtoaN(v.Get(), 'g', -1, int(v.Type().Size()*8)))
+ e.WriteString(strconv.FtoaN(v.Get(), 'g', -1, v.Type().Bits()))
case *reflect.StringValue:
e.string(v.Get())
* copy in order to access the private fields.
*/
+// commonType is the common implementation of most values.
+// It is embedded in other, public struct types, but always
+// with a unique tag like "uint" or "float" so that the client cannot
+// convert from, say, *UintType to *FloatType.
+
type commonType struct {
size uintptr
hash uint32
// BoolType represents a boolean type.
type BoolType struct {
- commonType
+ commonType "bool"
}
// FloatType represents a float type.
type FloatType struct {
- commonType
+ commonType "float"
}
// ComplexType represents a complex type.
type ComplexType struct {
- commonType
+ commonType "complex"
}
// IntType represents a signed integer type.
type IntType struct {
- commonType
+ commonType "int"
}
// UintType represents a uint type.
type UintType struct {
- commonType
+ commonType "uint"
}
// StringType represents a string type.
type StringType struct {
- commonType
+ commonType "string"
}
// UnsafePointerType represents an unsafe.Pointer type.
type UnsafePointerType struct {
- commonType
+ commonType "unsafe.Pointer"
}
// ArrayType represents a fixed array type.
type ArrayType struct {
- commonType
- elem *runtime.Type
- len uintptr
+ commonType "array"
+ elem *runtime.Type
+ len uintptr
}
// ChanDir represents a channel type's direction.
// ChanType represents a channel type.
type ChanType struct {
- commonType
- elem *runtime.Type
- dir uintptr
+ commonType "chan"
+ elem *runtime.Type
+ dir uintptr
}
// FuncType represents a function type.
type FuncType struct {
- commonType
- dotdotdot bool
- in []*runtime.Type
- out []*runtime.Type
+ commonType "func"
+ dotdotdot bool
+ in []*runtime.Type
+ out []*runtime.Type
}
// Method on interface type
// InterfaceType represents an interface type.
type InterfaceType struct {
- commonType
- methods []imethod
+ commonType "interface"
+ methods []imethod
}
// MapType represents a map type.
type MapType struct {
- commonType
- key *runtime.Type
- elem *runtime.Type
+ commonType "map"
+ key *runtime.Type
+ elem *runtime.Type
}
// PtrType represents a pointer type.
type PtrType struct {
- commonType
- elem *runtime.Type
+ commonType "ptr"
+ elem *runtime.Type
}
// SliceType represents a slice type.
type SliceType struct {
- commonType
- elem *runtime.Type
+ commonType "slice"
+ elem *runtime.Type
}
// Struct field
// StructType represents a struct type.
type StructType struct {
- commonType
- fields []structField
+ commonType "struct"
+ fields []structField
}
// a value of the given type; it is analogous to unsafe.Sizeof.
Size() uintptr
+ // Bits returns the size of the type in bits.
+ // It is intended for use with numeric types and may overflow
+ // when used for composite types.
+ Bits() int
+
// Align returns the alignment of a value of this type
// when allocated in memory.
Align() int
func (t *commonType) Size() uintptr { return t.size }
+func (t *commonType) Bits() int { return int(t.size * 8) }
+
func (t *commonType) Align() int { return int(t.align) }
func (t *commonType) FieldAlign() int { return int(t.fieldAlign) }
}
m.Type = toType(*p.typ).(*FuncType)
fn := p.tfn
- m.Func = newFuncValue(m.Type, addr(&fn), true)
+ m.Func = &FuncValue{value: value{m.Type, addr(&fn), true}}
return
}
getAddr() addr
}
+// value is the common implementation of most values.
+// It is embedded in other, public struct types, but always
+// with a unique tag like "uint" or "float" so that the client cannot
+// convert from, say, *UintValue to *FloatValue.
type value struct {
typ Type
addr addr
// BoolValue represents a bool value.
type BoolValue struct {
- value
+ value "bool"
}
// Get returns the underlying bool value.
// FloatValue represents a float value.
type FloatValue struct {
- value
+ value "float"
}
// Get returns the underlying int value.
// ComplexValue represents a complex value.
type ComplexValue struct {
- value
+ value "complex"
}
// Get returns the underlying complex value.
// Set sets v to the value x.
func (v *ComplexValue) SetValue(x Value) { v.Set(x.(*ComplexValue).Get()) }
-// Complex64Value represents a complex64 value.
-type Complex64Value struct {
- value
-}
-
-// Get returns the underlying complex64 value.
-func (v *Complex64Value) Get() complex64 { return *(*complex64)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Complex64Value) Set(x complex64) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*complex64)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Complex64Value) SetValue(x Value) { v.Set(x.(*Complex64Value).Get()) }
-
-// Complex128Value represents a complex128 value.
-type Complex128Value struct {
- value
-}
-
-// Get returns the underlying complex128 value.
-func (v *Complex128Value) Get() complex128 { return *(*complex128)(v.addr) }
-
-// Set sets v to the value x.
-func (v *Complex128Value) Set(x complex128) {
- if !v.canSet {
- panic(cannotSet)
- }
- *(*complex128)(v.addr) = x
-}
-
-// Set sets v to the value x.
-func (v *Complex128Value) SetValue(x Value) { v.Set(x.(*Complex128Value).Get()) }
-
// IntValue represents an int value.
type IntValue struct {
- value
+ value "int"
}
// Get returns the underlying int 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
+ bitSize := uint(v.typ.Bits())
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
return x != trunc
}
// StringValue represents a string value.
type StringValue struct {
- value
+ value "string"
}
// Get returns the underlying string value.
// UintValue represents a uint value.
type UintValue struct {
- value
+ value "uint"
}
// Get returns the underlying uuint value.
// 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
+ bitSize := uint(v.typ.Bits())
trunc := (x << (64 - bitSize)) >> (64 - bitSize)
return x != trunc
}
// UnsafePointerValue represents an unsafe.Pointer value.
type UnsafePointerValue struct {
- value
+ value "unsafe.Pointer"
}
// Get returns the underlying uintptr value.
// An ArrayValue represents an array.
type ArrayValue struct {
- value
+ value "array"
}
// Len returns the length of the array.
// A SliceValue represents a slice.
type SliceValue struct {
- value
+ value "slice"
}
func (v *SliceValue) slice() *SliceHeader { return (*SliceHeader)(v.value.addr) }
// A ChanValue represents a chan.
type ChanValue struct {
- value
+ value "chan"
}
// IsNil returns whether v is a nil channel.
// A FuncValue represents a function value.
type FuncValue struct {
- value
+ value "func"
first *value
isInterface bool
}
// An InterfaceValue represents an interface value.
type InterfaceValue struct {
- value
+ value "interface"
}
// No Get because v.Interface() is available.
// A MapValue represents a map value.
type MapValue struct {
- value
+ value "map"
}
// IsNil returns whether v is a nil map value.
// A PtrValue represents a pointer.
type PtrValue struct {
- value
+ value "ptr"
}
// IsNil returns whether v is a nil pointer.
// A StructValue represents a struct value.
type StructValue struct {
- value
+ value "struct"
}
// Set assigns x to v.
return newValue(toType(t), addr(a), true)
}
-
-func newFuncValue(typ Type, addr addr, canSet bool) *FuncValue {
- return &FuncValue{value: value{typ, addr, canSet}}
-}
-
func newValue(typ Type, addr addr, canSet bool) Value {
- // FuncValue has a different layout;
- // it needs a extra space for the fixed receivers.
- if _, ok := typ.(*FuncType); ok {
- return newFuncValue(typ, addr, canSet)
- }
-
- // All values have same memory layout;
- // build once and convert.
- v := &struct{ value }{value{typ, addr, canSet}}
+ v := value{typ, addr, canSet}
switch typ.(type) {
case *ArrayType:
- // TODO(rsc): Something must prevent
- // clients of the package from doing
- // this same kind of cast.
- // We should be allowed because
- // they're our types.
- // Something about implicit assignment
- // to struct fields.
- return (*ArrayValue)(v)
+ return &ArrayValue{v}
case *BoolType:
- return (*BoolValue)(v)
+ return &BoolValue{v}
case *ChanType:
- return (*ChanValue)(v)
+ return &ChanValue{v}
case *FloatType:
- return (*FloatValue)(v)
+ return &FloatValue{v}
+ case *FuncType:
+ return &FuncValue{value: v}
case *ComplexType:
- return (*ComplexValue)(v)
+ return &ComplexValue{v}
case *IntType:
- return (*IntValue)(v)
+ return &IntValue{v}
case *InterfaceType:
- return (*InterfaceValue)(v)
+ return &InterfaceValue{v}
case *MapType:
- return (*MapValue)(v)
+ return &MapValue{v}
case *PtrType:
- return (*PtrValue)(v)
+ return &PtrValue{v}
case *SliceType:
- return (*SliceValue)(v)
+ return &SliceValue{v}
case *StringType:
- return (*StringValue)(v)
+ return &StringValue{v}
case *StructType:
- return (*StructValue)(v)
+ return &StructValue{v}
case *UintType:
- return (*UintValue)(v)
+ return &UintValue{v}
case *UnsafePointerType:
- return (*UnsafePointerValue)(v)
+ return &UnsafePointerValue{v}
}
panic("newValue" + typ.String())
}
var a interface{}
switch c := reflect.NewValue(a).(type) {
- case *reflect.Complex64Value:
- v := c.Get()
- _, _ = complex64(v), true
case *reflect.ComplexValue:
if complexBits == 64 {
v := c.Get()