1 // Copyright 2018 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.
11 "golang.org/x/tools/go/ast/astutil"
14 // Callee returns the named target of a function call, if any:
15 // a function, method, builtin, or variable.
16 func Callee(info *types.Info, call *ast.CallExpr) types.Object {
18 switch fun := astutil.Unparen(call.Fun).(type) {
20 obj = info.Uses[fun] // type, var, builtin, or declared func
21 case *ast.SelectorExpr:
22 if sel, ok := info.Selections[fun]; ok {
23 obj = sel.Obj() // method or field
25 obj = info.Uses[fun.Sel] // qualified identifier?
28 if _, ok := obj.(*types.TypeName); ok {
29 return nil // T(x) is a conversion, not a call
34 // StaticCallee returns the target (function or method) of a static
35 // function call, if any. It returns nil for calls to builtins.
36 func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func {
37 if f, ok := Callee(info, call).(*types.Func); ok && !interfaceMethod(f) {
43 func interfaceMethod(f *types.Func) bool {
44 recv := f.Type().(*types.Signature).Recv()
45 return recv != nil && types.IsInterface(recv.Type())