github.com/goplus/gogen@v1.16.0/inferfunc_go121.go (about)

     1  //go:build go1.21
     2  // +build go1.21
     3  
     4  package gogen
     5  
     6  import (
     7  	"go/types"
     8  
     9  	"github.com/goplus/gogen/internal"
    10  )
    11  
    12  func inferFunc(pkg *Package, fn *internal.Elem, sig *types.Signature, targs []types.Type, args []*Element, flags InstrFlags) ([]types.Type, types.Type, error) {
    13  	args, err := checkInferArgs(pkg, fn, sig, args, flags)
    14  	if err != nil {
    15  		return nil, nil, err
    16  	}
    17  	xlist := make([]*operand, len(args))
    18  	tp := sig.TypeParams()
    19  	n := tp.Len()
    20  	tparams := make([]*types.TypeParam, n)
    21  	for i := 0; i < n; i++ {
    22  		tparams[i] = tp.At(i)
    23  	}
    24  	for i, arg := range args {
    25  		xlist[i] = &operand{
    26  			mode: value,
    27  			expr: arg.Val,
    28  			typ:  arg.Type,
    29  			val:  arg.CVal,
    30  		}
    31  		tt := arg.Type
    32  	retry:
    33  		switch t := tt.(type) {
    34  		case *types.Slice:
    35  			tt = t.Elem()
    36  			goto retry
    37  		case *inferFuncType:
    38  			xlist[i].typ = t.typ
    39  			if tp := t.typ.TypeParams(); tp != nil {
    40  				for i := 0; i < tp.Len(); i++ {
    41  					tparams = append(tparams, tp.At(i))
    42  				}
    43  			}
    44  		case *types.Signature:
    45  			if tp := t.TypeParams(); tp != nil {
    46  				for i := 0; i < tp.Len(); i++ {
    47  					tparams = append(tparams, tp.At(i))
    48  				}
    49  			}
    50  		}
    51  	}
    52  	targs, err = infer(pkg, fn.Val, tparams, targs, sig.Params(), xlist)
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  	typ, err := types.Instantiate(pkg.cb.ctxt, sig, targs[:n], true)
    57  	return targs, typ, err
    58  }