error.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef COMMON_ERROR_H_
  5. #define COMMON_ERROR_H_
  6. #include <string>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "llvm/ADT/Twine.h"
  10. namespace Carbon {
  11. // Success values should be represented as the presence of a value in ErrorOr,
  12. // using `ErrorOr<Success>` and `return Success();` if no value needs to be
  13. // returned.
  14. struct Success {};
  15. // Tracks an error message.
  16. class [[nodiscard]] Error {
  17. public:
  18. // Represents an error state.
  19. explicit Error(llvm::Twine message) : message_(message.str()) {
  20. CHECK(!message_.empty()) << "Errors must have a message.";
  21. }
  22. Error(Error&& other) noexcept : message_(std::move(other.message_)) {}
  23. // Prints the error string. Note this marks as used.
  24. void Print(llvm::raw_ostream& out) const { out << message(); }
  25. // Returns the error message.
  26. auto message() const -> const std::string& { return message_; }
  27. private:
  28. // The error message.
  29. std::string message_;
  30. };
  31. // Holds a value of type `T`, or an Error explaining why the value is
  32. // unavailable.
  33. template <typename T>
  34. class [[nodiscard]] ErrorOr {
  35. public:
  36. // Constructs with an error; the error must not be Error::Success().
  37. // Implicit for easy construction on returns.
  38. // NOLINTNEXTLINE(google-explicit-constructor)
  39. ErrorOr(Error err) : val_(std::move(err)) {}
  40. // Constructs with a value.
  41. // Implicit for easy construction on returns.
  42. // NOLINTNEXTLINE(google-explicit-constructor)
  43. ErrorOr(T val) : val_(std::move(val)) {}
  44. // Moves held state.
  45. ErrorOr(ErrorOr&& other) noexcept : val_(std::move(other.val_)) {}
  46. // Returns true for success.
  47. auto ok() const -> bool { return std::holds_alternative<T>(val_); }
  48. // Returns the contained error.
  49. // REQUIRES: `ok()` is false.
  50. auto error() const& -> const Error& {
  51. CHECK(!ok());
  52. return std::get<Error>(val_);
  53. }
  54. auto error() && -> Error {
  55. CHECK(!ok());
  56. return std::get<Error>(std::move(val_));
  57. }
  58. // Returns the contained value.
  59. // REQUIRES: `ok()` is true.
  60. auto operator*() -> T& {
  61. CHECK(ok());
  62. return std::get<T>(val_);
  63. }
  64. // Returns the contained value.
  65. // REQUIRES: `ok()` is true.
  66. auto operator*() const -> const T& {
  67. CHECK(ok());
  68. return std::get<T>(val_);
  69. }
  70. // Returns the contained value.
  71. // REQUIRES: `ok()` is true.
  72. auto operator->() -> T* {
  73. CHECK(ok());
  74. return &std::get<T>(val_);
  75. }
  76. // Returns the contained value.
  77. // REQUIRES: `ok()` is true.
  78. auto operator->() const -> const T* {
  79. CHECK(ok());
  80. return &std::get<T>(val_);
  81. }
  82. private:
  83. // Either an error message or
  84. std::variant<Error, T> val_;
  85. };
  86. // A helper class for accumulating error message and converting to
  87. // `Error` and `ErrorOr<T>`.
  88. class ErrorBuilder {
  89. public:
  90. ErrorBuilder() : out_(std::make_unique<llvm::raw_string_ostream>(message_)) {}
  91. // Accumulates string message.
  92. template <typename T>
  93. [[nodiscard]] auto operator<<(const T& message) -> ErrorBuilder& {
  94. *out_ << message;
  95. return *this;
  96. }
  97. // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
  98. operator Error() { return Error(message_); }
  99. template <typename T>
  100. // NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
  101. operator ErrorOr<T>() {
  102. return Error(message_);
  103. }
  104. private:
  105. std::string message_;
  106. // Use a pointer to allow move construction.
  107. std::unique_ptr<llvm::raw_string_ostream> out_;
  108. };
  109. } // namespace Carbon
  110. // Macro hackery to get a unique variable name.
  111. #define MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
  112. #define MAKE_UNIQUE_NAME(a, b, c) MAKE_UNIQUE_NAME_IMPL(a, b, c)
  113. #define RETURN_IF_ERROR_IMPL(unique_name, expr) \
  114. if (auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
  115. !(unique_name).ok()) { \
  116. return std::move(unique_name).error(); \
  117. }
  118. #define RETURN_IF_ERROR(expr) \
  119. RETURN_IF_ERROR_IMPL( \
  120. MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), expr)
  121. #define ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
  122. auto unique_name = (expr); /* NOLINT(bugprone-macro-parentheses) */ \
  123. if (!(unique_name).ok()) { \
  124. return std::move(unique_name).error(); \
  125. } \
  126. var = std::move(*(unique_name)); /* NOLINT(bugprone-macro-parentheses) */
  127. #define ASSIGN_OR_RETURN(var, expr) \
  128. ASSIGN_OR_RETURN_IMPL( \
  129. MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), var, expr)
  130. #endif // COMMON_ERROR_H_