1 //===--- Token.h - Symbol Search primitive ----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Token objects represent a characteristic of a symbol, which can be used to
11 /// perform efficient search. Tokens are keys for inverted index which are
12 /// mapped to the corresponding posting lists.
14 /// The symbol std::cout might have the tokens:
18 /// * Type "std::ostream"
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_TOKEN_H
23 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_TOKEN_H
25 #include "llvm/ADT/Hashing.h"
26 #include "llvm/Support/raw_ostream.h"
34 /// A Token represents an attribute of a symbol, such as a particular trigram
35 /// present in the name (used for fuzzy search).
37 /// Tokens can be used to perform more sophisticated search queries by
38 /// constructing complex iterator trees.
41 /// Kind specifies Token type which defines semantics for the internal
42 /// representation. Each Kind has different representation stored in Data
44 // FIXME(kbobyrev): Storing Data hash would be more efficient than storing raw
45 // strings. For example, PathURI store URIs of each directory and its parents,
46 // which induces a lot of overhead because these paths tend to be long and
47 // each parent directory is a prefix.
49 /// Represents trigram used for fuzzy search of unqualified symbol names.
51 /// Data contains 3 bytes with trigram contents.
53 /// Scope primitives, e.g. "symbol belongs to namespace foo::bar".
55 /// Data stroes full scope name, e.g. "foo::bar::baz::" or "" (for global
58 /// Path Proximity URI to symbol declaration.
60 /// Data stores path URI of symbol declaration file or its parent.
62 /// Example: "file:///path/to/clang-tools-extra/clangd/index/SymbolIndex.h"
63 /// and some amount of its parents.
65 /// Type of symbol (see `Symbol::Type`).
67 /// Internal Token type for invalid/special tokens, e.g. empty tokens for
72 Token(Kind TokenKind
, llvm::StringRef Data
)
73 : Data(Data
), TokenKind(TokenKind
) {}
75 bool operator==(const Token
&Other
) const {
76 return TokenKind
== Other
.TokenKind
&& Data
== Other
.Data
;
79 friend llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const Token
&T
) {
80 switch (T
.TokenKind
) {
87 case Kind::ProximityURI
:
101 /// Representation which is unique among Token with the same Kind.
105 friend llvm::hash_code
hash_value(const Token
&Token
) {
106 return llvm::hash_combine(static_cast<int>(Token
.TokenKind
), Token
.Data
);
111 } // namespace clangd
116 // Support Tokens as DenseMap keys.
117 template <> struct DenseMapInfo
<clang::clangd::dex::Token
> {
118 static inline clang::clangd::dex::Token
getEmptyKey() {
119 return {clang::clangd::dex::Token::Kind::Sentinel
, "EmptyKey"};
122 static inline clang::clangd::dex::Token
getTombstoneKey() {
123 return {clang::clangd::dex::Token::Kind::Sentinel
, "TombstoneKey"};
126 static unsigned getHashValue(const clang::clangd::dex::Token
&Tag
) {
127 return hash_value(Tag
);
130 static bool isEqual(const clang::clangd::dex::Token
&LHS
,
131 const clang::clangd::dex::Token
&RHS
) {
138 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_TOKEN_H