1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:34:39 -07:00

gofix: add support for reflect rename

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/4450053
This commit is contained in:
Russ Cox 2011-04-25 13:39:00 -04:00
parent f1781bec3b
commit 41342dc73c
17 changed files with 129 additions and 106 deletions

View File

@ -21,6 +21,7 @@ var reflectFix = fix{
`Adapt code to new reflect API. `Adapt code to new reflect API.
http://codereview.appspot.com/4281055 http://codereview.appspot.com/4281055
http://codereview.appspot.com/4433066
`, `,
} }
@ -279,6 +280,23 @@ func reflectFn(f *ast.File) bool {
fixed = true fixed = true
}) })
// Rewrite
// reflect.Typeof -> reflect.TypeOf,
walk(f, func(n interface{}) {
sel, ok := n.(*ast.SelectorExpr)
if !ok {
return
}
if isTopName(sel.X, "reflect") && sel.Sel.Name == "Typeof" {
sel.Sel.Name = "TypeOf"
fixed = true
}
if isTopName(sel.X, "reflect") && sel.Sel.Name == "NewValue" {
sel.Sel.Name = "ValueOf"
fixed = true
}
})
return fixed return fixed
} }

View File

@ -418,13 +418,13 @@ func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type
} }
var ( var (
bitStringType = reflect.Typeof(BitString{}) bitStringType = reflect.TypeOf(BitString{})
objectIdentifierType = reflect.Typeof(ObjectIdentifier{}) objectIdentifierType = reflect.TypeOf(ObjectIdentifier{})
enumeratedType = reflect.Typeof(Enumerated(0)) enumeratedType = reflect.TypeOf(Enumerated(0))
flagType = reflect.Typeof(Flag(false)) flagType = reflect.TypeOf(Flag(false))
timeType = reflect.Typeof(&time.Time{}) timeType = reflect.TypeOf(&time.Time{})
rawValueType = reflect.Typeof(RawValue{}) rawValueType = reflect.TypeOf(RawValue{})
rawContentsType = reflect.Typeof(RawContent(nil)) rawContentsType = reflect.TypeOf(RawContent(nil))
) )
// invalidLength returns true iff offset + length > sliceLength, or if the // invalidLength returns true iff offset + length > sliceLength, or if the
@ -461,7 +461,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
} }
result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length], bytes[initOffset : offset+t.length]} result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length], bytes[initOffset : offset+t.length]}
offset += t.length offset += t.length
v.Set(reflect.NewValue(result)) v.Set(reflect.ValueOf(result))
return return
} }
@ -506,7 +506,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
return return
} }
if result != nil { if result != nil {
ifaceValue.Set(reflect.NewValue(result)) ifaceValue.Set(reflect.ValueOf(result))
} }
return return
} }
@ -609,7 +609,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
sliceValue := v sliceValue := v
sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), len(newSlice), len(newSlice))) sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), len(newSlice), len(newSlice)))
if err1 == nil { if err1 == nil {
reflect.Copy(sliceValue, reflect.NewValue(newSlice)) reflect.Copy(sliceValue, reflect.ValueOf(newSlice))
} }
err = err1 err = err1
return return
@ -617,7 +617,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
structValue := v structValue := v
bs, err1 := parseBitString(innerBytes) bs, err1 := parseBitString(innerBytes)
if err1 == nil { if err1 == nil {
structValue.Set(reflect.NewValue(bs)) structValue.Set(reflect.ValueOf(bs))
} }
err = err1 err = err1
return return
@ -631,7 +631,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
time, err1 = parseGeneralizedTime(innerBytes) time, err1 = parseGeneralizedTime(innerBytes)
} }
if err1 == nil { if err1 == nil {
ptrValue.Set(reflect.NewValue(time)) ptrValue.Set(reflect.ValueOf(time))
} }
err = err1 err = err1
return return
@ -679,7 +679,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
if structType.NumField() > 0 && if structType.NumField() > 0 &&
structType.Field(0).Type == rawContentsType { structType.Field(0).Type == rawContentsType {
bytes := bytes[initOffset:offset] bytes := bytes[initOffset:offset]
val.Field(0).Set(reflect.NewValue(RawContent(bytes))) val.Field(0).Set(reflect.ValueOf(RawContent(bytes)))
} }
innerOffset := 0 innerOffset := 0
@ -701,7 +701,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
sliceType := fieldType sliceType := fieldType
if sliceType.Elem().Kind() == reflect.Uint8 { if sliceType.Elem().Kind() == reflect.Uint8 {
val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes))) val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
reflect.Copy(val, reflect.NewValue(innerBytes)) reflect.Copy(val, reflect.ValueOf(innerBytes))
return return
} }
newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem()) newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
@ -806,7 +806,7 @@ func Unmarshal(b []byte, val interface{}) (rest []byte, err os.Error) {
// UnmarshalWithParams allows field parameters to be specified for the // UnmarshalWithParams allows field parameters to be specified for the
// top-level element. The form of the params is the same as the field tags. // top-level element. The form of the params is the same as the field tags.
func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err os.Error) { func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err os.Error) {
v := reflect.NewValue(val).Elem() v := reflect.ValueOf(val).Elem()
offset, err := parseField(v, b, 0, parseFieldParameters(params)) offset, err := parseField(v, b, 0, parseFieldParameters(params))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -671,7 +671,7 @@ func (f Format) Eval(env Environment, args ...interface{}) ([]byte, os.Error) {
go func() { go func() {
for _, v := range args { for _, v := range args {
fld := reflect.NewValue(v) fld := reflect.ValueOf(v)
if !fld.IsValid() { if !fld.IsValid() {
errors <- os.NewError("nil argument") errors <- os.NewError("nil argument")
return return

View File

@ -122,11 +122,11 @@ func (d *decodeState) unmarshal(v interface{}) (err os.Error) {
} }
}() }()
rv := reflect.NewValue(v) rv := reflect.ValueOf(v)
pv := rv pv := rv
if pv.Kind() != reflect.Ptr || if pv.Kind() != reflect.Ptr ||
pv.IsNil() { pv.IsNil() {
return &InvalidUnmarshalError{reflect.Typeof(v)} return &InvalidUnmarshalError{reflect.TypeOf(v)}
} }
d.scan.reset() d.scan.reset()
@ -314,7 +314,7 @@ func (d *decodeState) array(v reflect.Value) {
iv := v iv := v
ok := iv.Kind() == reflect.Interface ok := iv.Kind() == reflect.Interface
if ok { if ok {
iv.Set(reflect.NewValue(d.arrayInterface())) iv.Set(reflect.ValueOf(d.arrayInterface()))
return return
} }
@ -410,7 +410,7 @@ func (d *decodeState) object(v reflect.Value) {
// Decoding into nil interface? Switch to non-reflect code. // Decoding into nil interface? Switch to non-reflect code.
iv := v iv := v
if iv.Kind() == reflect.Interface { if iv.Kind() == reflect.Interface {
iv.Set(reflect.NewValue(d.objectInterface())) iv.Set(reflect.ValueOf(d.objectInterface()))
return return
} }
@ -423,7 +423,7 @@ func (d *decodeState) object(v reflect.Value) {
case reflect.Map: case reflect.Map:
// map must have string type // map must have string type
t := v.Type() t := v.Type()
if t.Key() != reflect.Typeof("") { if t.Key() != reflect.TypeOf("") {
d.saveError(&UnmarshalTypeError{"object", v.Type()}) d.saveError(&UnmarshalTypeError{"object", v.Type()})
break break
} }
@ -514,7 +514,7 @@ func (d *decodeState) object(v reflect.Value) {
// Write value back to map; // Write value back to map;
// if using struct, subv points into struct already. // if using struct, subv points into struct already.
if mv.IsValid() { if mv.IsValid() {
mv.SetMapIndex(reflect.NewValue(key), subv) mv.SetMapIndex(reflect.ValueOf(key), subv)
} }
// Next token must be , or }. // Next token must be , or }.
@ -570,7 +570,7 @@ func (d *decodeState) literal(v reflect.Value) {
case reflect.Bool: case reflect.Bool:
v.SetBool(value) v.SetBool(value)
case reflect.Interface: case reflect.Interface:
v.Set(reflect.NewValue(value)) v.Set(reflect.ValueOf(value))
} }
case '"': // string case '"': // string
@ -592,11 +592,11 @@ func (d *decodeState) literal(v reflect.Value) {
d.saveError(err) d.saveError(err)
break break
} }
v.Set(reflect.NewValue(b[0:n])) v.Set(reflect.ValueOf(b[0:n]))
case reflect.String: case reflect.String:
v.SetString(string(s)) v.SetString(string(s))
case reflect.Interface: case reflect.Interface:
v.Set(reflect.NewValue(string(s))) v.Set(reflect.ValueOf(string(s)))
} }
default: // number default: // number
@ -613,7 +613,7 @@ func (d *decodeState) literal(v reflect.Value) {
d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) d.saveError(&UnmarshalTypeError{"number " + s, v.Type()})
break break
} }
v.Set(reflect.NewValue(n)) v.Set(reflect.ValueOf(n))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.Atoi64(s) n, err := strconv.Atoi64(s)
@ -767,7 +767,7 @@ func (d *decodeState) literalInterface() interface{} {
} }
n, err := strconv.Atof64(string(item)) n, err := strconv.Atof64(string(item))
if err != nil { if err != nil {
d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.Typeof(0.0)}) d.saveError(&UnmarshalTypeError{"number " + string(item), reflect.TypeOf(0.0)})
} }
return n return n
} }

