ErrorOr.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/ErrorOr.h - Error Smart Pointer -------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. ///
  16. /// Provides ErrorOr<T> smart pointer.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_ERROROR_H
  20. #define LLVM_SUPPORT_ERROROR_H
  21. #include "llvm/Support/AlignOf.h"
  22. #include <cassert>
  23. #include <system_error>
  24. #include <type_traits>
  25. #include <utility>
  26. namespace llvm {
  27. /// Represents either an error or a value T.
  28. ///
  29. /// ErrorOr<T> is a pointer-like class that represents the result of an
  30. /// operation. The result is either an error, or a value of type T. This is
  31. /// designed to emulate the usage of returning a pointer where nullptr indicates
  32. /// failure. However instead of just knowing that the operation failed, we also
  33. /// have an error_code and optional user data that describes why it failed.
  34. ///
  35. /// It is used like the following.
  36. /// \code
  37. /// ErrorOr<Buffer> getBuffer();
  38. ///
  39. /// auto buffer = getBuffer();
  40. /// if (error_code ec = buffer.getError())
  41. /// return ec;
  42. /// buffer->write("adena");
  43. /// \endcode
  44. ///
  45. ///
  46. /// Implicit conversion to bool returns true if there is a usable value. The
  47. /// unary * and -> operators provide pointer like access to the value. Accessing
  48. /// the value when there is an error has undefined behavior.
  49. ///
  50. /// When T is a reference type the behavior is slightly different. The reference
  51. /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
  52. /// there is special handling to make operator -> work as if T was not a
  53. /// reference.
  54. ///
  55. /// T cannot be a rvalue reference.
  56. template<class T>
  57. class ErrorOr {
  58. template <class OtherT> friend class ErrorOr;
  59. static constexpr bool isRef = std::is_reference<T>::value;
  60. using wrap = std::reference_wrapper<std::remove_reference_t<T>>;
  61. public:
  62. using storage_type = std::conditional_t<isRef, wrap, T>;
  63. private:
  64. using reference = std::remove_reference_t<T> &;
  65. using const_reference = const std::remove_reference_t<T> &;
  66. using pointer = std::remove_reference_t<T> *;
  67. using const_pointer = const std::remove_reference_t<T> *;
  68. public:
  69. template <class E>
  70. ErrorOr(E ErrorCode,
  71. std::enable_if_t<std::is_error_code_enum<E>::value ||
  72. std::is_error_condition_enum<E>::value,
  73. void *> = nullptr)
  74. : HasError(true) {
  75. new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
  76. }
  77. ErrorOr(std::error_code EC) : HasError(true) {
  78. new (getErrorStorage()) std::error_code(EC);
  79. }
  80. template <class OtherT>
  81. ErrorOr(OtherT &&Val,
  82. std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
  83. : HasError(false) {
  84. new (getStorage()) storage_type(std::forward<OtherT>(Val));
  85. }
  86. ErrorOr(const ErrorOr &Other) {
  87. copyConstruct(Other);
  88. }
  89. template <class OtherT>
  90. ErrorOr(const ErrorOr<OtherT> &Other,
  91. std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) {
  92. copyConstruct(Other);
  93. }
  94. template <class OtherT>
  95. explicit ErrorOr(
  96. const ErrorOr<OtherT> &Other,
  97. std::enable_if_t<!std::is_convertible<OtherT, const T &>::value> * =
  98. nullptr) {
  99. copyConstruct(Other);
  100. }
  101. ErrorOr(ErrorOr &&Other) {
  102. moveConstruct(std::move(Other));
  103. }
  104. template <class OtherT>
  105. ErrorOr(ErrorOr<OtherT> &&Other,
  106. std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) {
  107. moveConstruct(std::move(Other));
  108. }
  109. // This might eventually need SFINAE but it's more complex than is_convertible
  110. // & I'm too lazy to write it right now.
  111. template <class OtherT>
  112. explicit ErrorOr(
  113. ErrorOr<OtherT> &&Other,
  114. std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) {
  115. moveConstruct(std::move(Other));
  116. }
  117. ErrorOr &operator=(const ErrorOr &Other) {
  118. copyAssign(Other);
  119. return *this;
  120. }
  121. ErrorOr &operator=(ErrorOr &&Other) {
  122. moveAssign(std::move(Other));
  123. return *this;
  124. }
  125. ~ErrorOr() {
  126. if (!HasError)
  127. getStorage()->~storage_type();
  128. }
  129. /// Return false if there is an error.
  130. explicit operator bool() const {
  131. return !HasError;
  132. }
  133. reference get() { return *getStorage(); }
  134. const_reference get() const { return const_cast<ErrorOr<T> *>(this)->get(); }
  135. std::error_code getError() const {
  136. return HasError ? *getErrorStorage() : std::error_code();
  137. }
  138. pointer operator ->() {
  139. return toPointer(getStorage());
  140. }
  141. const_pointer operator->() const { return toPointer(getStorage()); }
  142. reference operator *() {
  143. return *getStorage();
  144. }
  145. const_reference operator*() const { return *getStorage(); }
  146. private:
  147. template <class OtherT>
  148. void copyConstruct(const ErrorOr<OtherT> &Other) {
  149. if (!Other.HasError) {
  150. // Get the other value.
  151. HasError = false;
  152. new (getStorage()) storage_type(*Other.getStorage());
  153. } else {
  154. // Get other's error.
  155. HasError = true;
  156. new (getErrorStorage()) std::error_code(Other.getError());
  157. }
  158. }
  159. template <class T1>
  160. static bool compareThisIfSameType(const T1 &a, const T1 &b) {
  161. return &a == &b;
  162. }
  163. template <class T1, class T2>
  164. static bool compareThisIfSameType(const T1 &a, const T2 &b) {
  165. return false;
  166. }
  167. template <class OtherT>
  168. void copyAssign(const ErrorOr<OtherT> &Other) {
  169. if (compareThisIfSameType(*this, Other))
  170. return;
  171. this->~ErrorOr();
  172. new (this) ErrorOr(Other);
  173. }
  174. template <class OtherT>
  175. void moveConstruct(ErrorOr<OtherT> &&Other) {
  176. if (!Other.HasError) {
  177. // Get the other value.
  178. HasError = false;
  179. new (getStorage()) storage_type(std::move(*Other.getStorage()));
  180. } else {
  181. // Get other's error.
  182. HasError = true;
  183. new (getErrorStorage()) std::error_code(Other.getError());
  184. }
  185. }
  186. template <class OtherT>
  187. void moveAssign(ErrorOr<OtherT> &&Other) {
  188. if (compareThisIfSameType(*this, Other))
  189. return;
  190. this->~ErrorOr();
  191. new (this) ErrorOr(std::move(Other));
  192. }
  193. pointer toPointer(pointer Val) {
  194. return Val;
  195. }
  196. const_pointer toPointer(const_pointer Val) const { return Val; }
  197. pointer toPointer(wrap *Val) {
  198. return &Val->get();
  199. }
  200. const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
  201. storage_type *getStorage() {
  202. assert(!HasError && "Cannot get value when an error exists!");
  203. return reinterpret_cast<storage_type *>(&TStorage);
  204. }
  205. const storage_type *getStorage() const {
  206. assert(!HasError && "Cannot get value when an error exists!");
  207. return reinterpret_cast<const storage_type *>(&TStorage);
  208. }
  209. std::error_code *getErrorStorage() {
  210. assert(HasError && "Cannot get error when a value exists!");
  211. return reinterpret_cast<std::error_code *>(&ErrorStorage);
  212. }
  213. const std::error_code *getErrorStorage() const {
  214. return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
  215. }
  216. union {
  217. AlignedCharArrayUnion<storage_type> TStorage;
  218. AlignedCharArrayUnion<std::error_code> ErrorStorage;
  219. };
  220. bool HasError : 1;
  221. };
  222. template <class T, class E>
  223. std::enable_if_t<std::is_error_code_enum<E>::value ||
  224. std::is_error_condition_enum<E>::value,
  225. bool>
  226. operator==(const ErrorOr<T> &Err, E Code) {
  227. return Err.getError() == Code;
  228. }
  229. } // end namespace llvm
  230. #endif // LLVM_SUPPORT_ERROROR_H
  231. #ifdef __GNUC__
  232. #pragma GCC diagnostic pop
  233. #endif