clang-tools  9.0.0
Relation.h
Go to the documentation of this file.
1 //===--- Relation.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_RELATION_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RELATION_H
11 
12 #include "SymbolID.h"
13 #include "SymbolLocation.h"
14 #include "clang/Index/IndexSymbol.h"
15 #include "llvm/ADT/iterator_range.h"
16 #include <cstdint>
17 #include <utility>
18 
19 namespace clang {
20 namespace clangd {
21 
22 /// Represents a relation between two symbols.
23 /// For an example "A is a base class of B" may be represented
24 /// as { Subject = A, Predicate = RelationBaseOf, Object = B }.
25 struct Relation {
27  index::SymbolRole Predicate;
29 
30  bool operator==(const Relation &Other) const {
31  return std::tie(Subject, Predicate, Object) ==
32  std::tie(Other.Subject, Other.Predicate, Other.Object);
33  }
34  // SPO order
35  bool operator<(const Relation &Other) const {
36  return std::tie(Subject, Predicate, Object) <
37  std::tie(Other.Subject, Other.Predicate, Other.Object);
38  }
39 };
40 
41 class RelationSlab {
42 public:
44  using const_iterator = std::vector<value_type>::const_iterator;
46 
47  RelationSlab() = default;
48  RelationSlab(RelationSlab &&Slab) = default;
49  RelationSlab &operator=(RelationSlab &&RHS) = default;
50 
51  const_iterator begin() const { return Relations.begin(); }
52  const_iterator end() const { return Relations.end(); }
53  size_t size() const { return Relations.size(); }
54  bool empty() const { return Relations.empty(); }
55 
56  size_t bytes() const {
57  return sizeof(*this) + sizeof(value_type) * Relations.capacity();
58  }
59 
60  /// Lookup all relations matching the given subject and predicate.
61  llvm::iterator_range<iterator> lookup(const SymbolID &Subject,
62  index::SymbolRole Predicate) const;
63 
64  /// RelationSlab::Builder is a mutable container that can 'freeze' to
65  /// RelationSlab.
66  class Builder {
67  public:
68  /// Adds a relation to the slab.
69  void insert(const Relation &R) { Relations.push_back(R); }
70 
71  /// Consumes the builder to finalize the slab.
72  RelationSlab build() &&;
73 
74  private:
75  std::vector<Relation> Relations;
76  };
77 
78 private:
79  RelationSlab(std::vector<Relation> Relations)
80  : Relations(std::move(Relations)) {}
81 
82  std::vector<Relation> Relations;
83 };
84 
85 } // namespace clangd
86 } // namespace clang
87 
88 namespace llvm {
89 
90 // Support index::SymbolRole as a DenseMap key for the purpose of looking up
91 // relations.
92 template <> struct DenseMapInfo<clang::index::SymbolRole> {
93  static inline clang::index::SymbolRole getEmptyKey() {
94  // Choose an enumerator that's not a relation.
95  return clang::index::SymbolRole::Declaration;
96  }
97 
98  static inline clang::index::SymbolRole getTombstoneKey() {
99  // Choose another enumerator that's not a relation.
100  return clang::index::SymbolRole::Definition;
101  }
102 
103  static unsigned getHashValue(const clang::index::SymbolRole &Key) {
104  return hash_value(Key);
105  }
106 
107  static bool isEqual(const clang::index::SymbolRole &LHS,
108  const clang::index::SymbolRole &RHS) {
109  return LHS == RHS;
110  }
111 };
112 
113 } // namespace llvm
114 
115 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RELATION_H
bool operator==(const Relation &Other) const
Definition: Relation.h:30
Some operations such as code completion produce a set of candidates.
Represents a relation between two symbols.
Definition: Relation.h:25
std::vector< value_type >::const_iterator const_iterator
Definition: Relation.h:44
const_iterator end() const
Definition: Relation.h:52
static bool isEqual(const clang::index::SymbolRole &LHS, const clang::index::SymbolRole &RHS)
Definition: Relation.h:107
static clang::index::SymbolRole getEmptyKey()
Definition: Relation.h:93
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
index::SymbolRole Predicate
Definition: Relation.h:27
bool operator<(const Relation &Other) const
Definition: Relation.h:35
RelationSlab Relations
const_iterator begin() const
Definition: Relation.h:51
void insert(const Relation &R)
Adds a relation to the slab.
Definition: Relation.h:69
static unsigned getHashValue(const clang::index::SymbolRole &Key)
Definition: Relation.h:103
RelationSlab::Builder is a mutable container that can &#39;freeze&#39; to RelationSlab.
Definition: Relation.h:66
size_t bytes() const
Definition: Relation.h:56
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const_iterator iterator
Definition: Relation.h:45
static clang::index::SymbolRole getTombstoneKey()
Definition: Relation.h:98
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:50
size_t size() const
Definition: Relation.h:53