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
- func ForFuncDecl(n *ast.FuncDecl) *ast.FieldList
- func ForTypeDecl(n *ast.TypeSpec) *ast.FieldList
- func GetInferred(info *types.Info, e ast.Expr) ([]types.Type, *types.Signature)
- func InitInferred(info *types.Info)
- func Instantiate(env *Environment, typ types.Type, targs []types.Type, validate bool) (types.Type, error)
- func IsComparable(iface *types.Interface) bool
- func IsConstraint(iface *types.Interface) bool
- func NamedTypeArgs(named *types.Named) []types.Type
- func SetForNamed(n *types.Named, tparams []*TypeParam)
- func SetForSignature(sig *types.Signature, tparams []*TypeParam)
- func SetRecvTypeParams(sig *types.Signature, rparams []*TypeParam)
- func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type)
- type Environment
- type IndexExprData
- type Term
- 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.
Functions ¶
func ForFuncDecl ¶
ForFuncDecl returns n.Type.TypeParams.
func ForTypeDecl ¶
ForTypeDecl returns n.TypeParams.
func GetInferred ¶
GetInferred extracts inferred type information from info for e.
The expression e may have an inferred type if it is an *ast.IndexExpr representing partial instantiation of a generic function type for which type arguments have been inferred using constraint type inference, or if it is an *ast.CallExpr for which type type arguments have be inferred using both constraint type inference and function argument inference.
func InitInferred ¶
InitInferred initializes info to record inferred type information.
func Instantiate ¶
func Instantiate(env *Environment, typ types.Type, targs []types.Type, validate bool) (types.Type, error)
Instantiate calls types.Instantiate.
func IsComparable ¶
IsComparable calls iface.IsComparable().
func IsConstraint ¶
IsConstraint calls iface.IsConstraint().
func NamedTypeArgs ¶
NamedTypeArgs extracts the (possibly empty) type argument list from named.
func SetForNamed ¶
SetForNamed sets the type params tparams on n. Each tparam must be of dynamic type *types.TypeParam.
func SetForSignature ¶
SetForSignature calls sig.SetTypeParams(tparams)
func SetRecvTypeParams ¶
SetRecvTypeParams calls sig.SetRecvTypeParams(rparams).
func SetTypeParamConstraint ¶
SetTypeParamConstraint calls tparam.SetConstraint(constraint).
Types ¶
type Environment ¶
type Environment = types.Environment
Environment is an alias for types.Environment.
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.
type Term ¶
Term is an alias for types.Term.
func NewTerm ¶
NewTerm calls types.NewTerm.
type TypeList ¶
TypeList is an alias for types.TypeList
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 typeparams_go118.go
- Version
- v0.1.6
- Published
- Sep 17, 2021
- Platform
- windows/amd64
- Imports
- 3 packages
- Last checked
- 2 hours ago –
Tools for package owners.