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.

83 lines
1.9 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 godoc
  5. // ----------------------------------------------------------------------------
  6. // SpotInfo
  7. // A SpotInfo value describes a particular identifier spot in a given file;
  8. // It encodes three values: the SpotKind (declaration or use), a line or
  9. // snippet index "lori", and whether it's a line or index.
  10. //
  11. // The following encoding is used:
  12. //
  13. // bits 32 4 1 0
  14. // value [lori|kind|isIndex]
  15. //
  16. type SpotInfo uint32
  17. // SpotKind describes whether an identifier is declared (and what kind of
  18. // declaration) or used.
  19. type SpotKind uint32
  20. const (
  21. PackageClause SpotKind = iota
  22. ImportDecl
  23. ConstDecl
  24. TypeDecl
  25. VarDecl
  26. FuncDecl
  27. MethodDecl
  28. Use
  29. nKinds
  30. )
  31. var (
  32. // These must match the SpotKind values above.
  33. name = []string{
  34. "Packages",
  35. "Imports",
  36. "Constants",
  37. "Types",
  38. "Variables",
  39. "Functions",
  40. "Methods",
  41. "Uses",
  42. "Unknown",
  43. }
  44. )
  45. func (x SpotKind) Name() string { return name[x] }
  46. func init() {
  47. // sanity check: if nKinds is too large, the SpotInfo
  48. // accessor functions may need to be updated
  49. if nKinds > 8 {
  50. panic("internal error: nKinds > 8")
  51. }
  52. }
  53. // makeSpotInfo makes a SpotInfo.
  54. func makeSpotInfo(kind SpotKind, lori int, isIndex bool) SpotInfo {
  55. // encode lori: bits [4..32)
  56. x := SpotInfo(lori) << 4
  57. if int(x>>4) != lori {
  58. // lori value doesn't fit - since snippet indices are
  59. // most certainly always smaller then 1<<28, this can
  60. // only happen for line numbers; give it no line number (= 0)
  61. x = 0
  62. }
  63. // encode kind: bits [1..4)
  64. x |= SpotInfo(kind) << 1
  65. // encode isIndex: bit 0
  66. if isIndex {
  67. x |= 1
  68. }
  69. return x
  70. }
  71. func (x SpotInfo) Kind() SpotKind { return SpotKind(x >> 1 & 7) }
  72. func (x SpotInfo) Lori() int { return int(x >> 4) }
  73. func (x SpotInfo) IsIndex() bool { return x&1 != 0 }