mtype

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package mtype holds mvm's symbolic type representation: the compile-time Type graph and the constructors/derivations over it.

Index

Constants

This section is empty.

Variables

View Source
var AnyRtype = reflect.TypeOf((*any)(nil)).Elem()

AnyRtype is the reflect.Type for the empty interface (any).

Functions

func NewPlaceholderRtype

func NewPlaceholderRtype(name string) reflect.Type

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

func PatchRtype(dst, src reflect.Type)

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

func (m Method) IsResolved() bool

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

func FuncOf(arg, ret []*Type, variadic bool) *Type

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

func IfaceMethodTypes(typ *Type) (types [6]*Type, n int)

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

func NewStructType(name string) *Type

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 SymArray

func SymArray(n int, elem *Type) *Type

SymArray builds a symbolic [n]elem.

func SymBasic

func SymBasic(k reflect.Kind) *Type

SymBasic builds a symbolic type of a basic kind with Rtype unset.

func SymChan

func SymChan(dir reflect.ChanDir, elem *Type) *Type

SymChan builds a symbolic chan-elem with direction dir.

func SymFunc

func SymFunc(arg, ret []*Type, variadic bool) *Type

SymFunc builds a symbolic func type (Rtype unset); comp materializes it.

func SymMap

func SymMap(key, elem *Type) *Type

SymMap builds a symbolic map[key]elem.

func SymPtr

func SymPtr(elem *Type) *Type

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 SymSlice

func SymSlice(elem *Type) *Type

SymSlice builds a symbolic []elem.

func SymStruct

func SymStruct(fields []*Type, embedded []EmbeddedField, tags []string) *Type

SymStruct builds a symbolic struct type (Rtype unset); comp materializes it.

func TypeOf

func TypeOf(v any) *Type

TypeOf returns the mvm type of v.

func (*Type) Align

func (t *Type) Align() int

Align returns t's required alignment in bytes (see Size).

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

func (t *Type) Elem() *Type

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

func (t *Type) FieldIndex(name string) []int

FieldIndex returns the index of struct field name.

func (*Type) FieldLookup

func (t *Type) FieldLookup(name string) ([]int, *Type)

FieldLookup returns the index path and type of struct field name in a single pass.

func (*Type) FieldOffset

func (t *Type) FieldOffset(path []int) uintptr

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

func (t *Type) FieldType(name string) *Type

FieldType returns the type of struct field name, using mvm-level info when available.

func (*Type) FieldTypeAtPath

func (t *Type) FieldTypeAtPath(path []int) *Type

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

func (t *Type) Identical(u *Type) bool

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

func (t *Type) Implements(iface *Type) bool

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

func (t *Type) IsComparable() bool

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) IsFunc

func (t *Type) IsFunc() bool

IsFunc returns true if type t is of func kind.

func (*Type) IsInterface

func (t *Type) IsInterface() bool

IsInterface reports whether t represents an interface type.

func (*Type) IsPtr

func (t *Type) IsPtr() bool

IsPtr returns true if type t is of pointer kind.

func (*Type) IsSlice

func (t *Type) IsSlice() bool

IsSlice returns true if type t is of slice kind.

func (*Type) IsStruct

func (t *Type) IsStruct() bool

IsStruct returns true if type t is of struct kind.

func (*Type) IsVariadic

func (t *Type) IsVariadic() bool

IsVariadic reports whether a func type's final parameter is variadic, from the symbolic graph when no rtype is materialized yet.

func (*Type) Key

func (t *Type) Key() *Type

Key returns a map type's key type.

func (*Type) Kind

func (t *Type) Kind() reflect.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

func (t *Type) Len() int

Len returns an array type's length, from the symbolic graph when no rtype is materialized yet.

func (*Type) MissingMethod

func (t *Type) MissingMethod(rt reflect.Type) string

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

func (t *Type) NativeImplements(rt reflect.Type) bool

NativeImplements reports whether native reflect type rt has all the methods required by interface type t.

func (*Type) NumIn

func (t *Type) NumIn() int

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

func (t *Type) NumOut() int

NumOut returns a func type's number of results, from the symbolic Returns slice when populated, else from reflect. Symmetric with ReturnType.

func (*Type) Out

func (t *Type) Out(i int) *Type

Out returns the type's i'th output parameter.

func (*Type) ParamType

func (t *Type) ParamType(i int) *Type

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

func (t *Type) ResolveMethodType(id int) *Type

ResolveMethodType returns the Type whose Methods[id] holds the resolved entry, scanning typ, its ElemType, and the Base chain (via IfaceMethodTypes).

func (*Type) ReturnType

func (t *Type) ReturnType(i int) *Type

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) SameAs

func (t *Type) SameAs(u *Type) bool

SameAs reports whether t and u represent the same concrete type.

func (*Type) SetFields

func (t *Type) SetFields(src *Type)

SetFields finalizes a forward-declared struct from src, patching the rtype in place so derived types (e.g. PointerTo) see the real layout.

func (*Type) Size

func (t *Type) Size() uintptr

Size returns the number of bytes a value of t occupies, computed from the symbolic graph so it is available before Rtype materializes. It matches reflect.Type.Size for every kind mvm constructs.

func (*Type) String

func (t *Type) String() string

type TypeElem

type TypeElem struct {
	Approx bool
	Type   *Type
}

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL