2008-10-22 12:02:56 -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.
|
|
|
|
|
|
|
|
// Reflection library.
|
|
|
|
// Handling values.
|
|
|
|
|
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect";
|
2008-12-09 16:41:21 -07:00
|
|
|
"unsafe";
|
2008-10-22 12:02:56 -06:00
|
|
|
)
|
|
|
|
|
2008-12-09 16:41:21 -07:00
|
|
|
type Addr unsafe.pointer // TODO: where are ptrint/intptr etc?
|
2008-11-04 23:54:11 -07:00
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
export type Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
2008-11-12 15:19:39 -07:00
|
|
|
Addr() Addr;
|
2008-11-24 15:51:33 -07:00
|
|
|
Interface() interface {};
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-26 09:28:33 -06:00
|
|
|
// Common fields and functionality for all values
|
|
|
|
|
2008-11-03 17:00:08 -07:00
|
|
|
type Common struct {
|
2008-10-26 09:28:33 -06:00
|
|
|
kind int;
|
|
|
|
typ Type;
|
|
|
|
addr Addr;
|
|
|
|
}
|
|
|
|
|
2008-11-03 17:00:08 -07:00
|
|
|
func (c *Common) Kind() int {
|
2008-10-26 09:28:33 -06:00
|
|
|
return c.kind
|
|
|
|
}
|
|
|
|
|
2008-11-03 17:00:08 -07:00
|
|
|
func (c *Common) Type() Type {
|
2008-10-26 09:28:33 -06:00
|
|
|
return c.typ
|
|
|
|
}
|
|
|
|
|
2008-11-12 15:19:39 -07:00
|
|
|
func (c *Common) Addr() Addr {
|
|
|
|
return c.addr
|
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
func (c *Common) Interface() interface {} {
|
2008-12-09 16:41:21 -07:00
|
|
|
return sys.unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String());
|
2008-11-04 23:54:11 -07:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
func NewValueAddr(typ Type, addr Addr) Value
|
|
|
|
|
|
|
|
type Creator *(typ Type, addr Addr) Value
|
|
|
|
|
|
|
|
|
2008-11-02 13:32:14 -07:00
|
|
|
// -- Missing
|
|
|
|
|
|
|
|
export type MissingValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type MissingValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-11-02 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func MissingCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &MissingValueStruct{ Common{IntKind, typ, addr} }
|
2008-11-02 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Int
|
|
|
|
|
|
|
|
export type IntValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() int;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int);
|
2008-10-29 16:31:02 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func IntCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &IntValueStruct{ Common{IntKind, typ, addr} }
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *IntValueStruct) Get() int {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *IntValueStruct) Set(i int) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Int8
|
|
|
|
|
|
|
|
export type Int8Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() int8;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int8);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Int8ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Int8Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Int8ValueStruct{ Common{Int8Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Int8ValueStruct) Get() int8 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int8)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Int8ValueStruct) Set(i int8) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int8) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int16
|
|
|
|
|
|
|
|
export type Int16Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() int16;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int16);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Int16ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Int16Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Int16ValueStruct{ Common{Int16Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Int16ValueStruct) Get() int16 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int16)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Int16ValueStruct) Set(i int16) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int16) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int32
|
|
|
|
|
|
|
|
export type Int32Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() int32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int32);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Int32ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Int32Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Int32ValueStruct{ Common{Int32Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Int32ValueStruct) Get() int32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Int32ValueStruct) Set(i int32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int32) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Int64
|
|
|
|
|
|
|
|
export type Int64Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() int64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(int64);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Int64ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 18:39:15 -06:00
|
|
|
func Int64Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Int64ValueStruct{ Common{Int64Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Int64ValueStruct) Get() int64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*int64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Int64ValueStruct) Set(i int64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*int64) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Uint
|
|
|
|
|
|
|
|
export type UintValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() uint;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint);
|
2008-10-29 16:31:02 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type UintValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func UintCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &UintValueStruct{ Common{UintKind, typ, addr} }
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *UintValueStruct) Get() uint {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *UintValueStruct) Set(i uint) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint) = i
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Uint8
|
|
|
|
|
|
|
|
export type Uint8Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() uint8;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint8);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Uint8ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Uint8Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Uint8ValueStruct{ Common{Uint8Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Uint8ValueStruct) Get() uint8 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint8)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Uint8ValueStruct) Set(i uint8) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint8) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint16
|
|
|
|
|
|
|
|
export type Uint16Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() uint16;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint16);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Uint16ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Uint16Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Uint16ValueStruct{ Common{Uint16Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Uint16ValueStruct) Get() uint16 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint16)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Uint16ValueStruct) Set(i uint16) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint16) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint32
|
|
|
|
|
|
|
|
export type Uint32Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() uint32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint32);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Uint32ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Uint32Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Uint32ValueStruct{ Common{Uint32Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Uint32ValueStruct) Get() uint32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Uint32ValueStruct) Set(i uint32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint32) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Uint64
|
|
|
|
|
|
|
|
export type Uint64Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() uint64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(uint64);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Uint64ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Uint64Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Uint64ValueStruct{ Common{Uint64Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Uint64ValueStruct) Get() uint64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*uint64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Uint64ValueStruct) Set(i uint64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*uint64) = i
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:31:02 -06:00
|
|
|
// -- Float
|
|
|
|
|
|
|
|
export type FloatValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() float;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float);
|
2008-10-29 16:31:02 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type FloatValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func FloatCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &FloatValueStruct{ Common{FloatKind, typ, addr} }
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FloatValueStruct) Get() float {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float)
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *FloatValueStruct) Set(f float) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float) = f
|
2008-10-29 16:31:02 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Float32
|
|
|
|
|
|
|
|
export type Float32Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() float32;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float32);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Float32ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Float32Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Float32ValueStruct{ Common{Float32Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Float32ValueStruct) Get() float32 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float32)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Float32ValueStruct) Set(f float32) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float32) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Float64
|
|
|
|
|
|
|
|
export type Float64Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() float64;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float64);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Float64ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Float64Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Float64ValueStruct{ Common{Float64Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Float64ValueStruct) Get() float64 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float64)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Float64ValueStruct) Set(f float64) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float64) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Float80
|
|
|
|
|
|
|
|
export type Float80Value interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() float80;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(float80);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Float80ValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func Float80Creator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &Float80ValueStruct{ Common{Float80Kind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
BUG: can't gen code for float80s
|
|
|
|
func (v *Float80ValueStruct) Get() float80 {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*float80)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *Float80ValueStruct) Set(f float80) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*float80) = f
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// -- String
|
|
|
|
|
|
|
|
export type StringValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() string;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(string);
|
2008-10-22 12:02:56 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type StringValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func StringCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &StringValueStruct{ Common{StringKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *StringValueStruct) Get() string {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*string)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *StringValueStruct) Set(s string) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*string) = s
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-31 17:34:47 -06:00
|
|
|
// -- Bool
|
|
|
|
|
|
|
|
export type BoolValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Get() bool;
|
2008-11-13 14:42:59 -07:00
|
|
|
Set(bool);
|
2008-10-31 17:34:47 -06:00
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type BoolValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func BoolCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &BoolValueStruct{ Common{BoolKind, typ, addr} }
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *BoolValueStruct) Get() bool {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*bool)
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *BoolValueStruct) Set(b bool) {
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*bool) = b
|
2008-10-31 17:34:47 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
// -- Pointer
|
|
|
|
|
|
|
|
export type PtrValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
2008-10-24 17:33:29 -06:00
|
|
|
Sub() Value;
|
|
|
|
Get() Addr;
|
2008-11-12 15:19:39 -07:00
|
|
|
SetSub(Value);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type PtrValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-24 17:33:29 -06:00
|
|
|
func (v *PtrValueStruct) Get() Addr {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*Addr)
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-22 17:48:17 -06:00
|
|
|
func (v *PtrValueStruct) Sub() Value {
|
2008-10-24 17:33:29 -06:00
|
|
|
return NewValueAddr(v.typ.(PtrType).Sub(), v.Get());
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-12 15:19:39 -07:00
|
|
|
func (v *PtrValueStruct) SetSub(subv Value) {
|
2008-11-12 20:05:05 -07:00
|
|
|
a := v.typ.(PtrType).Sub().String();
|
|
|
|
b := subv.Type().String();
|
|
|
|
if a != b {
|
|
|
|
panicln("reflect: incompatible types in PtrValue.SetSub:", a, b);
|
|
|
|
}
|
2008-12-09 16:41:21 -07:00
|
|
|
*v.addr.(*Addr) = subv.Addr();
|
2008-11-12 15:19:39 -07:00
|
|
|
}
|
|
|
|
|
2008-10-22 12:02:56 -06:00
|
|
|
func PtrCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &PtrValueStruct{ Common{PtrKind, typ, addr} };
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-26 09:28:33 -06:00
|
|
|
// -- Array
|
2008-10-22 12:02:56 -06:00
|
|
|
|
|
|
|
export type ArrayValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
Open() bool;
|
2008-11-05 11:17:38 -07:00
|
|
|
Len() int;
|
2008-11-13 14:42:59 -07:00
|
|
|
Cap() int;
|
2008-11-05 11:17:38 -07:00
|
|
|
Elem(i int) Value;
|
2008-11-13 14:42:59 -07:00
|
|
|
SetLen(len int);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
2008-10-26 09:28:33 -06:00
|
|
|
|
2008-10-22 17:48:17 -06:00
|
|
|
/*
|
|
|
|
Run-time representation of open arrays looks like this:
|
|
|
|
struct Array {
|
|
|
|
byte* array; // actual data
|
|
|
|
uint32 nel; // number of elements
|
2008-11-13 14:42:59 -07:00
|
|
|
uint32 cap;
|
2008-10-22 17:48:17 -06:00
|
|
|
};
|
|
|
|
*/
|
2008-11-13 14:42:59 -07:00
|
|
|
type RuntimeArray struct {
|
|
|
|
data Addr;
|
|
|
|
len uint32;
|
|
|
|
cap uint32;
|
|
|
|
}
|
|
|
|
|
|
|
|
type OpenArrayValueStruct struct {
|
|
|
|
Common;
|
|
|
|
elemtype Type;
|
|
|
|
elemsize int;
|
|
|
|
array *RuntimeArray;
|
|
|
|
}
|
2008-10-22 12:02:56 -06:00
|
|
|
|
|
|
|
func (v *OpenArrayValueStruct) Open() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2008-11-05 11:17:38 -07:00
|
|
|
func (v *OpenArrayValueStruct) Len() int {
|
2008-11-13 14:42:59 -07:00
|
|
|
return int(v.array.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *OpenArrayValueStruct) Cap() int {
|
|
|
|
return int(v.array.cap);
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *OpenArrayValueStruct) SetLen(len int) {
|
|
|
|
if len > v.Cap() {
|
|
|
|
panicln("reflect: OpenArrayValueStruct.SetLen", len, v.Cap());
|
|
|
|
}
|
|
|
|
v.array.len = uint32(len);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-05 11:17:38 -07:00
|
|
|
func (v *OpenArrayValueStruct) Elem(i int) Value {
|
2008-12-09 16:41:21 -07:00
|
|
|
data_uint := uintptr(v.array.data) + uintptr(i * v.elemsize);
|
|
|
|
return NewValueAddr(v.elemtype, Addr(data_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type FixedArrayValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common;
|
2008-10-22 12:02:56 -06:00
|
|
|
elemtype Type;
|
2008-11-05 11:17:38 -07:00
|
|
|
elemsize int;
|
|
|
|
len int;
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FixedArrayValueStruct) Open() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2008-11-05 11:17:38 -07:00
|
|
|
func (v *FixedArrayValueStruct) Len() int {
|
2008-10-22 12:02:56 -06:00
|
|
|
return v.len
|
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
func (v *FixedArrayValueStruct) Cap() int {
|
|
|
|
return v.len
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *FixedArrayValueStruct) SetLen(len int) {
|
|
|
|
}
|
|
|
|
|
2008-11-05 11:17:38 -07:00
|
|
|
func (v *FixedArrayValueStruct) Elem(i int) Value {
|
2008-12-09 16:41:21 -07:00
|
|
|
data_uint := uintptr(v.addr) + uintptr(i * v.elemsize);
|
|
|
|
return NewValueAddr(v.elemtype, Addr(data_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ArrayCreator(typ Type, addr Addr) Value {
|
|
|
|
arraytype := typ.(ArrayType);
|
|
|
|
if arraytype.Open() {
|
|
|
|
v := new(OpenArrayValueStruct);
|
2008-10-26 09:28:33 -06:00
|
|
|
v.kind = ArrayKind;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.addr = addr;
|
2008-10-22 12:02:56 -06:00
|
|
|
v.typ = typ;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.elemtype = arraytype.Elem();
|
|
|
|
v.elemsize = v.elemtype.Size();
|
2008-12-09 16:41:21 -07:00
|
|
|
v.array = addr.(*RuntimeArray);
|
2008-10-22 12:02:56 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
v := new(FixedArrayValueStruct);
|
2008-10-26 09:28:33 -06:00
|
|
|
v.kind = ArrayKind;
|
2008-10-22 17:48:17 -06:00
|
|
|
v.addr = addr;
|
2008-10-22 12:02:56 -06:00
|
|
|
v.typ = typ;
|
|
|
|
v.elemtype = arraytype.Elem();
|
2008-10-22 17:48:17 -06:00
|
|
|
v.elemsize = v.elemtype.Size();
|
|
|
|
v.len = arraytype.Len();
|
2008-10-22 12:02:56 -06:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Map TODO: finish and test
|
|
|
|
|
|
|
|
export type MapValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
Len() int;
|
|
|
|
Elem(key Value) Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
type MapValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-23 13:41:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func MapCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &MapValueStruct{ Common{MapKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MapValueStruct) Len() int {
|
2008-10-23 13:41:06 -06:00
|
|
|
return 0 // TODO: probably want this to be dynamic
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MapValueStruct) Elem(key Value) Value {
|
|
|
|
panic("map value element");
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Chan
|
|
|
|
|
|
|
|
export type ChanValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChanValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-23 13:41:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func ChanCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &ChanValueStruct{ Common{ChanKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Struct
|
|
|
|
|
|
|
|
export type StructValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
Len() int;
|
|
|
|
Field(i int) Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
type StructValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common;
|
2008-10-22 12:02:56 -06:00
|
|
|
field *[]Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *StructValueStruct) Len() int {
|
|
|
|
return len(v.field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *StructValueStruct) Field(i int) Value {
|
|
|
|
return v.field[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func StructCreator(typ Type, addr Addr) Value {
|
|
|
|
t := typ.(StructType);
|
|
|
|
nfield := t.Len();
|
2008-11-03 17:00:08 -07:00
|
|
|
v := &StructValueStruct{ Common{StructKind, typ, addr}, new([]Value, nfield) };
|
2008-10-22 12:02:56 -06:00
|
|
|
for i := 0; i < nfield; i++ {
|
2008-10-30 18:29:53 -06:00
|
|
|
name, ftype, str, offset := t.Field(i);
|
2008-12-09 16:41:21 -07:00
|
|
|
addr_uint := uintptr(addr) + uintptr(offset);
|
|
|
|
v.field[i] = NewValueAddr(ftype, Addr(addr_uint));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
v.typ = typ;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Interface
|
|
|
|
|
|
|
|
export type InterfaceValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
2008-11-24 15:51:33 -07:00
|
|
|
Get() interface {};
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
type InterfaceValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
func (v *InterfaceValueStruct) Get() interface{} {
|
2008-12-09 16:41:21 -07:00
|
|
|
return *v.addr.(*interface{})
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func InterfaceCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &InterfaceValueStruct{ Common{InterfaceKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- Func
|
|
|
|
|
|
|
|
export type FuncValue interface {
|
|
|
|
Kind() int;
|
|
|
|
Type() Type;
|
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
type FuncValueStruct struct {
|
2008-11-03 17:00:08 -07:00
|
|
|
Common
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:41:06 -06:00
|
|
|
func FuncCreator(typ Type, addr Addr) Value {
|
2008-11-03 17:00:08 -07:00
|
|
|
return &FuncValueStruct{ Common{FuncKind, typ, addr} }
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var creator *map[int] Creator
|
2008-11-10 15:53:40 -07:00
|
|
|
var typecache *map[string] *Type
|
2008-10-22 12:02:56 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
creator = new(map[int] Creator);
|
2008-11-02 13:32:14 -07:00
|
|
|
creator[MissingKind] = &MissingCreator;
|
2008-10-29 16:31:02 -06:00
|
|
|
creator[IntKind] = &IntCreator;
|
2008-10-22 12:02:56 -06:00
|
|
|
creator[Int8Kind] = &Int8Creator;
|
|
|
|
creator[Int16Kind] = &Int16Creator;
|
|
|
|
creator[Int32Kind] = &Int32Creator;
|
|
|
|
creator[Int64Kind] = &Int64Creator;
|
2008-10-29 16:31:02 -06:00
|
|
|
creator[UintKind] = &UintCreator;
|
2008-10-22 12:02:56 -06:00
|
|
|
creator[Uint8Kind] = &Uint8Creator;
|
|
|
|
creator[Uint16Kind] = &Uint16Creator;
|
|
|
|
creator[Uint32Kind] = &Uint32Creator;
|
|
|
|
creator[Uint64Kind] = &Uint64Creator;
|
2008-10-29 16:31:02 -06:00
|
|
|
creator[FloatKind] = &FloatCreator;
|
2008-10-22 12:02:56 -06:00
|
|
|
creator[Float32Kind] = &Float32Creator;
|
|
|
|
creator[Float64Kind] = &Float64Creator;
|
|
|
|
creator[Float80Kind] = &Float80Creator;
|
|
|
|
creator[StringKind] = &StringCreator;
|
2008-10-31 17:34:47 -06:00
|
|
|
creator[BoolKind] = &BoolCreator;
|
2008-10-22 12:02:56 -06:00
|
|
|
creator[PtrKind] = &PtrCreator;
|
|
|
|
creator[ArrayKind] = &ArrayCreator;
|
|
|
|
creator[MapKind] = &MapCreator;
|
|
|
|
creator[ChanKind] = &ChanCreator;
|
|
|
|
creator[StructKind] = &StructCreator;
|
|
|
|
creator[InterfaceKind] = &InterfaceCreator;
|
|
|
|
creator[FuncKind] = &FuncCreator;
|
2008-11-10 15:53:40 -07:00
|
|
|
|
|
|
|
typecache = new(map[string] *Type);
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewValueAddr(typ Type, addr Addr) Value {
|
|
|
|
c, ok := creator[typ.Kind()];
|
|
|
|
if !ok {
|
|
|
|
panicln("no creator for type" , typ.Kind());
|
|
|
|
}
|
|
|
|
return c(typ, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
export func NewInitValue(typ Type) Value {
|
2008-10-22 17:48:17 -06:00
|
|
|
// Some values cannot be made this way.
|
|
|
|
switch typ.Kind() {
|
|
|
|
case FuncKind, ChanKind, MapKind: // must be pointers, at least for now (TODO?)
|
|
|
|
return nil;
|
|
|
|
case ArrayKind:
|
|
|
|
if typ.(ArrayType).Open() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2008-10-22 12:02:56 -06:00
|
|
|
size := typ.Size();
|
|
|
|
if size == 0 {
|
|
|
|
size = 1;
|
|
|
|
}
|
|
|
|
data := new([]uint8, size);
|
2008-12-09 16:41:21 -07:00
|
|
|
return NewValueAddr(typ, Addr(&data[0]));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
|
2008-11-13 14:42:59 -07:00
|
|
|
/*
|
|
|
|
Run-time representation of open arrays looks like this:
|
|
|
|
struct Array {
|
|
|
|
byte* array; // actual data
|
|
|
|
uint32 nel; // number of elements
|
|
|
|
uint32 cap; // allocated number of elements
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
export func NewOpenArrayValue(typ ArrayType, len, cap int) ArrayValue {
|
|
|
|
if !typ.Open() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
array := new(RuntimeArray);
|
|
|
|
size := typ.Elem().Size() * cap;
|
|
|
|
if size == 0 {
|
|
|
|
size = 1;
|
|
|
|
}
|
|
|
|
data := new([]uint8, size);
|
2008-12-09 16:41:21 -07:00
|
|
|
array.data = Addr(&data[0]);
|
2008-11-13 14:42:59 -07:00
|
|
|
array.len = uint32(len);
|
|
|
|
array.cap = uint32(cap);
|
|
|
|
|
2008-12-09 16:41:21 -07:00
|
|
|
return NewValueAddr(typ, Addr(array));
|
2008-11-13 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
2008-11-24 15:51:33 -07:00
|
|
|
export func NewValue(e interface {}) Value {
|
2008-10-22 17:48:17 -06:00
|
|
|
value, typestring := sys.reflect(e);
|
2008-11-10 15:53:40 -07:00
|
|
|
p, ok := typecache[typestring];
|
|
|
|
if !ok {
|
|
|
|
typ := ParseTypeString("", typestring);
|
|
|
|
p = new(Type);
|
|
|
|
*p = typ;
|
|
|
|
typecache[typestring] = p;
|
|
|
|
}
|
2008-10-22 17:48:17 -06:00
|
|
|
// Content of interface is a value; need a permanent copy to take its address
|
|
|
|
// so we can modify the contents. Values contain pointers to 'values'.
|
|
|
|
ap := new(uint64);
|
|
|
|
*ap = value;
|
2008-12-09 16:41:21 -07:00
|
|
|
return NewValueAddr(*p, ap.(Addr));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|