14 #ifndef LLVM_SUPPORT_ERROR_H 15 #define LLVM_SUPPORT_ERROR_H 21 #include "llvm/Config/abi-breaking.h" 37 #include <system_error> 38 #include <type_traits> 70 static const void *
classID() {
return &ID; }
77 virtual bool isA(
const void *
const ClassID)
const {
82 template <
typename ErrorInfoT>
bool isA()
const {
83 return isA(ErrorInfoT::classID());
87 virtual void anchor();
163 template <
typename... HandlerTs>
189 *
this = std::move(Other);
194 Error(std::unique_ptr<ErrorInfoBase> Payload) {
195 setPtr(Payload.release());
200 Error &operator=(
const Error &Other) =
delete;
209 setPtr(Other.getPtr());
215 Other.setPtr(
nullptr);
216 Other.setChecked(
true);
232 setChecked(
getPtr() ==
nullptr);
233 return getPtr() !=
nullptr;
237 template <
typename ErrT>
bool isA()
const {
246 return getPtr()->dynamicClassID();
250 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 257 void fatalUncheckedError()
const;
260 void assertIsChecked() {
261 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 263 fatalUncheckedError();
269 reinterpret_cast<uintptr_t
>(Payload) &
270 ~static_cast<uintptr_t>(0x1));
274 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 276 (
reinterpret_cast<uintptr_t
>(EI) &
277 ~static_cast<uintptr_t>(0x1)) |
278 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
284 bool getChecked()
const {
285 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 286 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
292 void setChecked(
bool V) {
294 (
reinterpret_cast<uintptr_t
>(Payload) &
295 ~static_cast<uintptr_t>(0x1)) |
299 std::unique_ptr<ErrorInfoBase> takePayload() {
300 std::unique_ptr<ErrorInfoBase> Tmp(
getPtr());
307 if (
auto P = E.getPtr())
327 return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(
Args)...));
339 template <
typename ThisErrT,
typename ParentErrT = ErrorInfoBase>
346 bool isA(
const void *
const ClassID)
const override {
347 return ClassID ==
classID() || ParentErrT::isA(ClassID);
356 template <
typename... HandlerTs>
364 OS <<
"Multiple errors:\n";
365 for (
auto &ErrPayload : Payloads) {
377 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
378 std::unique_ptr<ErrorInfoBase> Payload2) {
380 "ErrorList constructor payloads should be singleton errors");
381 Payloads.push_back(std::move(Payload1));
382 Payloads.push_back(std::move(Payload2));
391 auto &E1List =
static_cast<ErrorList &
>(*E1.getPtr());
393 auto E2Payload = E2.takePayload();
394 auto &E2List =
static_cast<ErrorList &
>(*E2Payload);
395 for (
auto &Payload : E2List.Payloads)
396 E1List.Payloads.push_back(std::move(Payload));
398 E1List.Payloads.push_back(E2.takePayload());
403 auto &E2List =
static_cast<ErrorList &
>(*E2.getPtr());
404 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
407 return Error(std::unique_ptr<ErrorList>(
408 new ErrorList(E1.takePayload(), E2.takePayload())));
411 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
418 return ErrorList::join(std::move(E1), std::move(E2));
429 template <
class OtherT>
friend class Expected;
431 static const bool isRef = std::is_reference<T>::value;
433 using wrap = std::reference_wrapper<typename std::remove_reference<T>::type>;
435 using error_type = std::unique_ptr<ErrorInfoBase>;
442 using reference =
typename std::remove_reference<T>::type &;
443 using const_reference =
const typename std::remove_reference<T>::type &;
444 using pointer =
typename std::remove_reference<T>::type *;
445 using const_pointer =
const typename std::remove_reference<T>::type *;
451 #
if LLVM_ENABLE_ABI_BREAKING_CHECKS
456 assert(Err &&
"Cannot create Expected<T> from Error success value.");
457 new (getErrorStorage()) error_type(Err.takePayload());
467 template <
typename OtherT>
469 typename std::enable_if<std::is_convertible<OtherT, T>::value>::
type 472 #
if LLVM_ENABLE_ABI_BREAKING_CHECKS
477 new (getStorage())
storage_type(std::forward<OtherT>(Val));
485 template <
class OtherT>
487 typename std::enable_if<std::is_convertible<OtherT, T>::value>::
type 489 moveConstruct(std::move(
Other));
494 template <
class OtherT>
497 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::
type * =
499 moveConstruct(std::move(
Other));
504 moveAssign(std::move(
Other));
512 getStorage()->~storage_type();
514 getErrorStorage()->~error_type();
519 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 520 Unchecked = HasError;
528 return *getStorage();
532 const_reference
get()
const {
539 return HasError && (*getErrorStorage())->
template isA<ErrT>();
547 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 556 return toPointer(getStorage());
562 return toPointer(getStorage());
568 return *getStorage();
574 return *getStorage();
579 static bool compareThisIfSameType(
const T1 &a,
const T1 &b) {
583 template <
class T1,
class T2>
584 static bool compareThisIfSameType(
const T1 &a,
const T2 &b) {
589 HasError =
Other.HasError;
590 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 592 Other.Unchecked =
false;
598 new (getErrorStorage()) error_type(std::move(*
Other.getErrorStorage()));
604 if (compareThisIfSameType(*
this,
Other))
608 new (
this) Expected(std::move(
Other));
611 pointer toPointer(pointer Val) {
return Val; }
613 const_pointer toPointer(const_pointer Val)
const {
return Val; }
615 pointer toPointer(wrap *Val) {
return &Val->get(); }
617 const_pointer toPointer(
const wrap *Val)
const {
return &Val->get(); }
620 assert(!HasError &&
"Cannot get value when an error exists!");
621 return reinterpret_cast<storage_type *
>(TStorage.buffer);
625 assert(!HasError &&
"Cannot get value when an error exists!");
626 return reinterpret_cast<const storage_type *
>(TStorage.buffer);
629 error_type *getErrorStorage() {
630 assert(HasError &&
"Cannot get error when a value exists!");
631 return reinterpret_cast<error_type *
>(ErrorStorage.buffer);
634 const error_type *getErrorStorage()
const {
635 assert(HasError &&
"Cannot get error when a value exists!");
636 return reinterpret_cast<const error_type *
>(ErrorStorage.buffer);
640 void setUnchecked() {
641 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 646 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 649 void fatalUncheckedExpected()
const {
650 dbgs() <<
"Expected<T> must be checked before access or destruction.\n";
652 dbgs() <<
"Unchecked Expected<T> contained error:\n";
653 (*getErrorStorage())->
log(
dbgs());
655 dbgs() <<
"Expected<T> value was in success state. (Note: Expected<T> " 656 "values in success mode must still be checked prior to being " 662 void assertIsChecked() {
663 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 665 fatalUncheckedExpected();
674 #if LLVM_ENABLE_ABI_BREAKING_CHECKS 682 bool gen_crash_diag =
true);
700 Msg =
"Failure value returned from cantFail wrapped call";
718 template <
typename T>
721 return std::move(*ValOrErr);
724 Msg =
"Failure value returned from cantFail wrapped call";
742 template <
typename T>
748 Msg =
"Failure value returned from cantFail wrapped call";
755 template <
typename HandlerT>
758 &std::remove_reference<HandlerT>::type::operator())> {};
764 return E.template isA<ErrT>();
767 template <
typename HandlerT>
769 assert(appliesTo(*E) &&
"Applying incorrect handler");
770 return H(static_cast<ErrT &>(*E));
778 return E.template isA<ErrT>();
781 template <
typename HandlerT>
783 assert(appliesTo(*E) &&
"Applying incorrect handler");
784 H(static_cast<ErrT &>(*E));
790 template <
typename ErrT>
794 return E.template isA<ErrT>();
797 template <
typename HandlerT>
799 assert(appliesTo(*E) &&
"Applying incorrect handler");
800 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
801 return H(std::move(SubE));
806 template <
typename ErrT>
810 return E.template isA<ErrT>();
813 template <
typename HandlerT>
815 assert(appliesTo(*E) &&
"Applying incorrect handler");
816 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
823 template <
typename C,
typename RetT,
typename ErrT>
828 template <
typename C,
typename RetT,
typename ErrT>
833 template <
typename C,
typename RetT,
typename ErrT>
838 template <
typename C,
typename RetT,
typename ErrT>
844 template <
typename C,
typename RetT,
typename ErrT>
850 template <
typename C,
typename RetT,
typename ErrT>
855 return Error(std::move(Payload));
858 template <
typename HandlerT,
typename... HandlerTs>
860 HandlerT &&Handler, HandlerTs &&... Handlers) {
865 std::forward<HandlerTs>(Handlers)...);
874 template <
typename... HandlerTs>
879 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
884 for (
auto &
P : List.Payloads)
891 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
897 template <
typename... HandlerTs>
932 template <
typename T,
typename RecoveryFtor,
typename... HandlerTs>
934 HandlerTs &&... Handlers) {
939 std::forward<HandlerTs>(Handlers)...))
940 return std::move(Err);
942 return RecoveryPath();
981 bool IsError =
static_cast<bool>(Err);
1032 template <
typename T>
1036 : ValOrErr(ValOrErr) {
1043 ValOrErr->setUnchecked();
1092 if (
auto EC = EO.getError())
1094 return std::move(*EO);
1099 if (
auto Err = E.takeError())
1101 return std::move(*E);
1126 template <
typename... Ts>
1128 const Ts &... Vals) {
1131 Stream <<
format(Fmt, Vals...);
1132 return make_error<StringError>(Stream.
str(), EC);
1145 : Banner(
std::move(Banner)),
1146 GetExitCode([=](
const Error &) {
return DefaultErrorExitCode; }) {}
1149 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1153 this->GetExitCode = std::move(GetExitCode);
1162 checkError(E.takeError());
1163 return std::move(*E);
1169 checkError(E.takeError());
1174 void checkError(
Error Err)
const {
1176 int ExitCode = GetExitCode(Err);
1183 std::function<int(const Error &)> GetExitCode;
1188 #endif // LLVM_SUPPORT_ERROR_H reference operator*()
Returns a reference to the stored T value.
static const void * classID()
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
bool errorIsA() const
Check that this Expected<T> is an error of type ErrT.
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 make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
const void * dynamicClassID() const
Returns the dynamic class id of this error, or null if this is a success value.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner)
Log all errors (if any) in E to OS.
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.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
static const char * getPtr(const MachOObjectFile &O, size_t Offset)
T & operator()(Expected< T &> &&E) const
Check E.
static bool appliesTo(const ErrorInfoBase &E)
Helper for Expected<T>s used as out-parameters.
#define LLVM_ATTRIBUTE_NOINLINE
LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, mark a method "not for inl...
virtual std::string message() const
Return the error message as a string.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
ErrorOr< T > expectedToErrorOr(Expected< T > &&E)
Convert an Expected<T> to an ErrorOr<T>.
pointer operator->()
Returns a pointer to the stored T value.
block Block Frequency true
Error takeError()
Take ownership of the stored error.
Expected< T > handleExpected(Expected< T > ValOrErr, RecoveryFtor &&RecoveryPath, HandlerTs &&... Handlers)
Handle any errors (if present) in an Expected<T>, then try a recovery path.
Base class for error info classes.
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 apply(Opt *O, const Mod &M, const Mods &... Ms)
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_reference operator*() const
Returns a const reference to the stored T value.
const void * dynamicClassID() const override
Error & operator=(Error &&Other)
Move-assign an error value.
ErrorAsOutParameter(Error *Err)
virtual const void * dynamicClassID() const =0
ECError(std::error_code EC)
void setExitCodeMapper(std::function< int(const Error &)> GetExitCode)
Set the exit-code mapper function.
const_pointer operator->() const
Returns a const pointer to the stored T value.
Expected(Error Err)
Create an Expected<T> error value from the given Error.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
void operator()(Error Err) const
Check Err. If it's in a failure state log the error(s) and exit.
AlignedCharArrayUnion< error_type > ErrorStorage
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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>.
#define LLVM_ATTRIBUTE_NORETURN
#define LLVM_UNLIKELY(EXPR)
void consumeError(Error Err)
Consume a Error without doing anything.
const std::string & getMessage() const
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Special ErrorInfo subclass representing a list of ErrorInfos.
static const void * classID()
Error(std::unique_ptr< ErrorInfoBase > Payload)
Create an error value.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
static ErrorSuccess success()
Create a success value.
static bool appliesTo(const ErrorInfoBase &E)
friend raw_ostream & operator<<(raw_ostream &OS, const Error &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...
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
T operator()(Expected< T > &&E) const
Check E.
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.
void setBanner(std::string Banner)
Set the banner string for any errors caught by operator().
Base class for user error types.
Helper for Errors used as out-parameters.
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()
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.
bool isA(const void *const ClassID) const override
bool isA() const
Check whether one error is a subclass of another.
void setErrorCode(std::error_code EC)
typename std::conditional< isRef, wrap, T >::type storage_type
ExpectedAsOutParameter(Expected< T > *ValOrErr)
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.
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
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.
print Print MemDeps of function
~ExpectedAsOutParameter()
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
virtual bool isA(const void *const ClassID) const
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...