summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-02-11 17:55:16 -0800
committerRuss Cox <rsc@golang.org>2009-02-11 17:55:16 -0800
commitf6d092aa89daccb517a89abaa9e077a683b57bf1 (patch)
tree3a27c60cd93482129d8beda588d769a2d7868d4d /src
parentd89426aacb706dba5b305bd29d5e7f67927e3848 (diff)
downloadgolang-f6d092aa89daccb517a89abaa9e077a683b57bf1.tar.gz
insert type assertions when narrowing.
R=r OCL=24349 CL=24913
Diffstat (limited to 'src')
-rw-r--r--src/lib/json/generic.go4
-rw-r--r--src/lib/json/struct.go4
-rw-r--r--src/lib/reflect/all_test.go4
-rw-r--r--src/lib/reflect/tostring.go4
-rw-r--r--src/lib/reflect/type.go42
-rw-r--r--src/lib/reflect/value.go132
6 files changed, 164 insertions, 26 deletions
diff --git a/src/lib/json/generic.go b/src/lib/json/generic.go
index b57fcb12e..08ae8dc29 100644
--- a/src/lib/json/generic.go
+++ b/src/lib/json/generic.go
@@ -75,7 +75,7 @@ func (j *_Array) Elem(i int) Json {
if i < 0 || i >= j.a.Len() {
return Null
}
- return j.a.At(i)
+ return j.a.At(i).(Json)
}
func (j *_Array) String() string {
s := "[";
@@ -232,7 +232,7 @@ func (b *_JsonBuilder) Get() Json {
case b.ptr != nil:
return *b.ptr;
case b.a != nil:
- return b.a.At(b.i);
+ return b.a.At(b.i).(Json);
case b.m != nil:
return b.m[b.k];
}
diff --git a/src/lib/json/struct.go b/src/lib/json/struct.go
index f37ee461e..82e0a80a7 100644
--- a/src/lib/json/struct.go
+++ b/src/lib/json/struct.go
@@ -122,7 +122,7 @@ func (b *_StructBuilder) Array() {
pv := v.(reflect.PtrValue);
psubtype := pv.Type().(reflect.PtrType).Sub();
if pv.Get() == nil && psubtype.Kind() == reflect.ArrayKind {
- av := reflect.NewSliceValue(psubtype, 0, 8);
+ av := reflect.NewSliceValue(psubtype.(reflect.ArrayType), 0, 8);
pv.SetSub(av);
}
}
@@ -148,7 +148,7 @@ func (b *_StructBuilder) Elem(i int) Builder {
for n <= i {
n *= 2
}
- av1 := reflect.NewSliceValue(av.Type(), av.Len(), n);
+ av1 := reflect.NewSliceValue(av.Type().(reflect.ArrayType), av.Len(), n);
av1.CopyFrom(av, av.Len());
pv.SetSub(av1);
av = av1;
diff --git a/src/lib/reflect/all_test.go b/src/lib/reflect/all_test.go
index a840fc892..f6428fdf3 100644
--- a/src/lib/reflect/all_test.go
+++ b/src/lib/reflect/all_test.go
@@ -270,7 +270,7 @@ func TestAll(tt *testing.T) { // TODO(r): wrap up better
assert(typ.String(), "[]uint32");
t = reflect.ParseTypeString("", "[]int32");
- v := reflect.NewSliceValue(t, 5, 10);
+ v := reflect.NewSliceValue(t.(ArrayType), 5, 10);
t1 := reflect.ParseTypeString("", "*[]int32");
v1 := reflect.NewInitValue(t1);
if v1 == nil { panic("V1 is nil"); }
@@ -308,7 +308,7 @@ func TestCopyArray(t *testing.T) {
}
}
for tocopy := 1; tocopy <= 7; tocopy++ {
- vb.(PtrValue).Sub().(ArrayValue).CopyFrom(va.(PtrValue).Sub(), tocopy);
+ vb.(PtrValue).Sub().(ArrayValue).CopyFrom(va.(PtrValue).Sub().(ArrayValue), tocopy);
for i := 0; i < tocopy; i++ {
if a[i] != b[i] {
t.Errorf("1 tocopy=%d a[%d]=%d, b[%d]=%d",
diff --git a/src/lib/reflect/tostring.go b/src/lib/reflect/tostring.go
index d317e1a68..b2ccfdf48 100644
--- a/src/lib/reflect/tostring.go
+++ b/src/lib/reflect/tostring.go
@@ -105,9 +105,9 @@ func TypeToString(typ Type, expand bool) string {
}
return str + TypeToString(c.Elem(), false);
case StructKind:
- return "struct{" + typeFieldsToString(typ, ";") + "}";
+ return "struct{" + typeFieldsToString(typ.(StructType), ";") + "}";
case InterfaceKind:
- return "interface{" + typeFieldsToString(typ, ";") + "}";
+ return "interface{" + typeFieldsToString(typ.(InterfaceType), ";") + "}";
case FuncKind:
f := typ.(FuncType);
str = "(" + typeFieldsToString(f.In(), ",") + ")";
diff --git a/src/lib/reflect/type.go b/src/lib/reflect/type.go
index b02ad32d7..d949f23f4 100644
--- a/src/lib/reflect/type.go
+++ b/src/lib/reflect/type.go
@@ -149,6 +149,12 @@ func (t *stubType) Get() Type {
// -- Pointer
type PtrType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
Sub() Type
}
@@ -168,6 +174,12 @@ func (t *ptrTypeStruct) Sub() Type {
// -- Array
type ArrayType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
IsSlice() bool;
Len() int;
Elem() Type;
@@ -207,6 +219,12 @@ func (t *arrayTypeStruct) Elem() Type {
// -- Map
type MapType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
Key() Type;
Elem() Type;
}
@@ -232,6 +250,12 @@ func (t *mapTypeStruct) Elem() Type {
// -- Chan
type ChanType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
Dir() int;
Elem() Type;
}
@@ -263,6 +287,12 @@ func (t *chanTypeStruct) Elem() Type {
// -- Struct
type StructType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
Field(int) (name string, typ Type, tag string, offset int);
Len() int;
}
@@ -323,6 +353,12 @@ func (t *structTypeStruct) Len() int {
// -- Interface
type InterfaceType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
Field(int) (name string, typ Type, tag string, offset int);
Len() int;
}
@@ -349,6 +385,12 @@ var nilInterface = newInterfaceTypeStruct("nil", "", make([]structField, 0));
// -- Func
type FuncType interface {
+ // TODO: Type;
+ Kind() int;
+ Name() string;
+ String() string;
+ Size() int;
+
In() StructType;
Out() StructType;
}
diff --git a/src/lib/reflect/value.go b/src/lib/reflect/value.go
index 8a2706e97..6464d7e6d 100644
--- a/src/lib/reflect/value.go
+++ b/src/lib/reflect/value.go
@@ -66,9 +66,11 @@ type creatorFn func(typ Type, addr Addr) Value
// -- Missing
type MissingValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
Addr() Addr;
+ Interface() interface {};
}
type missingValueStruct struct {
@@ -82,10 +84,14 @@ func missingCreator(typ Type, addr Addr) Value {
// -- Int
type IntValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() int;
Set(int);
- Type() Type;
}
type intValueStruct struct {
@@ -107,10 +113,14 @@ func (v *intValueStruct) Set(i int) {
// -- Int8
type Int8Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() int8;
Set(int8);
- Type() Type;
}
type int8ValueStruct struct {
@@ -132,10 +142,14 @@ func (v *int8ValueStruct) Set(i int8) {
// -- Int16
type Int16Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() int16;
Set(int16);
- Type() Type;
}
type int16ValueStruct struct {
@@ -157,10 +171,14 @@ func (v *int16ValueStruct) Set(i int16) {
// -- Int32
type Int32Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() int32;
Set(int32);
- Type() Type;
}
type int32ValueStruct struct {
@@ -182,10 +200,14 @@ func (v *int32ValueStruct) Set(i int32) {
// -- Int64
type Int64Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() int64;
Set(int64);
- Type() Type;
}
type int64ValueStruct struct {
@@ -207,10 +229,14 @@ func (v *int64ValueStruct) Set(i int64) {
// -- Uint
type UintValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uint;
Set(uint);
- Type() Type;
}
type uintValueStruct struct {
@@ -232,10 +258,14 @@ func (v *uintValueStruct) Set(i uint) {
// -- Uint8
type Uint8Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uint8;
Set(uint8);
- Type() Type;
}
type uint8ValueStruct struct {
@@ -257,10 +287,14 @@ func (v *uint8ValueStruct) Set(i uint8) {
// -- Uint16
type Uint16Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uint16;
Set(uint16);
- Type() Type;
}
type uint16ValueStruct struct {
@@ -282,10 +316,14 @@ func (v *uint16ValueStruct) Set(i uint16) {
// -- Uint32
type Uint32Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uint32;
Set(uint32);
- Type() Type;
}
type uint32ValueStruct struct {
@@ -307,10 +345,14 @@ func (v *uint32ValueStruct) Set(i uint32) {
// -- Uint64
type Uint64Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uint64;
Set(uint64);
- Type() Type;
}
type uint64ValueStruct struct {
@@ -332,10 +374,14 @@ func (v *uint64ValueStruct) Set(i uint64) {
// -- Uintptr
type UintptrValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() uintptr;
Set(uintptr);
- Type() Type;
}
type uintptrValueStruct struct {
@@ -357,10 +403,14 @@ func (v *uintptrValueStruct) Set(i uintptr) {
// -- Float
type FloatValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() float;
Set(float);
- Type() Type;
}
type floatValueStruct struct {
@@ -382,10 +432,14 @@ func (v *floatValueStruct) Set(f float) {
// -- Float32
type Float32Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() float32;
Set(float32);
- Type() Type;
}
type float32ValueStruct struct {
@@ -407,10 +461,14 @@ func (v *float32ValueStruct) Set(f float32) {
// -- Float64
type Float64Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() float64;
Set(float64);
- Type() Type;
}
type float64ValueStruct struct {
@@ -432,10 +490,14 @@ func (v *float64ValueStruct) Set(f float64) {
// -- Float80
type Float80Value interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() float80;
Set(float80);
- Type() Type;
}
type float80ValueStruct struct {
@@ -460,10 +522,14 @@ func (v *Float80ValueStruct) Set(f float80) {
// -- String
type StringValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() string;
Set(string);
- Type() Type;
}
type stringValueStruct struct {
@@ -485,10 +551,14 @@ func (v *stringValueStruct) Set(s string) {
// -- Bool
type BoolValue interface {
+ // TODO: Value;
Kind() int;
+ Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() bool;
Set(bool);
- Type() Type;
}
type boolValueStruct struct {
@@ -510,8 +580,12 @@ func (v *boolValueStruct) Set(b bool) {
// -- Pointer
type PtrValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Sub() Value;
Get() Addr;
SetSub(Value);
@@ -547,8 +621,12 @@ func ptrCreator(typ Type, addr Addr) Value {
// Slices and arrays are represented by the same interface.
type ArrayValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
IsSlice() bool;
Len() int;
Cap() int;
@@ -681,8 +759,12 @@ func arrayCreator(typ Type, addr Addr) Value {
// -- Map TODO: finish and test
type MapValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Len() int;
Elem(key Value) Value;
}
@@ -707,8 +789,11 @@ func (v *mapValueStruct) Elem(key Value) Value {
// -- Chan
type ChanValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
}
type chanValueStruct struct {
@@ -722,8 +807,12 @@ func chanCreator(typ Type, addr Addr) Value {
// -- Struct
type StructValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Len() int;
Field(i int) Value;
}
@@ -757,8 +846,12 @@ func structCreator(typ Type, addr Addr) Value {
// -- Interface
type InterfaceValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
+
Get() interface {};
}
@@ -777,8 +870,11 @@ func interfaceCreator(typ Type, addr Addr) Value {
// -- Func
type FuncValue interface {
+ // TODO: Value;
Kind() int;
Type() Type;
+ Addr() Addr;
+ Interface() interface {};
}
type funcValueStruct struct {
@@ -860,7 +956,7 @@ func NewSliceValue(typ ArrayType, len, cap int) ArrayValue {
array.len = uint32(len);
array.cap = uint32(cap);
- return newValueAddr(typ, Addr(array));
+ return newValueAddr(typ, Addr(array)).(ArrayValue);
}
// Works on both slices and arrays