Documentation ¶
Overview ¶
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 MarkImplicit(iface *types.Interface)
- func NamedTypeOrigin(named *types.Named) types.Type
- func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, ...) *types.Signature
- func NormalizeInterface(iface *types.Interface) (*types.Interface, error)
- 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
- type TypeList
- type TypeParam
- type TypeParamList
- type Union
Constants ¶
const Enabled = true
Enabled reports whether type parameters are enabled in the current build environment.
Variables ¶
var ErrEmptyTypeSet = errors.New("empty type set")
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 IsMethodSet ¶
IsMethodSet calls iface.IsMethodSet().
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 NormalizeInterface ¶
NormalizeInterface returns the normal form of the interface iface, or nil if iface has an empty type set (i.e. there are no types that satisfy iface). If the resulting interface is non-nil, it will be identical to iface.
An error is returned if the interface type is invalid, or too complicated to reasonably normalize (for example, contains unions with more than a hundred terms).
An interface is in normal form if and only if:
- it has 0 or 1 embedded types.
- its embedded type is either a types.Union or has a concrete (non-interface) underlying type
- if the embedded type is a union, each term of the union has a concrete underlying type, and no terms may be removed without changing the type set of the interface
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 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 Term ¶
Term is an alias for types.Term.
func StructuralTerms ¶
StructuralTerms returns the normalized structural type restrictions of a type, if any. For types that are not type parameters, it returns term slice containing a single non-tilde term holding the given type. For type parameters, it returns the normalized term list of the type parameter's constraint. See NormalizeInterface for more information on the normal form of a constraint interface.
StructuralTerms returns an error if the structural term list cannot be computed. If the type set of typ is empty, it returns ErrEmptyTypeSet.
type TypeList ¶
TypeList is an alias for types.TypeList
func NamedTypeArgs ¶
NamedTypeArgs returns named.TypeArgs().
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().
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
The genericfeatures package provides utilities for detecting usage of generic programming in Go packages.
|
The genericfeatures package provides utilities for detecting usage of generic programming in Go packages. |