2013-01-20 20:32:30 +00:00
|
|
|
//===- llvm/Support/ErrorOr.h - Error Smart Pointer -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// Provides ErrorOr<T> smart pointer.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 16:26:38 +00:00
|
|
|
#ifndef LLVM_SUPPORT_ERROROR_H
|
|
|
|
#define LLVM_SUPPORT_ERROROR_H
|
2013-01-20 20:32:30 +00:00
|
|
|
|
2013-01-23 00:18:31 +00:00
|
|
|
#include "llvm/ADT/PointerIntPair.h"
|
2013-01-20 20:32:30 +00:00
|
|
|
#include "llvm/Support/AlignOf.h"
|
|
|
|
#include <cassert>
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2013-01-20 20:32:30 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<class T, class V>
|
|
|
|
typename std::enable_if< std::is_constructible<T, V>::value
|
|
|
|
, typename std::remove_reference<V>::type>::type &&
|
|
|
|
moveIfMoveConstructible(V &Val) {
|
|
|
|
return std::move(Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class V>
|
|
|
|
typename std::enable_if< !std::is_constructible<T, V>::value
|
|
|
|
, typename std::remove_reference<V>::type>::type &
|
|
|
|
moveIfMoveConstructible(V &Val) {
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Stores a reference that can be changed.
|
|
|
|
template <typename T>
|
|
|
|
class ReferenceStorage {
|
|
|
|
T *Storage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReferenceStorage(T &Ref) : Storage(&Ref) {}
|
|
|
|
|
|
|
|
operator T &() const { return *Storage; }
|
|
|
|
T &get() const { return *Storage; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Represents either an error or a value T.
|
|
|
|
///
|
|
|
|
/// ErrorOr<T> is a pointer-like class that represents the result of an
|
|
|
|
/// operation. The result is either an error, or a value of type T. This is
|
|
|
|
/// designed to emulate the usage of returning a pointer where nullptr indicates
|
|
|
|
/// failure. However instead of just knowing that the operation failed, we also
|
|
|
|
/// have an error_code and optional user data that describes why it failed.
|
|
|
|
///
|
|
|
|
/// It is used like the following.
|
|
|
|
/// \code
|
|
|
|
/// ErrorOr<Buffer> getBuffer();
|
|
|
|
///
|
|
|
|
/// auto buffer = getBuffer();
|
2014-01-09 14:40:43 +00:00
|
|
|
/// if (error_code ec = buffer.getError())
|
|
|
|
/// return ec;
|
2013-01-20 20:32:30 +00:00
|
|
|
/// buffer->write("adena");
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
///
|
2014-11-04 21:01:48 +00:00
|
|
|
/// Implicit conversion to bool returns true if there is a usable value. The
|
|
|
|
/// unary * and -> operators provide pointer like access to the value. Accessing
|
|
|
|
/// the value when there is an error has undefined behavior.
|
2013-01-20 20:32:30 +00:00
|
|
|
///
|
|
|
|
/// When T is a reference type the behaivor is slightly different. The reference
|
|
|
|
/// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
|
|
|
|
/// there is special handling to make operator -> work as if T was not a
|
|
|
|
/// reference.
|
|
|
|
///
|
|
|
|
/// T cannot be a rvalue reference.
|
|
|
|
template<class T>
|
|
|
|
class ErrorOr {
|
2013-02-06 22:28:53 +00:00
|
|
|
template <class OtherT> friend class ErrorOr;
|
2014-03-07 14:42:25 +00:00
|
|
|
static const bool isRef = std::is_reference<T>::value;
|
|
|
|
typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
|
2013-01-20 20:32:30 +00:00
|
|
|
|
|
|
|
public:
|
2014-03-07 14:42:25 +00:00
|
|
|
typedef typename std::conditional<isRef, wrap, T>::type storage_type;
|
2013-01-20 20:32:30 +00:00
|
|
|
|
|
|
|
private:
|
2014-03-07 14:42:25 +00:00
|
|
|
typedef typename std::remove_reference<T>::type &reference;
|
|
|
|
typedef const typename std::remove_reference<T>::type &const_reference;
|
|
|
|
typedef typename std::remove_reference<T>::type *pointer;
|
2013-01-20 20:32:30 +00:00
|
|
|
|
|
|
|
public:
|
2013-02-28 01:44:26 +00:00
|
|
|
template <class E>
|
2014-06-12 01:10:47 +00:00
|
|
|
ErrorOr(E ErrorCode,
|
|
|
|
typename std::enable_if<std::is_error_code_enum<E>::value ||
|
|
|
|
std::is_error_condition_enum<E>::value,
|
|
|
|
void *>::type = 0)
|
2013-11-05 23:41:57 +00:00
|
|
|
: HasError(true) {
|
2014-06-12 21:46:39 +00:00
|
|
|
new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
|
2013-02-28 01:44:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 14:11:22 +00:00
|
|
|
ErrorOr(std::error_code EC) : HasError(true) {
|
2014-06-12 21:46:39 +00:00
|
|
|
new (getErrorStorage()) std::error_code(EC);
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 23:41:57 +00:00
|
|
|
ErrorOr(T Val) : HasError(false) {
|
2014-01-08 17:43:26 +00:00
|
|
|
new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 23:41:57 +00:00
|
|
|
ErrorOr(const ErrorOr &Other) {
|
2013-02-06 22:28:53 +00:00
|
|
|
copyConstruct(Other);
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 22:28:53 +00:00
|
|
|
template <class OtherT>
|
2014-09-03 17:31:25 +00:00
|
|
|
ErrorOr(
|
|
|
|
const ErrorOr<OtherT> &Other,
|
|
|
|
typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
|
|
|
|
nullptr) {
|
2013-02-06 22:28:53 +00:00
|
|
|
copyConstruct(Other);
|
|
|
|
}
|
2013-01-20 20:32:30 +00:00
|
|
|
|
2013-02-06 22:28:53 +00:00
|
|
|
template <class OtherT>
|
2014-09-03 17:31:25 +00:00
|
|
|
explicit ErrorOr(
|
|
|
|
const ErrorOr<OtherT> &Other,
|
|
|
|
typename std::enable_if<
|
|
|
|
!std::is_convertible<OtherT, const T &>::value>::type * = nullptr) {
|
|
|
|
copyConstruct(Other);
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 23:41:57 +00:00
|
|
|
ErrorOr(ErrorOr &&Other) {
|
2013-02-06 22:28:53 +00:00
|
|
|
moveConstruct(std::move(Other));
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 22:28:53 +00:00
|
|
|
template <class OtherT>
|
2014-09-03 17:31:25 +00:00
|
|
|
ErrorOr(
|
|
|
|
ErrorOr<OtherT> &&Other,
|
|
|
|
typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
|
|
|
|
nullptr) {
|
2013-02-06 22:28:53 +00:00
|
|
|
moveConstruct(std::move(Other));
|
|
|
|
}
|
2013-01-20 20:32:30 +00:00
|
|
|
|
2014-09-03 17:31:25 +00:00
|
|
|
// This might eventually need SFINAE but it's more complex than is_convertible
|
|
|
|
// & I'm too lazy to write it right now.
|
|
|
|
template <class OtherT>
|
|
|
|
explicit ErrorOr(
|
|
|
|
ErrorOr<OtherT> &&Other,
|
|
|
|
typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
|
|
|
|
nullptr) {
|
|
|
|
moveConstruct(std::move(Other));
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr &operator=(const ErrorOr &Other) {
|
|
|
|
copyAssign(Other);
|
2013-02-06 22:28:53 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-01-20 20:32:30 +00:00
|
|
|
|
2014-09-03 17:31:25 +00:00
|
|
|
ErrorOr &operator=(ErrorOr &&Other) {
|
2013-02-06 22:28:53 +00:00
|
|
|
moveAssign(std::move(Other));
|
2013-01-20 20:32:30 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ErrorOr() {
|
2013-11-05 00:28:01 +00:00
|
|
|
if (!HasError)
|
2014-01-08 17:43:26 +00:00
|
|
|
getStorage()->~storage_type();
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Return false if there is an error.
|
2014-01-16 23:37:23 +00:00
|
|
|
LLVM_EXPLICIT operator bool() const {
|
|
|
|
return !HasError;
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 00:25:25 +00:00
|
|
|
reference get() { return *getStorage(); }
|
2014-10-20 20:41:21 +00:00
|
|
|
const_reference get() const { return const_cast<ErrorOr<T> *>(this)->get(); }
|
2014-01-08 21:17:09 +00:00
|
|
|
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code getError() const {
|
|
|
|
return HasError ? *getErrorStorage() : std::error_code();
|
2014-01-08 21:17:09 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 20:32:30 +00:00
|
|
|
pointer operator ->() {
|
2014-01-08 17:43:26 +00:00
|
|
|
return toPointer(getStorage());
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reference operator *() {
|
2014-01-08 17:43:26 +00:00
|
|
|
return *getStorage();
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-02-06 22:28:53 +00:00
|
|
|
template <class OtherT>
|
|
|
|
void copyConstruct(const ErrorOr<OtherT> &Other) {
|
|
|
|
if (!Other.HasError) {
|
|
|
|
// Get the other value.
|
|
|
|
HasError = false;
|
2014-01-08 17:56:46 +00:00
|
|
|
new (getStorage()) storage_type(*Other.getStorage());
|
2013-02-06 22:28:53 +00:00
|
|
|
} else {
|
|
|
|
// Get other's error.
|
|
|
|
HasError = true;
|
2014-06-12 21:46:39 +00:00
|
|
|
new (getErrorStorage()) std::error_code(Other.getError());
|
2013-02-06 22:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T1>
|
|
|
|
static bool compareThisIfSameType(const T1 &a, const T1 &b) {
|
|
|
|
return &a == &b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T1, class T2>
|
|
|
|
static bool compareThisIfSameType(const T1 &a, const T2 &b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherT>
|
|
|
|
void copyAssign(const ErrorOr<OtherT> &Other) {
|
|
|
|
if (compareThisIfSameType(*this, Other))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->~ErrorOr();
|
|
|
|
new (this) ErrorOr(Other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherT>
|
|
|
|
void moveConstruct(ErrorOr<OtherT> &&Other) {
|
|
|
|
if (!Other.HasError) {
|
|
|
|
// Get the other value.
|
|
|
|
HasError = false;
|
2014-01-08 17:43:26 +00:00
|
|
|
new (getStorage()) storage_type(std::move(*Other.getStorage()));
|
2013-02-06 22:28:53 +00:00
|
|
|
} else {
|
|
|
|
// Get other's error.
|
|
|
|
HasError = true;
|
2014-06-12 21:46:39 +00:00
|
|
|
new (getErrorStorage()) std::error_code(Other.getError());
|
2013-02-06 22:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OtherT>
|
|
|
|
void moveAssign(ErrorOr<OtherT> &&Other) {
|
|
|
|
if (compareThisIfSameType(*this, Other))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->~ErrorOr();
|
|
|
|
new (this) ErrorOr(std::move(Other));
|
|
|
|
}
|
|
|
|
|
2013-01-20 20:32:30 +00:00
|
|
|
pointer toPointer(pointer Val) {
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer toPointer(wrap *Val) {
|
|
|
|
return &Val->get();
|
|
|
|
}
|
|
|
|
|
2014-01-08 17:43:26 +00:00
|
|
|
storage_type *getStorage() {
|
2013-01-20 20:32:30 +00:00
|
|
|
assert(!HasError && "Cannot get value when an error exists!");
|
|
|
|
return reinterpret_cast<storage_type*>(TStorage.buffer);
|
|
|
|
}
|
|
|
|
|
2014-01-08 17:43:26 +00:00
|
|
|
const storage_type *getStorage() const {
|
2013-01-20 20:32:30 +00:00
|
|
|
assert(!HasError && "Cannot get value when an error exists!");
|
|
|
|
return reinterpret_cast<const storage_type*>(TStorage.buffer);
|
|
|
|
}
|
|
|
|
|
2014-06-12 21:46:39 +00:00
|
|
|
std::error_code *getErrorStorage() {
|
2013-11-05 01:07:06 +00:00
|
|
|
assert(HasError && "Cannot get error when a value exists!");
|
2014-06-12 21:46:39 +00:00
|
|
|
return reinterpret_cast<std::error_code *>(ErrorStorage.buffer);
|
2013-11-05 01:07:06 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 21:46:39 +00:00
|
|
|
const std::error_code *getErrorStorage() const {
|
2014-01-08 17:43:26 +00:00
|
|
|
return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
|
2013-11-05 01:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-20 20:32:30 +00:00
|
|
|
union {
|
|
|
|
AlignedCharArrayUnion<storage_type> TStorage;
|
2014-06-12 21:46:39 +00:00
|
|
|
AlignedCharArrayUnion<std::error_code> ErrorStorage;
|
2013-01-20 20:32:30 +00:00
|
|
|
};
|
|
|
|
bool HasError : 1;
|
|
|
|
};
|
|
|
|
|
2014-06-12 01:10:47 +00:00
|
|
|
template <class T, class E>
|
|
|
|
typename std::enable_if<std::is_error_code_enum<E>::value ||
|
|
|
|
std::is_error_condition_enum<E>::value,
|
|
|
|
bool>::type
|
|
|
|
operator==(ErrorOr<T> &Err, E Code) {
|
2014-06-12 21:46:39 +00:00
|
|
|
return std::error_code(Err) == Code;
|
2013-01-20 20:32:30 +00:00
|
|
|
}
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|