mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 22:02:37 +01:00
* Replaced std::exit() with return llvm::Expected/llvm::Error<T> for expected errors like invalid syntax. * Use llvm::formatv() for formatting lexer error messages. x * Addresed merge errors. * Fixed impl scope. * Made ErrorBuilder::operator<< nodiscard, to catch code forgetting 'return' in 'return FATAL_COMPILATION_ERROR()'. * FatalComplationError() -> ParseAndLexContext::RecordLexerError(). Other usages of ERROR_TOKEN in lexer.lpp were actually supposed to be END_OF_FILE. * Update executable_semantics/syntax/parse_and_lex_context.h Co-authored-by: Jon Meow <jperkins@google.com> * Code review fixes. * Update executable_semantics/syntax/parser.ypp Co-authored-by: Jon Meow <jperkins@google.com> * More code review fixes. * Update executable_semantics/interpreter/type_checker.h Co-authored-by: Jon Meow <jperkins@google.com> * Yet more code review fixes... * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review comments * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * Update executable_semantics/syntax/lexer.lpp Co-authored-by: Jon Meow <jperkins@google.com> * code review * code review * Apply suggestions from code review Co-authored-by: Jon Meow <jperkins@google.com> * formatted code * review comments * Switched to the new ErrorOr<V> error implementation * code review comments * fixed comment * restored ostream.h as #976 makes the change unnecesary * review comments Co-authored-by: Jon Meow <jperkins@google.com> Co-authored-by: Geoff Romer <gromer@google.com>
110 lines
2.8 KiB
C++
110 lines
2.8 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef COMMON_ERROR_H_
|
|
#define COMMON_ERROR_H_
|
|
|
|
#include <string>
|
|
|
|
#include "common/check.h"
|
|
#include "common/ostream.h"
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Success values should be represented as the presence of a value in ErrorOr,
|
|
// using `ErrorOr<Success>` and `return Success();` if no value needs to be
|
|
// returned.
|
|
struct Success {};
|
|
|
|
// Tracks an error message.
|
|
class [[nodiscard]] Error {
|
|
public:
|
|
// Represents an error state.
|
|
explicit Error(llvm::Twine message) : message_(message.str()) {
|
|
CHECK(!message_.empty()) << "Errors must have a message.";
|
|
}
|
|
|
|
Error(Error&& other) noexcept : message_(std::move(other.message_)) {}
|
|
|
|
// Prints the error string. Note this marks as used.
|
|
void Print(llvm::raw_ostream& out) const { out << message(); }
|
|
|
|
// Returns the error message.
|
|
auto message() const -> const std::string& { return message_; }
|
|
|
|
private:
|
|
// The error message.
|
|
std::string message_;
|
|
};
|
|
|
|
// Holds a value of type `T`, or an Error explaining why the value is
|
|
// unavailable.
|
|
template <typename T>
|
|
class [[nodiscard]] ErrorOr {
|
|
public:
|
|
// Constructs with an error; the error must not be Error::Success().
|
|
// Implicit for easy construction on returns.
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
ErrorOr(Error err) : val_(std::move(err)) {}
|
|
|
|
// Constructs with a value.
|
|
// Implicit for easy construction on returns.
|
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
ErrorOr(T val) : val_(std::move(val)) {}
|
|
|
|
// Moves held state.
|
|
ErrorOr(ErrorOr&& other) noexcept : val_(std::move(other.val_)) {}
|
|
|
|
// Returns true for success.
|
|
auto ok() const -> bool { return std::holds_alternative<T>(val_); }
|
|
|
|
// Returns the contained error.
|
|
// REQUIRES: `ok()` is false.
|
|
auto error() const& -> const Error& {
|
|
CHECK(!ok());
|
|
return std::get<Error>(val_);
|
|
}
|
|
auto error() && -> Error {
|
|
CHECK(!ok());
|
|
return std::get<Error>(std::move(val_));
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator*() -> T& {
|
|
CHECK(ok());
|
|
return std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator*() const -> const T& {
|
|
CHECK(ok());
|
|
return std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator->() -> T* {
|
|
CHECK(ok());
|
|
return &std::get<T>(val_);
|
|
}
|
|
|
|
// Returns the contained value.
|
|
// REQUIRES: `ok()` is true.
|
|
auto operator->() const -> const T* {
|
|
CHECK(ok());
|
|
return &std::get<T>(val_);
|
|
}
|
|
|
|
private:
|
|
// Either an error message or
|
|
std::variant<Error, T> val_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // COMMON_ERROR_H_
|