Commit 554d0aa5 authored by Rob Pike's avatar Rob Pike

make sizes and offsets int, not uint64

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