engine

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package engine holds base implementation details for use by generated code. Users should not depend on any particular feature of this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Abstract

type Abstract struct {
	// contains filtered or unexported fields
}

Abstract allows a visitable object to be manipulated as an abstract tree of nodes. This should be enclosed in a type-safe wrapper. An Abstract should only ever represent a struct or a slice; pointers and interfaces should be resolved to their respective targets before being wrapped in an Abstract.

func (*Abstract) ChildAt

func (a *Abstract) ChildAt(index int) *Abstract

ChildAt returns the nth field or slice element. If that value is a pointer or an interface, it is dereferenced before returning. Nil pointers, interfaces, and empty slices will return nil here.

func (*Abstract) NumChildren

func (a *Abstract) NumChildren() int

NumChildren returns the number of fields or slice elements.

func (*Abstract) Ptr

func (a *Abstract) Ptr() Ptr

Ptr returns the embedded pointer. This should not be exposed to user code, but should instead be provided via a type-safe facade.

func (*Abstract) TypeID added in v0.1.4

func (a *Abstract) TypeID() TypeID

TypeID returns the type token of the embedded value.

type Action added in v0.1.4

type Action struct {
	// contains filtered or unexported fields
}

Action allows user-defined actions to be inserted into the visitation flow.

type ActionFn added in v0.1.4

type ActionFn func() error

ActionFn describes a simple callback function.

type Context added in v0.1.4

type Context struct{}

Context is provided to generated, type-safe facades.

func (Context) ActionCall added in v0.1.4

func (Context) ActionCall(fn ActionFn) Action

ActionCall constructs an action which will invoke the function.

func (Context) ActionVisit added in v0.1.4

func (Context) ActionVisit(td *TypeData, value Ptr) Action

ActionVisit constructs an action which will visit the given value.

func (Context) ActionVisitTypeID added in v0.1.4

func (Context) ActionVisitTypeID(id TypeID, value Ptr) Action

ActionVisitTypeID constructs an action which will visit the given value.

func (Context) Actions added in v0.1.4

func (Context) Actions(actions []Action) Decision

Actions is for use by generated code only.

func (Context) Continue added in v0.1.4

func (Context) Continue() Decision

Continue is for use by generated code only.

func (Context) Error added in v0.1.4

func (Context) Error(err error) Decision

Error is for use by generated code only.

func (Context) Halt added in v0.1.4

func (Context) Halt() Decision

Halt is for use by generated code only.

func (Context) Skip added in v0.1.4

func (Context) Skip() Decision

Skip is for use by generated code only.

type Decision added in v0.1.4

type Decision struct {
	// contains filtered or unexported fields
}

Decision is wrapped by generated, type-safe facades.

func (Decision) Intercept added in v0.1.4

func (d Decision) Intercept(fn FacadeFn) Decision

Intercept is for use by generated code only.

func (Decision) Post added in v0.1.4

func (d Decision) Post(fn FacadeFn) Decision

Post is for use by generated code only.

func (Decision) Replace added in v0.1.4

func (d Decision) Replace(id TypeID, x Ptr) Decision

Replace is for use by generated code only.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

An Engine holds the necessary information to pass a visitor over a field.

func New

func New(m TypeMap) *Engine

New constructs an Engine.

func (*Engine) Abstract

func (e *Engine) Abstract(typeID TypeID, x Ptr) *Abstract

Abstract constructs an abstract accessor around a struct's field.

func (*Engine) Execute

func (e *Engine) Execute(
	fn FacadeFn, t TypeID, x Ptr, assignableTo TypeID,
) (retType TypeID, ret Ptr, changed bool, err error)

Execute drives the visitation process. This is an "unrolled recursive" function that maintains its own stack to avoid deeply-nested call stacks. We can also perform cycle-detection at fairly low cost. Any replacement of the top-level value must be assignable to the given TypeID.

func (*Engine) Stringify added in v0.1.4

func (e *Engine) Stringify(id TypeID) string

Stringify returns a string representation of the given type that is suitable for debugging purposes.

type FacadeFn

type FacadeFn interface{}

FacadeFn is a generated function type that depends on the visitable interface.

type FieldInfo

type FieldInfo struct {
	Name   string
	Offset uintptr
	Target TypeID
	// contains filtered or unexported fields
}

FieldInfo describes a field within a struct.

type Kind

type Kind int

Kind determines the dispatch strategy for a given visitable type.

const (
	KindInterface Kind
	KindPointer
	KindSlice
	KindStruct
)

A visitable type has some combinations of kinds which determine its access pattern.

type Ptr

type Ptr unsafe.Pointer

Ptr is an alias for unsafe.Pointer.

type TypeData

type TypeData struct {
	// Copy will effect a type aware copy of the data at from to dest.
	Copy func(dest, from Ptr)
	// Elem is the element type of a slice or of a pointer.
	Elem TypeID
	// Facade will call a user-provided facade function in a
	// type-safe fashion.
	Facade func(Context, FacadeFn, Ptr) Decision
	// Fields holds information about the fields of a struct.
	Fields []FieldInfo
	// IntfType accepts a pointer to an interface type and returns a
	// TypeID for the enclosed datatype.
	//
	// An interface's type-tag contains several flag bits which
	// fall into the category of "too much magic" for us to want
	// to handle ourselves. Instead, we generate functions which
	// will perform the necessary type mapping.
	IntfType func(Ptr) TypeID
	// IntfWrap provides the opposite function of IntfType. It accepts
	// a TypeID and a pointer to the interface's value and returns a
	// pointer to the resulting interface array.
	IntfWrap func(TypeID, Ptr) Ptr
	// Kind selects various strategies for handling the given type.
	Kind Kind
	// Name is the source name of the type.
	Name string
	// NewSlice constructs a slice of the given length and returns a
	// pointer to the slice's header.
	NewSlice func(size int) Ptr
	// NewStruct returns a pointer to a newly-allocated struct.
	NewStruct func() Ptr
	// SizeOf is the size of the data type. This is used for traversing
	// slices. It could be expanded in the future to generalizing the
	// Copy() function.
	SizeOf uintptr
	// TypeID is a generated id.
	TypeID TypeID
	// contains filtered or unexported fields
}

TypeData contains metadata and accessors that are produced by the code generator.

type TypeID added in v0.1.4

type TypeID int

A TypeID is an opaque reference to a visitable type. These are assigned by the code-generator and their specific values and order are arbitrary.

type TypeMap

type TypeMap []TypeData

A TypeMap holds the necessary metadata to visit a collection of types.

Jump to

Keyboard shortcuts

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