error.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Returns the contained value.
  55. // REQUIRES: `ok()` is true.
  56. auto operator*() -> T& {
  57. CHECK(ok());
  58. return std::get<T>(val_);
  59. }
  60. // Returns the contained value.
  61. // REQUIRES: `ok()` is true.
  62. auto operator*() const -> const T& {
  63. CHECK(ok());
  64. return std::get<T>(val_);
  65. }
  66. // Returns the contained value.
  67. // REQUIRES: `ok()` is true.
  68. auto operator->() -> T* {
  69. CHECK(ok());
  70. return &std::get<T>(val_);
  71. }
  72. // Returns the contained value.
  73. // REQUIRES: `ok()` is true.
  74. auto operator->() const -> const T* {
  75. CHECK(ok());
  76. return &std::get<T>(val_);
  77. }
  78. private:
  79. // Either an error message or
  80. std::variant<Error, T> val_;
  81. };
  82. } // namespace Carbon
  83. #endif // COMMON_ERROR_H_