aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/reflect
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2011-01-21 18:19:03 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2011-01-21 18:19:03 +0000
commitff5f50c52c421d75940ef9392211e3ab24d71332 (patch)
tree27d8768fb1d25696d3c40b42535eb5e073c278da /libgo/go/reflect
parentd6ed1c8903e728f4233122554bab5910853338bd (diff)
downloadgcc-ff5f50c52c421d75940ef9392211e3ab24d71332.zip
gcc-ff5f50c52c421d75940ef9392211e3ab24d71332.tar.gz
gcc-ff5f50c52c421d75940ef9392211e3ab24d71332.tar.bz2
Remove the types float and complex.
Update to current version of Go library. Update testsuite for removed types. * go-lang.c (go_langhook_init): Omit float_type_size when calling go_create_gogo. * go-c.h: Update declaration of go_create_gogo. From-SVN: r169098
Diffstat (limited to 'libgo/go/reflect')
-rw-r--r--libgo/go/reflect/all_test.go90
-rw-r--r--libgo/go/reflect/type.go11
-rw-r--r--libgo/go/reflect/value.go76
3 files changed, 125 insertions, 52 deletions
diff --git a/libgo/go/reflect/all_test.go b/libgo/go/reflect/all_test.go
index 4071a97..320f442 100644
--- a/libgo/go/reflect/all_test.go
+++ b/libgo/go/reflect/all_test.go
@@ -48,7 +48,6 @@ var typeTests = []pair{
{struct{ x uint16 }{}, "uint16"},
{struct{ x uint32 }{}, "uint32"},
{struct{ x uint64 }{}, "uint64"},
- {struct{ x float }{}, "float"},
{struct{ x float32 }{}, "float32"},
{struct{ x float64 }{}, "float64"},
{struct{ x int8 }{}, "int8"},
@@ -244,8 +243,6 @@ func TestSet(t *testing.T) {
}
case *FloatValue:
switch v.Type().Kind() {
- case Float:
- v.Set(128.5)
case Float32:
v.Set(256.25)
case Float64:
@@ -253,8 +250,6 @@ func TestSet(t *testing.T) {
}
case *ComplexValue:
switch v.Type().Kind() {
- case Complex:
- v.Set(53200.0 + 100i)
case Complex64:
v.Set(532.125 + 10i)
case Complex128:
@@ -304,17 +299,13 @@ func TestSetValue(t *testing.T) {
}
case *FloatValue:
switch v.Type().Kind() {
- case Float:
- v.SetValue(NewValue(float(128.5)))
case Float32:
v.SetValue(NewValue(float32(256.25)))
case Float64:
- v.SetValue(NewValue(float64(512.125)))
+ v.SetValue(NewValue(512.125))
}
case *ComplexValue:
switch v.Type().Kind() {
- case Complex:
- v.SetValue(NewValue(complex(53200.0 + 100i)))
case Complex64:
v.SetValue(NewValue(complex64(532.125 + 10i)))
case Complex128:
@@ -470,7 +461,7 @@ func TestInterfaceGet(t *testing.T) {
assert(t, v2.Type().String(), "interface { }")
i2 := v2.(*InterfaceValue).Interface()
v3 := NewValue(i2)
- assert(t, v3.Type().String(), "float")
+ assert(t, v3.Type().String(), "float64")
}
func TestInterfaceValue(t *testing.T) {
@@ -482,11 +473,11 @@ func TestInterfaceValue(t *testing.T) {
v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
assert(t, v2.Type().String(), "interface { }")
v3 := v2.(*InterfaceValue).Elem()
- assert(t, v3.Type().String(), "float")
+ assert(t, v3.Type().String(), "float64")
i3 := v2.Interface()
- if _, ok := i3.(float); !ok {
- t.Error("v2.Interface() did not return float, got ", Typeof(i3))
+ if _, ok := i3.(float64); !ok {
+ t.Error("v2.Interface() did not return float64, got ", Typeof(i3))
}
}
@@ -498,22 +489,67 @@ func TestFunctionValue(t *testing.T) {
assert(t, v.Type().String(), "func()")
}
-func TestCopyArray(t *testing.T) {
+var appendTests = []struct {
+ orig, extra []int
+}{
+ {make([]int, 2, 4), []int{22}},
+ {make([]int, 2, 4), []int{22, 33, 44}},
+}
+
+func TestAppend(t *testing.T) {
+ for i, test := range appendTests {
+ origLen, extraLen := len(test.orig), len(test.extra)
+ want := append(test.orig, test.extra...)
+ // Convert extra from []int to []Value.
+ e0 := make([]Value, len(test.extra))
+ for j, e := range test.extra {
+ e0[j] = NewValue(e)
+ }
+ // Convert extra from []int to *SliceValue.
+ e1 := NewValue(test.extra).(*SliceValue)
+ // Test Append.
+ a0 := NewValue(test.orig).(*SliceValue)
+ have0 := Append(a0, e0...).Interface().([]int)
+ if !DeepEqual(have0, want) {
+ t.Errorf("Append #%d: have %v, want %v", i, have0, want)
+ }
+ // Check that the orig and extra slices were not modified.
+ if len(test.orig) != origLen {
+ t.Errorf("Append #%d origLen: have %v, want %v", i, len(test.orig), origLen)
+ }
+ if len(test.extra) != extraLen {
+ t.Errorf("Append #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
+ }
+ // Test AppendSlice.
+ a1 := NewValue(test.orig).(*SliceValue)
+ have1 := AppendSlice(a1, e1).Interface().([]int)
+ if !DeepEqual(have1, want) {
+ t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want)
+ }
+ // Check that the orig and extra slices were not modified.
+ if len(test.orig) != origLen {
+ t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen)
+ }
+ if len(test.extra) != extraLen {
+ t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
+ }
+ }
+}
+
+func TestCopy(t *testing.T) {
a := []int{1, 2, 3, 4, 10, 9, 8, 7}
b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
- va := NewValue(&a)
- vb := NewValue(&b)
for i := 0; i < len(b); i++ {
if b[i] != c[i] {
t.Fatalf("b != c before test")
}
}
- aa := va.(*PtrValue).Elem().(*SliceValue)
- ab := vb.(*PtrValue).Elem().(*SliceValue)
+ aa := NewValue(a).(*SliceValue)
+ ab := NewValue(b).(*SliceValue)
for tocopy := 1; tocopy <= 7; tocopy++ {
aa.SetLen(tocopy)
- ArrayCopy(ab, aa)
+ Copy(ab, aa)
aa.SetLen(8)
for i := 0; i < tocopy; i++ {
if a[i] != b[i] {
@@ -652,11 +688,11 @@ type _Complex struct {
a int
b [3]*_Complex
c *string
- d map[float]float
+ d map[float64]float64
}
func TestDeepEqualComplexStruct(t *testing.T) {
- m := make(map[float]float)
+ m := make(map[float64]float64)
stra, strb := "hello", "hello"
a, b := new(_Complex), new(_Complex)
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
@@ -667,7 +703,7 @@ func TestDeepEqualComplexStruct(t *testing.T) {
}
func TestDeepEqualComplexStructInequality(t *testing.T) {
- m := make(map[float]float)
+ m := make(map[float64]float64)
stra, strb := "hello", "helloo" // Difference is here
a, b := new(_Complex), new(_Complex)
*a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
@@ -873,7 +909,7 @@ func TestMap(t *testing.T) {
// Check that value lookup is correct.
vv := mv.Elem(NewValue(k))
if vi := vv.(*IntValue).Get(); vi != int64(v) {
- t.Errorf("Key %q: have value %d, want %d", vi, v)
+ t.Errorf("Key %q: have value %d, want %d", k, vi, v)
}
// Copy into new map.
@@ -1272,12 +1308,12 @@ func TestImportPath(t *testing.T) {
func TestDotDotDot(t *testing.T) {
// Test example from FuncType.DotDotDot documentation.
- var f func(x int, y ...float)
+ var f func(x int, y ...float64)
typ := Typeof(f).(*FuncType)
if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
sl, ok := typ.In(1).(*SliceType)
if ok {
- if sl.Elem() == Typeof(float(0)) {
+ if sl.Elem() == Typeof(0.0) {
// ok
return
}
@@ -1285,7 +1321,7 @@ func TestDotDotDot(t *testing.T) {
}
// Failed
- t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float")
+ t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
for i := 0; i < typ.NumIn(); i++ {
s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
diff --git a/libgo/go/reflect/type.go b/libgo/go/reflect/type.go
index 2a93fd3..4ad4c5f 100644
--- a/libgo/go/reflect/type.go
+++ b/libgo/go/reflect/type.go
@@ -265,10 +265,8 @@ const (
Uint32
Uint64
Uintptr
- Float
Float32
Float64
- Complex
Complex64
Complex128
Array
@@ -307,9 +305,10 @@ var kindNames = []string{
Uint32: "uint32",
Uint64: "uint64",
Uintptr: "uintptr",
- Float: "float",
Float32: "float32",
Float64: "float64",
+ Complex64: "complex64",
+ Complex128: "complex128",
Array: "array",
Chan: "chan",
Func: "func",
@@ -551,7 +550,7 @@ func (t *StructType) fieldByNameFunc(match func(string) bool, mark map[*StructTy
var fi int // field index
n := 0 // number of matching fields at depth fd
L:
- for i, _ := range t.fields {
+ for i := range t.fields {
f := t.Field(i)
d := inf
switch {
@@ -702,9 +701,9 @@ func runtimeToType(v *runtime.Type) Type {
r = (*IntType)(unsafe.Pointer(v))
case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
r = (*UintType)(unsafe.Pointer(v))
- case Float, Float32, Float64:
+ case Float32, Float64:
r = (*FloatType)(unsafe.Pointer(v))
- case Complex, Complex64, Complex128:
+ case Complex64, Complex128:
r = (*ComplexType)(unsafe.Pointer(v))
case Array:
r = (*ArrayType)(unsafe.Pointer(v))
diff --git a/libgo/go/reflect/value.go b/libgo/go/reflect/value.go
index eda6feb..8ef402b 100644
--- a/libgo/go/reflect/value.go
+++ b/libgo/go/reflect/value.go
@@ -141,9 +141,7 @@ type FloatValue struct {
// Get returns the underlying int value.
func (v *FloatValue) Get() float64 {
- switch v.typ.(*FloatType).Kind() {
- case Float:
- return float64(*(*float)(v.addr))
+ switch v.typ.Kind() {
case Float32:
return float64(*(*float32)(v.addr))
case Float64:
@@ -157,11 +155,9 @@ func (v *FloatValue) Set(x float64) {
if !v.canSet {
panic(cannotSet)
}
- switch v.typ.(*FloatType).Kind() {
+ switch v.typ.Kind() {
default:
panic("reflect: invalid float kind")
- case Float:
- *(*float)(v.addr) = float(x)
case Float32:
*(*float32)(v.addr) = float32(x)
case Float64:
@@ -190,9 +186,7 @@ type ComplexValue struct {
// Get returns the underlying complex value.
func (v *ComplexValue) Get() complex128 {
- switch v.typ.(*ComplexType).Kind() {
- case Complex:
- return complex128(*(*complex)(v.addr))
+ switch v.typ.Kind() {
case Complex64:
return complex128(*(*complex64)(v.addr))
case Complex128:
@@ -206,11 +200,9 @@ func (v *ComplexValue) Set(x complex128) {
if !v.canSet {
panic(cannotSet)
}
- switch v.typ.(*ComplexType).Kind() {
+ switch v.typ.Kind() {
default:
panic("reflect: invalid complex kind")
- case Complex:
- *(*complex)(v.addr) = complex(x)
case Complex64:
*(*complex64)(v.addr) = complex64(x)
case Complex128:
@@ -228,7 +220,7 @@ type IntValue struct {
// Get returns the underlying int value.
func (v *IntValue) Get() int64 {
- switch v.typ.(*IntType).Kind() {
+ switch v.typ.Kind() {
case Int:
return int64(*(*int)(v.addr))
case Int8:
@@ -248,7 +240,7 @@ func (v *IntValue) Set(x int64) {
if !v.canSet {
panic(cannotSet)
}
- switch v.typ.(*IntType).Kind() {
+ switch v.typ.Kind() {
default:
panic("reflect: invalid int kind")
case Int:
@@ -306,7 +298,7 @@ type UintValue struct {
// Get returns the underlying uuint value.
func (v *UintValue) Get() uint64 {
- switch v.typ.(*UintType).Kind() {
+ switch v.typ.Kind() {
case Uint:
return uint64(*(*uint)(v.addr))
case Uint8:
@@ -328,7 +320,7 @@ func (v *UintValue) Set(x uint64) {
if !v.canSet {
panic(cannotSet)
}
- switch v.typ.(*UintType).Kind() {
+ switch v.typ.Kind() {
default:
panic("reflect: invalid uint kind")
case Uint:
@@ -400,11 +392,57 @@ type ArrayOrSliceValue interface {
addr() addr
}
-// ArrayCopy copies the contents of src into dst until either
+// grow grows the slice s so that it can hold extra more values, allocating
+// more capacity if needed. It also returns the old and new slice lengths.
+func grow(s *SliceValue, extra int) (*SliceValue, int, int) {
+ i0 := s.Len()
+ i1 := i0 + extra
+ if i1 < i0 {
+ panic("append: slice overflow")
+ }
+ m := s.Cap()
+ if i1 <= m {
+ return s.Slice(0, i1), i0, i1
+ }
+ if m == 0 {
+ m = extra
+ } else {
+ for m < i1 {
+ if i0 < 1024 {
+ m += m
+ } else {
+ m += m / 4
+ }
+ }
+ }
+ t := MakeSlice(s.Type().(*SliceType), i1, m)
+ Copy(t, s)
+ return t, i0, i1
+}
+
+// Append appends the values x to a slice s and returns the resulting slice.
+// Each x must have the same type as s' element type.
+func Append(s *SliceValue, x ...Value) *SliceValue {
+ s, i0, i1 := grow(s, len(x))
+ for i, j := i0, 0; i < i1; i, j = i+1, j+1 {
+ s.Elem(i).SetValue(x[j])
+ }
+ return s
+}
+
+// AppendSlice appends a slice t to a slice s and returns the resulting slice.
+// The slices s and t must have the same element type.
+func AppendSlice(s, t *SliceValue) *SliceValue {
+ s, i0, i1 := grow(s, t.Len())
+ Copy(s.Slice(i0, i1), t)
+ return s
+}
+
+// Copy copies the contents of src into dst until either
// dst has been filled or src has been exhausted.
// It returns the number of elements copied.
// The arrays dst and src must have the same element type.
-func ArrayCopy(dst, src ArrayOrSliceValue) int {
+func Copy(dst, src ArrayOrSliceValue) int {
// TODO: This will have to move into the runtime
// once the real gc goes in.
de := dst.Type().(ArrayOrSliceType).Elem()
@@ -439,7 +477,7 @@ func (v *ArrayValue) Set(x *ArrayValue) {
panic(cannotSet)
}
typesMustMatch(v.typ, x.typ)
- ArrayCopy(v, x)
+ Copy(v, x)
}
// Set sets v to the value x.