14 #ifndef LLVM_SUPPORT_ERROR_H
15 #define LLVM_SUPPORT_ERROR_H
21 #include "llvm/Config/abi-breaking.h"
36 #include <system_error>
37 #include <type_traits>
69 static const void *
classID() {
return &ID; }
76 virtual bool isA(
const void *
const ClassID)
const {
81 template <
typename ErrorInfoT>
bool isA()
const {
82 return isA(ErrorInfoT::classID());
86 virtual void anchor();
162 template <
typename... HandlerTs>
188 *
this = std::move(
Other);
193 Error(std::unique_ptr<ErrorInfoBase> Payload) {
194 setPtr(Payload.release());
208 setPtr(
Other.getPtr());
214 Other.setPtr(
nullptr);
215 Other.setChecked(
true);
231 setChecked(
getPtr() ==
nullptr);
232 return getPtr() !=
nullptr;
236 template <
typename ErrT>
bool isA()
const {
245 return getPtr()->dynamicClassID();
249 void assertIsChecked() {
250 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
251 if (!getChecked() ||
getPtr()) {
252 dbgs() <<
"Program aborted due to an unhandled Error:\n";
257 <<
"Error value was Success. (Note: Success values must still be "
258 "checked prior to being destroyed).\n";
264 ErrorInfoBase *
getPtr()
const {
265 return reinterpret_cast<ErrorInfoBase*
>(
266 reinterpret_cast<uintptr_t
>(Payload) &
267 ~static_cast<uintptr_t>(0x1));
270 void setPtr(ErrorInfoBase *EI) {
271 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
272 Payload =
reinterpret_cast<ErrorInfoBase*
>(
273 (
reinterpret_cast<uintptr_t
>(EI) &
274 ~static_cast<uintptr_t>(0x1)) |
275 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
281 bool getChecked()
const {
282 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
283 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
289 void setChecked(
bool V) {
290 Payload =
reinterpret_cast<ErrorInfoBase*
>(
291 (
reinterpret_cast<uintptr_t
>(Payload) &
292 ~static_cast<uintptr_t>(0x1)) |
296 std::unique_ptr<ErrorInfoBase> takePayload() {
297 std::unique_ptr<ErrorInfoBase> Tmp(
getPtr());
303 ErrorInfoBase *Payload =
nullptr;
316 return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(
Args)...));
328 template <
typename ThisErrT,
typename ParentErrT = ErrorInfoBase>
335 bool isA(
const void *
const ClassID)
const override {
336 return ClassID ==
classID() || ParentErrT::isA(ClassID);
345 template <
typename... HandlerTs>
353 OS <<
"Multiple errors:\n";
354 for (
auto &ErrPayload : Payloads) {
366 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
367 std::unique_ptr<ErrorInfoBase> Payload2) {
369 "ErrorList constructor payloads should be singleton errors");
370 Payloads.push_back(std::move(Payload1));
371 Payloads.push_back(std::move(Payload2));
380 auto &E1List =
static_cast<ErrorList &
>(*E1.getPtr());
382 auto E2Payload = E2.takePayload();
383 auto &E2List =
static_cast<ErrorList &
>(*E2Payload);
384 for (
auto &Payload : E2List.Payloads)
385 E1List.Payloads.push_back(std::move(Payload));
387 E1List.Payloads.push_back(E2.takePayload());
391 if (E2.
isA<ErrorList>()) {
392 auto &E2List =
static_cast<ErrorList &
>(*E2.getPtr());
393 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
396 return Error(std::unique_ptr<ErrorList>(
397 new ErrorList(E1.takePayload(), E2.takePayload())));
400 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
407 return ErrorList::join(std::move(E1), std::move(E2));
412 template <
typename HandlerT>
415 &std::remove_reference<HandlerT>::type::operator())> {};
421 return E.template isA<ErrT>();
424 template <
typename HandlerT>
425 static Error apply(HandlerT &&
H, std::unique_ptr<ErrorInfoBase> E) {
426 assert(appliesTo(*E) &&
"Applying incorrect handler");
427 return H(static_cast<ErrT &>(*E));
435 return E.template isA<ErrT>();
438 template <
typename HandlerT>
439 static Error apply(HandlerT &&
H, std::unique_ptr<ErrorInfoBase> E) {
440 assert(appliesTo(*E) &&
"Applying incorrect handler");
441 H(static_cast<ErrT &>(*E));
447 template <
typename ErrT>
451 return E.template isA<ErrT>();
454 template <
typename HandlerT>
455 static Error apply(HandlerT &&
H, std::unique_ptr<ErrorInfoBase> E) {
456 assert(appliesTo(*E) &&
"Applying incorrect handler");
457 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
458 return H(std::move(SubE));
463 template <
typename ErrT>
467 return E.template isA<ErrT>();
470 template <
typename HandlerT>
471 static Error apply(HandlerT &&
H, std::unique_ptr<ErrorInfoBase> E) {
472 assert(appliesTo(*E) &&
"Applying incorrect handler");
473 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
480 template <
typename C,
typename RetT,
typename ErrT>
485 template <
typename C,
typename RetT,
typename ErrT>
490 template <
typename C,
typename RetT,
typename ErrT>
495 template <
typename C,
typename RetT,
typename ErrT>
501 template <
typename C,
typename RetT,
typename ErrT>
507 template <
typename C,
typename RetT,
typename ErrT>
512 return Error(std::move(Payload));
515 template <
typename HandlerT,
typename... HandlerTs>
517 HandlerT &&Handler, HandlerTs &&... Handlers) {
522 std::forward<HandlerTs>(Handlers)...);
531 template <
typename... HandlerTs>
536 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
541 for (
auto &
P : List.Payloads)
548 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
554 template <
typename... HandlerTs>
556 auto F =
handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...);
649 static const bool isRef = std::is_reference<T>::value;
653 using error_type = std::unique_ptr<ErrorInfoBase>;
660 using reference =
typename std::remove_reference<T>::type &;
661 using const_reference =
const typename std::remove_reference<T>::type &;
662 using pointer =
typename std::remove_reference<T>::type *;
663 using const_pointer =
const typename std::remove_reference<T>::type *;
669 #
if LLVM_ENABLE_ABI_BREAKING_CHECKS
674 assert(Err &&
"Cannot create Expected<T> from Error success value.");
675 new (getErrorStorage()) error_type(Err.takePayload());
685 template <
typename OtherT>
687 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
690 #
if LLVM_ENABLE_ABI_BREAKING_CHECKS
695 new (getStorage())
storage_type(std::forward<OtherT>(Val));
703 template <
class OtherT>
705 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
707 moveConstruct(std::move(
Other));
712 template <
class OtherT>
715 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
717 moveConstruct(std::move(
Other));
722 moveAssign(std::move(
Other));
730 getStorage()->~storage_type();
732 getErrorStorage()->~error_type();
737 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
738 Unchecked = HasError;
746 return *getStorage();
750 const_reference
get()
const {
757 return HasError && (*getErrorStorage())->
template isA<ErrT>();
765 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
774 return toPointer(getStorage());
780 return toPointer(getStorage());
786 return *getStorage();
792 return *getStorage();
797 static bool compareThisIfSameType(
const T1 &a,
const T1 &b) {
801 template <
class T1,
class T2>
802 static bool compareThisIfSameType(
const T1 &a,
const T2 &b) {
806 template <
class OtherT>
void moveConstruct(Expected<OtherT> &&
Other) {
807 HasError =
Other.HasError;
808 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
810 Other.Unchecked =
false;
814 new (getStorage()) storage_type(std::move(*
Other.getStorage()));
816 new (getErrorStorage()) error_type(std::move(*
Other.getErrorStorage()));
819 template <
class OtherT>
void moveAssign(Expected<OtherT> &&
Other) {
822 if (compareThisIfSameType(*
this,
Other))
826 new (
this) Expected(std::move(
Other));
829 pointer toPointer(pointer Val) {
return Val; }
831 const_pointer toPointer(const_pointer Val)
const {
return Val; }
833 pointer toPointer(
wrap *Val) {
return &Val->get(); }
835 const_pointer toPointer(
const wrap *Val)
const {
return &Val->get(); }
837 storage_type *getStorage() {
838 assert(!HasError &&
"Cannot get value when an error exists!");
839 return reinterpret_cast<storage_type *
>(TStorage.buffer);
842 const storage_type *getStorage()
const {
843 assert(!HasError &&
"Cannot get value when an error exists!");
844 return reinterpret_cast<const storage_type *
>(TStorage.buffer);
847 error_type *getErrorStorage() {
848 assert(HasError &&
"Cannot get error when a value exists!");
849 return reinterpret_cast<error_type *
>(ErrorStorage.buffer);
852 const error_type *getErrorStorage()
const {
853 assert(HasError &&
"Cannot get error when a value exists!");
854 return reinterpret_cast<const error_type *
>(ErrorStorage.buffer);
858 void setUnchecked() {
859 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
864 void assertIsChecked() {
865 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
867 dbgs() <<
"Expected<T> must be checked before access or destruction.\n";
869 dbgs() <<
"Unchecked Expected<T> contained error:\n";
870 (*getErrorStorage())->log(
dbgs());
872 dbgs() <<
"Expected<T> value was in success state. (Note: Expected<T> "
873 "values in success mode must still be checked prior to being "
885 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
893 template <
typename T>
897 : ValOrErr(ValOrErr) {
904 ValOrErr->setUnchecked();
953 if (
auto EC = EO.getError())
955 return std::move(*EO);
960 if (
auto Err = E.takeError())
962 return std::move(*E);
993 ExitOnError(std::string Banner =
"",
int DefaultErrorExitCode = 1)
994 : Banner(std::move(Banner)),
998 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1006 void operator()(
Error Err)
const { checkError(std::move(Err)); }
1010 template <
typename T>
T operator()(Expected<T> &&E)
const {
1011 checkError(E.takeError());
1012 return std::move(*E);
1017 template <
typename T>
T& operator()(Expected<T&> &&E)
const {
1018 checkError(E.takeError());
1023 void checkError(
Error Err)
const {
1038 bool gen_crash_diag =
true);
1071 template <
typename T>
1074 return std::move(*ValOrErr);
1092 template <
typename T>
1102 #endif // LLVM_SUPPORT_ERROR_H
reference operator*()
Returns a reference to the stored T value.
static const void * classID()
void push_back(const T &Elt)
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Error handleErrors(Error E, HandlerTs &&...Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
void cantFail(Error Err)
Report a fatal error if Err is a failure value.
Subclass of Error for the sole purpose of identifying the success path in the type system...
AlignedCharArrayUnion< storage_type > TStorage
Helper for check-and-exit error handling.
static const char * getPtr(const MachOObjectFile &O, size_t Offset)
static bool appliesTo(const ErrorInfoBase &E)
Helper for Expected<T>s used as out-parameters.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
ErrorOr< T > expectedToErrorOr(Expected< T > &&E)
Convert an Expected<T> to an ErrorOr<T>.
pointer operator->()
Returns a pointer to the stored T value.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
bool errorIsA() const
Check that this Expected<T> is an error of type ErrT.
Error takeError()
Take ownership of the stored error.
friend Error handleErrors(Error E, HandlerTs &&...Handlers)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Base class for error info classes.
virtual bool isA(const void *const ClassID) const
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Expected(Expected< OtherT > &&Other, typename std::enable_if<!std::is_convertible< OtherT, T >::value >::type *=nullptr)
Move construct an Expected<T> value from an Expected<OtherT>, where OtherT isn't convertible to T...
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Expected(Expected &&Other)
Move construct an Expected<T> value.
Expected(Expected< OtherT > &&Other, typename std::enable_if< std::is_convertible< OtherT, T >::value >::type *=nullptr)
Move construct an Expected<T> value from an Expected<OtherT>, where OtherT must be convertible to T...
virtual void log(raw_ostream &OS) const =0
Print an error message to an output stream.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
~Expected()
Destroy an Expected<T>.
Tagged union holding either a T or a Error.
virtual ~ErrorInfoBase()=default
const void * dynamicClassID() const override
friend Error joinErrors(Error, Error)
Concatenate errors.
Error & operator=(Error &&Other)
Move-assign an error value.
Error make_error(ArgTs &&...Args)
Make a Error instance representing failure using the given error info type.
std::function< int(const Error &)> GetExitCode
ErrorAsOutParameter(Error *Err)
virtual const void * dynamicClassID() const =0
Function Alias Analysis false
ECError(std::error_code EC)
Expected(Error Err)
Create an Expected<T> error value from the given Error.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
AlignedCharArrayUnion< error_type > ErrorStorage
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
ExitOnError(std::string Banner="", int DefaultErrorExitCode=1)
Create an error on exit helper.
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
bool isA() const
Check whether one error is a subclass of another.
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
void apply(Opt *O, const Mod &M, const Mods &...Ms)
void consumeError(Error Err)
Consume a Error without doing anything.
const std::string & getMessage() const
StringError(const Twine &S, std::error_code EC)
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Special ErrorInfo subclass representing a list of ErrorInfos.
static const void * classID()
const_reference operator*() const
Returns a const reference to the stored T value.
Error(std::unique_ptr< ErrorInfoBase > Payload)
Create an error value.
static ErrorSuccess success()
Create a success value.
static bool appliesTo(const ErrorInfoBase &E)
Expected(OtherT &&Val, typename std::enable_if< std::is_convertible< OtherT, T >::value >::type *=nullptr)
Create an Expected<T> success value from the given OtherT value, which must be convertible to T...
static bool appliesTo(const ErrorInfoBase &E)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
#define LLVM_ATTRIBUTE_NORETURN
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
Expected & operator=(Expected &&Other)
Move-assign from another Expected<T>.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Base class for user error types.
Helper for Errors used as out-parameters.
llvm_unreachable("Unknown RegBank!")
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Error handleErrorImpl(std::unique_ptr< ErrorInfoBase > Payload)
Error(Error &&Other)
Move-construct an error value.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
LLVMAttributeRef wrap(Attribute Attr)
This class wraps a string in an Error.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
bool isA(const void *const ClassID) const override
const void * dynamicClassID() const
Returns the dynamic class id of this error, or null if this is a success value.
void setErrorCode(std::error_code EC)
typename std::conditional< isRef, wrap, T >::type storage_type
Stores a reference that can be changed.
friend Error errorCodeToError(std::error_code)
Helper for converting an std::error_code to a Error.
ExpectedAsOutParameter(Expected< T > *ValOrErr)
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Provides ErrorOr<T> smart pointer.
Error()
Create a success value. Prefer using 'Error::success()' for readability.
static bool appliesTo(const ErrorInfoBase &E)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Lightweight error class with error context and mandatory checking.
This class wraps a std::error_code in a Error.
This class implements an extremely fast bulk output stream that can only output to a stream...
Helper for testing applicability of, and applying, handlers for ErrorInfo types.
virtual std::string message() const
Return the error message as a string.
print Print MemDeps of function
~ExpectedAsOutParameter()
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
const_pointer operator->() const
Returns a const pointer to the stored T value.