//===-------- error.h - Enforced error checking for ORC RT ------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef ORC_RT_ERROR_H #define ORC_RT_ERROR_H #include "compiler.h" #include "extensible_rtti.h" #include "stl_extras.h" #include <cassert> #include <memory> #include <string> #include <type_traits> namespace __orc_rt { /// Base class for all errors. class ErrorInfoBase : public RTTIExtends<ErrorInfoBase, RTTIRoot> { … }; /// Represents an environmental error. class ORC_RT_NODISCARD Error { … }; /// Construct an error of ErrT with the given arguments. template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&...Args) { … } /// Construct an error of ErrT using a std::unique_ptr<ErrorInfoBase>. The /// primary use-case for this is 're-packaging' errors after inspecting them /// using error_cast, hence the name. inline Error repackage_error(std::unique_ptr<ErrorInfoBase> EIB) { … } /// If the argument is an error of type ErrT then this function unpacks it /// and returns a std::unique_ptr<ErrT>. Otherwise returns a nullptr and /// leaves the error untouched. Common usage looks like: /// /// \code{.cpp} /// if (Error E = foo()) { /// if (auto EV1 = error_cast<ErrorType1>(E)) { /// // use unwrapped EV1 value. /// } else if (EV2 = error_cast<ErrorType2>(E)) { /// // use unwrapped EV2 value. /// } ... /// } /// \endcode template <typename ErrT> std::unique_ptr<ErrT> error_cast(Error &Err) { … } /// Helper for Errors used as out-parameters. /// Sets the 'checked' flag on construction, resets it on destruction. class ErrorAsOutParameter { … }; template <typename T> class ORC_RT_NODISCARD Expected { … }; /// Consume an error without doing anything. inline void consumeError(Error Err) { … } /// Consumes success values. It is a programmatic error to call this function /// on a failure value. inline void cantFail(Error Err) { … } /// Auto-unwrap an Expected<T> value in the success state. It is a programmatic /// error to call this function on a failure value. template <typename T> T cantFail(Expected<T> E) { … } /// Auto-unwrap an Expected<T> value in the success state. It is a programmatic /// error to call this function on a failure value. template <typename T> T &cantFail(Expected<T &> E) { … } /// Convert the given error to a string. The error value is consumed in the /// process. inline std::string toString(Error Err) { … } class StringError : public RTTIExtends<StringError, ErrorInfoBase> { … }; } // end namespace __orc_rt #endif // ORC_RT_ERROR_H