Home | History | Annotate | Download | only in Support
      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 <cassert>
     22 #include <system_error>
     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     new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
    103   }
    104 
    105   ErrorOr(std::error_code EC) : HasError(true) {
    106     new (getErrorStorage()) std::error_code(EC);
    107   }
    108 
    109   ErrorOr(T Val) : HasError(false) {
    110     new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
    111   }
    112 
    113   ErrorOr(const ErrorOr &Other) {
    114     copyConstruct(Other);
    115   }
    116 
    117   template <class OtherT>
    118   ErrorOr(const ErrorOr<OtherT> &Other) {
    119     copyConstruct(Other);
    120   }
    121 
    122   ErrorOr &operator =(const ErrorOr &Other) {
    123     copyAssign(Other);
    124     return *this;
    125   }
    126 
    127   template <class OtherT>
    128   ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
    129     copyAssign(Other);
    130     return *this;
    131   }
    132 
    133   ErrorOr(ErrorOr &&Other) {
    134     moveConstruct(std::move(Other));
    135   }
    136 
    137   template <class OtherT>
    138   ErrorOr(ErrorOr<OtherT> &&Other) {
    139     moveConstruct(std::move(Other));
    140   }
    141 
    142   ErrorOr &operator =(ErrorOr &&Other) {
    143     moveAssign(std::move(Other));
    144     return *this;
    145   }
    146 
    147   template <class OtherT>
    148   ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
    149     moveAssign(std::move(Other));
    150     return *this;
    151   }
    152 
    153   ~ErrorOr() {
    154     if (!HasError)
    155       getStorage()->~storage_type();
    156   }
    157 
    158   /// \brief Return false if there is an error.
    159   LLVM_EXPLICIT operator bool() const {
    160     return !HasError;
    161   }
    162 
    163   reference get() { return *getStorage(); }
    164   const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
    165 
    166   std::error_code getError() const {
    167     return HasError ? *getErrorStorage() : std::error_code();
    168   }
    169 
    170   pointer operator ->() {
    171     return toPointer(getStorage());
    172   }
    173 
    174   reference operator *() {
    175     return *getStorage();
    176   }
    177 
    178 private:
    179   template <class OtherT>
    180   void copyConstruct(const ErrorOr<OtherT> &Other) {
    181     if (!Other.HasError) {
    182       // Get the other value.
    183       HasError = false;
    184       new (getStorage()) storage_type(*Other.getStorage());
    185     } else {
    186       // Get other's error.
    187       HasError = true;
    188       new (getErrorStorage()) std::error_code(Other.getError());
    189     }
    190   }
    191 
    192   template <class T1>
    193   static bool compareThisIfSameType(const T1 &a, const T1 &b) {
    194     return &a == &b;
    195   }
    196 
    197   template <class T1, class T2>
    198   static bool compareThisIfSameType(const T1 &a, const T2 &b) {
    199     return false;
    200   }
    201 
    202   template <class OtherT>
    203   void copyAssign(const ErrorOr<OtherT> &Other) {
    204     if (compareThisIfSameType(*this, Other))
    205       return;
    206 
    207     this->~ErrorOr();
    208     new (this) ErrorOr(Other);
    209   }
    210 
    211   template <class OtherT>
    212   void moveConstruct(ErrorOr<OtherT> &&Other) {
    213     if (!Other.HasError) {
    214       // Get the other value.
    215       HasError = false;
    216       new (getStorage()) storage_type(std::move(*Other.getStorage()));
    217     } else {
    218       // Get other's error.
    219       HasError = true;
    220       new (getErrorStorage()) std::error_code(Other.getError());
    221     }
    222   }
    223 
    224   template <class OtherT>
    225   void moveAssign(ErrorOr<OtherT> &&Other) {
    226     if (compareThisIfSameType(*this, Other))
    227       return;
    228 
    229     this->~ErrorOr();
    230     new (this) ErrorOr(std::move(Other));
    231   }
    232 
    233   pointer toPointer(pointer Val) {
    234     return Val;
    235   }
    236 
    237   pointer toPointer(wrap *Val) {
    238     return &Val->get();
    239   }
    240 
    241   storage_type *getStorage() {
    242     assert(!HasError && "Cannot get value when an error exists!");
    243     return reinterpret_cast<storage_type*>(TStorage.buffer);
    244   }
    245 
    246   const storage_type *getStorage() const {
    247     assert(!HasError && "Cannot get value when an error exists!");
    248     return reinterpret_cast<const storage_type*>(TStorage.buffer);
    249   }
    250 
    251   std::error_code *getErrorStorage() {
    252     assert(HasError && "Cannot get error when a value exists!");
    253     return reinterpret_cast<std::error_code *>(ErrorStorage.buffer);
    254   }
    255 
    256   const std::error_code *getErrorStorage() const {
    257     return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
    258   }
    259 
    260 
    261   union {
    262     AlignedCharArrayUnion<storage_type> TStorage;
    263     AlignedCharArrayUnion<std::error_code> ErrorStorage;
    264   };
    265   bool HasError : 1;
    266 };
    267 
    268 template <class T, class E>
    269 typename std::enable_if<std::is_error_code_enum<E>::value ||
    270                             std::is_error_condition_enum<E>::value,
    271                         bool>::type
    272 operator==(ErrorOr<T> &Err, E Code) {
    273   return std::error_code(Err) == Code;
    274 }
    275 } // end namespace llvm
    276 
    277 #endif
    278