2008-10-15 18:11:51 -06:00
|
|
|
// 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.
|
|
|
|
|
2008-10-16 17:38:33 -06:00
|
|
|
// Reflection library.
|
|
|
|
// Formatting of types for debugging.
|
|
|
|
|
2008-10-15 18:11:51 -06:00
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
2008-10-16 12:12:02 -06:00
|
|
|
"reflect";
|
|
|
|
"strings";
|
2008-10-15 18:11:51 -06:00
|
|
|
)
|
|
|
|
|
2008-10-17 19:06:29 -06:00
|
|
|
export func TypeToString(typ Type) string
|
|
|
|
export func ValueToString(val Value) string
|
2008-10-16 12:12:02 -06:00
|
|
|
|
2008-10-17 19:06:29 -06:00
|
|
|
func TypeFieldsToString(t Type, sep string) string {
|
2008-10-16 12:12:02 -06:00
|
|
|
s := t.(StructType);
|
|
|
|
var str string;
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
str1, t := s.Field(i);
|
2008-10-17 19:06:29 -06:00
|
|
|
str1 += " " + TypeToString(t);
|
2008-10-16 12:12:02 -06:00
|
|
|
if i < s.Len() - 1 {
|
2008-10-16 19:09:38 -06:00
|
|
|
str1 += sep + " ";
|
2008-10-16 12:12:02 -06:00
|
|
|
}
|
|
|
|
str += str1;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-10-17 19:06:29 -06:00
|
|
|
func TypeToString(typ Type) string {
|
2008-10-16 12:12:02 -06:00
|
|
|
var str string;
|
2008-10-15 18:11:51 -06:00
|
|
|
switch(typ.Kind()) {
|
2008-10-16 17:38:33 -06:00
|
|
|
case MissingKind:
|
|
|
|
return "missing";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Int8Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "int8";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Int16Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "int16";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Int32Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "int32";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Int64Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "int64";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Uint8Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "uint8";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Uint16Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "uint16";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Uint32Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "uint32";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Uint64Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "uint64";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Float32Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "float32";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Float64Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "float64";
|
2008-10-15 18:11:51 -06:00
|
|
|
case Float80Kind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "float80";
|
2008-10-15 18:11:51 -06:00
|
|
|
case StringKind:
|
2008-10-16 12:12:02 -06:00
|
|
|
return "string";
|
2008-10-15 18:11:51 -06:00
|
|
|
case PtrKind:
|
|
|
|
p := typ.(PtrType);
|
2008-10-17 19:06:29 -06:00
|
|
|
return "*" + TypeToString(p.Sub());
|
2008-10-15 18:11:51 -06:00
|
|
|
case ArrayKind:
|
|
|
|
a := typ.(ArrayType);
|
2008-10-16 12:12:02 -06:00
|
|
|
if a.Len() < 0 {
|
|
|
|
str = "[]"
|
2008-10-15 18:11:51 -06:00
|
|
|
} else {
|
2008-10-16 12:12:02 -06:00
|
|
|
str = "[" + strings.itoa(a.Len()) + "]"
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|
2008-10-17 19:06:29 -06:00
|
|
|
return str + TypeToString(a.Elem());
|
2008-10-15 18:11:51 -06:00
|
|
|
case MapKind:
|
|
|
|
m := typ.(MapType);
|
2008-10-17 19:06:29 -06:00
|
|
|
str = "map[" + TypeToString(m.Key()) + "]";
|
|
|
|
return str + TypeToString(m.Elem());
|
2008-10-15 18:11:51 -06:00
|
|
|
case ChanKind:
|
|
|
|
c := typ.(ChanType);
|
|
|
|
switch c.Dir() {
|
|
|
|
case RecvDir:
|
2008-10-16 12:12:02 -06:00
|
|
|
str = "<-chan";
|
2008-10-15 18:11:51 -06:00
|
|
|
case SendDir:
|
2008-10-16 12:12:02 -06:00
|
|
|
str = "chan<-";
|
2008-10-15 18:11:51 -06:00
|
|
|
case BothDir:
|
2008-10-16 12:12:02 -06:00
|
|
|
str = "chan";
|
2008-10-15 18:11:51 -06:00
|
|
|
default:
|
2008-10-17 19:06:29 -06:00
|
|
|
panicln("reflect.TypeToString: unknown chan direction");
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|
2008-10-17 19:06:29 -06:00
|
|
|
return str + TypeToString(c.Elem());
|
2008-10-15 18:11:51 -06:00
|
|
|
case StructKind:
|
2008-10-17 19:06:29 -06:00
|
|
|
return "struct{" + TypeFieldsToString(typ, ";") + "}";
|
2008-10-16 19:09:38 -06:00
|
|
|
case InterfaceKind:
|
2008-10-17 19:06:29 -06:00
|
|
|
return "interface{" + TypeFieldsToString(typ, ";") + "}";
|
2008-10-15 18:11:51 -06:00
|
|
|
case FuncKind:
|
|
|
|
f := typ.(FuncType);
|
2008-10-16 12:12:02 -06:00
|
|
|
str = "func";
|
2008-10-17 19:06:29 -06:00
|
|
|
str += "(" + TypeFieldsToString(f.In(), ",") + ")";
|
2008-10-15 18:11:51 -06:00
|
|
|
if f.Out() != nil {
|
2008-10-17 19:06:29 -06:00
|
|
|
str += "(" + TypeFieldsToString(f.Out(), ",") + ")";
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|
2008-10-16 12:12:02 -06:00
|
|
|
return str;
|
2008-10-15 18:11:51 -06:00
|
|
|
default:
|
2008-10-17 19:06:29 -06:00
|
|
|
panicln("reflect.TypeToString: can't print type ", typ.Kind());
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|
2008-10-17 19:06:29 -06:00
|
|
|
return "reflect.TypeToString: can't happen";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: want an unsigned one too
|
|
|
|
func integer(v int64) string {
|
|
|
|
return strings.itol(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValueToString(val Value) string {
|
|
|
|
var str string;
|
|
|
|
typ := val.Type();
|
|
|
|
switch(val.Kind()) {
|
|
|
|
case MissingKind:
|
|
|
|
return "missing";
|
|
|
|
case Int8Kind:
|
|
|
|
return integer(int64(val.(Int8Value).Get()));
|
|
|
|
case Int16Kind:
|
|
|
|
return integer(int64(val.(Int16Value).Get()));
|
|
|
|
case Int32Kind:
|
|
|
|
return integer(int64(val.(Int32Value).Get()));
|
|
|
|
case Int64Kind:
|
|
|
|
return integer(int64(val.(Int64Value).Get()));
|
|
|
|
case Uint8Kind:
|
|
|
|
return integer(int64(val.(Uint8Value).Get()));
|
|
|
|
case Uint16Kind:
|
|
|
|
return integer(int64(val.(Uint16Value).Get()));
|
|
|
|
case Uint32Kind:
|
|
|
|
return integer(int64(val.(Uint32Value).Get()));
|
|
|
|
case Uint64Kind:
|
|
|
|
return integer(int64(val.(Uint64Value).Get()));
|
|
|
|
case Float32Kind:
|
|
|
|
return "float32";
|
|
|
|
case Float64Kind:
|
|
|
|
return "float64";
|
|
|
|
case Float80Kind:
|
|
|
|
return "float80";
|
|
|
|
case StringKind:
|
|
|
|
return val.(StringValue).Get();
|
|
|
|
case PtrKind:
|
|
|
|
p := typ.(PtrType);
|
|
|
|
return ValueToString(p.Sub());
|
|
|
|
default:
|
|
|
|
panicln("reflect.ValueToString: can't print type ", val.Kind());
|
|
|
|
}
|
|
|
|
return "reflect.ValueToString: can't happen";
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|