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