clang-tools 10.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 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 {
30 SymbolID Subject;
31 RelationKind Predicate;
32 SymbolID Object;
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:
47 using value_type = Relation;
48 using const_iterator = std::vector<value_type>::const_iterator;
49 using iterator = 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
clang::clangd::Relation::Object
SymbolID Object
Definition: Relation.h:32
clang::clangd::Relation::operator==
bool operator==(const Relation &Other) const
Definition: Relation.h:34
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:29
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22
clang::clangd::RelationSlab
Definition: Relation.h:45
clang::clangd::RelationSlab::const_iterator
std::vector< value_type >::const_iterator const_iterator
Definition: Relation.h:48
clang::clangd::RelationKind::BaseOf
clang::clangd::RelationSlab::end
const_iterator end() const
Definition: Relation.h:56
clang::clangd::Relation::Predicate
RelationKind Predicate
Definition: Relation.h:31
clang::clangd::lookup
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
clang::clangd::RelationSlab::empty
bool empty() const
Definition: Relation.h:58
clang::clangd::Relation::operator<
bool operator<(const Relation &Other) const
Definition: Relation.h:39
clang::clangd::RelationSlab::begin
const_iterator begin() const
Definition: Relation.h:55
clang::clangd::RelationSlab::Builder::insert
void insert(const Relation &R)
Adds a relation to the slab.
Definition: Relation.h:73
clang::clangd::RelationSlab::Builder
RelationSlab::Builder is a mutable container that can &#39;freeze&#39; to RelationSlab.
Definition: Relation.h:70
clang::clangd::RelationSlab::bytes
size_t bytes() const
Definition: Relation.h:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::RelationSlab::iterator
const_iterator iterator
Definition: Relation.h:49
clang::clangd::RelationSlab::size
size_t size() const
Definition: Relation.h:57
clang::clangd::Relation::Subject
SymbolID Subject
Definition: Relation.h:30
SymbolID.h
SymbolLocation.h
clang::clangd::SymbolID
Definition: SymbolID.h:31