kernel: Update to use atmosphere macros and correct Result (#7242)

* kernel: Switch to atmosphere style macros

* code: Rename ResultCode to Result

* code: Result constants are lower case

* Address review comments

* core: Remove CASCADE_CODE

* R_TRY replaces completely

* core: Run clang format
This commit is contained in:
GPUCode 2023-12-31 19:01:40 +02:00 committed by GitHub
parent 811303ea54
commit 5a7f615da1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
132 changed files with 2807 additions and 2995 deletions

View file

@ -192,7 +192,7 @@ enum class ErrorLevel : u32 {
};
/// Encapsulates a CTR-OS error code, allowing it to be separated into its constituent fields.
union ResultCode {
union Result {
u32 raw;
BitField<0, 10, u32> description;
@ -205,18 +205,18 @@ union ResultCode {
// error
BitField<31, 1, u32> is_error;
constexpr explicit ResultCode(u32 raw) : raw(raw) {}
constexpr explicit Result(u32 raw) : raw(raw) {}
constexpr ResultCode(ErrorDescription description, ErrorModule module, ErrorSummary summary,
ErrorLevel level)
: ResultCode(static_cast<u32>(description), module, summary, level) {}
constexpr Result(ErrorDescription description, ErrorModule module, ErrorSummary summary,
ErrorLevel level)
: Result(static_cast<u32>(description), module, summary, level) {}
constexpr ResultCode(u32 description_, ErrorModule module_, ErrorSummary summary_,
ErrorLevel level_)
constexpr Result(u32 description_, ErrorModule module_, ErrorSummary summary_,
ErrorLevel level_)
: raw(description.FormatValue(description_) | module.FormatValue(module_) |
summary.FormatValue(summary_) | level.FormatValue(level_)) {}
constexpr ResultCode& operator=(const ResultCode& o) = default;
constexpr Result& operator=(const Result& o) = default;
constexpr bool IsSuccess() const {
return is_error.ExtractValue(raw) == 0;
@ -234,23 +234,23 @@ private:
friend class boost::serialization::access;
};
constexpr bool operator==(const ResultCode& a, const ResultCode& b) {
constexpr bool operator==(const Result& a, const Result& b) {
return a.raw == b.raw;
}
constexpr bool operator!=(const ResultCode& a, const ResultCode& b) {
constexpr bool operator!=(const Result& a, const Result& b) {
return a.raw != b.raw;
}
// Convenience functions for creating some common kinds of errors:
/// The default success `ResultCode`.
constexpr ResultCode RESULT_SUCCESS(0);
/// The default success `Result`.
constexpr Result ResultSuccess(0);
/// Might be returned instead of a dummy success for unimplemented APIs.
constexpr ResultCode UnimplementedFunction(ErrorModule module) {
return ResultCode(ErrorDescription::NotImplemented, module, ErrorSummary::NotSupported,
ErrorLevel::Permanent);
constexpr Result UnimplementedFunction(ErrorModule module) {
return Result(ErrorDescription::NotImplemented, module, ErrorSummary::NotSupported,
ErrorLevel::Permanent);
}
/**
@ -259,10 +259,10 @@ constexpr ResultCode UnimplementedFunction(ErrorModule module) {
* @note This should only be used when a particular error code
* is not known yet.
*/
constexpr ResultCode RESULT_UNKNOWN(UINT32_MAX);
constexpr Result ResultUnknown(std::numeric_limits<u32>::max());
/**
* This is an optional value type. It holds a `ResultCode` and, if that code is ResultSuccess, it
* This is an optional value type. It holds a `Result` and, if that code is ResultSuccess, it
* also holds a result of type `T`. If the code is an error code (not ResultSuccess), then trying
* to access the inner value with operator* is undefined behavior and will assert with Unwrap().
* Users of this class must be cognizant to check the status of the ResultVal with operator bool(),
@ -273,7 +273,7 @@ constexpr ResultCode RESULT_UNKNOWN(UINT32_MAX);
* ResultVal<int> Frobnicate(float strength) {
* if (strength < 0.f || strength > 1.0f) {
* // Can't frobnicate too weakly or too strongly
* return ResultCode(ErrorDescription::OutOfRange, ErrorModule::Common,
* return Result(ErrorDescription::OutOfRange, ErrorModule::Common,
* ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
* } else {
* // Frobnicated! Give caller a cookie
@ -297,7 +297,7 @@ class ResultVal {
public:
constexpr ResultVal() : expected{} {}
constexpr ResultVal(ResultCode code) : expected{Common::Unexpected(code)} {}
constexpr ResultVal(Result code) : expected{Common::Unexpected(code)} {}
template <typename U>
constexpr ResultVal(U&& val) : expected{std::forward<U>(val)} {}
@ -317,8 +317,8 @@ public:
return expected.has_value();
}
[[nodiscard]] constexpr ResultCode Code() const {
return expected.has_value() ? RESULT_SUCCESS : expected.error();
[[nodiscard]] constexpr Result Code() const {
return expected.has_value() ? ResultSuccess : expected.error();
}
[[nodiscard]] constexpr bool Succeeded() const {
@ -385,7 +385,7 @@ public:
private:
// TODO: Replace this with std::expected once it is standardized in the STL.
Common::Expected<T, ResultCode> expected;
Common::Expected<T, Result> expected;
};
/**
@ -400,11 +400,28 @@ private:
return CONCAT2(check_result_L, __LINE__).Code(); \
target = std::move(*CONCAT2(check_result_L, __LINE__))
/**
* Analogous to CASCADE_RESULT, but for a bare ResultCode. The code will be propagated if
* non-success, or discarded otherwise.
*/
#define CASCADE_CODE(source) \
auto CONCAT2(check_result_L, __LINE__) = source; \
if (CONCAT2(check_result_L, __LINE__).IsError()) \
return CONCAT2(check_result_L, __LINE__);
#define R_SUCCEEDED(res) (static_cast<Result>(res).IsSuccess())
#define R_FAILED(res) (static_cast<Result>(res).IsError())
/// Evaluates a boolean expression, and returns a result unless that expression is true.
#define R_UNLESS(expr, res) \
{ \
if (!(expr)) { \
return (res); \
} \
}
/// Evaluates an expression that returns a result, and returns the result if it would fail.
#define R_TRY(res_expr) \
{ \
const auto _tmp_r_try_rc = (res_expr); \
if (R_FAILED(_tmp_r_try_rc)) { \
return (_tmp_r_try_rc); \
} \
}
/// Evaluates a boolean expression, and succeeds if that expression is true.
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess)
/// Evaluates a boolean expression, and asserts if that expression is false.
#define R_ASSERT(expr) ASSERT(R_SUCCEEDED(expr))