Documentation
¶
Index ¶
- Constants
- Variables
- func EmulatedInterfaceGetMethod(obj reflect.Value, index int) reflect.Value
- 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 reflect.Type) reflect.Type
- func ToBasicKind(kind reflect.Kind, untyped bool) types.BasicKind
- func ToEmulatedInterface(rtypeinterf reflect.Type, v reflect.Value, t Type, ...) reflect.Value
- func ToReflectKind(gkind types.BasicKind) reflect.Kind
- func Zero(t Type) reflect.Value
- type Error
- type Forward
- type Importer
- type InterfaceHeader
- type Key
- type Method
- type Package
- type QName
- type QNameI
- type QNameI_github_com_cosmos72_gomacro_xreflect
- 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() reflect.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() *[]reflect.Value
- 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() reflect.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() reflect.Type
- func (t Type) RemoveMethods(names []string, pkgpath string)
- func (t Type) SetUnderlying(underlying Type)
- func (t Type) Size() uintptr
- func (t Type) String() string
- func (t Type) Universe() *Universe
- func (t Type) UnsafeForceReflectType(rtype reflect.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 reflect.ChanDir, elem Type) Type
- func (v *Universe) FromReflectType(rtype reflect.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 reflect.Type) 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, kind reflect.Kind) 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
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/steele232/zoumacro/base/constants.go
const MaxDepth = int(^uint(0) >> 1)
Variables ¶
var ReflectBasicTypes = rbasictypes
Functions ¶
func EmulatedInterfaceGetMethod ¶
extract the already-made i-th closure from inside the emulated interface object.
func GensymAnonymous ¶
func GensymPrivate ¶
func IsEmulatedInterface ¶
func IsGoUntypedKind ¶
func ReflectUnderlying ¶
best-effort implementation of missing reflect.Type.Underlying()
func ToEmulatedInterface ¶
func ToEmulatedInterface(rtypeinterf reflect.Type, v reflect.Value, t Type, obj2methods []func(reflect.Value) reflect.Value) reflect.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)
Types ¶
type Importer ¶
type Importer struct {
// 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 reflect.Value, typ Type) InterfaceHeader
func (InterfaceHeader) Type ¶
func (h InterfaceHeader) Type() Type
func (InterfaceHeader) Value ¶
func (h InterfaceHeader) Value() reflect.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 Method ¶
type Method struct {
Name string
Pkg *Package
Type Type // method type
Funs *[]reflect.Value // (*Funs)[Index] is the method, with receiver as first argument
Index int // index for Type.Method
FieldIndex []int // embedded fields index sequence for reflect.Type.FieldByIndex or reflect.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/steele232/zoumacro/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 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 reflect.StructTag // field tag string
Offset uintptr // offset within struct, in bytes. meaningful only if all Deref[] are false
Index []int // index sequence for reflect.Type.FieldByIndex or reflect.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
func FromEmulatedInterface ¶
extract the concrete value and type contained in an emulated interface
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) 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) RemoveMethods ¶
RemoveMethods removes given methods from type. It panics if the type is unnamed.
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) 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 {
Types
ReflectTypes map[reflect.Type]Type
BasicTypes []Type
TypeOfInterface Type
TypeOfForward Type
TypeOfError Type
TryResolve func(name, pkgpath string) Type
Packages map[string]*Package
Importer types.ImporterFrom
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, 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) 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.fieldcache and xtype.methodcache. invoked by NamedOf() when a type is redefined.
func (*Universe) InvalidateMethodCache ¶
func (v *Universe) InvalidateMethodCache()
clears all xtype.methodcache. 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