toolsgolang.org/x/tools/internal/typeparams Index | Files

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

const Enabled = true

Enabled reports whether type parameters are enabled in the current build environment.

Functions

func ForFuncDecl

func ForFuncDecl(n *ast.FuncDecl) *ast.FieldList

ForFuncDecl returns n.Type.TypeParams.

func ForTypeDecl

func ForTypeDecl(n *ast.TypeSpec) *ast.FieldList

ForTypeDecl returns n.TypeParams.

func GetInferred

func GetInferred(info *types.Info, e ast.Expr) ([]types.Type, *types.Signature)

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

func InitInferred(info *types.Info)

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

func IsComparable(iface *types.Interface) bool

IsComparable calls iface.IsComparable().

func IsConstraint

func IsConstraint(iface *types.Interface) bool

IsConstraint calls iface.IsConstraint().

func NamedTypeArgs

func NamedTypeArgs(named *types.Named) []types.Type

NamedTypeArgs extracts the (possibly empty) type argument list from named.

func SetForNamed

func SetForNamed(n *types.Named, tparams []*TypeParam)

SetForNamed sets the type params tparams on n. Each tparam must be of dynamic type *types.TypeParam.

func SetForSignature

func SetForSignature(sig *types.Signature, tparams []*TypeParam)

SetForSignature calls sig.SetTypeParams(tparams)

func SetRecvTypeParams

func SetRecvTypeParams(sig *types.Signature, rparams []*TypeParam)

SetRecvTypeParams calls sig.SetRecvTypeParams(rparams).

func SetTypeParamConstraint

func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type)

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

type Term = types.Term

Term is an alias for types.Term.

func NewTerm

func NewTerm(tilde bool, typ types.Type) *Term

NewTerm calls types.NewTerm.

type TypeList

type TypeList = types.TypeList

TypeList is an alias for types.TypeList

type TypeParam

type TypeParam = types.TypeParam

TypeParam is an alias for types.TypeParam

func NewTypeParam

func NewTypeParam(name *types.TypeName, constraint types.Type) *TypeParam

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

type Union = types.Union

Union is an alias for types.Union

func NewUnion

func NewUnion(terms []*Term) *Union

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.