d2a316fbf407d3021fa53bf62dc01d5b75db6547
[oota-llvm.git] / include / llvm / Support / ErrorOr.h
1 //===- llvm/Support/ErrorOr.h - Error Smart Pointer -----------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 ///
12 /// Provides ErrorOr<T> smart pointer.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_ERROR_OR_H
17 #define LLVM_SUPPORT_ERROR_OR_H
18
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/Support/AlignOf.h"
21 #include "llvm/Support/system_error.h"
22 #include <cassert>
23 #include <type_traits>
24
25 namespace llvm {
26 template<class T, class V>
27 typename std::enable_if< std::is_constructible<T, V>::value
28                        , typename std::remove_reference<V>::type>::type &&
29  moveIfMoveConstructible(V &Val) {
30   return std::move(Val);
31 }
32
33 template<class T, class V>
34 typename std::enable_if< !std::is_constructible<T, V>::value
35                        , typename std::remove_reference<V>::type>::type &
36 moveIfMoveConstructible(V &Val) {
37   return Val;
38 }
39
40 /// \brief Stores a reference that can be changed.
41 template <typename T>
42 class ReferenceStorage {
43   T *Storage;
44
45 public:
46   ReferenceStorage(T &Ref) : Storage(&Ref) {}
47
48   operator T &() const { return *Storage; }
49   T &get() const { return *Storage; }
50 };
51
52 /// \brief Represents either an error or a value T.
53 ///
54 /// ErrorOr<T> is a pointer-like class that represents the result of an
55 /// operation. The result is either an error, or a value of type T. This is
56 /// designed to emulate the usage of returning a pointer where nullptr indicates
57 /// failure. However instead of just knowing that the operation failed, we also
58 /// have an error_code and optional user data that describes why it failed.
59 ///
60 /// It is used like the following.
61 /// \code
62 ///   ErrorOr<Buffer> getBuffer();
63 ///
64 ///   auto buffer = getBuffer();
65 ///   if (error_code ec = buffer.getError())
66 ///     return ec;
67 ///   buffer->write("adena");
68 /// \endcode
69 ///
70 ///
71 /// An implicit conversion to bool provides a way to check if there was an
72 /// error. The unary * and -> operators provide pointer like access to the
73 /// value. Accessing the value when there is an error has undefined behavior.
74 ///
75 /// When T is a reference type the behaivor is slightly different. The reference
76 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
77 /// there is special handling to make operator -> work as if T was not a
78 /// reference.
79 ///
80 /// T cannot be a rvalue reference.
81 template<class T>
82 class ErrorOr {
83   template <class OtherT> friend class ErrorOr;
84   static const bool isRef = std::is_reference<T>::value;
85   typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
86
87 public:
88   typedef typename std::conditional<isRef, wrap, T>::type storage_type;
89
90 private:
91   typedef typename std::remove_reference<T>::type &reference;
92   typedef const typename std::remove_reference<T>::type &const_reference;
93   typedef typename std::remove_reference<T>::type *pointer;
94
95 public:
96   template <class E>
97   ErrorOr(E ErrorCode,
98           typename std::enable_if<std::is_error_code_enum<E>::value ||
99                                       std::is_error_condition_enum<E>::value,
100                                   void *>::type = 0)
101       : HasError(true) {
102     using std::make_error_code;
103     new (getErrorStorage()) error_code(make_error_code(ErrorCode));
104   }
105
106   ErrorOr(llvm::error_code EC) : HasError(true) {
107     new (getErrorStorage()) error_code(EC);
108   }
109
110   ErrorOr(T Val) : HasError(false) {
111     new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
112   }
113
114   ErrorOr(const ErrorOr &Other) {
115     copyConstruct(Other);
116   }
117
118   template <class OtherT>
119   ErrorOr(const ErrorOr<OtherT> &Other) {
120     copyConstruct(Other);
121   }
122
123   ErrorOr &operator =(const ErrorOr &Other) {
124     copyAssign(Other);
125     return *this;
126   }
127
128   template <class OtherT>
129   ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
130     copyAssign(Other);
131     return *this;
132   }
133
134   ErrorOr(ErrorOr &&Other) {
135     moveConstruct(std::move(Other));
136   }
137
138   template <class OtherT>
139   ErrorOr(ErrorOr<OtherT> &&Other) {
140     moveConstruct(std::move(Other));
141   }
142
143   ErrorOr &operator =(ErrorOr &&Other) {
144     moveAssign(std::move(Other));
145     return *this;
146   }
147
148   template <class OtherT>
149   ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
150     moveAssign(std::move(Other));
151     return *this;
152   }
153
154   ~ErrorOr() {
155     if (!HasError)
156       getStorage()->~storage_type();
157   }
158
159   /// \brief Return false if there is an error.
160   LLVM_EXPLICIT operator bool() const {
161     return !HasError;
162   }
163
164   reference get() { return *getStorage(); }
165   const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
166
167   error_code getError() const {
168     return HasError ? *getErrorStorage() : error_code();
169   }
170
171   pointer operator ->() {
172     return toPointer(getStorage());
173   }
174
175   reference operator *() {
176     return *getStorage();
177   }
178
179 private:
180   template <class OtherT>
181   void copyConstruct(const ErrorOr<OtherT> &Other) {
182     if (!Other.HasError) {
183       // Get the other value.
184       HasError = false;
185       new (getStorage()) storage_type(*Other.getStorage());
186     } else {
187       // Get other's error.
188       HasError = true;
189       new (getErrorStorage()) error_code(Other.getError());
190     }
191   }
192
193   template <class T1>
194   static bool compareThisIfSameType(const T1 &a, const T1 &b) {
195     return &a == &b;
196   }
197
198   template <class T1, class T2>
199   static bool compareThisIfSameType(const T1 &a, const T2 &b) {
200     return false;
201   }
202
203   template <class OtherT>
204   void copyAssign(const ErrorOr<OtherT> &Other) {
205     if (compareThisIfSameType(*this, Other))
206       return;
207
208     this->~ErrorOr();
209     new (this) ErrorOr(Other);
210   }
211
212   template <class OtherT>
213   void moveConstruct(ErrorOr<OtherT> &&Other) {
214     if (!Other.HasError) {
215       // Get the other value.
216       HasError = false;
217       new (getStorage()) storage_type(std::move(*Other.getStorage()));
218     } else {
219       // Get other's error.
220       HasError = true;
221       new (getErrorStorage()) error_code(Other.getError());
222     }
223   }
224
225   template <class OtherT>
226   void moveAssign(ErrorOr<OtherT> &&Other) {
227     if (compareThisIfSameType(*this, Other))
228       return;
229
230     this->~ErrorOr();
231     new (this) ErrorOr(std::move(Other));
232   }
233
234   pointer toPointer(pointer Val) {
235     return Val;
236   }
237
238   pointer toPointer(wrap *Val) {
239     return &Val->get();
240   }
241
242   storage_type *getStorage() {
243     assert(!HasError && "Cannot get value when an error exists!");
244     return reinterpret_cast<storage_type*>(TStorage.buffer);
245   }
246
247   const storage_type *getStorage() const {
248     assert(!HasError && "Cannot get value when an error exists!");
249     return reinterpret_cast<const storage_type*>(TStorage.buffer);
250   }
251
252   error_code *getErrorStorage() {
253     assert(HasError && "Cannot get error when a value exists!");
254     return reinterpret_cast<error_code*>(ErrorStorage.buffer);
255   }
256
257   const error_code *getErrorStorage() const {
258     return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
259   }
260
261
262   union {
263     AlignedCharArrayUnion<storage_type> TStorage;
264     AlignedCharArrayUnion<error_code> ErrorStorage;
265   };
266   bool HasError : 1;
267 };
268
269 template <class T, class E>
270 typename std::enable_if<std::is_error_code_enum<E>::value ||
271                             std::is_error_condition_enum<E>::value,
272                         bool>::type
273 operator==(ErrorOr<T> &Err, E Code) {
274   return error_code(Err) == Code;
275 }
276 } // end namespace llvm
277
278 #endif