blob: 5254b04e11012fe15b04025e641e9090f6f4bbd4 [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"
19#include <memory>
20#include <type_traits>
21
22namespace clang {
23namespace clangd {
24
25/// A key for a value of type \p Type, stored inside a context. Keys are
26/// non-movable and non-copyable. See documentation of the Context class for
27/// more details and usage examples.
28template <class Type> class Key {
29public:
30 static_assert(!std::is_reference<Type>::value,
31 "Reference arguments to Key<> are not allowed");
32
33 Key() = default;
34
35 Key(Key const &) = delete;
36 Key &operator=(Key const &) = delete;
37 Key(Key &&) = delete;
38 Key &operator=(Key &&) = delete;
39};
40
41/// A context is an immutable container for per-request data that must be
42/// propagated through layers that don't care about it. An example is a request
43/// ID that we may want to use when logging.
44///
45/// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has
46/// an associated value type, which allows the map to be typesafe.
47///
48/// You can't add data to an existing context, instead you create a new
49/// immutable context derived from it with extra data added. When you retrieve
50/// data, the context will walk up the parent chain until the key is found.
51///
52/// Contexts should be:
53/// - passed by reference when calling synchronous functions
54/// - passed by value (move) when calling asynchronous functions. The result
55/// callback of async operations will receive the context again.
56/// - cloned only when 'forking' an asynchronous computation that we don't wait
57/// for.
58///
59/// Copy operations for this class are deleted, use an explicit clone() method
60/// when you need a copy of the context instead.
61///
62/// To derive a child context use derive() function, e.g.
63/// Context ChildCtx = ParentCtx.derive(RequestIdKey, 123);
64///
65/// To create a new root context, derive() from empty Context.
66/// e.g.:
67/// Context Ctx = Context::empty().derive(RequestIdKey, 123);
68///
69/// Values in the context are indexed by typed keys (instances of Key<T> class).
70/// Key<T> serves two purposes:
71/// - it provides a lookup key for the context (each instance of a key is
72/// unique),
73/// - it keeps the type information about the value stored in the context map
74/// in the template arguments.
75/// This provides a type-safe interface to store and access values of multiple
76/// types inside a single context.
77/// For example,
78/// Key<int> RequestID;
79/// Key<int> Version;
80///
81/// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3);
82/// assert(*Ctx.get(RequestID) == 10);
83/// assert(*Ctx.get(Version) == 3);
84///
85/// Keys are typically used across multiple functions, so most of the time you
86/// would want to make them static class members or global variables.
Sam McCall1b475a12018-01-26 09:00:30 +000087///
88/// FIXME: Rather than manual plumbing, pass Context using thread-local storage
89/// by default, and make thread boundaries deal with propagation explicitly.
Ilya Biryukov657159c2017-12-12 11:16:45 +000090class Context {
91public:
92 /// Returns an empty context that contains no data. Useful for calling
93 /// functions that require a context when no explicit context is available.
94 static Context empty();
95
96private:
97 struct Data;
98 Context(std::shared_ptr<const Data> DataPtr);
99
100public:
Ilya Biryukova1d324d2017-12-13 13:43:47 +0000101 /// Same as Context::empty(), please use Context::empty() instead.
102 /// Constructor is defined to workaround a bug in MSVC's version of STL.
103 /// (arguments of std::future<> must be default-construcitble in MSVC).
104 Context() = default;
105
Ilya Biryukov657159c2017-12-12 11:16:45 +0000106 /// Move-only.
107 Context(Context const &) = delete;
108 Context &operator=(const Context &) = delete;
109
110 Context(Context &&) = default;
111 Context &operator=(Context &&) = default;
112
113 /// Get data stored for a typed \p Key. If values are not found
114 /// \returns Pointer to the data associated with \p Key. If no data is
115 /// specified for \p Key, return null.
116 template <class Type> const Type *get(const Key<Type> &Key) const {
117 for (const Data *DataPtr = this->DataPtr.get(); DataPtr != nullptr;
118 DataPtr = DataPtr->Parent.get()) {
119 if (DataPtr->KeyPtr == &Key)
120 return static_cast<const Type *>(DataPtr->Value->getValuePtr());
121 }
122 return nullptr;
123 }
124
125 /// A helper to get a reference to a \p Key that must exist in the map.
126 /// Must not be called for keys that are not in the map.
127 template <class Type> const Type &getExisting(const Key<Type> &Key) const {
128 auto Val = get(Key);
129 assert(Val && "Key does not exist");
130 return *Val;
131 }
132
133 /// Derives a child context
134 /// It is safe to move or destroy a parent context after calling derive() from
135 /// it. The child context will continue to have access to the data stored in
136 /// the parent context.
137 template <class Type>
138 Context derive(const Key<Type> &Key,
139 typename std::decay<Type>::type Value) const & {
140 return Context(std::make_shared<Data>(Data{
141 /*Parent=*/DataPtr, &Key,
142 llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(
143 std::move(Value))}));
144 }
145
146 template <class Type>
147 Context
148 derive(const Key<Type> &Key,
149 typename std::decay<Type>::type Value) && /* takes ownership */ {
150 return Context(std::make_shared<Data>(Data{
151 /*Parent=*/std::move(DataPtr), &Key,
152 llvm::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(
153 std::move(Value))}));
154 }
155
Sam McCall1b475a12018-01-26 09:00:30 +0000156 /// Derives a child context, using an anonymous key.
157 /// Intended for objects stored only for their destructor's side-effect.
158 template <class Type> Context derive(Type &&Value) const & {
159 static Key<typename std::decay<Type>::type> Private;
160 return derive(Private, std::forward<Type>(Value));
161 }
162
163 template <class Type> Context derive(Type &&Value) && {
164 static Key<typename std::decay<Type>::type> Private;
165 return std::move(this)->derive(Private, std::forward<Type>(Value));
166 }
167
Ilya Biryukov657159c2017-12-12 11:16:45 +0000168 /// Clone this context object.
169 Context clone() const;
170
171private:
172 class AnyStorage {
173 public:
174 virtual ~AnyStorage() = default;
175 virtual void *getValuePtr() = 0;
176 };
177
178 template <class T> class TypedAnyStorage : public Context::AnyStorage {
179 static_assert(std::is_same<typename std::decay<T>::type, T>::value,
180 "Argument to TypedAnyStorage must be decayed");
181
182 public:
183 TypedAnyStorage(T &&Value) : Value(std::move(Value)) {}
184
185 void *getValuePtr() override { return &Value; }
186
187 private:
188 T Value;
189 };
190
191 struct Data {
192 // We need to make sure Parent outlives the Value, so the order of members
193 // is important. We do that to allow classes stored in Context's child
194 // layers to store references to the data in the parent layers.
195 std::shared_ptr<const Data> Parent;
196 const void *KeyPtr;
197 std::unique_ptr<AnyStorage> Value;
198 };
199
200 std::shared_ptr<const Data> DataPtr;
201}; // namespace clangd
202
203} // namespace clangd
204} // namespace clang
205
206#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONTEXT_H_