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. 4 5package typeutil 6 7import ( 8 "go/ast" 9 "go/types" 10) 11 12// StaticCallee returns the target (function or method) of a static 13// function call, if any. It returns nil for calls to builtin. 14func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { 15 var obj types.Object 16 switch fun := call.Fun.(type) { 17 case *ast.Ident: 18 obj = info.Uses[fun] // type, var, builtin, or declared func 19 case *ast.SelectorExpr: 20 if sel, ok := info.Selections[fun]; ok { 21 obj = sel.Obj() // method or field 22 } else { 23 obj = info.Uses[fun.Sel] // qualified identifier? 24 } 25 } 26 if f, ok := obj.(*types.Func); ok && !interfaceMethod(f) { 27 return f 28 } 29 return nil 30} 31 32func interfaceMethod(f *types.Func) bool { 33 recv := f.Type().(*types.Signature).Recv() 34 return recv != nil && types.IsInterface(recv.Type()) 35} 36