clang-tools  10.0.0svn
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 enum class RelationKind : uint8_t {
23  BaseOf,
24 };
25 
26 /// Represents a relation between two symbols.
27 /// For an example "A is a base class of B" may be represented
28 /// as { Subject = A, Predicate = BaseOf, Object = B }.
29 struct Relation {
33 
34  bool operator==(const Relation &Other) const {
35  return std::tie(Subject, Predicate, Object) ==
36  std::tie(Other.Subject, Other.Predicate, Other.Object);
37  }
38  // SPO order
39  bool operator<(const Relation &Other) const {
40  return std::tie(Subject, Predicate, Object) <
41  std::tie(Other.Subject, Other.Predicate, Other.Object);
42  }
43 };
44 
45 class RelationSlab {
46 public:
48  using const_iterator = std::vector<value_type>::const_iterator;
50 
51  RelationSlab() = default;
52  RelationSlab(RelationSlab &&Slab) = default;
53  RelationSlab &operator=(RelationSlab &&RHS) = default;
54 
55  const_iterator begin() const { return Relations.begin(); }
56  const_iterator end() const { return Relations.end(); }
57  size_t size() const { return Relations.size(); }
58  bool empty() const { return Relations.empty(); }
59 
60  size_t bytes() const {
61  return sizeof(*this) + sizeof(value_type) * Relations.capacity();
62  }
63 
64  /// Lookup all relations matching the given subject and predicate.
65  llvm::iterator_range<iterator> lookup(const SymbolID &Subject,
66  RelationKind Predicate) const;
67 
68  /// RelationSlab::Builder is a mutable container that can 'freeze' to
69  /// RelationSlab.
70  class Builder {
71  public:
72  /// Adds a relation to the slab.
73  void insert(const Relation &R) { Relations.push_back(R); }
74 
75  /// Consumes the builder to finalize the slab.
76  RelationSlab build() &&;
77 
78  private:
79  std::vector<Relation> Relations;
80  };
81 
82 private:
83  RelationSlab(std::vector<Relation> Relations)
84  : Relations(std::move(Relations)) {}
85 
86  std::vector<Relation> Relations;
87 };
88 
89 } // namespace clangd
90 } // namespace clang
91 
92 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RELATION_H
bool operator==(const Relation &Other) const
Definition: Relation.h:34
Represents a relation between two symbols.
Definition: Relation.h:29
std::vector< value_type >::const_iterator const_iterator
Definition: Relation.h:48
const_iterator end() const
Definition: Relation.h:56
RelationKind Predicate
Definition: Relation.h:31
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
bool operator<(const Relation &Other) const
Definition: Relation.h:39
const_iterator begin() const
Definition: Relation.h:55
void insert(const Relation &R)
Adds a relation to the slab.
Definition: Relation.h:73
RelationSlab::Builder is a mutable container that can &#39;freeze&#39; to RelationSlab.
Definition: Relation.h:70
size_t bytes() const
Definition: Relation.h:60
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const_iterator iterator
Definition: Relation.h:49
size_t size() const
Definition: Relation.h:57