1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/assert.h"
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/expected.h"
// All the constants in this file come from http://switchbrew.org/index.php?title=Error_codes
/**
* Identifies the module which caused the error. Error codes can be propagated through a call
* chain, meaning that this doesn't always correspond to the module where the API call made is
* contained.
*/
enum class ErrorModule : u32 {
Common = 0,
Kernel = 1,
FS = 2,
OS = 3, // used for Memory, Thread, Mutex, Nvidia
HTCS = 4,
NCM = 5,
DD = 6,
LR = 8,
Loader = 9,
CMIF = 10,
HIPC = 11,
TMA = 12,
DMNT = 13,
GDS = 14,
PM = 15,
NS = 16,
BSDSockets = 17,
HTC = 18,
TSC = 19,
NCMContent = 20,
SM = 21,
RO = 22,
GC = 23,
SDMMC = 24,
OVLN = 25,
SPL = 26,
Socket = 27,
HTCLOW = 29,
DDSF = 30,
HTCFS = 31,
Async = 32,
Util = 33,
TIPC = 35,
ANIF = 37,
ETHC = 100,
I2C = 101,
GPIO = 102,
UART = 103,
CPAD = 104,
Settings = 105,
FTM = 106,
WLAN = 107,
XCD = 108,
TMP451 = 109,
NIFM = 110,
HwOpus = 111,
LSM6DS3 = 112,
Bluetooth = 113,
VI = 114,
NFP = 115,
Time = 116,
FGM = 117,
OE = 118,
BH1730FVC = 119,
PCIe = 120,
Friends = 121,
BCAT = 122,
SSLSrv = 123,
Account = 124,
News = 125,
Mii = 126,
NFC = 127,
AM = 128,
PlayReport = 129,
AHID = 130,
Qlaunch = 132,
PCV = 133,
USBPD = 134,
BPC = 135,
PSM = 136,
NIM = 137,
PSC = 138,
TC = 139,
USB = 140,
NSD = 141,
PCTL = 142,
BTM = 143,
LA = 144,
ETicket = 145,
NGC = 146,
ERPT = 147,
APM = 148,
CEC = 149,
Profiler = 150,
ErrorUpload = 151,
LIDBE = 152,
Audio = 153,
NPNS = 154,
NPNSHTTPSTREAM = 155,
ARP = 157,
SWKBD = 158,
BOOT = 159,
NetDiag = 160,
NFCMifare = 161,
UserlandAssert = 162,
Fatal = 163,
NIMShop = 164,
SPSM = 165,
BGTC = 167,
UserlandCrash = 168,
SASBUS = 169,
PI = 170,
AudioCtrl = 172,
LBL = 173,
JIT = 175,
HDCP = 176,
OMM = 177,
PDM = 178,
OLSC = 179,
SREPO = 180,
Dauth = 181,
STDFU = 182,
DBG = 183,
DHCPS = 186,
SPI = 187,
AVM = 188,
PWM = 189,
RTC = 191,
Regulator = 192,
LED = 193,
SIO = 195,
PCM = 196,
CLKRST = 197,
POWCTL = 198,
AudioOld = 201,
HID = 202,
LDN = 203,
CS = 204,
Irsensor = 205,
Capture = 206,
Manu = 208,
ATK = 209,
WEB = 210,
LCS = 211,
GRC = 212,
Repair = 213,
Album = 214,
RID = 215,
Migration = 216,
MigrationLdcServ = 217,
HIDBUS = 218,
ENS = 219,
WebSocket = 223,
DCDMTP = 227,
PGL = 228,
Notification = 229,
INS = 230,
LP2P = 231,
RCD = 232,
LCM40607 = 233,
PRC = 235,
TMAHTC = 237,
ECTX = 238,
MNPP = 239,
HSHL = 240,
CAPMTP = 242,
DP2HDMI = 244,
Cradle = 245,
SProfile = 246,
NDRM = 250,
TSPM = 499,
DevMenu = 500,
GeneralWebApplet = 800,
WifiWebAuthApplet = 809,
WhitelistedApplet = 810,
ShopN = 811,
};
/// Encapsulates a Horizon OS error code, allowing it to be separated into its constituent fields.
union Result {
u32 raw;
using Module = BitField<0, 9, ErrorModule>;
using Description = BitField<9, 13, u32>;
Result() = default;
constexpr explicit Result(u32 raw_) : raw(raw_) {}
constexpr Result(ErrorModule module_, u32 description_)
: raw(Module::FormatValue(module_) | Description::FormatValue(description_)) {}
[[nodiscard]] constexpr bool IsSuccess() const {
return raw == 0;
}
[[nodiscard]] constexpr bool IsError() const {
return !IsSuccess();
}
[[nodiscard]] constexpr bool IsFailure() const {
return !IsSuccess();
}
[[nodiscard]] constexpr u32 GetInnerValue() const {
return raw;
}
[[nodiscard]] constexpr ErrorModule GetModule() const {
return Module::ExtractValue(raw);
}
[[nodiscard]] constexpr u32 GetDescription() const {
return Description::ExtractValue(raw);
}
[[nodiscard]] constexpr bool Includes(Result result) const {
return GetInnerValue() == result.GetInnerValue();
}
};
static_assert(std::is_trivial_v<Result>);
[[nodiscard]] constexpr bool operator==(const Result& a, const Result& b) {
return a.raw == b.raw;
}
[[nodiscard]] constexpr bool operator!=(const Result& a, const Result& b) {
return !operator==(a, b);
}
// Convenience functions for creating some common kinds of errors:
/// The default success `Result`.
constexpr Result ResultSuccess(0);
/**
* Placeholder result code used for unknown error codes.
*
* @note This should only be used when a particular error code
* is not known yet.
*/
constexpr Result ResultUnknown(UINT32_MAX);
/**
* A ResultRange defines an inclusive range of error descriptions within an error module.
* This can be used to check whether the description of a given Result falls within the range.
* The conversion function returns a Result with its description set to description_start.
*
* An example of how it could be used:
* \code
* constexpr ResultRange ResultCommonError{ErrorModule::Common, 0, 9999};
*
* Result Example(int value) {
* const Result result = OtherExample(value);
*
* // This will only evaluate to true if result.module is ErrorModule::Common and
* // result.description is in between 0 and 9999 inclusive.
* if (ResultCommonError.Includes(result)) {
* // This returns Result{ErrorModule::Common, 0};
* return ResultCommonError;
* }
*
* return ResultSuccess;
* }
* \endcode
*/
class ResultRange {
public:
consteval ResultRange(ErrorModule module, u32 description_start, u32 description_end_)
: code{module, description_start}, description_end{description_end_} {}
[[nodiscard]] constexpr operator Result() const {
return code;
}
[[nodiscard]] constexpr bool Includes(Result other) const {
return code.GetModule() == other.GetModule() &&
code.GetDescription() <= other.GetDescription() &&
other.GetDescription() <= description_end;
}
private:
Result code;
u32 description_end;
};
#define R_SUCCEEDED(res) (static_cast<Result>(res).IsSuccess())
#define R_FAILED(res) (static_cast<Result>(res).IsFailure())
namespace ResultImpl {
template <auto EvaluateResult, class F>
class ScopedResultGuard {
YUZU_NON_COPYABLE(ScopedResultGuard);
YUZU_NON_MOVEABLE(ScopedResultGuard);
private:
Result& m_ref;
F m_f;
public:
constexpr ScopedResultGuard(Result& ref, F f) : m_ref(ref), m_f(std::move(f)) {}
constexpr ~ScopedResultGuard() {
if (EvaluateResult(m_ref)) {
m_f();
}
}
};
template <auto EvaluateResult>
class ResultReferenceForScopedResultGuard {
private:
Result& m_ref;
public:
constexpr ResultReferenceForScopedResultGuard(Result& r) : m_ref(r) {}
constexpr operator Result&() const {
return m_ref;
}
};
template <auto EvaluateResult, typename F>
constexpr ScopedResultGuard<EvaluateResult, F> operator+(
ResultReferenceForScopedResultGuard<EvaluateResult> ref, F&& f) {
return ScopedResultGuard<EvaluateResult, F>(static_cast<Result&>(ref), std::forward<F>(f));
}
constexpr bool EvaluateResultSuccess(const Result& r) {
return R_SUCCEEDED(r);
}
constexpr bool EvaluateResultFailure(const Result& r) {
return R_FAILED(r);
}
template <auto... R>
constexpr bool EvaluateAnyResultIncludes(const Result& r) {
return ((r == R) || ...);
}
template <auto... R>
constexpr bool EvaluateResultNotIncluded(const Result& r) {
return !EvaluateAnyResultIncludes<R...>(r);
}
template <typename T>
constexpr void UpdateCurrentResultReference(T result_reference, Result result) = delete;
// Intentionally not defined
template <>
constexpr void UpdateCurrentResultReference<Result&>(Result& result_reference, Result result) {
result_reference = result;
}
template <>
constexpr void UpdateCurrentResultReference<const Result>(Result result_reference, Result result) {}
} // namespace ResultImpl
#define DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(COUNTER_VALUE) \
[[maybe_unused]] constexpr bool CONCAT2(HasPrevRef_, COUNTER_VALUE) = \
std::same_as<decltype(__TmpCurrentResultReference), Result&>; \
[[maybe_unused]] Result CONCAT2(PrevRef_, COUNTER_VALUE) = __TmpCurrentResultReference; \
[[maybe_unused]] Result CONCAT2(__tmp_result_, COUNTER_VALUE) = ResultSuccess; \
Result& __TmpCurrentResultReference = CONCAT2(HasPrevRef_, COUNTER_VALUE) \
? CONCAT2(PrevRef_, COUNTER_VALUE) \
: CONCAT2(__tmp_result_, COUNTER_VALUE)
#define ON_RESULT_RETURN_IMPL(...) \
static_assert(std::same_as<decltype(__TmpCurrentResultReference), Result&>); \
auto CONCAT2(RESULT_GUARD_STATE_, __COUNTER__) = \
ResultImpl::ResultReferenceForScopedResultGuard<__VA_ARGS__>( \
__TmpCurrentResultReference) + \
[&]()
#define ON_RESULT_FAILURE_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultFailure)
#define ON_RESULT_FAILURE \
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
ON_RESULT_FAILURE_2
#define ON_RESULT_SUCCESS_2 ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateResultSuccess)
#define ON_RESULT_SUCCESS \
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
ON_RESULT_SUCCESS_2
#define ON_RESULT_INCLUDED_2(...) \
ON_RESULT_RETURN_IMPL(ResultImpl::EvaluateAnyResultIncludes<__VA_ARGS__>)
#define ON_RESULT_INCLUDED(...) \
DECLARE_CURRENT_RESULT_REFERENCE_AND_STORAGE(__COUNTER__); \
ON_RESULT_INCLUDED_2(__VA_ARGS__)
constexpr inline Result __TmpCurrentResultReference = ResultSuccess;
/// Returns a result.
#define R_RETURN(res_expr) \
{ \
const Result _tmp_r_throw_rc = (res_expr); \
ResultImpl::UpdateCurrentResultReference<decltype(__TmpCurrentResultReference)>( \
__TmpCurrentResultReference, _tmp_r_throw_rc); \
return _tmp_r_throw_rc; \
}
/// Returns ResultSuccess()
#define R_SUCCEED() R_RETURN(ResultSuccess)
/// Throws a result.
#define R_THROW(res_expr) R_RETURN(res_expr)
/// Evaluates a boolean expression, and returns a result unless that expression is true.
#define R_UNLESS(expr, res) \
{ \
if (!(expr)) { \
R_THROW(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)) { \
R_THROW(_tmp_r_try_rc); \
} \
}
/// Evaluates a boolean expression, and succeeds if that expression is true.
#define R_SUCCEED_IF(expr) R_UNLESS(!(expr), ResultSuccess)
#define R_TRY_CATCH(res_expr) \
{ \
const auto R_CURRENT_RESULT = (res_expr); \
if (R_FAILED(R_CURRENT_RESULT)) { \
if (false)
#define R_END_TRY_CATCH \
else if (R_FAILED(R_CURRENT_RESULT)) { \
R_THROW(R_CURRENT_RESULT); \
} \
} \
}
#define R_CATCH_ALL() \
} \
else if (R_FAILED(R_CURRENT_RESULT)) { \
if (true)
#define R_CATCH(res_expr) \
} \
else if ((res_expr) == (R_CURRENT_RESULT)) { \
if (true)
#define R_CONVERT(catch_type, convert_type) \
R_CATCH(catch_type) { R_THROW(static_cast<Result>(convert_type)); }
#define R_CONVERT_ALL(convert_type) \
R_CATCH_ALL() { R_THROW(static_cast<Result>(convert_type)); }
#define R_ASSERT(res_expr) ASSERT(R_SUCCEEDED(res_expr))
|