clang-tools  10.0.0svn
Ref.h
Go to the documentation of this file.
1 //===--- Ref.h ---------------------------------------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
11 
12 #include "SymbolID.h"
13 #include "SymbolLocation.h"
14 #include "clang/Index/IndexSymbol.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/Support/StringSaver.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cstdint>
19 #include <set>
20 #include <utility>
21 
22 namespace clang {
23 namespace clangd {
24 
25 /// Describes the kind of a cross-reference.
26 ///
27 /// This is a bitfield which can be combined from different kinds.
28 enum class RefKind : uint8_t {
29  Unknown = 0,
30  Declaration = static_cast<uint8_t>(index::SymbolRole::Declaration),
31  Definition = static_cast<uint8_t>(index::SymbolRole::Definition),
32  Reference = static_cast<uint8_t>(index::SymbolRole::Reference),
34 };
35 
37  return static_cast<RefKind>(static_cast<uint8_t>(L) |
38  static_cast<uint8_t>(R));
39 }
40 inline RefKind &operator|=(RefKind &L, RefKind R) { return L = L | R; }
42  return static_cast<RefKind>(static_cast<uint8_t>(A) &
43  static_cast<uint8_t>(B));
44 }
45 
46 llvm::raw_ostream &operator<<(llvm::raw_ostream &, RefKind);
47 
48 /// Represents a symbol occurrence in the source file.
49 /// Despite the name, it could be a declaration/definition/reference.
50 ///
51 /// WARNING: Location does not own the underlying data - Copies are shallow.
52 struct Ref {
53  /// The source location where the symbol is named.
56 };
57 
58 inline bool operator<(const Ref &L, const Ref &R) {
59  return std::tie(L.Location, L.Kind) < std::tie(R.Location, R.Kind);
60 }
61 inline bool operator==(const Ref &L, const Ref &R) {
62  return std::tie(L.Location, L.Kind) == std::tie(R.Location, R.Kind);
63 }
64 
65 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Ref &);
66 
67 /// An efficient structure of storing large set of symbol references in memory.
68 /// Filenames are deduplicated.
69 class RefSlab {
70 public:
71  // Refs are stored in order.
72  using value_type = std::pair<SymbolID, llvm::ArrayRef<Ref>>;
73  using const_iterator = std::vector<value_type>::const_iterator;
75 
76  RefSlab() = default;
77  RefSlab(RefSlab &&Slab) = default;
78  RefSlab &operator=(RefSlab &&RHS) = default;
79 
80  const_iterator begin() const { return Refs.begin(); }
81  const_iterator end() const { return Refs.end(); }
82  /// Gets the number of symbols.
83  size_t size() const { return Refs.size(); }
84  size_t numRefs() const { return NumRefs; }
85  bool empty() const { return Refs.empty(); }
86 
87  size_t bytes() const {
88  return sizeof(*this) + Arena.getTotalMemory() +
89  sizeof(value_type) * Refs.capacity();
90  }
91 
92  /// RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
93  class Builder {
94  public:
95  Builder() : UniqueStrings(Arena) {}
96  /// Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
97  void insert(const SymbolID &ID, const Ref &S);
98  /// Consumes the builder to finalize the slab.
99  RefSlab build() &&;
100 
101  private:
102  llvm::BumpPtrAllocator Arena;
103  llvm::UniqueStringSaver UniqueStrings; // Contents on the arena.
104  llvm::DenseMap<SymbolID, std::set<Ref>> Refs;
105  };
106 
107 private:
108  RefSlab(std::vector<value_type> Refs, llvm::BumpPtrAllocator Arena,
109  size_t NumRefs)
110  : Arena(std::move(Arena)), Refs(std::move(Refs)), NumRefs(NumRefs) {}
111 
112  llvm::BumpPtrAllocator Arena;
113  std::vector<value_type> Refs;
114  /// Number of all references.
115  size_t NumRefs = 0;
116 };
117 
118 } // namespace clangd
119 } // namespace clang
120 
121 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
size_t numRefs() const
Definition: Ref.h:84
DeclRelationSet operator &(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:176
BindArgumentKind Kind
const_iterator begin() const
Definition: Ref.h:80
const_iterator iterator
Definition: Ref.h:74
llvm::BumpPtrAllocator Arena
DeclRelationSet operator|(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:173
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
bool operator==(const Ref &L, const Ref &R)
Definition: Ref.h:61
SymbolLocation Location
The source location where the symbol is named.
Definition: Ref.h:54
size_t size() const
Gets the number of symbols.
Definition: Ref.h:83
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:28
RefSlab::Builder is a mutable container that can &#39;freeze&#39; to RefSlab.
Definition: Ref.h:93
bool empty() const
Definition: Ref.h:85
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
size_t bytes() const
Definition: Ref.h:87
RefSlab Refs
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
const_iterator end() const
Definition: Ref.h:81
std::vector< value_type >::const_iterator const_iterator
Definition: Ref.h:73
std::pair< SymbolID, llvm::ArrayRef< Ref > > value_type
Definition: Ref.h:72
RefKind Kind
Definition: Ref.h:55
IncludeGraphNode::SourceFlag & operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B)
Definition: Headers.h:96