clang-tools  15.0.0git
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 "index/SymbolID.h"
13 #include "llvm/ADT/iterator_range.h"
14 #include <cstdint>
15 #include <utility>
16 
17 namespace clang {
18 namespace clangd {
19 
20 enum class RelationKind : uint8_t {
21  BaseOf,
23 };
24 
25 /// Represents a relation between two symbols.
26 /// For an example:
27 /// - "A is a base class of B" is represented as
28 /// { Subject = A, Predicate = BaseOf, Object = B }.
29 /// - "Derived::Foo overrides Base::Foo" is represented as
30 /// { Subject = Base::Foo, Predicate = OverriddenBy, Object = Derived::Foo
31 /// }.
32 struct Relation {
36 
37  bool operator==(const Relation &Other) const {
38  return std::tie(Subject, Predicate, Object) ==
39  std::tie(Other.Subject, Other.Predicate, Other.Object);
40  }
41  // SPO order
42  bool operator<(const Relation &Other) const {
43  return std::tie(Subject, Predicate, Object) <
44  std::tie(Other.Subject, Other.Predicate, Other.Object);
45  }
46 };
47 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const RelationKind R);
48 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Relation &R);
49 
50 class RelationSlab {
51 public:
53  using const_iterator = std::vector<value_type>::const_iterator;
55 
56  RelationSlab() = default;
57  RelationSlab(RelationSlab &&Slab) = default;
58  RelationSlab &operator=(RelationSlab &&RHS) = default;
59 
60  const_iterator begin() const { return Relations.begin(); }
61  const_iterator end() const { return Relations.end(); }
62  size_t size() const { return Relations.size(); }
63  bool empty() const { return Relations.empty(); }
64 
65  size_t bytes() const {
66  return sizeof(*this) + sizeof(value_type) * Relations.capacity();
67  }
68 
69  /// Lookup all relations matching the given subject and predicate.
70  llvm::iterator_range<iterator> lookup(const SymbolID &Subject,
71  RelationKind Predicate) const;
72 
73  /// RelationSlab::Builder is a mutable container that can 'freeze' to
74  /// RelationSlab.
75  class Builder {
76  public:
77  /// Adds a relation to the slab.
78  void insert(const Relation &R) { Relations.push_back(R); }
79 
80  /// Consumes the builder to finalize the slab.
81  RelationSlab build() &&;
82 
83  private:
84  std::vector<Relation> Relations;
85  };
86 
87 private:
88  RelationSlab(std::vector<Relation> Relations)
89  : Relations(std::move(Relations)) {}
90 
91  std::vector<Relation> Relations;
92 };
93 
94 } // namespace clangd
95 } // namespace clang
96 
97 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_RELATION_H
SymbolID.h
clang::clangd::RelationKind::BaseOf
@ BaseOf
clang::clangd::RelationSlab::operator=
RelationSlab & operator=(RelationSlab &&RHS)=default
clang::clangd::RelationSlab::begin
const_iterator begin() const
Definition: Relation.h:60
clang::clangd::RelationSlab::value_type
Relation value_type
Definition: Relation.h:52
clang::clangd::RelationSlab::const_iterator
std::vector< value_type >::const_iterator const_iterator
Definition: Relation.h:53
clang::clangd::RelationSlab::Builder::insert
void insert(const Relation &R)
Adds a relation to the slab.
Definition: Relation.h:78
clang::clangd::Relation
Represents a relation between two symbols.
Definition: Relation.h:32
clang::clangd::RelationSlab::Builder
RelationSlab::Builder is a mutable container that can 'freeze' to RelationSlab.
Definition: Relation.h:75
clang::clangd::RelationSlab::empty
bool empty() const
Definition: Relation.h:63
clang::clangd::RelationSlab
Definition: Relation.h:50
clang::clangd::Relation::Object
SymbolID Object
Definition: Relation.h:35
clang::clangd::RelationSlab::iterator
const_iterator iterator
Definition: Relation.h:54
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2159
clang::clangd::RelationSlab::size
size_t size() const
Definition: Relation.h:62
clang::clangd::RelationSlab::bytes
size_t bytes() const
Definition: Relation.h:65
clang::clangd::Relation::operator==
bool operator==(const Relation &Other) const
Definition: Relation.h:37
clang::clangd::Relation::Subject
SymbolID Subject
Definition: Relation.h:33
clang::clangd::RelationSlab::end
const_iterator end() const
Definition: Relation.h:61
clang::clangd::RelationSlab::RelationSlab
RelationSlab()=default
clang::clangd::Relation::Predicate
RelationKind Predicate
Definition: Relation.h:34
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
clang::clangd::Relation::operator<
bool operator<(const Relation &Other) const
Definition: Relation.h:42
clang::clangd::RelationSlab::Builder::build
RelationSlab build() &&
Consumes the builder to finalize the slab.
Definition: Relation.cpp:41
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::RelationKind::OverriddenBy
@ OverriddenBy
clang::clangd::RelationSlab::lookup
llvm::iterator_range< iterator > lookup(const SymbolID &Subject, RelationKind Predicate) const
Lookup all relations matching the given subject and predicate.
Definition: Relation.cpp:31
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:20