aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/Support/Error.h
blob: 63c0b2f6b4de06867f978243c04396222cfaa7c1 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_SUPPORT_ERROR_H
#define RCC_SUPPORT_ERROR_H

#include <cstdint>
#include <expected>
#include <string>
#include <string_view>
#include <utility>

namespace rcc {

enum class ErrorCode : std::uint8_t {
  InvalidArgument,
  ParseError,
  VerificationError,
  Unsupported,
  RegisterAllocationFailed,
  IoError,
  InternalError,
  FrameLayoutFailed,
  SchedulingFailed,
  BranchRelaxationFailed,
  PseudoExpansionFailed,
  FinalEncodabilityFailed,
  CFGOptimizationFailed,
  AssemblyLoweringFailed,
  InstructionEncodingFailed,
  FixupApplicationFailed,
  RelocationFailed,
  ObjectWritingFailed,
};

[[nodiscard]] std::string_view getErrorCodeName(ErrorCode Code) noexcept;

class Error final {
public:
  Error(ErrorCode Code, std::string Message);

  [[nodiscard]] ErrorCode code() const noexcept {
    return Code;
  }
  [[nodiscard]] std::string_view message() const noexcept {
    return Message;
  }

private:
  ErrorCode Code;
  std::string Message;
};

template <typename ValueT> using Expected = std::expected<ValueT, Error>;

[[nodiscard]] inline std::unexpected<Error> makeUnexpected(ErrorCode Code, std::string Message) {
  return std::unexpected<Error>(Error(Code, std::move(Message)));
}

} // namespace rcc

#endif // RCC_SUPPORT_ERROR_H