2019-10-28 13:16:55 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// The code in this file is based largely on the code in
|
|
|
|
// cmd/guru/implements.go. The guru implementation supports
|
|
|
|
// looking up "implementers" of methods also, but that
|
|
|
|
// code has been cut out here for now for simplicity.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-05 15:03:09 -07:00
|
|
|
"fmt"
|
2019-11-25 12:40:42 -07:00
|
|
|
"go/token"
|
2019-10-28 13:16:55 -06:00
|
|
|
"go/types"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/types/typeutil"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-11-26 13:03:20 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-12-07 23:07:30 -07:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-10-28 13:16:55 -06:00
|
|
|
)
|
|
|
|
|
2019-11-12 14:33:11 -07:00
|
|
|
func (i *IdentifierInfo) Implementation(ctx context.Context) ([]protocol.Location, error) {
|
2019-11-26 13:03:20 -07:00
|
|
|
ctx = telemetry.Package.With(ctx, i.pkg.ID())
|
|
|
|
|
2019-11-12 14:33:11 -07:00
|
|
|
res, err := i.implementations(ctx)
|
2019-10-28 13:16:55 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:03:09 -07:00
|
|
|
var objs []types.Object
|
2019-11-26 13:03:20 -07:00
|
|
|
pkgs := map[token.Pos]Package{}
|
2019-11-05 15:03:09 -07:00
|
|
|
|
|
|
|
if res.toMethod != nil {
|
|
|
|
// If we looked up a method, results are in toMethod.
|
|
|
|
for _, s := range res.toMethod {
|
2019-11-26 13:03:20 -07:00
|
|
|
if pkgs[s.Obj().Pos()] != nil {
|
2019-11-25 12:40:42 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-11-11 14:51:47 -07:00
|
|
|
// Determine package of receiver.
|
|
|
|
recv := s.Recv()
|
|
|
|
if p, ok := recv.(*types.Pointer); ok {
|
|
|
|
recv = p.Elem()
|
|
|
|
}
|
|
|
|
if n, ok := recv.(*types.Named); ok {
|
|
|
|
pkg := res.pkgs[n]
|
2019-11-26 13:03:20 -07:00
|
|
|
pkgs[s.Obj().Pos()] = pkg
|
2019-11-11 14:51:47 -07:00
|
|
|
}
|
|
|
|
// Add object to objs.
|
2019-11-05 15:03:09 -07:00
|
|
|
objs = append(objs, s.Obj())
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
2019-11-05 15:03:09 -07:00
|
|
|
} else {
|
|
|
|
// Otherwise, the results are in to.
|
|
|
|
for _, t := range res.to {
|
|
|
|
// We'll provide implementations that are named types and pointers to named types.
|
|
|
|
if p, ok := t.(*types.Pointer); ok {
|
|
|
|
t = p.Elem()
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
2019-11-05 15:03:09 -07:00
|
|
|
if n, ok := t.(*types.Named); ok {
|
2019-11-26 13:03:20 -07:00
|
|
|
if pkgs[n.Obj().Pos()] != nil {
|
2019-11-25 12:40:42 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-11-11 14:51:47 -07:00
|
|
|
pkg := res.pkgs[n]
|
2019-11-26 13:03:20 -07:00
|
|
|
pkgs[n.Obj().Pos()] = pkg
|
2019-11-25 12:40:42 -07:00
|
|
|
objs = append(objs, n.Obj())
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:03:09 -07:00
|
|
|
var locations []protocol.Location
|
|
|
|
for _, obj := range objs {
|
2019-11-26 13:03:20 -07:00
|
|
|
pkg := pkgs[obj.Pos()]
|
|
|
|
if pkgs[obj.Pos()] == nil || len(pkg.CompiledGoFiles()) == 0 {
|
2019-11-11 14:51:47 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-11-26 13:03:20 -07:00
|
|
|
file, _, err := i.Snapshot.View().FindPosInPackage(pkgs[obj.Pos()], obj.Pos())
|
2019-11-05 15:03:09 -07:00
|
|
|
if err != nil {
|
2019-11-26 13:03:20 -07:00
|
|
|
log.Error(ctx, "Error getting file for object", err)
|
|
|
|
continue
|
2019-11-05 15:03:09 -07:00
|
|
|
}
|
2019-11-22 12:49:12 -07:00
|
|
|
ident, err := findIdentifier(i.Snapshot, pkg, file, obj.Pos())
|
2019-11-11 14:51:47 -07:00
|
|
|
if err != nil {
|
2019-11-26 13:03:20 -07:00
|
|
|
log.Error(ctx, "Error getting ident for object", err)
|
|
|
|
continue
|
2019-11-11 14:51:47 -07:00
|
|
|
}
|
2019-11-05 15:03:09 -07:00
|
|
|
decRange, err := ident.Declaration.Range()
|
|
|
|
if err != nil {
|
2019-11-26 13:03:20 -07:00
|
|
|
log.Error(ctx, "Error getting range for object", err)
|
|
|
|
continue
|
2019-11-05 15:03:09 -07:00
|
|
|
}
|
2019-11-25 12:40:42 -07:00
|
|
|
// Do not add interface itself to the list.
|
|
|
|
if ident.Declaration.spanRange == i.Declaration.spanRange {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-05 15:03:09 -07:00
|
|
|
locations = append(locations, protocol.Location{
|
|
|
|
URI: protocol.NewURI(ident.Declaration.URI()),
|
|
|
|
Range: decRange,
|
|
|
|
})
|
|
|
|
}
|
2019-10-28 13:16:55 -06:00
|
|
|
return locations, nil
|
|
|
|
}
|
2019-11-25 12:40:42 -07:00
|
|
|
|
2019-12-07 23:07:30 -07:00
|
|
|
var ErrNotAMethod = errors.New("this function is not a method")
|
|
|
|
|
2019-10-28 13:16:55 -06:00
|
|
|
func (i *IdentifierInfo) implementations(ctx context.Context) (implementsResult, error) {
|
2019-11-05 15:03:09 -07:00
|
|
|
var T types.Type
|
|
|
|
var method *types.Func
|
2019-11-01 10:23:20 -06:00
|
|
|
if i.Type.Object == nil {
|
2019-11-05 15:03:09 -07:00
|
|
|
// This isn't a type. Is it a method?
|
|
|
|
obj, ok := i.Declaration.obj.(*types.Func)
|
|
|
|
if !ok {
|
|
|
|
return implementsResult{}, fmt.Errorf("no type info object for identifier %q", i.Name)
|
|
|
|
}
|
|
|
|
recv := obj.Type().(*types.Signature).Recv()
|
|
|
|
if recv == nil {
|
2019-12-07 23:07:30 -07:00
|
|
|
return implementsResult{}, ErrNotAMethod
|
2019-11-05 15:03:09 -07:00
|
|
|
}
|
|
|
|
method = obj
|
|
|
|
T = recv.Type()
|
|
|
|
} else {
|
|
|
|
T = i.Type.Object.Type()
|
2019-11-01 10:23:20 -06:00
|
|
|
}
|
2019-10-28 13:16:55 -06:00
|
|
|
|
2019-12-05 22:14:00 -07:00
|
|
|
// Find all named types, even local types (which can have methods
|
|
|
|
// due to promotion). We ignore aliases 'type M = N' to avoid
|
|
|
|
// duplicate reporting of the Named type N.
|
2019-10-28 13:16:55 -06:00
|
|
|
var allNamed []*types.Named
|
2019-11-11 14:51:47 -07:00
|
|
|
pkgs := map[*types.Named]Package{}
|
|
|
|
for _, pkg := range i.Snapshot.KnownPackages(ctx) {
|
|
|
|
info := pkg.GetTypesInfo()
|
|
|
|
for _, obj := range info.Defs {
|
|
|
|
if obj, ok := obj.(*types.TypeName); ok && !obj.IsAlias() {
|
2019-12-05 22:14:00 -07:00
|
|
|
if named, ok := obj.Type().(*types.Named); ok && !isInterface(named) {
|
2019-11-11 14:51:47 -07:00
|
|
|
allNamed = append(allNamed, named)
|
|
|
|
pkgs[named] = pkg
|
|
|
|
}
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msets typeutil.MethodSetCache
|
|
|
|
|
|
|
|
// Test each named type.
|
2019-12-05 22:14:00 -07:00
|
|
|
var to []types.Type
|
2019-10-28 13:16:55 -06:00
|
|
|
for _, U := range allNamed {
|
|
|
|
if isInterface(T) {
|
|
|
|
if msets.MethodSet(T).Len() == 0 {
|
|
|
|
continue // empty interface
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:14:00 -07:00
|
|
|
// T interface, U concrete
|
|
|
|
if types.AssignableTo(U, T) {
|
|
|
|
to = append(to, U)
|
|
|
|
} else if pU := types.NewPointer(U); types.AssignableTo(pU, T) {
|
|
|
|
to = append(to, pU)
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 15:03:09 -07:00
|
|
|
var toMethod []*types.Selection // contain nils
|
|
|
|
if method != nil {
|
|
|
|
for _, t := range to {
|
|
|
|
toMethod = append(toMethod,
|
|
|
|
types.NewMethodSet(t).Lookup(method.Pkg(), method.Name()))
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 22:14:00 -07:00
|
|
|
return implementsResult{pkgs, to, toMethod}, nil
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// implementsResult contains the results of an implements query.
|
|
|
|
type implementsResult struct {
|
2019-11-11 14:51:47 -07:00
|
|
|
pkgs map[*types.Named]Package
|
2019-11-05 15:03:09 -07:00
|
|
|
to []types.Type // named or ptr-to-named types assignable to interface T
|
|
|
|
toMethod []*types.Selection
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|