| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_CONTEXT_H |
| 15 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_CONTEXT_H |
| 16 |
|
| 17 |
#include "llvm/ADT/STLExtras.h" |
| 18 |
#include "llvm/Support/Compiler.h" |
| 19 |
#include <memory> |
| 20 |
#include <type_traits> |
| 21 |
|
| 22 |
namespace clang { |
| 23 |
namespace clangd { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
template <class Type> class Key { |
| 41 |
public: |
| 42 |
static_assert(!std::is_reference<Type>::value, |
| 43 |
"Reference arguments to Key<> are not allowed"); |
| 44 |
|
| 45 |
constexpr Key() = default; |
| 46 |
|
| 47 |
Key(Key const &) = delete; |
| 48 |
Key &operator=(Key const &) = delete; |
| 49 |
Key(Key &&) = delete; |
| 50 |
Key &operator=(Key &&) = delete; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
class Context { |
| 70 |
public: |
| 71 |
|
| 72 |
static Context empty(); |
| 73 |
|
| 74 |
static const Context ¤t(); |
| 75 |
|
| 76 |
|
| 77 |
static Context swapCurrent(Context Replacement); |
| 78 |
|
| 79 |
private: |
| 80 |
struct Data; |
| 81 |
Context(std::shared_ptr<const Data> DataPtr); |
| 82 |
|
| 83 |
public: |
| 84 |
|
| 85 |
Context() = default; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
Context(Context const &) = delete; |
| 90 |
Context &operator=(const Context &) = delete; |
| 91 |
|
| 92 |
Context(Context &&) = default; |
| 93 |
Context &operator=(Context &&) = default; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
template <class Type> const Type *get(const Key<Type> &Key) const { |
| 99 |
for (const Data *DataPtr = this->DataPtr.get(); DataPtr != nullptr; |
| 100 |
DataPtr = DataPtr->Parent.get()) { |
| 101 |
if (DataPtr->KeyPtr == &Key) |
| 102 |
return static_cast<const Type *>(DataPtr->Value->getValuePtr()); |
| 103 |
} |
| 104 |
return nullptr; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
template <class Type> const Type &getExisting(const Key<Type> &Key) const { |
| 110 |
auto Val = get(Key); |
| 111 |
assert(Val && "Key does not exist"); |
| 112 |
return *Val; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
template <class Type> |
| 119 |
Context derive(const Key<Type> &Key, std::decay_t<Type> Value) const & { |
| 120 |
return Context(std::make_shared<Data>( |
| 121 |
Data{DataPtr, &Key, |
| 122 |
std::make_unique<TypedAnyStorage<std::decay_t<Type>>>( |
| 123 |
std::move(Value))})); |
| 124 |
} |
| 125 |
|
| 126 |
template <class Type> |
| 127 |
Context derive(const Key<Type> &Key, |
| 128 |
std::decay_t<Type> Value) && { |
| 129 |
return Context(std::make_shared<Data>( |
| 130 |
Data{std::move(DataPtr), &Key, |
| 131 |
std::make_unique<TypedAnyStorage<std::decay_t<Type>>>( |
| 132 |
std::move(Value))})); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
template <class Type> Context derive(Type &&Value) const & { |
| 138 |
static Key<std::decay_t<Type>> Private; |
| 139 |
return derive(Private, std::forward<Type>(Value)); |
| 140 |
} |
| 141 |
|
| 142 |
template <class Type> Context derive(Type &&Value) && { |
| 143 |
static Key<std::decay_t<Type>> Private; |
| 144 |
return std::move(*this).derive(Private, std::forward<Type>(Value)); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
Context clone() const; |
| 149 |
|
| 150 |
private: |
| 151 |
class AnyStorage { |
| 152 |
public: |
| 153 |
virtual ~AnyStorage() = default; |
| 154 |
virtual void *getValuePtr() = 0; |
| 155 |
}; |
| 156 |
|
| 157 |
template <class T> class TypedAnyStorage : public Context::AnyStorage { |
| 158 |
static_assert(std::is_same<std::decay_t<T>, T>::value, |
| 159 |
"Argument to TypedAnyStorage must be decayed"); |
| 160 |
|
| 161 |
public: |
| 162 |
TypedAnyStorage(T &&Value) : Value(std::move(Value)) {} |
| 163 |
|
| 164 |
void *getValuePtr() override { return &Value; } |
| 165 |
|
| 166 |
private: |
| 167 |
T Value; |
| 168 |
}; |
| 169 |
|
| 170 |
struct Data { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
std::shared_ptr<const Data> Parent; |
| 175 |
const void *KeyPtr; |
| 176 |
std::unique_ptr<AnyStorage> Value; |
| 177 |
}; |
| 178 |
|
| 179 |
std::shared_ptr<const Data> DataPtr; |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
class [[nodiscard]] WithContext { |
| 186 |
public: |
| 187 |
WithContext(Context C) : Restore(Context::swapCurrent(std::move(C))) {} |
| 188 |
~WithContext() { Context::swapCurrent(std::move(Restore)); } |
| 189 |
WithContext(const WithContext &) = delete; |
| 190 |
WithContext &operator=(const WithContext &) = delete; |
| 191 |
WithContext(WithContext &&) = delete; |
| 192 |
WithContext &operator=(WithContext &&) = delete; |
| 193 |
|
| 194 |
private: |
| 195 |
Context Restore; |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
class [[nodiscard]] WithContextValue { |
| 201 |
public: |
| 202 |
template <typename T> |
| 203 |
WithContextValue(const Key<T> &K, std::decay_t<T> V) |
| 204 |
: Restore(Context::current().derive(K, std::move(V))) {} |
| 205 |
|
| 206 |
|
| 207 |
template <typename T> |
| 208 |
WithContextValue(T &&V) |
| 209 |
: Restore(Context::current().derive(std::forward<T>(V))) {} |
| 210 |
|
| 211 |
private: |
| 212 |
WithContext Restore; |
| 213 |
}; |
| 214 |
|
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
#endif |