From 16fd3566793b6fabe976dc8cc7aca47a937400b4 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 31 Oct 2008 16:34:47 -0700 Subject: [PATCH] Add support for the basic type "bool". R=r DELTA=51 (51 added, 0 deleted, 0 changed) OCL=18283 CL=18290 --- src/lib/reflect/cast_amd64.s | 10 ++++++++++ src/lib/reflect/gencast.sh | 1 + src/lib/reflect/test.go | 3 +++ src/lib/reflect/tostring.go | 6 ++++++ src/lib/reflect/type.go | 4 ++++ src/lib/reflect/value.go | 27 +++++++++++++++++++++++++++ 6 files changed, 51 insertions(+) diff --git a/src/lib/reflect/cast_amd64.s b/src/lib/reflect/cast_amd64.s index ae04b8b32e8..fe7de5470fd 100644 --- a/src/lib/reflect/cast_amd64.s +++ b/src/lib/reflect/cast_amd64.s @@ -161,3 +161,13 @@ TEXT reflect·PtrStringToAddr(SB),7,$-8 MOVQ AX, 16(SP) RET +TEXT reflect·AddrToPtrBool(SB),7,$-8 + MOVQ 8(SP), AX + MOVQ AX, 16(SP) + RET + +TEXT reflect·PtrBoolToAddr(SB),7,$-8 + MOVQ 8(SP), AX + MOVQ AX, 16(SP) + RET + diff --git a/src/lib/reflect/gencast.sh b/src/lib/reflect/gencast.sh index d33bc11fb18..f3b73f939eb 100755 --- a/src/lib/reflect/gencast.sh +++ b/src/lib/reflect/gencast.sh @@ -36,4 +36,5 @@ Float32 Float64 Float80 String +Bool ! diff --git a/src/lib/reflect/test.go b/src/lib/reflect/test.go index 9ec22d1ac44..864220d4e2f 100644 --- a/src/lib/reflect/test.go +++ b/src/lib/reflect/test.go @@ -77,6 +77,8 @@ func valuedump(s, t string) { v.(reflect.Float64Value).Put(64.0); case reflect.StringKind: v.(reflect.StringValue).Put("stringy cheese"); + case reflect.BoolKind: + v.(reflect.BoolValue).Put(true); } assert(reflect.ValueToString(v), t); } @@ -132,6 +134,7 @@ func main() { valuedump("float32", "+3.200000e+01"); valuedump("float64", "+6.400000e+01"); valuedump("string", "stringy cheese"); + valuedump("bool", "true"); valuedump("*int8", "*int8(0)"); valuedump("**int8", "**int8(0)"); valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}"); diff --git a/src/lib/reflect/tostring.go b/src/lib/reflect/tostring.go index 963ab0fbb3e..f35caded97d 100644 --- a/src/lib/reflect/tostring.go +++ b/src/lib/reflect/tostring.go @@ -162,6 +162,12 @@ func ValueToString(val Value) string { return "float80"; case StringKind: return val.(StringValue).Get(); + case BoolKind: + if val.(BoolValue).Get() { + return "true" + } else { + return "false" + } case PtrKind: v := val.(PtrValue); return TypeToString(typ, false) + "(" + integer(int64(v.Get())) + ")"; diff --git a/src/lib/reflect/type.go b/src/lib/reflect/type.go index 3e5179190cd..d4bc2638708 100644 --- a/src/lib/reflect/type.go +++ b/src/lib/reflect/type.go @@ -16,6 +16,7 @@ export func typestrings() string // implemented in C; declared here export const ( MissingKind = iota; ArrayKind; + BoolKind; ChanKind; FloatKind; Float32Kind; @@ -82,6 +83,7 @@ func NewBasicType(name string, kind int, size uint64) Type { // Prebuilt basic types export var ( Missing = NewBasicType(MissingString, MissingKind, 1); + Bool = NewBasicType("bool", BoolKind, 1); // TODO: need to know how big a bool is Int = NewBasicType("int", IntKind, 4); // TODO: need to know how big an int is Int8 = NewBasicType("int8", Int8Kind, 1); Int16 = NewBasicType("int16", Int16Kind, 2); @@ -409,6 +411,7 @@ func init() { types["float64"] = &Float64; types["float80"] = &Float80; types["string"] = &String; + types["bool"] = &Bool; // Basics get prebuilt stubs MissingStub = NewStubType(MissingString, Missing); @@ -428,6 +431,7 @@ func init() { basicstub["float64"] = NewStubType("float64", Float64); basicstub["float80"] = NewStubType("float80", Float80); basicstub["string"] = NewStubType("string", String); + basicstub["bool"] = NewStubType("bool", Bool); Unlock(); } diff --git a/src/lib/reflect/value.go b/src/lib/reflect/value.go index 9f3e4ab7df6..554da2d53e7 100644 --- a/src/lib/reflect/value.go +++ b/src/lib/reflect/value.go @@ -58,6 +58,7 @@ func AddrToPtrFloat32(Addr) *float32 func AddrToPtrFloat64(Addr) *float64 func AddrToPtrFloat80(Addr) *float80 func AddrToPtrString(Addr) *string +func AddrToPtrBool(Addr) *bool // -- Int @@ -438,6 +439,31 @@ func (v *StringValueStruct) Put(s string) { *AddrToPtrString(v.addr) = s } +// -- Bool + +export type BoolValue interface { + Kind() int; + Get() bool; + Put(bool); + Type() Type; +} + +type BoolValueStruct struct { + CommonV +} + +func BoolCreator(typ Type, addr Addr) Value { + return &BoolValueStruct{ CommonV{BoolKind, typ, addr} } +} + +func (v *BoolValueStruct) Get() bool { + return *AddrToPtrBool(v.addr) +} + +func (v *BoolValueStruct) Put(b bool) { + *AddrToPtrBool(v.addr) = b +} + // -- Pointer export type PtrValue interface { @@ -665,6 +691,7 @@ func init() { creator[Float64Kind] = &Float64Creator; creator[Float80Kind] = &Float80Creator; creator[StringKind] = &StringCreator; + creator[BoolKind] = &BoolCreator; creator[PtrKind] = &PtrCreator; creator[ArrayKind] = &ArrayCreator; creator[MapKind] = &MapCreator;