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