2018-11-06 12:04:07 -07:00
|
|
|
// Copyright 2018 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"
|
2019-12-04 11:45:53 -07:00
|
|
|
"fmt"
|
2018-11-06 12:04:07 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2018-11-14 18:47:28 -07:00
|
|
|
"go/types"
|
2019-05-10 08:32:25 -06:00
|
|
|
"strconv"
|
2018-11-06 12:04:07 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2019-08-26 22:26:45 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2018-11-06 12:04:07 -07:00
|
|
|
)
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
// IdentifierInfo holds information about an identifier in Go source.
|
|
|
|
type IdentifierInfo struct {
|
2019-10-23 14:10:24 -06:00
|
|
|
Name string
|
|
|
|
Snapshot Snapshot
|
2019-09-06 21:58:07 -06:00
|
|
|
mappedRange
|
2019-08-26 22:26:45 -06:00
|
|
|
|
|
|
|
Type struct {
|
|
|
|
mappedRange
|
2019-01-23 12:16:43 -07:00
|
|
|
Object types.Object
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
|
|
|
|
Declaration Declaration
|
2018-12-03 15:14:30 -07:00
|
|
|
|
2019-12-16 16:13:47 -07:00
|
|
|
ident *ast.Ident
|
|
|
|
|
|
|
|
// enclosing is an expression used to determine the link anchor for an identifier.
|
|
|
|
enclosing types.Type
|
2019-12-06 00:44:16 -07:00
|
|
|
|
|
|
|
pkg Package
|
|
|
|
qf types.Qualifier
|
2019-06-04 23:16:23 -06:00
|
|
|
}
|
|
|
|
|
2019-08-26 22:26:45 -06:00
|
|
|
type Declaration struct {
|
|
|
|
mappedRange
|
2019-12-16 14:06:34 -07:00
|
|
|
node ast.Node
|
|
|
|
obj types.Object
|
2019-06-04 23:16:23 -06:00
|
|
|
}
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
// Identifier returns identifier information for a position
|
2018-11-06 12:04:07 -07:00
|
|
|
// in a file, accounting for a potentially incomplete selector.
|
2019-12-17 16:57:54 -07:00
|
|
|
func Identifier(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protocol.Position, selectPackage PackagePolicy) (*IdentifierInfo, error) {
|
2019-11-22 12:49:12 -07:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.Identifier")
|
|
|
|
defer done()
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
pkg, pgh, err := getParsedFile(ctx, snapshot, fh, selectPackage)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
2019-12-04 11:45:53 -07:00
|
|
|
return nil, fmt.Errorf("getting file for Identifier: %v", err)
|
2019-09-16 16:17:51 -06:00
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
file, m, _, err := pgh.Cached()
|
2019-09-17 09:19:11 -06:00
|
|
|
if err != nil {
|
2019-09-16 16:17:51 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
spn, err := m.PointSpan(pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
2019-08-06 16:51:17 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-15 17:11:40 -07:00
|
|
|
return findIdentifier(ctx, snapshot, pkg, file, rng.Start)
|
2019-08-07 14:03:45 -06:00
|
|
|
}
|
|
|
|
|
2019-12-07 23:07:30 -07:00
|
|
|
var ErrNoIdentFound = errors.New("no identifier found")
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
func findIdentifier(ctx context.Context, s Snapshot, pkg Package, file *ast.File, pos token.Pos) (*IdentifierInfo, error) {
|
2019-05-15 15:58:16 -06:00
|
|
|
// Handle import specs separately, as there is no formal position for a package declaration.
|
2019-11-22 12:49:12 -07:00
|
|
|
if result, err := importSpec(s, pkg, file, pos); result != nil || err != nil {
|
2019-05-10 08:32:25 -06:00
|
|
|
return result, err
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
path := pathEnclosingObjNode(file, pos)
|
2019-06-24 14:34:21 -06:00
|
|
|
if path == nil {
|
2019-12-17 22:06:31 -07:00
|
|
|
return nil, ErrNoIdentFound
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-12-16 14:06:34 -07:00
|
|
|
|
2019-11-22 12:49:12 -07:00
|
|
|
view := s.View()
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
ident, _ := path[0].(*ast.Ident)
|
|
|
|
if ident == nil {
|
|
|
|
return nil, ErrNoIdentFound
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:16:23 -06:00
|
|
|
result := &IdentifierInfo{
|
2019-12-16 16:13:47 -07:00
|
|
|
Snapshot: s,
|
|
|
|
qf: qualifier(file, pkg.GetTypes(), pkg.GetTypesInfo()),
|
|
|
|
pkg: pkg,
|
2019-12-17 22:06:31 -07:00
|
|
|
ident: ident,
|
2019-12-16 16:13:47 -07:00
|
|
|
enclosing: searchForEnclosing(pkg, path),
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
2019-12-06 00:44:16 -07:00
|
|
|
|
2019-11-19 12:51:46 -07:00
|
|
|
var wasEmbeddedField bool
|
2019-01-23 12:16:43 -07:00
|
|
|
for _, n := range path[1:] {
|
|
|
|
if field, ok := n.(*ast.Field); ok {
|
2019-11-19 12:51:46 -07:00
|
|
|
wasEmbeddedField = len(field.Names) == 0
|
2019-04-19 09:10:38 -06:00
|
|
|
break
|
2019-01-23 12:16:43 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
result.Name = result.ident.Name
|
2019-12-17 22:06:31 -07:00
|
|
|
var err error
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.mappedRange, err = posToMappedRange(view, pkg, result.ident.Pos(), result.ident.End()); err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Declaration.obj = pkg.GetTypesInfo().ObjectOf(result.ident)
|
|
|
|
if result.Declaration.obj == nil {
|
2019-06-14 12:55:24 -06:00
|
|
|
// If there was no types.Object for the declaration, there might be an implicit local variable
|
|
|
|
// declaration in a type switch.
|
2019-12-17 22:06:31 -07:00
|
|
|
if objs := typeSwitchImplicits(pkg, path); len(objs) > 0 {
|
2019-06-14 12:55:24 -06:00
|
|
|
// There is no types.Object for the declaration of an implicit local variable,
|
|
|
|
// but all of the types.Objects associated with the usages of this variable can be
|
|
|
|
// used to connect it back to the declaration.
|
|
|
|
// Preserve the first of these objects and treat it as if it were the declaring object.
|
2019-08-26 22:26:45 -06:00
|
|
|
result.Declaration.obj = objs[0]
|
2019-06-14 12:55:24 -06:00
|
|
|
} else {
|
|
|
|
// Probably a type error.
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no object for ident %v", result.Name)
|
2019-06-14 12:55:24 -06:00
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
|
|
|
// Handle builtins separately.
|
2019-08-26 22:26:45 -06:00
|
|
|
if result.Declaration.obj.Parent() == types.Universe {
|
2020-01-15 17:11:40 -07:00
|
|
|
astObj, err := view.LookupBuiltin(ctx, result.Name)
|
2020-01-11 18:29:13 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-16 15:17:59 -06:00
|
|
|
}
|
2020-01-11 18:29:13 -07:00
|
|
|
decl, ok := astObj.Decl.(ast.Node)
|
2019-05-15 15:58:16 -06:00
|
|
|
if !ok {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no declaration for %s", result.Name)
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
result.Declaration.node = decl
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.Declaration.mappedRange, err = nameToMappedRange(view, pkg, decl.Pos(), result.Name); err != nil {
|
2019-05-15 15:58:16 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:51:46 -07:00
|
|
|
if wasEmbeddedField {
|
2019-01-23 12:16:43 -07:00
|
|
|
// The original position was on the embedded field declaration, so we
|
|
|
|
// try to dig out the type and jump to that instead.
|
2019-08-26 22:26:45 -06:00
|
|
|
if v, ok := result.Declaration.obj.(*types.Var); ok {
|
2019-04-19 09:10:38 -06:00
|
|
|
if typObj := typeToObject(v.Type()); typObj != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
result.Declaration.obj = typObj
|
2018-11-14 18:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.Declaration.mappedRange, err = objToMappedRange(view, pkg, result.Declaration.obj); err != nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.Declaration.node, err = objToNode(s.View(), pkg, result.Declaration.obj); err != nil {
|
2019-04-17 16:21:47 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-06 14:33:47 -07:00
|
|
|
typ := pkg.GetTypesInfo().TypeOf(result.ident)
|
2019-01-23 12:16:43 -07:00
|
|
|
if typ == nil {
|
2019-06-14 12:55:24 -06:00
|
|
|
return result, nil
|
2019-01-23 12:16:43 -07:00
|
|
|
}
|
2019-06-14 12:55:24 -06:00
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
result.Type.Object = typeToObject(typ)
|
|
|
|
if result.Type.Object != nil {
|
2019-04-15 14:15:49 -06:00
|
|
|
// Identifiers with the type "error" are a special case with no position.
|
2019-05-15 15:58:16 -06:00
|
|
|
if hasErrorType(result.Type.Object) {
|
2019-04-15 14:15:49 -06:00
|
|
|
return result, nil
|
|
|
|
}
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.Type.mappedRange, err = objToMappedRange(view, pkg, result.Type.Object); err != nil {
|
2019-01-23 12:16:43 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 18:47:28 -07:00
|
|
|
return result, nil
|
2018-11-06 12:04:07 -07:00
|
|
|
}
|
2018-11-17 17:01:23 -07:00
|
|
|
|
2019-12-16 16:13:47 -07:00
|
|
|
func searchForEnclosing(pkg Package, path []ast.Node) types.Type {
|
2019-12-06 00:44:16 -07:00
|
|
|
for _, n := range path {
|
2019-12-16 16:13:47 -07:00
|
|
|
switch n := n.(type) {
|
2019-12-06 00:44:16 -07:00
|
|
|
case *ast.SelectorExpr:
|
2019-12-16 16:13:47 -07:00
|
|
|
if selection, ok := pkg.GetTypesInfo().Selections[n]; ok {
|
|
|
|
return deref(selection.Recv())
|
|
|
|
}
|
|
|
|
case *ast.CompositeLit:
|
|
|
|
if t, ok := pkg.GetTypesInfo().Types[n]; ok {
|
|
|
|
return t.Type
|
|
|
|
}
|
|
|
|
case *ast.TypeSpec:
|
|
|
|
if _, ok := n.Type.(*ast.StructType); ok {
|
|
|
|
if t, ok := pkg.GetTypesInfo().Defs[n.Name]; ok {
|
|
|
|
return t.Type()
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 00:44:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:16:43 -07:00
|
|
|
func typeToObject(typ types.Type) types.Object {
|
|
|
|
switch typ := typ.(type) {
|
|
|
|
case *types.Named:
|
|
|
|
return typ.Obj()
|
|
|
|
case *types.Pointer:
|
|
|
|
return typeToObject(typ.Elem())
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 15:58:16 -06:00
|
|
|
func hasErrorType(obj types.Object) bool {
|
|
|
|
return types.IsInterface(obj.Type()) && obj.Pkg() == nil && obj.Name() == "error"
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:49:12 -07:00
|
|
|
func objToNode(v View, pkg Package, obj types.Object) (ast.Decl, error) {
|
2020-01-10 15:37:29 -07:00
|
|
|
declAST, _, err := findPosInPackage(v, pkg, obj.Pos())
|
2019-09-16 16:17:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-09 18:22:42 -06:00
|
|
|
path, _ := astutil.PathEnclosingInterval(declAST, obj.Pos(), obj.Pos())
|
2019-04-17 16:21:47 -06:00
|
|
|
if path == nil {
|
2019-09-09 18:22:42 -06:00
|
|
|
return nil, errors.Errorf("no path for object %v", obj.Name())
|
2019-04-17 16:21:47 -06:00
|
|
|
}
|
2019-04-19 11:09:33 -06:00
|
|
|
for _, node := range path {
|
|
|
|
switch node := node.(type) {
|
|
|
|
case *ast.GenDecl:
|
|
|
|
// Type names, fields, and methods.
|
|
|
|
switch obj.(type) {
|
|
|
|
case *types.TypeName, *types.Var, *types.Const, *types.Func:
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
// Function signatures.
|
|
|
|
if _, ok := obj.(*types.Func); ok {
|
2019-04-17 16:21:47 -06:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-19 11:09:33 -06:00
|
|
|
return nil, nil // didn't find a node, but don't fail
|
2019-04-17 16:21:47 -06:00
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
|
|
|
|
// importSpec handles positions inside of an *ast.ImportSpec.
|
2019-11-22 12:49:12 -07:00
|
|
|
func importSpec(s Snapshot, pkg Package, file *ast.File, pos token.Pos) (*IdentifierInfo, error) {
|
2019-06-24 14:34:21 -06:00
|
|
|
var imp *ast.ImportSpec
|
2019-09-09 17:26:26 -06:00
|
|
|
for _, spec := range file.Imports {
|
2019-08-21 17:36:09 -06:00
|
|
|
if spec.Path.Pos() <= pos && pos < spec.Path.End() {
|
2019-06-24 14:34:21 -06:00
|
|
|
imp = spec
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
if imp == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
importPath, err := strconv.Unquote(imp.Path.Value)
|
|
|
|
if err != nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("import path not quoted: %s (%v)", imp.Path.Value, err)
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
result := &IdentifierInfo{
|
2019-11-22 12:49:12 -07:00
|
|
|
Snapshot: s,
|
2019-10-23 14:10:24 -06:00
|
|
|
Name: importPath,
|
|
|
|
pkg: pkg,
|
2019-08-26 22:26:45 -06:00
|
|
|
}
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.mappedRange, err = posToMappedRange(s.View(), pkg, imp.Path.Pos(), imp.Path.End()); err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
return nil, err
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
// Consider the "declaration" of an import spec to be the imported package.
|
2019-10-29 16:13:19 -06:00
|
|
|
importedPkg, err := pkg.GetImport(importPath)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-10-24 13:44:41 -06:00
|
|
|
if importedPkg.GetSyntax() == nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("no syntax for for %q", importPath)
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
|
|
|
// Heuristic: Jump to the longest (most "interesting") file of the package.
|
|
|
|
var dest *ast.File
|
2019-10-24 13:44:41 -06:00
|
|
|
for _, f := range importedPkg.GetSyntax() {
|
2019-06-24 14:34:21 -06:00
|
|
|
if dest == nil || f.End()-f.Pos() > dest.End()-dest.Pos() {
|
|
|
|
dest = f
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 14:34:21 -06:00
|
|
|
if dest == nil {
|
2019-08-06 13:13:11 -06:00
|
|
|
return nil, errors.Errorf("package %q has no files", importPath)
|
2019-06-24 14:34:21 -06:00
|
|
|
}
|
2019-11-22 12:49:12 -07:00
|
|
|
if result.Declaration.mappedRange, err = posToMappedRange(s.View(), pkg, dest.Pos(), dest.End()); err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Declaration.node = imp
|
2019-06-24 14:34:21 -06:00
|
|
|
return result, nil
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
2019-06-14 12:55:24 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
// typeSwitchImplicits returns all the implicit type switch objects
|
|
|
|
// that correspond to the leaf *ast.Ident.
|
|
|
|
func typeSwitchImplicits(pkg Package, path []ast.Node) []types.Object {
|
|
|
|
ident, _ := path[0].(*ast.Ident)
|
|
|
|
if ident == nil {
|
2019-06-14 12:55:24 -06:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
ts *ast.TypeSwitchStmt
|
|
|
|
assign *ast.AssignStmt
|
|
|
|
cc *ast.CaseClause
|
|
|
|
obj = pkg.GetTypesInfo().ObjectOf(ident)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Walk our ancestors to determine if our leaf ident refers to a
|
|
|
|
// type switch variable, e.g. the "a" from "switch a := b.(type)".
|
|
|
|
Outer:
|
|
|
|
for i := 1; i < len(path); i++ {
|
|
|
|
switch n := path[i].(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
// Check if ident is the "a" in "a := foo.(type)". The "a" in
|
|
|
|
// this case has no types.Object, so check for ident equality.
|
|
|
|
if len(n.Lhs) == 1 && n.Lhs[0] == ident {
|
|
|
|
assign = n
|
|
|
|
}
|
|
|
|
case *ast.CaseClause:
|
|
|
|
// Check if ident is a use of "a" within a case clause. Each
|
|
|
|
// case clause implicitly maps "a" to a different types.Object,
|
|
|
|
// so check if ident's object is the case clause's implicit
|
|
|
|
// object.
|
|
|
|
if obj != nil && pkg.GetTypesInfo().Implicits[n] == obj {
|
|
|
|
cc = n
|
|
|
|
}
|
|
|
|
case *ast.TypeSwitchStmt:
|
|
|
|
// Look for the type switch that owns our previously found
|
|
|
|
// *ast.AssignStmt or *ast.CaseClause.
|
|
|
|
|
|
|
|
if n.Assign == assign {
|
|
|
|
ts = n
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stmt := range n.Body.List {
|
|
|
|
if stmt == cc {
|
|
|
|
ts = n
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 12:55:24 -06:00
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
|
|
|
|
if ts == nil {
|
2019-06-14 12:55:24 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
// Our leaf ident refers to a type switch variable. Fan out to the
|
|
|
|
// type switch's implicit case clause objects.
|
|
|
|
var objs []types.Object
|
|
|
|
for _, cc := range ts.Body.List {
|
|
|
|
if ccObj := pkg.GetTypesInfo().Implicits[cc]; ccObj != nil {
|
|
|
|
objs = append(objs, ccObj)
|
2019-06-14 12:55:24 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 22:06:31 -07:00
|
|
|
return objs
|
2019-06-14 12:55:24 -06:00
|
|
|
}
|