1  
     2  
     3  
     4  
     5  
     6  
     7  package types2
     8  
     9  import (
    10  	"cmd/compile/internal/syntax"
    11  	. "internal/types/errors"
    12  	"strings"
    13  )
    14  
    15  
    16  
    17  
    18  
    19  
    20  
    21  
    22  
    23  
    24  
    25  
    26  
    27  
    28  
    29  
    30  
    31  
    32  
    33  func (check *Checker) funcInst(T *target, pos syntax.Pos, x *operand, inst *syntax.IndexExpr, infer bool) []Type {
    34  	assert(T != nil || inst != nil)
    35  
    36  	var instErrPos poser
    37  	if inst != nil {
    38  		instErrPos = inst.Pos()
    39  		x.expr = inst 
    40  	} else {
    41  		instErrPos = pos
    42  	}
    43  	versionErr := !check.verifyVersionf(instErrPos, go1_18, "function instantiation")
    44  
    45  	
    46  	var targs []Type
    47  	var xlist []syntax.Expr
    48  	if inst != nil {
    49  		xlist = syntax.UnpackListExpr(inst.Index)
    50  		targs = check.typeList(xlist)
    51  		if targs == nil {
    52  			x.mode = invalid
    53  			return nil
    54  		}
    55  		assert(len(targs) == len(xlist))
    56  	}
    57  
    58  	
    59  	
    60  	
    61  	sig := x.typ.(*Signature)
    62  	got, want := len(targs), sig.TypeParams().Len()
    63  	if got > want {
    64  		
    65  		check.errorf(xlist[got-1], WrongTypeArgCount, "got %d type arguments but want %d", got, want)
    66  		x.mode = invalid
    67  		return nil
    68  	}
    69  
    70  	if got < want {
    71  		if !infer {
    72  			return targs
    73  		}
    74  
    75  		
    76  		
    77  		
    78  		
    79  		
    80  		
    81  		
    82  		
    83  		
    84  		
    85  		var args []*operand
    86  		var params []*Var
    87  		var reverse bool
    88  		if T != nil && sig.tparams != nil {
    89  			if !versionErr && !check.allowVersion(go1_21) {
    90  				if inst != nil {
    91  					check.versionErrorf(instErrPos, go1_21, "partially instantiated function in assignment")
    92  				} else {
    93  					check.versionErrorf(instErrPos, go1_21, "implicitly instantiated function in assignment")
    94  				}
    95  			}
    96  			gsig := NewSignatureType(nil, nil, nil, sig.params, sig.results, sig.variadic)
    97  			params = []*Var{NewParam(x.Pos(), check.pkg, "", gsig)}
    98  			
    99  			
   100  			
   101  			expr := syntax.NewName(x.Pos(), T.desc)
   102  			args = []*operand{{mode: value, expr: expr, typ: T.sig}}
   103  			reverse = true
   104  		}
   105  
   106  		
   107  		
   108  		tparams, params2 := check.renameTParams(pos, sig.TypeParams().list(), NewTuple(params...))
   109  
   110  		err := check.newError(CannotInferTypeArgs)
   111  		targs = check.infer(pos, tparams, targs, params2.(*Tuple), args, reverse, err)
   112  		if targs == nil {
   113  			if !err.empty() {
   114  				err.report()
   115  			}
   116  			x.mode = invalid
   117  			return nil
   118  		}
   119  		got = len(targs)
   120  	}
   121  	assert(got == want)
   122  
   123  	
   124  	sig = check.instantiateSignature(x.Pos(), x.expr, sig, targs, xlist)
   125  
   126  	x.typ = sig
   127  	x.mode = value
   128  	return nil
   129  }
   130  
   131  func (check *Checker) instantiateSignature(pos syntax.Pos, expr syntax.Expr, typ *Signature, targs []Type, xlist []syntax.Expr) (res *Signature) {
   132  	assert(check != nil)
   133  	assert(len(targs) == typ.TypeParams().Len())
   134  
   135  	if check.conf.Trace {
   136  		check.trace(pos, "-- instantiating signature %s with %s", typ, targs)
   137  		check.indent++
   138  		defer func() {
   139  			check.indent--
   140  			check.trace(pos, "=> %s (under = %s)", res, res.Underlying())
   141  		}()
   142  	}
   143  
   144  	
   145  	
   146  	
   147  	inst := check.instance(pos, typ, targs, nil, check.context()).(*Signature)
   148  	assert(inst.TypeParams().Len() == 0) 
   149  	check.recordInstance(expr, targs, inst)
   150  	assert(len(xlist) <= len(targs))
   151  
   152  	
   153  	check.later(func() {
   154  		tparams := typ.TypeParams().list()
   155  		
   156  		if i, err := check.verify(pos, tparams, targs, check.context()); err != nil {
   157  			
   158  			pos := pos
   159  			if i < len(xlist) {
   160  				pos = syntax.StartPos(xlist[i])
   161  			}
   162  			check.softErrorf(pos, InvalidTypeArg, "%s", err)
   163  		} else {
   164  			check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist)
   165  		}
   166  	}).describef(pos, "verify instantiation")
   167  
   168  	return inst
   169  }
   170  
   171  func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
   172  	var inst *syntax.IndexExpr 
   173  	if iexpr, _ := call.Fun.(*syntax.IndexExpr); iexpr != nil {
   174  		if check.indexExpr(x, iexpr) {
   175  			
   176  			
   177  			
   178  			assert(x.mode == value)
   179  			inst = iexpr
   180  		}
   181  		x.expr = iexpr
   182  		check.record(x)
   183  	} else {
   184  		check.exprOrType(x, call.Fun, true)
   185  	}
   186  	
   187  
   188  	switch x.mode {
   189  	case invalid:
   190  		check.use(call.ArgList...)
   191  		x.expr = call
   192  		return statement
   193  
   194  	case typexpr:
   195  		
   196  		check.nonGeneric(nil, x)
   197  		if x.mode == invalid {
   198  			return conversion
   199  		}
   200  		T := x.typ
   201  		x.mode = invalid
   202  		switch n := len(call.ArgList); n {
   203  		case 0:
   204  			check.errorf(call, WrongArgCount, "missing argument in conversion to %s", T)
   205  		case 1:
   206  			check.expr(nil, x, call.ArgList[0])
   207  			if x.mode != invalid {
   208  				if t, _ := T.Underlying().(*Interface); t != nil && !isTypeParam(T) {
   209  					if !t.IsMethodSet() {
   210  						check.errorf(call, MisplacedConstraintIface, "cannot use interface %s in conversion (contains specific type constraints or is comparable)", T)
   211  						break
   212  					}
   213  				}
   214  				if hasDots(call) {
   215  					check.errorf(call.ArgList[0], BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
   216  					break
   217  				}
   218  				check.conversion(x, T)
   219  			}
   220  		default:
   221  			check.use(call.ArgList...)
   222  			check.errorf(call.ArgList[n-1], WrongArgCount, "too many arguments in conversion to %s", T)
   223  		}
   224  		x.expr = call
   225  		return conversion
   226  
   227  	case builtin:
   228  		
   229  		id := x.id
   230  		if !check.builtin(x, call, id) {
   231  			x.mode = invalid
   232  		}
   233  		x.expr = call
   234  		
   235  		if x.mode != invalid && x.mode != constant_ {
   236  			check.hasCallOrRecv = true
   237  		}
   238  		return predeclaredFuncs[id].kind
   239  	}
   240  
   241  	
   242  	
   243  	cgocall := x.mode == cgofunc
   244  
   245  	
   246  	
   247  	u, err := commonUnder(x.typ, func(t, u Type) *typeError {
   248  		if _, ok := u.(*Signature); u != nil && !ok {
   249  			return typeErrorf("%s is not a function", t)
   250  		}
   251  		return nil
   252  	})
   253  	if err != nil {
   254  		check.errorf(x, InvalidCall, invalidOp+"cannot call %s: %s", x, err.format(check))
   255  		x.mode = invalid
   256  		x.expr = call
   257  		return statement
   258  	}
   259  	sig := u.(*Signature) 
   260  
   261  	
   262  	wasGeneric := sig.TypeParams().Len() > 0
   263  
   264  	
   265  	var xlist []syntax.Expr
   266  	var targs []Type
   267  	if inst != nil {
   268  		xlist = syntax.UnpackListExpr(inst.Index)
   269  		targs = check.typeList(xlist)
   270  		if targs == nil {
   271  			check.use(call.ArgList...)
   272  			x.mode = invalid
   273  			x.expr = call
   274  			return statement
   275  		}
   276  		assert(len(targs) == len(xlist))
   277  
   278  		
   279  		got, want := len(targs), sig.TypeParams().Len()
   280  		if got > want {
   281  			check.errorf(xlist[want], WrongTypeArgCount, "got %d type arguments but want %d", got, want)
   282  			check.use(call.ArgList...)
   283  			x.mode = invalid
   284  			x.expr = call
   285  			return statement
   286  		}
   287  
   288  		
   289  		
   290  		
   291  		
   292  		
   293  		if got == want && want > 0 {
   294  			check.verifyVersionf(inst, go1_18, "function instantiation")
   295  			sig = check.instantiateSignature(inst.Pos(), inst, sig, targs, xlist)
   296  			
   297  			
   298  			targs = nil
   299  			xlist = nil
   300  		}
   301  	}
   302  
   303  	
   304  	args, atargs := check.genericExprList(call.ArgList)
   305  	sig = check.arguments(call, sig, targs, xlist, args, atargs)
   306  
   307  	if wasGeneric && sig.TypeParams().Len() == 0 {
   308  		
   309  		check.recordTypeAndValue(call.Fun, value, sig, nil)
   310  	}
   311  
   312  	
   313  	switch sig.results.Len() {
   314  	case 0:
   315  		x.mode = novalue
   316  	case 1:
   317  		if cgocall {
   318  			x.mode = commaerr
   319  		} else {
   320  			x.mode = value
   321  		}
   322  		x.typ = sig.results.vars[0].typ 
   323  	default:
   324  		x.mode = value
   325  		x.typ = sig.results
   326  	}
   327  	x.expr = call
   328  	check.hasCallOrRecv = true
   329  
   330  	
   331  	
   332  	if x.mode == value && sig.TypeParams().Len() > 0 && isParameterized(sig.TypeParams().list(), x.typ) {
   333  		x.mode = invalid
   334  	}
   335  
   336  	return statement
   337  }
   338  
   339  
   340  
   341  func (check *Checker) exprList(elist []syntax.Expr) (xlist []*operand) {
   342  	if n := len(elist); n == 1 {
   343  		xlist, _ = check.multiExpr(elist[0], false)
   344  	} else if n > 1 {
   345  		
   346  		xlist = make([]*operand, n)
   347  		for i, e := range elist {
   348  			var x operand
   349  			check.expr(nil, &x, e)
   350  			xlist[i] = &x
   351  		}
   352  	}
   353  	return
   354  }
   355  
   356  
   357  
   358  
   359  
   360  
   361  
   362  
   363  func (check *Checker) genericExprList(elist []syntax.Expr) (resList []*operand, targsList [][]Type) {
   364  	if debug {
   365  		defer func() {
   366  			
   367  			for i, x := range resList {
   368  				if i < len(targsList) {
   369  					if n := len(targsList[i]); n > 0 {
   370  						
   371  						assert(n < x.typ.(*Signature).TypeParams().Len())
   372  					}
   373  				}
   374  			}
   375  		}()
   376  	}
   377  
   378  	
   379  	
   380  	infer := true 
   381  	n := len(elist)
   382  	if n > 0 && check.allowVersion(go1_21) {
   383  		infer = false
   384  	}
   385  
   386  	if n == 1 {
   387  		
   388  		e := elist[0]
   389  		var x operand
   390  		if inst, _ := e.(*syntax.IndexExpr); inst != nil && check.indexExpr(&x, inst) {
   391  			
   392  			targs := check.funcInst(nil, x.Pos(), &x, inst, infer)
   393  			if targs != nil {
   394  				
   395  				targsList = [][]Type{targs}
   396  				
   397  				x.expr = inst
   398  			} else {
   399  				
   400  				
   401  				check.record(&x)
   402  			}
   403  			resList = []*operand{&x}
   404  		} else {
   405  			
   406  			check.rawExpr(nil, &x, e, nil, true)
   407  			check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
   408  			if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
   409  				
   410  				resList = make([]*operand, t.Len())
   411  				for i, v := range t.vars {
   412  					resList[i] = &operand{mode: value, expr: e, typ: v.typ}
   413  				}
   414  			} else {
   415  				
   416  				resList = []*operand{&x}
   417  			}
   418  		}
   419  	} else if n > 1 {
   420  		
   421  		resList = make([]*operand, n)
   422  		targsList = make([][]Type, n)
   423  		for i, e := range elist {
   424  			var x operand
   425  			if inst, _ := e.(*syntax.IndexExpr); inst != nil && check.indexExpr(&x, inst) {
   426  				
   427  				targs := check.funcInst(nil, x.Pos(), &x, inst, infer)
   428  				if targs != nil {
   429  					
   430  					targsList[i] = targs
   431  					
   432  					x.expr = inst
   433  				} else {
   434  					
   435  					
   436  					check.record(&x)
   437  				}
   438  			} else {
   439  				
   440  				check.genericExpr(&x, e)
   441  			}
   442  			resList[i] = &x
   443  		}
   444  	}
   445  
   446  	return
   447  }
   448  
   449  
   450  
   451  
   452  
   453  
   454  
   455  
   456  
   457  
   458  
   459  
   460  func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []Type, xlist []syntax.Expr, args []*operand, atargs [][]Type) (rsig *Signature) {
   461  	rsig = sig
   462  
   463  	
   464  	
   465  	
   466  	
   467  	
   468  	
   469  	
   470  	
   471  
   472  	nargs := len(args)
   473  	npars := sig.params.Len()
   474  	ddd := hasDots(call)
   475  
   476  	
   477  	sigParams := sig.params 
   478  	adjusted := false       
   479  	if sig.variadic {
   480  		if ddd {
   481  			
   482  			if len(call.ArgList) == 1 && nargs > 1 {
   483  				
   484  				
   485  				check.errorf(call, InvalidDotDotDot, "cannot use ... with %d-valued %s", nargs, call.ArgList[0])
   486  				return
   487  			}
   488  		} else {
   489  			
   490  			if nargs >= npars-1 {
   491  				
   492  				
   493  				
   494  				vars := make([]*Var, npars-1) 
   495  				copy(vars, sig.params.vars)
   496  				last := sig.params.vars[npars-1]
   497  				typ := last.typ.(*Slice).elem
   498  				for len(vars) < nargs {
   499  					vars = append(vars, NewParam(last.pos, last.pkg, last.name, typ))
   500  				}
   501  				sigParams = NewTuple(vars...) 
   502  				adjusted = true
   503  				npars = nargs
   504  			} else {
   505  				
   506  				npars-- 
   507  			}
   508  		}
   509  	} else {
   510  		if ddd {
   511  			
   512  			
   513  			check.errorf(call, NonVariadicDotDotDot, "cannot use ... in call to non-variadic %s", call.Fun)
   514  			return
   515  		}
   516  		
   517  	}
   518  
   519  	
   520  	if nargs != npars {
   521  		var at poser = call
   522  		qualifier := "not enough"
   523  		if nargs > npars {
   524  			at = args[npars].expr 
   525  			qualifier = "too many"
   526  		} else if nargs > 0 {
   527  			at = args[nargs-1].expr 
   528  		}
   529  		
   530  		var params []*Var
   531  		if sig.params != nil {
   532  			params = sig.params.vars
   533  		}
   534  		err := check.newError(WrongArgCount)
   535  		err.addf(at, "%s arguments in call to %s", qualifier, call.Fun)
   536  		err.addf(nopos, "have %s", check.typesSummary(operandTypes(args), false, ddd))
   537  		err.addf(nopos, "want %s", check.typesSummary(varTypes(params), sig.variadic, false))
   538  		err.report()
   539  		return
   540  	}
   541  
   542  	
   543  	var tparams []*TypeParam
   544  
   545  	
   546  	n := sig.TypeParams().Len()
   547  	if n > 0 {
   548  		if !check.allowVersion(go1_18) {
   549  			if iexpr, _ := call.Fun.(*syntax.IndexExpr); iexpr != nil {
   550  				check.versionErrorf(iexpr, go1_18, "function instantiation")
   551  			} else {
   552  				check.versionErrorf(call, go1_18, "implicit function instantiation")
   553  			}
   554  		}
   555  		
   556  		var tmp Type
   557  		tparams, tmp = check.renameTParams(call.Pos(), sig.TypeParams().list(), sigParams)
   558  		sigParams = tmp.(*Tuple)
   559  		
   560  		for len(targs) < len(tparams) {
   561  			targs = append(targs, nil)
   562  		}
   563  	}
   564  	assert(len(tparams) == len(targs))
   565  
   566  	
   567  	var genericArgs []int 
   568  	if enableReverseTypeInference {
   569  		for i, arg := range args {
   570  			
   571  			if asig, _ := arg.typ.(*Signature); asig != nil && asig.TypeParams().Len() > 0 {
   572  				
   573  				
   574  				
   575  				
   576  				
   577  				
   578  				asig = clone(asig)
   579  				
   580  				
   581  				
   582  				
   583  				atparams, tmp := check.renameTParams(call.Pos(), asig.TypeParams().list(), asig)
   584  				asig = tmp.(*Signature)
   585  				asig.tparams = &TypeParamList{atparams} 
   586  				arg.typ = asig                          
   587  				tparams = append(tparams, atparams...)
   588  				
   589  				if i < len(atargs) {
   590  					targs = append(targs, atargs[i]...)
   591  				}
   592  				
   593  				for len(targs) < len(tparams) {
   594  					targs = append(targs, nil)
   595  				}
   596  				genericArgs = append(genericArgs, i)
   597  			}
   598  		}
   599  	}
   600  	assert(len(tparams) == len(targs))
   601  
   602  	
   603  	_ = len(genericArgs) > 0 && check.verifyVersionf(args[genericArgs[0]], go1_21, "implicitly instantiated function as argument")
   604  
   605  	
   606  	
   607  	
   608  
   609  	
   610  	if len(tparams) > 0 {
   611  		err := check.newError(CannotInferTypeArgs)
   612  		targs = check.infer(call.Pos(), tparams, targs, sigParams, args, false, err)
   613  		if targs == nil {
   614  			
   615  			
   616  			
   617  			
   618  			if !err.empty() {
   619  				check.errorf(err.pos(), CannotInferTypeArgs, "in call to %s, %s", call.Fun, err.msg())
   620  			}
   621  			return
   622  		}
   623  
   624  		
   625  		if n > 0 {
   626  			rsig = check.instantiateSignature(call.Pos(), call.Fun, sig, targs[:n], xlist)
   627  			
   628  			
   629  			
   630  			if adjusted {
   631  				sigParams = check.subst(call.Pos(), sigParams, makeSubstMap(tparams[:n], targs[:n]), nil, check.context()).(*Tuple)
   632  			} else {
   633  				sigParams = rsig.params
   634  			}
   635  		}
   636  
   637  		
   638  		j := n
   639  		for _, i := range genericArgs {
   640  			arg := args[i]
   641  			asig := arg.typ.(*Signature)
   642  			k := j + asig.TypeParams().Len()
   643  			
   644  			arg.typ = check.instantiateSignature(call.Pos(), arg.expr, asig, targs[j:k], nil) 
   645  			check.record(arg)                                                                 
   646  			j = k
   647  		}
   648  	}
   649  
   650  	
   651  	if len(args) > 0 {
   652  		context := check.sprintf("argument to %s", call.Fun)
   653  		for i, a := range args {
   654  			check.assignment(a, sigParams.vars[i].typ, context)
   655  		}
   656  	}
   657  
   658  	return
   659  }
   660  
   661  var cgoPrefixes = [...]string{
   662  	"_Ciconst_",
   663  	"_Cfconst_",
   664  	"_Csconst_",
   665  	"_Ctype_",
   666  	"_Cvar_", 
   667  	"_Cfpvar_fp_",
   668  	"_Cfunc_",
   669  	"_Cmacro_", 
   670  }
   671  
   672  func (check *Checker) selector(x *operand, e *syntax.SelectorExpr, def *TypeName, wantType bool) {
   673  	
   674  	var (
   675  		obj      Object
   676  		index    []int
   677  		indirect bool
   678  	)
   679  
   680  	sel := e.Sel.Value
   681  	
   682  	
   683  	
   684  	
   685  	if ident, ok := e.X.(*syntax.Name); ok {
   686  		obj := check.lookup(ident.Value)
   687  		if pname, _ := obj.(*PkgName); pname != nil {
   688  			assert(pname.pkg == check.pkg)
   689  			check.recordUse(ident, pname)
   690  			check.usedPkgNames[pname] = true
   691  			pkg := pname.imported
   692  
   693  			var exp Object
   694  			funcMode := value
   695  			if pkg.cgo {
   696  				
   697  				
   698  				
   699  				if sel == "malloc" {
   700  					sel = "_CMalloc"
   701  				} else {
   702  					funcMode = cgofunc
   703  				}
   704  				for _, prefix := range cgoPrefixes {
   705  					
   706  					
   707  					exp = check.lookup(prefix + sel)
   708  					if exp != nil {
   709  						break
   710  					}
   711  				}
   712  				if exp == nil {
   713  					if isValidName(sel) {
   714  						check.errorf(e.Sel, UndeclaredImportedName, "undefined: %s", syntax.Expr(e)) 
   715  					}
   716  					goto Error
   717  				}
   718  				check.objDecl(exp, nil)
   719  			} else {
   720  				exp = pkg.scope.Lookup(sel)
   721  				if exp == nil {
   722  					if !pkg.fake && isValidName(sel) {
   723  						
   724  						exps := pkg.scope.lookupIgnoringCase(sel, true)
   725  						if len(exps) >= 1 {
   726  							
   727  							check.errorf(e.Sel, UndeclaredImportedName, "undefined: %s (but have %s)", syntax.Expr(e), exps[0].Name())
   728  						} else {
   729  							check.errorf(e.Sel, UndeclaredImportedName, "undefined: %s", syntax.Expr(e))
   730  						}
   731  					}
   732  					goto Error
   733  				}
   734  				if !exp.Exported() {
   735  					check.errorf(e.Sel, UnexportedName, "name %s not exported by package %s", sel, pkg.name)
   736  					
   737  				}
   738  			}
   739  			check.recordUse(e.Sel, exp)
   740  
   741  			
   742  			
   743  			switch exp := exp.(type) {
   744  			case *Const:
   745  				assert(exp.Val() != nil)
   746  				x.mode = constant_
   747  				x.typ = exp.typ
   748  				x.val = exp.val
   749  			case *TypeName:
   750  				x.mode = typexpr
   751  				x.typ = exp.typ
   752  			case *Var:
   753  				x.mode = variable
   754  				x.typ = exp.typ
   755  				if pkg.cgo && strings.HasPrefix(exp.name, "_Cvar_") {
   756  					x.typ = x.typ.(*Pointer).base
   757  				}
   758  			case *Func:
   759  				x.mode = funcMode
   760  				x.typ = exp.typ
   761  				if pkg.cgo && strings.HasPrefix(exp.name, "_Cmacro_") {
   762  					x.mode = value
   763  					x.typ = x.typ.(*Signature).results.vars[0].typ
   764  				}
   765  			case *Builtin:
   766  				x.mode = builtin
   767  				x.typ = exp.typ
   768  				x.id = exp.id
   769  			default:
   770  				check.dump("%v: unexpected object %v", atPos(e.Sel), exp)
   771  				panic("unreachable")
   772  			}
   773  			x.expr = e
   774  			return
   775  		}
   776  	}
   777  
   778  	check.exprOrType(x, e.X, false)
   779  	switch x.mode {
   780  	case typexpr:
   781  		
   782  		if def != nil && def.typ == x.typ {
   783  			check.cycleError([]Object{def}, 0)
   784  			goto Error
   785  		}
   786  	case builtin:
   787  		check.errorf(e.Pos(), UncalledBuiltin, "invalid use of %s in selector expression", x)
   788  		goto Error
   789  	case invalid:
   790  		goto Error
   791  	}
   792  
   793  	
   794  	
   795  	
   796  	
   797  	
   798  	
   799  	
   800  	
   801  	
   802  	
   803  	
   804  	
   805  	
   806  	
   807  	if wantType {
   808  		check.errorf(e.Sel, NotAType, "%s is not a type", syntax.Expr(e))
   809  		goto Error
   810  	}
   811  
   812  	obj, index, indirect = lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel, false)
   813  	if obj == nil {
   814  		
   815  		if !isValid(x.typ.Underlying()) {
   816  			goto Error
   817  		}
   818  
   819  		if index != nil {
   820  			
   821  			check.errorf(e.Sel, AmbiguousSelector, "ambiguous selector %s.%s", x.expr, sel)
   822  			goto Error
   823  		}
   824  
   825  		if indirect {
   826  			if x.mode == typexpr {
   827  				check.errorf(e.Sel, InvalidMethodExpr, "invalid method expression %s.%s (needs pointer receiver (*%s).%s)", x.typ, sel, x.typ, sel)
   828  			} else {
   829  				check.errorf(e.Sel, InvalidMethodExpr, "cannot call pointer method %s on %s", sel, x.typ)
   830  			}
   831  			goto Error
   832  		}
   833  
   834  		var why string
   835  		if isInterfacePtr(x.typ) {
   836  			why = check.interfacePtrError(x.typ)
   837  		} else {
   838  			alt, _, _ := lookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel, true)
   839  			why = check.lookupError(x.typ, sel, alt, false)
   840  		}
   841  		check.errorf(e.Sel, MissingFieldOrMethod, "%s.%s undefined (%s)", x.expr, sel, why)
   842  		goto Error
   843  	}
   844  
   845  	
   846  	if m, _ := obj.(*Func); m != nil {
   847  		check.objDecl(m, nil)
   848  	}
   849  
   850  	if x.mode == typexpr {
   851  		
   852  		m, _ := obj.(*Func)
   853  		if m == nil {
   854  			check.errorf(e.Sel, MissingFieldOrMethod, "%s.%s undefined (type %s has no method %s)", x.expr, sel, x.typ, sel)
   855  			goto Error
   856  		}
   857  
   858  		check.recordSelection(e, MethodExpr, x.typ, m, index, indirect)
   859  
   860  		sig := m.typ.(*Signature)
   861  		if sig.recv == nil {
   862  			check.error(e, InvalidDeclCycle, "illegal cycle in method declaration")
   863  			goto Error
   864  		}
   865  
   866  		
   867  		
   868  		var params []*Var
   869  		if sig.params != nil {
   870  			params = sig.params.vars
   871  		}
   872  		
   873  		
   874  		
   875  		
   876  		
   877  		
   878  		name := ""
   879  		if len(params) > 0 && params[0].name != "" {
   880  			
   881  			name = sig.recv.name
   882  			if name == "" {
   883  				name = "_"
   884  			}
   885  		}
   886  		params = append([]*Var{NewParam(sig.recv.pos, sig.recv.pkg, name, x.typ)}, params...)
   887  		x.mode = value
   888  		x.typ = &Signature{
   889  			tparams:  sig.tparams,
   890  			params:   NewTuple(params...),
   891  			results:  sig.results,
   892  			variadic: sig.variadic,
   893  		}
   894  
   895  		check.addDeclDep(m)
   896  
   897  	} else {
   898  		
   899  		switch obj := obj.(type) {
   900  		case *Var:
   901  			check.recordSelection(e, FieldVal, x.typ, obj, index, indirect)
   902  			if x.mode == variable || indirect {
   903  				x.mode = variable
   904  			} else {
   905  				x.mode = value
   906  			}
   907  			x.typ = obj.typ
   908  
   909  		case *Func:
   910  			
   911  			
   912  			check.recordSelection(e, MethodVal, x.typ, obj, index, indirect)
   913  
   914  			x.mode = value
   915  
   916  			
   917  			sig := *obj.typ.(*Signature)
   918  			sig.recv = nil
   919  			x.typ = &sig
   920  
   921  			check.addDeclDep(obj)
   922  
   923  		default:
   924  			panic("unreachable")
   925  		}
   926  	}
   927  
   928  	
   929  	x.expr = e
   930  	return
   931  
   932  Error:
   933  	x.mode = invalid
   934  	x.typ = Typ[Invalid]
   935  	x.expr = e
   936  }
   937  
   938  
   939  
   940  
   941  
   942  
   943  func (check *Checker) use(args ...syntax.Expr) bool { return check.useN(args, false) }
   944  
   945  
   946  
   947  
   948  func (check *Checker) useLHS(args ...syntax.Expr) bool { return check.useN(args, true) }
   949  
   950  func (check *Checker) useN(args []syntax.Expr, lhs bool) bool {
   951  	ok := true
   952  	for _, e := range args {
   953  		if !check.use1(e, lhs) {
   954  			ok = false
   955  		}
   956  	}
   957  	return ok
   958  }
   959  
   960  func (check *Checker) use1(e syntax.Expr, lhs bool) bool {
   961  	var x operand
   962  	x.mode = value 
   963  	switch n := syntax.Unparen(e).(type) {
   964  	case nil:
   965  		
   966  	case *syntax.Name:
   967  		
   968  		if n.Value == "_" {
   969  			break
   970  		}
   971  		
   972  		
   973  		
   974  		var v *Var
   975  		var v_used bool
   976  		if lhs {
   977  			if obj := check.lookup(n.Value); obj != nil {
   978  				
   979  				
   980  				
   981  				if w, _ := obj.(*Var); w != nil && w.pkg == check.pkg {
   982  					v = w
   983  					v_used = check.usedVars[v]
   984  				}
   985  			}
   986  		}
   987  		check.exprOrType(&x, n, true)
   988  		if v != nil {
   989  			check.usedVars[v] = v_used 
   990  		}
   991  	case *syntax.ListExpr:
   992  		return check.useN(n.ElemList, lhs)
   993  	default:
   994  		check.rawExpr(nil, &x, e, nil, true)
   995  	}
   996  	return x.mode != invalid
   997  }
   998  
View as plain text