Documentation ¶
Index ¶
- Constants
- Variables
- func FromEmulatedInterface(v Value) (Value, Type)
- func GensymAnonymous(name string) string
- func GensymPrivate(name string) string
- func IsEmulatedInterface(t Type) bool
- func IsGoUntypedKind(gkind types.BasicKind) bool
- func QLess(p, q QName) bool
- func ReflectUnderlying(rtype r.Type) r.Type
- func ToBasicKind(kind r.Kind, untyped bool) types.BasicKind
- func ToReflectKind(gkind types.BasicKind) r.Kind
- func ToReflectValues(v []Value) []r.Value
- type Error
- type Forward
- type Importer
- type InterfaceHeader
- type Key
- type Kind
- type Method
- type Option
- type Package
- type QName
- type QNameI
- type QNameI_github_com_cosmos72_gomacro_xreflect
- type SelectCase
- type SelectDir
- type StructField
- type Type
- func (t Type) AddMethod(name string, signature Type) int
- func (t Type) Align() int
- func (t Type) AssignableTo(u Type) bool
- func (t Type) Bits() int
- func (t Type) ChanDir() r.ChanDir
- func (t Type) Comparable() bool
- func (t Type) Complete() Type
- func (t Type) ConvertibleTo(u Type) bool
- func (t Type) Elem() Type
- func (t Type) Field(i int) StructField
- func (t Type) FieldAlign() int
- func (t Type) FieldByName(name, pkgpath string) (field StructField, count int)
- func (t Type) GetMethods() *[]r.Value
- func (t Type) GetUserData(key interface{}) (interface{}, bool)
- func (t Type) GoType() types.Type
- func (t Type) IdenticalTo(u Type) bool
- func (t Type) Implements(u Type) bool
- func (t Type) In(i int) Type
- func (t Type) IsMethod() bool
- func (t Type) IsVariadic() bool
- func (t Type) Key() Type
- func (t Type) Kind() r.Kind
- func (t Type) Len() int
- func (t Type) Method(i int) Method
- func (t Type) MethodByName(name, pkgpath string) (method Method, count int)
- func (t Type) Name() string
- func (t Type) Named() bool
- func (t Type) NumAllMethod() int
- func (t Type) NumExplicitMethod() int
- func (t Type) NumField() int
- func (t Type) NumIn() int
- func (t Type) NumMethod() int
- func (t Type) NumOut() int
- func (t Type) Out(i int) Type
- func (t Type) Pkg() *Package
- func (t Type) PkgName() string
- func (t Type) PkgPath() string
- func (t Type) ReflectType() r.Type
- func (t Type) Resolve() Type
- func (t Type) SetUnderlying(underlying Type)
- func (t Type) SetUserData(key, value interface{})
- func (t Type) Size() uintptr
- func (t Type) String() string
- func (t Type) Universe() *Universe
- func (t Type) UnsafeForceReflectType(rtype r.Type)
- type Types
- type Universe
- func (v *Universe) ArrayOf(count int, elem Type) Type
- func (v *Universe) CachePackage(pkg *types.Package)
- func (v *Universe) ChanOf(dir r.ChanDir, elem Type) Type
- func (v *Universe) FromReflectType(rtype r.Type) Type
- func (v *Universe) FuncOf(in []Type, out []Type, variadic bool) Type
- func (v *Universe) InterfaceOf(pkg *Package, methodnames []string, methodtypes []Type, embeddeds []Type) Type
- func (v *Universe) InvalidateCache()
- func (v *Universe) InvalidateMethodCache()
- func (v *Universe) LoadPackage(path string) *Package
- func (v *Universe) MakeType(gtype types.Type, rtype r.Type, opt Option) Type
- func (v *Universe) MapOf(key, elem Type) Type
- func (v *Universe) MethodOf(recv Type, in []Type, out []Type, variadic bool) Type
- func (v *Universe) NamedOf(name, pkgpath string) Type
- func (v *Universe) PtrTo(elem Type) Type
- func (v *Universe) SliceOf(elem Type) Type
- func (v *Universe) StructOf(fields []StructField) Type
- func (v *Universe) TypeOf(rvalue interface{}) Type
- func (v *Universe) VisitFields(t Type, visitor func(StructField))
- type Value
- func Append(s Value, x ...Value) Value
- func EmulatedInterfaceGetMethod(obj Value, index int) Value
- func FromReflectValues(rv []r.Value) []Value
- func MakeChan(t Type, n int) Value
- func MakeFunc(t Type, fn func([]Value) []Value) Value
- func MakeMap(t Type) Value
- func MakeMapWithSize(t Type, n int) Value
- func MakeSlice(t Type, len, cap int) Value
- func MakeValue(rv r.Value) Value
- func New(t Type) Value
- func NewR(rt r.Type) Value
- func Select(cases []SelectCase) (chosen int, recv Value, ok bool)
- func ToEmulatedInterface(rtypeinterf r.Type, v Value, t Type, obj2methods []func(Value) Value) Value
- func ValueOf(x interface{}) Value
- func Zero(t Type) Value
- func ZeroR(typ r.Type) Value
- func (v Value) Addr() Value
- func (v Value) Bool() bool
- func (v Value) Bytes() []byte
- func (v Value) Call(in []Value) []Value
- func (v Value) CallSlice(in []Value) []Value
- func (v Value) CanAddr() bool
- func (v Value) CanInterface() bool
- func (v Value) CanSet() bool
- func (v Value) Cap() int
- func (v Value) Close()
- func (v Value) Complex() complex128
- func (v Value) Convert(rt r.Type) Value
- func (v Value) Elem() Value
- func (v Value) Field(i int) Value
- func (v Value) FieldByIndex(index []int) Value
- func (v Value) FieldByName(name string) Value
- func (v Value) FieldByNameFunc(match func(string) bool) Value
- func (v Value) Float() float64
- func (v Value) Index(i int) Value
- func (v Value) Int() int64
- func (v Value) Interface() interface{}
- func (v Value) InterfaceData() [2]uintptr
- func (v Value) IsNil() bool
- func (v Value) IsValid() bool
- func (v Value) IsZero() bool
- func (v Value) Kind() r.Kind
- func (v Value) Len() int
- func (v Value) MapIndex(key Value) Value
- func (v Value) MapKeys() []Value
- func (v Value) Method(i int) Value
- func (v Value) MethodByName(name string) Value
- func (v Value) NumField() int
- func (v Value) NumMethod() int
- func (v Value) OverflowComplex(x complex128) bool
- func (v Value) OverflowFloat(x float64) bool
- func (v Value) OverflowInt(x int64) bool
- func (v Value) OverflowUint(x uint64) bool
- func (v Value) Pointer() uintptr
- func (v Value) Recv() (x Value, ok bool)
- func (v Value) ReflectValue() r.Value
- func (v Value) Send(x Value)
- func (v Value) Set(x Value)
- func (v Value) SetBool(x bool)
- func (v Value) SetBytes(x []byte)
- func (v Value) SetCap(n int)
- func (v Value) SetComplex(x complex128)
- func (v Value) SetFloat(x float64)
- func (v Value) SetInt(x int64)
- func (v Value) SetLen(n int)
- func (v Value) SetMapIndex(key, elem Value)
- func (v Value) SetString(x string)
- func (v Value) SetUint(x uint64)
- func (v Value) Slice(i, j int) Value
- func (v Value) Slice3(i, j, k int) Value
- func (v Value) String() string
- func (v Value) TryRecv() (x Value, ok bool)
- func (v Value) TrySend(x Value) bool
- func (v Value) Type() r.Type
- func (v Value) Uint() uint64
Constants ¶
const ( StrGensymInterface = "\U0001202A" // name of extra struct field needed by the interpreter when creating interpreted interfaces StrGensymPrivate = "\U00012038" // prefix to generate names for unexported struct fields. StrGensymAnonymous = "\U00012039" // prefix to generate names for anonymous struct fields. )
the following constants must match with github.com/cosmos72/gomacro/base/constants.go
const MaxDepth = int(^uint(0) >> 1)
Variables ¶
var ConstrainedInterfaceReceiverType genericV2InterfaceReceiverType
var ReflectBasicTypes = rbasictypes
Functions ¶
func FromEmulatedInterface ¶
extract the concrete value and type contained in an emulated interface
func GensymAnonymous ¶
func GensymPrivate ¶
func IsEmulatedInterface ¶
func IsGoUntypedKind ¶
func ReflectUnderlying ¶
best-effort implementation of missing reflect.Type.Underlying()
func ToReflectValues ¶
Types ¶
type Importer ¶
type Importer struct { // converts from go/types to github.com/cosmos72/gomacro/go/types Converter types.Converter // contains filtered or unexported fields }
func DefaultImporter ¶
func DefaultImporter() *Importer
func (*Importer) ImportFrom ¶
type InterfaceHeader ¶
type InterfaceHeader struct {
// contains filtered or unexported fields
}
InterfaceHeader is the internal header of interpreted interfaces
func MakeInterfaceHeader ¶
func MakeInterfaceHeader(val Value, typ Type) InterfaceHeader
func (InterfaceHeader) Type ¶
func (h InterfaceHeader) Type() Type
func (InterfaceHeader) Value ¶
func (h InterfaceHeader) Value() Value
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key is a Type wrapper suitable for use with operator == and as map[T1]T2 key
type Kind ¶
re-export reflect.Kind from this package
const ( Invalid Kind = r.Invalid Bool Kind = r.Bool Int Kind = r.Int Int8 Kind = r.Int8 Int16 Kind = r.Int16 Int32 Kind = r.Int32 Int64 Kind = r.Int64 Uint Kind = r.Uint Uint8 Kind = r.Uint8 Uint16 Kind = r.Uint16 Uint32 Kind = r.Uint32 Uint64 Kind = r.Uint64 Uintptr Kind = r.Uintptr Float32 Kind = r.Float32 Float64 Kind = r.Float64 Complex64 Kind = r.Complex64 Complex128 Kind = r.Complex128 Array Kind = r.Array Chan Kind = r.Chan Func Kind = r.Func Interface Kind = r.Interface Map Kind = r.Map Ptr Kind = r.Ptr Slice Kind = r.Slice String Kind = r.String Struct Kind = r.Struct UnsafePointer Kind = r.UnsafePointer )
type Method ¶
type Method struct { Name string Pkg *Package Type Type // method type Funs *[]r.Value // (*Funs)[Index] is the method, with receiver as first argument Index int // index for Type.Method FieldIndex []int // embedded fields index sequence for r.Type.FieldByIndex or r.Value.FieldByIndex GoFun *types.Func // for completeness }
func MissingMethod ¶
return one of the methods defined by interface tinterf but missing from t
type QName ¶
type QName struct {
// contains filtered or unexported fields
}
QName is a replacement for go/types.Id and implements accurate comparison of type names, field names and method names. It recognizes unexported names, and names declared in different packages.
To compare two names, build two QNames with the functions QName* then compare the two QName structs with ==
type QNameI_github_com_cosmos72_gomacro_xreflect ¶
type QNameI_github_com_cosmos72_gomacro_xreflect struct { Object interface{} Name_ func() string PkgPath_ func() string }
--------------- proxy for github.com/cosmos72/gomacro/xreflect.QNameI ---------------
func (*QNameI_github_com_cosmos72_gomacro_xreflect) Name ¶
func (Proxy *QNameI_github_com_cosmos72_gomacro_xreflect) Name() string
func (*QNameI_github_com_cosmos72_gomacro_xreflect) PkgPath ¶
func (Proxy *QNameI_github_com_cosmos72_gomacro_xreflect) PkgPath() string
type SelectCase ¶
type SelectCase = r.SelectCase
type SelectDir ¶
re-export reflect.SelectDir from this package
const ( SelectSend SelectDir = r.SelectSend SelectRecv SelectDir = r.SelectRecv SelectDefault SelectDir = r.SelectDefault )
type StructField ¶
type StructField struct { // Name is the field name. If empty, it will be computed from Type name, and Anonymous will be set to true Name string // Pkg is the package that qualifies a lower case (unexported) // field name. It may be nil for upper case (exported) field names. // See https://golang.org/ref/spec#Uniqueness_of_identifiers Pkg *Package Type Type // field type Tag r.StructTag // field tag string Offset uintptr // offset within struct, in bytes. meaningful only if all Deref[] are false Index []int // index sequence for r.Type.FieldByIndex or r.Value.FieldByIndex Anonymous bool // is an embedded field. If true, Name should be empty or equal to the type's name }
type Type ¶
type Type func(z) *xtype
Type:s must be compared with IdenticalTo, not with == produce compile-time error on == between Type:s but allow comparison with nil
func (Type) AddMethod ¶
AddMethod adds method with given name and signature to type, unless it is already in the method list. It panics if the type is unnamed, or if the signature is not a function-with-receiver type. Returns the method index, or < 0 in case of errors
func (Type) Align ¶
Align returns the alignment in bytes of a value of this type when allocated in memory.
func (Type) AssignableTo ¶
AssignableTo reports whether a value of the type is assignable to type u.
func (Type) Bits ¶
Bits returns the size of the type in bits. It panics if the type's Kind is not one of the sized or unsized Int, Uint, Float, or Complex kinds.
func (Type) ChanDir ¶
ChanDir returns a channel type's direction. It panics if the type's Kind is not Chan.
func (Type) Comparable ¶
Comparable reports whether values of this type are comparable.
func (Type) Complete ¶
Complete marks an interface type as complete and computes wrapper methods for embedded fields. It must be called by users of InterfaceOf after the interface's embedded types are fully defined and before using the interface type in any way other than to form other types. Complete returns the receiver.
func (Type) ConvertibleTo ¶
ConvertibleTo reports whether a value of the type is convertible to type u.
func (Type) Elem ¶
Elem returns a type's element type. It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
func (Type) Field ¶
func (t Type) Field(i int) StructField
Field returns a struct type's i-th field. It panics if the type's Kind is not Struct. It panics if i is not in the range [0, NumField()).
func (Type) FieldAlign ¶
FieldAlign returns the alignment in bytes of a value of this type when used as a field in a struct.
func (Type) FieldByName ¶
func (t Type) FieldByName(name, pkgpath string) (field StructField, count int)
FieldByName returns the (possibly embedded) struct field with the given name and the number of fields found at the same (shallowest) depth: 0 if not found. Private fields are returned only if they were declared in pkgpath.
func (Type) GetMethods ¶
GetMethods returns the pointer to the method values. It panics if the type is unnamed
func (Type) GetUserData ¶
GetUserData returns the user-supplied data of the type.
func (Type) IdenticalTo ¶
Identical reports whether the type is identical to type u.
func (Type) Implements ¶
Implements reports whether the type implements the interface type u. It panics if u's Kind is not Interface
func (Type) In ¶
In returns the type of a function type's i'th input parameter. It panics if the type's Kind is not Func. It panics if i is not in the range [0, NumIn()).
func (Type) IsMethod ¶
IsMethod reports whether a function type's contains a receiver, i.e. is a method. If IsMethod returns true, the actual receiver type is available as the first parameter, i.e. Type.In(0) It panics if the type's Kind is not Func.
func (Type) IsVariadic ¶
IsVariadic reports whether a function type's final input parameter is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's implicit actual type []T. IsVariadic panics if the type's Kind is not Func.
func (Type) Method ¶
For interfaces, Method returns the i-th method, including methods from embedded interfaces. For all other named types, Method returns the i-th explicitly declared method, ignoring wrapper methods for embedded fields. It panics if i is outside the range 0 .. NumMethod()-1
func (Type) MethodByName ¶
MethodByName returns the method with given name (including wrapper methods for embedded fields) and the number of methods found at the same (shallowest) depth: 0 if not found. Private methods are returned only if they were declared in pkgpath.
func (Type) Name ¶
Name returns the type's name within its package. It returns an empty string for unnamed types.
func (Type) NumAllMethod ¶
NumAllMethod returns the *total* number of methods for interface or named type t, including wrapper methods for embedded fields or embedded interfaces. Note: it has slightly different semantics from go/types.(*Named).NumMethods(),
since the latter returns 0 for named interfaces, and callers need to manually invoke goNamedType.Underlying().NumMethods() to retrieve the number of methods of a named interface
func (Type) NumExplicitMethod ¶
NumExplicitMethod returns the number of explicitly declared methods of named type or interface t. Wrapper methods for embedded fields or embedded interfaces are not counted.
func (Type) NumField ¶
NumField returns a struct type's field count. It panics if the type's Kind is not Struct.
func (Type) NumIn ¶
NumIn returns a function type's input parameter count. It panics if the type's Kind is not Func.
func (Type) NumMethod ¶
For interfaces, NumMethod returns *total* number of methods for interface t, including wrapper methods for embedded interfaces. For all other named types, NumMethod returns the number of explicitly declared methods, ignoring wrapper methods for embedded fields. Returns 0 for other unnamed types.
func (Type) NumOut ¶
NumOut returns a function type's output parameter count. It panics if the type's Kind is not Func.
func (Type) Out ¶
Out returns the type of a function type's i'th output parameter. It panics if the type's Kind is not Func. It panics if i is not in the range [0, NumOut()).
func (Type) Pkg ¶
Pkg returns a named type's package, that is, the package where it was defined. If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), Pkg will return nil.
func (Type) PkgName ¶
PkgName returns a named type's package name, that is, the default name that the package provides when imported. If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), the package name will be the empty string.
func (Type) PkgPath ¶
PkgPath returns a named type's package path, that is, the import path that uniquely identifies the package, such as "encoding/base64". If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), the package path will be the empty string.
func (Type) ReflectType ¶
ReflectType returns a best-effort reflect.Type that approximates the type. It may be inexact for the following reasons:
- missing reflect.NamedOf(): no way to programmatically create named types, or to access the underlying type of a named type
- missing reflect.InterfaceOf(): interface types created at runtime will be approximated by structs
- missing reflect.MethodOf(): method types created at runtime will be approximated by functions whose first parameter is the receiver
- reflect.StructOf() does not support embedded or unexported fields
- go/reflect lacks the ability to create self-referencing types: references to the type itself will be replaced by interface{}.
Examples:
after invoking at runtime type2.NewStruct() and type2.NewNamed() to create the following type: type List struct { Elem int; Rest *List } ReflectType will return a reflect.Type equivalent to: struct { Elem int; Rest interface{} } i.e. the type name will be missing due to limitation 1 above, and the field 'Rest' will have type interface{} instead of *List due to limitation 5.
func (Type) Resolve ¶
if t.ReflectType() is Forward i.e. unknown/approximated due to recursive types, try to resolve it
func (Type) SetUnderlying ¶
SetUnderlying sets the underlying type of a named type and marks it as complete. It panics if the type is unnamed, or if the underlying type is named, or if SetUnderlying() was already invoked on the named type.
func (Type) SetUserData ¶
func (t Type) SetUserData(key, value interface{})
SetUserData sets the user-supplied data of the type.
func (Type) Size ¶
Size returns the number of bytes needed to store a value of the given type; it is analogous to unsafe.Sizeof.
func (Type) UnsafeForceReflectType ¶
type Universe ¶
type Universe struct { // map types.Type -> Type Types ReflectTypes map[r.Type]Type BasicTypes []Type TypeOfInterface Type TypeOfForward Type TypeOfError Type TryResolve func(name, pkgpath string) Type Packages map[string]*Package Importer *Importer RebuildDepth int DebugDepth int ThreadSafe bool // contains filtered or unexported fields }
func NewUniverse ¶
func NewUniverse() *Universe
func (*Universe) CachePackage ¶
CachePackage unconditionally adds pkg to Universe.Packages, then also adds its imports if not cached already
func (*Universe) FromReflectType ¶
FromReflectType creates a Type corresponding to given reflect.Type Note: conversions from Type to reflect.Type and back are not exact, for the reasons listed in Type.ReflectType() Conversions from reflect.Type to Type and back are not exact for the same reasons.
func (*Universe) InterfaceOf ¶
func (v *Universe) InterfaceOf(pkg *Package, methodnames []string, methodtypes []Type, embeddeds []Type) Type
InterfaceOf returns a new interface for the given methods and embedded types. After the methods and embeddeds are fully defined, call Complete() to mark the interface as complete and compute wrapper methods for embedded fields.
unexported method names are created in 'pkg'.
WARNING: the Type returned by InterfaceOf is not complete, i.e. its method set is not computed yet. Once you know that methods and embedded interfaces are complete, call Complete() to compute the method set and mark this Type as complete.
func (*Universe) InvalidateCache ¶
func (v *Universe) InvalidateCache()
clears all xtype.cache.field and xtype.cache.method. invoked by NamedOf() when a type is redefined.
func (*Universe) InvalidateMethodCache ¶
func (v *Universe) InvalidateMethodCache()
clears all xtype.cache.method. invoked by AddMethod() when a method is redefined.
func (*Universe) LoadPackage ¶
func (*Universe) NamedOf ¶
NamedOf returns a new named type for the given type name and package. Initially, the underlying type may be set to interface{} - use SetUnderlying to change it. These two steps are separate to allow creating self-referencing types, as for example type List struct { Elem int; Rest *List }
func (*Universe) StructOf ¶
func (v *Universe) StructOf(fields []StructField) Type
func (*Universe) TypeOf ¶
TypeOf creates a Type corresponding to reflect.TypeOf() of given value. Note: conversions from Type to reflect.Type and back are not exact, because of the reasons listed in Type.ReflectType() Conversions from reflect.Type to Type and back are not exact for the same reasons.
func (*Universe) VisitFields ¶
func (v *Universe) VisitFields(t Type, visitor func(StructField))
visit type's direct and embedded fields in breadth-first order
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
xreflect.Value is a replacement for reflect.Value that hides the presence of xreflect.Forward due to emulated recursive types
func EmulatedInterfaceGetMethod ¶
extract the already-made i-th closure from inside the emulated interface object.
func FromReflectValues ¶
func MakeMapWithSize ¶
func ToEmulatedInterface ¶
func ToEmulatedInterface(rtypeinterf r.Type, v Value, t Type, obj2methods []func(Value) Value) Value
create an emulated interface from given value, type and method extractors (methods extractors are functions that, given a value, return one of its methods)
func (Value) CanInterface ¶
func (Value) Complex ¶
func (v Value) Complex() complex128
func (Value) FieldByIndex ¶
func (Value) FieldByName ¶
func (Value) InterfaceData ¶
func (Value) MethodByName ¶
func (Value) OverflowComplex ¶
func (v Value) OverflowComplex(x complex128) bool
func (Value) OverflowFloat ¶
func (Value) OverflowInt ¶
func (Value) OverflowUint ¶
func (Value) ReflectValue ¶
func (Value) SetComplex ¶
func (v Value) SetComplex(x complex128)