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.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"go/types"
|
|
|
|
|
|
|
|
"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-12-05 22:34:17 -07:00
|
|
|
impls, 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 locations []protocol.Location
|
2019-12-05 22:34:17 -07:00
|
|
|
for _, impl := range impls {
|
|
|
|
if impl.pkg == nil || len(impl.pkg.CompiledGoFiles()) == 0 {
|
2019-11-11 14:51:47 -07:00
|
|
|
continue
|
|
|
|
}
|
2019-12-05 22:34:17 -07:00
|
|
|
|
|
|
|
file, _, err := i.Snapshot.View().FindPosInPackage(impl.pkg, impl.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-12-05 22:34:17 -07:00
|
|
|
|
|
|
|
ident, err := findIdentifier(i.Snapshot, impl.pkg, file, impl.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-12-05 22:34:17 -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-12-05 22:34:17 -07:00
|
|
|
|
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-05 22:34:17 -07:00
|
|
|
var ErrNotAnInterface = errors.New("not an interface or interface method")
|
2019-12-07 23:07:30 -07:00
|
|
|
|
2019-12-05 22:34:17 -07:00
|
|
|
func (i *IdentifierInfo) implementations(ctx context.Context) ([]implementation, error) {
|
|
|
|
var (
|
|
|
|
T *types.Interface
|
|
|
|
method *types.Func
|
|
|
|
)
|
|
|
|
|
|
|
|
switch obj := i.Declaration.obj.(type) {
|
|
|
|
case *types.Func:
|
2019-11-05 15:03:09 -07:00
|
|
|
method = obj
|
2019-12-05 22:34:17 -07:00
|
|
|
if recv := obj.Type().(*types.Signature).Recv(); recv != nil {
|
|
|
|
T, _ = recv.Type().Underlying().(*types.Interface)
|
|
|
|
}
|
|
|
|
case *types.TypeName:
|
|
|
|
T, _ = obj.Type().Underlying().(*types.Interface)
|
|
|
|
}
|
|
|
|
|
|
|
|
if T == nil {
|
|
|
|
return nil, ErrNotAnInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
if T.NumMethods() == 0 {
|
|
|
|
return nil, nil
|
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
|
2019-12-05 22:34:17 -07:00
|
|
|
// due to promotion).
|
|
|
|
var (
|
|
|
|
allNamed []*types.Named
|
|
|
|
pkgs = make(map[*types.Package]Package)
|
|
|
|
)
|
2019-11-11 14:51:47 -07:00
|
|
|
for _, pkg := range i.Snapshot.KnownPackages(ctx) {
|
2019-12-05 22:34:17 -07:00
|
|
|
pkgs[pkg.GetTypes()] = pkg
|
|
|
|
|
2019-11-11 14:51:47 -07:00
|
|
|
info := pkg.GetTypesInfo()
|
|
|
|
for _, obj := range info.Defs {
|
2019-12-05 22:34:17 -07:00
|
|
|
// We ignore aliases 'type M = N' to avoid duplicate reporting
|
|
|
|
// of the Named type N.
|
2019-11-11 14:51:47 -07:00
|
|
|
if obj, ok := obj.(*types.TypeName); ok && !obj.IsAlias() {
|
2019-12-05 22:34:17 -07:00
|
|
|
// We skip interface types since we only want concrete
|
|
|
|
// implementations.
|
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)
|
|
|
|
}
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 22:34:17 -07:00
|
|
|
var (
|
|
|
|
impls []implementation
|
|
|
|
seen = make(map[types.Object]bool)
|
|
|
|
)
|
2019-10-28 13:16:55 -06:00
|
|
|
|
2019-12-05 22:34:17 -07:00
|
|
|
// Find all the named types that implement our interface.
|
2019-10-28 13:16:55 -06:00
|
|
|
for _, U := range allNamed {
|
2019-12-05 22:34:17 -07:00
|
|
|
var concrete types.Type = U
|
|
|
|
if !types.AssignableTo(concrete, T) {
|
|
|
|
// We also accept T if *T implements our interface.
|
|
|
|
concrete = types.NewPointer(concrete)
|
|
|
|
if !types.AssignableTo(concrete, T) {
|
|
|
|
continue
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
2019-12-05 22:34:17 -07:00
|
|
|
}
|
2019-10-28 13:16:55 -06:00
|
|
|
|
2019-12-05 22:34:17 -07:00
|
|
|
var obj types.Object = U.Obj()
|
|
|
|
if method != nil {
|
|
|
|
obj = types.NewMethodSet(concrete).Lookup(method.Pkg(), method.Name()).Obj()
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
2019-12-05 22:34:17 -07:00
|
|
|
|
|
|
|
if obj == method || seen[obj] {
|
|
|
|
continue
|
2019-11-05 15:03:09 -07:00
|
|
|
}
|
2019-12-05 22:34:17 -07:00
|
|
|
|
|
|
|
seen[obj] = true
|
|
|
|
|
|
|
|
impls = append(impls, implementation{
|
|
|
|
obj: obj,
|
|
|
|
pkg: pkgs[obj.Pkg()],
|
|
|
|
})
|
2019-11-05 15:03:09 -07:00
|
|
|
}
|
2019-12-05 22:34:17 -07:00
|
|
|
|
|
|
|
return impls, nil
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|
|
|
|
|
2019-12-05 22:34:17 -07:00
|
|
|
type implementation struct {
|
|
|
|
// obj is the implementation, either a *types.TypeName or *types.Func.
|
|
|
|
obj types.Object
|
|
|
|
|
|
|
|
// pkg is the Package that contains obj's definition.
|
|
|
|
pkg Package
|
2019-10-28 13:16:55 -06:00
|
|
|
}
|