blob: 7e14f86c144d27fd29deade8ce9853035f929530 [file] [log] [blame]
Ilya Biryukov657159c2017-12-12 11:16:45 +00001//===--- Context.h - Mechanism for passing implicit data --------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Context for storing and retrieving implicit data. Useful for passing implicit
11// parameters on a per-request basis.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_
16#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_
17
18#include "llvm/ADT/STLExtras.h"
Sam McCalld1a7a372018-01-31 13:40:48 +000019#include "llvm/Support/Compiler.h"
Ilya Biryukov657159c2017-12-12 11:16:45 +000020#include <memory>
21#include <type_traits>
22
23namespace clang {
24namespace clangd {
25
Sam McCalld1a7a372018-01-31 13:40:48 +000026/// Values in a Context are indexed by typed keys.
27/// Key<T> serves two purposes:
28/// - it provides a lookup key for the context (each Key is unique),
29/// - it makes lookup type-safe: a Key<T> can only map to a T (or nothing).
30///
31/// Example:
32/// Key<int> RequestID;
33/// Key<int> Version;
34///
35/// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3);
36/// assert(*Ctx.get(RequestID) == 10);
37/// assert(*Ctx.get(Version) == 3);
38///
39/// Keys are typically used across multiple functions, so most of the time you
40/// would want to make them static class members or global variables.
Ilya Biryukov657159c2017-12-12 11:16:45 +000041template <class Type> class Key {
42public:
43 static_assert(!std::is_reference<Type>::value,
44 "Reference arguments to Key<> are not allowed");
45
Sam McCall599a3832018-06-15 12:39:21 +000046 constexpr Key() = default;
Ilya Biryukov657159c2017-12-12 11:16:45 +000047
48 Key(Key const &) = delete;
49 Key &operator=(Key const &) = delete;
50 Key(Key &&) = delete;
51 Key &operator=(Key &&) = delete;
52};
53
54/// A context is an immutable container for per-request data that must be
55/// propagated through layers that don't care about it. An example is a request
56/// ID that we may want to use when logging.
57///
58/// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has
59/// an associated value type, which allows the map to be typesafe.
60///
Sam McCalld1a7a372018-01-31 13:40:48 +000061/// There is an "ambient" context for each thread, Context::current().
62/// Most functions should read from this, and use WithContextValue or
63/// WithContext to extend or replace the context within a block scope.
64/// Only code dealing with threads and extension points should need to use
65/// other Context objects.
66///
Ilya Biryukov657159c2017-12-12 11:16:45 +000067/// You can't add data to an existing context, instead you create a new
68/// immutable context derived from it with extra data added. When you retrieve
69/// data, the context will walk up the parent chain until the key is found.
Ilya Biryukov657159c2017-12-12 11:16:45 +000070class Context {
71public:
Sam McCalld1a7a372018-01-31 13:40:48 +000072 /// Returns an empty root context that contains no data.
Ilya Biryukov657159c2017-12-12 11:16:45 +000073 static Context empty();
Sam McCalld1a7a372018-01-31 13:40:48 +000074 /// Returns the context for the current thread, creating it if needed.
75 static const Context &current();
76 // Sets the current() context to Replacement, and returns the old context.
77 // Prefer to use WithContext or WithContextValue to do this safely.
78 static Context swapCurrent(Context Replacement);
Ilya Biryukov657159c2017-12-12 11:16:45 +000079
80private:
81 struct Data;
82 Context(std::shared_ptr<const Data> DataPtr);
83
84public:
Ilya Biryukova1d324d2017-12-13 13:43:47 +000085 /// Same as Context::empty(), please use Context::empty() instead.
86 /// Constructor is defined to workaround a bug in MSVC's version of STL.
87 /// (arguments of std::future<> must be default-construcitble in MSVC).
88 Context() = default;
89
Sam McCalld1a7a372018-01-31 13:40:48 +000090 /// Copy operations for this class are deleted, use an explicit clone() method
91 /// when you need a copy of the context instead.
Ilya Biryukov657159c2017-12-12 11:16:45 +000092 Context(Context const &) = delete;
93 Context &operator=(const Context &) = delete;
94
95 Context(Context &&) = default;
96 Context &operator=(Context &&) = default;
97
98 /// Get data stored for a typed \p Key. If values are not found
99 /// \returns Pointer to the data associated with \p Key. If no data is
100 /// specified for \p Key, return null.
101 template <class Type> const Type *get(const Key<Type> &Key) const {
102 for (const Data *DataPtr = this->DataPtr.get(); DataPtr != nullptr;
103 DataPtr = DataPtr->Parent.get()) {
104 if (DataPtr->KeyPtr == &Key)
105 return static_cast<const Type *>(DataPtr->Value->getValuePtr());
106 }
107 return nullptr;
108 }
109
110 /// A helper to get a reference to a \p Key that must exist in the map.
111 /// Must not be called for keys that are not in the map.
112 template <class Type> const Type &getExisting(const Key<Type> &Key) const {
113 auto Val = get(Key);
114 assert(Val && "Key does not exist");
115 return *Val;
116 }
117
118 /// Derives a child context
Sam McCalld1a7a372018-01-31 13:40:48 +0000119 /// It is safe to move or destroy a parent context after calling derive().
120 /// The child will keep its parent alive, and its data remains accessible.
Ilya Biryukov657159c2017-12-12 11:16:45 +0000121 template <class Type>
122 Context derive(const Key<Type> &Key,
123 typename std::decay<Type>::type Value) const & {
124 return Context(std::make_shared<Data>(Data{
125 /*Parent=*/DataPtr, &Key,
126 llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(
127 std::move(Value))}));
128 }
129
130 template <class Type>
131 Context
132 derive(const Key<Type> &Key,
133 typename std::decay<Type>::type Value) && /* takes ownership */ {
134 return Context(std::make_shared<Data>(Data{
135 /*Parent=*/std::move(DataPtr), &Key,
136 llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(
137 std::move(Value))}));
138 }
139
Sam McCall1b475a12018-01-26 09:00:30 +0000140 /// Derives a child context, using an anonymous key.
141 /// Intended for objects stored only for their destructor's side-effect.
142 template <class Type> Context derive(Type &&Value) const & {
143 static Key<typename std::decay<Type>::type> Private;
144 return derive(Private, std::forward<Type>(Value));
145 }
146
147 template <class Type> Context derive(Type &&Value) && {
148 static Key<typename std::decay<Type>::type> Private;
Benjamin Kramer2dee02e2018-04-23 15:27:42 +0000149 return std::move(*this).derive(Private, std::forward<Type>(Value));
Sam McCall1b475a12018-01-26 09:00:30 +0000150 }
151
Ilya Biryukov657159c2017-12-12 11:16:45 +0000152 /// Clone this context object.
153 Context clone() const;
154
155private:
156 class AnyStorage {
157 public:
158 virtual ~AnyStorage() = default;
159 virtual void *getValuePtr() = 0;
160 };
161
162 template <class T> class TypedAnyStorage : public Context::AnyStorage {
163 static_assert(std::is_same<typename std::decay<T>::type, T>::value,
164 "Argument to TypedAnyStorage must be decayed");
165
166 public:
167 TypedAnyStorage(T &&Value) : Value(std::move(Value)) {}
168
169 void *getValuePtr() override { return &Value; }
170
171 private:
172 T Value;
173 };
174
175 struct Data {
176 // We need to make sure Parent outlives the Value, so the order of members
177 // is important. We do that to allow classes stored in Context's child
178 // layers to store references to the data in the parent layers.
179 std::shared_ptr<const Data> Parent;
180 const void *KeyPtr;
181 std::unique_ptr<AnyStorage> Value;
182 };
183
184 std::shared_ptr<const Data> DataPtr;
Sam McCalld1a7a372018-01-31 13:40:48 +0000185};
186
187/// WithContext replaces Context::current() with a provided scope.
188/// When the WithContext is destroyed, the original scope is restored.
189/// For extending the current context with new value, prefer WithContextValue.
190class LLVM_NODISCARD WithContext {
191public:
192 WithContext(Context C) : Restore(Context::swapCurrent(std::move(C))) {}
193 ~WithContext() { Context::swapCurrent(std::move(Restore)); }
194 WithContext(const WithContext &) = delete;
195 WithContext &operator=(const WithContext &) = delete;
196 WithContext(WithContext &&) = delete;
197 WithContext &operator=(WithContext &&) = delete;
198
199private:
200 Context Restore;
201};
202
203/// WithContextValue extends Context::current() with a single value.
204/// When the WithContextValue is destroyed, the original scope is restored.
205class LLVM_NODISCARD WithContextValue {
206public:
207 template <typename T>
208 WithContextValue(const Key<T> &K, typename std::decay<T>::type V)
209 : Restore(Context::current().derive(K, std::move(V))) {}
210
211 // Anonymous values can be used for the destructor side-effect.
212 template <typename T>
213 WithContextValue(T &&V)
214 : Restore(Context::current().derive(std::forward<T>(V))) {}
215
216private:
217 WithContext Restore;
218};
Ilya Biryukov657159c2017-12-12 11:16:45 +0000219
220} // namespace clangd
221} // namespace clang
222
223#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_