Documentation
¶
Overview ¶
Package mtype holds mvm's symbolic type representation: the compile-time Type graph and the constructors/derivations over it.
Index ¶
- Variables
- func NewPlaceholderRtype(name string) reflect.Type
- func PatchRtype(dst, src reflect.Type)
- type EmbeddedField
- type IfaceMethod
- type Method
- type Type
- func FuncOf(arg, ret []*Type, variadic bool) *Type
- func IfaceMethodTypes(typ *Type) (types [6]*Type, n int)
- func NewStructType(name string) *Type
- func StructOf(fields []*Type, embedded []EmbeddedField, tags []string) *Type
- func SymArray(n int, elem *Type) *Type
- func SymBasic(k reflect.Kind) *Type
- func SymChan(dir reflect.ChanDir, elem *Type) *Type
- func SymFunc(arg, ret []*Type, variadic bool) *Type
- func SymMap(key, elem *Type) *Type
- func SymPtr(elem *Type) *Type
- func SymSlice(elem *Type) *Type
- func SymStruct(fields []*Type, embedded []EmbeddedField, tags []string) *Type
- func TypeOf(v any) *Type
- func (t *Type) Align() int
- func (t *Type) CaptureKind()
- func (t *Type) Elem() *Type
- func (t *Type) EnsureIfaceMethods()
- func (t *Type) FieldIndex(name string) []int
- func (t *Type) FieldLookup(name string) ([]int, *Type)
- func (t *Type) FieldOffset(path []int) uintptr
- func (t *Type) FieldType(name string) *Type
- func (t *Type) FieldTypeAtPath(path []int) *Type
- func (t *Type) Identical(u *Type) bool
- func (t *Type) Implements(iface *Type) bool
- func (t *Type) IsComparable() bool
- func (t *Type) IsFunc() bool
- func (t *Type) IsInterface() bool
- func (t *Type) IsPtr() bool
- func (t *Type) IsSlice() bool
- func (t *Type) IsStruct() 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) MissingMethod(rt reflect.Type) string
- func (t *Type) NativeImplements(rt reflect.Type) bool
- func (t *Type) NumIn() int
- func (t *Type) NumOut() int
- func (t *Type) Out(i int) *Type
- func (t *Type) ParamType(i int) *Type
- func (t *Type) ResolveMethodType(id int) *Type
- func (t *Type) ReturnType(i int) *Type
- func (t *Type) SameAs(u *Type) bool
- func (t *Type) SetFields(src *Type)
- func (t *Type) Size() uintptr
- func (t *Type) String() string
- type TypeElem
Constants ¶
This section is empty.
Variables ¶
var AnyRtype = reflect.TypeOf((*any)(nil)).Elem()
AnyRtype is the reflect.Type for the empty interface (any).
Functions ¶
func NewPlaceholderRtype ¶
NewPlaceholderRtype builds a fresh, uniquely-shaped struct rtype used to break pointer cycles while materializing a named struct: install it as the type's Rtype before materializing fields (so a *T built mid-recursion resolves), then PatchRtype it in place with the real layout.
func PatchRtype ¶
PatchRtype overwrites dst's struct-rtype bytes with src's, preserving dst's identity (so derived/pointer rtypes captured against dst stay valid).
Types ¶
type EmbeddedField ¶
type EmbeddedField struct {
FieldIdx int // index of this field in the parent struct
Type *Type // mvm type of the embedded field (shares identity with symbol table)
}
EmbeddedField records a mvm embedded field within a struct type.
type IfaceMethod ¶
type IfaceMethod struct {
Name string
ID int // global method ID; -1 = not yet assigned
Rtype reflect.Type // method signature (no receiver, as declared in the interface body); nil if unknown
Sig *Type // symbolic method signature, the materialize-time source of Rtype
}
IfaceMethod describes a method required by an interface type.
type Method ¶
type Method struct {
Index int // data index of code address (-1 if unset or EmbedIface)
Path []int // field index path to embedded receiver (nil = direct, []int{} = deref only)
EmbedIface bool // Path leads to an embedded interface field; dispatch through it
PtrRecv bool // true if the method has a pointer receiver (e.g. *T)
Rtype reflect.Type // bound method signature (no receiver); nil if unknown. Per-type, so it disambiguates same-named methods (e.g. Unwrap() error vs Unwrap() []error) that share a global method ID.
Sig *Type // symbolic bound method signature (no receiver); the materialize-time source of Rtype. Preferred over Rtype for signature comparison when both methods carry it.
}
Method records a method's code location and receiver path for interface dispatch.
func (Method) IsResolved ¶
IsResolved reports whether this method slot has been populated with either a compiled code address or an embedded-interface dispatch entry.
type Type ¶
type Type struct {
PkgPath string
Name string
Rtype reflect.Type
Placeholder bool // true for forward-declared struct/interface placeholders until finalized (SetFields, or IfaceMethods assignment in parseTypeLine)
IfaceMethods []IfaceMethod // non-nil for interface types: required method signatures
TypeElems []TypeElem // non-nil for constraint interfaces: union members (e.g. cmp.Ordered)
Comparable bool // constraint interface embeds the built-in `comparable` (e.g. `interface { comparable; error }`)
Methods []Method // concrete types: methods[methodID] = code location + receiver path
Embedded []EmbeddedField // mvm types of anonymous (embedded) fields, for promoted method lookup
Params []*Type // mvm-level parameter types for func types (nil for non-func or if unknown)
Returns []*Type // mvm-level return types for func types (nil for non-func or if unknown)
Fields []*Type // mvm-level field types for struct types, parallel to reflect visible fields
ElemType *Type // mvm-level element type for map/slice/array/pointer/chan types
KeyType *Type // mvm-level key type for map types; nil for non-maps or native-built maps
// Symbolic descriptors of what Rtype otherwise carries, so a type can be
// materialized (Rtype built) from the symbolic graph alone (see comp materialize).
ArrayLen int // array length (kind Array)
ChanDir reflect.ChanDir // channel direction (kind Chan)
Variadic bool // last param is variadic (kind Func)
Tags []string // struct field tags, parallel to Fields (kind Struct)
// Base is the source *Type a struct-field shallow copy derived from, so
// methods registered on the source after the copy stay reachable.
Base *Type
// Defined marks a top-level `type X T` definition (set at parse), as opposed
// to a struct-field shallow copy of a named type. A defined type owns its
// identity and is never a field clone, even when its Base is a named type
// (type Y X); the field-clone copy sites clear it.
Defined bool
// contains filtered or unexported fields
}
Type is the representation of a mvm type.
func FuncOf ¶
FuncOf returns the canonical func type for the given args/results/variadic; repeated identical calls return the same *Type. Callers must not mutate the returned Params/Returns (they alias the cached slices).
func IfaceMethodTypes ¶
IfaceMethodTypes returns the types carrying typ's method set: typ, its ElemType (pointers register methods on T not *T), and the same along the Base chain (so a struct-field copy reaches methods registered on its source). The [6] bound suffices: Base chains are collapsed to depth 1 at creation.
func NewStructType ¶
NewStructType creates a forward-declared struct placeholder named name (empty for anonymous). Register it, then call SetFields to finalize. The placeholder is symbolic (Rtype nil); comp materializes the rtype once the fields are set.
func StructOf ¶
func StructOf(fields []*Type, embedded []EmbeddedField, tags []string) *Type
StructOf returns the canonical struct type for the given fields/embedded/tags, memoized on a structural key (Name+PkgPath+Base-or-self pointer per field) so equivalent shapes parsed separately converge despite per-call field clones.
func SymPtr ¶
SymPtr builds a symbolic *elem with Rtype unset for comp to materialize (see vm.MaterializeRtype); SymSlice/SymMap/SymArray/SymChan are the parse-time counterparts to vm's rtype-building PointerTo/SliceOf/... .
func SymStruct ¶
func SymStruct(fields []*Type, embedded []EmbeddedField, tags []string) *Type
SymStruct builds a symbolic struct type (Rtype unset); comp materializes it.
func (*Type) CaptureKind ¶
func (t *Type) CaptureKind()
CaptureKind records t's kind in the symbolic field so Kind() survives a caller nilling Rtype to defer materialization.
func (*Type) Elem ¶
Elem returns a type's element type, preserving mvm-level info (e.g. IfaceMethods).
func (*Type) EnsureIfaceMethods ¶
func (t *Type) EnsureIfaceMethods()
EnsureIfaceMethods populates IfaceMethods from the reflect method set if not already set. This covers native interface types (e.g. io.Reader) whose method sets were not explicitly enumerated at parse time.
func (*Type) FieldIndex ¶
FieldIndex returns the index of struct field name.
func (*Type) FieldLookup ¶
FieldLookup returns the index path and type of struct field name in a single pass.
func (*Type) FieldOffset ¶
FieldOffset returns the byte offset of the field reached by the reflect-style index path within struct t, computed symbolically (value-embed promotion; Go's unsafe.Offsetof does not cross pointer embeds).
func (*Type) FieldType ¶
FieldType returns the type of struct field name, using mvm-level info when available.
func (*Type) FieldTypeAtPath ¶
FieldTypeAtPath returns the type of the field reached by the reflect-style index path within struct t, computed from the symbolic Fields graph with a reflect fallback per segment. Pointers are dereferenced between segments. Returns nil if the path cannot be resolved.
func (*Type) Identical ¶
Identical reports whether t and u denote the same Go type. Materialized types compare by rtype identity; symbolic ones compare structurally (named types by name+package, composites recursively).
func (*Type) Implements ¶
Implements reports whether the concrete type t satisfies interface iface. iface.IfaceMethods must have IDs populated (by the compiler) before calling this.
func (*Type) IsComparable ¶
IsComparable reports whether values of t may be compared with == / !=, computed from the symbolic graph (matching reflect.Type.Comparable). Slices, maps and funcs are not comparable; a struct is comparable iff every field is; an array iff its element is; interfaces are comparable (may panic at runtime).
func (*Type) IsInterface ¶
IsInterface reports whether t represents an interface type.
func (*Type) IsVariadic ¶
IsVariadic reports whether a func type's final parameter is variadic, from the symbolic graph when no rtype is materialized yet.
func (*Type) Kind ¶
Kind returns t's kind. It prefers the symbolic kind set at construction and falls back to the rtype, which lets parse-time dispatch move off Rtype while construction sites are migrated to populate kind. The goal is that, once every constructor sets kind, the rtype need not exist before comp.
func (*Type) Len ¶
Len returns an array type's length, from the symbolic graph when no rtype is materialized yet.
func (*Type) MissingMethod ¶
MissingMethod returns the name of the first method required by interface type t that native reflect type rt does not have. Returns "" if all methods are present or t has no IfaceMethods.
func (*Type) NativeImplements ¶
NativeImplements reports whether native reflect type rt has all the methods required by interface type t.
func (*Type) NumIn ¶
NumIn returns a func type's number of parameters, from the symbolic Params slice when populated, else from reflect. Symmetric with ParamType.
func (*Type) NumOut ¶
NumOut returns a func type's number of results, from the symbolic Returns slice when populated, else from reflect. Symmetric with ReturnType.
func (*Type) ParamType ¶
ParamType returns the mvm-level i'th parameter type if known, else falls back to reflect. Returns nil when i is out of range. Symmetric with ReturnType; used by generic inference to walk a func-typed parameter whose reflect-derived bridge form has an empty Params slice.
func (*Type) ResolveMethodType ¶
ResolveMethodType returns the Type whose Methods[id] holds the resolved entry, scanning typ, its ElemType, and the Base chain (via IfaceMethodTypes).
func (*Type) ReturnType ¶
ReturnType returns the mvm-level i'th return type if known, else falls back to reflect. Returns nil when i is out of range.
func (*Type) SetFields ¶
SetFields finalizes a forward-declared struct from src, patching the rtype in place so derived types (e.g. PointerTo) see the real layout.
type TypeElem ¶
TypeElem describes one member of a constraint interface's type-element union, e.g. for "type Ordered interface { ~int | ~string }" the type elements are TypeElem{Approx: true, Type: intType}, TypeElem{Approx: true, Type: stringType}. Approx encodes the "~" prefix (any type whose underlying type is Type).