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 @@
# license that can be found in the LICENSE file.
# 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
GC=$(O)g
CC=$(O)c -w
......@@ -33,6 +34,7 @@ clean:
O1=\
type.$O\
cast_amd64.$O\
typestring.$O\
O2=\
value.$O\
......
......@@ -34,19 +34,23 @@ func valuedump(s string) {
case reflect.Uint64Kind:
v.(reflect.Uint64Value).Put(64);
case reflect.Float32Kind:
v.(reflect.Float32Value).Put(320.0);
v.(reflect.Float32Value).Put(32.0);
case reflect.Float64Kind:
v.(reflect.Float32Value).Put(640.0);
v.(reflect.Float64Value).Put(64.0);
case reflect.StringKind:
v.(reflect.StringValue).Put("stringy cheese");
}
print(s, " value = ", reflect.ValueToString(v), "\n");
}
export type empty interface {}
export type T struct { a int; b float64 }
func main() {
var s string;
var t reflect.Type;
typedump("int8");
typedump("int16");
typedump("int32");
......@@ -82,5 +86,28 @@ func main() {
valuedump("uint16");
valuedump("uint32");
valuedump("uint64");
valuedump("float32");
valuedump("float64");
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 @@
// license that can be found in the LICENSE file.
// Reflection library.
// Formatting of types for debugging.
// Formatting of types and values for debugging.
package reflect
......@@ -15,13 +15,17 @@ import (
export func TypeToString(typ Type) string
export func ValueToString(val Value) string
func TypeFieldsToString(t Type, sep string) string {
s := t.(StructType);
type HasFields interface {
Field(i int) (name string, typ Type, offset uint64);
Len() int;
}
func TypeFieldsToString(t HasFields, sep string) string {
var str string;
for i := 0; i < s.Len(); i++ {
str1, t := s.Field(i);
str1 += " " + TypeToString(t);
if i < s.Len() - 1 {
for i := 0; i < t.Len(); i++ {
str1, typ, offset := t.Field(i);
str1 += " " + TypeToString(typ);
if i < t.Len() - 1 {
str1 += sep + " ";
}
str += str1;
......@@ -33,7 +37,7 @@ func TypeToString(typ Type) string {
var str string;
switch(typ.Kind()) {
case MissingKind:
return "missing";
return "$missing$";
case Int8Kind:
return "int8";
case Int16Kind:
......@@ -63,10 +67,10 @@ func TypeToString(typ Type) string {
return "*" + TypeToString(p.Sub());
case ArrayKind:
a := typ.(ArrayType);
if a.Len() < 0 {
if a.Open() {
str = "[]"
} else {
str = "[" + strings.itoa(a.Len()) + "]"
str = "[" + strings.ltoa(int64(a.Len())) + "]"
}
return str + TypeToString(a.Elem());
case MapKind:
......@@ -92,8 +96,7 @@ func TypeToString(typ Type) string {
return "interface{" + TypeFieldsToString(typ, ";") + "}";
case FuncKind:
f := typ.(FuncType);
str = "func";
str += "(" + TypeFieldsToString(f.In(), ",") + ")";
str = "(" + TypeFieldsToString(f.In(), ",") + ")";
if f.Out() != nil {
str += "(" + TypeFieldsToString(f.Out(), ",") + ")";
}
......@@ -106,7 +109,11 @@ func TypeToString(typ Type) string {
// TODO: want an unsigned one too
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 {
......@@ -132,16 +139,56 @@ func ValueToString(val Value) string {
case Uint64Kind:
return integer(int64(val.(Uint64Value).Get()));
case Float32Kind:
return "float32";
return floatingpoint(float64(val.(Float32Value).Get()));
case Float64Kind:
return "float64";
return floatingpoint(float64(val.(Float64Value).Get()));
case Float80Kind:
return "float80";
case StringKind:
return val.(StringValue).Get();
case PtrKind:
p := typ.(PtrType);
return ValueToString(p.Sub());
v := val.(PtrValue);
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:
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