]> Cypherpunks repositories - gostls13.git/commitdiff
make sizes and offsets int, not uint64
authorRob Pike <r@golang.org>
Wed, 5 Nov 2008 18:17:38 +0000 (10:17 -0800)
committerRob Pike <r@golang.org>
Wed, 5 Nov 2008 18:17:38 +0000 (10:17 -0800)
add Value.Interface, to extract an empty interface
that can be converted to a regular Go value of the
appropriate type, if known.

R=rsc
DELTA=49  (2 added, 0 deleted, 47 changed)
OCL=18526
CL=18526

src/lib/reflect/test.go
src/lib/reflect/tostring.go
src/lib/reflect/type.go
src/lib/reflect/value.go

index 13be64cc0835168e4bea0d219b838eff3183ece6..a260a8aa3cf4cd9945fe9bfaba468a55060b8b34 100644 (file)
@@ -202,7 +202,7 @@ func main() {
        var name string;
        var typ reflect.Type;
        var tag string;
-       var offset uint64;
+       var offset int;
 
        // Type strings
        t = reflect.ParseTypeString("", "int8");
index 2359b8cd36cfc7f47776c7206d5a74328782a20b..4707a8e76fe2f838c2e616084a45297b6f000fd7 100644 (file)
@@ -39,7 +39,7 @@ func DoubleQuote(s string) string {
 }
 
 type HasFields interface {
-       Field(i int)    (name string, typ Type, tag string, offset uint64);
+       Field(i int)    (name string, typ Type, tag string, offset int);
        Len()   int;
 }
 
@@ -177,7 +177,7 @@ func ValueToString(val Value) string {
                v := val.(ArrayValue);
                str += TypeToString(t, false);
                str += "{";
-               for i := uint64(0); i < v.Len(); i++ {
+               for i := 0; i < v.Len(); i++ {
                        if i > 0 {
                                str += ", "
                        }
index 09ecbc0675e4a8d677e7ed1edbf87a68c39d6e12..899b02f155061c873c4e28c50ffc666828fda78e 100644 (file)
@@ -41,8 +41,9 @@ export const (
        Uint8Kind;
 )
 
-var ptrsize uint64
-var interfacesize uint64
+// Int is guaranteed large enough to store a size.
+var ptrsize int
+var interfacesize int
 
 var MissingString = "$missing$"        // syntactic name for undefined type names
 var DotDotDotString = "..."
@@ -52,7 +53,7 @@ export type Type interface {
        Name()  string;
        String()        string;
        SetString(string);      // TODO: remove when no longer needed
-       Size()  uint64;
+       Size()  int;
 }
 
 // Fields and methods common to all types
@@ -60,7 +61,7 @@ type Common struct {
        kind    int;
        str     string;
        name    string;
-       size    uint64;
+       size    int;
 }
 
 func (c *Common) Kind() int {
@@ -79,7 +80,7 @@ func (c *Common) SetString(s string) {
        c.str = s
 }
 
-func (c *Common) Size() uint64 {
+func (c *Common) Size() int {
        return c.size
 }
 
@@ -89,7 +90,7 @@ type BasicType struct {
        Common
 }
 
-func NewBasicType(name string, kind int, size uint64) Type {
+func NewBasicType(name string, kind int, size int) Type {
        return &BasicType{ Common{kind, name, name, size} }
 }
 
@@ -157,7 +158,7 @@ func (t *PtrTypeStruct) Sub() Type {
 
 export type ArrayType interface {
        Open()  bool;
-       Len()   uint64;
+       Len()   int;
        Elem()  Type;
 }
 
@@ -165,14 +166,14 @@ type ArrayTypeStruct struct {
        Common;
        elem    *StubType;
        open    bool;   // otherwise fixed size
-       len     uint64;
+       len     int;
 }
 
-func NewArrayTypeStruct(name, typestring string, open bool, len uint64, elem *StubType) *ArrayTypeStruct {
+func NewArrayTypeStruct(name, typestring string, open bool, len int, elem *StubType) *ArrayTypeStruct {
        return &ArrayTypeStruct{ Common{ArrayKind, typestring, name, 0}, elem, open, len}
 }
 
-func (t *ArrayTypeStruct) Size() uint64 {
+func (t *ArrayTypeStruct) Size() int {
        if t.open {
                return ptrsize  // open arrays are pointers to structures
        }
@@ -183,7 +184,7 @@ func (t *ArrayTypeStruct) Open() bool {
        return t.open
 }
 
-func (t *ArrayTypeStruct) Len() uint64 {
+func (t *ArrayTypeStruct) Len() int {
        // what about open array?  TODO
        return t.len
 }
@@ -209,7 +210,7 @@ func NewMapTypeStruct(name, typestring string, key, elem *StubType) *MapTypeStru
        return &MapTypeStruct{ Common{MapKind, typestring, name, 0}, key, elem}
 }
 
-func (t *MapTypeStruct) Size() uint64 {
+func (t *MapTypeStruct) Size() int {
        panic("reflect.type: map.Size(): cannot happen");
        return 0
 }
@@ -245,7 +246,7 @@ func NewChanTypeStruct(name, typestring string, dir int, elem *StubType) *ChanTy
        return &ChanTypeStruct{ Common{ChanKind, typestring, name, 0}, elem, dir}
 }
 
-func (t *ChanTypeStruct) Size() uint64 {
+func (t *ChanTypeStruct) Size() int {
        panic("reflect.type: chan.Size(): cannot happen");
        return 0
 }
@@ -261,7 +262,7 @@ func (t *ChanTypeStruct) Elem() Type {
 // -- Struct
 
 export type StructType interface {
-       Field(int)      (name string, typ Type, tag string, offset uint64);
+       Field(int)      (name string, typ Type, tag string, offset int);
        Len()   int;
 }
 
@@ -269,8 +270,8 @@ type Field struct {
        name    string;
        typ     *StubType;
        tag     string;
-       size    uint64;
-       offset  uint64;
+       size    int;
+       offset  int;
 }
 
 type StructTypeStruct struct {
@@ -283,11 +284,11 @@ func NewStructTypeStruct(name, typestring string, field *[]Field) *StructTypeStr
 }
 
 // TODO: not portable; depends on 6g
-func (t *StructTypeStruct) Size() uint64 {
+func (t *StructTypeStruct) Size() int {
        if t.size > 0 {
                return t.size
        }
-       size := uint64(0);
+       size := 0;
        for i := 0; i < len(t.field); i++ {
                elemsize := t.field[i].typ.Get().Size();
                // pad until at (elemsize mod 8) boundary
@@ -301,12 +302,13 @@ func (t *StructTypeStruct) Size() uint64 {
                t.field[i].offset = size;
                size += elemsize;
        }
-       size = (size + 7) & ((1<<64 - 1) & ^7);
+       structalignmask := 7;   // TODO: knows that size fits in int32 (also can't use const here)
+       size = (size + structalignmask) & ^(structalignmask);
        t.size = size;
        return size;
 }
 
-func (t *StructTypeStruct) Field(i int) (name string, typ Type, tag string, offset uint64) {
+func (t *StructTypeStruct) Field(i int) (name string, typ Type, tag string, offset int) {
        if t.field[i].offset == 0 {
                t.Size();       // will compute offsets
        }
@@ -320,7 +322,7 @@ func (t *StructTypeStruct) Len() int {
 // -- Interface
 
 export type InterfaceType interface {
-       Field(int)      (name string, typ Type, tag string, offset uint64);
+       Field(int)      (name string, typ Type, tag string, offset int);
        Len()   int;
 }
 
@@ -333,7 +335,7 @@ func NewInterfaceTypeStruct(name, typestring string, field *[]Field) *InterfaceT
        return &InterfaceTypeStruct{ Common{InterfaceKind, typestring, name, interfacesize}, field }
 }
 
-func (t *InterfaceTypeStruct) Field(i int) (name string, typ Type, tag string, offset uint64) {
+func (t *InterfaceTypeStruct) Field(i int) (name string, typ Type, tag string, offset int) {
        return t.field[i].name, t.field[i].typ.Get(), "", 0
 }
 
@@ -358,7 +360,7 @@ func NewFuncTypeStruct(name, typestring string, in, out *StructTypeStruct) *Func
        return &FuncTypeStruct{ Common{FuncKind, typestring, name, 0}, in, out }
 }
 
-func (t *FuncTypeStruct) Size() uint64 {
+func (t *FuncTypeStruct) Size() int {
        panic("reflect.type: func.Size(): cannot happen");
        return 0
 }
@@ -622,7 +624,7 @@ func (p *Parser) Next() {
 func (p *Parser) Type(name string) *StubType
 
 func (p *Parser) Array(name string, tokstart int) *StubType {
-       size := uint64(0);
+       size := 0;
        open := true;
        if p.token != "]" {
                if len(p.token) == 0 || !isdigit(p.token[0]) {
@@ -631,7 +633,7 @@ func (p *Parser) Array(name string, tokstart int) *StubType {
                // write our own (trivial and simpleminded) atoi to avoid dependency
                size = 0;
                for i := 0; i < len(p.token); i++ {
-                       size = size * 10 + uint64(p.token[i]) - '0'
+                       size = size * 10 + int(p.token[i]) - '0'
                }
                p.Next();
                open = false;
index 527f1ff0050d767280404d8f971d3a5f98cbf677..c7089b1580f2eb2fe5e0915da6567c78e3a6edc2 100644 (file)
@@ -39,7 +39,7 @@ export type Empty interface {}        // TODO(r): Delete when no longer needed?
 export type Value interface {
        Kind()  int;
        Type()  Type;
-       Unreflect()     Empty;
+       Interface()     Empty;
 }
 
 // Common fields and functionality for all values
@@ -58,7 +58,7 @@ func (c *Common) Type() Type {
        return c.typ
 }
 
-func (c *Common) Unreflect() Empty {
+func (c *Common) Interface() Empty {
        return sys.unreflect(*AddrToPtrAddr(c.addr), c.typ.String());
 }
 
@@ -517,14 +517,14 @@ export type ArrayValue interface {
        Kind()  int;
        Type()  Type;
        Open()  bool;
-       Len()   uint64;
-       Elem(i uint64)  Value;
+       Len()   int;
+       Elem(i int)     Value;
 }
 
 type OpenArrayValueStruct struct {
        Common;
        elemtype        Type;
-       elemsize        uint64;
+       elemsize        int;
 }
 
 /*
@@ -539,32 +539,32 @@ func (v *OpenArrayValueStruct) Open() bool {
        return true
 }
 
-func (v *OpenArrayValueStruct) Len() uint64 {
-       return uint64(*AddrToPtrInt32(v.addr+8));
+func (v *OpenArrayValueStruct) Len() int {
+       return int(*AddrToPtrInt32(v.addr+8));
 }
 
-func (v *OpenArrayValueStruct) Elem(i uint64) Value {
+func (v *OpenArrayValueStruct) Elem(i int) Value {
        base := *AddrToPtrAddr(v.addr);
-       return NewValueAddr(v.elemtype, base + i * v.elemsize);
+       return NewValueAddr(v.elemtype, base + Addr(i * v.elemsize));
 }
 
 type FixedArrayValueStruct struct {
        Common;
        elemtype        Type;
-       elemsize        uint64;
-       len     uint64;
+       elemsize        int;
+       len     int;
 }
 
 func (v *FixedArrayValueStruct) Open() bool {
        return false
 }
 
-func (v *FixedArrayValueStruct) Len() uint64 {
+func (v *FixedArrayValueStruct) Len() int {
        return v.len
 }
 
-func (v *FixedArrayValueStruct) Elem(i uint64) Value {
-       return NewValueAddr(v.elemtype, v.addr + i * v.elemsize);
+func (v *FixedArrayValueStruct) Elem(i int) Value {
+       return NewValueAddr(v.elemtype, v.addr + Addr(i * v.elemsize));
        return nil
 }
 
@@ -658,7 +658,7 @@ func StructCreator(typ Type, addr Addr) Value {
        v := &StructValueStruct{ Common{StructKind, typ, addr}, new([]Value, nfield) };
        for i := 0; i < nfield; i++ {
                name, ftype, str, offset := t.Field(i);
-               v.field[i] = NewValueAddr(ftype, addr + offset);
+               v.field[i] = NewValueAddr(ftype, addr + Addr(offset));
        }
        v.typ = typ;
        return v;