Commit fac3dfe6 authored by Rob Pike's avatar Rob Pike

More reflection code.

Beginnings of values.
typestrings are grabbed from the environment.

R=rsc
APPROVED=rsc
DELTA=1046  (952 added, 3 deleted, 91 changed)
OCL=17593
CL=17621
parent 14c63916
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
# license that can be found in the LICENSE file. # license that can be found in the LICENSE file.
# DO NOT EDIT. Automatically generated by gobuild. # DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m reflect tostring.go type.go value.go cast_amd64.s # gobuild -m reflect tostring.go type.go value.go cast_amd64.s\
# typestring.c
O=6 O=6
GC=$(O)g GC=$(O)g
CC=$(O)c -w CC=$(O)c -w
...@@ -33,6 +34,7 @@ clean: ...@@ -33,6 +34,7 @@ clean:
O1=\ O1=\
type.$O\ type.$O\
cast_amd64.$O\ cast_amd64.$O\
typestring.$O\
O2=\ O2=\
value.$O\ value.$O\
......
...@@ -34,15 +34,19 @@ func valuedump(s string) { ...@@ -34,15 +34,19 @@ func valuedump(s string) {
case reflect.Uint64Kind: case reflect.Uint64Kind:
v.(reflect.Uint64Value).Put(64); v.(reflect.Uint64Value).Put(64);
case reflect.Float32Kind: case reflect.Float32Kind:
v.(reflect.Float32Value).Put(320.0); v.(reflect.Float32Value).Put(32.0);
case reflect.Float64Kind: case reflect.Float64Kind:
v.(reflect.Float32Value).Put(640.0); v.(reflect.Float64Value).Put(64.0);
case reflect.StringKind: case reflect.StringKind:
v.(reflect.StringValue).Put("stringy cheese"); v.(reflect.StringValue).Put("stringy cheese");
} }
print(s, " value = ", reflect.ValueToString(v), "\n"); print(s, " value = ", reflect.ValueToString(v), "\n");
} }
export type empty interface {}
export type T struct { a int; b float64 }
func main() { func main() {
var s string; var s string;
var t reflect.Type; var t reflect.Type;
...@@ -82,5 +86,28 @@ func main() { ...@@ -82,5 +86,28 @@ func main() {
valuedump("uint16"); valuedump("uint16");
valuedump("uint32"); valuedump("uint32");
valuedump("uint64"); valuedump("uint64");
valuedump("float32");
valuedump("float64");
valuedump("string"); valuedump("string");
valuedump("*int8");
valuedump("**int8");
valuedump("[32]int32");
valuedump("**P.integer");
valuedump("[32]int32");
valuedump("[]int8");
valuedump("*map[string]int32");
valuedump("*chan<-string");
valuedump("struct {c *chan *int32; d float32}");
valuedump("*(a int8, b int32)");
valuedump("struct {c *(? *chan *P.integer, ? *int8)}");
valuedump("struct {a int8; b int32}");
valuedump("struct {a int8; b int8; b int32}");
valuedump("struct {a int8; b int8; c int8; b int32}");
valuedump("struct {a int8; b int8; c int8; d int8; b int32}");
valuedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}");
v := new(T);
a, b := sys.reflect(v.(empty));
println(a, b);
typedump(b);
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Reflection library. // Reflection library.
// Formatting of types for debugging. // Formatting of types and values for debugging.
package reflect package reflect
...@@ -15,13 +15,17 @@ import ( ...@@ -15,13 +15,17 @@ import (
export func TypeToString(typ Type) string export func TypeToString(typ Type) string
export func ValueToString(val Value) string export func ValueToString(val Value) string
func TypeFieldsToString(t Type, sep string) string { type HasFields interface {
s := t.(StructType); Field(i int) (name string, typ Type, offset uint64);
Len() int;
}
func TypeFieldsToString(t HasFields, sep string) string {
var str string; var str string;
for i := 0; i < s.Len(); i++ { for i := 0; i < t.Len(); i++ {
str1, t := s.Field(i); str1, typ, offset := t.Field(i);
str1 += " " + TypeToString(t); str1 += " " + TypeToString(typ);
if i < s.Len() - 1 { if i < t.Len() - 1 {
str1 += sep + " "; str1 += sep + " ";
} }
str += str1; str += str1;
...@@ -33,7 +37,7 @@ func TypeToString(typ Type) string { ...@@ -33,7 +37,7 @@ func TypeToString(typ Type) string {
var str string; var str string;
switch(typ.Kind()) { switch(typ.Kind()) {
case MissingKind: case MissingKind:
return "missing"; return "$missing$";
case Int8Kind: case Int8Kind:
return "int8"; return "int8";
case Int16Kind: case Int16Kind:
...@@ -63,10 +67,10 @@ func TypeToString(typ Type) string { ...@@ -63,10 +67,10 @@ func TypeToString(typ Type) string {
return "*" + TypeToString(p.Sub()); return "*" + TypeToString(p.Sub());
case ArrayKind: case ArrayKind:
a := typ.(ArrayType); a := typ.(ArrayType);
if a.Len() < 0 { if a.Open() {
str = "[]" str = "[]"
} else { } else {
str = "[" + strings.itoa(a.Len()) + "]" str = "[" + strings.ltoa(int64(a.Len())) + "]"
} }
return str + TypeToString(a.Elem()); return str + TypeToString(a.Elem());
case MapKind: case MapKind:
...@@ -92,8 +96,7 @@ func TypeToString(typ Type) string { ...@@ -92,8 +96,7 @@ func TypeToString(typ Type) string {
return "interface{" + TypeFieldsToString(typ, ";") + "}"; return "interface{" + TypeFieldsToString(typ, ";") + "}";
case FuncKind: case FuncKind:
f := typ.(FuncType); f := typ.(FuncType);
str = "func"; str = "(" + TypeFieldsToString(f.In(), ",") + ")";
str += "(" + TypeFieldsToString(f.In(), ",") + ")";
if f.Out() != nil { if f.Out() != nil {
str += "(" + TypeFieldsToString(f.Out(), ",") + ")"; str += "(" + TypeFieldsToString(f.Out(), ",") + ")";
} }
...@@ -106,7 +109,11 @@ func TypeToString(typ Type) string { ...@@ -106,7 +109,11 @@ func TypeToString(typ Type) string {
// TODO: want an unsigned one too // TODO: want an unsigned one too
func integer(v int64) string { func integer(v int64) string {
return strings.itol(v); return strings.ltoa(v);
}
func floatingpoint(v float64) string {
return strings.dtoa(v);
} }
func ValueToString(val Value) string { func ValueToString(val Value) string {
...@@ -132,16 +139,56 @@ func ValueToString(val Value) string { ...@@ -132,16 +139,56 @@ func ValueToString(val Value) string {
case Uint64Kind: case Uint64Kind:
return integer(int64(val.(Uint64Value).Get())); return integer(int64(val.(Uint64Value).Get()));
case Float32Kind: case Float32Kind:
return "float32"; return floatingpoint(float64(val.(Float32Value).Get()));
case Float64Kind: case Float64Kind:
return "float64"; return floatingpoint(float64(val.(Float64Value).Get()));
case Float80Kind: case Float80Kind:
return "float80"; return "float80";
case StringKind: case StringKind:
return val.(StringValue).Get(); return val.(StringValue).Get();
case PtrKind: case PtrKind:
p := typ.(PtrType); v := val.(PtrValue);
return ValueToString(p.Sub()); return TypeToString(typ) + "(" + integer(int64(v.Addr())) + ")";
case ArrayKind:
t := typ.(ArrayType);
v := val.(ArrayValue);
str += TypeToString(t);
str += "{";
for i := 0; i < v.Len(); i++ {
if i > 0 {
str += ", "
}
str += ValueToString(v.Elem(i));
}
str += "}";
return str;
case MapKind:
t := typ.(MapType);
v := val.(ArrayValue);
str = TypeToString(t);
str += "{";
str += "<can't iterate on maps>";
str += "}";
return str;
case ChanKind:
return "can't print chans yet";
case StructKind:
t := typ.(StructType);
v := val.(StructValue);
str += TypeToString(t); // TODO: use the name?
str += "{";
for i := 0; i < v.Len(); i++ {
if i > 0 {
str += ", "
}
str += ValueToString(v.Field(i));
}
str += "}";
return str;
case InterfaceKind:
return "can't print interfaces yet";
case FuncKind:
return "can't print funcs yet";
default: default:
panicln("reflect.ValueToString: can't print type ", val.Kind()); panicln("reflect.ValueToString: can't print type ", val.Kind());
} }
......
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
extern char gotypestrings[]; // really a go String, but we don't have the definition here
void FLUSH(void *v) { }
void reflect·typestrings(void *s) {
s = gotypestrings;
FLUSH(&s);
}
This diff is collapsed.
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