github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/go/types/call.go (about) 1 // Copyright 2013 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 // This file implements typechecking of call and selector expressions. 6 7 package types 8 9 import ( 10 "go/ast" 11 "go/token" 12 ) 13 14 func (check *Checker) call(x *operand, e *ast.CallExpr) exprKind { 15 check.exprOrType(x, e.Fun) 16 17 switch x.mode { 18 case invalid: 19 check.use(e.Args...) 20 x.mode = invalid 21 x.expr = e 22 return statement 23 24 case typexpr: 25 // conversion 26 T := x.typ 27 x.mode = invalid 28 switch n := len(e.Args); n { 29 case 0: 30 check.errorf(e.Rparen, "missing argument in conversion to %s", T) 31 case 1: 32 check.expr(x, e.Args[0]) 33 if x.mode != invalid { 34 check.conversion(x, T) 35 } 36 default: 37 check.errorf(e.Args[n-1].Pos(), "too many arguments in conversion to %s", T) 38 } 39 x.expr = e 40 return conversion 41 42 case builtin: 43 id := x.id 44 if !check.builtin(x, e, id) { 45 x.mode = invalid 46 } 47 x.expr = e 48 // a non-constant result implies a function call 49 if x.mode != invalid && x.mode != constant_ { 50 check.hasCallOrRecv = true 51 } 52 return predeclaredFuncs[id].kind 53 54 default: 55 // function/method call 56 sig, _ := x.typ.Underlying().(*Signature) 57 if sig == nil { 58 check.invalidOp(x.pos(), "cannot call non-function %s", x) 59 x.mode = invalid 60 x.expr = e 61 return statement 62 } 63 64 arg, n, _ := unpack(func(x *operand, i int) { check.multiExpr(x, e.Args[i]) }, len(e.Args), false) 65 if arg != nil { 66 check.arguments(x, e, sig, arg, n) 67 } else { 68 x.mode = invalid 69 } 70 71 // determine result 72 switch sig.results.Len() { 73 case 0: 74 x.mode = novalue 75 case 1: 76 x.mode = value 77 x.typ = sig.results.vars[0].typ // unpack tuple 78 default: 79 x.mode = value 80 x.typ = sig.results 81 } 82 83 x.expr = e 84 check.hasCallOrRecv = true 85 86 return statement 87 } 88 } 89 90 // use type-checks each argument. 91 // Useful to make sure expressions are evaluated 92 // (and variables are "used") in the presence of other errors. 93 func (check *Checker) use(arg ...ast.Expr) { 94 var x operand 95 for _, e := range arg { 96 check.rawExpr(&x, e, nil) 97 } 98 } 99 100 // useGetter is like use, but takes a getter instead of a list of expressions. 101 // It should be called instead of use if a getter is present to avoid repeated 102 // evaluation of the first argument (since the getter was likely obtained via 103 // unpack, which may have evaluated the first argument already). 104 func (check *Checker) useGetter(get getter, n int) { 105 var x operand 106 for i := 0; i < n; i++ { 107 get(&x, i) 108 } 109 } 110 111 // A getter sets x as the i'th operand, where 0 <= i < n and n is the total 112 // number of operands (context-specific, and maintained elsewhere). A getter 113 // type-checks the i'th operand; the details of the actual check are getter- 114 // specific. 115 type getter func(x *operand, i int) 116 117 // unpack takes a getter get and a number of operands n. If n == 1, unpack 118 // calls the incoming getter for the first operand. If that operand is 119 // invalid, unpack returns (nil, 0, false). Otherwise, if that operand is a 120 // function call, or a comma-ok expression and allowCommaOk is set, the result 121 // is a new getter and operand count providing access to the function results, 122 // or comma-ok values, respectively. The third result value reports if it 123 // is indeed the comma-ok case. In all other cases, the incoming getter and 124 // operand count are returned unchanged, and the third result value is false. 125 // 126 // In other words, if there's exactly one operand that - after type-checking 127 // by calling get - stands for multiple operands, the resulting getter provides 128 // access to those operands instead. 129 // 130 // If the returned getter is called at most once for a given operand index i 131 // (including i == 0), that operand is guaranteed to cause only one call of 132 // the incoming getter with that i. 133 // 134 func unpack(get getter, n int, allowCommaOk bool) (getter, int, bool) { 135 if n == 1 { 136 // possibly result of an n-valued function call or comma,ok value 137 var x0 operand 138 get(&x0, 0) 139 if x0.mode == invalid { 140 return nil, 0, false 141 } 142 143 if t, ok := x0.typ.(*Tuple); ok { 144 // result of an n-valued function call 145 return func(x *operand, i int) { 146 x.mode = value 147 x.expr = x0.expr 148 x.typ = t.At(i).typ 149 }, t.Len(), false 150 } 151 152 if x0.mode == mapindex || x0.mode == commaok { 153 // comma-ok value 154 if allowCommaOk { 155 a := [2]Type{x0.typ, Typ[UntypedBool]} 156 return func(x *operand, i int) { 157 x.mode = value 158 x.expr = x0.expr 159 x.typ = a[i] 160 }, 2, true 161 } 162 x0.mode = value 163 } 164 165 // single value 166 return func(x *operand, i int) { 167 if i != 0 { 168 unreachable() 169 } 170 *x = x0 171 }, 1, false 172 } 173 174 // zero or multiple values 175 return get, n, false 176 } 177 178 // arguments checks argument passing for the call with the given signature. 179 // The arg function provides the operand for the i'th argument. 180 func (check *Checker) arguments(x *operand, call *ast.CallExpr, sig *Signature, arg getter, n int) { 181 if call.Ellipsis.IsValid() { 182 // last argument is of the form x... 183 if !sig.variadic { 184 check.errorf(call.Ellipsis, "cannot use ... in call to non-variadic %s", call.Fun) 185 check.useGetter(arg, n) 186 return 187 } 188 if len(call.Args) == 1 && n > 1 { 189 // f()... is not permitted if f() is multi-valued 190 check.errorf(call.Ellipsis, "cannot use ... with %d-valued %s", n, call.Args[0]) 191 check.useGetter(arg, n) 192 return 193 } 194 } 195 196 // evaluate arguments 197 for i := 0; i < n; i++ { 198 arg(x, i) 199 if x.mode != invalid { 200 var ellipsis token.Pos 201 if i == n-1 && call.Ellipsis.IsValid() { 202 ellipsis = call.Ellipsis 203 } 204 check.argument(call.Fun, sig, i, x, ellipsis) 205 } 206 } 207 208 // check argument count 209 if sig.variadic { 210 // a variadic function accepts an "empty" 211 // last argument: count one extra 212 n++ 213 } 214 if n < sig.params.Len() { 215 check.errorf(call.Rparen, "too few arguments in call to %s", call.Fun) 216 // ok to continue 217 } 218 } 219 220 // argument checks passing of argument x to the i'th parameter of the given signature. 221 // If ellipsis is valid, the argument is followed by ... at that position in the call. 222 func (check *Checker) argument(fun ast.Expr, sig *Signature, i int, x *operand, ellipsis token.Pos) { 223 check.singleValue(x) 224 if x.mode == invalid { 225 return 226 } 227 228 n := sig.params.Len() 229 230 // determine parameter type 231 var typ Type 232 switch { 233 case i < n: 234 typ = sig.params.vars[i].typ 235 case sig.variadic: 236 typ = sig.params.vars[n-1].typ 237 if debug { 238 if _, ok := typ.(*Slice); !ok { 239 check.dump("%s: expected unnamed slice type, got %s", sig.params.vars[n-1].Pos(), typ) 240 } 241 } 242 default: 243 check.errorf(x.pos(), "too many arguments") 244 return 245 } 246 247 if ellipsis.IsValid() { 248 // argument is of the form x... and x is single-valued 249 if i != n-1 { 250 check.errorf(ellipsis, "can only use ... with matching parameter") 251 return 252 } 253 if _, ok := x.typ.Underlying().(*Slice); !ok { 254 check.errorf(x.pos(), "cannot use %s as parameter of type %s", x, typ) 255 return 256 } 257 } else if sig.variadic && i >= n-1 { 258 // use the variadic parameter slice's element type 259 typ = typ.(*Slice).elem 260 } 261 262 check.assignment(x, typ, check.sprintf("argument to %s", fun)) 263 } 264 265 func (check *Checker) selector(x *operand, e *ast.SelectorExpr) { 266 // these must be declared before the "goto Error" statements 267 var ( 268 obj Object 269 index []int 270 indirect bool 271 ) 272 273 sel := e.Sel.Name 274 // If the identifier refers to a package, handle everything here 275 // so we don't need a "package" mode for operands: package names 276 // can only appear in qualified identifiers which are mapped to 277 // selector expressions. 278 if ident, ok := e.X.(*ast.Ident); ok { 279 _, obj := check.scope.LookupParent(ident.Name, check.pos) 280 if pkg, _ := obj.(*PkgName); pkg != nil { 281 assert(pkg.pkg == check.pkg) 282 check.recordUse(ident, pkg) 283 pkg.used = true 284 exp := pkg.imported.scope.Lookup(sel) 285 if exp == nil { 286 if !pkg.imported.fake { 287 check.errorf(e.Pos(), "%s not declared by package %s", sel, ident) 288 } 289 goto Error 290 } 291 if !exp.Exported() { 292 check.errorf(e.Pos(), "%s not exported by package %s", sel, ident) 293 // ok to continue 294 } 295 check.recordUse(e.Sel, exp) 296 // Simplified version of the code for *ast.Idents: 297 // - imported objects are always fully initialized 298 switch exp := exp.(type) { 299 case *Const: 300 assert(exp.Val() != nil) 301 x.mode = constant_ 302 x.typ = exp.typ 303 x.val = exp.val 304 case *TypeName: 305 x.mode = typexpr 306 x.typ = exp.typ 307 case *Var: 308 x.mode = variable 309 x.typ = exp.typ 310 case *Func: 311 x.mode = value 312 x.typ = exp.typ 313 case *Builtin: 314 x.mode = builtin 315 x.typ = exp.typ 316 x.id = exp.id 317 default: 318 unreachable() 319 } 320 x.expr = e 321 return 322 } 323 } 324 325 check.exprOrType(x, e.X) 326 if x.mode == invalid { 327 goto Error 328 } 329 330 obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel) 331 if obj == nil { 332 switch { 333 case index != nil: 334 // TODO(gri) should provide actual type where the conflict happens 335 check.invalidOp(e.Pos(), "ambiguous selector %s", sel) 336 case indirect: 337 check.invalidOp(e.Pos(), "%s is not in method set of %s", sel, x.typ) 338 default: 339 check.invalidOp(e.Pos(), "%s has no field or method %s", x, sel) 340 } 341 goto Error 342 } 343 344 if x.mode == typexpr { 345 // method expression 346 m, _ := obj.(*Func) 347 if m == nil { 348 check.invalidOp(e.Pos(), "%s has no method %s", x, sel) 349 goto Error 350 } 351 352 check.recordSelection(e, MethodExpr, x.typ, m, index, indirect) 353 354 // the receiver type becomes the type of the first function 355 // argument of the method expression's function type 356 var params []*Var 357 sig := m.typ.(*Signature) 358 if sig.params != nil { 359 params = sig.params.vars 360 } 361 x.mode = value 362 x.typ = &Signature{ 363 params: NewTuple(append([]*Var{NewVar(token.NoPos, check.pkg, "", x.typ)}, params...)...), 364 results: sig.results, 365 variadic: sig.variadic, 366 } 367 368 check.addDeclDep(m) 369 370 } else { 371 // regular selector 372 switch obj := obj.(type) { 373 case *Var: 374 check.recordSelection(e, FieldVal, x.typ, obj, index, indirect) 375 if x.mode == variable || indirect { 376 x.mode = variable 377 } else { 378 x.mode = value 379 } 380 x.typ = obj.typ 381 382 case *Func: 383 // TODO(gri) If we needed to take into account the receiver's 384 // addressability, should we report the type &(x.typ) instead? 385 check.recordSelection(e, MethodVal, x.typ, obj, index, indirect) 386 387 if debug { 388 // Verify that LookupFieldOrMethod and MethodSet.Lookup agree. 389 typ := x.typ 390 if x.mode == variable { 391 // If typ is not an (unnamed) pointer or an interface, 392 // use *typ instead, because the method set of *typ 393 // includes the methods of typ. 394 // Variables are addressable, so we can always take their 395 // address. 396 if _, ok := typ.(*Pointer); !ok && !IsInterface(typ) { 397 typ = &Pointer{base: typ} 398 } 399 } 400 // If we created a synthetic pointer type above, we will throw 401 // away the method set computed here after use. 402 // TODO(gri) Method set computation should probably always compute 403 // both, the value and the pointer receiver method set and represent 404 // them in a single structure. 405 // TODO(gri) Consider also using a method set cache for the lifetime 406 // of checker once we rely on MethodSet lookup instead of individual 407 // lookup. 408 mset := NewMethodSet(typ) 409 if m := mset.Lookup(check.pkg, sel); m == nil || m.obj != obj { 410 check.dump("%s: (%s).%v -> %s", e.Pos(), typ, obj.name, m) 411 check.dump("%s\n", mset) 412 panic("method sets and lookup don't agree") 413 } 414 } 415 416 x.mode = value 417 418 // remove receiver 419 sig := *obj.typ.(*Signature) 420 sig.recv = nil 421 x.typ = &sig 422 423 check.addDeclDep(obj) 424 425 default: 426 unreachable() 427 } 428 } 429 430 // everything went well 431 x.expr = e 432 return 433 434 Error: 435 x.mode = invalid 436 x.expr = e 437 }