You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.5 KiB

  1. // Copyright 2013 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. package main
  5. import (
  6. "fmt"
  7. "go/ast"
  8. "go/build"
  9. "go/parser"
  10. "go/token"
  11. pathpkg "path"
  12. "path/filepath"
  13. "strconv"
  14. "golang.org/x/tools/cmd/guru/serial"
  15. "golang.org/x/tools/go/buildutil"
  16. "golang.org/x/tools/go/loader"
  17. )
  18. // definition reports the location of the definition of an identifier.
  19. func definition(q *Query) error {
  20. // First try the simple resolution done by parser.
  21. // It only works for intra-file references but it is very fast.
  22. // (Extending this approach to all the files of the package,
  23. // resolved using ast.NewPackage, was not worth the effort.)
  24. {
  25. qpos, err := fastQueryPos(q.Build, q.Pos)
  26. if err != nil {
  27. return err
  28. }
  29. id, _ := qpos.path[0].(*ast.Ident)
  30. if id == nil {
  31. return fmt.Errorf("no identifier here")
  32. }
  33. // Did the parser resolve it to a local object?
  34. if obj := id.Obj; obj != nil && obj.Pos().IsValid() {
  35. q.Output(qpos.fset, &definitionResult{
  36. pos: obj.Pos(),
  37. descr: fmt.Sprintf("%s %s", obj.Kind, obj.Name),
  38. })
  39. return nil // success
  40. }
  41. // Qualified identifier?
  42. if pkg := packageForQualIdent(qpos.path, id); pkg != "" {
  43. srcdir := filepath.Dir(qpos.fset.File(qpos.start).Name())
  44. tok, pos, err := findPackageMember(q.Build, qpos.fset, srcdir, pkg, id.Name)
  45. if err != nil {
  46. return err
  47. }
  48. q.Output(qpos.fset, &definitionResult{
  49. pos: pos,
  50. descr: fmt.Sprintf("%s %s.%s", tok, pkg, id.Name),
  51. })
  52. return nil // success
  53. }
  54. // Fall back on the type checker.
  55. }
  56. // Run the type checker.
  57. lconf := loader.Config{Build: q.Build}
  58. allowErrors(&lconf)
  59. if _, err := importQueryPackage(q.Pos, &lconf); err != nil {
  60. return err
  61. }
  62. // Load/parse/type-check the program.
  63. lprog, err := lconf.Load()
  64. if err != nil {
  65. return err
  66. }
  67. qpos, err := parseQueryPos(lprog, q.Pos, false)
  68. if err != nil {
  69. return err
  70. }
  71. id, _ := qpos.path[0].(*ast.Ident)
  72. if id == nil {
  73. return fmt.Errorf("no identifier here")
  74. }
  75. // Look up the declaration of this identifier.
  76. // If id is an anonymous field declaration,
  77. // it is both a use of a type and a def of a field;
  78. // prefer the use in that case.
  79. obj := qpos.info.Uses[id]
  80. if obj == nil {
  81. obj = qpos.info.Defs[id]
  82. if obj == nil {
  83. // Happens for y in "switch y := x.(type)",
  84. // and the package declaration,
  85. // but I think that's all.
  86. return fmt.Errorf("no object for identifier")
  87. }
  88. }
  89. if !obj.Pos().IsValid() {
  90. return fmt.Errorf("%s is built in", obj.Name())
  91. }
  92. q.Output(lprog.Fset, &definitionResult{
  93. pos: obj.Pos(),
  94. descr: qpos.objectString(obj),
  95. })
  96. return nil
  97. }
  98. // packageForQualIdent returns the package p if id is X in a qualified
  99. // identifier p.X; it returns "" otherwise.
  100. //
  101. // Precondition: id is path[0], and the parser did not resolve id to a
  102. // local object. For speed, packageForQualIdent assumes that p is a
  103. // package iff it is the basename of an import path (and not, say, a
  104. // package-level decl in another file or a predeclared identifier).
  105. func packageForQualIdent(path []ast.Node, id *ast.Ident) string {
  106. if sel, ok := path[1].(*ast.SelectorExpr); ok && sel.Sel == id && ast.IsExported(id.Name) {
  107. if pkgid, ok := sel.X.(*ast.Ident); ok && pkgid.Obj == nil {
  108. f := path[len(path)-1].(*ast.File)
  109. for _, imp := range f.Imports {
  110. path, _ := strconv.Unquote(imp.Path.Value)
  111. if imp.Name != nil {
  112. if imp.Name.Name == pkgid.Name {
  113. return path // renaming import
  114. }
  115. } else if pathpkg.Base(path) == pkgid.Name {
  116. return path // ordinary import
  117. }
  118. }
  119. }
  120. }
  121. return ""
  122. }
  123. // findPackageMember returns the type and position of the declaration of
  124. // pkg.member by loading and parsing the files of that package.
  125. // srcdir is the directory in which the import appears.
  126. func findPackageMember(ctxt *build.Context, fset *token.FileSet, srcdir, pkg, member string) (token.Token, token.Pos, error) {
  127. bp, err := ctxt.Import(pkg, srcdir, 0)
  128. if err != nil {
  129. return 0, token.NoPos, err // no files for package
  130. }
  131. // TODO(adonovan): opt: parallelize.
  132. for _, fname := range bp.GoFiles {
  133. filename := filepath.Join(bp.Dir, fname)
  134. // Parse the file, opening it the file via the build.Context
  135. // so that we observe the effects of the -modified flag.
  136. f, _ := buildutil.ParseFile(fset, ctxt, nil, ".", filename, parser.Mode(0))
  137. if f == nil {
  138. continue
  139. }
  140. // Find a package-level decl called 'member'.
  141. for _, decl := range f.Decls {
  142. switch decl := decl.(type) {
  143. case *ast.GenDecl:
  144. for _, spec := range decl.Specs {
  145. switch spec := spec.(type) {
  146. case *ast.ValueSpec:
  147. // const or var
  148. for _, id := range spec.Names {
  149. if id.Name == member {
  150. return decl.Tok, id.Pos(), nil
  151. }
  152. }
  153. case *ast.TypeSpec:
  154. if spec.Name.Name == member {
  155. return token.TYPE, spec.Name.Pos(), nil
  156. }
  157. }
  158. }
  159. case *ast.FuncDecl:
  160. if decl.Recv == nil && decl.Name.Name == member {
  161. return token.FUNC, decl.Name.Pos(), nil
  162. }
  163. }
  164. }
  165. }
  166. return 0, token.NoPos, fmt.Errorf("couldn't find declaration of %s in %q", member, pkg)
  167. }
  168. type definitionResult struct {
  169. pos token.Pos // (nonzero) location of definition
  170. descr string // description of object it denotes
  171. }
  172. func (r *definitionResult) PrintPlain(printf printfFunc) {
  173. printf(r.pos, "defined here as %s", r.descr)
  174. }
  175. func (r *definitionResult) JSON(fset *token.FileSet) []byte {
  176. return toJSON(&serial.Definition{
  177. Desc: r.descr,
  178. ObjPos: fset.Position(r.pos).String(),
  179. })
  180. }