1 //===--- Context.h - Mechanism for passing implicit data --------*- 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 // Context for storing and retrieving implicit data. Useful for passing implicit 10 // parameters on a per-request basis. 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 /// Values in a Context are indexed by typed keys. 26 /// Key<T> serves two purposes: 27 /// - it provides a lookup key for the context (each Key is unique), 28 /// - it makes lookup type-safe: a Key<T> can only map to a T (or nothing). 29 /// 30 /// Example: 31 /// Key<int> RequestID; 32 /// Key<int> Version; 33 /// 34 /// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3); 35 /// assert(*Ctx.get(RequestID) == 10); 36 /// assert(*Ctx.get(Version) == 3); 37 /// 38 /// Keys are typically used across multiple functions, so most of the time you 39 /// would want to make them static class members or global variables. 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 /// A context is an immutable container for per-request data that must be 54 /// propagated through layers that don't care about it. An example is a request 55 /// ID that we may want to use when logging. 56 /// 57 /// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has 58 /// an associated value type, which allows the map to be typesafe. 59 /// 60 /// There is an "ambient" context for each thread, Context::current(). 61 /// Most functions should read from this, and use WithContextValue or 62 /// WithContext to extend or replace the context within a block scope. 63 /// Only code dealing with threads and extension points should need to use 64 /// other Context objects. 65 /// 66 /// You can't add data to an existing context, instead you create a new 67 /// immutable context derived from it with extra data added. When you retrieve 68 /// data, the context will walk up the parent chain until the key is found. 69 class Context { 70 public: 71 /// Returns an empty root context that contains no data. 72 static Context empty(); 73 /// Returns the context for the current thread, creating it if needed. 74 static const Context ¤t(); 75 // Sets the current() context to Replacement, and returns the old context. 76 // Prefer to use WithContext or WithContextValue to do this safely. 77 static Context swapCurrent(Context Replacement); 78 79 private: 80 struct Data; 81 Context(std::shared_ptr<const Data> DataPtr); 82 83 public: 84 /// Same as Context::empty(), please use Context::empty() instead. 85 /// Constructor is defined to workaround a bug in MSVC's version of STL. 86 /// (arguments of std::future<> must be default-constructible in MSVC). 87 Context() = default; 88 89 /// Copy operations for this class are deleted, use an explicit clone() method 90 /// when you need a copy of the context instead. 91 Context(Context const &) = delete; 92 Context &operator=(const Context &) = delete; 93 94 Context(Context &&) = default; 95 Context &operator=(Context &&) = default; 96 97 /// Get data stored for a typed \p Key. If values are not found 98 /// \returns Pointer to the data associated with \p Key. If no data is 99 /// specified for \p Key, return null. get(const Key<Type> & Key)100 template <class Type> const Type *get(const Key<Type> &Key) const { 101 for (const Data *DataPtr = this->DataPtr.get(); DataPtr != nullptr; 102 DataPtr = DataPtr->Parent.get()) { 103 if (DataPtr->KeyPtr == &Key) 104 return static_cast<const Type *>(DataPtr->Value->getValuePtr()); 105 } 106 return nullptr; 107 } 108 109 /// A helper to get a reference to a \p Key that must exist in the map. 110 /// Must not be called for keys that are not in the map. getExisting(const Key<Type> & Key)111 template <class Type> const Type &getExisting(const Key<Type> &Key) const { 112 auto Val = get(Key); 113 assert(Val && "Key does not exist"); 114 return *Val; 115 } 116 117 /// Derives a child context 118 /// It is safe to move or destroy a parent context after calling derive(). 119 /// The child will keep its parent alive, and its data remains accessible. 120 template <class Type> derive(const Key<Type> & Key,typename std::decay<Type>::type Value)121 Context derive(const Key<Type> &Key, 122 typename std::decay<Type>::type Value) const & { 123 return Context(std::make_shared<Data>( 124 Data{/*Parent=*/DataPtr, &Key, 125 std::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>( 126 std::move(Value))})); 127 } 128 129 template <class Type> 130 Context derive(const Key<Type> & Key,typename std::decay<Type>::type Value)131 derive(const Key<Type> &Key, 132 typename std::decay<Type>::type Value) && /* takes ownership */ { 133 return Context(std::make_shared<Data>( 134 Data{/*Parent=*/std::move(DataPtr), &Key, 135 std::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>( 136 std::move(Value))})); 137 } 138 139 /// Derives a child context, using an anonymous key. 140 /// Intended for objects stored only for their destructor's side-effect. derive(Type && Value)141 template <class Type> Context derive(Type &&Value) const & { 142 static Key<typename std::decay<Type>::type> Private; 143 return derive(Private, std::forward<Type>(Value)); 144 } 145 derive(Type && Value)146 template <class Type> Context derive(Type &&Value) && { 147 static Key<typename std::decay<Type>::type> Private; 148 return std::move(*this).derive(Private, std::forward<Type>(Value)); 149 } 150 151 /// Clone this context object. 152 Context clone() const; 153 154 private: 155 class AnyStorage { 156 public: 157 virtual ~AnyStorage() = default; 158 virtual void *getValuePtr() = 0; 159 }; 160 161 template <class T> class TypedAnyStorage : public Context::AnyStorage { 162 static_assert(std::is_same<typename std::decay<T>::type, T>::value, 163 "Argument to TypedAnyStorage must be decayed"); 164 165 public: TypedAnyStorage(T && Value)166 TypedAnyStorage(T &&Value) : Value(std::move(Value)) {} 167 getValuePtr()168 void *getValuePtr() override { return &Value; } 169 170 private: 171 T Value; 172 }; 173 174 struct Data { 175 // We need to make sure Parent outlives the Value, so the order of members 176 // is important. We do that to allow classes stored in Context's child 177 // layers to store references to the data in the parent layers. 178 std::shared_ptr<const Data> Parent; 179 const void *KeyPtr; 180 std::unique_ptr<AnyStorage> Value; 181 }; 182 183 std::shared_ptr<const Data> DataPtr; 184 }; 185 186 /// WithContext replaces Context::current() with a provided scope. 187 /// When the WithContext is destroyed, the original scope is restored. 188 /// For extending the current context with new value, prefer WithContextValue. 189 class LLVM_NODISCARD WithContext { 190 public: WithContext(Context C)191 WithContext(Context C) : Restore(Context::swapCurrent(std::move(C))) {} ~WithContext()192 ~WithContext() { Context::swapCurrent(std::move(Restore)); } 193 WithContext(const WithContext &) = delete; 194 WithContext &operator=(const WithContext &) = delete; 195 WithContext(WithContext &&) = delete; 196 WithContext &operator=(WithContext &&) = delete; 197 198 private: 199 Context Restore; 200 }; 201 202 /// WithContextValue extends Context::current() with a single value. 203 /// When the WithContextValue is destroyed, the original scope is restored. 204 class LLVM_NODISCARD WithContextValue { 205 public: 206 template <typename T> WithContextValue(const Key<T> & K,typename std::decay<T>::type V)207 WithContextValue(const Key<T> &K, typename std::decay<T>::type V) 208 : Restore(Context::current().derive(K, std::move(V))) {} 209 210 // Anonymous values can be used for the destructor side-effect. 211 template <typename T> WithContextValue(T && V)212 WithContextValue(T &&V) 213 : Restore(Context::current().derive(std::forward<T>(V))) {} 214 215 private: 216 WithContext Restore; 217 }; 218 219 } // namespace clangd 220 } // namespace clang 221 222 #endif 223