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