Commit 4793400b authored by Russ Cox's avatar Russ Cox

new reflect library data structures and code declarations

  * use structs instead of interfaces
  * compiler lays out data structures ahead of time,
    so no more parsing of strings.
  * unified reflect data structures with interface
    runtime data structures.
  * richer data structures should enable reflection
    on chans and maps, but not implemented here.

R=r,iant
DELTA=1179  (1179 added, 0 deleted, 0 changed)
OCL=31107
CL=31213
parent eaf6a344
// 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.
package reflect
import (
"runtime";
"strconv";
"strings";
"unsafe";
)
/*
* Copy of data structures from ../runtime/type.go.
* For comments, see the ones in that file.
*
* These data structures are known to the compiler and the runtime.
*
* Putting these types in runtime instead of reflect means that
* reflect doesn't need to be autolinked into every binary, which
* simplifies bootstrapping and package dependencies.
* Unfortunately, it also means that reflect needs its own
* copy in order to access the private fields.
*/
type uncommonType struct
type commonType struct {
size uintptr;
hash uint32;
alg uint8;
align uint8;
fieldAlign uint8;
string *string;
*uncommonType;
}
type method struct {
hash uint32;
name *string;
PkgPath *string;
typ *runtime.Type;
ifn unsafe.Pointer;
tfn unsafe.Pointer;
}
type uncommonType struct {
name *string;
pkgPath *string;
methods []method;
}
// BoolType represents a boolean type.
type BoolType struct {
commonType
}
// Float32Type represents a float32 type.
type Float32Type struct {
commonType
}
// Float64Type represents a float64 type.
type Float64Type struct {
commonType
}
// FloatType represents a float type.
type FloatType struct {
commonType
}
// Int16Type represents an int16 type.
type Int16Type struct {
commonType
}
// Int32Type represents an int32 type.
type Int32Type struct {
commonType
}
// Int64Type represents an int64 type.
type Int64Type struct {
commonType
}
// Int8Type represents an int8 type.
type Int8Type struct {
commonType
}
// IntType represents an int type.
type IntType struct {
commonType
}
// Uint16Type represents a uint16 type.
type Uint16Type struct {
commonType
}
// Uint32Type represents a uint32 type.
type Uint32Type struct {
commonType
}
// Uint64Type represents a uint64 type.
type Uint64Type struct {
commonType
}
// Uint8Type represents a uint8 type.
type Uint8Type struct {
commonType
}
// UintType represents a uint type.
type UintType struct {
commonType
}
// StringType represents a string type.
type StringType struct {
commonType
}
// UintptrType represents a uintptr type.
type UintptrType struct {
commonType
}
// DotDotDotType represents the ... that can
// be used as the type of the final function parameter.
type DotDotDotType struct {
commonType
}
// UnsafePointerType represents an unsafe.Pointer type.
type UnsafePointerType struct {
commonType
}
// ArrayType represents a fixed array type.
type ArrayType struct {
commonType;
elem *runtime.Type;
len uintptr;
}
// SliceType represents a slice type.
type SliceType struct {
commonType;
elem *runtime.Type;
}
// ChanDir represents a channel type's direction.
type ChanDir int
const (
RecvDir ChanDir = 1<<iota;
SendDir;
BothDir = RecvDir | SendDir;
)
// ChanType represents a channel type.
type ChanType struct {
commonType;
elem *runtime.Type;
dir uintptr;
}
// FuncType represents a function type.
type FuncType struct {
commonType;
in []*runtime.Type;
out []*runtime.Type;
}
// Method on interface type
type imethod struct {
hash uint32;
perm uint32;
name *string;
pkgPath *string;
typ *runtime.Type;
}
// InterfaceType represents an interface type.
type InterfaceType struct {
commonType;
methods []imethod;
}
// MapType represents a map type.
type MapType struct {
commonType;
key *runtime.Type;
elem *runtime.Type;
}
// PtrType represents a pointer type.
type PtrType struct {
commonType;
elem *runtime.Type;
}
// Struct field
type structField struct {
name *string;
pkgPath *string;
typ *runtime.Type;
tag *string;
offset uintptr;
}
// StructType represents a struct type.
type StructType struct {
commonType;
fields []structField;
}
/*
* The compiler knows the exact layout of all the data structures above.
* The compiler does not know about the data structures and methods below.
*/
type Type interface
type addr unsafe.Pointer
type FuncValue struct
func newFuncValue(typ Type, addr addr) *FuncValue
// Method represents a single method.
type Method struct {
PkgPath string; // empty for uppercase Name
Name string;
Type *FuncType;
Func *FuncValue;
}
// Type is the runtime representation of a Go type.
// Every type implements the methods listed here.
// Some types implement additional interfaces;
// use a type switch to find out what kind of type a Type is.
// Each type in a program has a unique Type, so == on Types
// corresponds to Go's type equality.
type Type interface {
// Name returns the type's package and name.
// The package is a full package import path like "container/vector".
Name() (pkgPath string, name string);
// String returns a string representation of the type.
// The string representation may use shortened package names
// (e.g., vector instead of "container/vector") and is not
// guaranteed to be unique among types. To test for equality,
// compare the Types directly.
String() string;
// Size returns the number of bytes needed to store
// a value of the given type; it is analogous to unsafe.Sizeof.
Size() uintptr;
// Align returns the alignment of a value of this type
// when allocated in memory.
Align() int;
// FieldAlign returns the alignment of a value of this type
// when used as a field in a struct.
FieldAlign() int;
// For non-interface types, Method returns the i'th method with receiver T.
// For interface types, Method returns the i'th method in the interface.
// NumMethod returns the number of such methods.
Method(int) Method;
NumMethod() int;
}
func toType(i interface{}) Type
func (t *uncommonType) Name() (pkgPath string, name string) {
}
func (t *commonType) String() string {
}
func (t *commonType) Size() uintptr {
}
func (t *commonType) Align() int {
}
func (t *commonType) FieldAlign() int {
}
func (t *uncommonType) Method(i int) (m Method) {
}
func (t *uncommonType) NumMethod() int {
}
// Len returns the number of elements in the array.
func (t *ArrayType) Len() int {
}
// Elem returns the type of the array's elements.
func (t *ArrayType) Elem() Type {
}
// Dir returns the channel direction.
func (t *ChanType) Dir() ChanDir {
}
// Elem returns the channel's element type.
func (t *ChanType) Elem() Type {
}
func (d ChanDir) String() string {
}
// In returns the type of the i'th function input parameter.
func (t *FuncType) In(i int) Type {
}
// NumIn returns the number of input parameters.
func (t *FuncType) NumIn() int {
}
// Out returns the type of the i'th function output parameter.
func (t *FuncType) Out(i int) Type {
}
// NumOut returns the number of function output parameters.
func (t *FuncType) NumOut() int {
}
// Method returns the i'th interface method.
func (t *InterfaceType) Method(i int) (m Method) {
}
// NumMethod returns the number of interface methods.
func (t *InterfaceType) NumMethod() int {
}
// Key returns the map key type.
func (t *MapType) Key() Type {
}
// Elem returns the map element type.
func (t *MapType) Elem() Type {
}
// Elem returns the pointer element type.
func (t *PtrType) Elem() Type {
}
// Elem returns the type of the slice's elements.
func (t *SliceType) Elem() Type {
}
type StructField struct {
PkgPath string; // empty for uppercase Name
Name string;
Type Type;
Tag string;
Offset uintptr;
Anonymous bool;
}
// Field returns the i'th struct field.
func (t *StructType) Field(i int) (f StructField) {
}
// NumField returns the number of struct fields.
func (t *StructType) NumField() int {
}
// ArrayOrSliceType is the common interface implemented
// by both ArrayType and SliceType.
type ArrayOrSliceType interface {
Type;
Elem() Type;
}
// 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.
package reflect
import (
"reflect";
"unsafe";
)
// Value is the common interface to reflection values.
// The implementations of Value (e.g., ArrayValue, StructValue)
// have additional type-specific methods.
type Value interface {
// Type returns the value's type.
Type() Type;
// Interface returns the value as an interface{}.
Interface() interface{};
// CanSet returns whether the value can be changed.
// If CanSet() returns false, calling the type-specific Set
// will cause a crash.
CanSet() bool;
// Addr returns a pointer to the underlying data.
// It is for advanced clients that also
// import the "unsafe" package.
Addr() uintptr;
}
/*
* basic types
*/
// BoolValue represents a bool value.
type BoolValue struct {
}
// Get returns the underlying bool value.
func (v *BoolValue) Get() bool {
}
// Set sets v to the value x.
func (v *BoolValue) Set(x bool) {
}
// FloatValue represents a float value.
type FloatValue struct {
}
// Get returns the underlying float value.
func (v *FloatValue) Get() float {
}
// Set sets v to the value x.
func (v *FloatValue) Set(x float) {
}
// Float32Value represents a float32 value.
type Float32Value struct {
}
// Get returns the underlying float32 value.
func (v *Float32Value) Get() float32 {
}
// Set sets v to the value x.
func (v *Float32Value) Set(x float32) {
}
// Float64Value represents a float64 value.
type Float64Value struct {
}
// Get returns the underlying float64 value.
func (v *Float64Value) Get() float64 {
}
// Set sets v to the value x.
func (v *Float64Value) Set(x float64) {
}
// IntValue represents an int value.
type IntValue struct {
}
// Get returns the underlying int value.
func (v *IntValue) Get() int {
}
// Set sets v to the value x.
func (v *IntValue) Set(x int) {
}
// Int8Value represents an int8 value.
type Int8Value struct {
}
// Get returns the underlying int8 value.
func (v *Int8Value) Get() int8 {
}
// Set sets v to the value x.
func (v *Int8Value) Set(x int8) {
}
// Int16Value represents an int16 value.
type Int16Value struct {
}
// Get returns the underlying int16 value.
func (v *Int16Value) Get() int16 {
}
// Set sets v to the value x.
func (v *Int16Value) Set(x int16) {
}
// Int32Value represents an int32 value.
type Int32Value struct {
}
// Get returns the underlying int32 value.
func (v *Int32Value) Get() int32 {
}
// Set sets v to the value x.
func (v *Int32Value) Set(x int32) {
}
// Int64Value represents an int64 value.
type Int64Value struct {
}
// Get returns the underlying int64 value.
func (v *Int64Value) Get() int64 {
}
// Set sets v to the value x.
func (v *Int64Value) Set(x int64) {
}
// StringValue represents a string value.
type StringValue struct {
}
// Get returns the underlying string value.
func (v *StringValue) Get() string {
}
// Set sets v to the value x.
func (v *StringValue) Set(x string) {
}
// UintValue represents a uint value.
type UintValue struct {
}
// Get returns the underlying uint value.
func (v *UintValue) Get() uint {
}
// Set sets v to the value x.
func (v *UintValue) Set(x uint) {
}
// Uint8Value represents a uint8 value.
type Uint8Value struct {
}
// Get returns the underlying uint8 value.
func (v *Uint8Value) Get() uint8 {
}
// Set sets v to the value x.
func (v *Uint8Value) Set(x uint8) {
}
// Uint16Value represents a uint16 value.
type Uint16Value struct {
}
// Get returns the underlying uint16 value.
func (v *Uint16Value) Get() uint16 {
}
// Set sets v to the value x.
func (v *Uint16Value) Set(x uint16) {
}
// Uint32Value represents a uint32 value.
type Uint32Value struct {
}
// Get returns the underlying uint32 value.
func (v *Uint32Value) Get() uint32 {
}
// Set sets v to the value x.
func (v *Uint32Value) Set(x uint32) {
}
// Uint64Value represents a uint64 value.
type Uint64Value struct {
}
// Get returns the underlying uint64 value.
func (v *Uint64Value) Get() uint64 {
}
// Set sets v to the value x.
func (v *Uint64Value) Set(x uint64) {
}
// UintptrValue represents a uintptr value.
type UintptrValue struct {
}
// Get returns the underlying uintptr value.
func (v *UintptrValue) Get() uintptr {
}
// Set sets v to the value x.
func (v *UintptrValue) Set(x uintptr) {
}
// UnsafePointerValue represents an unsafe.Pointer value.
type UnsafePointerValue struct {
}
// Get returns the underlying uintptr value.
// Get returns uintptr, not unsafe.Pointer, so that
// programs that do not import "unsafe" cannot
// obtain a value of unsafe.Pointer type from "reflect".
func (v *UnsafePointerValue) Get() uintptr {
}
// Set sets v to the value x.
func (v *UnsafePointerValue) Set(x unsafe.Pointer) {
}
/*
* array
*/
// ArrayOrSliceValue is the common interface
// implemented by both ArrayValue and SliceValue.
type ArrayOrSliceValue interface {
Value;
Len() int;
Cap() int;
Elem(i int) Value;
addr() addr;
}
// ArrayCopy copies the contents of src into dst until either
// dst has been filled or src has been exhausted.
// It returns the number of elements copied.
// The arrays dst and src must have the same element type.
func ArrayCopy(dst, src ArrayOrSliceValue) int {
}
// An ArrayValue represents an array.
type ArrayValue struct {
}
// Len returns the length of the array.
func (v *ArrayValue) Len() int {
}
// Cap returns the capacity of the array (equal to Len()).
func (v *ArrayValue) Cap() int {
}
// addr returns the base address of the data in the array.
func (v *ArrayValue) addr() addr {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *ArrayValue) Set(x *ArrayValue) {
}
// Elem returns the i'th element of v.
func (v *ArrayValue) Elem(i int) Value {
}
/*
* slice
*/
// runtime representation of slice
type SliceHeader struct {
Data uintptr;
Len uint32;
Cap uint32;
}
// A SliceValue represents a slice.
type SliceValue struct {
}
func (v *SliceValue) slice() *SliceHeader {
}
// IsNil returns whether v is a nil slice.
func (v *SliceValue) IsNil() bool {
}
// Len returns the length of the slice.
func (v *SliceValue) Len() int {
}
// Cap returns the capacity of the slice.
func (v *SliceValue) Cap() int {
}
// addr returns the base address of the data in the slice.
func (v *SliceValue) addr() addr {
}
// SetLen changes the length of v.
// The new length n must be between 0 and the capacity, inclusive.
func (v *SliceValue) SetLen(n int) {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *SliceValue) Set(x *SliceValue) {
}
// Slice returns a sub-slice of the slice v.
func (v *SliceValue) Slice(beg, end int) *SliceValue {
}
// Elem returns the i'th element of v.
func (v *SliceValue) Elem(i int) Value {
}
// MakeSlice creates a new zero-initialized slice value
// for the specified slice type, length, and capacity.
func MakeSlice(typ *SliceType, len, cap int) *SliceValue {
}
/*
* chan
*/
// A ChanValue represents a chan.
type ChanValue struct {
}
// IsNil returns whether v is a nil channel.
func (v *ChanValue) IsNil() bool {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *ChanValue) Set(x *ChanValue) {
}
// Get returns the uintptr value of v.
// It is mainly useful for printing.
func (v *ChanValue) Get() uintptr {
}
// Send sends x on the channel v.
func (v *ChanValue) Send(x Value) {
}
// Recv receives and returns a value from the channel v.
func (v *ChanValue) Recv() Value {
}
// TrySend attempts to sends x on the channel v but will not block.
// It returns true if the value was sent, false otherwise.
func (v *ChanValue) TrySend(x Value) bool {
}
// TryRecv attempts to receive a value from the channel v but will not block.
// It returns the value if one is received, nil otherwise.
func (v *ChanValue) TryRecv() Value {
}
/*
* func
*/
// A FuncValue represents a function value.
type FuncValue struct {
}
// IsNil returns whether v is a nil function.
func (v *FuncValue) IsNil() bool {
}
// Get returns the uintptr value of v.
// It is mainly useful for printing.
func (v *FuncValue) Get() uintptr {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *FuncValue) Set(x *FuncValue) {
}
// Call calls the function v with input parameters in.
// It returns the function's output parameters as Values.
func (v *FuncValue) Call(in []Value) []Value {
}
/*
* interface
*/
// An InterfaceValue represents an interface value.
type InterfaceValue struct {
}
// No Get because v.Interface() is available.
// IsNil returns whether v is a nil interface value.
func (v *InterfaceValue) IsNil() bool {
}
// Elem returns the concrete value stored in the interface value v.
func (v *InterfaceValue) Elem() Value {
}
// Set assigns x to v.
func (v *InterfaceValue) Set(x interface{}) {
}
/*
* map
*/
// A MapValue represents a map value.
type MapValue struct {
}
// IsNil returns whether v is a nil map value.
func (v *MapValue) IsNil() bool {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *MapValue) Set(x *MapValue) {
}
// Elem returns the value associated with key in the map v.
// It returns nil if key is not found in the map.
func (v *MapValue) Elem(key Value) Value {
}
// Len returns the number of keys in the map v.
func (v *MapValue) Len() int {
}
// Keys returns a slice containing all the keys present in the map,
// in unspecified order.
func (v *MapValue) Keys() []Value {
}
/*
* ptr
*/
// A PtrValue represents a pointer.
type PtrValue struct {
}
// IsNil returns whether v is a nil pointer.
func (v *PtrValue) IsNil() bool {
}
// Get returns the uintptr value of v.
// It is mainly useful for printing.
func (v *PtrValue) Get() uintptr {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *PtrValue) Set(x *PtrValue) {
}
// PointTo changes v to point to x.
func (v *PtrValue) PointTo(x Value) {
}
// Elem returns the value that v points to.
// If v is a nil pointer, Elem returns a nil Value.
func (v *PtrValue) Elem() Value {
}
// Indirect returns the value that v points to.
// If v is a nil pointer, Indirect returns a nil Value.
// If v is not a pointer, Indirect returns v.
func Indirect(v Value) Value {
}
/*
* struct
*/
// A StructValue represents a struct value.
type StructValue struct {
}
// Set assigns x to v.
// The new value x must have the same type as v.
func (v *StructValue) Set(x *StructValue) {
}
// Field returns the i'th field of the struct.
func (v *StructValue) Field(i int) Value {
}
// NumField returns the number of fields in the struct.
func (v *StructValue) NumField() int {
}
/*
* constructors
*/
// Typeof returns the reflection Type of the value in the interface{}.
func Typeof(i interface{}) Type {
}
// NewValue returns a new Value initialized to the concrete value
// stored in the interface i. NewValue(nil) returns nil.
func NewValue(i interface{}) Value {
}
// MakeZeroValue returns a zero Value for the specified Type.
func MakeZero(typ Type) Value {
}
// 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.
/*
* Runtime type representation.
*
* The following files know the exact layout of these
* data structures and must be kept in sync with this file:
*
* ../../cmd/gc/reflect.c
* ../reflect/type.go
* type.h
*/
package runtime
import "unsafe"
// The compiler can only construct empty interface values at
// compile time; non-empty interface values get created
// during initialization. Type is an empty interface
// so that the compiler can lay out references as data.
type Type interface { }
type uncommonType struct
// All types begin with a few common fields needed for
// the interface runtime.
type commonType struct {
size uintptr; // size in bytes
hash uint32; // hash of type; avoids computation in hash tables
alg uint8; // algorithm for copy+hash+cmp (../runtime/runtime.h:/AMEM)
align uint8; // alignment of variable with this type
fieldAlign uint8; // alignment of struct field with this type
string *string; // string form; unnecessary but undeniably useful
*uncommonType; // (relatively) uncommon fields
}
// Method on non-interface type
type method struct {
hash uint32; // hash of name + pkg + typ
name *string; // name of method
pkgPath *string; // nil for exported Names; otherwise import path
typ *Type; // .(*FuncType) underneath
ifn unsafe.Pointer; // fn used in interface call (one-word receiver)
tfn unsafe.Pointer; // fn used for normal method call
}
// uncommonType is present only for types with names or methods
// (if T is a named type, the uncommonTypes for T and *T have methods).
// Using a pointer to this struct reduces the overall size required
// to describe an unnamed type with no methods.
type uncommonType struct {
name *string; // name of type
pkgPath *string; // import path; nil for built-in types like int, string
methods []method; // methods associated with type
}
// BoolType represents a boolean type.
type BoolType commonType
// Float32Type represents a float32 type.
type Float32Type commonType
// Float64Type represents a float64 type.
type Float64Type commonType
// FloatType represents a float type.
type FloatType commonType
// Int16Type represents an int16 type.
type Int16Type commonType
// Int32Type represents an int32 type.
type Int32Type commonType
// Int64Type represents an int64 type.
type Int64Type commonType
// Int8Type represents an int8 type.
type Int8Type commonType
// IntType represents an int type.
type IntType commonType
// Uint16Type represents a uint16 type.
type Uint16Type commonType
// Uint32Type represents a uint32 type.
type Uint32Type commonType
// Uint64Type represents a uint64 type.
type Uint64Type commonType
// Uint8Type represents a uint8 type.
type Uint8Type commonType
// UintType represents a uint type.
type UintType commonType
// StringType represents a string type.
type StringType commonType
// UintptrType represents a uintptr type.
type UintptrType commonType
// DotDotDotType represents the ... that can
// be used as the type of the final function parameter.
type DotDotDotType commonType
// UnsafePointerType represents an unsafe.Pointer type.
type UnsafePointerType commonType
// ArrayType represents a fixed array type.
type ArrayType struct {
commonType;
elem *Type; // array element type
len uintptr;
}
// SliceType represents a slice type.
type SliceType struct {
commonType;
elem *Type; // slice element type
}
// ChanDir represents a channel type's direction.
type ChanDir int
const (
RecvDir ChanDir = 1<<iota; // <-chan
SendDir; // chan<-
BothDir = RecvDir | SendDir; // chan
)
// ChanType represents a channel type.
type ChanType struct {
commonType;
elem *Type; // channel element type
dir uintptr; // channel direction (ChanDir)
}
// FuncType represents a function type.
type FuncType struct {
commonType;
in []*Type; // input parameter types
out []*Type; // output parameter types
}
// Method on interface type
type imethod struct {
hash uint32; // hash of name + pkg + typ; same hash as method
perm uint32; // index of function pointer in interface map
name *string; // name of method
pkgPath *string; // nil for exported Names; otherwise import path
typ *Type; // .(*FuncType) underneath
}
// InterfaceType represents an interface type.
type InterfaceType struct {
commonType;
methods []imethod; // sorted by hash
}
// MapType represents a map type.
type MapType struct {
commonType;
key *Type; // map key type
elem *Type; // map element (value) type
}
// PtrType represents a pointer type.
type PtrType struct {
commonType;
elem *Type; // pointer element (pointed at) type
}
// Struct field
type structField struct {
name *string; // nil for embedded fields
pkgPath *string; // nil for exported Names; otherwise import path
typ *Type; // type of field
tag *string; // nil if no tag
offset uintptr; // byte offset of field within struct
}
// StructType represents a struct type.
type StructType struct {
commonType;
fields []structField; // sorted by offset
}
// 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.
/*
* Runtime type representation; master is type.go
*/
typedef struct CommonType CommonType;
typedef struct UncommonType UncommonType;
typedef struct InterfaceType InterfaceType;
typedef struct Method Method;
typedef struct IMethod IMethod;
struct CommonType
{
uintptr size;
uint32 hash;
uint8 alg;
uint8 align;
uint8 fieldAlign;
String *string;
UncommonType *x;
};
struct Method
{
uint32 hash;
String *name;
String *pkgPath;
Type *typ;
void (*ifn)(void);
void (*tfn)(void);
};
struct UncommonType
{
String *name;
String *pkgPath;
Array mhdr;
Method m[];
};
struct Type
{
void *type; // interface{} value
void *ptr;
CommonType;
};
struct IMethod
{
uint32 hash;
uint32 perm;
String *name;
String *pkgPath;
Type *type;
};
struct InterfaceType
{
Type;
Array mhdr;
IMethod m[];
};
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