package typeparams
import "golang.org/x/tools/internal/typeparams"
Package typeparams provides functions to work indirectly with type parameter data stored in go/ast and go/types objects, while these API are guarded by a build constraint.
This package exists to make it easier for tools to work with generic code, while also compiling against older Go versions.
Index ¶
- Constants
- Variables
- func ForFuncType(n *ast.FuncType) *ast.FieldList
- func ForTypeSpec(n *ast.TypeSpec) *ast.FieldList
- func GetInstances(info *types.Info) map[*ast.Ident]Instance
- func InitInstanceInfo(info *types.Info)
- func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error)
- func IsComparable(iface *types.Interface) bool
- func IsImplicit(iface *types.Interface) bool
- func IsMethodSet(iface *types.Interface) bool
- func IsTypeParam(t types.Type) bool
- func MarkImplicit(iface *types.Interface)
- func NamedTypeOrigin(named *types.Named) types.Type
- func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature
- func PackIndexExpr(x ast.Expr, lbrack token.Pos, indices []ast.Expr, rbrack token.Pos) ast.Expr
- func SetForNamed(n *types.Named, tparams []*TypeParam)
- func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type)
- type Context
- type IndexExprData
- type IndexListExpr
- type Instance
- type Term
- func InterfaceTermSet(iface *types.Interface) ([]*Term, error)
- func NewTerm(tilde bool, typ types.Type) *Term
- func StructuralTerms(tparam *TypeParam) ([]*Term, error)
- func UnionTermSet(union *Union) ([]*Term, error)
- type TypeList
- type TypeParam
- type TypeParamList
- func ForNamed(named *types.Named) *TypeParamList
- func ForSignature(sig *types.Signature) *TypeParamList
- func RecvTypeParams(sig *types.Signature) *TypeParamList
- type Union
Constants ¶
const Enabled = true
Enabled reports whether type parameters are enabled in the current build environment.
Variables ¶
Functions ¶
func ForFuncType ¶
ForFuncType returns n.TypeParams.
func ForTypeSpec ¶
ForTypeSpec returns n.TypeParams.
func GetInstances ¶
GetInstances returns info.Instances.
func InitInstanceInfo ¶
InitInstanceInfo initializes info to record information about type and function instances.
func Instantiate ¶
func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error)
Instantiate calls types.Instantiate.
func IsComparable ¶
IsComparable calls iface.IsComparable().
func IsImplicit ¶
IsImplicit calls iface.IsImplicit().
func IsMethodSet ¶
IsMethodSet calls iface.IsMethodSet().
func IsTypeParam ¶
IsTypeParam reports whether t is a type parameter.
func MarkImplicit ¶
MarkImplicit calls iface.MarkImplicit().
func NamedTypeOrigin ¶
NamedTypeOrigin returns named.Orig().
func NewSignatureType ¶
func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature
NewSignatureType calls types.NewSignatureType.
func PackIndexExpr ¶
PackIndexExpr returns an *ast.IndexExpr or *ast.IndexListExpr, depending on the cardinality of indices. Calling PackIndexExpr with len(indices) == 0 will panic.
func SetForNamed ¶
SetForNamed sets the type params tparams on n. Each tparam must be of dynamic type *types.TypeParam.
func SetTypeParamConstraint ¶
SetTypeParamConstraint calls tparam.SetConstraint(constraint).
Types ¶
type Context ¶
Context is an alias for types.Context.
type IndexExprData ¶
type IndexExprData struct { X ast.Expr // expression Lbrack token.Pos // position of "[" Indices []ast.Expr // index expressions Rbrack token.Pos // position of "]" }
A IndexExprData holds data from both ast.IndexExpr and the new ast.MultiIndexExpr, which was introduced in Go 1.18.
func GetIndexExprData ¶
func GetIndexExprData(n ast.Node) *IndexExprData
GetIndexExprData extracts data from AST nodes that represent index expressions.
For an ast.IndexExpr, the resulting IndexExprData will have exactly one index expression. For an ast.IndexListExpr (go1.18+), it may have a variable number of index expressions.
For nodes that don't represent index expressions, GetIndexExprData returns nil. TODO(rfindley): remove this function in favor of using the alias below.
type IndexListExpr ¶
type IndexListExpr = ast.IndexListExpr
IndexListExpr is an alias for ast.IndexListExpr.
type Instance ¶
Instance is an alias for types.Instance.
type Term ¶
Term is an alias for types.Term.
func InterfaceTermSet ¶
InterfaceTermSet computes the normalized terms for a constraint interface, returning an error if the term set cannot be computed or is empty. In the latter case, the error will be ErrEmptyTypeSet.
See the documentation of StructuralTerms for more information on normalization.
func NewTerm ¶
NewTerm calls types.NewTerm.
func StructuralTerms ¶
StructuralTerms returns a slice of terms representing the normalized structural type restrictions of a type parameter, if any.
Structural type restrictions of a type parameter are created via non-interface types embedded in its constraint interface (directly, or via a chain of interface embeddings). For example, in the declaration
type T[P interface{~int; m()}] int
the structural restriction of the type parameter P is ~int.
With interface embedding and unions, the specification of structural type restrictions may be arbitrarily complex. For example, consider the following:
type A interface{ ~string|~[]byte } type B interface{ int|string } type C interface { ~string|~int } type T[P interface{ A|B; C }] int
In this example, the structural type restriction of P is ~string|int: A|B expands to ~string|~[]byte|int|string, which reduces to ~string|~[]byte|int, which when intersected with C (~string|~int) yields ~string|int.
StructuralTerms computes these expansions and reductions, producing a "normalized" form of the embeddings. A structural restriction is normalized if it is a single union containing no interface terms, and is minimal in the sense that removing any term changes the set of types satisfying the constraint. It is left as a proof for the reader that, modulo sorting, there is exactly one such normalized form.
Because the minimal representation always takes this form, StructuralTerms returns a slice of tilde terms corresponding to the terms of the union in the normalized structural restriction. An error is returned if the constraint interface is invalid, exceeds complexity bounds, or has an empty type set. In the latter case, StructuralTerms returns ErrEmptyTypeSet.
StructuralTerms makes no guarantees about the order of terms, except that it is deterministic.
func UnionTermSet ¶
UnionTermSet computes the normalized terms for a union, returning an error if the term set cannot be computed or is empty. In the latter case, the error will be ErrEmptyTypeSet.
See the documentation of StructuralTerms for more information on normalization.
type TypeList ¶
TypeList is an alias for types.TypeList
func NamedTypeArgs ¶
NamedTypeArgs returns named.TypeArgs().
type TypeParam ¶
TypeParam is an alias for types.TypeParam
func NewTypeParam ¶
NewTypeParam calls types.NewTypeParam.
type TypeParamList ¶
type TypeParamList = types.TypeParamList
TypeParamList is an alias for types.TypeParamList
func ForNamed ¶
func ForNamed(named *types.Named) *TypeParamList
ForNamed extracts the (possibly empty) type parameter object list from named.
func ForSignature ¶
func ForSignature(sig *types.Signature) *TypeParamList
ForSignature returns sig.TypeParams()
func RecvTypeParams ¶
func RecvTypeParams(sig *types.Signature) *TypeParamList
RecvTypeParams returns sig.RecvTypeParams().
type Union ¶
Union is an alias for types.Union
func NewUnion ¶
NewUnion calls types.NewUnion.
Source Files ¶
common.go enabled_go118.go normalize.go termlist.go typeparams_go118.go typeterm.go
Directories ¶
Path | Synopsis |
---|---|
internal/typeparams/genericfeatures | The genericfeatures package provides utilities for detecting usage of generic programming in Go packages. |
- Version
- v0.1.8
- Published
- Dec 2, 2021
- Platform
- windows/amd64
- Imports
- 8 packages
- Last checked
- 2 hours ago –
Tools for package owners.