View File

@ -50,7 +50,7 @@ func (dec *Decoder) recvType(id typeId) {
// Type: // Type:
wire := new(wireType) wire := new(wireType)
dec.decodeValue(tWireType, reflect.NewValue(wire)) dec.decodeValue(tWireType, reflect.ValueOf(wire))
if dec.err != nil { if dec.err != nil {
return return
} }
@ -161,7 +161,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
if e == nil { if e == nil {
return dec.DecodeValue(reflect.Value{}) return dec.DecodeValue(reflect.Value{})
} }
value := reflect.NewValue(e) value := reflect.ValueOf(e)
// If e represents a value as opposed to a pointer, the answer won't // If e represents a value as opposed to a pointer, the answer won't
// get back to the caller. Make sure it's a pointer. // get back to the caller. Make sure it's a pointer.
if value.Type().Kind() != reflect.Ptr { if value.Type().Kind() != reflect.Ptr {

View File

@ -430,7 +430,7 @@ func packStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok bool)
if off+n > len(msg) { if off+n > len(msg) {
return len(msg), false return len(msg), false
} }
reflect.Copy(reflect.NewValue(msg[off:off+n]), fv) reflect.Copy(reflect.ValueOf(msg[off:off+n]), fv)
off += n off += n
case reflect.String: case reflect.String:
// There are multiple string encodings. // There are multiple string encodings.
@ -460,7 +460,7 @@ func packStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok bool)
} }
func structValue(any interface{}) reflect.Value { func structValue(any interface{}) reflect.Value {
return reflect.NewValue(any).Elem() return reflect.ValueOf(any).Elem()
} }
func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) { func packStruct(any interface{}, msg []byte, off int) (off1 int, ok bool) {
@ -508,7 +508,7 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok boo
if off+n > len(msg) { if off+n > len(msg) {
return len(msg), false return len(msg), false
} }
reflect.Copy(fv, reflect.NewValue(msg[off:off+n])) reflect.Copy(fv, reflect.ValueOf(msg[off:off+n]))
off += n off += n
case reflect.String: case reflect.String:
var s string var s string

View File

@ -172,7 +172,7 @@ func (e *encodeState) marshal(v interface{}) (err os.Error) {
err = r.(os.Error) err = r.(os.Error)
} }
}() }()
e.reflectValue(reflect.NewValue(v)) e.reflectValue(reflect.ValueOf(v))
return nil return nil
} }
@ -180,7 +180,7 @@ func (e *encodeState) error(err os.Error) {
panic(err) panic(err)
} }
var byteSliceType = reflect.Typeof([]byte(nil)) var byteSliceType = reflect.TypeOf([]byte(nil))
func (e *encodeState) reflectValue(v reflect.Value) { func (e *encodeState) reflectValue(v reflect.Value) {
if !v.IsValid() { if !v.IsValid() {

View File

@ -97,7 +97,7 @@ func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTyp
// Id: // Id:
state.encodeInt(-int64(info.id)) state.encodeInt(-int64(info.id))
// Type: // Type:
enc.encode(state.b, reflect.NewValue(info.wire), wireTypeUserInfo) enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
enc.writeMessage(w, state.b) enc.writeMessage(w, state.b)
if enc.err != nil { if enc.err != nil {
return return
@ -162,7 +162,7 @@ func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Typ
// Encode transmits the data item represented by the empty interface value, // Encode transmits the data item represented by the empty interface value,
// guaranteeing that all necessary type information has been transmitted first. // guaranteeing that all necessary type information has been transmitted first.
func (enc *Encoder) Encode(e interface{}) os.Error { func (enc *Encoder) Encode(e interface{}) os.Error {
return enc.EncodeValue(reflect.NewValue(e)) return enc.EncodeValue(reflect.ValueOf(e))
} }
// sendTypeDescriptor makes sure the remote side knows about this type. // sendTypeDescriptor makes sure the remote side knows about this type.

View File

@ -111,9 +111,9 @@ func (client *expClient) getChan(hdr *header, dir Dir) *netChan {
// data arrives from the client. // data arrives from the client.
func (client *expClient) run() { func (client *expClient) run() {
hdr := new(header) hdr := new(header)
hdrValue := reflect.NewValue(hdr) hdrValue := reflect.ValueOf(hdr)
req := new(request) req := new(request)
reqValue := reflect.NewValue(req) reqValue := reflect.ValueOf(req)
error := new(error) error := new(error)
for { for {
*hdr = header{} *hdr = header{}
@ -341,7 +341,7 @@ func (exp *Exporter) Sync(timeout int64) os.Error {
} }
func checkChan(chT interface{}, dir Dir) (reflect.Value, os.Error) { func checkChan(chT interface{}, dir Dir) (reflect.Value, os.Error) {
chanType := reflect.Typeof(chT) chanType := reflect.TypeOf(chT)
if chanType.Kind() != reflect.Chan { if chanType.Kind() != reflect.Chan {
return reflect.Value{}, os.ErrorString("not a channel") return reflect.Value{}, os.ErrorString("not a channel")
} }
@ -359,7 +359,7 @@ func checkChan(chT interface{}, dir Dir) (reflect.Value, os.Error) {
return reflect.Value{}, os.ErrorString("to import/export with Recv, must provide chan<-") return reflect.Value{}, os.ErrorString("to import/export with Recv, must provide chan<-")
} }
} }
return reflect.NewValue(chT), nil return reflect.ValueOf(chT), nil
} }
// Export exports a channel of a given type and specified direction. The // Export exports a channel of a given type and specified direction. The

View File

@ -260,7 +260,7 @@ func getField(v reflect.Value, i int) reflect.Value {
val := v.Field(i) val := v.Field(i)
if i := val; i.Kind() == reflect.Interface { if i := val; i.Kind() == reflect.Interface {
if inter := i.Interface(); inter != nil { if inter := i.Interface(); inter != nil {
return reflect.NewValue(inter) return reflect.ValueOf(inter)
} }
} }
return val return val
@ -284,7 +284,7 @@ func (p *pp) unknownType(v interface{}) {
return return
} }
p.buf.WriteByte('?') p.buf.WriteByte('?')
p.buf.WriteString(reflect.Typeof(v).String()) p.buf.WriteString(reflect.TypeOf(v).String())
p.buf.WriteByte('?') p.buf.WriteByte('?')
} }
@ -296,7 +296,7 @@ func (p *pp) badVerb(verb int, val interface{}) {
if val == nil { if val == nil {
p.buf.Write(nilAngleBytes) p.buf.Write(nilAngleBytes)
} else { } else {
p.buf.WriteString(reflect.Typeof(val).String()) p.buf.WriteString(reflect.TypeOf(val).String())
p.add('=') p.add('=')
p.printField(val, 'v', false, false, 0) p.printField(val, 'v', false, false, 0)
} }
@ -525,7 +525,7 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt
} }
if goSyntax { if goSyntax {
p.add('(') p.add('(')
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
p.add(')') p.add(')')
p.add('(') p.add('(')
if u == 0 { if u == 0 {
@ -540,10 +540,10 @@ func (p *pp) fmtPointer(field interface{}, value reflect.Value, verb int, goSynt
} }
var ( var (
intBits = reflect.Typeof(0).Bits() intBits = reflect.TypeOf(0).Bits()
floatBits = reflect.Typeof(0.0).Bits() floatBits = reflect.TypeOf(0.0).Bits()
complexBits = reflect.Typeof(1i).Bits() complexBits = reflect.TypeOf(1i).Bits()
uintptrBits = reflect.Typeof(uintptr(0)).Bits() uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
) )
func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) { func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) {
@ -560,10 +560,10 @@ func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth
// %T (the value's type) and %p (its address) are special; we always do them first. // %T (the value's type) and %p (its address) are special; we always do them first.
switch verb { switch verb {
case 'T': case 'T':
p.printField(reflect.Typeof(field).String(), 's', false, false, 0) p.printField(reflect.TypeOf(field).String(), 's', false, false, 0)
return false return false
case 'p': case 'p':
p.fmtPointer(field, reflect.NewValue(field), verb, goSyntax) p.fmtPointer(field, reflect.ValueOf(field), verb, goSyntax)
return false return false
} }
// Is it a Formatter? // Is it a Formatter?
@ -651,7 +651,7 @@ func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth
} }
// Need to use reflection // Need to use reflection
value := reflect.NewValue(field) value := reflect.ValueOf(field)
BigSwitch: BigSwitch:
switch f := value; f.Kind() { switch f := value; f.Kind() {
@ -702,7 +702,7 @@ BigSwitch:
} }
case reflect.Struct: case reflect.Struct:
if goSyntax { if goSyntax {
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
} }
p.add('{') p.add('{')
v := f v := f
@ -728,7 +728,7 @@ BigSwitch:
value := f.Elem() value := f.Elem()
if !value.IsValid() { if !value.IsValid() {
if goSyntax { if goSyntax {
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.Write(nilParenBytes) p.buf.Write(nilParenBytes)
} else { } else {
p.buf.Write(nilAngleBytes) p.buf.Write(nilAngleBytes)
@ -754,7 +754,7 @@ BigSwitch:
return verb == 's' return verb == 's'
} }
if goSyntax { if goSyntax {
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte('{') p.buf.WriteByte('{')
} else { } else {
p.buf.WriteByte('[') p.buf.WriteByte('[')
@ -792,7 +792,7 @@ BigSwitch:
} }
if goSyntax { if goSyntax {
p.buf.WriteByte('(') p.buf.WriteByte('(')
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte(')') p.buf.WriteByte(')')
p.buf.WriteByte('(') p.buf.WriteByte('(')
if v == 0 { if v == 0 {
@ -913,7 +913,7 @@ func (p *pp) doPrintf(format string, a []interface{}) {
for ; fieldnum < len(a); fieldnum++ { for ; fieldnum < len(a); fieldnum++ {
field := a[fieldnum] field := a[fieldnum]
if field != nil { if field != nil {
p.buf.WriteString(reflect.Typeof(field).String()) p.buf.WriteString(reflect.TypeOf(field).String())
p.buf.WriteByte('=') p.buf.WriteByte('=')
} }
p.printField(field, 'v', false, false, 0) p.printField(field, 'v', false, false, 0)
@ -932,7 +932,7 @@ func (p *pp) doPrint(a []interface{}, addspace, addnewline bool) {
// always add spaces if we're doing println // always add spaces if we're doing println
field := a[fieldnum] field := a[fieldnum]
if fieldnum > 0 { if fieldnum > 0 {
isString := field != nil && reflect.Typeof(field).Kind() == reflect.String isString := field != nil && reflect.TypeOf(field).Kind() == reflect.String
if addspace || !isString && !prevString { if addspace || !isString && !prevString {
p.buf.WriteByte(' ') p.buf.WriteByte(' ')
} }

View File

@ -59,39 +59,39 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
switch concrete := t; concrete.Kind() { switch concrete := t; concrete.Kind() {
case reflect.Bool: case reflect.Bool:
return reflect.NewValue(rand.Int()&1 == 0), true return reflect.ValueOf(rand.Int()&1 == 0), true
case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Complex64, reflect.Complex128: case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Complex64, reflect.Complex128:
switch t.Kind() { switch t.Kind() {
case reflect.Float32: case reflect.Float32:
return reflect.NewValue(randFloat32(rand)), true return reflect.ValueOf(randFloat32(rand)), true
case reflect.Float64: case reflect.Float64:
return reflect.NewValue(randFloat64(rand)), true return reflect.ValueOf(randFloat64(rand)), true
case reflect.Complex64: case reflect.Complex64:
return reflect.NewValue(complex(randFloat32(rand), randFloat32(rand))), true return reflect.ValueOf(complex(randFloat32(rand), randFloat32(rand))), true
case reflect.Complex128: case reflect.Complex128:
return reflect.NewValue(complex(randFloat64(rand), randFloat64(rand))), true return reflect.ValueOf(complex(randFloat64(rand), randFloat64(rand))), true
case reflect.Int16: case reflect.Int16:
return reflect.NewValue(int16(randInt64(rand))), true return reflect.ValueOf(int16(randInt64(rand))), true
case reflect.Int32: case reflect.Int32:
return reflect.NewValue(int32(randInt64(rand))), true return reflect.ValueOf(int32(randInt64(rand))), true
case reflect.Int64: case reflect.Int64:
return reflect.NewValue(randInt64(rand)), true return reflect.ValueOf(randInt64(rand)), true
case reflect.Int8: case reflect.Int8:
return reflect.NewValue(int8(randInt64(rand))), true return reflect.ValueOf(int8(randInt64(rand))), true
case reflect.Int: case reflect.Int:
return reflect.NewValue(int(randInt64(rand))), true return reflect.ValueOf(int(randInt64(rand))), true
case reflect.Uint16: case reflect.Uint16:
return reflect.NewValue(uint16(randInt64(rand))), true return reflect.ValueOf(uint16(randInt64(rand))), true
case reflect.Uint32: case reflect.Uint32:
return reflect.NewValue(uint32(randInt64(rand))), true return reflect.ValueOf(uint32(randInt64(rand))), true
case reflect.Uint64: case reflect.Uint64:
return reflect.NewValue(uint64(randInt64(rand))), true return reflect.ValueOf(uint64(randInt64(rand))), true
case reflect.Uint8: case reflect.Uint8:
return reflect.NewValue(uint8(randInt64(rand))), true return reflect.ValueOf(uint8(randInt64(rand))), true
case reflect.Uint: case reflect.Uint:
return reflect.NewValue(uint(randInt64(rand))), true return reflect.ValueOf(uint(randInt64(rand))), true
case reflect.Uintptr: case reflect.Uintptr:
return reflect.NewValue(uintptr(randInt64(rand))), true return reflect.ValueOf(uintptr(randInt64(rand))), true
} }
case reflect.Map: case reflect.Map:
numElems := rand.Intn(complexSize) numElems := rand.Intn(complexSize)
@ -130,7 +130,7 @@ func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
for i := 0; i < numChars; i++ { for i := 0; i < numChars; i++ {
codePoints[i] = rand.Intn(0x10ffff) codePoints[i] = rand.Intn(0x10ffff)
} }
return reflect.NewValue(string(codePoints)), true return reflect.ValueOf(string(codePoints)), true
case reflect.Struct: case reflect.Struct:
s := reflect.Zero(t) s := reflect.Zero(t)
for i := 0; i < s.NumField(); i++ { for i := 0; i < s.NumField(); i++ {
@ -339,7 +339,7 @@ func arbitraryValues(args []reflect.Value, f reflect.Type, config *Config, rand
} }
func functionAndType(f interface{}) (v reflect.Value, t reflect.Type, ok bool) { func functionAndType(f interface{}) (v reflect.Value, t reflect.Type, ok bool) {
v = reflect.NewValue(f) v = reflect.ValueOf(f)
ok = v.Kind() == reflect.Func ok = v.Kind() == reflect.Func
if !ok { if !ok {
return return

View File

@ -139,7 +139,7 @@ import (
// to a freshly allocated value and then mapping the element to that value. // to a freshly allocated value and then mapping the element to that value.
// //
func Unmarshal(r io.Reader, val interface{}) os.Error { func Unmarshal(r io.Reader, val interface{}) os.Error {
v := reflect.NewValue(val) v := reflect.ValueOf(val)
if v.Kind() != reflect.Ptr { if v.Kind() != reflect.Ptr {
return os.NewError("non-pointer passed to Unmarshal") return os.NewError("non-pointer passed to Unmarshal")
} }
@ -176,7 +176,7 @@ func (e *TagPathError) String() string {
// Passing a nil start element indicates that Unmarshal should // Passing a nil start element indicates that Unmarshal should
// read the token stream to find the start element. // read the token stream to find the start element.
func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error { func (p *Parser) Unmarshal(val interface{}, start *StartElement) os.Error {
v := reflect.NewValue(val) v := reflect.ValueOf(val)
if v.Kind() != reflect.Ptr { if v.Kind() != reflect.Ptr {
return os.NewError("non-pointer passed to Unmarshal") return os.NewError("non-pointer passed to Unmarshal")
} }
@ -280,7 +280,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
case reflect.Struct: case reflect.Struct:
if _, ok := v.Interface().(Name); ok { if _, ok := v.Interface().(Name); ok {
v.Set(reflect.NewValue(start.Name)) v.Set(reflect.ValueOf(start.Name))
break break
} }
@ -316,7 +316,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
if _, ok := v.Interface().(Name); !ok { if _, ok := v.Interface().(Name); !ok {
return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name") return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name")
} }
v.Set(reflect.NewValue(start.Name)) v.Set(reflect.ValueOf(start.Name))
} }
// Assign attributes. // Assign attributes.
@ -508,21 +508,21 @@ Loop:
case reflect.String: case reflect.String:
t.SetString(string(data)) t.SetString(string(data))
case reflect.Slice: case reflect.Slice:
t.Set(reflect.NewValue(data)) t.Set(reflect.ValueOf(data))
} }
switch t := saveComment; t.Kind() { switch t := saveComment; t.Kind() {
case reflect.String: case reflect.String:
t.SetString(string(comment)) t.SetString(string(comment))
case reflect.Slice: case reflect.Slice:
t.Set(reflect.NewValue(comment)) t.Set(reflect.ValueOf(comment))
} }
switch t := saveXML; t.Kind() { switch t := saveXML; t.Kind() {
case reflect.String: case reflect.String:
t.SetString(string(saveXMLData)) t.SetString(string(saveXMLData))
case reflect.Slice: case reflect.Slice:
t.Set(reflect.NewValue(saveXMLData)) t.Set(reflect.ValueOf(saveXMLData))
} }
return nil return nil

View File

@ -423,7 +423,7 @@ func (s *ss) token(skipSpace bool, f func(int) bool) []byte {
// typeError indicates that the type of the operand did not match the format // typeError indicates that the type of the operand did not match the format
func (s *ss) typeError(field interface{}, expected string) { func (s *ss) typeError(field interface{}, expected string) {
s.errorString("expected field of type pointer to " + expected + "; found " + reflect.Typeof(field).String()) s.errorString("expected field of type pointer to " + expected + "; found " + reflect.TypeOf(field).String())
} }
var complexError = os.ErrorString("syntax error scanning complex number") var complexError = os.ErrorString("syntax error scanning complex number")
@ -908,7 +908,7 @@ func (s *ss) scanOne(verb int, field interface{}) {
// If we scanned to bytes, the slice would point at the buffer. // If we scanned to bytes, the slice would point at the buffer.
*v = []byte(s.convertString(verb)) *v = []byte(s.convertString(verb))
default: default:
val := reflect.NewValue(v) val := reflect.ValueOf(v)
ptr := val ptr := val
if ptr.Kind() != reflect.Ptr { if ptr.Kind() != reflect.Ptr {
s.errorString("Scan: type not a pointer: " + val.Type().String()) s.errorString("Scan: type not a pointer: " + val.Type().String())

View File

@ -134,19 +134,19 @@ type empty struct {
} }
func newEmptyInterface(e empty) reflect.Value { func newEmptyInterface(e empty) reflect.Value {
return reflect.NewValue(e).Field(0) return reflect.ValueOf(e).Field(0)
} }
func (s Send) send() { func (s Send) send() {
// With reflect.ChanValue.Send, we must match the types exactly. So, if // With reflect.ChanValue.Send, we must match the types exactly. So, if
// s.Channel is a chan interface{} we convert s.Value to an interface{} // s.Channel is a chan interface{} we convert s.Value to an interface{}
// first. // first.
c := reflect.NewValue(s.Channel) c := reflect.ValueOf(s.Channel)
var v reflect.Value var v reflect.Value
if iface := c.Type().Elem(); iface.Kind() == reflect.Interface && iface.NumMethod() == 0 { if iface := c.Type().Elem(); iface.Kind() == reflect.Interface && iface.NumMethod() == 0 {
v = newEmptyInterface(empty{s.Value}) v = newEmptyInterface(empty{s.Value})
} else { } else {
v = reflect.NewValue(s.Value) v = reflect.ValueOf(s.Value)
} }
c.Send(v) c.Send(v)
} }
@ -162,7 +162,7 @@ func (s Close) getSend() sendAction { return s }
func (s Close) getChannel() interface{} { return s.Channel } func (s Close) getChannel() interface{} { return s.Channel }
func (s Close) send() { reflect.NewValue(s.Channel).Close() } func (s Close) send() { reflect.ValueOf(s.Channel).Close() }
// A ReceivedUnexpected error results if no active Events match a value // A ReceivedUnexpected error results if no active Events match a value
// received from a channel. // received from a channel.
@ -278,7 +278,7 @@ func getChannels(events []*Event) ([]interface{}, os.Error) {
continue continue
} }
c := event.action.getChannel() c := event.action.getChannel()
if reflect.NewValue(c).Kind() != reflect.Chan { if reflect.ValueOf(c).Kind() != reflect.Chan {
return nil, SetupError("one of the channel values is not a channel") return nil, SetupError("one of the channel values is not a channel")
} }
@ -303,7 +303,7 @@ func getChannels(events []*Event) ([]interface{}, os.Error) {
// channel repeatedly, wrapping them up as either a channelRecv or // channel repeatedly, wrapping them up as either a channelRecv or
// channelClosed structure, and forwards them to the multiplex channel. // channelClosed structure, and forwards them to the multiplex channel.
func recvValues(multiplex chan<- interface{}, channel interface{}) { func recvValues(multiplex chan<- interface{}, channel interface{}) {
c := reflect.NewValue(channel) c := reflect.ValueOf(channel)
for { for {
v, ok := c.Recv() v, ok := c.Recv()

View File

@ -646,7 +646,7 @@ func (t *Template) lookup(st *state, v reflect.Value, name string) reflect.Value
} }
return av.FieldByName(name) return av.FieldByName(name)
case reflect.Map: case reflect.Map:
if v := av.MapIndex(reflect.NewValue(name)); v.IsValid() { if v := av.MapIndex(reflect.ValueOf(name)); v.IsValid() {
return v return v
} }
return reflect.Zero(typ.Elem()) return reflect.Zero(typ.Elem())
@ -797,7 +797,7 @@ func (t *Template) executeElement(i int, st *state) int {
return elem.end return elem.end
} }
e := t.elems.At(i) e := t.elems.At(i)
t.execError(st, 0, "internal error: bad directive in execute: %v %T\n", reflect.NewValue(e).Interface(), e) t.execError(st, 0, "internal error: bad directive in execute: %v %T\n", reflect.ValueOf(e).Interface(), e)
return 0 return 0
} }
@ -980,7 +980,7 @@ func (t *Template) ParseFile(filename string) (err os.Error) {
// generating output to wr. // generating output to wr.
func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) { func (t *Template) Execute(wr io.Writer, data interface{}) (err os.Error) {
// Extract the driver data. // Extract the driver data.
val := reflect.NewValue(data) val := reflect.ValueOf(data)
defer checkError(&err) defer checkError(&err)
t.p = 0 t.p = 0
t.execute(0, t.elems.Len(), &state{parent: nil, data: val, wr: wr}) t.execute(0, t.elems.Len(), &state{parent: nil, data: val, wr: wr})

View File

@ -243,18 +243,18 @@ var (
) )
// Predefined because it's needed by the Decoder // Predefined because it's needed by the Decoder
var tWireType = mustGetTypeInfo(reflect.Typeof(wireType{})).id var tWireType = mustGetTypeInfo(reflect.TypeOf(wireType{})).id
var wireTypeUserInfo *userTypeInfo // userTypeInfo of (*wireType) var wireTypeUserInfo *userTypeInfo // userTypeInfo of (*wireType)
func init() { func init() {
// Some magic numbers to make sure there are no surprises. // Some magic numbers to make sure there are no surprises.
checkId(16, tWireType) checkId(16, tWireType)
checkId(17, mustGetTypeInfo(reflect.Typeof(arrayType{})).id) checkId(17, mustGetTypeInfo(reflect.TypeOf(arrayType{})).id)
checkId(18, mustGetTypeInfo(reflect.Typeof(CommonType{})).id) checkId(18, mustGetTypeInfo(reflect.TypeOf(CommonType{})).id)
checkId(19, mustGetTypeInfo(reflect.Typeof(sliceType{})).id) checkId(19, mustGetTypeInfo(reflect.TypeOf(sliceType{})).id)
checkId(20, mustGetTypeInfo(reflect.Typeof(structType{})).id) checkId(20, mustGetTypeInfo(reflect.TypeOf(structType{})).id)
checkId(21, mustGetTypeInfo(reflect.Typeof(fieldType{})).id) checkId(21, mustGetTypeInfo(reflect.TypeOf(fieldType{})).id)
checkId(23, mustGetTypeInfo(reflect.Typeof(mapType{})).id) checkId(23, mustGetTypeInfo(reflect.TypeOf(mapType{})).id)
builtinIdToType = make(map[typeId]gobType) builtinIdToType = make(map[typeId]gobType)
for k, v := range idToType { for k, v := range idToType {
@ -268,7 +268,7 @@ func init() {
} }
nextId = firstUserId nextId = firstUserId
registerBasics() registerBasics()
wireTypeUserInfo = userType(reflect.Typeof((*wireType)(nil))) wireTypeUserInfo = userType(reflect.TypeOf((*wireType)(nil)))
} }
// Array type // Array type
@ -569,7 +569,7 @@ func checkId(want, got typeId) {
// used for building the basic types; called only from init(). the incoming // used for building the basic types; called only from init(). the incoming
// interface always refers to a pointer. // interface always refers to a pointer.
func bootstrapType(name string, e interface{}, expect typeId) typeId { func bootstrapType(name string, e interface{}, expect typeId) typeId {
rt := reflect.Typeof(e).Elem() rt := reflect.TypeOf(e).Elem()
_, present := types[rt] _, present := types[rt]
if present { if present {
panic("bootstrap type already present: " + name + ", " + rt.String()) panic("bootstrap type already present: " + name + ", " + rt.String())
@ -723,7 +723,7 @@ func RegisterName(name string, value interface{}) {
// reserved for nil // reserved for nil
panic("attempt to register empty name") panic("attempt to register empty name")
} }
base := userType(reflect.Typeof(value)).base base := userType(reflect.TypeOf(value)).base
// Check for incompatible duplicates. // Check for incompatible duplicates.
if t, ok := nameToConcreteType[name]; ok && t != base { if t, ok := nameToConcreteType[name]; ok && t != base {
panic("gob: registering duplicate types for " + name) panic("gob: registering duplicate types for " + name)
@ -732,7 +732,7 @@ func RegisterName(name string, value interface{}) {
panic("gob: registering duplicate names for " + base.String()) panic("gob: registering duplicate names for " + base.String())
} }
// Store the name and type provided by the user.... // Store the name and type provided by the user....
nameToConcreteType[name] = reflect.Typeof(value) nameToConcreteType[name] = reflect.TypeOf(value)
// but the flattened type in the type table, since that's what decode needs. // but the flattened type in the type table, since that's what decode needs.
concreteTypeToName[base] = name concreteTypeToName[base] = name
} }
@ -745,7 +745,7 @@ func RegisterName(name string, value interface{}) {
// between types and names is not a bijection. // between types and names is not a bijection.
func Register(value interface{}) { func Register(value interface{}) {
// Default to printed representation for unnamed types // Default to printed representation for unnamed types
rt := reflect.Typeof(value) rt := reflect.TypeOf(value)
name := rt.String() name := rt.String()
// But for named types (or pointers to them), qualify with import path. // But for named types (or pointers to them), qualify with import path.

View File

@ -259,7 +259,7 @@ func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string) {
if n == nil { if n == nil {
return return
} }
if false && reflect.Typeof(n).Kind() == reflect.Ptr { // debugging trace if false && reflect.TypeOf(n).Kind() == reflect.Ptr { // debugging trace
defer func() { defer func() {
if t := typeof[n]; t != "" { if t := typeof[n]; t != "" {
pos := fset.Position(n.(ast.Node).Pos()) pos := fset.Position(n.(ast.Node).Pos())
@ -375,6 +375,11 @@ func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string) {
typeof[n] = gofmt(n.Args[0]) typeof[n] = gofmt(n.Args[0])
return return
} }
// new(T) has type *T
if isTopName(n.Fun, "new") && len(n.Args) == 1 {
typeof[n] = "*" + gofmt(n.Args[0])
return
}
// Otherwise, use type of function to determine arguments. // Otherwise, use type of function to determine arguments.
t := typeof[n.Fun] t := typeof[n.Fun]
in, out := splitFunc(t) in, out := splitFunc(t)