xref: /openbsd/gnu/llvm/compiler-rt/lib/orc/error.h (revision 810390e3)
1d89ec533Spatrick //===-------- Error.h - Enforced error checking for ORC RT ------*- C++ -*-===//
2d89ec533Spatrick //
3d89ec533Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d89ec533Spatrick // See https://llvm.org/LICENSE.txt for license information.
5d89ec533Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d89ec533Spatrick //
7d89ec533Spatrick //===----------------------------------------------------------------------===//
8d89ec533Spatrick 
9d89ec533Spatrick #ifndef ORC_RT_ERROR_H
10d89ec533Spatrick #define ORC_RT_ERROR_H
11d89ec533Spatrick 
12d89ec533Spatrick #include "compiler.h"
13d89ec533Spatrick #include "extensible_rtti.h"
14d89ec533Spatrick #include "stl_extras.h"
15d89ec533Spatrick 
16d89ec533Spatrick #include <cassert>
17d89ec533Spatrick #include <memory>
18d89ec533Spatrick #include <string>
19d89ec533Spatrick #include <type_traits>
20d89ec533Spatrick 
21d89ec533Spatrick namespace __orc_rt {
22d89ec533Spatrick 
23d89ec533Spatrick /// Base class for all errors.
24d89ec533Spatrick class ErrorInfoBase : public RTTIExtends<ErrorInfoBase, RTTIRoot> {
25d89ec533Spatrick public:
26d89ec533Spatrick   virtual std::string toString() const = 0;
27d89ec533Spatrick };
28d89ec533Spatrick 
29d89ec533Spatrick /// Represents an environmental error.
30d89ec533Spatrick class ORC_RT_NODISCARD Error {
31d89ec533Spatrick 
32d89ec533Spatrick   template <typename ErrT, typename... ArgTs>
33d89ec533Spatrick   friend Error make_error(ArgTs &&...Args);
34d89ec533Spatrick 
35d89ec533Spatrick   friend Error repackage_error(std::unique_ptr<ErrorInfoBase>);
36d89ec533Spatrick 
37d89ec533Spatrick   template <typename ErrT> friend std::unique_ptr<ErrT> error_cast(Error &);
38d89ec533Spatrick 
39d89ec533Spatrick   template <typename T> friend class Expected;
40d89ec533Spatrick 
41d89ec533Spatrick public:
42d89ec533Spatrick   /// Destroy this error. Aborts if error was not checked, or was checked but
43d89ec533Spatrick   /// not handled.
~Error()44d89ec533Spatrick   ~Error() { assertIsChecked(); }
45d89ec533Spatrick 
46d89ec533Spatrick   Error(const Error &) = delete;
47d89ec533Spatrick   Error &operator=(const Error &) = delete;
48d89ec533Spatrick 
49d89ec533Spatrick   /// Move-construct an error. The newly constructed error is considered
50d89ec533Spatrick   /// unchecked, even if the source error had been checked. The original error
51d89ec533Spatrick   /// becomes a checked success value.
Error(Error && Other)52d89ec533Spatrick   Error(Error &&Other) {
53d89ec533Spatrick     setChecked(true);
54d89ec533Spatrick     *this = std::move(Other);
55d89ec533Spatrick   }
56d89ec533Spatrick 
57d89ec533Spatrick   /// Move-assign an error value. The current error must represent success, you
58d89ec533Spatrick   /// you cannot overwrite an unhandled error. The current error is then
59d89ec533Spatrick   /// considered unchecked. The source error becomes a checked success value,
60d89ec533Spatrick   /// regardless of its original state.
61d89ec533Spatrick   Error &operator=(Error &&Other) {
62d89ec533Spatrick     // Don't allow overwriting of unchecked values.
63d89ec533Spatrick     assertIsChecked();
64d89ec533Spatrick     setPtr(Other.getPtr());
65d89ec533Spatrick 
66d89ec533Spatrick     // This Error is unchecked, even if the source error was checked.
67d89ec533Spatrick     setChecked(false);
68d89ec533Spatrick 
69d89ec533Spatrick     // Null out Other's payload and set its checked bit.
70d89ec533Spatrick     Other.setPtr(nullptr);
71d89ec533Spatrick     Other.setChecked(true);
72d89ec533Spatrick 
73d89ec533Spatrick     return *this;
74d89ec533Spatrick   }
75d89ec533Spatrick 
76d89ec533Spatrick   /// Create a success value.
success()77d89ec533Spatrick   static Error success() { return Error(); }
78d89ec533Spatrick 
79d89ec533Spatrick   /// Error values convert to true for failure values, false otherwise.
80d89ec533Spatrick   explicit operator bool() {
81d89ec533Spatrick     setChecked(getPtr() == nullptr);
82d89ec533Spatrick     return getPtr() != nullptr;
83d89ec533Spatrick   }
84d89ec533Spatrick 
85d89ec533Spatrick   /// Return true if this Error contains a failure value of the given type.
isA()86d89ec533Spatrick   template <typename ErrT> bool isA() const {
87d89ec533Spatrick     return getPtr() && getPtr()->isA<ErrT>();
88d89ec533Spatrick   }
89d89ec533Spatrick 
90d89ec533Spatrick private:
91d89ec533Spatrick   Error() = default;
92d89ec533Spatrick 
Error(std::unique_ptr<ErrorInfoBase> ErrInfo)93d89ec533Spatrick   Error(std::unique_ptr<ErrorInfoBase> ErrInfo) {
94d89ec533Spatrick     auto RawErrPtr = reinterpret_cast<uintptr_t>(ErrInfo.release());
95d89ec533Spatrick     assert((RawErrPtr & 0x1) == 0 && "ErrorInfo is insufficiently aligned");
96d89ec533Spatrick     ErrPtr = RawErrPtr | 0x1;
97d89ec533Spatrick   }
98d89ec533Spatrick 
assertIsChecked()99d89ec533Spatrick   void assertIsChecked() {
100d89ec533Spatrick     if (ORC_RT_UNLIKELY(!isChecked() || getPtr())) {
101d89ec533Spatrick       fprintf(stderr, "Error must be checked prior to destruction.\n");
102d89ec533Spatrick       abort(); // Some sort of JIT program abort?
103d89ec533Spatrick     }
104d89ec533Spatrick   }
105d89ec533Spatrick 
getPtr()106d89ec533Spatrick   template <typename ErrT = ErrorInfoBase> ErrT *getPtr() const {
107d89ec533Spatrick     return reinterpret_cast<ErrT *>(ErrPtr & ~uintptr_t(1));
108d89ec533Spatrick   }
109d89ec533Spatrick 
setPtr(ErrorInfoBase * Ptr)110d89ec533Spatrick   void setPtr(ErrorInfoBase *Ptr) {
111d89ec533Spatrick     ErrPtr = (reinterpret_cast<uintptr_t>(Ptr) & ~uintptr_t(1)) | (ErrPtr & 1);
112d89ec533Spatrick   }
113d89ec533Spatrick 
isChecked()114d89ec533Spatrick   bool isChecked() const { return ErrPtr & 0x1; }
115d89ec533Spatrick 
setChecked(bool Checked)116*810390e3Srobert   void setChecked(bool Checked) { ErrPtr = (ErrPtr & ~uintptr_t(1)) | Checked; }
117d89ec533Spatrick 
takePayload()118d89ec533Spatrick   template <typename ErrT = ErrorInfoBase> std::unique_ptr<ErrT> takePayload() {
119d89ec533Spatrick     static_assert(std::is_base_of<ErrorInfoBase, ErrT>::value,
120d89ec533Spatrick                   "ErrT is not an ErrorInfoBase subclass");
121d89ec533Spatrick     std::unique_ptr<ErrT> Tmp(getPtr<ErrT>());
122d89ec533Spatrick     setPtr(nullptr);
123d89ec533Spatrick     setChecked(true);
124d89ec533Spatrick     return Tmp;
125d89ec533Spatrick   }
126d89ec533Spatrick 
127d89ec533Spatrick   uintptr_t ErrPtr = 0;
128d89ec533Spatrick };
129d89ec533Spatrick 
130d89ec533Spatrick /// Construct an error of ErrT with the given arguments.
make_error(ArgTs &&...Args)131d89ec533Spatrick template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&...Args) {
132d89ec533Spatrick   static_assert(std::is_base_of<ErrorInfoBase, ErrT>::value,
133d89ec533Spatrick                 "ErrT is not an ErrorInfoBase subclass");
134d89ec533Spatrick   return Error(std::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
135d89ec533Spatrick }
136d89ec533Spatrick 
137d89ec533Spatrick /// Construct an error of ErrT using a std::unique_ptr<ErrorInfoBase>. The
138d89ec533Spatrick /// primary use-case for this is 're-packaging' errors after inspecting them
139d89ec533Spatrick /// using error_cast, hence the name.
repackage_error(std::unique_ptr<ErrorInfoBase> EIB)140d89ec533Spatrick inline Error repackage_error(std::unique_ptr<ErrorInfoBase> EIB) {
141d89ec533Spatrick   return Error(std::move(EIB));
142d89ec533Spatrick }
143d89ec533Spatrick 
144d89ec533Spatrick /// If the argument is an error of type ErrT then this function unpacks it
145d89ec533Spatrick /// and returns a std::unique_ptr<ErrT>. Otherwise returns a nullptr and
146d89ec533Spatrick /// leaves the error untouched. Common usage looks like:
147d89ec533Spatrick ///
148d89ec533Spatrick /// \code{.cpp}
149d89ec533Spatrick ///   if (Error E = foo()) {
150d89ec533Spatrick ///     if (auto EV1 = error_cast<ErrorType1>(E)) {
151d89ec533Spatrick ///       // use unwrapped EV1 value.
152d89ec533Spatrick ///     } else if (EV2 = error_cast<ErrorType2>(E)) {
153d89ec533Spatrick ///       // use unwrapped EV2 value.
154d89ec533Spatrick ///     } ...
155d89ec533Spatrick ///   }
156d89ec533Spatrick /// \endcode
error_cast(Error & Err)157d89ec533Spatrick template <typename ErrT> std::unique_ptr<ErrT> error_cast(Error &Err) {
158d89ec533Spatrick   static_assert(std::is_base_of<ErrorInfoBase, ErrT>::value,
159d89ec533Spatrick                 "ErrT is not an ErrorInfoBase subclass");
160d89ec533Spatrick   if (Err.isA<ErrT>())
161d89ec533Spatrick     return Err.takePayload<ErrT>();
162d89ec533Spatrick   return nullptr;
163d89ec533Spatrick }
164d89ec533Spatrick 
165d89ec533Spatrick /// Helper for Errors used as out-parameters.
166d89ec533Spatrick /// Sets the 'checked' flag on construction, resets it on destruction.
167d89ec533Spatrick class ErrorAsOutParameter {
168d89ec533Spatrick public:
ErrorAsOutParameter(Error * Err)169d89ec533Spatrick   ErrorAsOutParameter(Error *Err) : Err(Err) {
170d89ec533Spatrick     // Raise the checked bit if Err is success.
171d89ec533Spatrick     if (Err)
172d89ec533Spatrick       (void)!!*Err;
173d89ec533Spatrick   }
174d89ec533Spatrick 
~ErrorAsOutParameter()175d89ec533Spatrick   ~ErrorAsOutParameter() {
176d89ec533Spatrick     // Clear the checked bit.
177d89ec533Spatrick     if (Err && !*Err)
178d89ec533Spatrick       *Err = Error::success();
179d89ec533Spatrick   }
180d89ec533Spatrick 
181d89ec533Spatrick private:
182d89ec533Spatrick   Error *Err;
183d89ec533Spatrick };
184d89ec533Spatrick 
185d89ec533Spatrick template <typename T> class ORC_RT_NODISCARD Expected {
186d89ec533Spatrick 
187d89ec533Spatrick   template <class OtherT> friend class Expected;
188d89ec533Spatrick 
189d89ec533Spatrick   static constexpr bool IsRef = std::is_reference<T>::value;
190d89ec533Spatrick   using wrap = std::reference_wrapper<std::remove_reference_t<T>>;
191d89ec533Spatrick   using error_type = std::unique_ptr<ErrorInfoBase>;
192d89ec533Spatrick   using storage_type = std::conditional_t<IsRef, wrap, T>;
193d89ec533Spatrick   using value_type = T;
194d89ec533Spatrick 
195d89ec533Spatrick   using reference = std::remove_reference_t<T> &;
196d89ec533Spatrick   using const_reference = const std::remove_reference_t<T> &;
197d89ec533Spatrick   using pointer = std::remove_reference_t<T> *;
198d89ec533Spatrick   using const_pointer = const std::remove_reference_t<T> *;
199d89ec533Spatrick 
200d89ec533Spatrick public:
201d89ec533Spatrick   /// Create an Expected from a failure value.
Expected(Error Err)202d89ec533Spatrick   Expected(Error Err) : HasError(true), Unchecked(true) {
203d89ec533Spatrick     assert(Err && "Cannot create Expected<T> from Error success value");
204d89ec533Spatrick     new (getErrorStorage()) error_type(Err.takePayload());
205d89ec533Spatrick   }
206d89ec533Spatrick 
207d89ec533Spatrick   /// Create an Expected from a T value.
208d89ec533Spatrick   template <typename OtherT>
209d89ec533Spatrick   Expected(OtherT &&Val,
210d89ec533Spatrick            std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
HasError(false)211d89ec533Spatrick       : HasError(false), Unchecked(true) {
212d89ec533Spatrick     new (getStorage()) storage_type(std::forward<OtherT>(Val));
213d89ec533Spatrick   }
214d89ec533Spatrick 
215d89ec533Spatrick   /// Move-construct an Expected<T> from an Expected<OtherT>.
Expected(Expected && Other)216d89ec533Spatrick   Expected(Expected &&Other) { moveConstruct(std::move(Other)); }
217d89ec533Spatrick 
218d89ec533Spatrick   /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
219d89ec533Spatrick   /// must be convertible to T.
220d89ec533Spatrick   template <class OtherT>
221d89ec533Spatrick   Expected(
222d89ec533Spatrick       Expected<OtherT> &&Other,
223d89ec533Spatrick       std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr) {
224d89ec533Spatrick     moveConstruct(std::move(Other));
225d89ec533Spatrick   }
226d89ec533Spatrick 
227d89ec533Spatrick   /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
228d89ec533Spatrick   /// isn't convertible to T.
229d89ec533Spatrick   template <class OtherT>
230d89ec533Spatrick   explicit Expected(
231d89ec533Spatrick       Expected<OtherT> &&Other,
232d89ec533Spatrick       std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr) {
233d89ec533Spatrick     moveConstruct(std::move(Other));
234d89ec533Spatrick   }
235d89ec533Spatrick 
236d89ec533Spatrick   /// Move-assign from another Expected<T>.
237d89ec533Spatrick   Expected &operator=(Expected &&Other) {
238d89ec533Spatrick     moveAssign(std::move(Other));
239d89ec533Spatrick     return *this;
240d89ec533Spatrick   }
241d89ec533Spatrick 
242d89ec533Spatrick   /// Destroy an Expected<T>.
~Expected()243d89ec533Spatrick   ~Expected() {
244d89ec533Spatrick     assertIsChecked();
245d89ec533Spatrick     if (!HasError)
246d89ec533Spatrick       getStorage()->~storage_type();
247d89ec533Spatrick     else
248d89ec533Spatrick       getErrorStorage()->~error_type();
249d89ec533Spatrick   }
250d89ec533Spatrick 
251d89ec533Spatrick   /// Returns true if this Expected value is in a success state (holding a T),
252d89ec533Spatrick   /// and false if this Expected value is in a failure state.
253d89ec533Spatrick   explicit operator bool() {
254d89ec533Spatrick     Unchecked = HasError;
255d89ec533Spatrick     return !HasError;
256d89ec533Spatrick   }
257d89ec533Spatrick 
258d89ec533Spatrick   /// Returns true if this Expected value holds an Error of type error_type.
isFailureOfType()259d89ec533Spatrick   template <typename ErrT> bool isFailureOfType() const {
260d89ec533Spatrick     return HasError && (*getErrorStorage())->template isFailureOfType<ErrT>();
261d89ec533Spatrick   }
262d89ec533Spatrick 
263d89ec533Spatrick   /// Take ownership of the stored error.
264d89ec533Spatrick   ///
265d89ec533Spatrick   /// If this Expected value is in a success state (holding a T) then this
266d89ec533Spatrick   /// method is a no-op and returns Error::success.
267d89ec533Spatrick   ///
268d89ec533Spatrick   /// If thsi Expected value is in a failure state (holding an Error) then this
269d89ec533Spatrick   /// method returns the contained error and leaves this Expected in an
270d89ec533Spatrick   /// 'empty' state from which it may be safely destructed but not otherwise
271d89ec533Spatrick   /// accessed.
takeError()272d89ec533Spatrick   Error takeError() {
273d89ec533Spatrick     Unchecked = false;
274d89ec533Spatrick     return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
275d89ec533Spatrick   }
276d89ec533Spatrick 
277d89ec533Spatrick   /// Returns a pointer to the stored T value.
278d89ec533Spatrick   pointer operator->() {
279d89ec533Spatrick     assertIsChecked();
280d89ec533Spatrick     return toPointer(getStorage());
281d89ec533Spatrick   }
282d89ec533Spatrick 
283d89ec533Spatrick   /// Returns a pointer to the stored T value.
284d89ec533Spatrick   const_pointer operator->() const {
285d89ec533Spatrick     assertIsChecked();
286d89ec533Spatrick     return toPointer(getStorage());
287d89ec533Spatrick   }
288d89ec533Spatrick 
289d89ec533Spatrick   /// Returns a reference to the stored T value.
290d89ec533Spatrick   reference operator*() {
291d89ec533Spatrick     assertIsChecked();
292d89ec533Spatrick     return *getStorage();
293d89ec533Spatrick   }
294d89ec533Spatrick 
295d89ec533Spatrick   /// Returns a reference to the stored T value.
296d89ec533Spatrick   const_reference operator*() const {
297d89ec533Spatrick     assertIsChecked();
298d89ec533Spatrick     return *getStorage();
299d89ec533Spatrick   }
300d89ec533Spatrick 
301d89ec533Spatrick private:
302d89ec533Spatrick   template <class T1>
compareThisIfSameType(const T1 & a,const T1 & b)303d89ec533Spatrick   static bool compareThisIfSameType(const T1 &a, const T1 &b) {
304d89ec533Spatrick     return &a == &b;
305d89ec533Spatrick   }
306d89ec533Spatrick 
307d89ec533Spatrick   template <class T1, class T2>
compareThisIfSameType(const T1 & a,const T2 & b)308d89ec533Spatrick   static bool compareThisIfSameType(const T1 &a, const T2 &b) {
309d89ec533Spatrick     return false;
310d89ec533Spatrick   }
311d89ec533Spatrick 
moveConstruct(Expected<OtherT> && Other)312d89ec533Spatrick   template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) {
313d89ec533Spatrick     HasError = Other.HasError;
314d89ec533Spatrick     Unchecked = true;
315d89ec533Spatrick     Other.Unchecked = false;
316d89ec533Spatrick 
317d89ec533Spatrick     if (!HasError)
318d89ec533Spatrick       new (getStorage()) storage_type(std::move(*Other.getStorage()));
319d89ec533Spatrick     else
320d89ec533Spatrick       new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
321d89ec533Spatrick   }
322d89ec533Spatrick 
moveAssign(Expected<OtherT> && Other)323d89ec533Spatrick   template <class OtherT> void moveAssign(Expected<OtherT> &&Other) {
324d89ec533Spatrick     assertIsChecked();
325d89ec533Spatrick 
326d89ec533Spatrick     if (compareThisIfSameType(*this, Other))
327d89ec533Spatrick       return;
328d89ec533Spatrick 
329d89ec533Spatrick     this->~Expected();
330d89ec533Spatrick     new (this) Expected(std::move(Other));
331d89ec533Spatrick   }
332d89ec533Spatrick 
toPointer(pointer Val)333d89ec533Spatrick   pointer toPointer(pointer Val) { return Val; }
334d89ec533Spatrick 
toPointer(const_pointer Val)335d89ec533Spatrick   const_pointer toPointer(const_pointer Val) const { return Val; }
336d89ec533Spatrick 
toPointer(wrap * Val)337d89ec533Spatrick   pointer toPointer(wrap *Val) { return &Val->get(); }
338d89ec533Spatrick 
toPointer(const wrap * Val)339d89ec533Spatrick   const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
340d89ec533Spatrick 
getStorage()341d89ec533Spatrick   storage_type *getStorage() {
342d89ec533Spatrick     assert(!HasError && "Cannot get value when an error exists!");
343d89ec533Spatrick     return reinterpret_cast<storage_type *>(&TStorage);
344d89ec533Spatrick   }
345d89ec533Spatrick 
getStorage()346d89ec533Spatrick   const storage_type *getStorage() const {
347d89ec533Spatrick     assert(!HasError && "Cannot get value when an error exists!");
348d89ec533Spatrick     return reinterpret_cast<const storage_type *>(&TStorage);
349d89ec533Spatrick   }
350d89ec533Spatrick 
getErrorStorage()351d89ec533Spatrick   error_type *getErrorStorage() {
352d89ec533Spatrick     assert(HasError && "Cannot get error when a value exists!");
353d89ec533Spatrick     return reinterpret_cast<error_type *>(&ErrorStorage);
354d89ec533Spatrick   }
355d89ec533Spatrick 
getErrorStorage()356d89ec533Spatrick   const error_type *getErrorStorage() const {
357d89ec533Spatrick     assert(HasError && "Cannot get error when a value exists!");
358d89ec533Spatrick     return reinterpret_cast<const error_type *>(&ErrorStorage);
359d89ec533Spatrick   }
360d89ec533Spatrick 
assertIsChecked()361d89ec533Spatrick   void assertIsChecked() {
362d89ec533Spatrick     if (ORC_RT_UNLIKELY(Unchecked)) {
363d89ec533Spatrick       fprintf(stderr,
364d89ec533Spatrick               "Expected<T> must be checked before access or destruction.\n");
365d89ec533Spatrick       abort();
366d89ec533Spatrick     }
367d89ec533Spatrick   }
368d89ec533Spatrick 
369d89ec533Spatrick   union {
370d89ec533Spatrick     std::aligned_union_t<1, storage_type> TStorage;
371d89ec533Spatrick     std::aligned_union_t<1, error_type> ErrorStorage;
372d89ec533Spatrick   };
373d89ec533Spatrick 
374d89ec533Spatrick   bool HasError : 1;
375d89ec533Spatrick   bool Unchecked : 1;
376d89ec533Spatrick };
377d89ec533Spatrick 
378d89ec533Spatrick /// Consume an error without doing anything.
consumeError(Error Err)379d89ec533Spatrick inline void consumeError(Error Err) {
380d89ec533Spatrick   if (Err)
381d89ec533Spatrick     (void)error_cast<ErrorInfoBase>(Err);
382d89ec533Spatrick }
383d89ec533Spatrick 
384d89ec533Spatrick /// Consumes success values. It is a programmatic error to call this function
385d89ec533Spatrick /// on a failure value.
cantFail(Error Err)386d89ec533Spatrick inline void cantFail(Error Err) {
387d89ec533Spatrick   assert(!Err && "cantFail called on failure value");
388d89ec533Spatrick   consumeError(std::move(Err));
389d89ec533Spatrick }
390d89ec533Spatrick 
391d89ec533Spatrick /// Auto-unwrap an Expected<T> value in the success state. It is a programmatic
392d89ec533Spatrick /// error to call this function on a failure value.
cantFail(Expected<T> E)393d89ec533Spatrick template <typename T> T cantFail(Expected<T> E) {
394d89ec533Spatrick   assert(E && "cantFail called on failure value");
395d89ec533Spatrick   consumeError(E.takeError());
396d89ec533Spatrick   return std::move(*E);
397d89ec533Spatrick }
398d89ec533Spatrick 
399d89ec533Spatrick /// Auto-unwrap an Expected<T> value in the success state. It is a programmatic
400d89ec533Spatrick /// error to call this function on a failure value.
cantFail(Expected<T &> E)401d89ec533Spatrick template <typename T> T &cantFail(Expected<T &> E) {
402d89ec533Spatrick   assert(E && "cantFail called on failure value");
403d89ec533Spatrick   consumeError(E.takeError());
404d89ec533Spatrick   return *E;
405d89ec533Spatrick }
406d89ec533Spatrick 
407d89ec533Spatrick /// Convert the given error to a string. The error value is consumed in the
408d89ec533Spatrick /// process.
toString(Error Err)409d89ec533Spatrick inline std::string toString(Error Err) {
410d89ec533Spatrick   if (auto EIB = error_cast<ErrorInfoBase>(Err))
411d89ec533Spatrick     return EIB->toString();
412d89ec533Spatrick   return {};
413d89ec533Spatrick }
414d89ec533Spatrick 
415d89ec533Spatrick class StringError : public RTTIExtends<StringError, ErrorInfoBase> {
416d89ec533Spatrick public:
StringError(std::string ErrMsg)417d89ec533Spatrick   StringError(std::string ErrMsg) : ErrMsg(std::move(ErrMsg)) {}
toString()418d89ec533Spatrick   std::string toString() const override { return ErrMsg; }
419d89ec533Spatrick 
420d89ec533Spatrick private:
421d89ec533Spatrick   std::string ErrMsg;
422d89ec533Spatrick };
423d89ec533Spatrick 
424d89ec533Spatrick } // end namespace __orc_rt
425d89ec533Spatrick 
426d89ec533Spatrick #endif // ORC_RT_ERROR_H
427