| 1 | // Copyright 2014 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 | // Package satisfy inspects the type-checked ASTs of Go packages and |
| 6 | // reports the set of discovered type constraints of the form (lhs, rhs |
| 7 | // Type) where lhs is a non-trivial interface, rhs satisfies this |
| 8 | // interface, and this fact is necessary for the package to be |
| 9 | // well-typed. |
| 10 | // |
| 11 | // It requires well-typed inputs. |
| 12 | package satisfy // import "golang.org/x/tools/refactor/satisfy" |
| 13 | |
| 14 | // NOTES: |
| 15 | // |
| 16 | // We don't care about numeric conversions, so we don't descend into |
| 17 | // types or constant expressions. This is unsound because |
| 18 | // constant expressions can contain arbitrary statements, e.g. |
| 19 | // const x = len([1]func(){func() { |
| 20 | // ... |
| 21 | // }}) |
| 22 | // |
| 23 | // Assignability conversions are possible in the following places: |
| 24 | // - in assignments y = x, y := x, var y = x. |
| 25 | // - from call argument types to formal parameter types |
| 26 | // - in append and delete calls |
| 27 | // - from return operands to result parameter types |
| 28 | // - in composite literal T{k:v}, from k and v to T's field/element/key type |
| 29 | // - in map[key] from key to the map's key type |
| 30 | // - in comparisons x==y and switch x { case y: }. |
| 31 | // - in explicit conversions T(x) |
| 32 | // - in sends ch <- x, from x to the channel element type |
| 33 | // - in type assertions x.(T) and switch x.(type) { case T: } |
| 34 | // |
| 35 | // The results of this pass provide information equivalent to the |
| 36 | // ssa.MakeInterface and ssa.ChangeInterface instructions. |
| 37 | |
| 38 | import ( |
| 39 | "fmt" |
| 40 | "go/ast" |
| 41 | "go/token" |
| 42 | "go/types" |
| 43 | |
| 44 | "golang.org/x/tools/go/types/typeutil" |
| 45 | "golang.org/x/tools/internal/typeparams" |
| 46 | ) |
| 47 | |
| 48 | // A Constraint records the fact that the RHS type does and must |
| 49 | // satisfy the LHS type, which is an interface. |
| 50 | // The names are suggestive of an assignment statement LHS = RHS. |
| 51 | // |
| 52 | // The constraint is implicitly universally quantified over any type |
| 53 | // parameters appearing within the two types. |
| 54 | type Constraint struct { |
| 55 | LHS, RHS types.Type |
| 56 | } |
| 57 | |
| 58 | // A Finder inspects the type-checked ASTs of Go packages and |
| 59 | // accumulates the set of type constraints (x, y) such that x is |
| 60 | // assignable to y, y is an interface, and both x and y have methods. |
| 61 | // |
| 62 | // In other words, it returns the subset of the "implements" relation |
| 63 | // that is checked during compilation of a package. Refactoring tools |
| 64 | // will need to preserve at least this part of the relation to ensure |
| 65 | // continued compilation. |
| 66 | type Finder struct { |
| 67 | Result map[Constraint]bool |
| 68 | msetcache typeutil.MethodSetCache |
| 69 | |
| 70 | // per-Find state |
| 71 | info *types.Info |
| 72 | sig *types.Signature |
| 73 | } |
| 74 | |
| 75 | // Find inspects a single package, populating Result with its pairs of |
| 76 | // constrained types. |
| 77 | // |
| 78 | // The result is non-canonical and thus may contain duplicates (but this |
| 79 | // tends to preserves names of interface types better). |
| 80 | // |
| 81 | // The package must be free of type errors, and |
| 82 | // info.{Defs,Uses,Selections,Types} must have been populated by the |
| 83 | // type-checker. |
| 84 | func (f *Finder) Find(info *types.Info, files []*ast.File) { |
| 85 | if info.Defs == nil || info.Uses == nil || info.Selections == nil || info.Types == nil { |
| 86 | panic("Finder.Find: one of info.{Defs,Uses,Selections.Types} is not populated") |
| 87 | } |
| 88 | if f.Result == nil { |
| 89 | f.Result = make(map[Constraint]bool) |
| 90 | } |
| 91 | |
| 92 | f.info = info |
| 93 | for _, file := range files { |
| 94 | for _, d := range file.Decls { |
| 95 | switch d := d.(type) { |
| 96 | case *ast.GenDecl: |
| 97 | if d.Tok == token.VAR { // ignore consts |
| 98 | for _, spec := range d.Specs { |
| 99 | f.valueSpec(spec.(*ast.ValueSpec)) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | case *ast.FuncDecl: |
| 104 | if d.Body != nil { |
| 105 | f.sig = f.info.Defs[d.Name].Type().(*types.Signature) |
| 106 | f.stmt(d.Body) |
| 107 | f.sig = nil |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | f.info = nil |
| 113 | } |
| 114 | |
| 115 | var ( |
| 116 | tInvalid = types.Typ[types.Invalid] |
| 117 | tUntypedBool = types.Typ[types.UntypedBool] |
| 118 | tUntypedNil = types.Typ[types.UntypedNil] |
| 119 | ) |
| 120 | |
| 121 | // exprN visits an expression in a multi-value context. |
| 122 | func (f *Finder) exprN(e ast.Expr) types.Type { |
| 123 | typ := f.info.Types[e].Type.(*types.Tuple) |
| 124 | switch e := e.(type) { |
| 125 | case *ast.ParenExpr: |
| 126 | return f.exprN(e.X) |
| 127 | |
| 128 | case *ast.CallExpr: |
| 129 | // x, err := f(args) |
| 130 | sig := typeparams.CoreType(f.expr(e.Fun)).(*types.Signature) |
| 131 | f.call(sig, e.Args) |
| 132 | |
| 133 | case *ast.IndexExpr: |
| 134 | // y, ok := x[i] |
| 135 | x := f.expr(e.X) |
| 136 | f.assign(f.expr(e.Index), typeparams.CoreType(x).(*types.Map).Key()) |
| 137 | |
| 138 | case *ast.TypeAssertExpr: |
| 139 | // y, ok := x.(T) |
| 140 | f.typeAssert(f.expr(e.X), typ.At(0).Type()) |
| 141 | |
| 142 | case *ast.UnaryExpr: // must be receive <- |
| 143 | // y, ok := <-x |
| 144 | f.expr(e.X) |
| 145 | |
| 146 | default: |
| 147 | panic(e) |
| 148 | } |
| 149 | return typ |
| 150 | } |
| 151 | |
| 152 | func (f *Finder) call(sig *types.Signature, args []ast.Expr) { |
| 153 | if len(args) == 0 { |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | // Ellipsis call? e.g. f(x, y, z...) |
| 158 | if _, ok := args[len(args)-1].(*ast.Ellipsis); ok { |
| 159 | for i, arg := range args { |
| 160 | // The final arg is a slice, and so is the final param. |
| 161 | f.assign(sig.Params().At(i).Type(), f.expr(arg)) |
| 162 | } |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | var argtypes []types.Type |
| 167 | |
| 168 | // Gather the effective actual parameter types. |
| 169 | if tuple, ok := f.info.Types[args[0]].Type.(*types.Tuple); ok { |
| 170 | // f(g()) call where g has multiple results? |
| 171 | f.expr(args[0]) |
| 172 | // unpack the tuple |
| 173 | for v := range tuple.Variables() { |
| 174 | argtypes = append(argtypes, v.Type()) |
| 175 | } |
| 176 | } else { |
| 177 | for _, arg := range args { |
| 178 | argtypes = append(argtypes, f.expr(arg)) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Assign the actuals to the formals. |
| 183 | if !sig.Variadic() { |
| 184 | for i, argtype := range argtypes { |
| 185 | f.assign(sig.Params().At(i).Type(), argtype) |
| 186 | } |
| 187 | } else { |
| 188 | // The first n-1 parameters are assigned normally. |
| 189 | nnormals := sig.Params().Len() - 1 |
| 190 | for i, argtype := range argtypes[:nnormals] { |
| 191 | f.assign(sig.Params().At(i).Type(), argtype) |
| 192 | } |
| 193 | // Remaining args are assigned to elements of varargs slice. |
| 194 | tElem := sig.Params().At(nnormals).Type().(*types.Slice).Elem() |
| 195 | for i := nnormals; i < len(argtypes); i++ { |
| 196 | f.assign(tElem, argtypes[i]) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // builtin visits the arguments of a builtin type with signature sig. |
| 202 | func (f *Finder) builtin(obj *types.Builtin, sig *types.Signature, args []ast.Expr) { |
| 203 | switch obj.Name() { |
| 204 | case "make", "new": |
| 205 | for i, arg := range args { |
| 206 | if i == 0 && f.info.Types[arg].IsType() { |
| 207 | continue // skip the type operand |
| 208 | } |
| 209 | f.expr(arg) |
| 210 | } |
| 211 | |
| 212 | case "append": |
| 213 | s := f.expr(args[0]) |
| 214 | if _, ok := args[len(args)-1].(*ast.Ellipsis); ok && len(args) == 2 { |
| 215 | // append(x, y...) including append([]byte, "foo"...) |
| 216 | f.expr(args[1]) |
| 217 | } else { |
| 218 | // append(x, y, z) |
| 219 | tElem := typeparams.CoreType(s).(*types.Slice).Elem() |
| 220 | for _, arg := range args[1:] { |
| 221 | f.assign(tElem, f.expr(arg)) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | case "delete": |
| 226 | m := f.expr(args[0]) |
| 227 | k := f.expr(args[1]) |
| 228 | f.assign(typeparams.CoreType(m).(*types.Map).Key(), k) |
| 229 | |
| 230 | default: |
| 231 | // ordinary call |
| 232 | f.call(sig, args) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func (f *Finder) extract(tuple types.Type, i int) types.Type { |
| 237 | if tuple, ok := tuple.(*types.Tuple); ok && i < tuple.Len() { |
| 238 | return tuple.At(i).Type() |
| 239 | } |
| 240 | return tInvalid |
| 241 | } |
| 242 | |
| 243 | func (f *Finder) valueSpec(spec *ast.ValueSpec) { |
| 244 | var T types.Type |
| 245 | if spec.Type != nil { |
| 246 | T = f.info.Types[spec.Type].Type |
| 247 | } |
| 248 | switch len(spec.Values) { |
| 249 | case len(spec.Names): // e.g. var x, y = f(), g() |
| 250 | for _, value := range spec.Values { |
| 251 | v := f.expr(value) |
| 252 | if T != nil { |
| 253 | f.assign(T, v) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | case 1: // e.g. var x, y = f() |
| 258 | tuple := f.exprN(spec.Values[0]) |
| 259 | for i := range spec.Names { |
| 260 | if T != nil { |
| 261 | f.assign(T, f.extract(tuple, i)) |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // assign records pairs of distinct types that are related by |
| 268 | // assignability, where the left-hand side is an interface and both |
| 269 | // sides have methods. |
| 270 | // |
| 271 | // It should be called for all assignability checks, type assertions, |
| 272 | // explicit conversions and comparisons between two types, unless the |
| 273 | // types are uninteresting (e.g. lhs is a concrete type, or the empty |
| 274 | // interface; rhs has no methods). |
| 275 | func (f *Finder) assign(lhs, rhs types.Type) { |
| 276 | if types.Identical(lhs, rhs) { |
| 277 | return |
| 278 | } |
| 279 | if !types.IsInterface(lhs) { |
| 280 | return |
| 281 | } |
| 282 | |
| 283 | if f.msetcache.MethodSet(lhs).Len() == 0 { |
| 284 | return |
| 285 | } |
| 286 | if f.msetcache.MethodSet(rhs).Len() == 0 { |
| 287 | return |
| 288 | } |
| 289 | // record the pair |
| 290 | f.Result[Constraint{lhs, rhs}] = true |
| 291 | } |
| 292 | |
| 293 | // typeAssert must be called for each type assertion x.(T) where x has |
| 294 | // interface type I. |
| 295 | func (f *Finder) typeAssert(I, T types.Type) { |
| 296 | // Type assertions are slightly subtle, because they are allowed |
| 297 | // to be "impossible", e.g. |
| 298 | // |
| 299 | // var x interface{f()} |
| 300 | // _ = x.(interface{f()int}) // legal |
| 301 | // |
| 302 | // (In hindsight, the language spec should probably not have |
| 303 | // allowed this, but it's too late to fix now.) |
| 304 | // |
| 305 | // This means that a type assert from I to T isn't exactly a |
| 306 | // constraint that T is assignable to I, but for a refactoring |
| 307 | // tool it is a conditional constraint that, if T is assignable |
| 308 | // to I before a refactoring, it should remain so after. |
| 309 | |
| 310 | if types.AssignableTo(T, I) { |
| 311 | f.assign(I, T) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // compare must be called for each comparison x==y. |
| 316 | func (f *Finder) compare(x, y types.Type) { |
| 317 | if types.AssignableTo(x, y) { |
| 318 | f.assign(y, x) |
| 319 | } else if types.AssignableTo(y, x) { |
| 320 | f.assign(x, y) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // expr visits a true expression (not a type or defining ident) |
| 325 | // and returns its type. |
| 326 | func (f *Finder) expr(e ast.Expr) types.Type { |
| 327 | tv := f.info.Types[e] |
| 328 | if tv.Value != nil { |
| 329 | return tv.Type // prune the descent for constants |
| 330 | } |
| 331 | |
| 332 | // tv.Type may be nil for an ast.Ident. |
| 333 | |
| 334 | switch e := e.(type) { |
| 335 | case *ast.BadExpr, *ast.BasicLit: |
| 336 | // no-op |
| 337 | |
| 338 | case *ast.Ident: |
| 339 | // (referring idents only) |
| 340 | if obj, ok := f.info.Uses[e]; ok { |
| 341 | return obj.Type() |
| 342 | } |
| 343 | if e.Name == "_" { // e.g. "for _ = range x" |
| 344 | return tInvalid |
| 345 | } |
| 346 | panic("undefined ident: " + e.Name) |
| 347 | |
| 348 | case *ast.Ellipsis: |
| 349 | if e.Elt != nil { |
| 350 | f.expr(e.Elt) |
| 351 | } |
| 352 | |
| 353 | case *ast.FuncLit: |
| 354 | saved := f.sig |
| 355 | f.sig = tv.Type.(*types.Signature) |
| 356 | f.stmt(e.Body) |
| 357 | f.sig = saved |
| 358 | |
| 359 | case *ast.CompositeLit: |
| 360 | switch T := typeparams.CoreType(typeparams.Deref(tv.Type)).(type) { |
| 361 | case *types.Struct: |
| 362 | for i, elem := range e.Elts { |
| 363 | if kv, ok := elem.(*ast.KeyValueExpr); ok { |
| 364 | f.assign(f.info.Uses[kv.Key.(*ast.Ident)].Type(), f.expr(kv.Value)) |
| 365 | } else { |
| 366 | f.assign(T.Field(i).Type(), f.expr(elem)) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | case *types.Map: |
| 371 | for _, elem := range e.Elts { |
| 372 | elem := elem.(*ast.KeyValueExpr) |
| 373 | f.assign(T.Key(), f.expr(elem.Key)) |
| 374 | f.assign(T.Elem(), f.expr(elem.Value)) |
| 375 | } |
| 376 | |
| 377 | case *types.Array, *types.Slice: |
| 378 | tElem := T.(interface { |
| 379 | Elem() types.Type |
| 380 | }).Elem() |
| 381 | for _, elem := range e.Elts { |
| 382 | if kv, ok := elem.(*ast.KeyValueExpr); ok { |
| 383 | // ignore the key |
| 384 | f.assign(tElem, f.expr(kv.Value)) |
| 385 | } else { |
| 386 | f.assign(tElem, f.expr(elem)) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | default: |
| 391 | panic(fmt.Sprintf("unexpected composite literal type %T: %v", tv.Type, tv.Type.String())) |
| 392 | } |
| 393 | |
| 394 | case *ast.ParenExpr: |
| 395 | f.expr(e.X) |
| 396 | |
| 397 | case *ast.SelectorExpr: |
| 398 | if seln, ok := f.info.Selections[e]; ok { |
| 399 | // If e.X is a type (e.g., e is interface{ m() }.m), don't visit it. |
| 400 | if seln.Kind() != types.MethodExpr { |
| 401 | f.expr(e.X) |
| 402 | } |
| 403 | } else { |
| 404 | return f.info.Uses[e.Sel].Type() // qualified identifier |
| 405 | } |
| 406 | |
| 407 | case *ast.IndexExpr: |
| 408 | if instance(f.info, e.X) { |
| 409 | // f[T] or C[T] -- generic instantiation |
| 410 | } else { |
| 411 | // x[i] or m[k] -- index or lookup operation |
| 412 | x := f.expr(e.X) |
| 413 | i := f.expr(e.Index) |
| 414 | if ux, ok := typeparams.CoreType(x).(*types.Map); ok { |
| 415 | f.assign(ux.Key(), i) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | case *ast.IndexListExpr: |
| 420 | // f[X, Y] -- generic instantiation |
| 421 | |
| 422 | case *ast.SliceExpr: |
| 423 | f.expr(e.X) |
| 424 | if e.Low != nil { |
| 425 | f.expr(e.Low) |
| 426 | } |
| 427 | if e.High != nil { |
| 428 | f.expr(e.High) |
| 429 | } |
| 430 | if e.Max != nil { |
| 431 | f.expr(e.Max) |
| 432 | } |
| 433 | |
| 434 | case *ast.TypeAssertExpr: |
| 435 | x := f.expr(e.X) |
| 436 | f.typeAssert(x, f.info.Types[e.Type].Type) |
| 437 | |
| 438 | case *ast.CallExpr: |
| 439 | if tvFun := f.info.Types[e.Fun]; tvFun.IsType() { |
| 440 | // conversion |
| 441 | arg0 := f.expr(e.Args[0]) |
| 442 | f.assign(tvFun.Type, arg0) |
| 443 | } else { |
| 444 | // function call |
| 445 | |
| 446 | // unsafe call. Treat calls to functions in unsafe like ordinary calls, |
| 447 | // except that their signature cannot be determined by their func obj. |
| 448 | // Without this special handling, f.expr(e.Fun) would fail below. |
| 449 | if s, ok := ast.Unparen(e.Fun).(*ast.SelectorExpr); ok { |
| 450 | if obj, ok := f.info.Uses[s.Sel].(*types.Builtin); ok && obj.Pkg().Path() == "unsafe" { |
| 451 | sig := f.info.Types[e.Fun].Type.(*types.Signature) |
| 452 | f.call(sig, e.Args) |
| 453 | return tv.Type |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // builtin call |
| 458 | if id, ok := ast.Unparen(e.Fun).(*ast.Ident); ok { |
| 459 | if obj, ok := f.info.Uses[id].(*types.Builtin); ok { |
| 460 | sig := f.info.Types[id].Type.(*types.Signature) |
| 461 | f.builtin(obj, sig, e.Args) |
| 462 | return tv.Type |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // ordinary call |
| 467 | f.call(typeparams.CoreType(f.expr(e.Fun)).(*types.Signature), e.Args) |
| 468 | } |
| 469 | |
| 470 | case *ast.StarExpr: |
| 471 | f.expr(e.X) |
| 472 | |
| 473 | case *ast.UnaryExpr: |
| 474 | f.expr(e.X) |
| 475 | |
| 476 | case *ast.BinaryExpr: |
| 477 | x := f.expr(e.X) |
| 478 | y := f.expr(e.Y) |
| 479 | if e.Op == token.EQL || e.Op == token.NEQ { |
| 480 | f.compare(x, y) |
| 481 | } |
| 482 | |
| 483 | case *ast.KeyValueExpr: |
| 484 | f.expr(e.Key) |
| 485 | f.expr(e.Value) |
| 486 | |
| 487 | case *ast.ArrayType, |
| 488 | *ast.StructType, |
| 489 | *ast.FuncType, |
| 490 | *ast.InterfaceType, |
| 491 | *ast.MapType, |
| 492 | *ast.ChanType: |
| 493 | panic(e) |
| 494 | } |
| 495 | |
| 496 | if tv.Type == nil { |
| 497 | panic(fmt.Sprintf("no type for %T", e)) |
| 498 | } |
| 499 | |
| 500 | return tv.Type |
| 501 | } |
| 502 | |
| 503 | func (f *Finder) stmt(s ast.Stmt) { |
| 504 | switch s := s.(type) { |
| 505 | case *ast.BadStmt, |
| 506 | *ast.EmptyStmt, |
| 507 | *ast.BranchStmt: |
| 508 | // no-op |
| 509 | |
| 510 | case *ast.DeclStmt: |
| 511 | d := s.Decl.(*ast.GenDecl) |
| 512 | if d.Tok == token.VAR { // ignore consts |
| 513 | for _, spec := range d.Specs { |
| 514 | f.valueSpec(spec.(*ast.ValueSpec)) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | case *ast.LabeledStmt: |
| 519 | f.stmt(s.Stmt) |
| 520 | |
| 521 | case *ast.ExprStmt: |
| 522 | f.expr(s.X) |
| 523 | |
| 524 | case *ast.SendStmt: |
| 525 | ch := f.expr(s.Chan) |
| 526 | val := f.expr(s.Value) |
| 527 | f.assign(typeparams.CoreType(ch).(*types.Chan).Elem(), val) |
| 528 | |
| 529 | case *ast.IncDecStmt: |
| 530 | f.expr(s.X) |
| 531 | |
| 532 | case *ast.AssignStmt: |
| 533 | switch s.Tok { |
| 534 | case token.ASSIGN, token.DEFINE: |
| 535 | // y := x or y = x |
| 536 | var rhsTuple types.Type |
| 537 | if len(s.Lhs) != len(s.Rhs) { |
| 538 | rhsTuple = f.exprN(s.Rhs[0]) |
| 539 | } |
| 540 | for i := range s.Lhs { |
| 541 | var lhs, rhs types.Type |
| 542 | if rhsTuple == nil { |
| 543 | rhs = f.expr(s.Rhs[i]) // 1:1 assignment |
| 544 | } else { |
| 545 | rhs = f.extract(rhsTuple, i) // n:1 assignment |
| 546 | } |
| 547 | |
| 548 | if id, ok := s.Lhs[i].(*ast.Ident); ok { |
| 549 | if id.Name != "_" { |
| 550 | if obj, ok := f.info.Defs[id]; ok { |
| 551 | lhs = obj.Type() // definition |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | if lhs == nil { |
| 556 | lhs = f.expr(s.Lhs[i]) // assignment |
| 557 | } |
| 558 | f.assign(lhs, rhs) |
| 559 | } |
| 560 | |
| 561 | default: |
| 562 | // y op= x |
| 563 | f.expr(s.Lhs[0]) |
| 564 | f.expr(s.Rhs[0]) |
| 565 | } |
| 566 | |
| 567 | case *ast.GoStmt: |
| 568 | f.expr(s.Call) |
| 569 | |
| 570 | case *ast.DeferStmt: |
| 571 | f.expr(s.Call) |
| 572 | |
| 573 | case *ast.ReturnStmt: |
| 574 | formals := f.sig.Results() |
| 575 | switch len(s.Results) { |
| 576 | case formals.Len(): // 1:1 |
| 577 | for i, result := range s.Results { |
| 578 | f.assign(formals.At(i).Type(), f.expr(result)) |
| 579 | } |
| 580 | |
| 581 | case 1: // n:1 |
| 582 | tuple := f.exprN(s.Results[0]) |
| 583 | for i := 0; i < formals.Len(); i++ { |
| 584 | f.assign(formals.At(i).Type(), f.extract(tuple, i)) |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | case *ast.SelectStmt: |
| 589 | f.stmt(s.Body) |
| 590 | |
| 591 | case *ast.BlockStmt: |
| 592 | for _, s := range s.List { |
| 593 | f.stmt(s) |
| 594 | } |
| 595 | |
| 596 | case *ast.IfStmt: |
| 597 | if s.Init != nil { |
| 598 | f.stmt(s.Init) |
| 599 | } |
| 600 | f.expr(s.Cond) |
| 601 | f.stmt(s.Body) |
| 602 | if s.Else != nil { |
| 603 | f.stmt(s.Else) |
| 604 | } |
| 605 | |
| 606 | case *ast.SwitchStmt: |
| 607 | if s.Init != nil { |
| 608 | f.stmt(s.Init) |
| 609 | } |
| 610 | var tag types.Type = tUntypedBool |
| 611 | if s.Tag != nil { |
| 612 | tag = f.expr(s.Tag) |
| 613 | } |
| 614 | for _, cc := range s.Body.List { |
| 615 | cc := cc.(*ast.CaseClause) |
| 616 | for _, cond := range cc.List { |
| 617 | f.compare(tag, f.info.Types[cond].Type) |
| 618 | } |
| 619 | for _, s := range cc.Body { |
| 620 | f.stmt(s) |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | case *ast.TypeSwitchStmt: |
| 625 | if s.Init != nil { |
| 626 | f.stmt(s.Init) |
| 627 | } |
| 628 | var I types.Type |
| 629 | switch ass := s.Assign.(type) { |
| 630 | case *ast.ExprStmt: // x.(type) |
| 631 | I = f.expr(ast.Unparen(ass.X).(*ast.TypeAssertExpr).X) |
| 632 | case *ast.AssignStmt: // y := x.(type) |
| 633 | I = f.expr(ast.Unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) |
| 634 | } |
| 635 | for _, cc := range s.Body.List { |
| 636 | cc := cc.(*ast.CaseClause) |
| 637 | for _, cond := range cc.List { |
| 638 | tCase := f.info.Types[cond].Type |
| 639 | if tCase != tUntypedNil { |
| 640 | f.typeAssert(I, tCase) |
| 641 | } |
| 642 | } |
| 643 | for _, s := range cc.Body { |
| 644 | f.stmt(s) |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | case *ast.CommClause: |
| 649 | if s.Comm != nil { |
| 650 | f.stmt(s.Comm) |
| 651 | } |
| 652 | for _, s := range s.Body { |
| 653 | f.stmt(s) |
| 654 | } |
| 655 | |
| 656 | case *ast.ForStmt: |
| 657 | if s.Init != nil { |
| 658 | f.stmt(s.Init) |
| 659 | } |
| 660 | if s.Cond != nil { |
| 661 | f.expr(s.Cond) |
| 662 | } |
| 663 | if s.Post != nil { |
| 664 | f.stmt(s.Post) |
| 665 | } |
| 666 | f.stmt(s.Body) |
| 667 | |
| 668 | case *ast.RangeStmt: |
| 669 | x := f.expr(s.X) |
| 670 | // No conversions are involved when Tok==DEFINE. |
| 671 | if s.Tok == token.ASSIGN { |
| 672 | if s.Key != nil { |
| 673 | k := f.expr(s.Key) |
| 674 | var xelem types.Type |
| 675 | // Keys of array, *array, slice, string aren't interesting |
| 676 | // since the RHS key type is just an int. |
| 677 | switch ux := typeparams.CoreType(x).(type) { |
| 678 | case *types.Chan: |
| 679 | xelem = ux.Elem() |
| 680 | case *types.Map: |
| 681 | xelem = ux.Key() |
| 682 | } |
| 683 | if xelem != nil { |
| 684 | f.assign(k, xelem) |
| 685 | } |
| 686 | } |
| 687 | if s.Value != nil { |
| 688 | val := f.expr(s.Value) |
| 689 | var xelem types.Type |
| 690 | // Values of type strings aren't interesting because |
| 691 | // the RHS value type is just a rune. |
| 692 | switch ux := typeparams.CoreType(x).(type) { |
| 693 | case *types.Array: |
| 694 | xelem = ux.Elem() |
| 695 | case *types.Map: |
| 696 | xelem = ux.Elem() |
| 697 | case *types.Pointer: // *array |
| 698 | xelem = typeparams.CoreType(typeparams.Deref(ux)).(*types.Array).Elem() |
| 699 | case *types.Slice: |
| 700 | xelem = ux.Elem() |
| 701 | } |
| 702 | if xelem != nil { |
| 703 | f.assign(val, xelem) |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | f.stmt(s.Body) |
| 708 | |
| 709 | default: |
| 710 | panic(s) |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // -- Plundered from golang.org/x/tools/go/ssa ----------------- |
| 715 | |
| 716 | func instance(info *types.Info, expr ast.Expr) bool { |
| 717 | var id *ast.Ident |
| 718 | switch x := expr.(type) { |
| 719 | case *ast.Ident: |
| 720 | id = x |
| 721 | case *ast.SelectorExpr: |
| 722 | id = x.Sel |
| 723 | default: |
| 724 | return false |
| 725 | } |
| 726 | _, ok := info.Instances[id] |
| 727 | return ok |
| 728 | } |