github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/typeparams/typeparams_go118.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.18
     6  // +build go1.18
     7  
     8  package typeparams
     9  
    10  import (
    11  	"go/ast"
    12  	"go/types"
    13  )
    14  
    15  // IndexListExpr is an alias for ast.IndexListExpr.
    16  type IndexListExpr = ast.IndexListExpr
    17  
    18  // ForTypeSpec returns n.TypeParams.
    19  func ForTypeSpec(n *ast.TypeSpec) *ast.FieldList {
    20  	if n == nil {
    21  		return nil
    22  	}
    23  	return n.TypeParams
    24  }
    25  
    26  // ForFuncType returns n.TypeParams.
    27  func ForFuncType(n *ast.FuncType) *ast.FieldList {
    28  	if n == nil {
    29  		return nil
    30  	}
    31  	return n.TypeParams
    32  }
    33  
    34  // TypeParam is an alias for types.TypeParam
    35  type TypeParam = types.TypeParam
    36  
    37  // TypeParamList is an alias for types.TypeParamList
    38  type TypeParamList = types.TypeParamList
    39  
    40  // TypeList is an alias for types.TypeList
    41  type TypeList = types.TypeList
    42  
    43  // NewTypeParam calls types.NewTypeParam.
    44  func NewTypeParam(name *types.TypeName, constraint types.Type) *TypeParam {
    45  	return types.NewTypeParam(name, constraint)
    46  }
    47  
    48  // SetTypeParamConstraint calls tparam.SetConstraint(constraint).
    49  func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type) {
    50  	tparam.SetConstraint(constraint)
    51  }
    52  
    53  // NewSignatureType calls types.NewSignatureType.
    54  func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature {
    55  	return types.NewSignatureType(recv, recvTypeParams, typeParams, params, results, variadic)
    56  }
    57  
    58  // ForSignature returns sig.TypeParams()
    59  func ForSignature(sig *types.Signature) *TypeParamList {
    60  	return sig.TypeParams()
    61  }
    62  
    63  // RecvTypeParams returns sig.RecvTypeParams().
    64  func RecvTypeParams(sig *types.Signature) *TypeParamList {
    65  	return sig.RecvTypeParams()
    66  }
    67  
    68  // IsComparable calls iface.IsComparable().
    69  func IsComparable(iface *types.Interface) bool {
    70  	return iface.IsComparable()
    71  }
    72  
    73  // IsMethodSet calls iface.IsMethodSet().
    74  func IsMethodSet(iface *types.Interface) bool {
    75  	return iface.IsMethodSet()
    76  }
    77  
    78  // IsImplicit calls iface.IsImplicit().
    79  func IsImplicit(iface *types.Interface) bool {
    80  	return iface.IsImplicit()
    81  }
    82  
    83  // MarkImplicit calls iface.MarkImplicit().
    84  func MarkImplicit(iface *types.Interface) {
    85  	iface.MarkImplicit()
    86  }
    87  
    88  // ForNamed extracts the (possibly empty) type parameter object list from
    89  // named.
    90  func ForNamed(named *types.Named) *TypeParamList {
    91  	return named.TypeParams()
    92  }
    93  
    94  // SetForNamed sets the type params tparams on n. Each tparam must be of
    95  // dynamic type *types.TypeParam.
    96  func SetForNamed(n *types.Named, tparams []*TypeParam) {
    97  	n.SetTypeParams(tparams)
    98  }
    99  
   100  // NamedTypeArgs returns named.TypeArgs().
   101  func NamedTypeArgs(named *types.Named) *TypeList {
   102  	return named.TypeArgs()
   103  }
   104  
   105  // NamedTypeOrigin returns named.Orig().
   106  func NamedTypeOrigin(named *types.Named) types.Type {
   107  	return named.Origin()
   108  }
   109  
   110  // Term is an alias for types.Term.
   111  type Term = types.Term
   112  
   113  // NewTerm calls types.NewTerm.
   114  func NewTerm(tilde bool, typ types.Type) *Term {
   115  	return types.NewTerm(tilde, typ)
   116  }
   117  
   118  // Union is an alias for types.Union
   119  type Union = types.Union
   120  
   121  // NewUnion calls types.NewUnion.
   122  func NewUnion(terms []*Term) *Union {
   123  	return types.NewUnion(terms)
   124  }
   125  
   126  // InitInstanceInfo initializes info to record information about type and
   127  // function instances.
   128  func InitInstanceInfo(info *types.Info) {
   129  	info.Instances = make(map[*ast.Ident]types.Instance)
   130  }
   131  
   132  // Instance is an alias for types.Instance.
   133  type Instance = types.Instance
   134  
   135  // GetInstances returns info.Instances.
   136  func GetInstances(info *types.Info) map[*ast.Ident]Instance {
   137  	return info.Instances
   138  }
   139  
   140  // Context is an alias for types.Context.
   141  type Context = types.Context
   142  
   143  // NewContext calls types.NewContext.
   144  func NewContext() *Context {
   145  	return types.NewContext()
   146  }
   147  
   148  // Instantiate calls types.Instantiate.
   149  func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error) {
   150  	return types.Instantiate(ctxt, typ, targs, validate)
   151  }