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